| 1 | /* $Header: d:/cvsroot/tads/tads3/VMSTR.H,v 1.2 1999/05/17 02:52:28 MJRoberts Exp $ */ |
| 2 | |
| 3 | /* |
| 4 | * Copyright (c) 1998, 2002 Michael J. Roberts. All Rights Reserved. |
| 5 | * |
| 6 | * Please see the accompanying license file, LICENSE.TXT, for information |
| 7 | * on using and copying this software. |
| 8 | */ |
| 9 | /* |
| 10 | Name |
| 11 | vmstr.h - VM dynamic string implementation |
| 12 | Function |
| 13 | |
| 14 | Notes |
| 15 | |
| 16 | Modified |
| 17 | 10/28/98 MJRoberts - Creation |
| 18 | */ |
| 19 | |
| 20 | #ifndef VMSTR_H |
| 21 | #define VMSTR_H |
| 22 | |
| 23 | #include "vmglob.h" |
| 24 | #include "vmobj.h" |
| 25 | |
| 26 | |
| 27 | /* |
| 28 | * String object |
| 29 | */ |
| 30 | class CVmObjString: public CVmObject |
| 31 | { |
| 32 | friend class CVmMetaclassString; |
| 33 | |
| 34 | public: |
| 35 | /* metaclass registration object */ |
| 36 | static class CVmMetaclass *metaclass_reg_; |
| 37 | class CVmMetaclass *get_metaclass_reg() const { return metaclass_reg_; } |
| 38 | |
| 39 | /* am I of the given metaclass? */ |
| 40 | virtual int is_of_metaclass(class CVmMetaclass *meta) const |
| 41 | { |
| 42 | /* try my own metaclass and my base class */ |
| 43 | return (meta == metaclass_reg_ |
| 44 | || CVmObject::is_of_metaclass(meta)); |
| 45 | } |
| 46 | |
| 47 | /* is the given object reference a string object? */ |
| 48 | static int is_string_obj(VMG_ vm_obj_id_t obj) |
| 49 | { return vm_objp(vmg_ obj)->is_of_metaclass(metaclass_reg_); } |
| 50 | |
| 51 | /* create from stack arguments */ |
| 52 | static vm_obj_id_t create_from_stack(VMG_ const uchar **pc_ptr, |
| 53 | uint argc); |
| 54 | |
| 55 | /* reserve constant data */ |
| 56 | virtual void reserve_const_data(VMG_ class CVmConstMapper *mapper, |
| 57 | vm_obj_id_t self); |
| 58 | |
| 59 | /* convert to constant data */ |
| 60 | virtual void convert_to_const_data(VMG_ class CVmConstMapper *mapper, |
| 61 | vm_obj_id_t self); |
| 62 | |
| 63 | /* get my datatype when converted to constant data */ |
| 64 | virtual vm_datatype_t get_convert_to_const_data_type() const |
| 65 | { return VM_SSTRING; } |
| 66 | |
| 67 | /* create a string with no initial contents */ |
| 68 | static vm_obj_id_t create(VMG_ int in_root_set); |
| 69 | |
| 70 | /* create a string to hold a string of the given byte length */ |
| 71 | static vm_obj_id_t create(VMG_ int in_root_set, size_t bytelen); |
| 72 | |
| 73 | /* create from a constant UTF-8 string */ |
| 74 | static vm_obj_id_t create(VMG_ int in_root_set, |
| 75 | const char *str, size_t bytelen); |
| 76 | |
| 77 | /* create from a wide unicode string */ |
| 78 | static vm_obj_id_t create(VMG_ int in_root_set, |
| 79 | const wchar_t *str, size_t charlen); |
| 80 | |
| 81 | /* create from a byte stream, treating the bytes as Latin-1 characters */ |
| 82 | static vm_obj_id_t create_latin1(VMG_ int in_root_set, |
| 83 | class CVmDataSource *src); |
| 84 | |
| 85 | /* create from a string, mapping through the given character mapper */ |
| 86 | static vm_obj_id_t create(VMG_ int in_root_set, |
| 87 | const char *str, size_t bytelen, |
| 88 | CCharmapToUni *cmap); |
| 89 | |
| 90 | /* |
| 91 | * For construction: get a pointer to the string's underlying |
| 92 | * buffer. Returns a pointer into which the caller can write. The |
| 93 | * buffer starts after the length prefix. |
| 94 | */ |
| 95 | char *cons_get_buf() const { return ext_ + 2; } |
| 96 | |
| 97 | /* get the current construction length in bytes */ |
| 98 | size_t cons_get_len() const { return vmb_get_len(ext_); } |
| 99 | |
| 100 | /* |
| 101 | * Ensure space for a string under construction. This expands the |
| 102 | * buffer if necessary. 'ptr' is a pointer into our buffer, giving the |
| 103 | * current write position, and 'len' is the amount of data we want to |
| 104 | * add. If we already have enough space, we'll simply return 'ptr'. |
| 105 | * If not, we'll reallocate the buffer with enough space for the added |
| 106 | * chunk plus the given overhead margin, and return a new pointer that |
| 107 | * points to the same offset in the new buffer as 'ptr' did in the |
| 108 | * original. |
| 109 | */ |
| 110 | char *cons_ensure_space(VMG_ char *ptr, size_t len, size_t margin); |
| 111 | |
| 112 | /* |
| 113 | * Append a string to the buffer during construction, expanding the |
| 114 | * buffer if necessary. 'ptr' is a pointer into our buffer to where |
| 115 | * the string is to be appended. Returns the updated pointer to the |
| 116 | * end of the appended data. |
| 117 | */ |
| 118 | char *cons_append(VMG_ char *ptr, const char *addstr, size_t addlen, |
| 119 | size_t margin); |
| 120 | |
| 121 | /* append a character to the buffer during construction */ |
| 122 | char *cons_append(VMG_ char *ptr, wchar_t ch, size_t margin); |
| 123 | |
| 124 | /* |
| 125 | * Shrink the buffer to the given actual size to finalize construction. |
| 126 | * 'ptr' is a pointer into the construction buffer; we'll reallocate |
| 127 | * the buffer so that it's just big enough for the text up to the byte |
| 128 | * before 'ptr', and set the string length accordingly. |
| 129 | * |
| 130 | * If the buffer is within a reasonable margin of the actual used size, |
| 131 | * we'll ignore the reallocation request. Reallocation takes some work |
| 132 | * (to find new memory and copy the string contents), so if the memory |
| 133 | * savings won't be substantial, it's more efficient just to waste the |
| 134 | * memory. In all likelihood the string will be collected as garbage |
| 135 | * eventually anyway, so the extra space will probably only be tied up |
| 136 | * temporarily. |
| 137 | * |
| 138 | * The caller must not call cons_set_len() prior to calling this, |
| 139 | * because we take the current length from our internal length element. |
| 140 | */ |
| 141 | void cons_shrink_buffer(VMG_ char *ptr); |
| 142 | |
| 143 | /* |
| 144 | * For construction: set my length. This can be used if the string |
| 145 | * stored is smaller than the buffer allocated. This cannot be used to |
| 146 | * expand the buffer, since this merely writes the length prefix and |
| 147 | * does not reallocate the buffer. |
| 148 | * |
| 149 | * When used with a pointer, the pointer must point within our buffer; |
| 150 | * we'll set the length according to the offset of the pointer from the |
| 151 | * start of the buffer. This can be used for dynamic construction with |
| 152 | * the final write pointer after filling in the buffer's contents. |
| 153 | */ |
| 154 | void cons_set_len(size_t len) { vmb_put_len(ext_, len); } |
| 155 | void cons_set_len(char *ptr) { vmb_put_len(ext_, ptr - ext_ - 2); } |
| 156 | |
| 157 | /* notify of deletion */ |
| 158 | void notify_delete(VMG_ int in_root_set); |
| 159 | |
| 160 | /* set a property */ |
| 161 | void set_prop(VMG_ class CVmUndo *undo, |
| 162 | vm_obj_id_t self, vm_prop_id_t prop, const vm_val_t *val); |
| 163 | |
| 164 | /* call a static property */ |
| 165 | static int call_stat_prop(VMG_ vm_val_t *result, |
| 166 | const uchar **pc_ptr, uint *argc, |
| 167 | vm_prop_id_t prop); |
| 168 | |
| 169 | /* undo operations - strings are immutable and hence keep no undo */ |
| 170 | void notify_new_savept() { } |
| 171 | void apply_undo(VMG_ struct CVmUndoRecord *) { }; |
| 172 | void mark_undo_ref(VMG_ struct CVmUndoRecord *) { } |
| 173 | void remove_stale_undo_weak_ref(VMG_ struct CVmUndoRecord *) { } |
| 174 | |
| 175 | /* reference operations - strings reference no other objects */ |
| 176 | void mark_refs(VMG_ uint state) { } |
| 177 | void remove_stale_weak_refs(VMG0_) { } |
| 178 | |
| 179 | /* load from an image file */ |
| 180 | void load_from_image(VMG_ vm_obj_id_t, const char *ptr, size_t) |
| 181 | { ext_ = (char *)ptr; } |
| 182 | |
| 183 | /* rebuild for image file */ |
| 184 | virtual ulong rebuild_image(VMG_ char *buf, ulong buflen); |
| 185 | |
| 186 | /* save to a file */ |
| 187 | void save_to_file(VMG_ class CVmFile *fp); |
| 188 | |
| 189 | /* restore from a file */ |
| 190 | void restore_from_file(VMG_ vm_obj_id_t self, |
| 191 | class CVmFile *fp, class CVmObjFixup *fixups); |
| 192 | |
| 193 | /* |
| 194 | * add a value to the string -- this creates a new string by |
| 195 | * appending the value to this string |
| 196 | */ |
| 197 | int add_val(VMG_ vm_val_t *result, |
| 198 | vm_obj_id_t self, const vm_val_t *val); |
| 199 | |
| 200 | /* |
| 201 | * Get a string representation of the object. This is trivial for a |
| 202 | * string object - we simply return our extension, which contains |
| 203 | * the string in the required format. |
| 204 | */ |
| 205 | const char *cast_to_string(VMG_ vm_obj_id_t self, |
| 206 | vm_val_t *new_str) const |
| 207 | { |
| 208 | /* we are the string object */ |
| 209 | new_str->set_obj(self); |
| 210 | |
| 211 | /* return our extension directly */ |
| 212 | return ext_; |
| 213 | } |
| 214 | |
| 215 | /* convert a value to string via reflection services in the bytecode */ |
| 216 | static const char *reflect_to_str( |
| 217 | VMG_ vm_val_t *new_str, char *result_buf, size_t result_buf_size, |
| 218 | const vm_val_t *val, const char *fmt, ...); |
| 219 | |
| 220 | /* get the underlying string */ |
| 221 | const char *get_as_string(VMG0_) const { return ext_; } |
| 222 | |
| 223 | /* cast to integer */ |
| 224 | virtual long cast_to_int(VMG0_) const; |
| 225 | |
| 226 | /* cast to number */ |
| 227 | virtual void cast_to_num(VMG_ vm_val_t *val, vm_obj_id_t self) const; |
| 228 | |
| 229 | /* parse a string as an integer value or (optionally) a BigNumber */ |
| 230 | static void parse_num_val(VMG_ vm_val_t *retval, |
| 231 | const char *str, size_t len, |
| 232 | int radix, int int_only); |
| 233 | |
| 234 | /* |
| 235 | * Static routine to add a value to a string constant. Creates a |
| 236 | * new string by appending the given value to the given string |
| 237 | * constant. The string constant must be stored in portable format: |
| 238 | * the first two bytes are the length prefix, in UINT2 format, |
| 239 | * giving the length of the string's contents not counting the |
| 240 | * prefix itself; immediately following the length prefix are the |
| 241 | * bytes of the string's contents. |
| 242 | */ |
| 243 | static void add_to_str(VMG_ vm_val_t *result, |
| 244 | const vm_val_t *self, const vm_val_t *val); |
| 245 | |
| 246 | /* |
| 247 | * Check a value for equality. We will match any constant string |
| 248 | * that contains the same text as our string, and any other string |
| 249 | * object with the same text. |
| 250 | */ |
| 251 | int equals(VMG_ vm_obj_id_t self, const vm_val_t *val, int depth) const; |
| 252 | |
| 253 | /* |
| 254 | * Compare the string to another value. If the other value is a |
| 255 | * constant string or string object, we'll perform a lexical |
| 256 | * comparison of the string; other types are not comparable to |
| 257 | * strings, so we'll throw an error for any other type. |
| 258 | */ |
| 259 | int compare_to(VMG_ vm_obj_id_t self, const vm_val_t *val) const; |
| 260 | |
| 261 | /* calculate a hash */ |
| 262 | uint calc_hash(VMG_ vm_obj_id_t self, int depth) const; |
| 263 | |
| 264 | /* |
| 265 | * Convert a value to a string. Throws an error if the value is not |
| 266 | * convertible to a string. |
| 267 | * |
| 268 | * The result is stored in the given buffer, if possible, in portable |
| 269 | * string format (with a portable UINT2 length prefix followed by the |
| 270 | * string's bytes). If the buffer is not provided or is not large |
| 271 | * enough to contain the result, we will allocate a new string object |
| 272 | * and return its contents; since the string object will never be |
| 273 | * referenced by anyone, it will be deleted in the next garbage |
| 274 | * collection pass. In any case, we will return a pointer to a buffer |
| 275 | * containing the result string. |
| 276 | * |
| 277 | * We'll fill in *new_obj with the new string object value, or nil if |
| 278 | * we don't create a new string; this allows the caller to protect the |
| 279 | * allocated object from garbage collection if necessary. |
| 280 | * |
| 281 | * 'flags' is a combination of TOSTR_xxx values (see vmobj.h). |
| 282 | */ |
| 283 | static const char *cvt_to_str(VMG_ vm_val_t *new_obj, |
| 284 | char *result_buf, size_t result_buf_size, |
| 285 | const vm_val_t *val, |
| 286 | int radix, int flags); |
| 287 | |
| 288 | /* |
| 289 | * Convert an integer to a string, storing the result in the given |
| 290 | * buffer in portable string format (with length prefix). The radix |
| 291 | * can be from 2 to 36. |
| 292 | * |
| 293 | * If is_signed is true, we'll show a hyphen as the first character. |
| 294 | * Otherwise we'll treat the value as unsigned. |
| 295 | * |
| 296 | * For efficiency, we store the number at the end of the buffer (this |
| 297 | * makes it easy to generate the number, since we need to generate |
| 298 | * numerals in reverse order). We return a pointer to the result, |
| 299 | * which may not start at the beginning of the buffer. |
| 300 | * |
| 301 | * 'flags' is a combination of TOSTR_xxx flags (see vmobj.h). |
| 302 | */ |
| 303 | static char *cvt_int_to_str(char *buf, size_t buflen, |
| 304 | int32_t inval, int radix, int flags); |
| 305 | |
| 306 | /* |
| 307 | * Allocate a string buffer large enough to hold a given value. |
| 308 | * We'll use the provided buffer if possible. |
| 309 | * |
| 310 | * If the provided buffer is null or is not large enough, we'll |
| 311 | * allocate a new string object with a large enough buffer to hold |
| 312 | * the value, and return the object's extension as the buffer. |
| 313 | * |
| 314 | * The buffer size and requested size are in bytes. |
| 315 | * |
| 316 | * If we allocate a new object, we'll set new_obj to the object |
| 317 | * value; otherwise we'll set new_obj to nil. |
| 318 | */ |
| 319 | static char *alloc_str_buf(VMG_ vm_val_t *new_obj, |
| 320 | char *buf, size_t buf_size, |
| 321 | size_t required_size); |
| 322 | |
| 323 | /* |
| 324 | * Constant string equality test routine. Compares the given |
| 325 | * constant string (in portable format, with leading UINT2 length |
| 326 | * prefix followed by the string's text in UTF8 format) to the other |
| 327 | * value. Returns true if the values are lexically identical, false |
| 328 | * if not or if the other value is not a string of some kind. |
| 329 | */ |
| 330 | static int const_equals(VMG_ const char *str, const vm_val_t *val); |
| 331 | |
| 332 | /* |
| 333 | * Constant string hash value calculation |
| 334 | */ |
| 335 | static uint const_calc_hash(const char *str); |
| 336 | |
| 337 | /* |
| 338 | * Constant string magnitude comparison routine. Compares the given |
| 339 | * constant string (in portable format) to the other value. Returns |
| 340 | * a positive value if the constant string is lexically greater than |
| 341 | * the other value, a negative value if the constant string is |
| 342 | * lexically less than the other value, or zero if the two values |
| 343 | * are identical. Throws an error for any other type of value. |
| 344 | */ |
| 345 | static int const_compare(VMG_ const char *str, const vm_val_t *val); |
| 346 | |
| 347 | /* |
| 348 | * Find a substring within a string. Returns a pointer to to the start |
| 349 | * of the substring within the string, or null if the substring isn't |
| 350 | * found. If 'idxp' is non-null, we'll fill in *idxp with the |
| 351 | * character index, starting at zero for the first character, of the |
| 352 | * substring within the string. |
| 353 | * |
| 354 | * start_idx is the 1-based index where we start the search. If this |
| 355 | * is negative, it's an index from the end of the string (-1 for the |
| 356 | * last character). |
| 357 | * |
| 358 | * Both strings are in standard constant string format, with UINT2 |
| 359 | * length prefixes. |
| 360 | */ |
| 361 | static const char *find_substr( |
| 362 | VMG_ const char *str, int32_t start_idx, |
| 363 | const char *substr, size_t *idxp); |
| 364 | |
| 365 | /* |
| 366 | * Find a substring or RexPattern. Returns a pointer to the matching |
| 367 | * substring, or null if no match is found. |
| 368 | * |
| 369 | * 'basestr' is the base string we're searching, and 'str' and 'len' |
| 370 | * give the starting point and remaining length of the substring of |
| 371 | * 'basestr' where the search actually begins. 'basestr' and 'str' |
| 372 | * point directly to the bytes to search, not a VMB_LEN prefix (which |
| 373 | * is why we have 'len' as a separate argument). We won't actually |
| 374 | * search the portion of the base string before 'str', but we need to |
| 375 | * know where the overall string starts to properly handle certain |
| 376 | * regex assertions (e.g., '^'). |
| 377 | * |
| 378 | * 'substr' is the tads-style (VMB_LEN-prefixed) substring we're |
| 379 | * looking for; if 'substr' is null, 'pat' is the regular expression |
| 380 | * object to match, otherwise 'pat' is ignored. |
| 381 | * |
| 382 | * 'match_idx' returns with the CHARACTER index from 'str' (starting at |
| 383 | * 0 for the first byte of 'str') of the substring we found, if |
| 384 | * successful; otherwise this is undefined. |
| 385 | * |
| 386 | * 'match_len' returns with the BYTE length of the substring we |
| 387 | * matched, if successful; otherwise this is undefined. |
| 388 | */ |
| 389 | static const char *find_substr( |
| 390 | VMG_ const vm_val_t *strval, |
| 391 | const char *basestr, const char *str, size_t len, |
| 392 | const char *substr, class CVmObjPattern *pat, |
| 393 | int *match_idx, int *match_len); |
| 394 | |
| 395 | /* find the last matching substring or pattern */ |
| 396 | static const char *find_last_substr( |
| 397 | VMG_ const vm_val_t *strval, |
| 398 | const char *basestr, const char *str, size_t len, |
| 399 | const char *substr, class CVmObjPattern *pat, |
| 400 | int *match_idx, int *match_len); |
| 401 | |
| 402 | |
| 403 | /* |
| 404 | * Evaluate a property of a constant string value. Returns true if |
| 405 | * we successfully evaluated the property, false if the property is |
| 406 | * not one of the properties that the string class defines. |
| 407 | */ |
| 408 | static int const_get_prop(VMG_ vm_val_t *retval, const vm_val_t *self_val, |
| 409 | const char *str, vm_prop_id_t prop, |
| 410 | vm_obj_id_t *srcobj, uint *argc); |
| 411 | |
| 412 | /* evaluate a property */ |
| 413 | virtual int get_prop(VMG_ vm_prop_id_t prop, vm_val_t *val, |
| 414 | vm_obj_id_t self, vm_obj_id_t *source_obj, |
| 415 | uint *argc); |
| 416 | |
| 417 | /* property evaluator - undefined property */ |
| 418 | static int getp_undef(VMG_ vm_val_t *, const vm_val_t *, |
| 419 | const char *, uint *) |
| 420 | { return FALSE; } |
| 421 | |
| 422 | /* property evaluator - get the length */ |
| 423 | static int getp_len(VMG_ vm_val_t *retval, const vm_val_t *self_val, |
| 424 | const char *str, uint *argc); |
| 425 | |
| 426 | /* property evaluator - extract a substring */ |
| 427 | static int getp_substr(VMG_ vm_val_t *retval, const vm_val_t *self_val, |
| 428 | const char *str, uint *argc); |
| 429 | |
| 430 | /* property evaluator - toUpper */ |
| 431 | static int getp_upper(VMG_ vm_val_t *retval, const vm_val_t *self_val, |
| 432 | const char *str, uint *argc); |
| 433 | |
| 434 | /* property evaluator - toLower */ |
| 435 | static int getp_lower(VMG_ vm_val_t *retval, const vm_val_t *self_val, |
| 436 | const char *str, uint *argc); |
| 437 | |
| 438 | /* property evaluator - toTitleCase */ |
| 439 | static int getp_toTitleCase( |
| 440 | VMG_ vm_val_t *retval, const vm_val_t *self_val, |
| 441 | const char *str, uint *argc); |
| 442 | |
| 443 | /* property evaluator - toFoldedCase */ |
| 444 | static int getp_toFoldedCase(VMG_ vm_val_t *retval, |
| 445 | const vm_val_t *self_val, |
| 446 | const char *str, uint *argc); |
| 447 | |
| 448 | /* common handler for case conversions (toUpper, toLower, etc) */ |
| 449 | static int gen_getp_case_conv(VMG_ vm_val_t *retval, |
| 450 | const vm_val_t *self_val, |
| 451 | const char *str, uint *argc, |
| 452 | const wchar_t *(*conv)(wchar_t)); |
| 453 | |
| 454 | /* property evaluator - find substring */ |
| 455 | static int getp_find(VMG_ vm_val_t *retval, const vm_val_t *self_val, |
| 456 | const char *str, uint *argc); |
| 457 | |
| 458 | /* common handler for find() and findLast() */ |
| 459 | template<int dir> static inline int find_common( |
| 460 | VMG_ vm_val_t *retval, |
| 461 | const vm_val_t *self_val, const char *str, |
| 462 | uint *argc); |
| 463 | |
| 464 | /* property evaluator - convert to unicode */ |
| 465 | static int getp_to_uni(VMG_ vm_val_t *retval, const vm_val_t *self_val, |
| 466 | const char *str, uint *argc); |
| 467 | |
| 468 | /* property evaluator - htmlify */ |
| 469 | static int getp_htmlify(VMG_ vm_val_t *retval, const vm_val_t *self_val, |
| 470 | const char *str, uint *argc); |
| 471 | |
| 472 | /* property evaluator - startsWith */ |
| 473 | static int getp_starts_with(VMG_ vm_val_t *retval, |
| 474 | const vm_val_t *self_val, |
| 475 | const char *str, uint *argc); |
| 476 | |
| 477 | /* property evaluator - endsWith */ |
| 478 | static int getp_ends_with(VMG_ vm_val_t *retval, |
| 479 | const vm_val_t *self_val, |
| 480 | const char *str, uint *argc); |
| 481 | |
| 482 | /* property evaluator - mapToByteArray */ |
| 483 | static int getp_to_byte_array(VMG_ vm_val_t *retval, |
| 484 | const vm_val_t *self_val, |
| 485 | const char *str, uint *argc); |
| 486 | |
| 487 | /* property evaluator - findReplace() - replace substring */ |
| 488 | static int getp_replace(VMG_ vm_val_t *retval, const vm_val_t *self_val, |
| 489 | const char *str, uint *argc); |
| 490 | |
| 491 | /* property evaluator - splice */ |
| 492 | static int getp_splice(VMG_ vm_val_t *retval, const vm_val_t *self_val, |
| 493 | const char *str, uint *argc); |
| 494 | |
| 495 | /* property evaluator - split */ |
| 496 | static int getp_split(VMG_ vm_val_t *retval, const vm_val_t *self_val, |
| 497 | const char *str, uint *argc); |
| 498 | |
| 499 | /* property evaluator - specialsToHtml */ |
| 500 | static int getp_specialsToHtml(VMG_ vm_val_t *retval, |
| 501 | const vm_val_t *self_val, |
| 502 | const char *str, uint *argc); |
| 503 | |
| 504 | /* property evaluator - specialsToText */ |
| 505 | static int getp_specialsToText(VMG_ vm_val_t *retval, |
| 506 | const vm_val_t *self_val, |
| 507 | const char *str, uint *argc); |
| 508 | |
| 509 | /* property evaluator - urlEncode */ |
| 510 | static int getp_urlEncode(VMG_ vm_val_t *retval, |
| 511 | const vm_val_t *self_val, |
| 512 | const char *str, uint *argc); |
| 513 | |
| 514 | /* property evaluator - urlDecode */ |
| 515 | static int getp_urlDecode(VMG_ vm_val_t *retval, |
| 516 | const vm_val_t *self_val, |
| 517 | const char *str, uint *argc); |
| 518 | |
| 519 | /* property evaluator - sha256 hash */ |
| 520 | static int getp_sha256(VMG_ vm_val_t *retval, |
| 521 | const vm_val_t *self_val, |
| 522 | const char *str, uint *argc); |
| 523 | |
| 524 | /* property evaluator - md5 hash */ |
| 525 | static int getp_md5(VMG_ vm_val_t *retval, |
| 526 | const vm_val_t *self_val, |
| 527 | const char *str, uint *argc); |
| 528 | |
| 529 | /* property evaluator - packBytes */ |
| 530 | static int getp_packBytes(VMG_ vm_val_t *retval, |
| 531 | const vm_val_t *self_val, |
| 532 | const char *str, uint *argc) |
| 533 | { return static_getp_packBytes(vmg_ retval, argc); } |
| 534 | |
| 535 | /* static property - packBytes */ |
| 536 | static int static_getp_packBytes(VMG_ vm_val_t *retval, uint *argc); |
| 537 | |
| 538 | /* property evaluator - unpackBytes */ |
| 539 | static int getp_unpackBytes(VMG_ vm_val_t *retval, |
| 540 | const vm_val_t *self_val, |
| 541 | const char *str, uint *argc); |
| 542 | |
| 543 | /* property evaluator - compareTo */ |
| 544 | static int getp_compareTo(VMG_ vm_val_t *retval, |
| 545 | const vm_val_t *self_val, |
| 546 | const char *str, uint *argc); |
| 547 | |
| 548 | /* property evaluator - compareIgnoreCase */ |
| 549 | static int getp_compareIgnoreCase(VMG_ vm_val_t *retval, |
| 550 | const vm_val_t *self_val, |
| 551 | const char *str, uint *argc); |
| 552 | |
| 553 | /* property evaluator - findLast */ |
| 554 | static int getp_findLast(VMG_ vm_val_t *retval, const vm_val_t *self_val, |
| 555 | const char *str, uint *argc); |
| 556 | |
| 557 | /* property evaluator - findAll */ |
| 558 | static int getp_findAll(VMG_ vm_val_t *retval, const vm_val_t *self_val, |
| 559 | const char *str, uint *argc); |
| 560 | |
| 561 | /* property evaluator - match */ |
| 562 | static int getp_match(VMG_ vm_val_t *retval, const vm_val_t *self_val, |
| 563 | const char *str, uint *argc); |
| 564 | |
| 565 | |
| 566 | protected: |
| 567 | /* create a string with no initial contents */ |
| 568 | CVmObjString() { ext_ = 0; } |
| 569 | |
| 570 | /* create with a given buffer size in bytes */ |
| 571 | CVmObjString(VMG_ size_t bytelen); |
| 572 | |
| 573 | /* create from a constant UTF-8 string */ |
| 574 | CVmObjString(VMG_ const char *str, size_t bytelen); |
| 575 | |
| 576 | /* |
| 577 | * Set the length of the string. This can be used after a string is |
| 578 | * constructed to set the size of the actual stored string. |
| 579 | */ |
| 580 | void set_length(size_t bytelen) { vmb_put_len(ext_, bytelen); } |
| 581 | |
| 582 | /* copy bytes into the string buffer */ |
| 583 | void copy_into_str(const char *str, size_t bytelen) |
| 584 | { memcpy(ext_ + VMB_LEN, str, bytelen); } |
| 585 | |
| 586 | /* copy bytes into the string buffer starting at the given byte offset */ |
| 587 | void copy_into_str(size_t ofs, const char *str, size_t bytelen) |
| 588 | { memcpy(ext_ + VMB_LEN + ofs, str, bytelen); } |
| 589 | |
| 590 | /* |
| 591 | * Copy bytes from the byte array into the string buffer, treating the |
| 592 | * bytes as Latin-1 characters. Returns the number of bytes used in |
| 593 | * the output buffer. If the output exceeds the available length in |
| 594 | * 'len', we won't store any bytes past 'len', but we'll still |
| 595 | * calculate the full needed length and return it. Call with len == 0 |
| 596 | * to scan the string and get the required allocation length. |
| 597 | */ |
| 598 | static size_t copy_latin1_to_string(char *str, size_t len, |
| 599 | class CVmDataSource *src); |
| 600 | |
| 601 | /* common handler for specialsToText and specialsToHtml */ |
| 602 | static int specialsTo(VMG_ vm_val_t *retval, |
| 603 | const vm_val_t *self_val, |
| 604 | const char *str, uint *argc, int html); |
| 605 | |
| 606 | /* property evaluation function table */ |
| 607 | static int (*func_table_[])(VMG_ vm_val_t *retval, |
| 608 | const vm_val_t *self_val, |
| 609 | const char *str, uint *argc); |
| 610 | }; |
| 611 | |
| 612 | /* ------------------------------------------------------------------------ */ |
| 613 | /* |
| 614 | * A constant string is exactly like an ordinary string, except that our |
| 615 | * contents come from the constant pool. We store a pointer directly to |
| 616 | * our constant pool data rather than making a separate copy. The only |
| 617 | * thing we have to do differently from an ordinary string is that we don't |
| 618 | * delete our extension when we're deleted, since our extension is really |
| 619 | * just a pointer into the constant pool. |
| 620 | */ |
| 621 | class CVmObjStringConst: public CVmObjString |
| 622 | { |
| 623 | public: |
| 624 | /* notify of deletion */ |
| 625 | void notify_delete(VMG_ int /*in_root_set*/) |
| 626 | { |
| 627 | /* |
| 628 | * do nothing, since our extension is just a pointer into the |
| 629 | * constant pool |
| 630 | */ |
| 631 | } |
| 632 | |
| 633 | /* create from constant pool data */ |
| 634 | static vm_obj_id_t create(VMG_ const char *const_ptr); |
| 635 | |
| 636 | protected: |
| 637 | /* construct from constant pool data */ |
| 638 | CVmObjStringConst(VMG_ const char *const_ptr) |
| 639 | { |
| 640 | /* point our extension directly to the constant pool data */ |
| 641 | ext_ = (char *)const_ptr; |
| 642 | } |
| 643 | }; |
| 644 | |
| 645 | |
| 646 | /* ------------------------------------------------------------------------ */ |
| 647 | /* |
| 648 | * Registration table object |
| 649 | */ |
| 650 | class CVmMetaclassString: public CVmMetaclass |
| 651 | { |
| 652 | public: |
| 653 | /* get the global name */ |
| 654 | const char *get_meta_name() const { return "string/030008"; } |
| 655 | |
| 656 | /* create from image file */ |
| 657 | void create_for_image_load(VMG_ vm_obj_id_t id) |
| 658 | { |
| 659 | new (vmg_ id) CVmObjString(); |
| 660 | G_obj_table->set_obj_gc_characteristics(id, FALSE, FALSE); |
| 661 | } |
| 662 | |
| 663 | /* create from restoring from saved state */ |
| 664 | void create_for_restore(VMG_ vm_obj_id_t id) |
| 665 | { |
| 666 | new (vmg_ id) CVmObjString(); |
| 667 | G_obj_table->set_obj_gc_characteristics(id, FALSE, FALSE); |
| 668 | } |
| 669 | |
| 670 | /* create dynamically using stack arguments */ |
| 671 | vm_obj_id_t create_from_stack(VMG_ const uchar **pc_ptr, uint argc) |
| 672 | { return CVmObjString::create_from_stack(vmg_ pc_ptr, argc); } |
| 673 | |
| 674 | /* call a static property */ |
| 675 | int call_stat_prop(VMG_ vm_val_t *result, |
| 676 | const uchar **pc_ptr, uint *argc, |
| 677 | vm_prop_id_t prop) |
| 678 | { |
| 679 | return CVmObjString::call_stat_prop(vmg_ result, pc_ptr, argc, prop); |
| 680 | } |
| 681 | }; |
| 682 | |
| 683 | #endif /* VMSTR_H */ |
| 684 | |
| 685 | /* |
| 686 | * Register the class |
| 687 | */ |
| 688 | VM_REGISTER_METACLASS(CVmObjString) |
| 689 |