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