]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/json/detail/buffer.hpp
import quincy beta 17.1.0
[ceph.git] / ceph / src / boost / boost / json / detail / buffer.hpp
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
10 #ifndef BOOST_JSON_DETAIL_BUFFER_HPP
11 #define BOOST_JSON_DETAIL_BUFFER_HPP
12
13 #include <boost/json/detail/config.hpp>
14 #include <boost/json/string_view.hpp>
15 #include <cstring>
16
17 BOOST_JSON_NS_BEGIN
18 namespace detail {
19
20 // A simple string-like temporary static buffer
21 template<std::size_t N>
22 class buffer
23 {
24 public:
25 using size_type = std::size_t;
26
27 buffer() = default;
28
29 bool
30 empty() const noexcept
31 {
32 return size_ == 0;
33 }
34
35 string_view
36 get() const noexcept
37 {
38 return {buf_, size_};
39 }
40
41 operator string_view() const noexcept
42 {
43 return get();
44 }
45
46 char const*
47 data() const noexcept
48 {
49 return buf_;
50 }
51
52 size_type
53 size() const noexcept
54 {
55 return size_;
56 }
57
58 size_type
59 capacity() const noexcept
60 {
61 return N - size_;
62 }
63
64 size_type
65 max_size() const noexcept
66 {
67 return N;
68 }
69
70 void
71 clear() noexcept
72 {
73 size_ = 0;
74 }
75
76 void
77 push_back(char ch) noexcept
78 {
79 BOOST_ASSERT(capacity() > 0);
80 buf_[size_++] = ch;
81 }
82
83 // append an unescaped string
84 void
85 append(
86 char const* s,
87 size_type n)
88 {
89 BOOST_ASSERT(n <= N - size_);
90 std::memcpy(buf_ + size_, s, n);
91 size_ += n;
92 }
93
94 // append valid 32-bit code point as utf8
95 void
96 append_utf8(
97 unsigned long cp) noexcept
98 {
99 auto dest = buf_ + size_;
100 if(cp < 0x80)
101 {
102 BOOST_ASSERT(size_ <= N - 1);
103 dest[0] = static_cast<char>(cp);
104 size_ += 1;
105 return;
106 }
107
108 if(cp < 0x800)
109 {
110 BOOST_ASSERT(size_ <= N - 2);
111 dest[0] = static_cast<char>( (cp >> 6) | 0xc0);
112 dest[1] = static_cast<char>( (cp & 0x3f) | 0x80);
113 size_ += 2;
114 return;
115 }
116
117 if(cp < 0x10000)
118 {
119 BOOST_ASSERT(size_ <= N - 3);
120 dest[0] = static_cast<char>( (cp >> 12) | 0xe0);
121 dest[1] = static_cast<char>(((cp >> 6) & 0x3f) | 0x80);
122 dest[2] = static_cast<char>( (cp & 0x3f) | 0x80);
123 size_ += 3;
124 return;
125 }
126
127 {
128 BOOST_ASSERT(size_ <= N - 4);
129 dest[0] = static_cast<char>( (cp >> 18) | 0xf0);
130 dest[1] = static_cast<char>(((cp >> 12) & 0x3f) | 0x80);
131 dest[2] = static_cast<char>(((cp >> 6) & 0x3f) | 0x80);
132 dest[3] = static_cast<char>( (cp & 0x3f) | 0x80);
133 size_ += 4;
134 }
135 }
136 private:
137 char buf_[N];
138 size_type size_ = 0;
139 };
140
141 } // detail
142 BOOST_JSON_NS_END
143
144 #endif