JSStringBuilder.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. * Copyright (C) 2009 Apple Inc. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
  14. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  15. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  16. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
  17. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  18. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  19. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  20. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  21. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  23. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #ifndef JSStringBuilder_h
  26. #define JSStringBuilder_h
  27. #include "ExceptionHelpers.h"
  28. #include "JSString.h"
  29. #include <wtf/Vector.h>
  30. namespace JSC {
  31. class JSStringBuilder {
  32. public:
  33. JSStringBuilder()
  34. : m_okay(true)
  35. , m_is8Bit(true)
  36. {
  37. }
  38. void append(const UChar u)
  39. {
  40. if (m_is8Bit) {
  41. if (u < 0xff) {
  42. LChar c = u;
  43. m_okay &= buffer8.tryAppend(&c, 1);
  44. return;
  45. }
  46. upConvert();
  47. }
  48. m_okay &= buffer16.tryAppend(&u, 1);
  49. }
  50. void append(const char* str)
  51. {
  52. append(str, strlen(str));
  53. }
  54. void append(const char* str, size_t len)
  55. {
  56. if (m_is8Bit) {
  57. m_okay &= buffer8.tryAppend(reinterpret_cast<const LChar*>(str), len);
  58. return;
  59. }
  60. m_okay &= buffer8.tryReserveCapacity(buffer16.size() + len);
  61. for (size_t i = 0; i < len; i++) {
  62. UChar u = static_cast<unsigned char>(str[i]);
  63. m_okay &= buffer16.tryAppend(&u, 1);
  64. }
  65. }
  66. void append(const LChar* str, size_t len)
  67. {
  68. if (m_is8Bit) {
  69. m_okay &= buffer8.tryAppend(str, len);
  70. return;
  71. }
  72. m_okay &= buffer8.tryReserveCapacity(buffer16.size() + len);
  73. for (size_t i = 0; i < len; i++) {
  74. UChar u = str[i];
  75. m_okay &= buffer16.tryAppend(&u, 1);
  76. }
  77. }
  78. void append(const UChar* str, size_t len)
  79. {
  80. if (m_is8Bit)
  81. upConvert(); // FIXME: We could check character by character its size.
  82. m_okay &= buffer16.tryAppend(str, len);
  83. }
  84. void append(const String& str)
  85. {
  86. unsigned length = str.length();
  87. if (!length)
  88. return;
  89. if (m_is8Bit) {
  90. if (str.is8Bit()) {
  91. m_okay &= buffer8.tryAppend(str.characters8(), length);
  92. return;
  93. }
  94. upConvert();
  95. }
  96. m_okay &= buffer16.tryAppend(str.characters(), length);
  97. }
  98. void upConvert()
  99. {
  100. ASSERT(m_is8Bit);
  101. size_t len = buffer8.size();
  102. for (size_t i = 0; i < len; i++)
  103. buffer16.append(buffer8[i]);
  104. buffer8.clear();
  105. m_is8Bit = false;
  106. }
  107. JSValue build(ExecState* exec)
  108. {
  109. if (!m_okay)
  110. return throwOutOfMemoryError(exec);
  111. if (m_is8Bit) {
  112. buffer8.shrinkToFit();
  113. if (!buffer8.data())
  114. return throwOutOfMemoryError(exec);
  115. return jsString(exec, String::adopt(buffer8));
  116. }
  117. buffer16.shrinkToFit();
  118. if (!buffer16.data())
  119. return throwOutOfMemoryError(exec);
  120. return jsString(exec, String::adopt(buffer16));
  121. }
  122. protected:
  123. Vector<LChar, 64, UnsafeVectorOverflow> buffer8;
  124. Vector<UChar, 64, UnsafeVectorOverflow> buffer16;
  125. bool m_okay;
  126. bool m_is8Bit;
  127. };
  128. template<typename StringType1, typename StringType2>
  129. inline JSValue jsMakeNontrivialString(ExecState* exec, StringType1 string1, StringType2 string2)
  130. {
  131. PassRefPtr<StringImpl> result = WTF::tryMakeString(string1, string2);
  132. if (!result)
  133. return throwOutOfMemoryError(exec);
  134. return jsNontrivialString(exec, result);
  135. }
  136. template<typename StringType1, typename StringType2, typename StringType3>
  137. inline JSValue jsMakeNontrivialString(ExecState* exec, StringType1 string1, StringType2 string2, StringType3 string3)
  138. {
  139. PassRefPtr<StringImpl> result = WTF::tryMakeString(string1, string2, string3);
  140. if (!result)
  141. return throwOutOfMemoryError(exec);
  142. return jsNontrivialString(exec, result);
  143. }
  144. template<typename StringType1, typename StringType2, typename StringType3, typename StringType4>
  145. inline JSValue jsMakeNontrivialString(ExecState* exec, StringType1 string1, StringType2 string2, StringType3 string3, StringType4 string4)
  146. {
  147. PassRefPtr<StringImpl> result = WTF::tryMakeString(string1, string2, string3, string4);
  148. if (!result)
  149. return throwOutOfMemoryError(exec);
  150. return jsNontrivialString(exec, result);
  151. }
  152. template<typename StringType1, typename StringType2, typename StringType3, typename StringType4, typename StringType5>
  153. inline JSValue jsMakeNontrivialString(ExecState* exec, StringType1 string1, StringType2 string2, StringType3 string3, StringType4 string4, StringType5 string5)
  154. {
  155. PassRefPtr<StringImpl> result = WTF::tryMakeString(string1, string2, string3, string4, string5);
  156. if (!result)
  157. return throwOutOfMemoryError(exec);
  158. return jsNontrivialString(exec, result);
  159. }
  160. template<typename StringType1, typename StringType2, typename StringType3, typename StringType4, typename StringType5, typename StringType6>
  161. inline JSValue jsMakeNontrivialString(ExecState* exec, StringType1 string1, StringType2 string2, StringType3 string3, StringType4 string4, StringType5 string5, StringType6 string6)
  162. {
  163. PassRefPtr<StringImpl> result = WTF::tryMakeString(string1, string2, string3, string4, string5, string6);
  164. if (!result)
  165. return throwOutOfMemoryError(exec);
  166. return jsNontrivialString(exec, result);
  167. }
  168. }
  169. #endif