| 1 | /* |
| 2 | * Copyright (c) 2002 by Michael J. Roberts. All Rights Reserved. |
| 3 | * |
| 4 | * Please see the accompanying license file, LICENSE.TXT, for information |
| 5 | * on using and copying this software. |
| 6 | */ |
| 7 | /* |
| 8 | Name |
| 9 | vmstrcmp.cpp - T3 String Comparator intrinsic class |
| 10 | Function |
| 11 | |
| 12 | Notes |
| 13 | |
| 14 | Modified |
| 15 | 09/05/02 MJRoberts - Creation |
| 16 | */ |
| 17 | |
| 18 | |
| 19 | #include <stdlib.h> |
| 20 | #include "os.h" |
| 21 | #include "utf8.h" |
| 22 | #include "vmuni.h" |
| 23 | #include "vmtype.h" |
| 24 | #include "vmobj.h" |
| 25 | #include "vmmeta.h" |
| 26 | #include "vmglob.h" |
| 27 | #include "vmstrcmp.h" |
| 28 | #include "vmstack.h" |
| 29 | #include "vmbif.h" |
| 30 | #include "vmfile.h" |
| 31 | #include "vmlst.h" |
| 32 | #include "vmstr.h" |
| 33 | #include "vmrun.h" |
| 34 | |
| 35 | |
| 36 | /* ------------------------------------------------------------------------ */ |
| 37 | /* |
| 38 | * Statics |
| 39 | */ |
| 40 | |
| 41 | /* metaclass registration */ |
| 42 | static CVmMetaclassStrComp metaclass_reg_obj; |
| 43 | CVmMetaclass *CVmObjStrComp::metaclass_reg_ = &metaclass_reg_obj; |
| 44 | |
| 45 | /* function table */ |
| 46 | int (CVmObjStrComp:: |
| 47 | *CVmObjStrComp::func_table_[])(VMG_ vm_obj_id_t self, |
| 48 | vm_val_t *retval, uint *argc) = |
| 49 | { |
| 50 | &CVmObjStrComp::getp_undef, |
| 51 | &CVmObjStrComp::getp_calc_hash, |
| 52 | &CVmObjStrComp::getp_match_values |
| 53 | }; |
| 54 | |
| 55 | /* ------------------------------------------------------------------------ */ |
| 56 | /* |
| 57 | * notify of deletion |
| 58 | */ |
| 59 | void CVmObjStrComp::notify_delete(VMG_ int in_root_set) |
| 60 | { |
| 61 | /* delete my extension data */ |
| 62 | delete_ext(vmg0_); |
| 63 | } |
| 64 | |
| 65 | /* |
| 66 | * Delete my extension data |
| 67 | */ |
| 68 | void CVmObjStrComp::delete_ext(VMG0_) |
| 69 | { |
| 70 | vmobj_strcmp_ext *ext = get_ext(); |
| 71 | |
| 72 | /* if I have an extension, delete it */ |
| 73 | if (ext != 0) |
| 74 | { |
| 75 | size_t i; |
| 76 | |
| 77 | /* delete each first-level mapping table */ |
| 78 | for (i = 0 ; i < countof(ext->equiv) ; ++i) |
| 79 | { |
| 80 | /* if this table is present, delete it */ |
| 81 | if (ext->equiv[i] != 0) |
| 82 | t3free(ext->equiv[i]); |
| 83 | } |
| 84 | |
| 85 | /* delete and forget our extension */ |
| 86 | G_mem->get_var_heap()->free_mem(ext_); |
| 87 | ext_ = 0; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | /* ------------------------------------------------------------------------ */ |
| 92 | /* |
| 93 | * set a property |
| 94 | */ |
| 95 | void CVmObjStrComp::set_prop(VMG_ class CVmUndo *, |
| 96 | vm_obj_id_t, vm_prop_id_t, |
| 97 | const vm_val_t *) |
| 98 | { |
| 99 | /* we have no properties to set */ |
| 100 | err_throw(VMERR_INVALID_SETPROP); |
| 101 | } |
| 102 | |
| 103 | /* ------------------------------------------------------------------------ */ |
| 104 | /* |
| 105 | * Get a property |
| 106 | */ |
| 107 | int CVmObjStrComp::get_prop(VMG_ vm_prop_id_t prop, vm_val_t *val, |
| 108 | vm_obj_id_t self, vm_obj_id_t *source_obj, |
| 109 | uint *argc) |
| 110 | { |
| 111 | uint func_idx; |
| 112 | |
| 113 | /* translate the property into a function vector index */ |
| 114 | func_idx = G_meta_table |
| 115 | ->prop_to_vector_idx(metaclass_reg_->get_reg_idx(), prop); |
| 116 | |
| 117 | /* call the function, if we found it */ |
| 118 | if ((this->*func_table_[func_idx])(vmg_ self, val, argc)) |
| 119 | { |
| 120 | *source_obj = metaclass_reg_->get_class_obj(vmg0_); |
| 121 | return TRUE; |
| 122 | } |
| 123 | |
| 124 | /* not found - inherit default handling */ |
| 125 | return CVmObject::get_prop(vmg_ prop, val, self, source_obj, argc); |
| 126 | } |
| 127 | |
| 128 | |
| 129 | /* ------------------------------------------------------------------------ */ |
| 130 | /* |
| 131 | * Abstract equivalence mapping reader. This can be implemented on |
| 132 | * different underlying data sources. |
| 133 | */ |
| 134 | class CVmObjStrCompMapReader |
| 135 | { |
| 136 | public: |
| 137 | /* |
| 138 | * Read the next equivalence mapping. We fill in *ref_ch with the |
| 139 | * reference character, *uc_result_flags with the upper-case result |
| 140 | * flags, and *lc_result_flags with the lower-case result flags. On |
| 141 | * input, *value_ch_cnt is the maximum number of value characters we |
| 142 | * can store in the buffer at val_buf; on return, this is the number |
| 143 | * of characters in the mapping, which might be higher than the number |
| 144 | * originally in the buffer. In any case, we will write no more than |
| 145 | * the allowed buffer size as given by *value_ch_cnt on input (so we |
| 146 | * might indicate a higher number than we actually wrote: we always |
| 147 | * return the actual value mapping size, even if we couldn't store the |
| 148 | * whole thing because of a lack of buffer space). |
| 149 | * |
| 150 | * This routine should retrieve one mapping each time it's called, and |
| 151 | * then move on to the next mapping. The caller is responsible for |
| 152 | * making sure that this routine is called the correct number of |
| 153 | * times, so we don't have to worry about running out of mappings. |
| 154 | */ |
| 155 | virtual void read_mapping(VMG_ wchar_t *ref_ch, |
| 156 | unsigned long *uc_result_flags, |
| 157 | unsigned long *lc_result_flags, |
| 158 | wchar_t *val_buf, size_t *value_ch_cnt) = 0; |
| 159 | }; |
| 160 | |
| 161 | /* |
| 162 | * Stream-based mapping reader. This reads mappings from a stream that |
| 163 | * uses our serialization format for image and saved-state files. |
| 164 | */ |
| 165 | class CVmObjStrCompMapReaderStream: public CVmObjStrCompMapReader |
| 166 | { |
| 167 | public: |
| 168 | CVmObjStrCompMapReaderStream(CVmStream *str) { str_ = str; } |
| 169 | |
| 170 | /* read a mapping */ |
| 171 | virtual void read_mapping(VMG_ wchar_t *ref_ch, |
| 172 | unsigned long *uc_result_flags, |
| 173 | unsigned long *lc_result_flags, |
| 174 | wchar_t *val_buf, size_t *value_ch_cnt) |
| 175 | { |
| 176 | size_t copy_limit; |
| 177 | size_t copy_size; |
| 178 | size_t i; |
| 179 | |
| 180 | /* limit our value character copying to the actual buffer size */ |
| 181 | copy_limit = *value_ch_cnt; |
| 182 | |
| 183 | /* read the header values */ |
| 184 | *ref_ch = (wchar_t)str_->read_uint2(); |
| 185 | copy_size = *value_ch_cnt = str_->read_byte(); |
| 186 | *uc_result_flags = str_->read_uint4(); |
| 187 | *lc_result_flags = str_->read_uint4(); |
| 188 | |
| 189 | /* limit copying to the actual buffer size */ |
| 190 | if (copy_size > copy_limit) |
| 191 | copy_size = copy_limit; |
| 192 | |
| 193 | /* read the values */ |
| 194 | for (i = 0 ; i < copy_size ; ++i) |
| 195 | *val_buf++ = (wchar_t)str_->read_uint2(); |
| 196 | |
| 197 | /* skip any values from the input that we weren't able to store */ |
| 198 | for ( ; i < *value_ch_cnt ; ++i) |
| 199 | str_->read_uint2(); |
| 200 | } |
| 201 | |
| 202 | protected: |
| 203 | /* my stream */ |
| 204 | CVmStream *str_; |
| 205 | }; |
| 206 | |
| 207 | /* |
| 208 | * Constructor-list mapping reader. This reads mappings from list data in |
| 209 | * the format that we use in our constructor. |
| 210 | */ |
| 211 | class CVmObjStrCompMapReaderList: public CVmObjStrCompMapReader |
| 212 | { |
| 213 | public: |
| 214 | CVmObjStrCompMapReaderList(const vm_val_t *lst) |
| 215 | { |
| 216 | /* remember the list */ |
| 217 | lst_ = lst; |
| 218 | |
| 219 | /* start at the first element */ |
| 220 | idx_ = 1; |
| 221 | } |
| 222 | |
| 223 | /* read a mapping */ |
| 224 | virtual void read_mapping(VMG_ wchar_t *ref_ch, |
| 225 | unsigned long *uc_result_flags, |
| 226 | unsigned long *lc_result_flags, |
| 227 | wchar_t *val_buf, size_t *value_ch_cnt) |
| 228 | { |
| 229 | vm_val_t val; |
| 230 | vm_val_t sublst; |
| 231 | const char *refstr; |
| 232 | const char *valstr; |
| 233 | size_t copy_rem; |
| 234 | size_t rem; |
| 235 | utf8_ptr p; |
| 236 | |
| 237 | /* note the size limit of the caller's value string buffer */ |
| 238 | copy_rem = *value_ch_cnt; |
| 239 | |
| 240 | /* retrieve the next element of the list */ |
| 241 | lst_->ll_index(vmg_ &sublst, idx_); |
| 242 | |
| 243 | /* get the value as a sublist */ |
| 244 | if (!sublst.is_listlike(vmg0_) || sublst.ll_length(vmg0_) < 0) |
| 245 | err_throw(VMERR_BAD_TYPE_BIF); |
| 246 | |
| 247 | /* retrieve the reference character string (sublst's 1st element) */ |
| 248 | sublst.ll_index(vmg_ &val, 1); |
| 249 | refstr = val.get_as_string(vmg0_); |
| 250 | |
| 251 | /* retrieve the value string (sublst's 2nd element) */ |
| 252 | sublst.ll_index(vmg_ &val, 2); |
| 253 | valstr = val.get_as_string(vmg0_); |
| 254 | |
| 255 | /* make sure the reference and value strings are indeed strings */ |
| 256 | if (refstr == 0 || valstr == 0) |
| 257 | err_throw(VMERR_BAD_TYPE_BIF); |
| 258 | |
| 259 | /* we need at least one character in each string */ |
| 260 | if (vmb_get_len(refstr) == 0 || vmb_get_len(valstr) == 0) |
| 261 | err_throw(VMERR_BAD_VAL_BIF); |
| 262 | |
| 263 | /* fill in the caller's reference character from the ref string */ |
| 264 | *ref_ch = utf8_ptr::s_getch(refstr + VMB_LEN); |
| 265 | |
| 266 | /* store the characters of the value string, up to the buffer limit */ |
| 267 | p.set((char *)valstr + VMB_LEN); |
| 268 | rem = vmb_get_len(valstr); |
| 269 | for (*value_ch_cnt = 0 ; rem != 0 ; p.inc(&rem)) |
| 270 | { |
| 271 | /* if we have room, copy this character */ |
| 272 | if (copy_rem != 0) |
| 273 | { |
| 274 | /* copy the character */ |
| 275 | *val_buf++ = p.getch(); |
| 276 | |
| 277 | /* count it */ |
| 278 | --copy_rem; |
| 279 | } |
| 280 | |
| 281 | /* count it in the actual length */ |
| 282 | ++(*value_ch_cnt); |
| 283 | } |
| 284 | |
| 285 | /* get the upper-case flags (sublst's 3rd element) */ |
| 286 | sublst.ll_index(vmg_ &val, 3); |
| 287 | if (val.typ != VM_INT) |
| 288 | err_throw(VMERR_BAD_TYPE_BIF); |
| 289 | *uc_result_flags = val.val.intval; |
| 290 | |
| 291 | /* get the lower-case flags (sublst's 4th element) */ |
| 292 | sublst.ll_index(vmg_ &val, 4); |
| 293 | if (val.typ != VM_INT) |
| 294 | err_throw(VMERR_BAD_TYPE_BIF); |
| 295 | *lc_result_flags = val.val.intval; |
| 296 | |
| 297 | /* we're done with this mapping, so advance to the next one */ |
| 298 | ++idx_; |
| 299 | } |
| 300 | |
| 301 | protected: |
| 302 | /* my list data */ |
| 303 | const vm_val_t *lst_; |
| 304 | |
| 305 | /* the list index of the next mapping to retrieve */ |
| 306 | size_t idx_; |
| 307 | }; |
| 308 | |
| 309 | /* ------------------------------------------------------------------------ */ |
| 310 | /* |
| 311 | * Create from stack. We take the following constructor arguments: |
| 312 | * |
| 313 | * trunc_len (int: truncation length) |
| 314 | *. case_sensitive (bool: case sensitivity flag) |
| 315 | *. mappings (list: equivalence mappings) |
| 316 | * |
| 317 | * Our equivalence mappings are given as a list of lists; the main list |
| 318 | * consists of one sublist per mapping. Each mapping sublist looks like |
| 319 | * this: |
| 320 | * |
| 321 | * ['ref_char', 'val_string', uc_flags, lc_flag] |
| 322 | * |
| 323 | * The 'ref_char' is a one-character string giving the reference character |
| 324 | * of the mapping, and 'val_string' is a string of one or more characters |
| 325 | * that can match the reference character in a value (input) string. |
| 326 | * uc_flags and lc_flags are integers giving the upper-case and lower-case |
| 327 | * flags (respectively) that are to be added to the match result code when |
| 328 | * the mapping is used to match a pair of strings. |
| 329 | */ |
| 330 | vm_obj_id_t CVmObjStrComp::create_from_stack( |
| 331 | VMG_ const uchar **pc_ptr, uint argc) |
| 332 | { |
| 333 | size_t trunc_len; |
| 334 | int case_sensitive; |
| 335 | const vm_val_t *lst; |
| 336 | vm_obj_id_t id; |
| 337 | CVmObjStrComp *obj; |
| 338 | size_t equiv_cnt; |
| 339 | size_t total_chars; |
| 340 | |
| 341 | /* check arguments */ |
| 342 | if (argc != 3) |
| 343 | err_throw(VMERR_WRONG_NUM_OF_ARGS); |
| 344 | |
| 345 | /* pop the truncation length parameter */ |
| 346 | if (G_stk->get(0)->typ == VM_NIL) |
| 347 | { |
| 348 | /* it's nil, so truncation is not allowed */ |
| 349 | trunc_len = 0; |
| 350 | G_stk->discard(); |
| 351 | } |
| 352 | else |
| 353 | { |
| 354 | /* retrieve the truncation length as an integer */ |
| 355 | trunc_len = CVmBif::pop_int_val(vmg0_); |
| 356 | } |
| 357 | |
| 358 | /* get the case sensitivity flag */ |
| 359 | case_sensitive = CVmBif::pop_bool_val(vmg0_); |
| 360 | |
| 361 | /* |
| 362 | * retrieve the mapping list, but leave it on the stack (for gc |
| 363 | * protection) |
| 364 | */ |
| 365 | if (G_stk->get(0)->typ == VM_NIL) |
| 366 | { |
| 367 | /* there are no mappings */ |
| 368 | lst = 0; |
| 369 | equiv_cnt = 0; |
| 370 | total_chars = 0; |
| 371 | } |
| 372 | else |
| 373 | { |
| 374 | size_t i; |
| 375 | int ec; |
| 376 | |
| 377 | /* get the list value from the argument */ |
| 378 | lst = G_stk->get(0); |
| 379 | if (!lst->is_listlike(vmg0_) |
| 380 | || (ec = lst->ll_length(vmg0_)) < 0) |
| 381 | err_throw(VMERR_BAD_TYPE_BIF); |
| 382 | |
| 383 | /* run through the list and count the value string characters */ |
| 384 | for (i = 1, total_chars = 0, equiv_cnt = ec ; i <= equiv_cnt ; ++i) |
| 385 | { |
| 386 | vm_val_t sublst; |
| 387 | vm_val_t val; |
| 388 | const char *strp; |
| 389 | utf8_ptr ustrp; |
| 390 | |
| 391 | /* get this mapping from the list */ |
| 392 | lst->ll_index(vmg_ &sublst, i); |
| 393 | |
| 394 | /* make sure it's a sublist */ |
| 395 | if (!sublst.is_listlike(vmg0_)) |
| 396 | err_throw(VMERR_BAD_TYPE_BIF); |
| 397 | |
| 398 | /* |
| 399 | * get the second element of the mapping sublist - this is the |
| 400 | * value string |
| 401 | */ |
| 402 | sublst.ll_index(vmg_ &val, 2); |
| 403 | if ((strp = val.get_as_string(vmg0_)) == 0) |
| 404 | err_throw(VMERR_BAD_TYPE_BIF); |
| 405 | |
| 406 | /* add the character length of the string to the total */ |
| 407 | ustrp.set((char *)strp + VMB_LEN); |
| 408 | total_chars += ustrp.len(vmb_get_len(strp)); |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | /* create the new object */ |
| 413 | id = vm_new_id(vmg_ FALSE, FALSE, FALSE); |
| 414 | obj = new (vmg_ id) CVmObjStrComp(); |
| 415 | |
| 416 | /* set up a list-based mapping reader */ |
| 417 | CVmObjStrCompMapReaderList reader(lst); |
| 418 | |
| 419 | /* allocate and initialize the new object's extension */ |
| 420 | obj->alloc_ext(vmg_ trunc_len, case_sensitive, equiv_cnt, total_chars, |
| 421 | &reader); |
| 422 | |
| 423 | /* discard the gc protection */ |
| 424 | G_stk->discard(); |
| 425 | |
| 426 | /* return the new object */ |
| 427 | return id; |
| 428 | } |
| 429 | |
| 430 | /* ------------------------------------------------------------------------ */ |
| 431 | /* |
| 432 | * Load from an image file |
| 433 | */ |
| 434 | void CVmObjStrComp::load_from_image(VMG_ vm_obj_id_t /*self*/, |
| 435 | const char *ptr, size_t len) |
| 436 | { |
| 437 | /* load my image data */ |
| 438 | CVmReadOnlyMemoryStream str(ptr, len); |
| 439 | load_from_stream(vmg_ &str); |
| 440 | } |
| 441 | |
| 442 | /* |
| 443 | * Load from an abstract stream |
| 444 | */ |
| 445 | void CVmObjStrComp::load_from_stream(VMG_ CVmStream *str) |
| 446 | { |
| 447 | unsigned int trunc_len; |
| 448 | unsigned int flags; |
| 449 | unsigned int equiv_cnt; |
| 450 | unsigned int total_chars; |
| 451 | |
| 452 | /* load the fixed header */ |
| 453 | trunc_len = str->read_uint2(); |
| 454 | flags = str->read_uint2(); |
| 455 | equiv_cnt = str->read_uint2(); |
| 456 | total_chars = str->read_uint2(); |
| 457 | |
| 458 | /* set up a stream-based mapping reader */ |
| 459 | CVmObjStrCompMapReaderStream reader(str); |
| 460 | |
| 461 | /* allocate and initialize our extension */ |
| 462 | alloc_ext(vmg_ trunc_len, (flags & 0x0001) != 0, equiv_cnt, total_chars, |
| 463 | &reader); |
| 464 | } |
| 465 | |
| 466 | /* |
| 467 | * Allocate and initialize our extension |
| 468 | */ |
| 469 | void CVmObjStrComp::alloc_ext(VMG_ size_t trunc_len, int case_sensitive, |
| 470 | size_t equiv_cnt, size_t total_chars, |
| 471 | CVmObjStrCompMapReader *reader) |
| 472 | { |
| 473 | size_t siz; |
| 474 | vmobj_strcmp_ext *ext; |
| 475 | vmobj_strcmp_equiv *nxt_equiv; |
| 476 | wchar_t *nxt_ch; |
| 477 | size_t ch_rem; |
| 478 | size_t i; |
| 479 | size_t idx1, idx2; |
| 480 | |
| 481 | /* delete my extension, if I have one already */ |
| 482 | delete_ext(vmg0_); |
| 483 | |
| 484 | /* |
| 485 | * Calculate how much space we need for our extension. In addition to |
| 486 | * the fixed part, allocate space for one vmobj_strcmp_equiv structure |
| 487 | * per mapping, plus the wchar_t's for the value mappings. |
| 488 | */ |
| 489 | siz = sizeof(vmobj_strcmp_ext) |
| 490 | + (equiv_cnt * sizeof(vmobj_strcmp_equiv)) |
| 491 | + (total_chars * sizeof(wchar_t)); |
| 492 | |
| 493 | /* allocate our new extension */ |
| 494 | ext_ = (char *)G_mem->get_var_heap()->alloc_mem(siz, this); |
| 495 | ext = get_ext(); |
| 496 | |
| 497 | /* |
| 498 | * set up our suballocation pool pointers: put the equivalence mapping |
| 499 | * structures after the fixed part of the extension, and put the |
| 500 | * wchar_t's after the equivalence mappings |
| 501 | */ |
| 502 | nxt_equiv = (vmobj_strcmp_equiv *)(ext + 1); |
| 503 | nxt_ch = (wchar_t *)(&nxt_equiv[equiv_cnt]); |
| 504 | ch_rem = total_chars; |
| 505 | |
| 506 | /* initialize the extension structure */ |
| 507 | ext->trunc_len = trunc_len; |
| 508 | ext->case_sensitive = case_sensitive; |
| 509 | |
| 510 | /* |
| 511 | * we have no equivalence mappings installed yet, so clear out the |
| 512 | * first tier of the mapping array |
| 513 | */ |
| 514 | for (i = 0 ; i < countof(ext->equiv) ; ++i) |
| 515 | ext->equiv[i] = 0; |
| 516 | |
| 517 | /* load the mappings */ |
| 518 | for (i = 0 ; i < equiv_cnt ; ++i, ++nxt_equiv) |
| 519 | { |
| 520 | wchar_t ref_ch; |
| 521 | |
| 522 | /* |
| 523 | * set up our equivalent's value buffer with the remainder of our |
| 524 | * main buffer |
| 525 | */ |
| 526 | nxt_equiv->val_ch = nxt_ch; |
| 527 | nxt_equiv->val_ch_cnt = ch_rem; |
| 528 | |
| 529 | /* read the mapping */ |
| 530 | reader->read_mapping(vmg_ &ref_ch, |
| 531 | &nxt_equiv->uc_result_flags, |
| 532 | &nxt_equiv->lc_result_flags, |
| 533 | nxt_equiv->val_ch, &nxt_equiv->val_ch_cnt); |
| 534 | |
| 535 | /* deduct this mapping from our main value character buffer */ |
| 536 | nxt_ch += nxt_equiv->val_ch_cnt; |
| 537 | ch_rem -= nxt_equiv->val_ch_cnt; |
| 538 | |
| 539 | /* if we don't have a first-tier table for this character, add one */ |
| 540 | idx1 = (ref_ch >> 8) & 0xFF; |
| 541 | idx2 = (ref_ch & 0xFF); |
| 542 | if (ext->equiv[idx1] == 0) |
| 543 | { |
| 544 | vmobj_strcmp_equiv **p; |
| 545 | size_t j; |
| 546 | |
| 547 | /* allocate a first-tier table for this index */ |
| 548 | ext->equiv[idx1] = p = (vmobj_strcmp_equiv **)t3malloc( |
| 549 | 256 * sizeof(vmobj_strcmp_equiv *)); |
| 550 | |
| 551 | /* clear out the first-tier table */ |
| 552 | for (j = 0 ; j < 256 ; ++j, ++p) |
| 553 | *p = 0; |
| 554 | } |
| 555 | |
| 556 | /* set the mapping for this character */ |
| 557 | ext->equiv[idx1][idx2] = nxt_equiv; |
| 558 | } |
| 559 | } |
| 560 | |
| 561 | /* ------------------------------------------------------------------------ */ |
| 562 | /* |
| 563 | * save to a file |
| 564 | */ |
| 565 | void CVmObjStrComp::save_to_file(VMG_ class CVmFile *fp) |
| 566 | { |
| 567 | /* write our data to the file */ |
| 568 | CVmFileStream str(fp); |
| 569 | write_to_stream(vmg_ &str, 0); |
| 570 | } |
| 571 | |
| 572 | /* |
| 573 | * Serialize to a stream |
| 574 | */ |
| 575 | ulong CVmObjStrComp::write_to_stream(VMG_ CVmStream *str, ulong *bytes_avail) |
| 576 | { |
| 577 | wchar_t ref_ch_base; |
| 578 | vmobj_strcmp_ext *ext = get_ext(); |
| 579 | size_t i; |
| 580 | vmobj_strcmp_equiv ***p; |
| 581 | size_t total_value_ch; |
| 582 | size_t equiv_cnt; |
| 583 | size_t need_size; |
| 584 | |
| 585 | /* get the mapping totals */ |
| 586 | count_equiv_mappings(&equiv_cnt, &total_value_ch); |
| 587 | |
| 588 | /* |
| 589 | * Calculate our space needs. We need 8 bytes for the fixed header, |
| 590 | * 11 bytes per equivalent mapping, and 2 bytes per value string |
| 591 | * character. |
| 592 | */ |
| 593 | need_size = 8 + (11 * equiv_cnt) + (2 * total_value_ch); |
| 594 | |
| 595 | /* if we have a size limit, check to make sure we can abide by it */ |
| 596 | if (bytes_avail != 0 && need_size > *bytes_avail) |
| 597 | { |
| 598 | /* |
| 599 | * there's not enough space in the output stream for us, so don't |
| 600 | * write anything at all; simply return the amount of space we |
| 601 | * need |
| 602 | */ |
| 603 | return need_size; |
| 604 | } |
| 605 | |
| 606 | /* write out the serialization structure header */ |
| 607 | str->write_uint2(ext->trunc_len); |
| 608 | str->write_uint2(ext->case_sensitive ? 0x0001 : 0x0000); |
| 609 | str->write_uint2(equiv_cnt); |
| 610 | str->write_uint2(total_value_ch); |
| 611 | |
| 612 | /* run through our equivalence table again and write the mappings */ |
| 613 | for (ref_ch_base = 0, i = 0, p = ext->equiv ; i < countof(ext->equiv) ; |
| 614 | ++i, ++p, ref_ch_base += 256) |
| 615 | { |
| 616 | vmobj_strcmp_equiv **ep; |
| 617 | size_t j; |
| 618 | |
| 619 | /* if this first-tier mapping is unused, skip it */ |
| 620 | if (*p == 0) |
| 621 | continue; |
| 622 | |
| 623 | /* run through our second-level table */ |
| 624 | for (j = 0, ep = *p ; j < 256 ; ++j, ++ep) |
| 625 | { |
| 626 | /* if this mapping is used, write it out */ |
| 627 | if (*ep != 0) |
| 628 | { |
| 629 | size_t k; |
| 630 | wchar_t *vp; |
| 631 | |
| 632 | /* write the fixed part of the mapping */ |
| 633 | str->write_uint2(ref_ch_base + j); |
| 634 | str->write_byte((uchar)(*ep)->val_ch_cnt); |
| 635 | str->write_uint4((*ep)->uc_result_flags); |
| 636 | str->write_uint4((*ep)->lc_result_flags); |
| 637 | |
| 638 | /* write the value mapping characters */ |
| 639 | for (k = (*ep)->val_ch_cnt, vp = (*ep)->val_ch ; k != 0 ; |
| 640 | --k, ++vp) |
| 641 | { |
| 642 | /* write this character */ |
| 643 | str->write_uint2(*vp); |
| 644 | } |
| 645 | } |
| 646 | } |
| 647 | } |
| 648 | |
| 649 | /* return our space needs */ |
| 650 | return need_size; |
| 651 | } |
| 652 | |
| 653 | /* |
| 654 | * Count the equivalence mappings. |
| 655 | */ |
| 656 | void CVmObjStrComp::count_equiv_mappings(size_t *equiv_cnt, |
| 657 | size_t *total_value_ch) |
| 658 | { |
| 659 | vmobj_strcmp_ext *ext = get_ext(); |
| 660 | size_t i; |
| 661 | vmobj_strcmp_equiv ***p; |
| 662 | |
| 663 | /* run through our table and count up the mappings */ |
| 664 | for (*total_value_ch = 0, *equiv_cnt = 0, i = 0, p = ext->equiv ; |
| 665 | i < countof(ext->equiv) ; ++i, ++p) |
| 666 | { |
| 667 | vmobj_strcmp_equiv **ep; |
| 668 | size_t j; |
| 669 | |
| 670 | /* if this first-tier mapping is unused, skip it */ |
| 671 | if (*p == 0) |
| 672 | continue; |
| 673 | |
| 674 | /* run through our second-level table */ |
| 675 | for (j = 0, ep = *p ; j < 256 ; ++j, ++ep) |
| 676 | { |
| 677 | /* if this mapping is used, count it */ |
| 678 | if (*ep != 0) |
| 679 | { |
| 680 | /* count this equivalent mapping */ |
| 681 | ++(*equiv_cnt); |
| 682 | |
| 683 | /* count its value mapping characters in the total */ |
| 684 | *total_value_ch += (*ep)->val_ch_cnt; |
| 685 | } |
| 686 | } |
| 687 | } |
| 688 | } |
| 689 | |
| 690 | /* ------------------------------------------------------------------------ */ |
| 691 | /* |
| 692 | * restore from a file |
| 693 | */ |
| 694 | void CVmObjStrComp::restore_from_file(VMG_ vm_obj_id_t /*self*/, |
| 695 | class CVmFile *fp, CVmObjFixup *) |
| 696 | { |
| 697 | /* load from the file */ |
| 698 | CVmFileStream str(fp); |
| 699 | load_from_stream(vmg_ &str); |
| 700 | } |
| 701 | |
| 702 | /* ------------------------------------------------------------------------ */ |
| 703 | /* |
| 704 | * property evaluator - calculate a hash value |
| 705 | */ |
| 706 | int CVmObjStrComp::getp_calc_hash(VMG_ vm_obj_id_t /*self*/, |
| 707 | vm_val_t *retval, uint *argc) |
| 708 | { |
| 709 | static CVmNativeCodeDesc desc(1); |
| 710 | const char *strp; |
| 711 | |
| 712 | /* check arguments */ |
| 713 | if (get_prop_check_argc(retval, argc, &desc)) |
| 714 | return TRUE; |
| 715 | |
| 716 | /* |
| 717 | * retrieve the string argument (it must be a string), but leave it on |
| 718 | * the stack for now, for gc protection |
| 719 | */ |
| 720 | strp = G_stk->get(0)->get_as_string(vmg0_); |
| 721 | if (strp == 0) |
| 722 | err_throw(VMERR_BAD_TYPE_BIF); |
| 723 | |
| 724 | /* calculate the hash value and return it */ |
| 725 | retval->set_int(calc_str_hash(strp + VMB_LEN, vmb_get_len(strp))); |
| 726 | |
| 727 | /* discard gc protection */ |
| 728 | G_stk->discard(); |
| 729 | |
| 730 | /* handled */ |
| 731 | return TRUE; |
| 732 | } |
| 733 | |
| 734 | /* |
| 735 | * Hash adder |
| 736 | */ |
| 737 | struct StrCompHashAdder |
| 738 | { |
| 739 | StrCompHashAdder(size_t trunc_len) |
| 740 | { |
| 741 | this->trunc_len = trunc_len; |
| 742 | this->has_trunc = (trunc_len != 0); |
| 743 | hash = 0; |
| 744 | } |
| 745 | |
| 746 | int add(wchar_t ch) |
| 747 | { |
| 748 | /* add the character to the hash */ |
| 749 | hash += ch; |
| 750 | hash &= 0xFFFF; |
| 751 | |
| 752 | /* if there's a truncation limit, count this against the limit */ |
| 753 | if (has_trunc && --trunc_len == 0) |
| 754 | { |
| 755 | /* we've reached the limit */ |
| 756 | return FALSE; |
| 757 | } |
| 758 | else |
| 759 | { |
| 760 | /* haven't reached the limit yet */ |
| 761 | return TRUE; |
| 762 | } |
| 763 | } |
| 764 | |
| 765 | int done() const { return has_trunc && trunc_len == 0; } |
| 766 | |
| 767 | /* truncation limit */ |
| 768 | size_t trunc_len; |
| 769 | |
| 770 | /* is there a truncation limit? */ |
| 771 | int has_trunc; |
| 772 | |
| 773 | /* the hash code */ |
| 774 | unsigned int hash; |
| 775 | }; |
| 776 | |
| 777 | /* |
| 778 | * Calculate a hash value |
| 779 | */ |
| 780 | unsigned int CVmObjStrComp::calc_str_hash(const char *strp, size_t len) |
| 781 | { |
| 782 | /* get my extension */ |
| 783 | vmobj_strcmp_ext *ext = get_ext(); |
| 784 | |
| 785 | /* set up to scan the string */ |
| 786 | utf8_ptr p((char *)strp); |
| 787 | |
| 788 | /* |
| 789 | * Scan the string. Limit the scan to our truncation length, counting |
| 790 | * substitution expansions and case folding expansions, because we |
| 791 | * don't want to distinguish hash buckets beyond the truncation point. |
| 792 | * If we did, a truncated string wouldn't hash into the same bucket as |
| 793 | * a longer string it matches; all matching strings are required to go |
| 794 | * into the same bucket, so we can't have such a hash mismatch. |
| 795 | */ |
| 796 | StrCompHashAdder hash(ext->trunc_len); |
| 797 | for ( ; len != 0 ; p.inc(&len)) |
| 798 | { |
| 799 | /* get the current character */ |
| 800 | wchar_t ch = p.getch(); |
| 801 | |
| 802 | /* check for a substitution mapping for this character */ |
| 803 | vmobj_strcmp_equiv **t1, *eq; |
| 804 | if ((t1 = ext->equiv[(ch >> 8) & 0xFF]) != 0 |
| 805 | && (eq = t1[ch & 0xFF]) != 0) |
| 806 | { |
| 807 | /* |
| 808 | * This character has a mapping, so add the contribution from |
| 809 | * the canonical form of the character, which is the value |
| 810 | * side of the mapping. |
| 811 | */ |
| 812 | wchar_t *vp; |
| 813 | size_t vlen; |
| 814 | for (vp = eq->val_ch, vlen = eq->val_ch_cnt ; vlen != 0 ; |
| 815 | ++vp, --vlen) |
| 816 | { |
| 817 | /* get this character */ |
| 818 | ch = *vp; |
| 819 | |
| 820 | /* |
| 821 | * use case folding if we're insensitive to case and the |
| 822 | * character has a case folding |
| 823 | */ |
| 824 | const wchar_t *f; |
| 825 | if (!ext->case_sensitive && (f = t3_to_fold(ch)) != 0) |
| 826 | { |
| 827 | /* add the folded expansion to the hash */ |
| 828 | for (const wchar_t *f = t3_to_fold(ch) ; *f != 0 ; ++f) |
| 829 | { |
| 830 | if (!hash.add(*f)) |
| 831 | return hash.hash; |
| 832 | } |
| 833 | } |
| 834 | else |
| 835 | { |
| 836 | if (!hash.add(ch)) |
| 837 | return hash.hash; |
| 838 | } |
| 839 | } |
| 840 | } |
| 841 | else |
| 842 | { |
| 843 | /* |
| 844 | * if we're not sensitive to case, and there's a case folding |
| 845 | * available, use the case-folded representation of a character |
| 846 | * for its hash value |
| 847 | */ |
| 848 | const wchar_t *f; |
| 849 | if (!ext->case_sensitive && (f = t3_to_fold(ch)) != 0) |
| 850 | { |
| 851 | /* add the folded expansion to the hash */ |
| 852 | for (const wchar_t *f = t3_to_fold(ch) ; *f != 0 ; ++f) |
| 853 | { |
| 854 | if (!hash.add(*f)) |
| 855 | return hash.hash; |
| 856 | } |
| 857 | } |
| 858 | else |
| 859 | { |
| 860 | /* exact case only - add the character as-is */ |
| 861 | if (!hash.add(ch)) |
| 862 | return hash.hash; |
| 863 | } |
| 864 | } |
| 865 | } |
| 866 | |
| 867 | /* return the hash code */ |
| 868 | return hash.hash; |
| 869 | } |
| 870 | |
| 871 | /* ------------------------------------------------------------------------ */ |
| 872 | /* |
| 873 | * Pre-defined return flag values. |
| 874 | */ |
| 875 | |
| 876 | #define RF_MATCH 0x0001 /* the string matched */ |
| 877 | #define RF_CASEFOLD 0x0002 /* matched with case folding */ |
| 878 | #define RF_TRUNC 0x0004 /* matched with truncation */ |
| 879 | |
| 880 | |
| 881 | /* |
| 882 | * property evaluator - calculate a hash value |
| 883 | */ |
| 884 | int CVmObjStrComp::getp_match_values(VMG_ vm_obj_id_t /*self*/, |
| 885 | vm_val_t *retval, uint *argc) |
| 886 | { |
| 887 | static CVmNativeCodeDesc desc(2); |
| 888 | const char *valstr; |
| 889 | const char *refstr; |
| 890 | |
| 891 | /* check arguments */ |
| 892 | if (get_prop_check_argc(retval, argc, &desc)) |
| 893 | return TRUE; |
| 894 | |
| 895 | /* retrieve the strings, but leave them on the stack for gc protection */ |
| 896 | valstr = G_stk->get(0)->get_as_string(vmg0_); |
| 897 | refstr = G_stk->get(1)->get_as_string(vmg0_); |
| 898 | |
| 899 | /* make sure they're valid strings */ |
| 900 | if (valstr == 0 || refstr == 0) |
| 901 | err_throw(VMERR_BAD_TYPE_BIF); |
| 902 | |
| 903 | /* compare the strings and return the result */ |
| 904 | retval->set_int(match_strings(valstr + VMB_LEN, vmb_get_len(valstr), |
| 905 | refstr + VMB_LEN, vmb_get_len(refstr))); |
| 906 | |
| 907 | /* discard the gc protection */ |
| 908 | G_stk->discard(2); |
| 909 | |
| 910 | /* handled */ |
| 911 | return TRUE; |
| 912 | } |
| 913 | |
| 914 | /* |
| 915 | * Match two strings |
| 916 | */ |
| 917 | unsigned long CVmObjStrComp::match_strings(const char *valstr, size_t vallen, |
| 918 | const char *refstr, size_t reflen) |
| 919 | { |
| 920 | vmobj_strcmp_ext *ext = get_ext(); |
| 921 | utf8_ptr valp; |
| 922 | utf8_ptr refp; |
| 923 | unsigned long ret; |
| 924 | int fold_case = !(ext->case_sensitive); |
| 925 | |
| 926 | /* set up to scan the strings */ |
| 927 | valp.set((char *)valstr); |
| 928 | refp.set((char *)refstr); |
| 929 | |
| 930 | /* start with no return flags */ |
| 931 | ret = 0; |
| 932 | |
| 933 | /* scan the strings */ |
| 934 | while (vallen != 0 && reflen != 0) |
| 935 | { |
| 936 | /* get each character */ |
| 937 | wchar_t valch = valp.getch(); |
| 938 | wchar_t refch = refp.getch(); |
| 939 | |
| 940 | /* check for an exact match first */ |
| 941 | if (refch == valch) |
| 942 | { |
| 943 | /* it's an exact match - skip this character in each string */ |
| 944 | valp.inc(&vallen); |
| 945 | refp.inc(&reflen); |
| 946 | continue; |
| 947 | } |
| 948 | |
| 949 | /* check for a case-folded match if we're insensitive to case */ |
| 950 | if (fold_case |
| 951 | && t3_compare_case_fold_min(valp, vallen, refp, reflen) == 0) |
| 952 | { |
| 953 | /* note in the flags that we have differing cases in the match */ |
| 954 | ret |= RF_CASEFOLD; |
| 955 | |
| 956 | /* keep going */ |
| 957 | continue; |
| 958 | } |
| 959 | |
| 960 | /* check for a reference equivalence mapping */ |
| 961 | vmobj_strcmp_equiv **t1; |
| 962 | vmobj_strcmp_equiv *eq; |
| 963 | if ((t1 = ext->equiv[(refch >> 8) & 0xFF]) != 0 |
| 964 | && (eq = t1[refch & 0xFF]) != 0) |
| 965 | { |
| 966 | /* |
| 967 | * In case we match, apply the appropriate flags added for the |
| 968 | * equivalence mapping, based on the case of the first value |
| 969 | * character we're testing. (If we don't match, we'll simply |
| 970 | * return failure, so it won't matter that we messed with the |
| 971 | * flags.) |
| 972 | */ |
| 973 | ret |= (t3_is_upper(valch) |
| 974 | ? eq->uc_result_flags |
| 975 | : eq->lc_result_flags); |
| 976 | |
| 977 | /* match each character from the mapping string */ |
| 978 | const wchar_t *vp; |
| 979 | size_t vlen; |
| 980 | for (vp = eq->val_ch, vlen = eq->val_ch_cnt ; |
| 981 | vallen != 0 && vlen != 0 ; ) |
| 982 | { |
| 983 | /* get this character */ |
| 984 | refch = *vp; |
| 985 | |
| 986 | /* if we have an exact match, keep going */ |
| 987 | if (refch == valch) |
| 988 | { |
| 989 | /* matched - skip this character in each string */ |
| 990 | valp.inc(&vallen); |
| 991 | ++vp, --vlen; |
| 992 | |
| 993 | /* keep going */ |
| 994 | continue; |
| 995 | } |
| 996 | |
| 997 | /* check for a case-folded match if appropriate */ |
| 998 | if (fold_case |
| 999 | && t3_compare_case_fold_min(valp, vallen, vp, vlen) == 0) |
| 1000 | { |
| 1001 | /* note the case-folded match and keep going */ |
| 1002 | ret |= RF_CASEFOLD; |
| 1003 | continue; |
| 1004 | } |
| 1005 | |
| 1006 | /* no match */ |
| 1007 | return 0; |
| 1008 | } |
| 1009 | |
| 1010 | /* |
| 1011 | * if we didn't make it to the end of the equivalence string, |
| 1012 | * we must have run out of source before we matched the whole |
| 1013 | * string, so we don'to have a match |
| 1014 | */ |
| 1015 | if (vlen != 0) |
| 1016 | return 0; |
| 1017 | |
| 1018 | /* |
| 1019 | * If we make it here, we matched the equivalence mapping. |
| 1020 | * We've already skipped the input we matched, so skip the |
| 1021 | * reference character and keep going. |
| 1022 | */ |
| 1023 | refp.inc(&reflen); |
| 1024 | continue; |
| 1025 | } |
| 1026 | |
| 1027 | /* we don't have anything else to try, so we don't have a match */ |
| 1028 | return 0; |
| 1029 | } |
| 1030 | |
| 1031 | /* |
| 1032 | * If we ran out of reference string before we ran out of value |
| 1033 | * string, we definitely do not have a match. If we ran out of value |
| 1034 | * string before we ran out reference string, we have a match as long |
| 1035 | * as we matched at least the truncation length. |
| 1036 | */ |
| 1037 | if (reflen == 0 && vallen == 0) |
| 1038 | { |
| 1039 | /* |
| 1040 | * We ran out of both at the same time - it's a match. Return the |
| 1041 | * result code up to this point OR'd with RF_MATCH, which is our |
| 1042 | * pre-defined bit that we set for every match. |
| 1043 | */ |
| 1044 | return (ret | RF_MATCH); |
| 1045 | } |
| 1046 | else if (vallen != 0) |
| 1047 | { |
| 1048 | /* we ran out of reference string first - it's not a match */ |
| 1049 | return 0; |
| 1050 | } |
| 1051 | else |
| 1052 | { |
| 1053 | /* |
| 1054 | * We ran out of value string first, so it's a truncated match if |
| 1055 | * we matched at least up to the truncation length (assuming we |
| 1056 | * allow truncation at all). If we didn't make it to the |
| 1057 | * truncation length, or we don't allow truncation, it's not a |
| 1058 | * match. |
| 1059 | */ |
| 1060 | size_t valcharlen = utf8_ptr::s_len(valstr, valp.getptr() - valstr); |
| 1061 | if (ext->trunc_len != 0 && valcharlen >= ext->trunc_len) |
| 1062 | { |
| 1063 | /* |
| 1064 | * it's a truncated match - return the result code up to this |
| 1065 | * point, OR'd with RF_MATCH (our pre-defined bit we set for |
| 1066 | * every match) and RF_TRUNC (our pre-defined bit we set for |
| 1067 | * truncated matches) |
| 1068 | */ |
| 1069 | return (ret | RF_MATCH | RF_TRUNC); |
| 1070 | } |
| 1071 | else |
| 1072 | { |
| 1073 | /* didn't make it to the truncation length, so it's not a match */ |
| 1074 | return 0; |
| 1075 | } |
| 1076 | } |
| 1077 | } |
| 1078 | |
| 1079 | /* |
| 1080 | * Check for an approximation match to a given character. This checks the |
| 1081 | * given input string for a match to the approximation for a given |
| 1082 | * reference character. Returns the number of characters in the match, or |
| 1083 | * zero if there's no match. |
| 1084 | */ |
| 1085 | size_t CVmObjStrComp::match_chars(const wchar_t *valstr, size_t vallen, |
| 1086 | wchar_t refch) |
| 1087 | { |
| 1088 | vmobj_strcmp_ext *ext = get_ext(); |
| 1089 | int fold_case = !(ext->case_sensitive); |
| 1090 | vmobj_strcmp_equiv **t1; |
| 1091 | vmobj_strcmp_equiv *eq; |
| 1092 | wchar_t valch = *valstr; |
| 1093 | const wchar_t *start = valstr; |
| 1094 | |
| 1095 | /* check for an exact match first */ |
| 1096 | if (refch == valch) |
| 1097 | return 1; |
| 1098 | |
| 1099 | /* check for a case-folded match if we're insensitive to case */ |
| 1100 | if (fold_case) |
| 1101 | { |
| 1102 | /* try a minimum case-folded match to the one-character ref string */ |
| 1103 | size_t ovallen = vallen, reflen = 1; |
| 1104 | const wchar_t *refstr = &refch; |
| 1105 | if (t3_compare_case_fold_min(valstr, vallen, refstr, reflen) == 0) |
| 1106 | { |
| 1107 | /* got it - return the length we consumed from the value string */ |
| 1108 | return ovallen - vallen; |
| 1109 | } |
| 1110 | } |
| 1111 | |
| 1112 | /* check for a reference equivalence mapping */ |
| 1113 | if ((t1 = ext->equiv[(refch >> 8) & 0xFF]) != 0 |
| 1114 | && (eq = t1[refch & 0xFF]) != 0) |
| 1115 | { |
| 1116 | /* match each character from the mapping string */ |
| 1117 | const wchar_t *vp; |
| 1118 | size_t vlen; |
| 1119 | for (vp = eq->val_ch, vlen = eq->val_ch_cnt ; |
| 1120 | vallen != 0 && vlen != 0 ; ) |
| 1121 | { |
| 1122 | /* get this mapping character */ |
| 1123 | refch = *vp; |
| 1124 | |
| 1125 | /* if we have an exact match, keep going */ |
| 1126 | if (refch == valch) |
| 1127 | { |
| 1128 | /* matched exactly - skip this character and keep going */ |
| 1129 | ++valstr, --vallen; |
| 1130 | ++vp, --vlen; |
| 1131 | continue; |
| 1132 | } |
| 1133 | |
| 1134 | /* check for a case-folded match if appropriate */ |
| 1135 | if (fold_case |
| 1136 | && t3_compare_case_fold_min(valstr, vallen, vp, vlen) == 0) |
| 1137 | { |
| 1138 | /* matched - keep going */ |
| 1139 | continue; |
| 1140 | } |
| 1141 | |
| 1142 | /* no match */ |
| 1143 | return 0; |
| 1144 | } |
| 1145 | |
| 1146 | /* we've matched the mapping - return the match length */ |
| 1147 | return valstr - start; |
| 1148 | } |
| 1149 | |
| 1150 | /* it doesn't match any of the possibilities */ |
| 1151 | return 0; |
| 1152 | } |
| 1153 | |
| 1154 | /* |
| 1155 | * Get the truncation length |
| 1156 | */ |
| 1157 | size_t CVmObjStrComp::trunc_len() const |
| 1158 | { |
| 1159 | return get_ext()->trunc_len; |
| 1160 | } |