KBufferBuilder.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 <Kernel/KBufferBuilder.h>
  29. #include <stdarg.h>
  30. namespace Kernel {
  31. inline bool KBufferBuilder::can_append(size_t size) const
  32. {
  33. if (!m_buffer)
  34. return false;
  35. bool has_space = ((m_size + size) < m_buffer->size());
  36. ASSERT(has_space);
  37. return has_space;
  38. }
  39. OwnPtr<KBuffer> KBufferBuilder::build()
  40. {
  41. m_buffer->set_size(m_size);
  42. return m_buffer.release_nonnull();
  43. }
  44. KBufferBuilder::KBufferBuilder()
  45. : m_buffer(KBuffer::try_create_with_size(4 * MiB, Region::Access::Read | Region::Access::Write))
  46. {
  47. }
  48. void KBufferBuilder::append(const StringView& str)
  49. {
  50. if (str.is_empty())
  51. return;
  52. if (!can_append(str.length()))
  53. return;
  54. memcpy(insertion_ptr(), str.characters_without_null_termination(), str.length());
  55. m_size += str.length();
  56. }
  57. void KBufferBuilder::append(const char* characters, int length)
  58. {
  59. if (!length)
  60. return;
  61. if (!can_append(length))
  62. return;
  63. memcpy(insertion_ptr(), characters, length);
  64. m_size += length;
  65. }
  66. void KBufferBuilder::append(char ch)
  67. {
  68. if (!can_append(1))
  69. return;
  70. insertion_ptr()[0] = ch;
  71. m_size += 1;
  72. }
  73. void KBufferBuilder::appendvf(const char* fmt, va_list ap)
  74. {
  75. printf_internal([this](char*&, char ch) {
  76. append(ch);
  77. },
  78. nullptr, fmt, ap);
  79. }
  80. void KBufferBuilder::appendf(const char* fmt, ...)
  81. {
  82. va_list ap;
  83. va_start(ap, fmt);
  84. appendvf(fmt, ap);
  85. va_end(ap);
  86. }
  87. void KBufferBuilder::append_escaped_for_json(const StringView& string)
  88. {
  89. for (auto ch : string) {
  90. switch (ch) {
  91. case '\e':
  92. append("\\u001B");
  93. break;
  94. case '\b':
  95. append("\\b");
  96. break;
  97. case '\n':
  98. append("\\n");
  99. break;
  100. case '\t':
  101. append("\\t");
  102. break;
  103. case '\"':
  104. append("\\\"");
  105. break;
  106. case '\\':
  107. append("\\\\");
  108. break;
  109. default:
  110. append(ch);
  111. }
  112. }
  113. }
  114. }