Changeset 11472 in webkit for trunk/JavaScriptCore/kjs/ustring.cpp
- Timestamp:
- Dec 6, 2005, 1:21:15 AM (19 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/JavaScriptCore/kjs/ustring.cpp
r10757 r11472 159 159 UCharReference& UCharReference::operator=(UChar c) 160 160 { 161 str-> detach();162 if (offset < str->rep ->len)163 *(str->rep ->data() + offset) = c;161 str->copyForWriting(); 162 if (offset < str->rep()->len) 163 *(str->rep()->data() + offset) = c; 164 164 /* TODO: lengthen string ? */ 165 165 return *this; … … 168 168 UChar& UCharReference::ref() const 169 169 { 170 if (offset < str->rep ->len)171 return *(str->rep ->data() + offset);170 if (offset < str->rep()->len) 171 return *(str->rep()->data() + offset); 172 172 else { 173 173 static UChar callerBetterNotModifyThis('\0'); … … 176 176 } 177 177 178 UString::Rep *UString::Rep::createCopying(const UChar *d, int l)178 PassRefPtr<UString::Rep> UString::Rep::createCopying(const UChar *d, int l) 179 179 { 180 180 int sizeInBytes = l * sizeof(UChar); … … 185 185 } 186 186 187 UString::Rep *UString::Rep::create(UChar *d, int l)187 PassRefPtr<UString::Rep> UString::Rep::create(UChar *d, int l) 188 188 { 189 189 Rep *r = new Rep; 190 190 r->offset = 0; 191 191 r->len = l; 192 r->rc = 1;192 r->rc = 0; 193 193 r->_hash = 0; 194 194 r->isIdentifier = 0; … … 202 202 } 203 203 204 UString::Rep *UString::Rep::create(Rep *base, int offset, int length)204 PassRefPtr<UString::Rep> UString::Rep::create(PassRefPtr<Rep> base, int offset, int length) 205 205 { 206 206 assert(base); … … 221 221 r->_hash = 0; 222 222 r->isIdentifier = 0; 223 r->baseString = base; 224 base->ref(); 223 r->baseString = base.release(); 225 224 r->buf = 0; 226 225 r->usedCapacity = 0; … … 346 345 inline int UString::usedCapacity() const 347 346 { 348 return rep->baseString ? rep->baseString->usedCapacity :rep->usedCapacity;347 return m_rep->baseString ? m_rep->baseString->usedCapacity : m_rep->usedCapacity; 349 348 } 350 349 351 350 inline int UString::usedPreCapacity() const 352 351 { 353 return rep->baseString ? rep->baseString->usedPreCapacity :rep->usedPreCapacity;352 return m_rep->baseString ? m_rep->baseString->usedPreCapacity : m_rep->usedPreCapacity; 354 353 } 355 354 356 355 void UString::expandCapacity(int requiredLength) 357 356 { 358 Rep *r = rep->baseString ? rep->baseString : rep;357 Rep *r = m_rep->baseString ? m_rep->baseString : rep(); 359 358 360 359 if (requiredLength > r->capacity) { … … 370 369 void UString::expandPreCapacity(int requiredPreCap) 371 370 { 372 Rep *r = rep->baseString ? rep->baseString : rep;371 Rep *r = m_rep->baseString ? m_rep->baseString : rep(); 373 372 374 373 if (requiredPreCap > r->preCapacity) { … … 393 392 UChar *d = static_cast<UChar *>(fastMalloc(sizeof(UChar))); 394 393 d[0] = c; 395 rep = Rep::create(d, 1);394 m_rep = Rep::create(d, 1); 396 395 } 397 396 … … 399 398 { 400 399 if (!c) { 401 attach(&Rep::null);400 m_rep = &Rep::null; 402 401 return; 403 402 } 404 403 int length = strlen(c); 405 404 if (length == 0) { 406 attach(&Rep::empty);405 m_rep = &Rep::empty; 407 406 return; 408 407 } … … 410 409 for (int i = 0; i < length; i++) 411 410 d[i].uc = c[i]; 412 rep = Rep::create(d, length);411 m_rep = Rep::create(d, length); 413 412 } 414 413 415 414 UString::UString(const UChar *c, int length) 416 415 { 417 if (length == 0) { 418 attach(&Rep::empty); 419 return; 420 } 421 rep = Rep::createCopying(c, length); 416 if (length == 0) 417 m_rep = &Rep::empty; 418 else 419 m_rep = Rep::createCopying(c, length); 422 420 } 423 421 424 422 UString::UString(UChar *c, int length, bool copy) 425 423 { 426 if (length == 0) { 427 attach(&Rep::empty); 428 return; 429 } 430 if (copy) { 431 rep = Rep::createCopying(c, length); 432 } else { 433 rep = Rep::create(c, length); 434 } 424 if (length == 0) 425 m_rep = &Rep::empty; 426 else if (copy) 427 m_rep = Rep::createCopying(c, length); 428 else 429 m_rep = Rep::create(c, length); 435 430 } 436 431 … … 438 433 { 439 434 int aSize = a.size(); 440 int aOffset = a. rep->offset;435 int aOffset = a.m_rep->offset; 441 436 int bSize = b.size(); 442 int bOffset = b. rep->offset;437 int bOffset = b.m_rep->offset; 443 438 int length = aSize + bSize; 444 439 … … 447 442 if (aSize == 0) { 448 443 // a is empty 449 attach(b.rep);444 m_rep = b.m_rep; 450 445 } else if (bSize == 0) { 451 446 // b is empty 452 attach(a.rep);447 m_rep = a.m_rep; 453 448 } else if (aOffset + aSize == a.usedCapacity() && 4 * aSize >= bSize && 454 449 (-bOffset != b.usedPreCapacity() || aSize >= bSize)) { … … 460 455 x.expandCapacity(aOffset + length); 461 456 memcpy(const_cast<UChar *>(a.data() + aSize), b.data(), bSize * sizeof(UChar)); 462 rep = Rep::create(a.rep, 0, length);457 m_rep = Rep::create(a.m_rep, 0, length); 463 458 } else if (-bOffset == b.usedPreCapacity() && 4 * bSize >= aSize) { 464 459 // - b reaches the beginning of its buffer so it qualifies for shared prepend … … 468 463 y.expandPreCapacity(-bOffset + aSize); 469 464 memcpy(const_cast<UChar *>(b.data() - aSize), a.data(), aSize * sizeof(UChar)); 470 rep = Rep::create(b.rep, -aSize, length);465 m_rep = Rep::create(b.m_rep, -aSize, length); 471 466 } else { 472 467 // a does not qualify for append, and b does not qualify for prepend, gotta make a whole new string … … 475 470 memcpy(d, a.data(), aSize * sizeof(UChar)); 476 471 memcpy(d + aSize, b.data(), bSize * sizeof(UChar)); 477 rep = Rep::create(d, length);478 rep->capacity = newCapacity;472 m_rep = Rep::create(d, length); 473 m_rep->capacity = newCapacity; 479 474 } 480 475 } … … 658 653 } 659 654 660 UString::Rep *rep = UString::Rep::create(buffer, totalLength); 661 UString result = UString(rep); 662 rep->deref(); 663 664 return result; 655 return UString(UString::Rep::create(buffer, totalLength)); 665 656 } 666 657 … … 670 661 { 671 662 int thisSize = size(); 672 int thisOffset = rep->offset;663 int thisOffset = m_rep->offset; 673 664 int tSize = t.size(); 674 665 int length = thisSize + tSize; … … 680 671 } else if (tSize == 0) { 681 672 // t is empty 682 } else if (! rep->baseString &&rep->rc == 1) {673 } else if (!m_rep->baseString && m_rep->rc == 1) { 683 674 // this is direct and has refcount of 1 (so we can just alter it directly) 684 675 expandCapacity(thisOffset + length); 685 676 memcpy(const_cast<UChar *>(data() + thisSize), t.data(), tSize * sizeof(UChar)); 686 rep->len = length;687 rep->_hash = 0;677 m_rep->len = length; 678 m_rep->_hash = 0; 688 679 } else if (thisOffset + thisSize == usedCapacity()) { 689 680 // this reaches the end of the buffer - extend it 690 681 expandCapacity(thisOffset + length); 691 682 memcpy(const_cast<UChar *>(data() + thisSize), t.data(), tSize * sizeof(UChar)); 692 Rep *newRep = Rep::create(rep, 0, length); 693 release(); 694 rep = newRep; 683 m_rep = Rep::create(m_rep, 0, length); 695 684 } else { 696 685 // this is shared with someone using more capacity, gotta make a whole new string … … 699 688 memcpy(d, data(), thisSize * sizeof(UChar)); 700 689 memcpy(const_cast<UChar *>(d + thisSize), t.data(), tSize * sizeof(UChar)); 701 release(); 702 rep = Rep::create(d, length); 703 rep->capacity = newCapacity; 690 m_rep = Rep::create(d, length); 691 m_rep->capacity = newCapacity; 704 692 } 705 693 … … 710 698 { 711 699 int thisSize = size(); 712 int thisOffset = rep->offset;700 int thisOffset = m_rep->offset; 713 701 int tSize = strlen(t); 714 702 int length = thisSize + tSize; … … 720 708 } else if (tSize == 0) { 721 709 // t is empty, we'll just return *this below. 722 } else if (! rep->baseString &&rep->rc == 1) {710 } else if (!m_rep->baseString && m_rep->rc == 1) { 723 711 // this is direct and has refcount of 1 (so we can just alter it directly) 724 712 expandCapacity(thisOffset + length); … … 726 714 for (int i = 0; i < tSize; ++i) 727 715 d[thisSize+i] = t[i]; 728 rep->len = length;729 rep->_hash = 0;716 m_rep->len = length; 717 m_rep->_hash = 0; 730 718 } else if (thisOffset + thisSize == usedCapacity()) { 731 719 // this string reaches the end of the buffer - extend it … … 734 722 for (int i = 0; i < tSize; ++i) 735 723 d[thisSize+i] = t[i]; 736 Rep *newRep = Rep::create(rep, 0, length); 737 release(); 738 rep = newRep; 724 m_rep = Rep::create(m_rep, 0, length); 739 725 } else { 740 726 // this is shared with someone using more capacity, gotta make a whole new string … … 744 730 for (int i = 0; i < tSize; ++i) 745 731 d[thisSize+i] = t[i]; 746 release(); 747 rep = Rep::create(d, length); 748 rep->capacity = newCapacity; 732 m_rep = Rep::create(d, length); 733 m_rep->capacity = newCapacity; 749 734 } 750 735 … … 754 739 UString &UString::append(unsigned short c) 755 740 { 756 int thisOffset = rep->offset;741 int thisOffset = m_rep->offset; 757 742 int length = size(); 758 743 759 744 // possible cases: 760 745 if (length == 0) { 761 // this is empty - must make a new rep because we don't want to pollute the shared empty one746 // this is empty - must make a new m_rep because we don't want to pollute the shared empty one 762 747 int newCapacity = expandedSize(1, 0); 763 748 UChar *d = static_cast<UChar *>(fastMalloc(sizeof(UChar) * newCapacity)); 764 749 d[0] = c; 765 release(); 766 rep = Rep::create(d, 1); 767 rep->capacity = newCapacity; 768 } else if (!rep->baseString && rep->rc == 1) { 750 m_rep = Rep::create(d, 1); 751 m_rep->capacity = newCapacity; 752 } else if (!m_rep->baseString && m_rep->rc == 1) { 769 753 // this is direct and has refcount of 1 (so we can just alter it directly) 770 754 expandCapacity(thisOffset + length + 1); 771 755 UChar *d = const_cast<UChar *>(data()); 772 756 d[length] = c; 773 rep->len = length + 1;774 rep->_hash = 0;757 m_rep->len = length + 1; 758 m_rep->_hash = 0; 775 759 } else if (thisOffset + length == usedCapacity()) { 776 760 // this reaches the end of the string - extend it and share … … 778 762 UChar *d = const_cast<UChar *>(data()); 779 763 d[length] = c; 780 Rep *newRep = Rep::create(rep, 0, length + 1); 781 release(); 782 rep = newRep; 764 m_rep = Rep::create(m_rep, 0, length + 1); 783 765 } else { 784 766 // this is shared with someone using more capacity, gotta make a whole new string … … 787 769 memcpy(d, data(), length * sizeof(UChar)); 788 770 d[length] = c; 789 release(); 790 rep = Rep::create(d, length); 791 rep->capacity = newCapacity; 771 m_rep = Rep::create(d, length); 772 m_rep->capacity = newCapacity; 792 773 } 793 774 … … 841 822 int l = c ? strlen(c) : 0; 842 823 UChar *d; 843 if ( rep->rc == 1 && l <= rep->capacity && !rep->baseString && rep->offset == 0 &&rep->preCapacity == 0) {844 d = rep->buf;845 rep->_hash = 0;824 if (m_rep->rc == 1 && l <= m_rep->capacity && !m_rep->baseString && m_rep->offset == 0 && m_rep->preCapacity == 0) { 825 d = m_rep->buf; 826 m_rep->_hash = 0; 846 827 } else { 847 release();848 828 d = static_cast<UChar *>(fastMalloc(sizeof(UChar) * l)); 849 rep = Rep::create(d, l);829 m_rep = Rep::create(d, l); 850 830 } 851 831 for (int i = 0; i < l; i++) … … 857 837 UString &UString::operator=(const UString &str) 858 838 { 859 str.rep->ref(); 860 release(); 861 rep = str.rep; 862 839 m_rep = str.m_rep; 863 840 return *this; 864 841 } … … 1001 978 1002 979 // Empty string is not OK. 1003 int len = rep->len;980 int len = m_rep->len; 1004 981 if (len == 0) 1005 982 return 0; 1006 const UChar *p = rep->data();983 const UChar *p = m_rep->data(); 1007 984 unsigned short c = p->unicode(); 1008 985 … … 1141 1118 return *this; 1142 1119 1143 Rep *newRep = Rep::create(rep, pos, len); 1144 UString result(newRep); 1145 newRep->deref(); 1146 1147 return result; 1148 } 1149 1150 void UString::detach() 1151 { 1152 if (rep->rc > 1 || rep->baseString) { 1120 return UString(Rep::create(m_rep, pos, len)); 1121 } 1122 1123 void UString::copyForWriting() 1124 { 1125 if (m_rep->rc > 1 || m_rep->baseString) { 1153 1126 int l = size(); 1154 1127 UChar *n = static_cast<UChar *>(fastMalloc(sizeof(UChar) * l)); 1155 1128 memcpy(n, data(), l * sizeof(UChar)); 1156 release(); 1157 rep = Rep::create(n, l); 1129 m_rep = Rep::create(n, l); 1158 1130 } 1159 1131 } … … 1161 1133 bool operator==(const UString& s1, const UString& s2) 1162 1134 { 1163 if (s1. rep->len != s2.rep->len)1135 if (s1.m_rep->len != s2.m_rep->len) 1164 1136 return false; 1165 1137 1166 return (memcmp(s1. rep->data(), s2.rep->data(),1167 s1. rep->len * sizeof(UChar)) == 0);1138 return (memcmp(s1.m_rep->data(), s2.m_rep->data(), 1139 s1.m_rep->len * sizeof(UChar)) == 0); 1168 1140 } 1169 1141
Note:
See TracChangeset
for help on using the changeset viewer.