Update String::containsOnlyASCII() to handle 8 bits strings
authorbenjamin@webkit.org <benjamin@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Mon, 5 Dec 2011 23:56:49 +0000 (23:56 +0000)
committerbenjamin@webkit.org <benjamin@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Mon, 5 Dec 2011 23:56:49 +0000 (23:56 +0000)
https://bugs.webkit.org/show_bug.cgi?id=73799

Reviewed by Darin Adler.

Source/JavaScriptCore:

Implement String::containsOnlyASCII() so that it does not
call String::characters().

* wtf/text/WTFString.h:
(WTF::String::containsOnlyASCII):

Source/WebCore:

When possible, change the call sites from charactersAreAllASCII()
to the optimized version String::containsOnlyASCII().

* platform/KURL.cpp:
(WebCore::KURL::init):
* platform/cf/BinaryPropertyList.cpp:
(WebCore::BinaryPropertyListPlan::writeStringObject):
* platform/graphics/chromium/FontCacheChromiumWin.cpp:
(WebCore::FontCodepage::if):

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@102059 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Source/JavaScriptCore/ChangeLog
Source/JavaScriptCore/wtf/text/WTFString.h
Source/WebCore/ChangeLog
Source/WebCore/platform/KURL.cpp
Source/WebCore/platform/cf/BinaryPropertyList.cpp
Source/WebCore/platform/graphics/chromium/FontCacheChromiumWin.cpp

index a99efdf1c83fd09726c9d8cd54ccea946ec38b4e..b420d0d41d5266716a7f05373f3656aad9fe6ecf 100644 (file)
@@ -1,3 +1,16 @@
+2011-12-05  Benjamin Poulain  <benjamin@webkit.org>
+
+        Update String::containsOnlyASCII() to handle 8 bits strings
+        https://bugs.webkit.org/show_bug.cgi?id=73799
+
+        Reviewed by Darin Adler.
+
+        Implement String::containsOnlyASCII() so that it does not
+        call String::characters().
+
+        * wtf/text/WTFString.h:
+        (WTF::String::containsOnlyASCII):
+
 2011-12-05  Filip Pizlo  <fpizlo@apple.com>
 
         Unreviewed build fix for non-DFG platforms.
index bc5ebdbd97b85e3af86c0b60585de8ff8b88e9bc..7ffc7ec4670dbe9baef04d2390313d3e6f36f10d 100644 (file)
@@ -61,7 +61,7 @@ struct StringHash;
 
 // Declarations of string operations
 
-bool charactersAreAllASCII(const UChar*, size_t);
+template<typename CharType> inline bool charactersAreAllASCII(const CharType* characters, size_t length);
 WTF_EXPORT_PRIVATE int charactersToIntStrict(const LChar*, size_t, bool* ok = 0, int base = 10);
 WTF_EXPORT_PRIVATE int charactersToIntStrict(const UChar*, size_t, bool* ok = 0, int base = 10);
 WTF_EXPORT_PRIVATE unsigned charactersToUIntStrict(const LChar*, size_t, bool* ok = 0, int base = 10);
@@ -373,7 +373,7 @@ public:
         return WTF::Unicode::LeftToRight;
     }
 
-    bool containsOnlyASCII() const { return charactersAreAllASCII(characters(), length()); }
+    bool containsOnlyASCII() const;
     bool containsOnlyLatin1() const;
     bool containsOnlyWhitespace() const { return !m_impl || m_impl->containsOnlyWhitespace(); }
 
@@ -482,12 +482,26 @@ inline bool String::containsOnlyLatin1() const
 inline NSString* nsStringNilIfEmpty(const String& str) {  return str.isEmpty() ? nil : (NSString*)str; }
 #endif
 
-inline bool charactersAreAllASCII(const UChar* characters, size_t length)
+template<typename CharType>
+inline bool charactersAreAllASCII(const CharType* characters, size_t length)
 {
-    UChar ored = 0;
+    CharType ored = 0;
     for (size_t i = 0; i < length; ++i)
         ored |= characters[i];
-    return !(ored & 0xFF80);
+
+    CharType lowBits = 0x7F;
+    return !(ored & ~lowBits);
+}
+
+inline bool String::containsOnlyASCII() const
+{
+    if (isEmpty())
+        return true;
+
+    if (is8Bit())
+        return charactersAreAllASCII(characters8(), m_impl->length());
+
+    return charactersAreAllASCII(characters16(), m_impl->length());
 }
 
 WTF_EXPORT_PRIVATE int codePointCompare(const String&, const String&);
index 32117b87e15e53931cd6e872feb4580f17d34eb6..82208acc9835de9d12447f6e49e012357e993522 100644 (file)
@@ -1,3 +1,20 @@
+2011-12-05  Benjamin Poulain  <benjamin@webkit.org>
+
+        Update String::containsOnlyASCII() to handle 8 bits strings
+        https://bugs.webkit.org/show_bug.cgi?id=73799
+
+        Reviewed by Darin Adler.
+
+        When possible, change the call sites from charactersAreAllASCII()
+        to the optimized version String::containsOnlyASCII().
+
+        * platform/KURL.cpp:
+        (WebCore::KURL::init):
+        * platform/cf/BinaryPropertyList.cpp:
+        (WebCore::BinaryPropertyListPlan::writeStringObject):
+        * platform/graphics/chromium/FontCacheChromiumWin.cpp:
+        (WebCore::FontCodepage::if):
+
 2011-12-01  Vangelis Kokkevis  <vangelis@chromium.org>
 
         [chromium] Use ANGLE's texture_usage and texture_storage extensions when allocating compositor textures
index 7c8b1dbfbcb9daca511bdb9d675742dedaac2d32..0fa35e5b43b74ae63730b5aeb003c4bf04d1e0d9 100644 (file)
@@ -376,7 +376,7 @@ void KURL::init(const KURL& base, const String& relative, const TextEncoding& en
     if (rel.contains('\\') && !(protocolIsJavaScript(rel) || protocolIs(rel, "data")))
         rel = substituteBackslashes(rel);
 
-    bool allASCII = charactersAreAllASCII(rel.characters(), rel.length());
+    bool allASCII = rel.containsOnlyASCII();
     CharBuffer strBuffer;
     char* str;
     size_t len;
index 65be70a1ae1f2e08aa0a4ce2a86f08992b98f64b..4b823b12b78b65dcc75bc1117626814066f285f9 100644 (file)
@@ -285,10 +285,9 @@ static size_t markerPlusLengthByteCount(size_t length)
 
 void BinaryPropertyListPlan::writeStringObject(const String& string)
 {
-    const UChar* characters = string.characters();
     unsigned length = string.length();
     m_byteCount += markerPlusLengthByteCount(length) + length;
-    if (!charactersAreAllASCII(characters, length))
+    if (!string.containsOnlyASCII())
         m_byteCount += length;
 }
 
index 52d1064c0b2765b27ec97543b79e313d07c361d2..9f5bd4919fda9032c0092e2cc1045d9e204b9fca 100644 (file)
@@ -50,12 +50,6 @@ using std::min;
 namespace WebCore
 {
 
-// FIXME: consider adding to WebKit String class
-static bool charactersAreAllASCII(const String& s)
-{
-    return WTF::charactersAreAllASCII(s.characters(), s.length());
-}
-
 // When asked for a CJK font with a native name under a non-CJK locale or
 // asked for a CJK font with a Romanized name under a CJK locale,
 // |GetTextFace| (after |CreateFont*|) returns a 'bogus' value (e.g. Arial).
@@ -216,7 +210,7 @@ static bool LookupAltName(const String& name, String& altName)
     // use |lower| only for ASCII names 
     // For non-ASCII names, we don't want to invoke an expensive 
     // and unnecessary |lower|. 
-    if (charactersAreAllASCII(name)) {
+    if (name.containsOnlyASCII()) {
         isAscii = true;
         n = name.lower();
     } else