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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
//
// Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
// 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_IMPL_STRING_IMPL_IPP
#define BOOST_JSON_DETAIL_IMPL_STRING_IMPL_IPP
 
#include <boost/json/detail/string_impl.hpp>
#include <boost/json/detail/except.hpp>
#include <cstring>
#include <functional>
 
BOOST_JSON_NS_BEGIN
namespace detail {
 
inline
bool
ptr_in_range(
    const char* first,
    const char* last,
    const char* ptr) noexcept
{
    return std::less<const char*>()(ptr, last) &&
        std::greater_equal<const char*>()(ptr, first);
}
 
string_impl::
string_impl() noexcept
{
    s_.k = short_string_;
    s_.buf[sbo_chars_] =
        static_cast<char>(
            sbo_chars_);
    s_.buf[0] = 0;
}
 
string_impl::
string_impl(
    std::size_t size,
    storage_ptr const& sp)
{
    if(size <= sbo_chars_)
    {
        s_.k = short_string_;
        s_.buf[sbo_chars_] =
            static_cast<char>(
                sbo_chars_ - size);
        s_.buf[size] = 0;
    }
    else
    {
        s_.k = kind::string;
        auto const n = growth(
            size, sbo_chars_ + 1);
        p_.t = ::new(sp->allocate(
            sizeof(table) +
                n + 1,
            alignof(table))) table{
                static_cast<
                    std::uint32_t>(size),
                static_cast<
                    std::uint32_t>(n)};
        data()[n] = 0;
    }
}
 
// construct a key, unchecked
string_impl::
string_impl(
    key_t,
    string_view s,
    storage_ptr const& sp)
{
    BOOST_ASSERT(
        s.size() <= max_size());
    k_.k = key_string_;
    k_.n = static_cast<
        std::uint32_t>(s.size());
    k_.s = reinterpret_cast<char*>(
        sp->allocate(s.size() + 1,
            alignof(char)));
    k_.s[s.size()] = 0; // null term
    std::memcpy(&k_.s[0],
        s.data(), s.size());
}
 
// construct a key, unchecked
string_impl::
string_impl(
    key_t,
    string_view s1,
    string_view s2,
    storage_ptr const& sp)
{
    auto len = s1.size() + s2.size();
    BOOST_ASSERT(len <= max_size());
    k_.k = key_string_;
    k_.n = static_cast<
        std::uint32_t>(len);
    k_.s = reinterpret_cast<char*>(
        sp->allocate(len + 1,
            alignof(char)));
    k_.s[len] = 0; // null term
    std::memcpy(&k_.s[0],
        s1.data(), s1.size());
    std::memcpy(&k_.s[s1.size()],
        s2.data(), s2.size());
}
 
std::uint32_t
string_impl::
growth(
    std::size_t new_size,
    std::size_t capacity)
{
    if(new_size > max_size())
        detail::throw_length_error(
            "string too large",
            BOOST_CURRENT_LOCATION);
    // growth factor 2
    if( capacity >
        max_size() - capacity)
        return static_cast<
            std::uint32_t>(max_size()); // overflow
    return static_cast<std::uint32_t>(
        (std::max)(capacity * 2, new_size));
}
 
char*
string_impl::
assign(
    std::size_t new_size,
    storage_ptr const& sp)
{
    if(new_size > capacity())
    {
        string_impl tmp(growth(
            new_size,
            capacity()), sp);
        destroy(sp);
        *this = tmp;
    }
    term(new_size);
    return data();
}
 
char*
string_impl::
append(
    std::size_t n,
    storage_ptr const& sp)
{
    if(n > max_size() - size())
        detail::throw_length_error(
            "string too large",
            BOOST_CURRENT_LOCATION);
    if(n <= capacity() - size())
    {
        term(size() + n);
        return end() - n;
    }
    string_impl tmp(growth(
        size() + n, capacity()), sp);
    std::memcpy(
        tmp.data(), data(), size());
    tmp.term(size() + n);
    destroy(sp);
    *this = tmp;
    return end() - n;
}
 
void
string_impl::
insert(
    std::size_t pos,
    const char* s,
    std::size_t n,
    storage_ptr const& sp)
{
    const auto curr_size = size();
    if(pos > curr_size)
        detail::throw_out_of_range(
            BOOST_CURRENT_LOCATION);
    const auto curr_data = data();
    if(n <= capacity() - curr_size)
    {
        const bool inside = detail::ptr_in_range(curr_data, curr_data + curr_size, s);
        if (!inside || (inside && ((s - curr_data) + n <= pos)))
        {
            std::memmove(&curr_data[pos + n], &curr_data[pos], curr_size - pos + 1);
            std::memcpy(&curr_data[pos], s, n);
        }
        else
        {
            const std::size_t offset = s - curr_data;
            std::memmove(&curr_data[pos + n], &curr_data[pos], curr_size - pos + 1);
            if (offset < pos)
            {
                const std::size_t diff = pos - offset;
                std::memcpy(&curr_data[pos], &curr_data[offset], diff);
                std::memcpy(&curr_data[pos + diff], &curr_data[pos + n], n - diff);
            }
            else
            {
                std::memcpy(&curr_data[pos], &curr_data[offset + n], n);
            }
        }
        size(curr_size + n);
    }
    else
    { 
        if(n > max_size() - curr_size)
            detail::throw_length_error(
                "string too large",
                BOOST_CURRENT_LOCATION);
        string_impl tmp(growth(
            curr_size + n, capacity()), sp);
        tmp.size(curr_size + n);
        std::memcpy(
            tmp.data(),
            curr_data,
            pos);
        std::memcpy(
            tmp.data() + pos + n,
            curr_data + pos,
            curr_size + 1 - pos);
        std::memcpy(
            tmp.data() + pos,
            s,
            n);
        destroy(sp);
        *this = tmp;
    }
}
 
char*
string_impl::
insert_unchecked(
    std::size_t pos,
    std::size_t n,
    storage_ptr const& sp)
{
    const auto curr_size = size();
    if(pos > curr_size)
        detail::throw_out_of_range(
            BOOST_CURRENT_LOCATION);
    const auto curr_data = data();
    if(n <= capacity() - size())
    {
        auto const dest =
            curr_data + pos;
        std::memmove(
            dest + n,
            dest,
            curr_size + 1 - pos);
        size(curr_size + n);
        return dest;
    }
    if(n > max_size() - curr_size)
        detail::throw_length_error(
            "string too large",
            BOOST_CURRENT_LOCATION);
    string_impl tmp(growth(
        curr_size + n, capacity()), sp);
    tmp.size(curr_size + n);
    std::memcpy(
        tmp.data(),
        curr_data,
        pos);
    std::memcpy(
        tmp.data() + pos + n,
        curr_data + pos,
        curr_size + 1 - pos);
    destroy(sp);
    *this = tmp;
    return data() + pos;
}
 
void
string_impl::
replace(
    std::size_t pos,
    std::size_t n1,
    const char* s,
    std::size_t n2,
    storage_ptr const& sp)
{
    const auto curr_size = size();
    if (pos > curr_size)
        detail::throw_out_of_range(
            BOOST_CURRENT_LOCATION);
    const auto curr_data = data();
    n1 = (std::min)(n1, curr_size - pos);
    const auto delta = (std::max)(n1, n2) -
        (std::min)(n1, n2);
    // if we are shrinking in size or we have enough
    // capacity, dont reallocate
    if (n1 > n2 || delta <= capacity() - curr_size)
    {
        const bool inside = detail::ptr_in_range(curr_data, curr_data + curr_size, s);
        // there is nothing to replace; return
        if (inside && s == curr_data + pos && n1 == n2)
            return;
        if (!inside || (inside && ((s - curr_data) + n2 <= pos)))
        {
            // source outside
            std::memmove(&curr_data[pos + n2], &curr_data[pos + n1], curr_size - pos - n1 + 1);
            std::memcpy(&curr_data[pos], s, n2);
        }
        else
        {
            // source inside
            const std::size_t offset = s - curr_data;
            if (n2 >= n1)
            {
                // grow/unchanged
                const std::size_t diff = offset <= pos + n1 ? (std::min)((pos + n1) - offset, n2) : 0;
                // shift all right of splice point by n2 - n1 to the right
                std::memmove(&curr_data[pos + n2], &curr_data[pos + n1], curr_size - pos - n1 + 1);
                // copy all before splice point
                std::memmove(&curr_data[pos], &curr_data[offset], diff);
                // copy all after splice point
                std::memmove(&curr_data[pos + diff], &curr_data[(offset - n1) + n2 + diff], n2 - diff);
            }
            else
            {
                // shrink
                // copy all elements into place
                std::memmove(&curr_data[pos], &curr_data[offset], n2);
                // shift all elements after splice point left
                std::memmove(&curr_data[pos + n2], &curr_data[pos + n1], curr_size - pos - n1 + 1);
            }
        }
        size((curr_size - n1) + n2);
    }
    else
    {
        if (delta > max_size() - curr_size)
            detail::throw_length_error(
                "string too large",
                BOOST_CURRENT_LOCATION);
        // would exceed capacity, reallocate
        string_impl tmp(growth(
            curr_size + delta, capacity()), sp);
        tmp.size(curr_size + delta);
        std::memcpy(
            tmp.data(),
            curr_data,
            pos);
        std::memcpy(
            tmp.data() + pos + n2,
            curr_data + pos + n1,
            curr_size - pos - n1 + 1);
        std::memcpy(
            tmp.data() + pos,
            s,
            n2);
        destroy(sp);
        *this = tmp;
    }
}
 
// unlike the replace overload, this function does 
// not move any characters
char*
string_impl::
replace_unchecked(
    std::size_t pos,
    std::size_t n1,
    std::size_t n2,
    storage_ptr const& sp)
{
    const auto curr_size = size();
    if(pos > curr_size)
        detail::throw_out_of_range(
            BOOST_CURRENT_LOCATION);
    const auto curr_data = data();
    const auto delta = (std::max)(n1, n2) -
        (std::min)(n1, n2);
    // if the size doesn't change, we don't need to
    // do anything
    if (!delta)
      return curr_data + pos;
    // if we are shrinking in size or we have enough
    // capacity, dont reallocate
    if(n1 > n2 || delta <= capacity() - curr_size)
    {
        auto const replace_pos = curr_data + pos;
        std::memmove(
            replace_pos + n2,
            replace_pos + n1,
            curr_size - pos - n1 + 1);
        size((curr_size - n1) + n2);
        return replace_pos;
    }
    if(delta > max_size() - curr_size)
        detail::throw_length_error(
            "string too large",
            BOOST_CURRENT_LOCATION);
    // would exceed capacity, reallocate
    string_impl tmp(growth(
        curr_size + delta, capacity()), sp);
    tmp.size(curr_size + delta);
    std::memcpy(
        tmp.data(),
        curr_data,
        pos);
    std::memcpy(
        tmp.data() + pos + n2,
        curr_data + pos + n1,
        curr_size - pos - n1 + 1);
    destroy(sp);
    *this = tmp;
    return data() + pos;
}
 
void
string_impl::
shrink_to_fit(
    storage_ptr const& sp) noexcept
{
    if(s_.k == short_string_)
        return;
    auto const t = p_.t;
    if(t->size <= sbo_chars_)
    {
        s_.k = short_string_;
        std::memcpy(
            s_.buf, data(), t->size);
        s_.buf[sbo_chars_] =
            static_cast<char>(
                sbo_chars_ - t->size);
        s_.buf[t->size] = 0;
        sp->deallocate(t,
            sizeof(table) +
                t->capacity + 1,
            alignof(table));
        return;
    }
    if(t->size >= t->capacity)
        return;
#ifndef BOOST_NO_EXCEPTIONS
    try
    {
#endif
        string_impl tmp(t->size, sp);
        std::memcpy(
            tmp.data(),
            data(),
            size());
        destroy(sp);
        *this = tmp;
#ifndef BOOST_NO_EXCEPTIONS
    }
    catch(std::exception const&)
    {
        // eat the exception
    }
#endif
}
 
} // detail
BOOST_JSON_NS_END
 
#endif