Coverage Report

Created: 2025-06-13 06:26

/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
79.0k
    buffer() = default;
29
30
    bool
31
    empty() const noexcept
32
2.60k
    {
33
2.60k
        return size_ == 0;
34
2.60k
    }
35
36
    string_view
37
    get() const noexcept
38
80.3k
    {
39
80.3k
        return {buf_, size_};
40
80.3k
    }
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
238k
    {
56
238k
        return size_;
57
238k
    }
58
59
    size_type
60
    capacity() const noexcept
61
    {
62
        return N - size_;
63
    }
64
65
    size_type
66
    max_size() const noexcept
67
81.3k
    {
68
81.3k
        return N;
69
81.3k
    }
70
71
    void
72
    clear() noexcept
73
2.11k
    {
74
2.11k
        size_ = 0;
75
2.11k
    }
76
77
    void
78
    push_back(char ch) noexcept
79
4.91M
    {
80
4.91M
        BOOST_ASSERT(capacity() > 0);
81
4.91M
        buf_[size_++] = ch;
82
4.91M
    }
83
84
    // append an unescaped string
85
    void
86
    append(
87
        char const* s,
88
        size_type n)
89
    {
90
        BOOST_ASSERT(n <= N - size_);
91
        std::memcpy(buf_ + size_, s, n);
92
        size_ += n;
93
    }
94
95
    // append valid 32-bit code point as utf8
96
    void
97
    append_utf8(
98
        unsigned long cp) noexcept
99
279k
    {
100
279k
        auto dest = buf_ + size_;
101
279k
        if(cp < 0x80)
102
244k
        {
103
244k
            BOOST_ASSERT(size_ <= N - 1);
104
244k
            dest[0] = static_cast<char>(cp);
105
244k
            size_ += 1;
106
244k
            return;
107
244k
        }
108
109
34.3k
        if(cp < 0x800)
110
17.2k
        {
111
17.2k
            BOOST_ASSERT(size_ <= N - 2);
112
17.2k
            dest[0] = static_cast<char>( (cp >> 6)          | 0xc0);
113
17.2k
            dest[1] = static_cast<char>( (cp & 0x3f)        | 0x80);
114
17.2k
            size_ += 2;
115
17.2k
            return;
116
17.2k
        }
117
118
17.1k
        if(cp < 0x10000)
119
4.07k
        {
120
4.07k
            BOOST_ASSERT(size_ <= N - 3);
121
4.07k
            dest[0] = static_cast<char>( (cp >> 12)         | 0xe0);
122
4.07k
            dest[1] = static_cast<char>(((cp >> 6) & 0x3f)  | 0x80);
123
4.07k
            dest[2] = static_cast<char>( (cp       & 0x3f)  | 0x80);
124
4.07k
            size_ += 3;
125
4.07k
            return;
126
4.07k
        }
127
128
13.0k
        {
129
13.0k
            BOOST_ASSERT(size_ <= N - 4);
130
13.0k
            dest[0] = static_cast<char>( (cp >> 18)         | 0xf0);
131
13.0k
            dest[1] = static_cast<char>(((cp >> 12) & 0x3f) | 0x80);
132
13.0k
            dest[2] = static_cast<char>(((cp >> 6)  & 0x3f) | 0x80);
133
13.0k
            dest[3] = static_cast<char>( (cp        & 0x3f) | 0x80);
134
13.0k
            size_ += 4;
135
13.0k
        }
136
13.0k
    }
137
private:
138
    char buf_[N];
139
    size_type size_ = 0;
140
};
141
142
} // detail
143
} // namespace json
144
} // namespace boost
145
146
#endif