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