StringBuilder.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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/Memory.h>
  28. #include <AK/PrintfImplementation.h>
  29. #include <AK/StdLibExtras.h>
  30. #include <AK/String.h>
  31. #include <AK/StringBuilder.h>
  32. #include <AK/StringView.h>
  33. #include <AK/Utf32View.h>
  34. namespace AK {
  35. inline void StringBuilder::will_append(size_t size)
  36. {
  37. if ((m_length + size) > m_buffer.size())
  38. m_buffer.grow(max(static_cast<size_t>(16), m_buffer.size() * 2 + size));
  39. }
  40. StringBuilder::StringBuilder(size_t initial_capacity)
  41. {
  42. m_buffer.grow((int)initial_capacity);
  43. }
  44. void StringBuilder::append(const StringView& str)
  45. {
  46. if (str.is_empty())
  47. return;
  48. will_append(str.length());
  49. memcpy(m_buffer.data() + m_length, str.characters_without_null_termination(), str.length());
  50. m_length += str.length();
  51. }
  52. void StringBuilder::append(const char* characters, size_t length)
  53. {
  54. append(StringView { characters, length });
  55. }
  56. void StringBuilder::append(char ch)
  57. {
  58. will_append(1);
  59. m_buffer.data()[m_length] = ch;
  60. m_length += 1;
  61. }
  62. void StringBuilder::appendvf(const char* fmt, va_list ap)
  63. {
  64. printf_internal([this](char*&, char ch) {
  65. append(ch);
  66. },
  67. nullptr, fmt, ap);
  68. }
  69. void StringBuilder::appendf(const char* fmt, ...)
  70. {
  71. va_list ap;
  72. va_start(ap, fmt);
  73. will_append(strlen(fmt));
  74. appendvf(fmt, ap);
  75. va_end(ap);
  76. }
  77. ByteBuffer StringBuilder::to_byte_buffer() const
  78. {
  79. ByteBuffer buffer_copy = m_buffer.isolated_copy();
  80. buffer_copy.trim(m_length);
  81. return buffer_copy;
  82. }
  83. String StringBuilder::to_string() const
  84. {
  85. if (is_empty())
  86. return String::empty();
  87. return String((const char*)m_buffer.data(), m_length);
  88. }
  89. String StringBuilder::build() const
  90. {
  91. return to_string();
  92. }
  93. StringView StringBuilder::string_view() const
  94. {
  95. return StringView { (const char*)m_buffer.data(), m_length };
  96. }
  97. void StringBuilder::clear()
  98. {
  99. m_buffer.clear();
  100. m_length = 0;
  101. }
  102. void StringBuilder::append_code_point(u32 code_point)
  103. {
  104. if (code_point <= 0x7f) {
  105. append((char)code_point);
  106. } else if (code_point <= 0x07ff) {
  107. append((char)(((code_point >> 6) & 0x1f) | 0xc0));
  108. append((char)(((code_point >> 0) & 0x3f) | 0x80));
  109. } else if (code_point <= 0xffff) {
  110. append((char)(((code_point >> 12) & 0x0f) | 0xe0));
  111. append((char)(((code_point >> 6) & 0x3f) | 0x80));
  112. append((char)(((code_point >> 0) & 0x3f) | 0x80));
  113. } else if (code_point <= 0x10ffff) {
  114. append((char)(((code_point >> 18) & 0x07) | 0xf0));
  115. append((char)(((code_point >> 12) & 0x3f) | 0x80));
  116. append((char)(((code_point >> 6) & 0x3f) | 0x80));
  117. append((char)(((code_point >> 0) & 0x3f) | 0x80));
  118. } else {
  119. append(0xef);
  120. append(0xbf);
  121. append(0xbd);
  122. }
  123. }
  124. void StringBuilder::append(const Utf32View& utf32_view)
  125. {
  126. for (size_t i = 0; i < utf32_view.length(); ++i) {
  127. auto code_point = utf32_view.code_points()[i];
  128. append_code_point(code_point);
  129. }
  130. }
  131. void StringBuilder::append_escaped_for_json(const StringView& string)
  132. {
  133. for (auto ch : string) {
  134. switch (ch) {
  135. case '\e':
  136. append("\\u001B");
  137. break;
  138. case '\b':
  139. append("\\b");
  140. break;
  141. case '\n':
  142. append("\\n");
  143. break;
  144. case '\t':
  145. append("\\t");
  146. break;
  147. case '\"':
  148. append("\\\"");
  149. break;
  150. case '\\':
  151. append("\\\\");
  152. break;
  153. default:
  154. append(ch);
  155. }
  156. }
  157. }
  158. }