Coverage Report

Created: 2023-03-26 06:57

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