StringBuilder.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * 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 are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (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 <AK/ByteBuffer.h>
  27. #include <AK/Checked.h>
  28. #include <AK/Memory.h>
  29. #include <AK/PrintfImplementation.h>
  30. #include <AK/StdLibExtras.h>
  31. #include <AK/String.h>
  32. #include <AK/StringBuilder.h>
  33. #include <AK/StringView.h>
  34. #include <AK/Utf32View.h>
  35. namespace AK {
  36. inline void StringBuilder::will_append(size_t size)
  37. {
  38. Checked<size_t> needed_capacity = m_length;
  39. needed_capacity += size;
  40. VERIFY(!needed_capacity.has_overflow());
  41. if (needed_capacity < inline_capacity)
  42. return;
  43. Checked<size_t> expanded_capacity = needed_capacity;
  44. expanded_capacity *= 2;
  45. VERIFY(!expanded_capacity.has_overflow());
  46. if (m_buffer.is_null()) {
  47. m_buffer.grow(expanded_capacity.value());
  48. memcpy(m_buffer.data(), m_inline_buffer, m_length);
  49. } else if (needed_capacity.value() > m_buffer.size()) {
  50. m_buffer.grow(expanded_capacity.value());
  51. }
  52. }
  53. StringBuilder::StringBuilder(size_t initial_capacity)
  54. {
  55. if (initial_capacity > inline_capacity)
  56. m_buffer.grow(initial_capacity);
  57. }
  58. void StringBuilder::append(const StringView& str)
  59. {
  60. if (str.is_empty())
  61. return;
  62. will_append(str.length());
  63. memcpy(data() + m_length, str.characters_without_null_termination(), str.length());
  64. m_length += str.length();
  65. }
  66. void StringBuilder::append(const char* characters, size_t length)
  67. {
  68. append(StringView { characters, length });
  69. }
  70. void StringBuilder::append(char ch)
  71. {
  72. will_append(1);
  73. data()[m_length] = ch;
  74. m_length += 1;
  75. }
  76. void StringBuilder::appendvf(const char* fmt, va_list ap)
  77. {
  78. printf_internal([this](char*&, char ch) {
  79. append(ch);
  80. },
  81. nullptr, fmt, ap);
  82. }
  83. void StringBuilder::appendf(const char* fmt, ...)
  84. {
  85. va_list ap;
  86. va_start(ap, fmt);
  87. will_append(strlen(fmt));
  88. appendvf(fmt, ap);
  89. va_end(ap);
  90. }
  91. ByteBuffer StringBuilder::to_byte_buffer() const
  92. {
  93. return ByteBuffer::copy(data(), length());
  94. }
  95. String StringBuilder::to_string() const
  96. {
  97. if (is_empty())
  98. return String::empty();
  99. return String((const char*)data(), length());
  100. }
  101. String StringBuilder::build() const
  102. {
  103. return to_string();
  104. }
  105. StringView StringBuilder::string_view() const
  106. {
  107. return StringView { data(), m_length };
  108. }
  109. void StringBuilder::clear()
  110. {
  111. m_buffer.clear();
  112. m_inline_buffer[0] = '\0';
  113. m_length = 0;
  114. }
  115. void StringBuilder::append_code_point(u32 code_point)
  116. {
  117. if (code_point <= 0x7f) {
  118. append((char)code_point);
  119. } else if (code_point <= 0x07ff) {
  120. append((char)(((code_point >> 6) & 0x1f) | 0xc0));
  121. append((char)(((code_point >> 0) & 0x3f) | 0x80));
  122. } else if (code_point <= 0xffff) {
  123. append((char)(((code_point >> 12) & 0x0f) | 0xe0));
  124. append((char)(((code_point >> 6) & 0x3f) | 0x80));
  125. append((char)(((code_point >> 0) & 0x3f) | 0x80));
  126. } else if (code_point <= 0x10ffff) {
  127. append((char)(((code_point >> 18) & 0x07) | 0xf0));
  128. append((char)(((code_point >> 12) & 0x3f) | 0x80));
  129. append((char)(((code_point >> 6) & 0x3f) | 0x80));
  130. append((char)(((code_point >> 0) & 0x3f) | 0x80));
  131. } else {
  132. append(0xef);
  133. append(0xbf);
  134. append(0xbd);
  135. }
  136. }
  137. void StringBuilder::append(const Utf32View& utf32_view)
  138. {
  139. for (size_t i = 0; i < utf32_view.length(); ++i) {
  140. auto code_point = utf32_view.code_points()[i];
  141. append_code_point(code_point);
  142. }
  143. }
  144. void StringBuilder::append_escaped_for_json(const StringView& string)
  145. {
  146. for (auto ch : string) {
  147. switch (ch) {
  148. case '\e':
  149. append("\\u001B");
  150. break;
  151. case '\b':
  152. append("\\b");
  153. break;
  154. case '\n':
  155. append("\\n");
  156. break;
  157. case '\t':
  158. append("\\t");
  159. break;
  160. case '\"':
  161. append("\\\"");
  162. break;
  163. case '\\':
  164. append("\\\\");
  165. break;
  166. default:
  167. append(ch);
  168. }
  169. }
  170. }
  171. }