Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:38:58

0001 //
0002 // Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
0003 //
0004 // Distributed under the Boost Software License, Version 1.0. (See accompanying
0005 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0006 //
0007 // Official repository: https://github.com/boostorg/json
0008 //
0009 
0010 #ifndef BOOST_JSON_DETAIL_BUFFER_HPP
0011 #define BOOST_JSON_DETAIL_BUFFER_HPP
0012 
0013 #include <boost/json/detail/config.hpp>
0014 #include <boost/json/string_view.hpp>
0015 #include <cstring>
0016 
0017 namespace boost {
0018 namespace json {
0019 namespace detail {
0020 
0021 // A simple string-like temporary static buffer
0022 template<std::size_t N>
0023 class buffer
0024 {
0025 public:
0026     using size_type = std::size_t;
0027 
0028     buffer() = default;
0029 
0030     bool
0031     empty() const noexcept
0032     {
0033         return size_ == 0;
0034     }
0035 
0036     string_view
0037     get() const noexcept
0038     {
0039         return {buf_, size_};
0040     }
0041 
0042     operator string_view() const noexcept
0043     {
0044         return get();
0045     }
0046 
0047     char const*
0048     data() const noexcept
0049     {
0050         return buf_;
0051     }
0052 
0053     size_type
0054     size() const noexcept
0055     {
0056         return size_;
0057     }
0058 
0059     size_type
0060     capacity() const noexcept
0061     {
0062         return N - size_;
0063     }
0064 
0065     size_type
0066     max_size() const noexcept
0067     {
0068         return N;
0069     }
0070 
0071     void
0072     clear() noexcept
0073     {
0074         size_ = 0;
0075     }
0076 
0077     void
0078     push_back(char ch) noexcept
0079     {
0080         BOOST_ASSERT(capacity() > 0);
0081         buf_[size_++] = ch;
0082     }
0083 
0084     // append an unescaped string
0085     void
0086     append(
0087         char const* s,
0088         size_type n)
0089     {
0090         BOOST_ASSERT(n <= N - size_);
0091         std::memcpy(buf_ + size_, s, n);
0092         size_ += n;
0093     }
0094 
0095     // append valid 32-bit code point as utf8
0096     void
0097     append_utf8(
0098         unsigned long cp) noexcept
0099     {
0100         auto dest = buf_ + size_;
0101         if(cp < 0x80)
0102         {
0103             BOOST_ASSERT(size_ <= N - 1);
0104             dest[0] = static_cast<char>(cp);
0105             size_ += 1;
0106             return;
0107         }
0108 
0109         if(cp < 0x800)
0110         {
0111             BOOST_ASSERT(size_ <= N - 2);
0112             dest[0] = static_cast<char>( (cp >> 6)          | 0xc0);
0113             dest[1] = static_cast<char>( (cp & 0x3f)        | 0x80);
0114             size_ += 2;
0115             return;
0116         }
0117 
0118         if(cp < 0x10000)
0119         {
0120             BOOST_ASSERT(size_ <= N - 3);
0121             dest[0] = static_cast<char>( (cp >> 12)         | 0xe0);
0122             dest[1] = static_cast<char>(((cp >> 6) & 0x3f)  | 0x80);
0123             dest[2] = static_cast<char>( (cp       & 0x3f)  | 0x80);
0124             size_ += 3;
0125             return;
0126         }
0127 
0128         {
0129             BOOST_ASSERT(size_ <= N - 4);
0130             dest[0] = static_cast<char>( (cp >> 18)         | 0xf0);
0131             dest[1] = static_cast<char>(((cp >> 12) & 0x3f) | 0x80);
0132             dest[2] = static_cast<char>(((cp >> 6)  & 0x3f) | 0x80);
0133             dest[3] = static_cast<char>( (cp        & 0x3f) | 0x80);
0134             size_ += 4;
0135         }
0136     }
0137 private:
0138     char buf_[N];
0139     size_type size_ = 0;
0140 };
0141 
0142 } // detail
0143 } // namespace json
0144 } // namespace boost
0145 
0146 #endif