liuxiaolong
2021-07-20 58d904a328c0d849769b483e901a0be9426b8209
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
//
// Copyright (c) 2020 Krystian Stasiowski (sdkrystian@gmail.com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// Official repository: https://github.com/boostorg/json
//
 
#ifndef BOOST_JSON_DETAIL_UTF8_HPP
#define BOOST_JSON_DETAIL_UTF8_HPP
 
#include <cstddef>
#include <cstring>
#include <cstdint>
 
BOOST_JSON_NS_BEGIN
namespace detail {
 
template<int N>
std::uint32_t
load_little_endian(void const* p)
{
    // VFALCO do we need to initialize this to 0?
    std::uint32_t v;
    std::memcpy(&v, p, N);
#ifdef BOOST_JSON_BIG_ENDIAN
    v = ((v & 0xFF000000) >> 24) |
        ((v & 0x00FF0000) >>  8) |
        ((v & 0x0000FF00) <<  8) |
        ((v & 0x000000FF) << 24);
#endif
    return v;
}
 
inline
uint16_t
classify_utf8(char c)
{
    // 0x000 = invalid
    // 0x102 = 2 bytes, second byte [80, BF]
    // 0x203 = 3 bytes, second byte [A0, BF]
    // 0x303 = 3 bytes, second byte [80, BF]
    // 0x403 = 3 bytes, second byte [80, 9F]
    // 0x504 = 4 bytes, second byte [90, BF]
    // 0x604 = 4 bytes, second byte [80, BF]
    // 0x704 = 4 bytes, second byte [80, 8F]
    static constexpr uint16_t first[128]
    {
       0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000,  
       0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 
       0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 
       0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 
       0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000,  
       0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 
       0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000,  
       0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 
                                       
       0x000, 0x000, 0x102, 0x102, 0x102, 0x102, 0x102, 0x102, 
       0x102, 0x102, 0x102, 0x102, 0x102, 0x102, 0x102, 0x102, 
       0x102, 0x102, 0x102, 0x102, 0x102, 0x102, 0x102, 0x102, 
       0x102, 0x102, 0x102, 0x102, 0x102, 0x102, 0x102, 0x102, 
       0x203, 0x303, 0x303, 0x303, 0x303, 0x303, 0x303, 0x303, 
       0x303, 0x303, 0x303, 0x303, 0x303, 0x403, 0x303, 0x303, 
       0x504, 0x604, 0x604, 0x604, 0x704, 0x000, 0x000, 0x000, 
       0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 
    };
    return first[static_cast<unsigned char>(c)];
}
 
inline
bool
is_valid_utf8(const char* p, uint16_t first)
{
    uint32_t v;
    switch(first >> 8)
    {
    default:
        return false;
 
    // 2 bytes, second byte [80, BF]
    case 1:
        v = load_little_endian<2>(p);
        return (v & 0xC000) == 0x8000;
 
    // 3 bytes, second byte [A0, BF]
    case 2:
        v = load_little_endian<3>(p);
        std::memcpy(&v, p, 3);
        return (v & 0xC0E000) == 0x80A000;
 
    // 3 bytes, second byte [80, BF]
    case 3:
        v = load_little_endian<3>(p);
        return (v & 0xC0C000) == 0x808000;
 
    // 3 bytes, second byte [80, 9F]
    case 4:
        v = load_little_endian<3>(p);
        return (v & 0xC0E000) == 0x808000;
 
    // 4 bytes, second byte [90, BF]
    case 5:
        v = load_little_endian<4>(p);
        return (v & 0xC0C0FF00) + 0x7F7F7000 <= 0x2F00;
 
    // 4 bytes, second byte [80, BF]
    case 6:
        v = load_little_endian<4>(p);
        return (v & 0xC0C0C000) == 0x80808000;
 
    // 4 bytes, second byte [80, 8F]
    case 7:
        v = load_little_endian<4>(p);
        return (v & 0xC0C0F000) == 0x80808000;
    }
}
 
class utf8_sequence
{
    char seq_[4];
    uint16_t first_;
    uint8_t size_;
 
public:
    void
    save(
        const char* p,
        std::size_t remain) noexcept
    {
        first_ = classify_utf8(*p & 0x7F);
        if(remain >= length())
            size_ = length();
        else
            size_ = static_cast<uint8_t>(remain);
        std::memcpy(seq_, p, size_);
    }
 
    uint8_t 
    length() const noexcept
    {
        return first_ & 0xFF;
    }
 
    bool
    complete() const noexcept
    {
        return size_ >= length();
    }
 
    // returns true if complete
    bool
    append(
        const char* p, 
        std::size_t remain) noexcept
    {
        if(BOOST_JSON_UNLIKELY(needed() == 0))
            return true;
        if(BOOST_JSON_LIKELY(remain >= needed()))
        {
            std::memcpy(
                seq_ + size_, p, needed());
            size_ = length();
            return true;
        }
        if(BOOST_JSON_LIKELY(remain > 0))
        {
            std::memcpy(seq_ + size_, p, remain);
            size_ += static_cast<uint8_t>(remain);
        }
        return false;
    }
 
    const char*
    data() const noexcept
    {
        return seq_;
    }
 
    uint8_t
    needed() const noexcept
    {
        return length() - size_;
    }
 
    bool
    valid() const noexcept
    {
        BOOST_ASSERT(size_ >= length());
        return is_valid_utf8(seq_, first_);
    }
};
 
} // detail
BOOST_JSON_NS_END
 
#endif