diff --git a/JavaScriptCore/ChangeLog b/JavaScriptCore/ChangeLog index c39feda1188c95a9ab9466eb665405db0a711e95..46027b0693dd32b8724ac4f5a9c34c536a016ca4 100644 --- a/JavaScriptCore/ChangeLog +++ b/JavaScriptCore/ChangeLog @@ -1,3 +1,27 @@ +2010-05-27 Luiz Agostini + + Reviewed by Darin Adler. + + UTF-16 code points compare() for String objects + https://bugs.webkit.org/show_bug.cgi?id=39701 + + Moving compare() implementation from UString to StringImpl for it to be shared + with String. Adding overloaded free functions codePointCompare() in StringImpl + and WTFString. Renaming function compare in UString to codePointCompare to be + consistent. + + * runtime/JSArray.cpp: + (JSC::compareByStringPairForQSort): + * runtime/UString.cpp: + * runtime/UString.h: + (JSC::codePointCompare): + * wtf/text/StringImpl.cpp: + (WebCore::codePointCompare): + * wtf/text/StringImpl.h: + * wtf/text/WTFString.cpp: + (WebCore::codePointCompare): + * wtf/text/WTFString.h: + 2010-05-26 Darin Adler Reviewed by Kent Tamura. diff --git a/JavaScriptCore/runtime/JSArray.cpp b/JavaScriptCore/runtime/JSArray.cpp index d3ef44cb9ee0da8448f42ffeaeff9ca9fef3343c..cf32e07ffd59334099bb5b11d6989707317d5f31 100644 --- a/JavaScriptCore/runtime/JSArray.cpp +++ b/JavaScriptCore/runtime/JSArray.cpp @@ -649,7 +649,7 @@ static int compareByStringPairForQSort(const void* a, const void* b) { const ValueStringPair* va = static_cast(a); const ValueStringPair* vb = static_cast(b); - return compare(va->second, vb->second); + return codePointCompare(va->second, vb->second); } void JSArray::sortNumeric(ExecState* exec, JSValue compareFunction, CallType callType, const CallData& callData) diff --git a/JavaScriptCore/runtime/UString.cpp b/JavaScriptCore/runtime/UString.cpp index f5ba9b7da15d01254b5aba93bd849e0809b0948a..1c1193614bb66d147e5bb1945a107fa6bd970e70 100644 --- a/JavaScriptCore/runtime/UString.cpp +++ b/JavaScriptCore/runtime/UString.cpp @@ -580,29 +580,6 @@ bool operator>(const UString& s1, const UString& s2) return (l1 > l2); } -int compare(const UString& s1, const UString& s2) -{ - const unsigned l1 = s1.size(); - const unsigned l2 = s2.size(); - const unsigned lmin = l1 < l2 ? l1 : l2; - const UChar* c1 = s1.data(); - const UChar* c2 = s2.data(); - unsigned l = 0; - while (l < lmin && *c1 == *c2) { - c1++; - c2++; - l++; - } - - if (l < lmin) - return (c1[0] > c2[0]) ? 1 : -1; - - if (l1 == l2) - return 0; - - return (l1 > l2) ? 1 : -1; -} - CString UString::UTF8String(bool strict) const { // Allocate a buffer big enough to hold all the characters. diff --git a/JavaScriptCore/runtime/UString.h b/JavaScriptCore/runtime/UString.h index a97e0d714dde90e86bda5967223d9aa178a3782c..43640216f3be0bb378e64b29b3eb65548f4217e4 100644 --- a/JavaScriptCore/runtime/UString.h +++ b/JavaScriptCore/runtime/UString.h @@ -202,7 +202,10 @@ namespace JSC { return !JSC::operator==(s1, s2); } - int compare(const UString&, const UString&); + inline int codePointCompare(const UString& s1, const UString& s2) + { + return codePointCompare(s1.rep(), s2.rep()); + } // Rule from ECMA 15.2 about what an array index is. // Must exactly match string form of an unsigned integer, and be less than 2^32 - 1. diff --git a/JavaScriptCore/wtf/text/StringImpl.cpp b/JavaScriptCore/wtf/text/StringImpl.cpp index 3606597a4da097c1e937be95d030f6e531f0618e..698cab9616f7f69f675745663239f903bb434a1e 100644 --- a/JavaScriptCore/wtf/text/StringImpl.cpp +++ b/JavaScriptCore/wtf/text/StringImpl.cpp @@ -476,6 +476,29 @@ static inline bool equalIgnoringCase(const UChar* a, const UChar* b, int length) return umemcasecmp(a, b, length) == 0; } +int codePointCompare(const StringImpl* s1, const StringImpl* s2) +{ + const unsigned l1 = s1 ? s1->length() : 0; + const unsigned l2 = s2 ? s2->length() : 0; + const unsigned lmin = l1 < l2 ? l1 : l2; + const UChar* c1 = s1 ? s1->characters() : 0; + const UChar* c2 = s2 ? s2->characters() : 0; + unsigned pos = 0; + while (pos < lmin && *c1 == *c2) { + c1++; + c2++; + pos++; + } + + if (pos < lmin) + return (c1[0] > c2[0]) ? 1 : -1; + + if (l1 == l2) + return 0; + + return (l1 > l2) ? 1 : -1; +} + int StringImpl::find(const char* chs, int index, bool caseSensitive) { if (!chs || index < 0) diff --git a/JavaScriptCore/wtf/text/StringImpl.h b/JavaScriptCore/wtf/text/StringImpl.h index f4b2970283f6633ff351726f9db8916f18e6fc3e..244009f6db5f0a6232c999ad13e394edc795f839 100644 --- a/JavaScriptCore/wtf/text/StringImpl.h +++ b/JavaScriptCore/wtf/text/StringImpl.h @@ -352,6 +352,8 @@ inline bool equalIgnoringCase(const char* a, const UChar* b, unsigned length) { bool equalIgnoringNullity(StringImpl*, StringImpl*); +int codePointCompare(const StringImpl*, const StringImpl*); + static inline bool isSpaceOrNewline(UChar c) { // Use isASCIISpace() for basic Latin-1. diff --git a/JavaScriptCore/wtf/text/WTFString.cpp b/JavaScriptCore/wtf/text/WTFString.cpp index 842d755c8ee665539e320feaf86bfeb931a556ea..d744b15247724bbbb10c1a8f3fbd7f13b8aeb737 100644 --- a/JavaScriptCore/wtf/text/WTFString.cpp +++ b/JavaScriptCore/wtf/text/WTFString.cpp @@ -126,6 +126,11 @@ String operator+(const char* cs, const String& s) return String(cs) + s; } +int codePointCompare(const String& a, const String& b) +{ + return codePointCompare(a.impl(), b.impl()); +} + void String::insert(const String& str, unsigned pos) { if (str.isEmpty()) { diff --git a/JavaScriptCore/wtf/text/WTFString.h b/JavaScriptCore/wtf/text/WTFString.h index d98621cb0395c5474234bd60242a15c7017b2088..90d9a71f4ccc78a550c87315960e743c311de882 100644 --- a/JavaScriptCore/wtf/text/WTFString.h +++ b/JavaScriptCore/wtf/text/WTFString.h @@ -351,6 +351,8 @@ inline bool charactersAreAllASCII(const UChar* characters, size_t length) return !(ored & 0xFF80); } +int codePointCompare(const String&, const String&); + inline int find(const UChar* characters, size_t length, UChar character, int startPosition) { if (startPosition >= static_cast(length))