Coverage Report

Created: 2025-06-13 06:26

/src/boost/boost/json/impl/string.ipp
Line
Count
Source (jump to first uncovered line)
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_IMPL_STRING_IPP
11
#define BOOST_JSON_IMPL_STRING_IPP
12
13
#include <boost/json/detail/except.hpp>
14
#include <algorithm>
15
#include <new>
16
#include <ostream>
17
#include <stdexcept>
18
#include <string>
19
#include <utility>
20
21
namespace boost {
22
namespace json {
23
24
//----------------------------------------------------------
25
//
26
// Construction
27
//
28
//----------------------------------------------------------
29
30
string::
31
string(
32
    std::size_t count,
33
    char ch,
34
    storage_ptr sp)
35
0
    : sp_(std::move(sp))
36
0
{
37
0
    assign(count, ch);
38
0
}
39
40
string::
41
string(
42
    char const* s,
43
    storage_ptr sp)
44
0
    : sp_(std::move(sp))
45
0
{
46
0
    assign(s);
47
0
}
48
49
string::
50
string(
51
    char const* s,
52
    std::size_t count,
53
    storage_ptr sp)
54
0
    : sp_(std::move(sp))
55
0
{
56
0
    assign(s, count);
57
0
}
58
59
string::
60
string(string const& other)
61
0
    : sp_(other.sp_)
62
0
{
63
0
    assign(other);
64
0
}
65
66
string::
67
string(
68
    string const& other,
69
    storage_ptr sp)
70
0
    : sp_(std::move(sp))
71
0
{
72
0
    assign(other);
73
0
}
74
75
string::
76
string(
77
    string&& other,
78
    storage_ptr sp)
79
0
    : sp_(std::move(sp))
80
0
{
81
0
    assign(std::move(other));
82
0
}
83
84
string::
85
string(
86
    string_view s,
87
    storage_ptr sp)
88
20.5k
    : sp_(std::move(sp))
89
20.5k
{
90
20.5k
    assign(s);
91
20.5k
}
92
93
//----------------------------------------------------------
94
//
95
// Assignment
96
//
97
//----------------------------------------------------------
98
99
string&
100
string::
101
operator=(string const& other)
102
0
{
103
0
    return assign(other);
104
0
}
105
106
string&
107
string::
108
operator=(string&& other)
109
0
{
110
0
    return assign(std::move(other));
111
0
}
112
113
string&
114
string::
115
operator=(char const* s)
116
0
{
117
0
    return assign(s);
118
0
}
119
120
string&
121
string::
122
operator=(string_view s)
123
0
{
124
0
    return assign(s);
125
0
}
126
127
128
129
string&
130
string::
131
assign(
132
    size_type count,
133
    char ch)
134
0
{
135
0
    std::char_traits<char>::assign(
136
0
        impl_.assign(count, sp_),
137
0
        count,
138
0
        ch);
139
0
    return *this;
140
0
}
141
142
string&
143
string::
144
assign(
145
    string const& other)
146
0
{
147
0
    if(this == &other)
148
0
        return *this;
149
0
    return assign(
150
0
        other.data(),
151
0
        other.size());
152
0
}
153
154
string&
155
string::
156
assign(string&& other)
157
0
{
158
0
    if( &other == this )
159
0
        return *this;
160
161
0
    if(*sp_ == *other.sp_)
162
0
    {
163
0
        impl_.destroy(sp_);
164
0
        impl_ = other.impl_;
165
0
        ::new(&other.impl_) detail::string_impl();
166
0
        return *this;
167
0
    }
168
169
    // copy
170
0
    return assign(other);
171
0
}
172
173
string&
174
string::
175
assign(
176
    char const* s,
177
    size_type count)
178
20.5k
{
179
20.5k
    std::char_traits<char>::copy(
180
20.5k
        impl_.assign(count, sp_),
181
20.5k
        s, count);
182
20.5k
    return *this;
183
20.5k
}
184
185
string&
186
string::
187
assign(
188
    char const* s)
189
0
{
190
0
    return assign(s, std::char_traits<
191
0
        char>::length(s));
192
0
}
193
194
//----------------------------------------------------------
195
//
196
// Capacity
197
//
198
//----------------------------------------------------------
199
200
void
201
string::
202
shrink_to_fit()
203
0
{
204
0
    impl_.shrink_to_fit(sp_);
205
0
}
206
207
//----------------------------------------------------------
208
//
209
// Access
210
//
211
//----------------------------------------------------------
212
213
system::result<char&>
214
string::try_at(std::size_t pos) noexcept
215
0
{
216
0
    if( pos < size() )
217
0
        return impl_.data()[pos];
218
219
0
    system::error_code ec;
220
0
    BOOST_JSON_FAIL(ec, error::out_of_range);
221
0
    return ec;
222
0
}
223
224
system::result<char const&>
225
string::try_at(std::size_t pos) const noexcept
226
0
{
227
0
    if( pos < size() )
228
0
        return impl_.data()[pos];
229
230
0
    system::error_code ec;
231
0
    BOOST_JSON_FAIL(ec, error::out_of_range);
232
0
    return ec;
233
0
}
234
235
char const&
236
string::at(std::size_t pos, source_location const& loc) const
237
0
{
238
0
    return try_at(pos).value(loc);
239
0
}
240
241
//----------------------------------------------------------
242
//
243
// Operations
244
//
245
//----------------------------------------------------------
246
247
void
248
string::
249
clear() noexcept
250
0
{
251
0
    impl_.term(0);
252
0
}
253
254
//----------------------------------------------------------
255
256
void
257
string::
258
push_back(char ch)
259
0
{
260
0
    *impl_.append(1, sp_) = ch;
261
0
}
262
263
void
264
string::
265
pop_back()
266
0
{
267
0
    back() = 0;
268
0
    impl_.size(impl_.size() - 1);
269
0
}
270
271
//----------------------------------------------------------
272
273
string&
274
string::
275
append(size_type count, char ch)
276
0
{
277
0
    std::char_traits<char>::assign(
278
0
        impl_.append(count, sp_),
279
0
        count, ch);
280
0
    return *this;
281
0
}
282
283
string&
284
string::
285
append(string_view sv)
286
0
{
287
0
    std::char_traits<char>::copy(
288
0
        impl_.append(sv.size(), sp_),
289
0
        sv.data(), sv.size());
290
0
    return *this;
291
0
}
292
293
//----------------------------------------------------------
294
295
string&
296
string::
297
insert(
298
    size_type pos,
299
    string_view sv)
300
0
{
301
0
    impl_.insert(pos, sv.data(), sv.size(), sp_);
302
0
    return *this;
303
0
}
304
305
string&
306
string::
307
insert(
308
    std::size_t pos,
309
    std::size_t count,
310
    char ch)
311
0
{
312
0
    std::char_traits<char>::assign(
313
0
        impl_.insert_unchecked(pos, count, sp_),
314
0
        count, ch);
315
0
    return *this;
316
0
}
317
318
//----------------------------------------------------------
319
320
string&
321
string::
322
replace(
323
    std::size_t pos,
324
    std::size_t count,
325
    string_view sv)
326
0
{
327
0
    impl_.replace(pos, count, sv.data(), sv.size(), sp_);
328
0
    return *this;
329
0
}
330
331
string&
332
string::
333
replace(
334
    std::size_t pos,
335
    std::size_t count,
336
    std::size_t count2,
337
    char ch)
338
0
{
339
0
    std::char_traits<char>::assign(
340
0
        impl_.replace_unchecked(pos, count, count2, sp_),
341
0
        count2, ch);
342
0
    return *this;
343
0
}
344
345
//----------------------------------------------------------
346
347
string&
348
string::
349
erase(
350
    size_type pos,
351
    size_type count)
352
0
{
353
0
    if(pos > impl_.size())
354
0
    {
355
0
        BOOST_STATIC_CONSTEXPR source_location loc = BOOST_CURRENT_LOCATION;
356
0
        detail::throw_system_error( error::out_of_range, &loc );
357
0
    }
358
0
    if( count > impl_.size() - pos)
359
0
        count = impl_.size() - pos;
360
0
    std::char_traits<char>::move(
361
0
        impl_.data() + pos,
362
0
        impl_.data() + pos + count,
363
0
        impl_.size() - pos - count + 1);
364
0
    impl_.term(impl_.size() - count);
365
0
    return *this;
366
0
}
367
368
auto
369
string::
370
erase(const_iterator pos) ->
371
    iterator
372
0
{
373
0
    return erase(pos, pos+1);
374
0
}
375
376
auto
377
string::
378
erase(
379
    const_iterator first,
380
    const_iterator last) ->
381
        iterator
382
0
{
383
0
    auto const pos = first - begin();
384
0
    auto const count = last - first;
385
0
    erase(pos, count);
386
0
    return data() + pos;
387
0
}
388
389
//----------------------------------------------------------
390
391
void
392
string::
393
resize(size_type count, char ch)
394
0
{
395
0
    if(count <= impl_.size())
396
0
    {
397
0
        impl_.term(count);
398
0
        return;
399
0
    }
400
401
0
    reserve(count);
402
0
    std::char_traits<char>::assign(
403
0
        impl_.end(),
404
0
        count - impl_.size(),
405
0
        ch);
406
0
    grow(count - size());
407
0
}
408
409
//----------------------------------------------------------
410
411
void
412
string::
413
swap(string& other)
414
0
{
415
0
    if(*sp_ == *other.sp_)
416
0
    {
417
0
        std::swap(impl_, other.impl_);
418
0
        return;
419
0
    }
420
0
    string temp1(
421
0
        std::move(*this), other.sp_);
422
0
    string temp2(
423
0
        std::move(other), sp_);
424
0
    this->~string();
425
0
    ::new(this) string(pilfer(temp2));
426
0
    other.~string();
427
0
    ::new(&other) string(pilfer(temp1));
428
0
}
429
430
//----------------------------------------------------------
431
432
void
433
string::
434
reserve_impl(size_type new_cap)
435
812
{
436
812
    BOOST_ASSERT(
437
812
        new_cap >= impl_.capacity());
438
812
    if(new_cap > impl_.capacity())
439
812
    {
440
        // grow
441
812
        new_cap = detail::string_impl::growth(
442
812
            new_cap, impl_.capacity());
443
812
        detail::string_impl tmp(new_cap, sp_);
444
812
        std::char_traits<char>::copy(tmp.data(),
445
812
            impl_.data(), impl_.size() + 1);
446
812
        tmp.size(impl_.size());
447
812
        impl_.destroy(sp_);
448
812
        impl_ = tmp;
449
812
        return;
450
812
    }
451
812
}
452
453
} // namespace json
454
} // namespace boost
455
456
//----------------------------------------------------------
457
458
std::size_t
459
std::hash< ::boost::json::string >::operator()(
460
    ::boost::json::string const& js ) const noexcept
461
0
{
462
0
    return ::boost::hash< ::boost::json::string >()( js );
463
0
}
464
465
#endif