StringBuilder.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. /*
  2. * Copyright (C) 2009, 2010, 2012, 2013 Apple Inc. All rights reserved.
  3. * Copyright (C) 2012 Google Inc. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
  15. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  16. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  17. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
  18. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  19. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  20. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  21. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  22. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #ifndef StringBuilder_h
  27. #define StringBuilder_h
  28. #include <wtf/text/AtomicString.h>
  29. #include <wtf/text/WTFString.h>
  30. namespace WTF {
  31. class StringBuilder {
  32. // Disallow copying since it's expensive and we don't want code to do it by accident.
  33. WTF_MAKE_NONCOPYABLE(StringBuilder);
  34. public:
  35. StringBuilder()
  36. : m_length(0)
  37. , m_is8Bit(true)
  38. , m_valid16BitShadowLength(0)
  39. , m_bufferCharacters8(0)
  40. {
  41. }
  42. WTF_EXPORT_PRIVATE void append(const UChar*, unsigned);
  43. WTF_EXPORT_PRIVATE void append(const LChar*, unsigned);
  44. ALWAYS_INLINE void append(const char* characters, unsigned length) { append(reinterpret_cast<const LChar*>(characters), length); }
  45. void append(const String& string)
  46. {
  47. if (!string.length())
  48. return;
  49. // If we're appending to an empty string, and there is not a buffer (reserveCapacity has not been called)
  50. // then just retain the string.
  51. if (!m_length && !m_buffer) {
  52. m_string = string;
  53. m_length = string.length();
  54. m_is8Bit = m_string.is8Bit();
  55. return;
  56. }
  57. if (string.is8Bit())
  58. append(string.characters8(), string.length());
  59. else
  60. append(string.characters16(), string.length());
  61. }
  62. void append(const StringBuilder& other)
  63. {
  64. if (!other.m_length)
  65. return;
  66. // If we're appending to an empty string, and there is not a buffer (reserveCapacity has not been called)
  67. // then just retain the string.
  68. if (!m_length && !m_buffer && !other.m_string.isNull()) {
  69. m_string = other.m_string;
  70. m_length = other.m_length;
  71. return;
  72. }
  73. if (other.is8Bit())
  74. append(other.characters8(), other.m_length);
  75. else
  76. append(other.characters16(), other.m_length);
  77. }
  78. void append(const String& string, unsigned offset, unsigned length)
  79. {
  80. if (!string.length())
  81. return;
  82. if ((offset + length) > string.length())
  83. return;
  84. if (string.is8Bit())
  85. append(string.characters8() + offset, length);
  86. else
  87. append(string.characters16() + offset, length);
  88. }
  89. void append(const char* characters)
  90. {
  91. if (characters)
  92. append(characters, strlen(characters));
  93. }
  94. void append(UChar c)
  95. {
  96. if (m_buffer && m_length < m_buffer->length() && m_string.isNull()) {
  97. if (!m_is8Bit) {
  98. m_bufferCharacters16[m_length++] = c;
  99. return;
  100. }
  101. if (!(c & ~0xff)) {
  102. m_bufferCharacters8[m_length++] = static_cast<LChar>(c);
  103. return;
  104. }
  105. }
  106. append(&c, 1);
  107. }
  108. void append(LChar c)
  109. {
  110. if (m_buffer && m_length < m_buffer->length() && m_string.isNull()) {
  111. if (m_is8Bit)
  112. m_bufferCharacters8[m_length++] = c;
  113. else
  114. m_bufferCharacters16[m_length++] = c;
  115. } else
  116. append(&c, 1);
  117. }
  118. void append(char c)
  119. {
  120. append(static_cast<LChar>(c));
  121. }
  122. void append(UChar32 c)
  123. {
  124. if (U_IS_BMP(c)) {
  125. append(static_cast<UChar>(c));
  126. return;
  127. }
  128. append(U16_LEAD(c));
  129. append(U16_TRAIL(c));
  130. }
  131. template<unsigned charactersCount>
  132. ALWAYS_INLINE void appendLiteral(const char (&characters)[charactersCount]) { append(characters, charactersCount - 1); }
  133. WTF_EXPORT_PRIVATE void appendNumber(int);
  134. WTF_EXPORT_PRIVATE void appendNumber(unsigned int);
  135. WTF_EXPORT_PRIVATE void appendNumber(long);
  136. WTF_EXPORT_PRIVATE void appendNumber(unsigned long);
  137. WTF_EXPORT_PRIVATE void appendNumber(long long);
  138. WTF_EXPORT_PRIVATE void appendNumber(unsigned long long);
  139. String toString()
  140. {
  141. shrinkToFit();
  142. if (m_string.isNull())
  143. reifyString();
  144. return m_string;
  145. }
  146. const String& toStringPreserveCapacity() const
  147. {
  148. if (m_string.isNull())
  149. reifyString();
  150. return m_string;
  151. }
  152. AtomicString toAtomicString() const
  153. {
  154. if (!m_length)
  155. return emptyAtom;
  156. // If the buffer is sufficiently over-allocated, make a new AtomicString from a copy so its buffer is not so large.
  157. if (canShrink()) {
  158. if (is8Bit())
  159. return AtomicString(characters8(), length());
  160. return AtomicString(characters16(), length());
  161. }
  162. if (!m_string.isNull())
  163. return AtomicString(m_string);
  164. ASSERT(m_buffer);
  165. return AtomicString(m_buffer.get(), 0, m_length);
  166. }
  167. unsigned length() const
  168. {
  169. return m_length;
  170. }
  171. bool isEmpty() const { return !m_length; }
  172. WTF_EXPORT_PRIVATE void reserveCapacity(unsigned newCapacity);
  173. unsigned capacity() const
  174. {
  175. return m_buffer ? m_buffer->length() : m_length;
  176. }
  177. WTF_EXPORT_PRIVATE void resize(unsigned newSize);
  178. WTF_EXPORT_PRIVATE bool canShrink() const;
  179. WTF_EXPORT_PRIVATE void shrinkToFit();
  180. UChar operator[](unsigned i) const
  181. {
  182. ASSERT_WITH_SECURITY_IMPLICATION(i < m_length);
  183. if (m_is8Bit)
  184. return characters8()[i];
  185. return characters16()[i];
  186. }
  187. const LChar* characters8() const
  188. {
  189. ASSERT(m_is8Bit);
  190. if (!m_length)
  191. return 0;
  192. if (!m_string.isNull())
  193. return m_string.characters8();
  194. ASSERT(m_buffer);
  195. return m_buffer->characters8();
  196. }
  197. const UChar* characters16() const
  198. {
  199. ASSERT(!m_is8Bit);
  200. if (!m_length)
  201. return 0;
  202. if (!m_string.isNull())
  203. return m_string.characters16();
  204. ASSERT(m_buffer);
  205. return m_buffer->characters16();
  206. }
  207. const UChar* characters() const
  208. {
  209. if (!m_length)
  210. return 0;
  211. if (!m_string.isNull())
  212. return m_string.characters();
  213. ASSERT(m_buffer);
  214. if (m_buffer->has16BitShadow() && m_valid16BitShadowLength < m_length)
  215. m_buffer->upconvertCharacters(m_valid16BitShadowLength, m_length);
  216. m_valid16BitShadowLength = m_length;
  217. return m_buffer->characters();
  218. }
  219. bool is8Bit() const { return m_is8Bit; }
  220. void clear()
  221. {
  222. m_length = 0;
  223. m_string = String();
  224. m_buffer = 0;
  225. m_bufferCharacters8 = 0;
  226. m_is8Bit = true;
  227. m_valid16BitShadowLength = 0;
  228. }
  229. void swap(StringBuilder& stringBuilder)
  230. {
  231. std::swap(m_length, stringBuilder.m_length);
  232. m_string.swap(stringBuilder.m_string);
  233. m_buffer.swap(stringBuilder.m_buffer);
  234. std::swap(m_is8Bit, stringBuilder.m_is8Bit);
  235. std::swap(m_valid16BitShadowLength, stringBuilder.m_valid16BitShadowLength);
  236. std::swap(m_bufferCharacters8, stringBuilder.m_bufferCharacters8);
  237. }
  238. private:
  239. void allocateBuffer(const LChar* currentCharacters, unsigned requiredLength);
  240. void allocateBuffer(const UChar* currentCharacters, unsigned requiredLength);
  241. void allocateBufferUpConvert(const LChar* currentCharacters, unsigned requiredLength);
  242. template <typename CharType>
  243. void reallocateBuffer(unsigned requiredLength);
  244. template <typename CharType>
  245. ALWAYS_INLINE CharType* appendUninitialized(unsigned length);
  246. template <typename CharType>
  247. CharType* appendUninitializedSlow(unsigned length);
  248. template <typename CharType>
  249. ALWAYS_INLINE CharType * getBufferCharacters();
  250. WTF_EXPORT_PRIVATE void reifyString() const;
  251. unsigned m_length;
  252. mutable String m_string;
  253. RefPtr<StringImpl> m_buffer;
  254. bool m_is8Bit;
  255. mutable unsigned m_valid16BitShadowLength;
  256. union {
  257. LChar* m_bufferCharacters8;
  258. UChar* m_bufferCharacters16;
  259. };
  260. };
  261. template <>
  262. ALWAYS_INLINE LChar* StringBuilder::getBufferCharacters<LChar>()
  263. {
  264. ASSERT(m_is8Bit);
  265. return m_bufferCharacters8;
  266. }
  267. template <>
  268. ALWAYS_INLINE UChar* StringBuilder::getBufferCharacters<UChar>()
  269. {
  270. ASSERT(!m_is8Bit);
  271. return m_bufferCharacters16;
  272. }
  273. template <typename CharType>
  274. bool equal(const StringBuilder& s, const CharType* buffer, unsigned length)
  275. {
  276. if (s.length() != length)
  277. return false;
  278. if (s.is8Bit())
  279. return equal(s.characters8(), buffer, length);
  280. return equal(s.characters16(), buffer, length);
  281. }
  282. template <typename StringType>
  283. bool equal(const StringBuilder& a, const StringType& b)
  284. {
  285. if (a.length() != b.length())
  286. return false;
  287. if (!a.length())
  288. return true;
  289. if (a.is8Bit()) {
  290. if (b.is8Bit())
  291. return equal(a.characters8(), b.characters8(), a.length());
  292. return equal(a.characters8(), b.characters16(), a.length());
  293. }
  294. if (b.is8Bit())
  295. return equal(a.characters16(), b.characters8(), a.length());
  296. return equal(a.characters16(), b.characters16(), a.length());
  297. }
  298. inline bool operator==(const StringBuilder& a, const StringBuilder& b) { return equal(a, b); }
  299. inline bool operator!=(const StringBuilder& a, const StringBuilder& b) { return !equal(a, b); }
  300. inline bool operator==(const StringBuilder& a, const String& b) { return equal(a, b); }
  301. inline bool operator!=(const StringBuilder& a, const String& b) { return !equal(a, b); }
  302. inline bool operator==(const String& a, const StringBuilder& b) { return equal(b, a); }
  303. inline bool operator!=(const String& a, const StringBuilder& b) { return !equal(b, a); }
  304. } // namespace WTF
  305. using WTF::StringBuilder;
  306. #endif // StringBuilder_h