StringBuilder.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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/PrintfImplementation.h>
  27. #include <AK/StdLibExtras.h>
  28. #include <AK/StringBuilder.h>
  29. #include <AK/String.h>
  30. namespace AK {
  31. inline void StringBuilder::will_append(size_t size)
  32. {
  33. if ((m_length + size) > (size_t)m_buffer.size())
  34. m_buffer.grow(max((size_t)16, (size_t)m_buffer.size() * 2 + size));
  35. }
  36. StringBuilder::StringBuilder(size_t initial_capacity)
  37. {
  38. m_buffer.grow((int)initial_capacity);
  39. }
  40. void StringBuilder::append(const StringView& str)
  41. {
  42. if (str.is_empty())
  43. return;
  44. will_append(str.length());
  45. memcpy(m_buffer.data() + m_length, str.characters_without_null_termination(), str.length());
  46. m_length += str.length();
  47. }
  48. void StringBuilder::append(const char* characters, size_t length)
  49. {
  50. if (!length)
  51. return;
  52. will_append(length);
  53. memcpy(m_buffer.data() + m_length, characters, length);
  54. m_length += 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()
  78. {
  79. m_buffer.trim(m_length);
  80. m_length = 0;
  81. return move(m_buffer);
  82. }
  83. String StringBuilder::to_string()
  84. {
  85. return String((const char*)m_buffer.data(), m_length);
  86. }
  87. String StringBuilder::build()
  88. {
  89. return to_string();
  90. }
  91. StringView StringBuilder::string_view() const
  92. {
  93. return StringView { (const char*)m_buffer.data(), m_length };
  94. }
  95. void StringBuilder::clear()
  96. {
  97. m_buffer.clear();
  98. m_length = 0;
  99. }
  100. }