Blame view

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