]> git.saurik.com Git - apple/javascriptcore.git/blobdiff - runtime/JSStringBuilder.h
JavaScriptCore-7600.1.4.11.8.tar.gz
[apple/javascriptcore.git] / runtime / JSStringBuilder.h
index 5d4960e8995385e248858a0c9a7eb4f02a800f5e..63e51a3ce29febc80860f9da357217c161a68d1f 100644 (file)
@@ -32,6 +32,7 @@
 
 namespace JSC {
 
+// FIXME: Should move the last few callers over from this to WTF::StringBuilder.
 class JSStringBuilder {
 public:
     JSStringBuilder()
@@ -40,72 +41,63 @@ public:
     {
     }
 
-    void append(const UChar u)
+    void append(LChar character)
     {
         if (m_is8Bit) {
-            if (u < 0xff) {
-                LChar c = u;
-                m_okay &= buffer8.tryAppend(&c, 1);
+            m_okay &= buffer8.tryAppend(&character, 1);
+            return;
+        }
+        UChar upconvertedCharacter = character;
+        m_okay &= buffer16.tryAppend(&upconvertedCharacter, 1);
+    }
+
+    void append(UChar character)
+    {
+        if (m_is8Bit) {
+            if (character < 0x100) {
+                LChar narrowedCharacter = character;
+                m_okay &= buffer8.tryAppend(&narrowedCharacter, 1);
                 return;
             }
             upConvert();
         }
-        m_okay &= buffer16.tryAppend(&u, 1);
+        m_okay &= buffer16.tryAppend(&character, 1);
     }
 
     void append(const char* str)
     {
-        append(str, strlen(str));
+        append(reinterpret_cast<const LChar*>(str), strlen(str));
     }
 
-    void append(const char* str, size_t len)
+    JSValue build(ExecState* exec)
     {
+        if (!m_okay)
+            return throwOutOfMemoryError(exec);
         if (m_is8Bit) {
-            m_okay &= buffer8.tryAppend(reinterpret_cast<const LChar*>(str), len);
-            return;
-        }
-        m_okay &= buffer8.tryReserveCapacity(buffer16.size() + len);
-        for (size_t i = 0; i < len; i++) {
-            UChar u = static_cast<unsigned char>(str[i]);
-            m_okay &= buffer16.tryAppend(&u, 1);
+            buffer8.shrinkToFit();
+            if (!buffer8.data())
+                return throwOutOfMemoryError(exec);
+            return jsString(exec, String::adopt(buffer8));
         }
+        buffer16.shrinkToFit();
+        if (!buffer16.data())
+            return throwOutOfMemoryError(exec);
+        return jsString(exec, String::adopt(buffer16));
     }
 
-    void append(const LChar* str, size_t len)
+private:
+    void append(const LChar* characters, size_t length)
     {
         if (m_is8Bit) {
-            m_okay &= buffer8.tryAppend(str, len);
+            m_okay &= buffer8.tryAppend(characters, length);
             return;
         }
-        m_okay &= buffer8.tryReserveCapacity(buffer16.size() + len);
-        for (size_t i = 0; i < len; i++) {
-            UChar u = str[i];
-            m_okay &= buffer16.tryAppend(&u, 1);
-        }
-    }
-    
-    void append(const UChar* str, size_t len)
-    {
-        if (m_is8Bit)
-            upConvert(); // FIXME: We could check character by character its size.
-        m_okay &= buffer16.tryAppend(str, len);
-    }
-
-    void append(const String& str)
-    {
-        unsigned length = str.length();
-
-        if (!length)
-            return;
-
-        if (m_is8Bit) {
-            if (str.is8Bit()) {
-                m_okay &= buffer8.tryAppend(str.characters8(), length);
-                return;
-            }
-            upConvert();
+        // FIXME: There must be a more efficient way of doing this.
+        m_okay &= buffer16.tryReserveCapacity(buffer16.size() + length);
+        for (size_t i = 0; i < length; i++) {
+            UChar upconvertedCharacter = characters[i];
+            m_okay &= buffer16.tryAppend(&upconvertedCharacter, 1);
         }
-        m_okay &= buffer16.tryAppend(str.characters(), length);
     }
 
     void upConvert()
@@ -120,23 +112,6 @@ public:
         m_is8Bit = false;
     }
 
-    JSValue build(ExecState* exec)
-    {
-        if (!m_okay)
-            return throwOutOfMemoryError(exec);
-        if (m_is8Bit) {
-            buffer8.shrinkToFit();
-            if (!buffer8.data())
-                return throwOutOfMemoryError(exec);
-            return jsString(exec, String::adopt(buffer8));
-        }
-        buffer16.shrinkToFit();
-        if (!buffer16.data())
-            return throwOutOfMemoryError(exec);
-        return jsString(exec, String::adopt(buffer16));
-    }
-
-protected:
     Vector<LChar, 64, UnsafeVectorOverflow> buffer8;
     Vector<UChar, 64, UnsafeVectorOverflow> buffer16;
     bool m_okay;