100.00% Lines (101/101) 100.00% Functions (34/34)
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_STREAM_HPP 10   #ifndef BOOST_JSON_DETAIL_STREAM_HPP
11   #define BOOST_JSON_DETAIL_STREAM_HPP 11   #define BOOST_JSON_DETAIL_STREAM_HPP
12   12  
13   namespace boost { 13   namespace boost {
14   namespace json { 14   namespace json {
15   namespace detail { 15   namespace detail {
16   16  
17   class const_stream 17   class const_stream
18   { 18   {
19   friend class local_const_stream; 19   friend class local_const_stream;
20   20  
21   char const* p_; 21   char const* p_;
22   char const* end_; 22   char const* end_;
23   23  
24   public: 24   public:
25   const_stream() = default; 25   const_stream() = default;
26   26  
HITCBC 27   35719 const_stream( 27   35719 const_stream(
28   char const* data, 28   char const* data,
29   std::size_t size) noexcept 29   std::size_t size) noexcept
HITCBC 30   35719 : p_(data) 30   35719 : p_(data)
HITCBC 31   35719 , end_(data + size) 31   35719 , end_(data + size)
32   { 32   {
HITCBC 33   35719 } 33   35719 }
34   34  
35   size_t 35   size_t
36   used(char const* begin) const noexcept 36   used(char const* begin) const noexcept
37   { 37   {
38   return static_cast< 38   return static_cast<
39   size_t>(p_ - begin); 39   size_t>(p_ - begin);
40   } 40   }
41   41  
42   size_t 42   size_t
HITCBC 43   64866 remain() const noexcept 43   64866 remain() const noexcept
44   { 44   {
HITCBC 45   64866 return end_ - p_; 45   64866 return end_ - p_;
46   } 46   }
47   47  
48   char const* 48   char const*
HITCBC 49   62244 data() const noexcept 49   62244 data() const noexcept
50   { 50   {
HITCBC 51   62244 return p_; 51   62244 return p_;
52   } 52   }
53   53  
HITCBC 54   31451934 operator bool() const noexcept 54   31451934 operator bool() const noexcept
55   { 55   {
HITCBC 56   31451934 return p_ < end_; 56   31451934 return p_ < end_;
57   } 57   }
58   58  
59   // unchecked 59   // unchecked
60   char 60   char
HITCBC 61   31417505 operator*() const noexcept 61   31417505 operator*() const noexcept
62   { 62   {
HITCBC 63   31417505 BOOST_ASSERT(p_ < end_); 63   31417505 BOOST_ASSERT(p_ < end_);
HITCBC 64   31417505 return *p_; 64   31417505 return *p_;
65   } 65   }
66   66  
67   // unchecked 67   // unchecked
68   const_stream& 68   const_stream&
HITCBC 69   31417505 operator++() noexcept 69   31417505 operator++() noexcept
70   { 70   {
HITCBC 71   31417505 BOOST_ASSERT(p_ < end_); 71   31417505 BOOST_ASSERT(p_ < end_);
HITCBC 72   31417505 ++p_; 72   31417505 ++p_;
HITCBC 73   31417505 return *this; 73   31417505 return *this;
74   } 74   }
75   75  
76   void 76   void
HITCBC 77   26559 skip(std::size_t n) noexcept 77   26559 skip(std::size_t n) noexcept
78   { 78   {
HITCBC 79   26559 BOOST_ASSERT(n <= remain()); 79   26559 BOOST_ASSERT(n <= remain());
HITCBC 80   26559 p_ += n; 80   26559 p_ += n;
HITCBC 81   26559 } 81   26559 }
82   82  
83   void 83   void
84   skip_to(const char* p) noexcept 84   skip_to(const char* p) noexcept
85   { 85   {
86   BOOST_ASSERT(p <= end_ && p >= p_); 86   BOOST_ASSERT(p <= end_ && p >= p_);
87   p_ = p; 87   p_ = p;
88   } 88   }
89   }; 89   };
90   90  
91   class local_const_stream 91   class local_const_stream
92   : public const_stream 92   : public const_stream
93   { 93   {
94   const_stream& src_; 94   const_stream& src_;
95   95  
96   public: 96   public:
97   explicit 97   explicit
HITCBC 98   44285 local_const_stream( 98   44285 local_const_stream(
99   const_stream& src) noexcept 99   const_stream& src) noexcept
HITCBC 100   44285 : const_stream(src) 100   44285 : const_stream(src)
HITCBC 101   44285 , src_(src) 101   44285 , src_(src)
102   { 102   {
HITCBC 103   44285 } 103   44285 }
104   104  
HITCBC 105   44285 ~local_const_stream() 105   44285 ~local_const_stream()
106   { 106   {
HITCBC 107   44285 src_.p_ = p_; 107   44285 src_.p_ = p_;
HITCBC 108   44285 } 108   44285 }
109   109  
110   void 110   void
111   clip(std::size_t n) noexcept 111   clip(std::size_t n) noexcept
112   { 112   {
113   if(static_cast<std::size_t>( 113   if(static_cast<std::size_t>(
114   src_.end_ - p_) > n) 114   src_.end_ - p_) > n)
115   end_ = p_ + n; 115   end_ = p_ + n;
116   else 116   else
117   end_ = src_.end_; 117   end_ = src_.end_;
118   } 118   }
119   }; 119   };
120   120  
121   class const_stream_wrapper 121   class const_stream_wrapper
122   { 122   {
123   const char*& p_; 123   const char*& p_;
124   const char* const end_; 124   const char* const end_;
125   125  
126   friend class clipped_const_stream; 126   friend class clipped_const_stream;
127   public: 127   public:
HITCBC 128   4794062 const_stream_wrapper( 128   4794062 const_stream_wrapper(
129   const char*& p, 129   const char*& p,
130   const char* end) 130   const char* end)
HITCBC 131   4794062 : p_(p) 131   4794062 : p_(p)
HITCBC 132   4794062 , end_(end) 132   4794062 , end_(end)
133   { 133   {
HITCBC 134   4794062 } 134   4794062 }
135   135  
HITCBC 136   63384959 void operator++() noexcept 136   63384959 void operator++() noexcept
137   { 137   {
HITCBC 138   63384959 ++p_; 138   63384959 ++p_;
HITCBC 139   63384959 } 139   63384959 }
140   140  
HITCBC 141   2046839 void operator+=(std::size_t n) noexcept 141   2046839 void operator+=(std::size_t n) noexcept
142   { 142   {
HITCBC 143   2046839 p_ += n; 143   2046839 p_ += n;
HITCBC 144   2046839 } 144   2046839 }
145   145  
HITCBC 146   7322858 void operator=(const char* p) noexcept 146   7322858 void operator=(const char* p) noexcept
147   { 147   {
HITCBC 148   7322858 p_ = p; 148   7322858 p_ = p;
HITCBC 149   7322858 } 149   7322858 }
150   150  
HITCBC 151   70856167 char operator*() const noexcept 151   70856167 char operator*() const noexcept
152   { 152   {
HITCBC 153   70856167 return *p_; 153   70856167 return *p_;
154   } 154   }
155   155  
HITCBC 156   76831743 operator bool() const noexcept 156   76831743 operator bool() const noexcept
157   { 157   {
HITCBC 158   76831743 return p_ < end_; 158   76831743 return p_ < end_;
159   } 159   }
160   160  
HITCBC 161   20797775 const char* begin() const noexcept 161   20797775 const char* begin() const noexcept
162   { 162   {
HITCBC 163   20797775 return p_; 163   20797775 return p_;
164   } 164   }
165   165  
HITCBC 166   4865457 const char* end() const noexcept 166   4865457 const char* end() const noexcept
167   { 167   {
HITCBC 168   4865457 return end_; 168   4865457 return end_;
169   } 169   }
170   170  
HITCBC 171   2135882 std::size_t remain() const noexcept 171   2135882 std::size_t remain() const noexcept
172   { 172   {
HITCBC 173   2135882 return end_ - p_; 173   2135882 return end_ - p_;
174   } 174   }
175   175  
HITCBC 176   3071 std::size_t remain(const char* p) const noexcept 176   3071 std::size_t remain(const char* p) const noexcept
177   { 177   {
HITCBC 178   3071 return end_ - p; 178   3071 return end_ - p;
179   } 179   }
180   180  
HITCBC 181   2314817 std::size_t used(const char* p) const noexcept 181   2314817 std::size_t used(const char* p) const noexcept
182   { 182   {
HITCBC 183   2314817 return p_ - p; 183   2314817 return p_ - p;
184   } 184   }
185   }; 185   };
186   186  
187   class clipped_const_stream 187   class clipped_const_stream
188   : public const_stream_wrapper 188   : public const_stream_wrapper
189   { 189   {
190   const char* clip_; 190   const char* clip_;
191   191  
192   public: 192   public:
HITCBC 193   13678 clipped_const_stream( 193   13678 clipped_const_stream(
194   const char*& p, 194   const char*& p,
195   const char* end) 195   const char* end)
HITCBC 196   13678 : const_stream_wrapper(p, end) 196   13678 : const_stream_wrapper(p, end)
HITCBC 197   13678 , clip_(end) 197   13678 , clip_(end)
198   { 198   {
HITCBC 199   13678 } 199   13678 }
200   200  
201   void operator=(const char* p) 201   void operator=(const char* p)
202   { 202   {
203   p_ = p; 203   p_ = p;
204   } 204   }
205   205  
206   const char* end() const noexcept 206   const char* end() const noexcept
207   { 207   {
208   return clip_; 208   return clip_;
209   } 209   }
210   210  
HITCBC 211   68643 operator bool() const noexcept 211   68643 operator bool() const noexcept
212   { 212   {
HITCBC 213   68643 return p_ < clip_; 213   68643 return p_ < clip_;
214   } 214   }
215   215  
HITCBC 216   11688 std::size_t remain() const noexcept 216   11688 std::size_t remain() const noexcept
217   { 217   {
HITCBC 218   11688 return clip_ - p_; 218   11688 return clip_ - p_;
219   } 219   }
220   220  
221   std::size_t remain(const char* p) const noexcept 221   std::size_t remain(const char* p) const noexcept
222   { 222   {
223   return clip_ - p; 223   return clip_ - p;
224   } 224   }
225   225  
226   void 226   void
HITCBC 227   15663 clip(std::size_t n) noexcept 227   15663 clip(std::size_t n) noexcept
228   { 228   {
HITCBC 229   15663 if(static_cast<std::size_t>( 229   15663 if(static_cast<std::size_t>(
HITCBC 230   15663 end_ - p_) > n) 230   15663 end_ - p_) > n)
HITCBC 231   37 clip_ = p_ + n; 231   37 clip_ = p_ + n;
232   else 232   else
HITCBC 233   15626 clip_ = end_; 233   15626 clip_ = end_;
HITCBC 234   15663 } 234   15663 }
235   }; 235   };
236   236  
237   //-------------------------------------- 237   //--------------------------------------
238   238  
239   class stream 239   class stream
240   { 240   {
241   friend class local_stream; 241   friend class local_stream;
242   242  
243   char* p_; 243   char* p_;
244   char* end_; 244   char* end_;
245   245  
246   public: 246   public:
HITCBC 247   32982 stream( 247   32982 stream(
248   char* data, 248   char* data,
249   std::size_t size) noexcept 249   std::size_t size) noexcept
HITCBC 250   32982 : p_(data) 250   32982 : p_(data)
HITCBC 251   32982 , end_(data + size) 251   32982 , end_(data + size)
252   { 252   {
HITCBC 253   32982 } 253   32982 }
254   254  
255   size_t 255   size_t
HITCBC 256   32980 used(char* begin) const noexcept 256   32980 used(char* begin) const noexcept
257   { 257   {
258   return static_cast< 258   return static_cast<
HITCBC 259   32980 size_t>(p_ - begin); 259   32980 size_t>(p_ - begin);
260   } 260   }
261   261  
262   size_t 262   size_t
HITCBC 263   90553 remain() const noexcept 263   90553 remain() const noexcept
264   { 264   {
HITCBC 265   90553 return end_ - p_; 265   90553 return end_ - p_;
266   } 266   }
267   267  
268   char* 268   char*
HITCBC 269   2956 data() noexcept 269   2956 data() noexcept
270   { 270   {
HITCBC 271   2956 return p_; 271   2956 return p_;
272   } 272   }
273   273  
HITCBC 274   31623944 operator bool() const noexcept 274   31623944 operator bool() const noexcept
275   { 275   {
HITCBC 276   31623944 return p_ < end_; 276   31623944 return p_ < end_;
277   } 277   }
278   278  
279   // unchecked 279   // unchecked
280   char& 280   char&
281   operator*() noexcept 281   operator*() noexcept
282   { 282   {
283   BOOST_ASSERT(p_ < end_); 283   BOOST_ASSERT(p_ < end_);
284   return *p_; 284   return *p_;
285   } 285   }
286   286  
287   // unchecked 287   // unchecked
288   stream& 288   stream&
289   operator++() noexcept 289   operator++() noexcept
290   { 290   {
291   BOOST_ASSERT(p_ < end_); 291   BOOST_ASSERT(p_ < end_);
292   ++p_; 292   ++p_;
293   return *this; 293   return *this;
294   } 294   }
295   295  
296   // unchecked 296   // unchecked
297   void 297   void
HITCBC 298   32736 append( 298   32736 append(
299   char const* src, 299   char const* src,
300   std::size_t n) noexcept 300   std::size_t n) noexcept
301   { 301   {
HITCBC 302   32736 BOOST_ASSERT(remain() >= n); 302   32736 BOOST_ASSERT(remain() >= n);
HITCBC 303   32736 std::memcpy(p_, src, n); 303   32736 std::memcpy(p_, src, n);
HITCBC 304   32736 p_ += n; 304   32736 p_ += n;
HITCBC 305   32736 } 305   32736 }
306   306  
307   // unchecked 307   // unchecked
308   void 308   void
HITCBC 309   31554204 append(char c) noexcept 309   31554204 append(char c) noexcept
310   { 310   {
HITCBC 311   31554204 BOOST_ASSERT(p_ < end_); 311   31554204 BOOST_ASSERT(p_ < end_);
HITCBC 312   31554204 *p_++ = c; 312   31554204 *p_++ = c;
HITCBC 313   31554204 } 313   31554204 }
314   314  
315   void 315   void
HITCBC 316   2956 advance(std::size_t n) noexcept 316   2956 advance(std::size_t n) noexcept
317   { 317   {
HITCBC 318   2956 BOOST_ASSERT(remain() >= n); 318   2956 BOOST_ASSERT(remain() >= n);
HITCBC 319   2956 p_ += n; 319   2956 p_ += n;
HITCBC 320   2956 } 320   2956 }
321   }; 321   };
322   322  
323   class local_stream 323   class local_stream
324   : public stream 324   : public stream
325   { 325   {
326   stream& src_; 326   stream& src_;
327   327  
328   public: 328   public:
329   explicit 329   explicit
HITCBC 330   84495 local_stream( 330   84495 local_stream(
331   stream& src) 331   stream& src)
HITCBC 332   84495 : stream(src) 332   84495 : stream(src)
HITCBC 333   84495 , src_(src) 333   84495 , src_(src)
334   { 334   {
HITCBC 335   84495 } 335   84495 }
336   336  
HITCBC 337   84495 ~local_stream() 337   84495 ~local_stream()
338   { 338   {
HITCBC 339   84495 src_.p_ = p_; 339   84495 src_.p_ = p_;
HITCBC 340   84495 } 340   84495 }
341   }; 341   };
342   342  
343   } // detail 343   } // detail
344   } // namespace json 344   } // namespace json
345   } // namespace boost 345   } // namespace boost
346   346  
347   #endif 347   #endif