Abstract the logic for appending a UChar32 onto StringBuilder
authorcommit-queue@webkit.org <commit-queue@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 24 Jan 2013 23:01:06 +0000 (23:01 +0000)
committercommit-queue@webkit.org <commit-queue@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 24 Jan 2013 23:01:06 +0000 (23:01 +0000)
https://bugs.webkit.org/show_bug.cgi?id=107505

Patch by Martin Robinson <mrobinson@igalia.com> on 2013-01-24
Reviewed by Darin Adler.

Source/WebCore:

* css/CSSOMUtils.cpp:
(WebCore::serializeCharacter): Use the new StringBuilder append.
(WebCore::serializeIdentifier): Ditto.
(WebCore::serializeString): Ditto.
* html/parser/HTMLEntityParser.cpp:
(WebCore::HTMLEntityParser::consumeNamedEntity): Ditto.
* svg/SVGFontData.cpp:
(WebCore::SVGFontData::createStringWithMirroredCharacters): Ditto.
* xml/parser/CharacterReferenceParserInlines.h:
(WebCore::consumeCharacterReference): Ditto.
* xml/parser/XMLCharacterReferenceParser.cpp: Remove an older helper
superseded by StringBuilder::append.

Source/WTF:

* wtf/text/StringBuilder.h:
(WTF::StringBuilder::append): Added a method for appending a UChar32 to a StringBuilder.

Tools:

* TestWebKitAPI/Tests/WTF/StringBuilder.cpp:
(TestWebKitAPI::TEST): Added a simple test for appending UChar32.

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

Source/WTF/ChangeLog
Source/WTF/wtf/text/StringBuilder.h
Source/WebCore/ChangeLog
Source/WebCore/css/CSSOMUtils.cpp
Source/WebCore/html/parser/HTMLEntityParser.cpp
Source/WebCore/svg/SVGFontData.cpp
Source/WebCore/xml/parser/CharacterReferenceParserInlines.h
Tools/ChangeLog
Tools/TestWebKitAPI/Tests/WTF/StringBuilder.cpp

index 9593f6f..56b76b4 100644 (file)
@@ -1,3 +1,13 @@
+2013-01-24  Martin Robinson  <mrobinson@igalia.com>
+
+        Abstract the logic for appending a UChar32 onto StringBuilder
+        https://bugs.webkit.org/show_bug.cgi?id=107505
+
+        Reviewed by Darin Adler.
+
+        * wtf/text/StringBuilder.h:
+        (WTF::StringBuilder::append): Added a method for appending a UChar32 to a StringBuilder.
+
 2013-01-24  Brent Fulgham  <bfulgham@webkit.org>
 
         Stipulate build order between WTFGenerated and WTF
index e65f8f3..9279151 100644 (file)
@@ -141,6 +141,16 @@ public:
         append(static_cast<LChar>(c));
     }
 
+    void append(UChar32 c)
+    {
+        if (U_IS_BMP(c)) {
+            append(static_cast<UChar>(c));
+            return;
+        }
+        append(U16_LEAD(c));
+        append(U16_TRAIL(c));
+    }
+
     template<unsigned charactersCount>
     ALWAYS_INLINE void appendLiteral(const char (&characters)[charactersCount]) { append(characters, charactersCount - 1); }
 
index 126392a..d5c1cba 100644 (file)
@@ -1,3 +1,23 @@
+2013-01-24  Martin Robinson  <mrobinson@igalia.com>
+
+        Abstract the logic for appending a UChar32 onto StringBuilder
+        https://bugs.webkit.org/show_bug.cgi?id=107505
+
+        Reviewed by Darin Adler.
+
+        * css/CSSOMUtils.cpp:
+        (WebCore::serializeCharacter): Use the new StringBuilder append.
+        (WebCore::serializeIdentifier): Ditto.
+        (WebCore::serializeString): Ditto.
+        * html/parser/HTMLEntityParser.cpp:
+        (WebCore::HTMLEntityParser::consumeNamedEntity): Ditto.
+        * svg/SVGFontData.cpp:
+        (WebCore::SVGFontData::createStringWithMirroredCharacters): Ditto.
+        * xml/parser/CharacterReferenceParserInlines.h:
+        (WebCore::consumeCharacterReference): Ditto.
+        * xml/parser/XMLCharacterReferenceParser.cpp: Remove an older helper
+        superseded by StringBuilder::append.
+
 2013-01-24  Kentaro Hara  <haraken@chromium.org>
 
         [V8] Pass an Isolate to GetTemplate() in v8/*.cpp
index 33642db..e763bfe 100644 (file)
 
 namespace WebCore {
 
-static void appendCharacter(UChar32 c, StringBuilder& appendTo)
-{
-    if (U16_LENGTH(c) == 1)
-        appendTo.append(static_cast<UChar>(c));
-    else {
-        appendTo.append(U16_LEAD(c));
-        appendTo.append(U16_TRAIL(c));
-    }
-}
-
 void serializeCharacter(UChar32 c, StringBuilder& appendTo)
 {
     appendTo.append('\\');
-    appendCharacter(c, appendTo);
+    appendTo.append(c);
 }
 
 void serializeCharacterAsCodePoint(UChar32 c, StringBuilder& appendTo)
@@ -81,7 +71,7 @@ void serializeIdentifier(const String& identifier, StringBuilder& appendTo)
         else if (c == 0x2d && isSecond && isFirstCharHyphen)
             serializeCharacter(c, appendTo);
         else if (0x80 <= c || c == 0x2d || c == 0x5f || (0x30 <= c && c <= 0x39) || (0x41 <= c && c <= 0x5a) || (0x61 <= c && c <= 0x7a))
-            appendCharacter(c, appendTo);
+            appendTo.append(c);
         else
             serializeCharacter(c, appendTo);
 
@@ -115,7 +105,7 @@ void serializeString(const String& string, StringBuilder& appendTo)
         else if (c == 0x22 || c == 0x5c)
             serializeCharacter(c, appendTo);
         else
-            appendCharacter(c, appendTo);
+            appendTo.append(c);
     }
 
     appendTo.append('\"');
index cff22e0..6eefad4 100644 (file)
@@ -68,18 +68,6 @@ public:
         return value;
     }
 
-    inline static void convertToUTF16(UChar32 value, StringBuilder& decodedEntity)
-    {
-        if (U_IS_BMP(value)) {
-            UChar character = static_cast<UChar>(value);
-            ASSERT(character == value);
-            decodedEntity.append(character);
-            return;
-        }
-        decodedEntity.append(U16_LEAD(value));
-        decodedEntity.append(U16_TRAIL(value));
-    }
-
     inline static bool acceptMalformed() { return true; }
 
     inline static bool consumeNamedEntity(SegmentedString& source, StringBuilder& decodedEntity, bool& notEnoughCharacters, UChar additionalAllowedCharacter, UChar& cc)
@@ -125,9 +113,9 @@ public:
         if (entitySearch.mostRecentMatch()->lastCharacter() == ';'
             || !additionalAllowedCharacter
             || !(isAlphaNumeric(cc) || cc == '=')) {
-            convertToUTF16(entitySearch.mostRecentMatch()->firstValue, decodedEntity);
+            decodedEntity.append(entitySearch.mostRecentMatch()->firstValue);
             if (entitySearch.mostRecentMatch()->secondValue)
-                convertToUTF16(entitySearch.mostRecentMatch()->secondValue, decodedEntity);
+                decodedEntity.append(entitySearch.mostRecentMatch()->secondValue);
             return true;
         }
         unconsumeCharacters(source, consumedCharacters);
index 84ca568..9a56e62 100644 (file)
@@ -291,18 +291,11 @@ String SVGFontData::createStringWithMirroredCharacters(const UChar* characters,
     StringBuilder mirroredCharacters;
     mirroredCharacters.reserveCapacity(length);
 
-    UChar32 character;
     unsigned i = 0;
     while (i < length) {
+        UChar32 character;
         U16_NEXT(characters, i, length, character);
-        character = mirroredChar(character);
-
-        if (U16_LENGTH(character) == 1)
-            mirroredCharacters.append(static_cast<UChar>(character));
-        else {
-            mirroredCharacters.append(U16_LEAD(character));
-            mirroredCharacters.append(U16_TRAIL(character));
-        }
+        mirroredCharacters.append(mirroredChar(character));
     }
 
     return mirroredCharacters.toString();
index 159cf93..62780c7 100644 (file)
@@ -128,10 +128,10 @@ bool consumeCharacterReference(SegmentedString& source, StringBuilder& decodedCh
                 result = result * 16 + 10 + cc - 'A';
             else if (cc == ';') {
                 source.advanceAndASSERT(cc);
-                ParserFunctions::convertToUTF16(ParserFunctions::legalEntityFor(result), decodedCharacter);
+                decodedCharacter.append(ParserFunctions::legalEntityFor(result));
                 return true;
             } else if (ParserFunctions::acceptMalformed()) {
-                ParserFunctions::convertToUTF16(ParserFunctions::legalEntityFor(result), decodedCharacter);
+                decodedCharacter.append(ParserFunctions::legalEntityFor(result));
                 return true;
             } else {
                 unconsumeCharacters(source, consumedCharacters);
@@ -144,10 +144,10 @@ bool consumeCharacterReference(SegmentedString& source, StringBuilder& decodedCh
                 result = result * 10 + cc - '0';
             else if (cc == ';') {
                 source.advanceAndASSERT(cc);
-                ParserFunctions::convertToUTF16(ParserFunctions::legalEntityFor(result), decodedCharacter);
+                decodedCharacter.append(ParserFunctions::legalEntityFor(result));
                 return true;
             } else if (ParserFunctions::acceptMalformed()) {
-                ParserFunctions::convertToUTF16(ParserFunctions::legalEntityFor(result), decodedCharacter);
+                decodedCharacter.append(ParserFunctions::legalEntityFor(result));
                 return true;
             } else {
                 unconsumeCharacters(source, consumedCharacters);
index ae960a9..50b8831 100644 (file)
@@ -1,3 +1,13 @@
+2013-01-24  Martin Robinson  <mrobinson@igalia.com>
+
+        Abstract the logic for appending a UChar32 onto StringBuilder
+        https://bugs.webkit.org/show_bug.cgi?id=107505
+
+        Reviewed by Darin Adler.
+
+        * TestWebKitAPI/Tests/WTF/StringBuilder.cpp:
+        (TestWebKitAPI::TEST): Added a simple test for appending UChar32.
+
 2013-01-24  Erik Arvidsson  <arv@chromium.org>
 
         Unreviewed, rolling out r140703.
index 8ee5c6e..03be759 100644 (file)
 
 namespace TestWebKitAPI {
 
-void expectBuilderContent(const char* expected, const StringBuilder& builder)
+static void expectBuilderContent(const String& expected, const StringBuilder& builder)
 {
     // Not using builder.toString() or builder.toStringPreserveCapacity() because they all
     // change internal state of builder.
-    EXPECT_EQ(String(expected), String(builder.characters(), builder.length()));
+    EXPECT_EQ(expected, String(builder.characters(), builder.length()));
 }
 
 void expectEmpty(const StringBuilder& builder)
@@ -85,6 +85,16 @@ TEST(StringBuilderTest, Append)
     builder2.toStringPreserveCapacity(); // Test after reifyString with buffer preserved.
     builder2.append("abcd");
     ASSERT_EQ(characters, builder2.characters());
+
+    // Test appending UChar32 characters to StringBuilder.
+    StringBuilder builderForUChar32Append;
+    UChar32 frakturAChar = 0x1D504;
+    builderForUChar32Append.append(frakturAChar); // The fraktur A is not in the BMP, so it's two UTF-16 code units long.
+    ASSERT_EQ(2U, builderForUChar32Append.length());
+    builderForUChar32Append.append(static_cast<UChar32>('A'));
+    ASSERT_EQ(3U, builderForUChar32Append.length());
+    const UChar resultArray[] = { U16_LEAD(frakturAChar), U16_TRAIL(frakturAChar), 'A' };
+    expectBuilderContent(String(resultArray, WTF_ARRAY_LENGTH(resultArray)), builderForUChar32Append);
 }
 
 TEST(StringBuilderTest, ToString)