StringBuilder.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. /*
  2. * Copyright (C) 2010, 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. #include "config.h"
  27. #include "StringBuilder.h"
  28. #include "IntegerToStringConversion.h"
  29. #include "WTFString.h"
  30. namespace WTF {
  31. static unsigned expandedCapacity(unsigned capacity, unsigned requiredLength)
  32. {
  33. static const unsigned minimumCapacity = 16;
  34. return std::max(requiredLength, std::max(minimumCapacity, capacity * 2));
  35. }
  36. void StringBuilder::reifyString() const
  37. {
  38. // Check if the string already exists.
  39. if (!m_string.isNull()) {
  40. ASSERT(m_string.length() == m_length);
  41. return;
  42. }
  43. // Check for empty.
  44. if (!m_length) {
  45. m_string = StringImpl::empty();
  46. return;
  47. }
  48. // Must be valid in the buffer, take a substring (unless string fills the buffer).
  49. ASSERT(m_buffer && m_length <= m_buffer->length());
  50. m_string = (m_length == m_buffer->length())
  51. ? m_buffer.get()
  52. : StringImpl::create(m_buffer, 0, m_length);
  53. if (m_buffer->has16BitShadow() && m_valid16BitShadowLength < m_length)
  54. m_buffer->upconvertCharacters(m_valid16BitShadowLength, m_length);
  55. m_valid16BitShadowLength = m_length;
  56. }
  57. void StringBuilder::resize(unsigned newSize)
  58. {
  59. // Check newSize < m_length, hence m_length > 0.
  60. ASSERT(newSize <= m_length);
  61. if (newSize == m_length)
  62. return;
  63. ASSERT(m_length);
  64. // If there is a buffer, we only need to duplicate it if it has more than one ref.
  65. if (m_buffer) {
  66. m_string = String(); // Clear the string to remove the reference to m_buffer if any before checking the reference count of m_buffer.
  67. if (!m_buffer->hasOneRef()) {
  68. if (m_buffer->is8Bit())
  69. allocateBuffer(m_buffer->characters8(), m_buffer->length());
  70. else
  71. allocateBuffer(m_buffer->characters16(), m_buffer->length());
  72. }
  73. m_length = newSize;
  74. return;
  75. }
  76. // Since m_length && !m_buffer, the string must be valid in m_string, and m_string.length() > 0.
  77. ASSERT(!m_string.isEmpty());
  78. ASSERT(m_length == m_string.length());
  79. ASSERT(newSize < m_string.length());
  80. m_length = newSize;
  81. m_string = StringImpl::create(m_string.impl(), 0, newSize);
  82. }
  83. // Allocate a new 8 bit buffer, copying in currentCharacters (these may come from either m_string
  84. // or m_buffer, neither will be reassigned until the copy has completed).
  85. void StringBuilder::allocateBuffer(const LChar* currentCharacters, unsigned requiredLength)
  86. {
  87. ASSERT(m_is8Bit);
  88. // Copy the existing data into a new buffer, set result to point to the end of the existing data.
  89. RefPtr<StringImpl> buffer = StringImpl::createUninitialized(requiredLength, m_bufferCharacters8);
  90. memcpy(m_bufferCharacters8, currentCharacters, static_cast<size_t>(m_length) * sizeof(LChar)); // This can't overflow.
  91. // Update the builder state.
  92. m_buffer = buffer.release();
  93. m_string = String();
  94. }
  95. // Allocate a new 16 bit buffer, copying in currentCharacters (these may come from either m_string
  96. // or m_buffer, neither will be reassigned until the copy has completed).
  97. void StringBuilder::allocateBuffer(const UChar* currentCharacters, unsigned requiredLength)
  98. {
  99. ASSERT(!m_is8Bit);
  100. // Copy the existing data into a new buffer, set result to point to the end of the existing data.
  101. RefPtr<StringImpl> buffer = StringImpl::createUninitialized(requiredLength, m_bufferCharacters16);
  102. memcpy(m_bufferCharacters16, currentCharacters, static_cast<size_t>(m_length) * sizeof(UChar)); // This can't overflow.
  103. // Update the builder state.
  104. m_buffer = buffer.release();
  105. m_string = String();
  106. }
  107. // Allocate a new 16 bit buffer, copying in currentCharacters (which is 8 bit and may come
  108. // from either m_string or m_buffer, neither will be reassigned until the copy has completed).
  109. void StringBuilder::allocateBufferUpConvert(const LChar* currentCharacters, unsigned requiredLength)
  110. {
  111. ASSERT(m_is8Bit);
  112. // Copy the existing data into a new buffer, set result to point to the end of the existing data.
  113. RefPtr<StringImpl> buffer = StringImpl::createUninitialized(requiredLength, m_bufferCharacters16);
  114. for (unsigned i = 0; i < m_length; ++i)
  115. m_bufferCharacters16[i] = currentCharacters[i];
  116. m_is8Bit = false;
  117. // Update the builder state.
  118. m_buffer = buffer.release();
  119. m_string = String();
  120. }
  121. template <>
  122. void StringBuilder::reallocateBuffer<LChar>(unsigned requiredLength)
  123. {
  124. // If the buffer has only one ref (by this StringBuilder), reallocate it,
  125. // otherwise fall back to "allocate and copy" method.
  126. m_string = String();
  127. ASSERT(m_is8Bit);
  128. ASSERT(m_buffer->is8Bit());
  129. if (m_buffer->hasOneRef())
  130. m_buffer = StringImpl::reallocate(m_buffer.release(), requiredLength, m_bufferCharacters8);
  131. else
  132. allocateBuffer(m_buffer->characters8(), requiredLength);
  133. }
  134. template <>
  135. void StringBuilder::reallocateBuffer<UChar>(unsigned requiredLength)
  136. {
  137. // If the buffer has only one ref (by this StringBuilder), reallocate it,
  138. // otherwise fall back to "allocate and copy" method.
  139. m_string = String();
  140. if (m_buffer->is8Bit())
  141. allocateBufferUpConvert(m_buffer->characters8(), requiredLength);
  142. else if (m_buffer->hasOneRef())
  143. m_buffer = StringImpl::reallocate(m_buffer.release(), requiredLength, m_bufferCharacters16);
  144. else
  145. allocateBuffer(m_buffer->characters16(), requiredLength);
  146. }
  147. void StringBuilder::reserveCapacity(unsigned newCapacity)
  148. {
  149. if (m_buffer) {
  150. // If there is already a buffer, then grow if necessary.
  151. if (newCapacity > m_buffer->length()) {
  152. if (m_buffer->is8Bit())
  153. reallocateBuffer<LChar>(newCapacity);
  154. else
  155. reallocateBuffer<UChar>(newCapacity);
  156. }
  157. } else {
  158. // Grow the string, if necessary.
  159. if (newCapacity > m_length) {
  160. if (!m_length) {
  161. LChar* nullPlaceholder = 0;
  162. allocateBuffer(nullPlaceholder, newCapacity);
  163. } else if (m_string.is8Bit())
  164. allocateBuffer(m_string.characters8(), newCapacity);
  165. else
  166. allocateBuffer(m_string.characters16(), newCapacity);
  167. }
  168. }
  169. }
  170. // Make 'length' additional capacity be available in m_buffer, update m_string & m_length,
  171. // return a pointer to the newly allocated storage.
  172. template <typename CharType>
  173. ALWAYS_INLINE CharType* StringBuilder::appendUninitialized(unsigned length)
  174. {
  175. ASSERT(length);
  176. // Calculate the new size of the builder after appending.
  177. unsigned requiredLength = length + m_length;
  178. if (requiredLength < length)
  179. CRASH();
  180. if ((m_buffer) && (requiredLength <= m_buffer->length())) {
  181. // If the buffer is valid it must be at least as long as the current builder contents!
  182. ASSERT(m_buffer->length() >= m_length);
  183. unsigned currentLength = m_length;
  184. m_string = String();
  185. m_length = requiredLength;
  186. return getBufferCharacters<CharType>() + currentLength;
  187. }
  188. return appendUninitializedSlow<CharType>(requiredLength);
  189. }
  190. // Make 'length' additional capacity be available in m_buffer, update m_string & m_length,
  191. // return a pointer to the newly allocated storage.
  192. template <typename CharType>
  193. CharType* StringBuilder::appendUninitializedSlow(unsigned requiredLength)
  194. {
  195. ASSERT(requiredLength);
  196. if (m_buffer) {
  197. // If the buffer is valid it must be at least as long as the current builder contents!
  198. ASSERT(m_buffer->length() >= m_length);
  199. reallocateBuffer<CharType>(expandedCapacity(capacity(), requiredLength));
  200. } else {
  201. ASSERT(m_string.length() == m_length);
  202. allocateBuffer(m_length ? m_string.getCharacters<CharType>() : 0, expandedCapacity(capacity(), requiredLength));
  203. }
  204. CharType* result = getBufferCharacters<CharType>() + m_length;
  205. m_length = requiredLength;
  206. return result;
  207. }
  208. void StringBuilder::append(const UChar* characters, unsigned length)
  209. {
  210. if (!length)
  211. return;
  212. ASSERT(characters);
  213. if (m_is8Bit) {
  214. if (length == 1 && !(*characters & ~0xff)) {
  215. // Append as 8 bit character
  216. LChar lChar = static_cast<LChar>(*characters);
  217. append(&lChar, 1);
  218. return;
  219. }
  220. // Calculate the new size of the builder after appending.
  221. unsigned requiredLength = length + m_length;
  222. if (requiredLength < length)
  223. CRASH();
  224. if (m_buffer) {
  225. // If the buffer is valid it must be at least as long as the current builder contents!
  226. ASSERT(m_buffer->length() >= m_length);
  227. allocateBufferUpConvert(m_buffer->characters8(), expandedCapacity(capacity(), requiredLength));
  228. } else {
  229. ASSERT(m_string.length() == m_length);
  230. allocateBufferUpConvert(m_string.isNull() ? 0 : m_string.characters8(), expandedCapacity(capacity(), requiredLength));
  231. }
  232. memcpy(m_bufferCharacters16 + m_length, characters, static_cast<size_t>(length) * sizeof(UChar));
  233. m_length = requiredLength;
  234. } else
  235. memcpy(appendUninitialized<UChar>(length), characters, static_cast<size_t>(length) * sizeof(UChar));
  236. }
  237. void StringBuilder::append(const LChar* characters, unsigned length)
  238. {
  239. if (!length)
  240. return;
  241. ASSERT(characters);
  242. if (m_is8Bit) {
  243. LChar* dest = appendUninitialized<LChar>(length);
  244. if (length > 8)
  245. memcpy(dest, characters, static_cast<size_t>(length) * sizeof(LChar));
  246. else {
  247. const LChar* end = characters + length;
  248. while (characters < end)
  249. *(dest++) = *(characters++);
  250. }
  251. } else {
  252. UChar* dest = appendUninitialized<UChar>(length);
  253. const LChar* end = characters + length;
  254. while (characters < end)
  255. *(dest++) = *(characters++);
  256. }
  257. }
  258. void StringBuilder::appendNumber(int number)
  259. {
  260. numberToStringSigned<StringBuilder>(number, this);
  261. }
  262. void StringBuilder::appendNumber(unsigned int number)
  263. {
  264. numberToStringUnsigned<StringBuilder>(number, this);
  265. }
  266. void StringBuilder::appendNumber(long number)
  267. {
  268. numberToStringSigned<StringBuilder>(number, this);
  269. }
  270. void StringBuilder::appendNumber(unsigned long number)
  271. {
  272. numberToStringUnsigned<StringBuilder>(number, this);
  273. }
  274. void StringBuilder::appendNumber(long long number)
  275. {
  276. numberToStringSigned<StringBuilder>(number, this);
  277. }
  278. void StringBuilder::appendNumber(unsigned long long number)
  279. {
  280. numberToStringUnsigned<StringBuilder>(number, this);
  281. }
  282. bool StringBuilder::canShrink() const
  283. {
  284. // Only shrink the buffer if it's less than 80% full. Need to tune this heuristic!
  285. return m_buffer && m_buffer->length() > (m_length + (m_length >> 2));
  286. }
  287. void StringBuilder::shrinkToFit()
  288. {
  289. if (canShrink()) {
  290. if (m_is8Bit)
  291. reallocateBuffer<LChar>(m_length);
  292. else
  293. reallocateBuffer<UChar>(m_length);
  294. m_string = m_buffer;
  295. m_buffer = 0;
  296. }
  297. }
  298. } // namespace WTF