| 1 | #ifdef RCSID |
| 2 | static char RCSid[] = |
| 3 | "$Header$"; |
| 4 | #endif |
| 5 | |
| 6 | /* |
| 7 | * Copyright (c) 2010 Michael J. Roberts. All Rights Reserved. |
| 8 | * |
| 9 | * Please see the accompanying license file, LICENSE.TXT, for information |
| 10 | * on using and copying this software. |
| 11 | */ |
| 12 | /* |
| 13 | Name |
| 14 | vmstrbuf.cpp - StringBuffer object |
| 15 | Function |
| 16 | |
| 17 | Notes |
| 18 | |
| 19 | Modified |
| 20 | 12/13/09 MJRoberts - Creation |
| 21 | */ |
| 22 | |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
| 25 | #include <assert.h> |
| 26 | |
| 27 | #include "t3std.h" |
| 28 | #include "vmstrbuf.h" |
| 29 | #include "vmerr.h" |
| 30 | #include "vmerrnum.h" |
| 31 | #include "vmtype.h" |
| 32 | #include "vmglob.h" |
| 33 | #include "vmobj.h" |
| 34 | #include "vmundo.h" |
| 35 | #include "vmmcreg.h" |
| 36 | #include "vmfile.h" |
| 37 | #include "vmbif.h" |
| 38 | #include "vmmeta.h" |
| 39 | #include "vmstack.h" |
| 40 | #include "vmrun.h" |
| 41 | #include "vmstr.h" |
| 42 | #include "utf8.h" |
| 43 | |
| 44 | /* ------------------------------------------------------------------------ */ |
| 45 | /* |
| 46 | * The largest buffer length we can store is the smaller of OSMALMAX or the |
| 47 | * maximum (positive) value for an int32_t (2^31-1). |
| 48 | */ |
| 49 | const int32_t STRBUF_MAX_LEN = (OSMALMAX < 0x7fffffff ? OSMALMAX : 0x7fffffff); |
| 50 | |
| 51 | |
| 52 | /* ------------------------------------------------------------------------ */ |
| 53 | /* |
| 54 | * Extended undo record. We attach this record to the standard undo record |
| 55 | * via the 'ptrval' field. |
| 56 | */ |
| 57 | struct strbuf_undo_rec |
| 58 | { |
| 59 | /* action type */ |
| 60 | enum strbuf_undo_action action; |
| 61 | |
| 62 | /* index of start of replacement */ |
| 63 | int32_t idx; |
| 64 | |
| 65 | /* length of old replaced data */ |
| 66 | int32_t old_len; |
| 67 | |
| 68 | /* length of new data */ |
| 69 | int32_t new_len; |
| 70 | |
| 71 | /* old data */ |
| 72 | wchar_t str[1]; |
| 73 | }; |
| 74 | |
| 75 | /* ------------------------------------------------------------------------ */ |
| 76 | /* |
| 77 | * Allocate an extension structure |
| 78 | */ |
| 79 | vm_strbuf_ext *vm_strbuf_ext::alloc_ext(VMG_ CVmObjStringBuffer *self, |
| 80 | int32_t alo, int32_t inc) |
| 81 | { |
| 82 | /* calculate how much space we need */ |
| 83 | int32_t siz = sizeof(vm_strbuf_ext) + (alo-1)*sizeof(wchar_t); |
| 84 | |
| 85 | /* allocate the memory */ |
| 86 | vm_strbuf_ext *ext = (vm_strbuf_ext *)G_mem->get_var_heap()->alloc_mem( |
| 87 | siz, self); |
| 88 | |
| 89 | /* remember the sizes */ |
| 90 | ext->alo = alo; |
| 91 | ext->inc = inc; |
| 92 | |
| 93 | /* the buffer is currently empty */ |
| 94 | ext->len = 0; |
| 95 | |
| 96 | /* return the new extension */ |
| 97 | return ext; |
| 98 | } |
| 99 | |
| 100 | /* |
| 101 | * Expand an extension structure to make room for the given string length |
| 102 | */ |
| 103 | vm_strbuf_ext *vm_strbuf_ext::expand_ext(VMG_ CVmObjStringBuffer *self, |
| 104 | vm_strbuf_ext *old_ext, |
| 105 | int32_t new_len) |
| 106 | { |
| 107 | /* if the buffer is big enough to hold the new length, we're done */ |
| 108 | if (old_ext->alo >= new_len) |
| 109 | return old_ext; |
| 110 | |
| 111 | /* |
| 112 | * figure the required new allocation size: we allocate in increments |
| 113 | * of 'inc' from the old extension, so allocate enough for the new |
| 114 | * length, rounded up |
| 115 | */ |
| 116 | int32_t new_alo = ((new_len + old_ext->inc - 1) / old_ext->inc) |
| 117 | * old_ext->inc; |
| 118 | vm_strbuf_ext *new_ext = alloc_ext(vmg_ self, new_alo, old_ext->inc); |
| 119 | |
| 120 | /* copy the current string buffer to the new extension */ |
| 121 | new_ext->len = old_ext->len; |
| 122 | memcpy(new_ext->buf, old_ext->buf, |
| 123 | old_ext->len * sizeof(old_ext->buf[0])); |
| 124 | |
| 125 | /* delete the old memory */ |
| 126 | G_mem->get_var_heap()->free_mem(old_ext); |
| 127 | |
| 128 | /* return the new extension */ |
| 129 | return new_ext; |
| 130 | } |
| 131 | |
| 132 | /* ------------------------------------------------------------------------ */ |
| 133 | /* |
| 134 | * StringBuffer object statics |
| 135 | */ |
| 136 | |
| 137 | /* metaclass registration object */ |
| 138 | static CVmMetaclassStringBuffer metaclass_reg_obj; |
| 139 | CVmMetaclass *CVmObjStringBuffer::metaclass_reg_ = &metaclass_reg_obj; |
| 140 | |
| 141 | /* function table */ |
| 142 | int (CVmObjStringBuffer:: |
| 143 | *CVmObjStringBuffer::func_table_[])(VMG_ vm_obj_id_t self, |
| 144 | vm_val_t *retval, uint *argc) = |
| 145 | { |
| 146 | &CVmObjStringBuffer::getp_undef, /* 0 */ |
| 147 | &CVmObjStringBuffer::getp_len, /* 1 */ |
| 148 | &CVmObjStringBuffer::getp_char_at, /* 2 */ |
| 149 | &CVmObjStringBuffer::getp_append, /* 3 */ |
| 150 | &CVmObjStringBuffer::getp_insert, /* 4 */ |
| 151 | &CVmObjStringBuffer::getp_copyChars, /* 5 */ |
| 152 | &CVmObjStringBuffer::getp_delete, /* 6 */ |
| 153 | &CVmObjStringBuffer::getp_splice, /* 7 */ |
| 154 | &CVmObjStringBuffer::getp_substr /* 8 */ |
| 155 | }; |
| 156 | |
| 157 | /* ------------------------------------------------------------------------ */ |
| 158 | /* |
| 159 | * StringBuffer metaclass implementation |
| 160 | */ |
| 161 | |
| 162 | /* |
| 163 | * construction |
| 164 | */ |
| 165 | CVmObjStringBuffer::CVmObjStringBuffer(VMG_ int32_t alo, int32_t inc) |
| 166 | { |
| 167 | /* allocate our extension structure */ |
| 168 | ext_ = (char *)vm_strbuf_ext::alloc_ext(vmg_ this, alo, inc); |
| 169 | } |
| 170 | |
| 171 | /* |
| 172 | * create a StringBuffer with a given allocation size |
| 173 | */ |
| 174 | vm_obj_id_t CVmObjStringBuffer::create(VMG_ int in_root_set, |
| 175 | int32_t alo, int32_t inc) |
| 176 | { |
| 177 | /* allocate the object ID */ |
| 178 | vm_obj_id_t id = vm_new_id(vmg_ in_root_set, FALSE, FALSE); |
| 179 | |
| 180 | /* create the C++ object */ |
| 181 | new (vmg_ id) CVmObjStringBuffer(vmg_ alo, inc); |
| 182 | |
| 183 | /* return the new ID */ |
| 184 | return id; |
| 185 | } |
| 186 | |
| 187 | /* |
| 188 | * create dynamically using stack arguments |
| 189 | */ |
| 190 | vm_obj_id_t CVmObjStringBuffer::create_from_stack( |
| 191 | VMG_ const uchar **pc_ptr, uint argc) |
| 192 | { |
| 193 | vm_obj_id_t id; |
| 194 | int alo, inc; |
| 195 | |
| 196 | /* get the arguments */ |
| 197 | if (argc == 0) |
| 198 | { |
| 199 | /* no arguments - use a default size */ |
| 200 | alo = 256; |
| 201 | inc = 256; |
| 202 | } |
| 203 | else if (argc == 1) |
| 204 | { |
| 205 | /* one argument - it's the initial size */ |
| 206 | alo = CVmBif::pop_int_val(vmg0_); |
| 207 | |
| 208 | /* |
| 209 | * figure a default increment: for small initial buffers, expand by |
| 210 | * the buffer size; for medium sizes, expand by half the buffer |
| 211 | * size; for large sizes, expand by a fixed maximum |
| 212 | */ |
| 213 | if (alo < 256) |
| 214 | inc = alo; |
| 215 | else if (alo < 4096) |
| 216 | inc = alo/2; |
| 217 | else |
| 218 | inc = 2048; |
| 219 | } |
| 220 | else if (argc == 2) |
| 221 | { |
| 222 | /* the initial size and increment are both specified */ |
| 223 | alo = CVmBif::pop_int_val(vmg0_); |
| 224 | inc = CVmBif::pop_int_val(vmg0_); |
| 225 | } |
| 226 | else |
| 227 | { |
| 228 | /* invalid arguments */ |
| 229 | err_throw(VMERR_WRONG_NUM_OF_ARGS); |
| 230 | } |
| 231 | |
| 232 | /* enforce minimum and maximum sizes */ |
| 233 | if (alo < 16) |
| 234 | alo = 16; |
| 235 | else if (alo > STRBUF_MAX_LEN) |
| 236 | alo = STRBUF_MAX_LEN; |
| 237 | if (inc < 16) |
| 238 | inc = 16; |
| 239 | else if (inc > STRBUF_MAX_LEN) |
| 240 | inc = STRBUF_MAX_LEN; |
| 241 | |
| 242 | /* |
| 243 | * allocate the object ID - this type of construction never creates a |
| 244 | * root object |
| 245 | */ |
| 246 | id = vm_new_id(vmg_ FALSE, FALSE, FALSE); |
| 247 | |
| 248 | /* create the object */ |
| 249 | new (vmg_ id) CVmObjStringBuffer(vmg_ alo, inc); |
| 250 | |
| 251 | /* return the new ID */ |
| 252 | return id; |
| 253 | } |
| 254 | |
| 255 | /* |
| 256 | * notify of deletion |
| 257 | */ |
| 258 | void CVmObjStringBuffer::notify_delete(VMG_ int /*in_root_set*/) |
| 259 | { |
| 260 | /* free our additional data, if we have any */ |
| 261 | if (ext_ != 0) |
| 262 | G_mem->get_var_heap()->free_mem(ext_); |
| 263 | } |
| 264 | |
| 265 | /* |
| 266 | * set a property |
| 267 | */ |
| 268 | void CVmObjStringBuffer::set_prop(VMG_ class CVmUndo *undo, |
| 269 | vm_obj_id_t self, vm_prop_id_t prop, |
| 270 | const vm_val_t *val) |
| 271 | { |
| 272 | /* no settable properties - throw an error */ |
| 273 | err_throw(VMERR_INVALID_SETPROP); |
| 274 | } |
| 275 | |
| 276 | /* |
| 277 | * get a property |
| 278 | */ |
| 279 | int CVmObjStringBuffer::get_prop(VMG_ vm_prop_id_t prop, vm_val_t *retval, |
| 280 | vm_obj_id_t self, vm_obj_id_t *source_obj, |
| 281 | uint *argc) |
| 282 | { |
| 283 | uint func_idx; |
| 284 | |
| 285 | /* translate the property into a function vector index */ |
| 286 | func_idx = G_meta_table |
| 287 | ->prop_to_vector_idx(metaclass_reg_->get_reg_idx(), prop); |
| 288 | |
| 289 | /* call the appropriate function */ |
| 290 | if ((this->*func_table_[func_idx])(vmg_ self, retval, argc)) |
| 291 | { |
| 292 | *source_obj = metaclass_reg_->get_class_obj(vmg0_); |
| 293 | return TRUE; |
| 294 | } |
| 295 | |
| 296 | /* inherit default handling from our base class */ |
| 297 | return CVmObject::get_prop(vmg_ prop, retval, self, source_obj, argc); |
| 298 | } |
| 299 | |
| 300 | /* |
| 301 | * apply an undo record |
| 302 | */ |
| 303 | void CVmObjStringBuffer::apply_undo(VMG_ struct CVmUndoRecord *undo_rec) |
| 304 | { |
| 305 | if (undo_rec->id.ptrval != 0) |
| 306 | { |
| 307 | strbuf_undo_rec *rec; |
| 308 | vm_obj_id_t self = undo_rec->obj; |
| 309 | |
| 310 | /* get our private record from the standard undo record */ |
| 311 | rec = (strbuf_undo_rec *)undo_rec->id.ptrval; |
| 312 | |
| 313 | /* check the action in the record */ |
| 314 | switch(rec->action) |
| 315 | { |
| 316 | case STRBUF_UNDO_INS: |
| 317 | /* we inserted text into the buffer; delete the new text */ |
| 318 | delete_text(vmg_ self, rec->idx, rec->new_len, FALSE); |
| 319 | break; |
| 320 | |
| 321 | case STRBUF_UNDO_DEL: |
| 322 | /* we deleted text; re-insert the old text */ |
| 323 | insert_text(vmg_ self, rec->idx, rec->str, rec->old_len, FALSE); |
| 324 | break; |
| 325 | |
| 326 | case STRBUF_UNDO_REPL: |
| 327 | /* we replaced text; splice the old text in place of the new */ |
| 328 | splice_text(vmg_ self, |
| 329 | rec->idx, rec->new_len, rec->str, rec->old_len, |
| 330 | FALSE); |
| 331 | break; |
| 332 | } |
| 333 | |
| 334 | /* discard the private record */ |
| 335 | t3free(rec); |
| 336 | |
| 337 | /* clear the pointer in the main record so we know it's gone */ |
| 338 | undo_rec->id.ptrval = 0; |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | /* |
| 343 | * discard extra undo information |
| 344 | */ |
| 345 | void CVmObjStringBuffer::discard_undo(VMG_ CVmUndoRecord *rec) |
| 346 | { |
| 347 | /* delete our extra information record */ |
| 348 | if (rec->id.ptrval != 0) |
| 349 | { |
| 350 | /* free the record */ |
| 351 | t3free((strbuf_undo_rec *)rec->id.ptrval); |
| 352 | |
| 353 | /* clear the pointer so we know it's gone */ |
| 354 | rec->id.ptrval = 0; |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | /* |
| 359 | * load from an image file |
| 360 | */ |
| 361 | void CVmObjStringBuffer::load_from_image(VMG_ vm_obj_id_t self, |
| 362 | const char *ptr, size_t siz) |
| 363 | { |
| 364 | /* load our image data */ |
| 365 | load_image_data(vmg_ ptr, siz); |
| 366 | |
| 367 | /* |
| 368 | * save our image data pointer in the object table, so that we can |
| 369 | * access it (without storing it ourselves) during a reload |
| 370 | */ |
| 371 | G_obj_table->save_image_pointer(self, ptr, siz); |
| 372 | } |
| 373 | |
| 374 | /* |
| 375 | * reload from the image file |
| 376 | */ |
| 377 | void CVmObjStringBuffer::reload_from_image(VMG_ vm_obj_id_t self, |
| 378 | const char *ptr, size_t siz) |
| 379 | { |
| 380 | /* load our image data */ |
| 381 | load_image_data(vmg_ ptr, siz); |
| 382 | } |
| 383 | |
| 384 | /* |
| 385 | * load or reload data from the image |
| 386 | */ |
| 387 | void CVmObjStringBuffer::load_image_data(VMG_ const char *ptr, size_t siz) |
| 388 | { |
| 389 | /* free our existing extension, if we have one */ |
| 390 | if (ext_ != 0) |
| 391 | G_mem->get_var_heap()->free_mem(ext_); |
| 392 | |
| 393 | /* read the string and allocation data from the image data */ |
| 394 | uint32_t alo = osrp4(ptr); |
| 395 | uint32_t inc = osrp4(ptr + 4); |
| 396 | uint32_t len = osrp4(ptr + 8); |
| 397 | |
| 398 | /* limit the sizes */ |
| 399 | if (alo < 16) |
| 400 | alo = 16; |
| 401 | else if (alo > STRBUF_MAX_LEN) |
| 402 | alo = STRBUF_MAX_LEN; |
| 403 | |
| 404 | if (inc < 16) |
| 405 | inc = 16; |
| 406 | else if (inc > STRBUF_MAX_LEN) |
| 407 | inc = STRBUF_MAX_LEN; |
| 408 | |
| 409 | if (len > alo) |
| 410 | len = alo; |
| 411 | |
| 412 | /* skip to the string characters */ |
| 413 | ptr += 12; |
| 414 | |
| 415 | /* allocate the extension */ |
| 416 | vm_strbuf_ext *ext = vm_strbuf_ext::alloc_ext(vmg_ this, alo, inc); |
| 417 | ext_ = (char *)ext; |
| 418 | |
| 419 | /* set the length */ |
| 420 | ext->len = len; |
| 421 | |
| 422 | /* read the string data into the buffer */ |
| 423 | for (wchar_t *dst = ext->buf ; len != 0 ; |
| 424 | *dst++ = osrp2(ptr), ptr += 2, --len) ; |
| 425 | } |
| 426 | |
| 427 | |
| 428 | /* |
| 429 | * save to a file |
| 430 | */ |
| 431 | void CVmObjStringBuffer::save_to_file(VMG_ class CVmFile *fp) |
| 432 | { |
| 433 | vm_strbuf_ext *ext = get_ext(); |
| 434 | |
| 435 | /* write our allocation and string length data */ |
| 436 | fp->write_uint4(ext->alo); |
| 437 | fp->write_uint4(ext->inc); |
| 438 | fp->write_uint4(ext->len); |
| 439 | |
| 440 | /* write the string contents */ |
| 441 | const wchar_t *src; |
| 442 | int i; |
| 443 | for (i = ext->len, src = ext->buf ; i != 0 ; --i) |
| 444 | fp->write_uint2(*src++); |
| 445 | } |
| 446 | |
| 447 | /* |
| 448 | * restore from a file |
| 449 | */ |
| 450 | void CVmObjStringBuffer::restore_from_file(VMG_ vm_obj_id_t self, |
| 451 | CVmFile *fp, CVmObjFixup *fixups) |
| 452 | { |
| 453 | const int32_t bufchars = 256; |
| 454 | char buf[bufchars * sizeof(uint16_t)]; |
| 455 | |
| 456 | /* free our existing extension, if we have one */ |
| 457 | if (ext_ != 0) |
| 458 | G_mem->get_var_heap()->free_mem(ext_); |
| 459 | |
| 460 | /* read the fixed fields */ |
| 461 | fp->read_bytes(buf, 12); |
| 462 | int32_t alo = vmb_get_int4(buf); |
| 463 | int32_t inc = vmb_get_int4(buf + 4); |
| 464 | int32_t len = vmb_get_int4(buf + 8); |
| 465 | |
| 466 | /* allocate the extension structure */ |
| 467 | vm_strbuf_ext *ext = vm_strbuf_ext::alloc_ext(vmg_ this, alo, inc); |
| 468 | ext_ = (char *)ext; |
| 469 | |
| 470 | /* limit the length */ |
| 471 | if (len > alo) |
| 472 | len = alo; |
| 473 | |
| 474 | /* store it in the extension */ |
| 475 | ext->len = len; |
| 476 | |
| 477 | /* read the string contents */ |
| 478 | wchar_t *dst; |
| 479 | int32_t rem; |
| 480 | for (dst = ext->buf, rem = len ; rem != 0 ; ) |
| 481 | { |
| 482 | /* read one buffer-full, or 'rem', whichever is less */ |
| 483 | int32_t cur = rem > bufchars ? bufchars : rem; |
| 484 | fp->read_bytes(buf, cur * sizeof(uint16_t)); |
| 485 | |
| 486 | /* deduct this read from the total */ |
| 487 | rem -= cur; |
| 488 | |
| 489 | /* decode the characters */ |
| 490 | for (const char *src = buf ; cur != 0 ; |
| 491 | *dst++ = osrp2(src), src += 2, --cur) ; |
| 492 | } |
| 493 | } |
| 494 | |
| 495 | /* |
| 496 | * add an undo record |
| 497 | */ |
| 498 | void CVmObjStringBuffer::add_undo_rec( |
| 499 | VMG_ vm_obj_id_t self, strbuf_undo_action action, |
| 500 | int32_t idx, int32_t old_len, int32_t new_len) |
| 501 | { |
| 502 | /* |
| 503 | * if we're not inserting or deleting anything, there's no change, so |
| 504 | * there's no undo information to save |
| 505 | */ |
| 506 | if (old_len == 0 && new_len == 0) |
| 507 | return; |
| 508 | |
| 509 | /* |
| 510 | * replacing zero bytes is simply an insertion; substituting zero bytes |
| 511 | * is a deletion |
| 512 | */ |
| 513 | if (action == STRBUF_UNDO_REPL && old_len == 0) |
| 514 | action = STRBUF_UNDO_INS; |
| 515 | if (action == STRBUF_UNDO_REPL && new_len == 0) |
| 516 | action = STRBUF_UNDO_DEL; |
| 517 | |
| 518 | /* |
| 519 | * Figure the size for the extension record. For an insertion, we |
| 520 | * don't need any extra data, since we're replacing zero characters. |
| 521 | * For a deletion or replacement, we need to make a copy of the |
| 522 | * characters we're removing, so we need old_len extra wchar_t's. |
| 523 | */ |
| 524 | int32_t siz = sizeof(strbuf_undo_rec); |
| 525 | if (action == STRBUF_UNDO_DEL || action == STRBUF_UNDO_REPL) |
| 526 | siz += (old_len - 1) * sizeof(wchar_t); |
| 527 | |
| 528 | /* allocate our record extension */ |
| 529 | strbuf_undo_rec *rec = (strbuf_undo_rec *)t3malloc(siz); |
| 530 | |
| 531 | /* set up the record */ |
| 532 | rec->action = action; |
| 533 | rec->idx = idx; |
| 534 | rec->old_len = old_len; |
| 535 | rec->new_len = new_len; |
| 536 | |
| 537 | /* if we're deleting or replacing text, save the old text */ |
| 538 | if (action == STRBUF_UNDO_DEL || action == STRBUF_UNDO_REPL) |
| 539 | memcpy(rec->str, get_ext()->buf + idx, old_len * sizeof(rec->str[0])); |
| 540 | |
| 541 | /* |
| 542 | * Add the record to the global undo stream. (We don't have anything |
| 543 | * to store in the 'value' field, so just store nil.) |
| 544 | */ |
| 545 | vm_val_t nilval; |
| 546 | nilval.set_nil(); |
| 547 | if (!G_undo->add_new_record_ptr_key(vmg_ self, rec, &nilval)) |
| 548 | { |
| 549 | /* |
| 550 | * we didn't add an undo record, so our extra undo information |
| 551 | * isn't actually going to be stored in the undo system - hence we |
| 552 | * must delete our extra information |
| 553 | */ |
| 554 | t3free(rec); |
| 555 | } |
| 556 | } |
| 557 | |
| 558 | /* |
| 559 | * Create a string representation of the number |
| 560 | */ |
| 561 | const char *CVmObjStringBuffer::cast_to_string(VMG_ vm_obj_id_t self, |
| 562 | vm_val_t *new_str) const |
| 563 | { |
| 564 | /* make a string out of the whole buffer */ |
| 565 | return substr_to_string(vmg_ new_str, 0, get_ext()->len); |
| 566 | } |
| 567 | |
| 568 | /* |
| 569 | * Test for equality |
| 570 | */ |
| 571 | int CVmObjStringBuffer::equals(VMG_ vm_obj_id_t self, |
| 572 | const vm_val_t *val, int /*depth*/) const |
| 573 | { |
| 574 | /* |
| 575 | * we can compare our contents to another string buffer, or to a |
| 576 | * regular string object |
| 577 | */ |
| 578 | const char *str; |
| 579 | if (val->typ == VM_OBJ && is_string_buffer_obj(vmg_ val->val.obj)) |
| 580 | { |
| 581 | /* compare the buffers character by character */ |
| 582 | CVmObjStringBuffer *other = |
| 583 | (CVmObjStringBuffer *)vm_objp(vmg_ val->val.obj); |
| 584 | int32_t len1 = get_ext()->len, len2 = other->get_ext()->len; |
| 585 | const wchar_t *buf1 = get_ext()->buf, *buf2 = other->get_ext()->buf; |
| 586 | |
| 587 | /* if the lengths aren't equal, they're not equal */ |
| 588 | if (len1 != len2) |
| 589 | return FALSE; |
| 590 | |
| 591 | /* they're equal if the contents match exactly */ |
| 592 | return memcmp(buf1, buf2, len1*sizeof(*buf1)) == 0; |
| 593 | } |
| 594 | else if ((str = val->get_as_string(vmg0_)) != 0) |
| 595 | { |
| 596 | /* compare to a constant string */ |
| 597 | return equals_str(str); |
| 598 | } |
| 599 | else |
| 600 | { |
| 601 | /* the other value isn't comparable to string */ |
| 602 | err_throw(VMERR_INVALID_COMPARISON); |
| 603 | AFTER_ERR_THROW(return 0;) |
| 604 | } |
| 605 | } |
| 606 | |
| 607 | /* |
| 608 | * test for equality against a constant string |
| 609 | */ |
| 610 | int CVmObjStringBuffer::equals_str(const char *str) const |
| 611 | { |
| 612 | /* get the buffer pointers and character lengths */ |
| 613 | const wchar_t *p1 = get_ext()->buf; |
| 614 | utf8_ptr p2((char *)str + VMB_LEN); |
| 615 | int32_t len1 = get_ext()->len, len2 = p2.len(vmb_get_len(str)); |
| 616 | |
| 617 | /* if the lengths don't match, we can't be equal */ |
| 618 | if (len1 != len2) |
| 619 | return FALSE; |
| 620 | |
| 621 | /* compare character by character */ |
| 622 | for (int32_t i = 0 ; i < len1 ; ++i, ++p1, p2.inc()) |
| 623 | { |
| 624 | if (*p1 != p2.getch()) |
| 625 | return FALSE; |
| 626 | } |
| 627 | |
| 628 | /* the characters match */ |
| 629 | return TRUE; |
| 630 | } |
| 631 | |
| 632 | /* |
| 633 | * Compare value |
| 634 | */ |
| 635 | int CVmObjStringBuffer::compare_to(VMG_ vm_obj_id_t self, |
| 636 | const vm_val_t *val) const |
| 637 | { |
| 638 | /* |
| 639 | * we can compare our contents to another string buffer, or to a |
| 640 | * regular string object |
| 641 | */ |
| 642 | const char *str; |
| 643 | if (val->typ == VM_OBJ && is_string_buffer_obj(vmg_ val->val.obj)) |
| 644 | { |
| 645 | /* compare the buffers character by character */ |
| 646 | CVmObjStringBuffer *other = |
| 647 | (CVmObjStringBuffer *)vm_objp(vmg_ val->val.obj); |
| 648 | int32_t len1 = get_ext()->len, len2 = other->get_ext()->len; |
| 649 | const wchar_t *buf1 = get_ext()->buf, *buf2 = other->get_ext()->buf; |
| 650 | for (int32_t i = 0 ; i < len1 && i < len2 ; ++i, ++buf1, ++buf2) |
| 651 | { |
| 652 | if (*buf1 < *buf2) |
| 653 | return -1; |
| 654 | else if (*buf1 > *buf2) |
| 655 | return 1; |
| 656 | } |
| 657 | |
| 658 | /* |
| 659 | * They're equal up to the full length of the shorter string. If |
| 660 | * we ran out of strings at the same time, they're equal; otherwise |
| 661 | * the longer string compares greater |
| 662 | */ |
| 663 | return len1 - len2; |
| 664 | } |
| 665 | else if ((str = val->get_as_string(vmg0_)) != 0) |
| 666 | { |
| 667 | /* compare to a constant string */ |
| 668 | return compare_str(str); |
| 669 | } |
| 670 | else |
| 671 | { |
| 672 | /* the other value isn't comparable to string */ |
| 673 | err_throw(VMERR_INVALID_COMPARISON); |
| 674 | AFTER_ERR_THROW(return 0;) |
| 675 | } |
| 676 | } |
| 677 | |
| 678 | /* |
| 679 | * compare to a constant string |
| 680 | */ |
| 681 | int CVmObjStringBuffer::compare_str(const char *str) const |
| 682 | { |
| 683 | /* get the buffer pointers and character lengths */ |
| 684 | const wchar_t *p1 = get_ext()->buf; |
| 685 | utf8_ptr p2((char *)str + VMB_LEN); |
| 686 | int32_t len1 = get_ext()->len, len2 = p2.len(vmb_get_len(str)); |
| 687 | |
| 688 | /* compare character by character */ |
| 689 | for (int32_t i = 0 ; i < len1 ; ++i, ++p1, p2.inc()) |
| 690 | { |
| 691 | wchar_t c1 = *p1, c2 = p2.getch(); |
| 692 | if (c1 < c2) |
| 693 | return -1; |
| 694 | else if (c1 > c2) |
| 695 | return 1; |
| 696 | } |
| 697 | |
| 698 | /* |
| 699 | * the strings are equal up to the length of the shorter string; the |
| 700 | * longer string compares higher |
| 701 | */ |
| 702 | return len1 - len2; |
| 703 | } |
| 704 | |
| 705 | /* |
| 706 | * Calculate my hash value. We yield the same hash value as a regular |
| 707 | * string with our contents. (This is a requirement, since we compare |
| 708 | * equal to a regular string with the same contents.) |
| 709 | */ |
| 710 | uint CVmObjStringBuffer::calc_hash(VMG_ vm_obj_id_t self, int) const |
| 711 | { |
| 712 | uint hash = 0; |
| 713 | const wchar_t *p = get_ext()->buf; |
| 714 | int32_t len = get_ext()->len; |
| 715 | |
| 716 | /* calculate the hash */ |
| 717 | for (int32_t i = 0 ; i < len ; ++i) |
| 718 | hash += *p++; |
| 719 | |
| 720 | /* return the result */ |
| 721 | return hash; |
| 722 | } |
| 723 | |
| 724 | /* |
| 725 | * Adjust arguments to limits. '*idx' is the 0-based index of the start of |
| 726 | * the segment we're extracting or replacing. If 'len' is non-null, '*len' |
| 727 | * is the length of the existing chunk we're extracting, deleting, or |
| 728 | * replacing. If 'ins' is non-null, '*ins' is the length of the new chunk |
| 729 | * we're inserting or substituting for the deleted chunk. |
| 730 | * |
| 731 | * Note that if both 'len' and 'ins' are provided, we assume the operation |
| 732 | * will delete '*len' characters and insert '*ins' characters in their |
| 733 | * place. |
| 734 | */ |
| 735 | void CVmObjStringBuffer::adjust_args( |
| 736 | int32_t *idx, int32_t *len, int32_t *ins) const |
| 737 | { |
| 738 | vm_strbuf_ext *ext = get_ext(); |
| 739 | |
| 740 | /* make sure the index is in the range 1..len */ |
| 741 | if (*idx < 0) |
| 742 | *idx = 0; |
| 743 | else if (*idx > ext->len) |
| 744 | *idx = ext->len; |
| 745 | |
| 746 | /* make sure the length is in range 0..(len-idx) */ |
| 747 | if (len != 0) |
| 748 | { |
| 749 | if (*len < 0) |
| 750 | *len = 0; |
| 751 | else if (*idx + *len > ext->len) |
| 752 | *len = ext->len - *idx; |
| 753 | } |
| 754 | |
| 755 | /* make sure the insertion wouldn't push us past the limit */ |
| 756 | if (ins != 0) |
| 757 | { |
| 758 | int32_t del = (len != 0 ? *len : 0); |
| 759 | if (*ins < 0) |
| 760 | *ins = 0; |
| 761 | else if (*idx + *ins + ext->len - del > STRBUF_MAX_LEN) |
| 762 | err_throw(VMERR_STR_TOO_LONG); |
| 763 | } |
| 764 | } |
| 765 | |
| 766 | /* |
| 767 | * Calculate the length in bytes in UTF-8 format |
| 768 | */ |
| 769 | int32_t CVmObjStringBuffer::utf8_length() const |
| 770 | { |
| 771 | vm_strbuf_ext *ext = get_ext(); |
| 772 | |
| 773 | /* get our character length */ |
| 774 | int32_t rem = ext->len; |
| 775 | |
| 776 | /* start with a zero length */ |
| 777 | int32_t bytelen = 0; |
| 778 | |
| 779 | /* scan our characters */ |
| 780 | for (const wchar_t *p = ext->buf ; rem > 0 ; ++p, --rem) |
| 781 | bytelen += utf8_ptr::s_wchar_size(*p); |
| 782 | |
| 783 | /* return the result */ |
| 784 | return bytelen; |
| 785 | } |
| 786 | |
| 787 | |
| 788 | /* |
| 789 | * Copy a portion of the string to a buffer as UTF-8 bytes. |
| 790 | * |
| 791 | * 'idx' is an in-out variable. On input, it's the starting character |
| 792 | * index requested for the copy. On output, it's updated to the index of |
| 793 | * the next character after the last character copied. This can be used |
| 794 | * for piecewise copies, since it's updated to point to the start of the |
| 795 | * next piece after copying each piece. |
| 796 | * |
| 797 | * 'bytelen' is an in-out variable. On input, this is the number of bytes |
| 798 | * requested to copy to the buffer. On output, it's the actual number of |
| 799 | * bytes copied. This might be smaller than the request size, because (a) |
| 800 | * we won't copy past the end of the string, and (b) we'll only copy whole, |
| 801 | * well-formed character sequences, so if the requested number of bytes |
| 802 | * would copy a fractional character, we'll omit that fractional character |
| 803 | * and stop at the previous whole character. |
| 804 | */ |
| 805 | void CVmObjStringBuffer::to_utf8(char *buf, int32_t &idx, int32_t &bytelen) |
| 806 | { |
| 807 | vm_strbuf_ext *ext = get_ext(); |
| 808 | |
| 809 | /* keep the index within our limits */ |
| 810 | adjust_args(&idx, 0, 0); |
| 811 | |
| 812 | /* we haven't copied any bytes yet */ |
| 813 | int32_t actual = 0; |
| 814 | |
| 815 | /* set up a utf8 pointer for the output buffer */ |
| 816 | utf8_ptr dst(buf); |
| 817 | |
| 818 | /* |
| 819 | * copy characters until we reach the end of the string, or exhaust the |
| 820 | * requested number of bytes |
| 821 | */ |
| 822 | for (actual = 0 ; actual < bytelen && idx < (int32_t)ext->len ; ++idx) |
| 823 | { |
| 824 | /* get the next source character */ |
| 825 | wchar_t c = ext->buf[idx]; |
| 826 | int clen = utf8_ptr::s_wchar_size(c); |
| 827 | |
| 828 | /* make sure it fits in the remaining output buffer space */ |
| 829 | if (clen > bytelen - actual) |
| 830 | break; |
| 831 | |
| 832 | /* store the character */ |
| 833 | dst.setch(c); |
| 834 | |
| 835 | /* count it in the output size */ |
| 836 | actual += clen; |
| 837 | } |
| 838 | |
| 839 | /* return the actual length copied in bytelen */ |
| 840 | bytelen = actual; |
| 841 | } |
| 842 | |
| 843 | /* |
| 844 | * Make a String object out of a substring of the buffer |
| 845 | */ |
| 846 | const char *CVmObjStringBuffer::substr_to_string( |
| 847 | VMG_ vm_val_t *new_str, int32_t idx, int32_t len) const |
| 848 | { |
| 849 | vm_strbuf_ext *ext = get_ext(); |
| 850 | |
| 851 | /* keep the arguments within our limits */ |
| 852 | adjust_args(&idx, &len, 0); |
| 853 | |
| 854 | /* calculate the size in bytes of the UTF-8 version of the string */ |
| 855 | utf8_ptr p; |
| 856 | int32_t bytelen = p.setwchars(ext->buf + idx, len, 0); |
| 857 | |
| 858 | /* allocate a string */ |
| 859 | new_str->set_obj(CVmObjString::create(vmg_ FALSE, bytelen)); |
| 860 | CVmObjString *str = (CVmObjString *)vm_objp(vmg_ new_str->val.obj); |
| 861 | |
| 862 | /* translate the string to UTF-8 into the buffer */ |
| 863 | p.set(str->cons_get_buf()); |
| 864 | p.setwchars(ext->buf + idx, len, bytelen); |
| 865 | |
| 866 | /* return the new string buffer */ |
| 867 | return str->get_as_string(vmg0_); |
| 868 | } |
| 869 | |
| 870 | /* |
| 871 | * Index the buffer - returns the character at the given index |
| 872 | */ |
| 873 | int CVmObjStringBuffer::index_val_q(VMG_ vm_val_t *result, vm_obj_id_t self, |
| 874 | const vm_val_t *index_val) |
| 875 | { |
| 876 | /* get the index as an integer */ |
| 877 | int32_t idx = index_val->num_to_int(vmg0_); |
| 878 | |
| 879 | /* adjust to zero-based indexing or a negative end-based index */ |
| 880 | idx += (idx < 0 ? get_ext()->len : -1); |
| 881 | |
| 882 | /* ensure idx is in range */ |
| 883 | if (idx < 0 || idx >= (int32_t)get_ext()->len) |
| 884 | err_throw(VMERR_INDEX_OUT_OF_RANGE); |
| 885 | |
| 886 | /* return a one-character string of the character at this index */ |
| 887 | substr_to_string(vmg_ result, idx, 1); |
| 888 | |
| 889 | /* handled */ |
| 890 | return TRUE; |
| 891 | } |
| 892 | |
| 893 | |
| 894 | /* |
| 895 | * Set a character in the buffer by index |
| 896 | */ |
| 897 | int CVmObjStringBuffer::set_index_val_q(VMG_ vm_val_t *result, |
| 898 | vm_obj_id_t self, |
| 899 | const vm_val_t *index_val, |
| 900 | const vm_val_t *new_val) |
| 901 | { |
| 902 | /* the index must be an integer */ |
| 903 | int32_t idx = index_val->num_to_int(vmg0_); |
| 904 | |
| 905 | /* adjust to zero-based indexing or a negative end-based index */ |
| 906 | idx += (idx < 0 ? get_ext()->len : -1); |
| 907 | |
| 908 | /* ensure idx is in range */ |
| 909 | if (idx < 0 || idx >= (int32_t)get_ext()->len) |
| 910 | err_throw(VMERR_INDEX_OUT_OF_RANGE); |
| 911 | |
| 912 | /* |
| 913 | * If the new value is an integer, interpret it as a unicode character |
| 914 | * value to set at the given character. Otherwise, cast it to a string |
| 915 | * and take its first character. |
| 916 | */ |
| 917 | wchar_t ch; |
| 918 | if (new_val->is_numeric(vmg0_)) |
| 919 | { |
| 920 | /* it's an integer - treat it as a unicode character value */ |
| 921 | int32_t ich = new_val->num_to_int(vmg0_); |
| 922 | if (ich < 0 || ich > 65535) |
| 923 | err_throw(VMERR_BAD_VAL_BIF); |
| 924 | |
| 925 | /* it's in range - cast to wchar_t */ |
| 926 | ch = (wchar_t)ich; |
| 927 | } |
| 928 | else |
| 929 | { |
| 930 | /* treat it as a string */ |
| 931 | const char *str = new_val->get_as_string(vmg0_); |
| 932 | |
| 933 | /* get the first character */ |
| 934 | utf8_ptr p((char *)str + VMB_LEN); |
| 935 | ch = (vmb_get_len(str) > 0 ? p.getch() : 0); |
| 936 | } |
| 937 | |
| 938 | /* splice the character at the desired index */ |
| 939 | splice_text(vmg_ self, (int)idx, 1, &ch, 1, TRUE); |
| 940 | |
| 941 | /* we change in place, so the result is 'self' */ |
| 942 | result->set_obj(self); |
| 943 | |
| 944 | /* handled */ |
| 945 | return TRUE; |
| 946 | } |
| 947 | |
| 948 | /* |
| 949 | * Ensure that the buffer is big enough to hold the given number of |
| 950 | * characters |
| 951 | */ |
| 952 | void CVmObjStringBuffer::ensure_space(VMG_ int32_t len) |
| 953 | { |
| 954 | /* if the desired new length exceeds the limit, it's an error */ |
| 955 | if (len > STRBUF_MAX_LEN) |
| 956 | err_throw(VMERR_STR_TOO_LONG); |
| 957 | |
| 958 | /* if the desired new length exceeds the current allocation, expand it */ |
| 959 | if (len > (int32_t)get_ext()->alo) |
| 960 | { |
| 961 | ext_ = (char *)vm_strbuf_ext::expand_ext( |
| 962 | vmg_ this, get_ext(), len); |
| 963 | } |
| 964 | } |
| 965 | |
| 966 | /* |
| 967 | * Open a gap for an insertion and/or delete characters for a deletion. |
| 968 | */ |
| 969 | void CVmObjStringBuffer::splice_move( |
| 970 | VMG_ int32_t idx, int32_t del, int32_t ins) |
| 971 | { |
| 972 | /* |
| 973 | * if we're adding more than we're deleting, expand the buffer if |
| 974 | * necessary to accommodate the added length |
| 975 | */ |
| 976 | if (ins > del) |
| 977 | ensure_added_space(vmg_ ins - del); |
| 978 | |
| 979 | /* |
| 980 | * if we're doing a net insertion or deletion, move the part of the |
| 981 | * string after the deleted text so that it'll be aligned at the end of |
| 982 | * the inserted text |
| 983 | */ |
| 984 | if (ins != del && idx + del < get_ext()->len) |
| 985 | memmove(get_ext()->buf + idx + ins, |
| 986 | get_ext()->buf + idx + del, |
| 987 | (get_ext()->len - (idx + del)) * sizeof(wchar_t)); |
| 988 | |
| 989 | /* adjust the buffer length */ |
| 990 | get_ext()->len += ins - del; |
| 991 | } |
| 992 | |
| 993 | /* |
| 994 | * splice text into the buffer |
| 995 | */ |
| 996 | void CVmObjStringBuffer::splice_text(VMG_ vm_obj_id_t self, |
| 997 | int32_t idx, int32_t del_chars, |
| 998 | const wchar_t *src, int32_t ins_chars, |
| 999 | int undo) |
| 1000 | { |
| 1001 | /* check arguments */ |
| 1002 | adjust_args(&idx, &del_chars, &ins_chars); |
| 1003 | |
| 1004 | /* if desired, save undo */ |
| 1005 | if (undo) |
| 1006 | add_undo_rec(vmg_ self, STRBUF_UNDO_REPL, idx, del_chars, ins_chars); |
| 1007 | |
| 1008 | /* delete the old characters and open a gap for the new characters */ |
| 1009 | splice_move(vmg_ idx, del_chars, ins_chars); |
| 1010 | |
| 1011 | /* copy in the characters */ |
| 1012 | memcpy(get_ext()->buf + idx, src, ins_chars * sizeof(get_ext()->buf[0])); |
| 1013 | } |
| 1014 | |
| 1015 | /* |
| 1016 | * splice UTF-8 text into the buffer |
| 1017 | */ |
| 1018 | void CVmObjStringBuffer::splice_text(VMG_ vm_obj_id_t self, |
| 1019 | int32_t idx, int32_t del_chars, |
| 1020 | const char *src, int32_t ins_bytes, |
| 1021 | int undo) |
| 1022 | { |
| 1023 | /* figure the character length of the string */ |
| 1024 | utf8_ptr p((char *)src); |
| 1025 | int32_t ins_chars = p.len(ins_bytes); |
| 1026 | |
| 1027 | /* check arguments */ |
| 1028 | adjust_args(&idx, &del_chars, &ins_chars); |
| 1029 | |
| 1030 | /* if desired, save undo */ |
| 1031 | if (undo) |
| 1032 | add_undo_rec(vmg_ self, STRBUF_UNDO_REPL, idx, del_chars, ins_chars); |
| 1033 | |
| 1034 | /* open up a gap in the buffer */ |
| 1035 | splice_move(vmg_ idx, del_chars, ins_chars); |
| 1036 | |
| 1037 | /* decode characters into the buffer */ |
| 1038 | p.to_wchar(get_ext()->buf + idx, ins_chars, src, ins_bytes); |
| 1039 | } |
| 1040 | |
| 1041 | /* |
| 1042 | * property evaluator - get length |
| 1043 | */ |
| 1044 | int CVmObjStringBuffer::getp_len(VMG_ vm_obj_id_t self, |
| 1045 | vm_val_t *retval, uint *argc) |
| 1046 | { |
| 1047 | /* check arguments */ |
| 1048 | static CVmNativeCodeDesc desc(0); |
| 1049 | if (get_prop_check_argc(retval, argc, &desc)) |
| 1050 | return TRUE; |
| 1051 | |
| 1052 | /* return the content length */ |
| 1053 | retval->set_int(get_ext()->len); |
| 1054 | |
| 1055 | /* handled */ |
| 1056 | return TRUE; |
| 1057 | } |
| 1058 | |
| 1059 | /* |
| 1060 | * property evaluator - charAt |
| 1061 | */ |
| 1062 | int CVmObjStringBuffer::getp_char_at(VMG_ vm_obj_id_t self, |
| 1063 | vm_val_t *retval, uint *argc) |
| 1064 | { |
| 1065 | /* check arguments */ |
| 1066 | static CVmNativeCodeDesc desc(1); |
| 1067 | if (get_prop_check_argc(retval, argc, &desc)) |
| 1068 | return TRUE; |
| 1069 | |
| 1070 | /* pop the index and adjust to 0-based or end-based */ |
| 1071 | int32_t idx = CVmBif::pop_int_val(vmg0_); |
| 1072 | idx += (idx < 0 ? get_ext()->len : -1); |
| 1073 | |
| 1074 | /* make sure it's in range */ |
| 1075 | if (idx < 0 || idx >= (int32_t)get_ext()->len) |
| 1076 | err_throw(VMERR_INDEX_OUT_OF_RANGE); |
| 1077 | |
| 1078 | /* return the character at the given index, as an integer Unicode code */ |
| 1079 | retval->set_int(get_ext()->buf[idx]); |
| 1080 | |
| 1081 | /* handled */ |
| 1082 | return TRUE; |
| 1083 | } |
| 1084 | |
| 1085 | /* |
| 1086 | * property evaluator - append text |
| 1087 | */ |
| 1088 | int CVmObjStringBuffer::getp_append(VMG_ vm_obj_id_t self, |
| 1089 | vm_val_t *retval, uint *argc) |
| 1090 | { |
| 1091 | /* check arguments */ |
| 1092 | static CVmNativeCodeDesc desc(1); |
| 1093 | if (get_prop_check_argc(retval, argc, &desc)) |
| 1094 | return TRUE; |
| 1095 | |
| 1096 | /* get or cast the string; leave it on the stack for gc protection */ |
| 1097 | vm_val_t strval; |
| 1098 | const char *str = G_stk->get(0)->cast_to_string(vmg_ &strval); |
| 1099 | *G_stk->get(0) = strval; |
| 1100 | |
| 1101 | /* insert the string at the end of the buffer */ |
| 1102 | insert_text(vmg_ self, get_ext()->len, |
| 1103 | str + VMB_LEN, vmb_get_len(str), TRUE); |
| 1104 | |
| 1105 | /* discard gc protection */ |
| 1106 | G_stk->discard(); |
| 1107 | |
| 1108 | /* handled */ |
| 1109 | retval->set_obj(self); |
| 1110 | return TRUE; |
| 1111 | } |
| 1112 | |
| 1113 | /* |
| 1114 | * property evaluator - insert |
| 1115 | */ |
| 1116 | int CVmObjStringBuffer::getp_insert(VMG_ vm_obj_id_t self, |
| 1117 | vm_val_t *retval, uint *argc) |
| 1118 | { |
| 1119 | /* check arguments */ |
| 1120 | static CVmNativeCodeDesc desc(2); |
| 1121 | if (get_prop_check_argc(retval, argc, &desc)) |
| 1122 | return TRUE; |
| 1123 | |
| 1124 | /* pop the insertion index; adjust to 0-based or end-based indexing */ |
| 1125 | int32_t idx = CVmBif::pop_int_val(vmg0_); |
| 1126 | idx += (idx < 0 ? get_ext()->len : -1); |
| 1127 | |
| 1128 | /* get or cast the string; leave it on the stack for gc protection */ |
| 1129 | vm_val_t strval; |
| 1130 | const char *str = G_stk->get(0)->cast_to_string(vmg_ &strval); |
| 1131 | *G_stk->get(0) = strval; |
| 1132 | |
| 1133 | /* do the insert */ |
| 1134 | insert_text(vmg_ self, idx, str + VMB_LEN, vmb_get_len(str), TRUE); |
| 1135 | |
| 1136 | /* discard gc protection */ |
| 1137 | G_stk->discard(); |
| 1138 | |
| 1139 | /* handled */ |
| 1140 | retval->set_obj(self); |
| 1141 | return TRUE; |
| 1142 | } |
| 1143 | |
| 1144 | /* |
| 1145 | * property evaluator - copyChars |
| 1146 | */ |
| 1147 | int CVmObjStringBuffer::getp_copyChars(VMG_ vm_obj_id_t self, |
| 1148 | vm_val_t *retval, uint *argc) |
| 1149 | { |
| 1150 | /* check arguments */ |
| 1151 | static CVmNativeCodeDesc desc(2); |
| 1152 | if (get_prop_check_argc(retval, argc, &desc)) |
| 1153 | return TRUE; |
| 1154 | |
| 1155 | /* pop the insertion index; adjust to 0-based or end-based indexing */ |
| 1156 | int32_t idx = CVmBif::pop_int_val(vmg0_); |
| 1157 | idx += (idx < 0 ? get_ext()->len : -1); |
| 1158 | |
| 1159 | /* get or cast the string; leave it on the stack for gc protection */ |
| 1160 | vm_val_t srcval; |
| 1161 | const char *src = G_stk->get(0)->cast_to_string(vmg_ &srcval); |
| 1162 | *G_stk->get(0) = srcval; |
| 1163 | |
| 1164 | /* |
| 1165 | * Get the character length of the new text. This is the number of |
| 1166 | * characters we're overwriting (effectively deleting) in the current |
| 1167 | * buffer. |
| 1168 | */ |
| 1169 | size_t src_bytes = vmb_get_len(src); |
| 1170 | src += VMB_LEN; |
| 1171 | |
| 1172 | /* get the new text's character length */ |
| 1173 | utf8_ptr srcp((char *)src); |
| 1174 | size_t src_chars = srcp.len(src_bytes); |
| 1175 | |
| 1176 | /* splice the text */ |
| 1177 | splice_text(vmg_ self, idx, src_chars, src, src_bytes, TRUE); |
| 1178 | |
| 1179 | /* discard gc protection */ |
| 1180 | G_stk->discard(); |
| 1181 | |
| 1182 | /* handled */ |
| 1183 | retval->set_obj(self); |
| 1184 | return TRUE; |
| 1185 | } |
| 1186 | |
| 1187 | /* |
| 1188 | * property evaluator - delete |
| 1189 | */ |
| 1190 | int CVmObjStringBuffer::getp_delete(VMG_ vm_obj_id_t self, |
| 1191 | vm_val_t *retval, uint *oargc) |
| 1192 | { |
| 1193 | /* check arguments */ |
| 1194 | uint argc = (oargc != 0 ? *oargc : 0); |
| 1195 | static CVmNativeCodeDesc desc(1, 2); |
| 1196 | if (get_prop_check_argc(retval, oargc, &desc)) |
| 1197 | return TRUE; |
| 1198 | |
| 1199 | /* pop the deletion index and adjust to 0-based or end-based */ |
| 1200 | int32_t idx = CVmBif::pop_int_val(vmg0_); |
| 1201 | idx += (idx < 0 ? get_ext()->len : -1); |
| 1202 | |
| 1203 | /* pop the length, if provided; if not, delete the rest of the string */ |
| 1204 | int32_t len; |
| 1205 | if (argc >= 2) |
| 1206 | len = CVmBif::pop_int_val(vmg0_); |
| 1207 | else |
| 1208 | len = get_ext()->len; |
| 1209 | |
| 1210 | /* do the delete */ |
| 1211 | delete_text(vmg_ self, idx, len, TRUE); |
| 1212 | |
| 1213 | /* handled */ |
| 1214 | retval->set_obj(self); |
| 1215 | return TRUE; |
| 1216 | } |
| 1217 | |
| 1218 | /* |
| 1219 | * property evaluator - splice |
| 1220 | */ |
| 1221 | int CVmObjStringBuffer::getp_splice(VMG_ vm_obj_id_t self, |
| 1222 | vm_val_t *retval, uint *argc) |
| 1223 | { |
| 1224 | /* check arguments */ |
| 1225 | static CVmNativeCodeDesc desc(3); |
| 1226 | if (get_prop_check_argc(retval, argc, &desc)) |
| 1227 | return TRUE; |
| 1228 | |
| 1229 | /* pop the insertion index and adjust to 0-based or end-based */ |
| 1230 | int32_t idx = CVmBif::pop_int_val(vmg0_); |
| 1231 | idx += (idx < 0 ? get_ext()->len : -1); |
| 1232 | |
| 1233 | /* pop the deletion length */ |
| 1234 | int32_t del = CVmBif::pop_int_val(vmg0_); |
| 1235 | |
| 1236 | /* get or cast the string; leave it on the stack for gc protection */ |
| 1237 | vm_val_t strval; |
| 1238 | const char *str = G_stk->get(0)->cast_to_string(vmg_ &strval); |
| 1239 | *G_stk->get(0) = strval; |
| 1240 | |
| 1241 | /* do the insert */ |
| 1242 | splice_text(vmg_ self, idx, del, str + VMB_LEN, vmb_get_len(str), TRUE); |
| 1243 | |
| 1244 | /* discard gc protection */ |
| 1245 | G_stk->discard(); |
| 1246 | |
| 1247 | /* handled */ |
| 1248 | retval->set_obj(self); |
| 1249 | return TRUE; |
| 1250 | } |
| 1251 | |
| 1252 | /* |
| 1253 | * property evaluator - substr |
| 1254 | */ |
| 1255 | int CVmObjStringBuffer::getp_substr(VMG_ vm_obj_id_t self, |
| 1256 | vm_val_t *retval, uint *oargc) |
| 1257 | { |
| 1258 | /* remember the argument count */ |
| 1259 | uint argc = (oargc != 0 ? *oargc : 0); |
| 1260 | |
| 1261 | /* check arguments */ |
| 1262 | static CVmNativeCodeDesc desc(1, 2); |
| 1263 | if (get_prop_check_argc(retval, oargc, &desc)) |
| 1264 | return TRUE; |
| 1265 | |
| 1266 | /* retrieve the starting index, and adjust for 0-based or end-based */ |
| 1267 | int32_t idx = CVmBif::pop_long_val(vmg0_); |
| 1268 | idx += (idx < 0 ? get_ext()->len : -1); |
| 1269 | |
| 1270 | /* if there's a length, pop it; otherwise use the rest of the string */ |
| 1271 | int32_t len = get_ext()->len; |
| 1272 | if (argc >= 2) |
| 1273 | len = CVmBif::pop_long_val(vmg0_); |
| 1274 | |
| 1275 | /* retrieve the substring */ |
| 1276 | substr_to_string(vmg_ retval, idx, len); |
| 1277 | |
| 1278 | /* handled */ |
| 1279 | return TRUE; |
| 1280 | } |