1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049
|
/*
* Copyright (C) 1999 Lars Knoll (knoll@kde.org)
* (C) 1999 Antti Koivisto (koivisto@kde.org)
* (C) 2001 Dirk Mueller ( mueller@kde.org )
* Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
* Copyright (C) 2006 Andrew Wellington (proton@wiretapped.net)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*
*/
#include "config.h"
#include "StringImpl.h"
#include "AtomicString.h"
#include "CString.h"
#include "CharacterNames.h"
#include "FloatConversion.h"
#include "Length.h"
#include "StringBuffer.h"
#include "StringHash.h"
#include "TextBreakIterator.h"
#include "TextEncoding.h"
#include <kjs/dtoa.h>
#include <kjs/identifier.h>
#include <wtf/Assertions.h>
#include <wtf/unicode/Unicode.h>
using namespace WTF;
using namespace Unicode;
using KJS::Identifier;
using KJS::UString;
namespace WebCore {
static inline UChar* newUCharVector(unsigned n)
{
return static_cast<UChar*>(fastMalloc(sizeof(UChar) * n));
}
static inline void deleteUCharVector(const UChar* p)
{
fastFree(const_cast<UChar*>(p));
}
// This constructor is used only to create the empty string.
StringImpl::StringImpl()
: m_length(0)
, m_data(0)
, m_hash(0)
, m_inTable(false)
, m_hasTerminatingNullCharacter(false)
{
}
// This is one of the most common constructors, but it's also used for the copy()
// operation. Because of that, it's the one constructor that doesn't assert the
// length is non-zero, since we support copying the empty string.
inline StringImpl::StringImpl(const UChar* characters, unsigned length)
: m_length(length)
, m_hash(0)
, m_inTable(false)
, m_hasTerminatingNullCharacter(false)
{
UChar* data = newUCharVector(length);
memcpy(data, characters, length * sizeof(UChar));
m_data = data;
}
inline StringImpl::StringImpl(const StringImpl& str, WithTerminatingNullCharacter)
: m_length(str.m_length)
, m_hash(str.m_hash)
, m_inTable(false)
, m_hasTerminatingNullCharacter(true)
{
UChar* data = newUCharVector(str.m_length + 1);
memcpy(data, str.m_data, str.m_length * sizeof(UChar));
data[str.m_length] = 0;
m_data = data;
}
inline StringImpl::StringImpl(const char* characters, unsigned length)
: m_length(length)
, m_hash(0)
, m_inTable(false)
, m_hasTerminatingNullCharacter(false)
{
ASSERT(characters);
ASSERT(length);
UChar* data = newUCharVector(length);
for (unsigned i = 0; i != length; ++i) {
unsigned char c = characters[i];
data[i] = c;
}
m_data = data;
}
inline StringImpl::StringImpl(UChar* characters, unsigned length, AdoptBuffer)
: m_length(length)
, m_data(characters)
, m_hash(0)
, m_inTable(false)
, m_hasTerminatingNullCharacter(false)
{
ASSERT(characters);
ASSERT(length);
}
// This constructor is only for use by AtomicString.
StringImpl::StringImpl(const UChar* characters, unsigned length, unsigned hash)
: m_length(length)
, m_hash(hash)
, m_inTable(true)
, m_hasTerminatingNullCharacter(false)
{
ASSERT(hash);
ASSERT(characters);
ASSERT(length);
UChar* data = newUCharVector(length);
memcpy(data, characters, length * sizeof(UChar));
m_data = data;
}
// This constructor is only for use by AtomicString.
StringImpl::StringImpl(const char* characters, unsigned length, unsigned hash)
: m_length(length)
, m_hash(hash)
, m_inTable(true)
, m_hasTerminatingNullCharacter(false)
{
ASSERT(hash);
ASSERT(characters);
ASSERT(length);
UChar* data = newUCharVector(length);
for (unsigned i = 0; i != length; ++i) {
unsigned char c = characters[i];
data[i] = c;
}
m_data = data;
}
StringImpl::~StringImpl()
{
if (m_inTable)
AtomicString::remove(this);
deleteUCharVector(m_data);
}
StringImpl* StringImpl::empty()
{
static StringImpl e;
return &e;
}
bool StringImpl::containsOnlyWhitespace()
{
// FIXME: The definition of whitespace here includes a number of characters
// that are not whitespace from the point of view of RenderText; I wonder if
// that's a problem in practice.
for (unsigned i = 0; i < m_length; i++)
if (!isASCIISpace(m_data[i]))
return false;
return true;
}
PassRefPtr<StringImpl> StringImpl::substring(unsigned pos, unsigned len)
{
if (pos >= m_length)
return empty();
if (len > m_length - pos)
len = m_length - pos;
return create(m_data + pos, len);
}
UChar32 StringImpl::characterStartingAt(unsigned i)
{
if (U16_IS_SINGLE(m_data[i]))
return m_data[i];
if (i + 1 < m_length && U16_IS_LEAD(m_data[i]) && U16_IS_TRAIL(m_data[i + 1]))
return U16_GET_SUPPLEMENTARY(m_data[i], m_data[i + 1]);
return 0;
}
static Length parseLength(const UChar* data, unsigned length)
{
if (length == 0)
return Length(1, Relative);
unsigned i = 0;
while (i < length && isSpaceOrNewline(data[i]))
++i;
if (i < length && (data[i] == '+' || data[i] == '-'))
++i;
while (i < length && Unicode::isDigit(data[i]))
++i;
bool ok;
int r = charactersToIntStrict(data, i, &ok);
/* Skip over any remaining digits, we are not that accurate (5.5% => 5%) */
while (i < length && (Unicode::isDigit(data[i]) || data[i] == '.'))
++i;
/* IE Quirk: Skip any whitespace (20 % => 20%) */
while (i < length && isSpaceOrNewline(data[i]))
++i;
if (ok) {
if (i < length) {
UChar next = data[i];
if (next == '%')
return Length(static_cast<double>(r), Percent);
if (next == '*')
return Length(r, Relative);
}
return Length(r, Fixed);
} else {
if (i < length) {
UChar next = data[i];
if (next == '*')
return Length(1, Relative);
if (next == '%')
return Length(1, Relative);
}
}
return Length(0, Relative);
}
Length StringImpl::toLength()
{
return parseLength(m_data, m_length);
}
static int countCharacter(StringImpl* string, UChar character)
{
int count = 0;
int length = string->length();
for (int i = 0; i < length; ++i)
count += (*string)[i] == character;
return count;
}
Length* StringImpl::toCoordsArray(int& len)
{
StringBuffer spacified(m_length);
for (unsigned i = 0; i < m_length; i++) {
UChar cc = m_data[i];
if (cc > '9' || (cc < '0' && cc != '-' && cc != '*' && cc != '.'))
spacified[i] = ' ';
else
spacified[i] = cc;
}
RefPtr<StringImpl> str = adopt(spacified);
str = str->simplifyWhiteSpace();
len = countCharacter(str.get(), ' ') + 1;
Length* r = new Length[len];
int i = 0;
int pos = 0;
int pos2;
while ((pos2 = str->find(' ', pos)) != -1) {
r[i++] = parseLength(str->characters() + pos, pos2 - pos);
pos = pos2+1;
}
r[i] = parseLength(str->characters() + pos, str->length() - pos);
ASSERT(i == len - 1);
return r;
}
Length* StringImpl::toLengthArray(int& len)
{
RefPtr<StringImpl> str = simplifyWhiteSpace();
if (!str->length()) {
len = 1;
return 0;
}
len = countCharacter(str.get(), ',') + 1;
Length* r = new Length[len];
int i = 0;
int pos = 0;
int pos2;
while ((pos2 = str->find(',', pos)) != -1) {
r[i++] = parseLength(str->characters() + pos, pos2 - pos);
pos = pos2+1;
}
ASSERT(i == len - 1);
/* IE Quirk: If the last comma is the last char skip it and reduce len by one */
if (str->length()-pos > 0)
r[i] = parseLength(str->characters() + pos, str->length() - pos);
else
len--;
return r;
}
bool StringImpl::isLower()
{
// Do a faster loop for the case where all the characters are ASCII.
bool allLower = true;
UChar ored = 0;
for (unsigned i = 0; i < m_length; i++) {
UChar c = m_data[i];
allLower = allLower && isASCIILower(c);
ored |= c;
}
if (!(ored & ~0x7F))
return allLower;
// Do a slower check for cases that include non-ASCII characters.
allLower = true;
unsigned i = 0;
while (i < m_length) {
UChar32 character;
U16_NEXT(m_data, i, m_length, character)
allLower = allLower && Unicode::isLower(character);
}
return allLower;
}
PassRefPtr<StringImpl> StringImpl::lower()
{
StringBuffer data(m_length);
int32_t length = m_length;
// Do a faster loop for the case where all the characters are ASCII.
UChar ored = 0;
for (int i = 0; i < length; i++) {
UChar c = m_data[i];
ored |= c;
data[i] = toASCIILower(c);
}
if (!(ored & ~0x7F))
return adopt(data);
// Do a slower implementation for cases that include non-ASCII characters.
bool error;
int32_t realLength = Unicode::toLower(data.characters(), length, m_data, m_length, &error);
if (!error && realLength == length)
return adopt(data);
data.resize(realLength);
Unicode::toLower(data.characters(), length, m_data, m_length, &error);
if (error)
return this;
return adopt(data);
}
PassRefPtr<StringImpl> StringImpl::upper()
{
bool error;
int32_t length = Unicode::toUpper(0, 0, m_data, m_length, &error);
StringBuffer data(length);
Unicode::toUpper(data.characters(), length, m_data, m_length, &error);
if (error)
return this;
return adopt(data);
}
PassRefPtr<StringImpl> StringImpl::secure(UChar aChar)
{
int length = m_length;
StringBuffer data(length);
for (int i = 0; i < length; ++i)
data[i] = aChar;
return adopt(data);
}
PassRefPtr<StringImpl> StringImpl::foldCase()
{
StringBuffer data(m_length);
int32_t length = m_length;
// Do a faster loop for the case where all the characters are ASCII.
UChar ored = 0;
for (int i = 0; i < length; i++) {
UChar c = m_data[i];
ored |= c;
data[i] = toASCIILower(c);
}
if (!(ored & ~0x7F))
return adopt(data);
// Do a slower implementation for cases that include non-ASCII characters.
bool error;
int32_t realLength = Unicode::foldCase(data.characters(), length, m_data, m_length, &error);
if (!error && realLength == length)
return adopt(data);
data.resize(realLength);
Unicode::foldCase(data.characters(), realLength, m_data, m_length, &error);
if (error)
return this;
return adopt(data);
}
PassRefPtr<StringImpl> StringImpl::stripWhiteSpace()
{
if (!m_length)
return empty();
unsigned start = 0;
unsigned end = m_length - 1;
// skip white space from start
while (start <= end && isSpaceOrNewline(m_data[start]))
start++;
// only white space
if (start > end)
return empty();
// skip white space from end
while (end && isSpaceOrNewline(m_data[end]))
end--;
return create(m_data + start, end + 1 - start);
}
PassRefPtr<StringImpl> StringImpl::simplifyWhiteSpace()
{
StringBuffer data(m_length);
const UChar* from = m_data;
const UChar* fromend = from + m_length;
int outc = 0;
UChar* to = data.characters();
while (true) {
while (from != fromend && isSpaceOrNewline(*from))
from++;
while (from != fromend && !isSpaceOrNewline(*from))
to[outc++] = *from++;
if (from != fromend)
to[outc++] = ' ';
else
break;
}
if (outc > 0 && to[outc - 1] == ' ')
outc--;
data.shrink(outc);
return adopt(data);
}
PassRefPtr<StringImpl> StringImpl::capitalize(UChar previous)
{
StringBuffer stringWithPrevious(m_length + 1);
stringWithPrevious[0] = previous == noBreakSpace ? ' ' : previous;
for (unsigned i = 1; i < m_length + 1; i++) {
// Replace   with a real space since ICU no longer treats   as a word separator.
if (m_data[i - 1] == noBreakSpace)
stringWithPrevious[i] = ' ';
else
stringWithPrevious[i] = m_data[i - 1];
}
TextBreakIterator* boundary = wordBreakIterator(stringWithPrevious.characters(), m_length + 1);
if (!boundary)
return this;
StringBuffer data(m_length);
int32_t endOfWord;
int32_t startOfWord = textBreakFirst(boundary);
for (endOfWord = textBreakNext(boundary); endOfWord != TextBreakDone; startOfWord = endOfWord, endOfWord = textBreakNext(boundary)) {
if (startOfWord != 0) // Ignore first char of previous string
data[startOfWord - 1] = m_data[startOfWord - 1] == noBreakSpace ? noBreakSpace : toTitleCase(stringWithPrevious[startOfWord]);
for (int i = startOfWord + 1; i < endOfWord; i++)
data[i - 1] = m_data[i - 1];
}
return adopt(data);
}
int StringImpl::toIntStrict(bool* ok, int base)
{
return charactersToIntStrict(m_data, m_length, ok, base);
}
unsigned StringImpl::toUIntStrict(bool* ok, int base)
{
return charactersToUIntStrict(m_data, m_length, ok, base);
}
int64_t StringImpl::toInt64Strict(bool* ok, int base)
{
return charactersToInt64Strict(m_data, m_length, ok, base);
}
uint64_t StringImpl::toUInt64Strict(bool* ok, int base)
{
return charactersToUInt64Strict(m_data, m_length, ok, base);
}
int StringImpl::toInt(bool* ok)
{
return charactersToInt(m_data, m_length, ok);
}
unsigned StringImpl::toUInt(bool* ok)
{
return charactersToUInt(m_data, m_length, ok);
}
int64_t StringImpl::toInt64(bool* ok)
{
return charactersToInt64(m_data, m_length, ok);
}
uint64_t StringImpl::toUInt64(bool* ok)
{
return charactersToUInt64(m_data, m_length, ok);
}
double StringImpl::toDouble(bool* ok)
{
return charactersToDouble(m_data, m_length, ok);
}
float StringImpl::toFloat(bool* ok)
{
return charactersToFloat(m_data, m_length, ok);
}
static bool equal(const UChar* a, const char* b, int length)
{
ASSERT(length >= 0);
while (length--) {
unsigned char bc = *b++;
if (*a++ != bc)
return false;
}
return true;
}
static bool equalIgnoringCase(const UChar* a, const char* b, int length)
{
ASSERT(length >= 0);
while (length--) {
unsigned char bc = *b++;
if (foldCase(*a++) != foldCase(bc))
return false;
}
return true;
}
static inline bool equalIgnoringCase(const UChar* a, const UChar* b, int length)
{
ASSERT(length >= 0);
return umemcasecmp(a, b, length) == 0;
}
int StringImpl::find(const char* chs, int index, bool caseSensitive)
{
if (!chs || index < 0)
return -1;
int chsLength = strlen(chs);
int n = m_length - index;
if (n < 0)
return -1;
n -= chsLength - 1;
if (n <= 0)
return -1;
const char* chsPlusOne = chs + 1;
int chsLengthMinusOne = chsLength - 1;
const UChar* ptr = m_data + index - 1;
if (caseSensitive) {
UChar c = *chs;
do {
if (*++ptr == c && equal(ptr + 1, chsPlusOne, chsLengthMinusOne))
return m_length - chsLength - n + 1;
} while (--n);
} else {
UChar lc = Unicode::foldCase(*chs);
do {
if (Unicode::foldCase(*++ptr) == lc && equalIgnoringCase(ptr + 1, chsPlusOne, chsLengthMinusOne))
return m_length - chsLength - n + 1;
} while (--n);
}
return -1;
}
int StringImpl::find(UChar c, int start)
{
return WebCore::find(m_data, m_length, c, start);
}
int StringImpl::find(StringImpl* str, int index, bool caseSensitive)
{
/*
We use a simple trick for efficiency's sake. Instead of
comparing strings, we compare the sum of str with that of
a part of this string. Only if that matches, we call memcmp
or ucstrnicmp.
*/
ASSERT(str);
if (index < 0)
index += m_length;
int lstr = str->m_length;
int lthis = m_length - index;
if ((unsigned)lthis > m_length)
return -1;
int delta = lthis - lstr;
if (delta < 0)
return -1;
const UChar* uthis = m_data + index;
const UChar* ustr = str->m_data;
unsigned hthis = 0;
unsigned hstr = 0;
if (caseSensitive) {
for (int i = 0; i < lstr; i++) {
hthis += uthis[i];
hstr += ustr[i];
}
int i = 0;
while (1) {
if (hthis == hstr && memcmp(uthis + i, ustr, lstr * sizeof(UChar)) == 0)
return index + i;
if (i == delta)
return -1;
hthis += uthis[i + lstr];
hthis -= uthis[i];
i++;
}
} else {
for (int i = 0; i < lstr; i++ ) {
hthis += toASCIILower(uthis[i]);
hstr += toASCIILower(ustr[i]);
}
int i = 0;
while (1) {
if (hthis == hstr && equalIgnoringCase(uthis + i, ustr, lstr))
return index + i;
if (i == delta)
return -1;
hthis += toASCIILower(uthis[i + lstr]);
hthis -= toASCIILower(uthis[i]);
i++;
}
}
}
int StringImpl::reverseFind(UChar c, int index)
{
return WebCore::reverseFind(m_data, m_length, c, index);
}
int StringImpl::reverseFind(StringImpl* str, int index, bool caseSensitive)
{
/*
See StringImpl::find() for explanations.
*/
ASSERT(str);
int lthis = m_length;
if (index < 0)
index += lthis;
int lstr = str->m_length;
int delta = lthis - lstr;
if ( index < 0 || index > lthis || delta < 0 )
return -1;
if ( index > delta )
index = delta;
const UChar *uthis = m_data;
const UChar *ustr = str->m_data;
unsigned hthis = 0;
unsigned hstr = 0;
int i;
if (caseSensitive) {
for ( i = 0; i < lstr; i++ ) {
hthis += uthis[index + i];
hstr += ustr[i];
}
i = index;
while (1) {
if (hthis == hstr && memcmp(uthis + i, ustr, lstr * sizeof(UChar)) == 0)
return i;
if (i == 0)
return -1;
i--;
hthis -= uthis[i + lstr];
hthis += uthis[i];
}
} else {
for (i = 0; i < lstr; i++) {
hthis += toASCIILower(uthis[index + i]);
hstr += toASCIILower(ustr[i]);
}
i = index;
while (1) {
if (hthis == hstr && equalIgnoringCase(uthis + i, ustr, lstr) )
return i;
if (i == 0)
return -1;
i--;
hthis -= toASCIILower(uthis[i + lstr]);
hthis += toASCIILower(uthis[i]);
}
}
// Should never get here.
return -1;
}
bool StringImpl::endsWith(StringImpl* m_data, bool caseSensitive)
{
ASSERT(m_data);
int start = m_length - m_data->m_length;
if (start >= 0)
return (find(m_data, start, caseSensitive) == start);
return false;
}
PassRefPtr<StringImpl> StringImpl::replace(UChar oldC, UChar newC)
{
if (oldC == newC)
return this;
unsigned i;
for (i = 0; i != m_length; ++i)
if (m_data[i] == oldC)
break;
if (i == m_length)
return this;
StringBuffer data(m_length);
for (i = 0; i != m_length; ++i) {
UChar ch = m_data[i];
if (ch == oldC)
ch = newC;
data[i] = ch;
}
return adopt(data);
}
PassRefPtr<StringImpl> StringImpl::replace(unsigned position, unsigned lengthToReplace, StringImpl* str)
{
position = min(position, length());
lengthToReplace = min(lengthToReplace, length() - position);
unsigned lengthToInsert = str ? str->length() : 0;
if (!lengthToReplace && !lengthToInsert)
return this;
StringBuffer buffer(length() - lengthToReplace + lengthToInsert);
memcpy(buffer.characters(), characters(), position * sizeof(UChar));
if (str)
memcpy(buffer.characters() + position, str->characters(), lengthToInsert * sizeof(UChar));
memcpy(buffer.characters() + position + lengthToInsert, characters() + position + lengthToReplace,
(length() - position - lengthToReplace) * sizeof(UChar));
return adopt(buffer);
}
PassRefPtr<StringImpl> StringImpl::replace(UChar pattern, StringImpl* replacement)
{
if (!replacement)
return this;
int repStrLength = replacement->length();
int srcSegmentStart = 0;
int matchCount = 0;
// Count the matches
while ((srcSegmentStart = find(pattern, srcSegmentStart)) >= 0) {
++matchCount;
++srcSegmentStart;
}
// If we have 0 matches, we don't have to do any more work
if (!matchCount)
return this;
StringBuffer data(m_length - matchCount + (matchCount * repStrLength));
// Construct the new data
int srcSegmentEnd;
int srcSegmentLength;
srcSegmentStart = 0;
int dstOffset = 0;
while ((srcSegmentEnd = find(pattern, srcSegmentStart)) >= 0) {
srcSegmentLength = srcSegmentEnd - srcSegmentStart;
memcpy(data.characters() + dstOffset, m_data + srcSegmentStart, srcSegmentLength * sizeof(UChar));
dstOffset += srcSegmentLength;
memcpy(data.characters() + dstOffset, replacement->m_data, repStrLength * sizeof(UChar));
dstOffset += repStrLength;
srcSegmentStart = srcSegmentEnd + 1;
}
srcSegmentLength = m_length - srcSegmentStart;
memcpy(data.characters() + dstOffset, m_data + srcSegmentStart, srcSegmentLength * sizeof(UChar));
ASSERT(dstOffset + srcSegmentLength == static_cast<int>(data.length()));
return adopt(data);
}
PassRefPtr<StringImpl> StringImpl::replace(StringImpl* pattern, StringImpl* replacement)
{
if (!pattern || !replacement)
return this;
int patternLength = pattern->length();
if (!patternLength)
return this;
int repStrLength = replacement->length();
int srcSegmentStart = 0;
int matchCount = 0;
// Count the matches
while ((srcSegmentStart = find(pattern, srcSegmentStart)) >= 0) {
++matchCount;
srcSegmentStart += patternLength;
}
// If we have 0 matches, we don't have to do any more work
if (!matchCount)
return this;
StringBuffer data(m_length + matchCount * (repStrLength - patternLength));
// Construct the new data
int srcSegmentEnd;
int srcSegmentLength;
srcSegmentStart = 0;
int dstOffset = 0;
while ((srcSegmentEnd = find(pattern, srcSegmentStart)) >= 0) {
srcSegmentLength = srcSegmentEnd - srcSegmentStart;
memcpy(data.characters() + dstOffset, m_data + srcSegmentStart, srcSegmentLength * sizeof(UChar));
dstOffset += srcSegmentLength;
memcpy(data.characters() + dstOffset, replacement->m_data, repStrLength * sizeof(UChar));
dstOffset += repStrLength;
srcSegmentStart = srcSegmentEnd + patternLength;
}
srcSegmentLength = m_length - srcSegmentStart;
memcpy(data.characters() + dstOffset, m_data + srcSegmentStart, srcSegmentLength * sizeof(UChar));
ASSERT(dstOffset + srcSegmentLength == static_cast<int>(data.length()));
return adopt(data);
}
bool equal(StringImpl* a, StringImpl* b)
{
return StringHash::equal(a, b);
}
bool equal(StringImpl* a, const char* b)
{
if (!a)
return !b;
if (!b)
return !a;
unsigned length = a->length();
const UChar* as = a->characters();
for (unsigned i = 0; i != length; ++i) {
unsigned char bc = b[i];
if (!bc)
return false;
if (as[i] != bc)
return false;
}
return !b[length];
}
bool equalIgnoringCase(StringImpl* a, StringImpl* b)
{
return CaseFoldingHash::equal(a, b);
}
bool equalIgnoringCase(StringImpl* a, const char* b)
{
if (!a)
return !b;
if (!b)
return !a;
unsigned length = a->length();
const UChar* as = a->characters();
// Do a faster loop for the case where all the characters are ASCII.
UChar ored = 0;
bool equal = true;
for (unsigned i = 0; i != length; ++i) {
char bc = b[i];
if (!bc)
return false;
UChar ac = as[i];
ored |= ac;
equal = equal && (toASCIILower(ac) == toASCIILower(bc));
}
// Do a slower implementation for cases that include non-ASCII characters.
if (ored & ~0x7F) {
equal = true;
for (unsigned i = 0; i != length; ++i) {
unsigned char bc = b[i];
equal = equal && (foldCase(as[i]) == foldCase(bc));
}
}
return equal && !b[length];
}
Vector<char> StringImpl::ascii()
{
Vector<char> buffer(m_length + 1);
for (unsigned i = 0; i != m_length; ++i) {
UChar c = m_data[i];
if ((c >= 0x20 && c < 0x7F) || c == 0x00)
buffer[i] = c;
else
buffer[i] = '?';
}
buffer[m_length] = '\0';
return buffer;
}
WTF::Unicode::Direction StringImpl::defaultWritingDirection()
{
for (unsigned i = 0; i < m_length; ++i) {
WTF::Unicode::Direction charDirection = WTF::Unicode::direction(m_data[i]);
if (charDirection == WTF::Unicode::LeftToRight)
return WTF::Unicode::LeftToRight;
if (charDirection == WTF::Unicode::RightToLeft || charDirection == WTF::Unicode::RightToLeftArabic)
return WTF::Unicode::RightToLeft;
}
return WTF::Unicode::LeftToRight;
}
// This is a hot function because it's used when parsing HTML.
PassRefPtr<StringImpl> StringImpl::createStrippingNullCharacters(const UChar* characters, unsigned length)
{
ASSERT(characters);
ASSERT(length);
// Optimize for the case where there are no Null characters by quickly
// searching for nulls, and then using StringImpl::create, which will
// memcpy the whole buffer. This is faster than assigning character by
// character during the loop.
// Fast case.
int foundNull = 0;
for (unsigned i = 0; !foundNull && i < length; i++) {
int c = characters[i]; // more efficient than using UChar here (at least on Intel Mac OS)
foundNull |= !c;
}
if (!foundNull)
return StringImpl::create(characters, length);
// Slow case.
StringBuffer strippedCopy(length);
unsigned strippedLength = 0;
for (unsigned i = 0; i < length; i++) {
if (int c = characters[i])
strippedCopy[strippedLength++] = c;
}
ASSERT(strippedLength < length); // Only take the slow case when stripping.
strippedCopy.shrink(strippedLength);
return adopt(strippedCopy);
}
PassRefPtr<StringImpl> StringImpl::adopt(StringBuffer& buffer)
{
unsigned length = buffer.length();
if (length == 0)
return empty();
return adoptRef(new StringImpl(buffer.release(), length, AdoptBuffer()));
}
PassRefPtr<StringImpl> StringImpl::adopt(Vector<UChar>& vector)
{
size_t size = vector.size();
if (size == 0)
return empty();
return adoptRef(new StringImpl(vector.releaseBuffer(), size, AdoptBuffer()));
}
PassRefPtr<StringImpl> StringImpl::create(const UChar* characters, unsigned length)
{
if (!characters || !length)
return empty();
return adoptRef(new StringImpl(characters, length));
}
PassRefPtr<StringImpl> StringImpl::create(const char* characters, unsigned length)
{
if (!characters || !length)
return empty();
return adoptRef(new StringImpl(characters, length));
}
PassRefPtr<StringImpl> StringImpl::create(const char* string)
{
if (!string)
return empty();
unsigned length = strlen(string);
if (!length)
return empty();
return adoptRef(new StringImpl(string, length));
}
PassRefPtr<StringImpl> StringImpl::createWithTerminatingNullCharacter(const StringImpl& string)
{
return adoptRef(new StringImpl(string, WithTerminatingNullCharacter()));
}
PassRefPtr<StringImpl> StringImpl::copy()
{
return adoptRef(new StringImpl(m_data, m_length));
}
} // namespace WebCore
|