buffer.hpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. //
  2. // Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // Official repository: https://github.com/boostorg/json
  8. //
  9. #ifndef BOOST_JSON_DETAIL_BUFFER_HPP
  10. #define BOOST_JSON_DETAIL_BUFFER_HPP
  11. #include <boost/json/detail/config.hpp>
  12. #include <boost/json/string_view.hpp>
  13. #include <cstring>
  14. BOOST_JSON_NS_BEGIN
  15. namespace detail {
  16. // A simple string-like temporary static buffer
  17. template<std::size_t N>
  18. class buffer
  19. {
  20. public:
  21. using size_type = std::size_t;
  22. buffer() = default;
  23. bool
  24. empty() const noexcept
  25. {
  26. return size_ == 0;
  27. }
  28. string_view
  29. get() const noexcept
  30. {
  31. return {buf_, size_};
  32. }
  33. operator string_view() const noexcept
  34. {
  35. return get();
  36. }
  37. char const*
  38. data() const noexcept
  39. {
  40. return buf_;
  41. }
  42. size_type
  43. size() const noexcept
  44. {
  45. return size_;
  46. }
  47. size_type
  48. capacity() const noexcept
  49. {
  50. return N - size_;
  51. }
  52. size_type
  53. max_size() const noexcept
  54. {
  55. return N;
  56. }
  57. void
  58. clear() noexcept
  59. {
  60. size_ = 0;
  61. }
  62. void
  63. push_back(char ch) noexcept
  64. {
  65. BOOST_ASSERT(capacity() > 0);
  66. buf_[size_++] = ch;
  67. }
  68. // append an unescaped string
  69. void
  70. append(
  71. char const* s,
  72. size_type n)
  73. {
  74. BOOST_ASSERT(n <= N - size_);
  75. std::memcpy(buf_ + size_, s, n);
  76. size_ += n;
  77. }
  78. // append valid 32-bit code point as utf8
  79. void
  80. append_utf8(
  81. unsigned long cp) noexcept
  82. {
  83. auto dest = buf_ + size_;
  84. if(cp < 0x80)
  85. {
  86. BOOST_ASSERT(size_ <= N - 1);
  87. dest[0] = static_cast<char>(cp);
  88. size_ += 1;
  89. return;
  90. }
  91. if(cp < 0x800)
  92. {
  93. BOOST_ASSERT(size_ <= N - 2);
  94. dest[0] = static_cast<char>( (cp >> 6) | 0xc0);
  95. dest[1] = static_cast<char>( (cp & 0x3f) | 0x80);
  96. size_ += 2;
  97. return;
  98. }
  99. if(cp < 0x10000)
  100. {
  101. BOOST_ASSERT(size_ <= N - 3);
  102. dest[0] = static_cast<char>( (cp >> 12) | 0xe0);
  103. dest[1] = static_cast<char>(((cp >> 6) & 0x3f) | 0x80);
  104. dest[2] = static_cast<char>( (cp & 0x3f) | 0x80);
  105. size_ += 3;
  106. return;
  107. }
  108. {
  109. BOOST_ASSERT(size_ <= N - 4);
  110. dest[0] = static_cast<char>( (cp >> 18) | 0xf0);
  111. dest[1] = static_cast<char>(((cp >> 12) & 0x3f) | 0x80);
  112. dest[2] = static_cast<char>(((cp >> 6) & 0x3f) | 0x80);
  113. dest[3] = static_cast<char>( (cp & 0x3f) | 0x80);
  114. size_ += 4;
  115. }
  116. }
  117. private:
  118. char buf_[N];
  119. size_type size_ = 0;
  120. };
  121. } // detail
  122. BOOST_JSON_NS_END
  123. #endif