Bug Summary

File:out/../deps/v8/src/inspector/v8-inspector-session-impl.cc
Warning:line 306, column 14
Called C++ object pointer is null

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-unknown-linux-gnu -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name v8-inspector-session-impl.cc -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model pic -pic-level 2 -pic-is-pie -mframe-pointer=all -relaxed-aliasing -fmath-errno -ffp-contract=on -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -ffunction-sections -fdata-sections -fcoverage-compilation-dir=/home/maurizio/node-v18.6.0/out -resource-dir /usr/local/lib/clang/16.0.0 -D _GLIBCXX_USE_CXX11_ABI=1 -D NODE_OPENSSL_CONF_NAME=nodejs_conf -D NODE_OPENSSL_HAS_QUIC -D V8_GYP_BUILD -D V8_TYPED_ARRAY_MAX_SIZE_IN_HEAP=64 -D __STDC_FORMAT_MACROS -D OPENSSL_NO_PINSHARED -D OPENSSL_THREADS -D V8_TARGET_ARCH_X64 -D V8_HAVE_TARGET_OS -D V8_TARGET_OS_LINUX -D V8_EMBEDDER_STRING="-node.8" -D ENABLE_DISASSEMBLER -D V8_PROMISE_INTERNAL_FIELD_COUNT=1 -D V8_SHORT_BUILTIN_CALLS -D OBJECT_PRINT -D V8_INTL_SUPPORT -D V8_ATOMIC_OBJECT_FIELD_WRITES -D V8_ENABLE_LAZY_SOURCE_POSITIONS -D V8_USE_SIPHASH -D V8_SHARED_RO_HEAP -D V8_WIN64_UNWINDING_INFO -D V8_ENABLE_REGEXP_INTERPRETER_THREADED_DISPATCH -D V8_SNAPSHOT_COMPRESSION -D V8_ENABLE_WEBASSEMBLY -D V8_ENABLE_JAVASCRIPT_PROMISE_HOOKS -D V8_ALLOCATION_FOLDING -D V8_ALLOCATION_SITE_TRACKING -D V8_SCRIPTORMODULE_LEGACY_LIFETIME -D V8_ADVANCED_BIGINT_ALGORITHMS -D ICU_UTIL_DATA_IMPL=ICU_UTIL_DATA_STATIC -D UCONFIG_NO_SERVICE=1 -D U_ENABLE_DYLOAD=0 -D U_STATIC_IMPLEMENTATION=1 -D U_HAVE_STD_STRING=1 -D UCONFIG_NO_BREAK_ITERATION=0 -I ../deps/v8 -I ../deps/v8/include -I /home/maurizio/node-v18.6.0/out/Release/obj/gen/inspector-generated-output-root -I ../deps/v8/third_party/inspector_protocol -I /home/maurizio/node-v18.6.0/out/Release/obj/gen -I /home/maurizio/node-v18.6.0/out/Release/obj/gen/generate-bytecode-output-root -I ../deps/icu-small/source/i18n -I ../deps/icu-small/source/common -I ../deps/v8/third_party/zlib -I ../deps/v8/third_party/zlib/google -internal-isystem /usr/lib/gcc/x86_64-redhat-linux/8/../../../../include/c++/8 -internal-isystem /usr/lib/gcc/x86_64-redhat-linux/8/../../../../include/c++/8/x86_64-redhat-linux -internal-isystem /usr/lib/gcc/x86_64-redhat-linux/8/../../../../include/c++/8/backward -internal-isystem /usr/local/lib/clang/16.0.0/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-redhat-linux/8/../../../../x86_64-redhat-linux/include -internal-externc-isystem /include -internal-externc-isystem /usr/include -O3 -Wno-unused-parameter -Wno-return-type -std=gnu++17 -fdeprecated-macro -fdebug-compilation-dir=/home/maurizio/node-v18.6.0/out -ferror-limit 19 -fno-rtti -fgnuc-version=4.2.1 -vectorize-loops -vectorize-slp -analyzer-output=html -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /tmp/scan-build-2022-08-22-142216-507842-1 -x c++ ../deps/v8/src/inspector/v8-inspector-session-impl.cc

../deps/v8/src/inspector/v8-inspector-session-impl.cc

1// Copyright 2016 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "src/inspector/v8-inspector-session-impl.h"
6
7#include "../../third_party/inspector_protocol/crdtp/cbor.h"
8#include "../../third_party/inspector_protocol/crdtp/dispatch.h"
9#include "../../third_party/inspector_protocol/crdtp/json.h"
10#include "src/base/logging.h"
11#include "src/base/macros.h"
12#include "src/inspector/injected-script.h"
13#include "src/inspector/inspected-context.h"
14#include "src/inspector/protocol/Protocol.h"
15#include "src/inspector/remote-object-id.h"
16#include "src/inspector/search-util.h"
17#include "src/inspector/string-util.h"
18#include "src/inspector/v8-console-agent-impl.h"
19#include "src/inspector/v8-debugger-agent-impl.h"
20#include "src/inspector/v8-debugger.h"
21#include "src/inspector/v8-heap-profiler-agent-impl.h"
22#include "src/inspector/v8-inspector-impl.h"
23#include "src/inspector/v8-profiler-agent-impl.h"
24#include "src/inspector/v8-runtime-agent-impl.h"
25#include "src/inspector/v8-schema-agent-impl.h"
26
27namespace v8_inspector {
28namespace {
29using v8_crdtp::span;
30using v8_crdtp::SpanFrom;
31using v8_crdtp::Status;
32using v8_crdtp::cbor::CheckCBORMessage;
33using v8_crdtp::json::ConvertCBORToJSON;
34using v8_crdtp::json::ConvertJSONToCBOR;
35
36bool IsCBORMessage(StringView msg) {
37 return msg.is8Bit() && msg.length() >= 2 && msg.characters8()[0] == 0xd8 &&
38 msg.characters8()[1] == 0x5a;
39}
40
41Status ConvertToCBOR(StringView state, std::vector<uint8_t>* cbor) {
42 return state.is8Bit()
43 ? ConvertJSONToCBOR(
44 span<uint8_t>(state.characters8(), state.length()), cbor)
45 : ConvertJSONToCBOR(
46 span<uint16_t>(state.characters16(), state.length()), cbor);
47}
48
49std::unique_ptr<protocol::DictionaryValue> ParseState(StringView state) {
50 std::vector<uint8_t> converted;
51 span<uint8_t> cbor;
52 if (IsCBORMessage(state))
53 cbor = span<uint8_t>(state.characters8(), state.length());
54 else if (ConvertToCBOR(state, &converted).ok())
55 cbor = SpanFrom(converted);
56 if (!cbor.empty()) {
57 std::unique_ptr<protocol::Value> value =
58 protocol::Value::parseBinary(cbor.data(), cbor.size());
59 std::unique_ptr<protocol::DictionaryValue> dictionaryValue =
60 protocol::DictionaryValue::cast(std::move(value));
61 if (dictionaryValue) return dictionaryValue;
62 }
63 return protocol::DictionaryValue::create();
64}
65} // namespace
66
67// static
68bool V8InspectorSession::canDispatchMethod(StringView method) {
69 return stringViewStartsWith(method,
70 protocol::Runtime::Metainfo::commandPrefix) ||
71 stringViewStartsWith(method,
72 protocol::Debugger::Metainfo::commandPrefix) ||
73 stringViewStartsWith(method,
74 protocol::Profiler::Metainfo::commandPrefix) ||
75 stringViewStartsWith(
76 method, protocol::HeapProfiler::Metainfo::commandPrefix) ||
77 stringViewStartsWith(method,
78 protocol::Console::Metainfo::commandPrefix) ||
79 stringViewStartsWith(method,
80 protocol::Schema::Metainfo::commandPrefix);
81}
82
83// static
84int V8ContextInfo::executionContextId(v8::Local<v8::Context> context) {
85 return InspectedContext::contextId(context);
86}
87
88std::unique_ptr<V8InspectorSessionImpl> V8InspectorSessionImpl::create(
89 V8InspectorImpl* inspector, int contextGroupId, int sessionId,
90 V8Inspector::Channel* channel, StringView state) {
91 return std::unique_ptr<V8InspectorSessionImpl>(new V8InspectorSessionImpl(
92 inspector, contextGroupId, sessionId, channel, state));
93}
94
95V8InspectorSessionImpl::V8InspectorSessionImpl(V8InspectorImpl* inspector,
96 int contextGroupId,
97 int sessionId,
98 V8Inspector::Channel* channel,
99 StringView savedState)
100 : m_contextGroupId(contextGroupId),
101 m_sessionId(sessionId),
102 m_inspector(inspector),
103 m_channel(channel),
104 m_customObjectFormatterEnabled(false),
105 m_dispatcher(this),
106 m_state(ParseState(savedState)),
107 m_runtimeAgent(nullptr),
108 m_debuggerAgent(nullptr),
109 m_heapProfilerAgent(nullptr),
110 m_profilerAgent(nullptr),
111 m_consoleAgent(nullptr),
112 m_schemaAgent(nullptr) {
113 m_state->getBoolean("use_binary_protocol", &use_binary_protocol_);
114
115 m_runtimeAgent.reset(new V8RuntimeAgentImpl(
116 this, this, agentState(protocol::Runtime::Metainfo::domainName)));
117 protocol::Runtime::Dispatcher::wire(&m_dispatcher, m_runtimeAgent.get());
118
119 m_debuggerAgent.reset(new V8DebuggerAgentImpl(
120 this, this, agentState(protocol::Debugger::Metainfo::domainName)));
121 protocol::Debugger::Dispatcher::wire(&m_dispatcher, m_debuggerAgent.get());
122
123 m_profilerAgent.reset(new V8ProfilerAgentImpl(
124 this, this, agentState(protocol::Profiler::Metainfo::domainName)));
125 protocol::Profiler::Dispatcher::wire(&m_dispatcher, m_profilerAgent.get());
126
127 m_heapProfilerAgent.reset(new V8HeapProfilerAgentImpl(
128 this, this, agentState(protocol::HeapProfiler::Metainfo::domainName)));
129 protocol::HeapProfiler::Dispatcher::wire(&m_dispatcher,
130 m_heapProfilerAgent.get());
131
132 m_consoleAgent.reset(new V8ConsoleAgentImpl(
133 this, this, agentState(protocol::Console::Metainfo::domainName)));
134 protocol::Console::Dispatcher::wire(&m_dispatcher, m_consoleAgent.get());
135
136 m_schemaAgent.reset(new V8SchemaAgentImpl(
137 this, this, agentState(protocol::Schema::Metainfo::domainName)));
138 protocol::Schema::Dispatcher::wire(&m_dispatcher, m_schemaAgent.get());
139
140 if (savedState.length()) {
141 m_runtimeAgent->restore();
142 m_debuggerAgent->restore();
143 m_heapProfilerAgent->restore();
144 m_profilerAgent->restore();
145 m_consoleAgent->restore();
146 }
147}
148
149V8InspectorSessionImpl::~V8InspectorSessionImpl() {
150 v8::Isolate::Scope scope(m_inspector->isolate());
151 discardInjectedScripts();
152 m_consoleAgent->disable();
153 m_profilerAgent->disable();
154 m_heapProfilerAgent->disable();
155 m_debuggerAgent->disable();
156 m_runtimeAgent->disable();
157 m_inspector->disconnect(this);
158}
159
160std::unique_ptr<V8InspectorSession::CommandLineAPIScope>
161V8InspectorSessionImpl::initializeCommandLineAPIScope(int executionContextId) {
162 auto scope =
163 std::make_unique<InjectedScript::ContextScope>(this, executionContextId);
164 auto result = scope->initialize();
165 if (!result.IsSuccess()) {
166 return nullptr;
167 }
168
169 scope->installCommandLineAPI();
170
171 return scope;
172}
173
174protocol::DictionaryValue* V8InspectorSessionImpl::agentState(
175 const String16& name) {
176 protocol::DictionaryValue* state = m_state->getObject(name);
177 if (!state) {
178 std::unique_ptr<protocol::DictionaryValue> newState =
179 protocol::DictionaryValue::create();
180 state = newState.get();
181 m_state->setObject(name, std::move(newState));
182 }
183 return state;
184}
185
186std::unique_ptr<StringBuffer> V8InspectorSessionImpl::serializeForFrontend(
187 std::unique_ptr<protocol::Serializable> message) {
188 std::vector<uint8_t> cbor = message->Serialize();
189 DCHECK(CheckCBORMessage(SpanFrom(cbor)).ok())((void) 0);
190 if (use_binary_protocol_) return StringBufferFrom(std::move(cbor));
191 std::vector<uint8_t> json;
192 Status status = ConvertCBORToJSON(SpanFrom(cbor), &json);
193 DCHECK(status.ok())((void) 0);
194 USE(status)do { ::v8::base::Use unused_tmp_array_for_use_macro[]{status}
; (void)unused_tmp_array_for_use_macro; } while (false)
;
195 // TODO(johannes): It should be OK to make a StringBuffer from |json|
196 // directly, since it's 7 Bit US-ASCII with anything else escaped.
197 // However it appears that the Node.js tests (or perhaps even production)
198 // assume that the StringBuffer is 16 Bit. It probably accesses
199 // characters16() somehwere without checking is8Bit. Until it's fixed
200 // we take a detour via String16 which makes the StringBuffer 16 bit.
201 String16 string16(reinterpret_cast<const char*>(json.data()), json.size());
202 return StringBufferFrom(std::move(string16));
203}
204
205void V8InspectorSessionImpl::SendProtocolResponse(
206 int callId, std::unique_ptr<protocol::Serializable> message) {
207 m_channel->sendResponse(callId, serializeForFrontend(std::move(message)));
208}
209
210void V8InspectorSessionImpl::SendProtocolNotification(
211 std::unique_ptr<protocol::Serializable> message) {
212 m_channel->sendNotification(serializeForFrontend(std::move(message)));
213}
214
215void V8InspectorSessionImpl::FallThrough(int callId,
216 const v8_crdtp::span<uint8_t> method,
217 v8_crdtp::span<uint8_t> message) {
218 // There's no other layer to handle the command.
219 UNREACHABLE()V8_Fatal("unreachable code");
220}
221
222void V8InspectorSessionImpl::FlushProtocolNotifications() {
223 m_channel->flushProtocolNotifications();
224}
225
226void V8InspectorSessionImpl::reset() {
227 m_debuggerAgent->reset();
228 m_runtimeAgent->reset();
229 discardInjectedScripts();
230}
231
232void V8InspectorSessionImpl::discardInjectedScripts() {
233 m_inspectedObjects.clear();
234 int sessionId = m_sessionId;
235 m_inspector->forEachContext(m_contextGroupId,
236 [&sessionId](InspectedContext* context) {
237 context->discardInjectedScript(sessionId);
238 });
239}
240
241Response V8InspectorSessionImpl::findInjectedScript(
242 int contextId, InjectedScript*& injectedScript) {
243 injectedScript = nullptr;
244 InspectedContext* context =
245 m_inspector->getContext(m_contextGroupId, contextId);
246 if (!context)
247 return Response::ServerError("Cannot find context with specified id");
248 injectedScript = context->getInjectedScript(m_sessionId);
249 if (!injectedScript) {
250 injectedScript = context->createInjectedScript(m_sessionId);
251 if (m_customObjectFormatterEnabled)
252 injectedScript->setCustomObjectFormatterEnabled(true);
253 }
254 return Response::Success();
255}
256
257Response V8InspectorSessionImpl::findInjectedScript(
258 RemoteObjectIdBase* objectId, InjectedScript*& injectedScript) {
259 if (objectId->isolateId() != m_inspector->isolateId())
7
Assuming the condition is true
8
Taking true branch
260 return Response::ServerError("Cannot find context with specified id");
9
Returning without writing to 'injectedScript'
261 return findInjectedScript(objectId->contextId(), injectedScript);
262}
263
264void V8InspectorSessionImpl::releaseObjectGroup(StringView objectGroup) {
265 releaseObjectGroup(toString16(objectGroup));
266}
267
268void V8InspectorSessionImpl::releaseObjectGroup(const String16& objectGroup) {
269 int sessionId = m_sessionId;
270 m_inspector->forEachContext(
271 m_contextGroupId, [&objectGroup, &sessionId](InspectedContext* context) {
272 InjectedScript* injectedScript = context->getInjectedScript(sessionId);
273 if (injectedScript) injectedScript->releaseObjectGroup(objectGroup);
274 });
275}
276
277bool V8InspectorSessionImpl::unwrapObject(
278 std::unique_ptr<StringBuffer>* error, StringView objectId,
279 v8::Local<v8::Value>* object, v8::Local<v8::Context>* context,
280 std::unique_ptr<StringBuffer>* objectGroup) {
281 String16 objectGroupString;
282 Response response = unwrapObject(toString16(objectId), object, context,
3
Calling 'V8InspectorSessionImpl::unwrapObject'
283 objectGroup ? &objectGroupString : nullptr);
1
Assuming 'objectGroup' is null
2
'?' condition is false
284 if (response.IsError()) {
285 if (error) {
286 const std::string& msg = response.Message();
287 *error = StringBufferFrom(String16::fromUTF8(msg.data(), msg.size()));
288 }
289 return false;
290 }
291 if (objectGroup)
292 *objectGroup = StringBufferFrom(std::move(objectGroupString));
293 return true;
294}
295
296Response V8InspectorSessionImpl::unwrapObject(const String16& objectId,
297 v8::Local<v8::Value>* object,
298 v8::Local<v8::Context>* context,
299 String16* objectGroup) {
300 std::unique_ptr<RemoteObjectId> remoteId;
301 Response response = RemoteObjectId::parse(objectId, &remoteId);
302 if (!response.IsSuccess()) return response;
4
Taking false branch
303 InjectedScript* injectedScript = nullptr;
5
'injectedScript' initialized to a null pointer value
304 response = findInjectedScript(remoteId.get(), injectedScript);
6
Calling 'V8InspectorSessionImpl::findInjectedScript'
10
Returning from 'V8InspectorSessionImpl::findInjectedScript'
305 if (!response.IsSuccess()) return response;
11
Taking false branch
306 response = injectedScript->findObject(*remoteId, object);
12
Called C++ object pointer is null
307 if (!response.IsSuccess()) return response;
308 *context = injectedScript->context()->context();
309 if (objectGroup) *objectGroup = injectedScript->objectGroupName(*remoteId);
310 return Response::Success();
311}
312
313std::unique_ptr<protocol::Runtime::API::RemoteObject>
314V8InspectorSessionImpl::wrapObject(v8::Local<v8::Context> context,
315 v8::Local<v8::Value> value,
316 StringView groupName, bool generatePreview) {
317 return wrapObject(context, value, toString16(groupName), generatePreview);
318}
319
320std::unique_ptr<protocol::Runtime::RemoteObject>
321V8InspectorSessionImpl::wrapObject(v8::Local<v8::Context> context,
322 v8::Local<v8::Value> value,
323 const String16& groupName,
324 bool generatePreview) {
325 InjectedScript* injectedScript = nullptr;
326 findInjectedScript(InspectedContext::contextId(context), injectedScript);
327 if (!injectedScript) return nullptr;
328 std::unique_ptr<protocol::Runtime::RemoteObject> result;
329 injectedScript->wrapObject(
330 value, groupName,
331 generatePreview ? WrapMode::kWithPreview : WrapMode::kNoPreview, &result);
332 return result;
333}
334
335std::unique_ptr<protocol::Runtime::RemoteObject>
336V8InspectorSessionImpl::wrapTable(v8::Local<v8::Context> context,
337 v8::Local<v8::Object> table,
338 v8::MaybeLocal<v8::Array> columns) {
339 InjectedScript* injectedScript = nullptr;
340 findInjectedScript(InspectedContext::contextId(context), injectedScript);
341 if (!injectedScript) return nullptr;
342 return injectedScript->wrapTable(table, columns);
343}
344
345void V8InspectorSessionImpl::setCustomObjectFormatterEnabled(bool enabled) {
346 m_customObjectFormatterEnabled = enabled;
347 int sessionId = m_sessionId;
348 m_inspector->forEachContext(
349 m_contextGroupId, [&enabled, &sessionId](InspectedContext* context) {
350 InjectedScript* injectedScript = context->getInjectedScript(sessionId);
351 if (injectedScript)
352 injectedScript->setCustomObjectFormatterEnabled(enabled);
353 });
354}
355
356void V8InspectorSessionImpl::reportAllContexts(V8RuntimeAgentImpl* agent) {
357 m_inspector->forEachContext(m_contextGroupId,
358 [&agent](InspectedContext* context) {
359 agent->reportExecutionContextCreated(context);
360 });
361}
362
363void V8InspectorSessionImpl::dispatchProtocolMessage(StringView message) {
364 using v8_crdtp::span;
365 using v8_crdtp::SpanFrom;
366 span<uint8_t> cbor;
367 std::vector<uint8_t> converted_cbor;
368 if (IsCBORMessage(message)) {
369 use_binary_protocol_ = true;
370 m_state->setBoolean("use_binary_protocol", true);
371 cbor = span<uint8_t>(message.characters8(), message.length());
372 } else {
373 // We're ignoring the return value of the conversion function
374 // intentionally. It means the |parsed_message| below will be nullptr.
375 auto status = ConvertToCBOR(message, &converted_cbor);
376 if (!status.ok()) {
377 m_channel->sendNotification(
378 serializeForFrontend(v8_crdtp::CreateErrorNotification(
379 v8_crdtp::DispatchResponse::ParseError(status.ToASCIIString()))));
380 return;
381 }
382 cbor = SpanFrom(converted_cbor);
383 }
384 v8_crdtp::Dispatchable dispatchable(cbor);
385 if (!dispatchable.ok()) {
386 if (dispatchable.HasCallId()) {
387 m_channel->sendNotification(serializeForFrontend(
388 v8_crdtp::CreateErrorNotification(dispatchable.DispatchError())));
389 } else {
390 m_channel->sendResponse(
391 dispatchable.CallId(),
392 serializeForFrontend(v8_crdtp::CreateErrorResponse(
393 dispatchable.CallId(), dispatchable.DispatchError())));
394 }
395 return;
396 }
397 m_dispatcher.Dispatch(dispatchable).Run();
398}
399
400std::vector<uint8_t> V8InspectorSessionImpl::state() {
401 return m_state->Serialize();
402}
403
404std::vector<std::unique_ptr<protocol::Schema::API::Domain>>
405V8InspectorSessionImpl::supportedDomains() {
406 std::vector<std::unique_ptr<protocol::Schema::Domain>> domains =
407 supportedDomainsImpl();
408 std::vector<std::unique_ptr<protocol::Schema::API::Domain>> result;
409 for (size_t i = 0; i < domains.size(); ++i)
410 result.push_back(std::move(domains[i]));
411 return result;
412}
413
414std::vector<std::unique_ptr<protocol::Schema::Domain>>
415V8InspectorSessionImpl::supportedDomainsImpl() {
416 std::vector<std::unique_ptr<protocol::Schema::Domain>> result;
417 result.push_back(protocol::Schema::Domain::create()
418 .setName(protocol::Runtime::Metainfo::domainName)
419 .setVersion(protocol::Runtime::Metainfo::version)
420 .build());
421 result.push_back(protocol::Schema::Domain::create()
422 .setName(protocol::Debugger::Metainfo::domainName)
423 .setVersion(protocol::Debugger::Metainfo::version)
424 .build());
425 result.push_back(protocol::Schema::Domain::create()
426 .setName(protocol::Profiler::Metainfo::domainName)
427 .setVersion(protocol::Profiler::Metainfo::version)
428 .build());
429 result.push_back(protocol::Schema::Domain::create()
430 .setName(protocol::HeapProfiler::Metainfo::domainName)
431 .setVersion(protocol::HeapProfiler::Metainfo::version)
432 .build());
433 result.push_back(protocol::Schema::Domain::create()
434 .setName(protocol::Schema::Metainfo::domainName)
435 .setVersion(protocol::Schema::Metainfo::version)
436 .build());
437 return result;
438}
439
440void V8InspectorSessionImpl::addInspectedObject(
441 std::unique_ptr<V8InspectorSession::Inspectable> inspectable) {
442 m_inspectedObjects.insert(m_inspectedObjects.begin(), std::move(inspectable));
443 if (m_inspectedObjects.size() > kInspectedObjectBufferSize)
444 m_inspectedObjects.resize(kInspectedObjectBufferSize);
445}
446
447V8InspectorSession::Inspectable* V8InspectorSessionImpl::inspectedObject(
448 unsigned num) {
449 if (num >= m_inspectedObjects.size()) return nullptr;
450 return m_inspectedObjects[num].get();
451}
452
453void V8InspectorSessionImpl::schedulePauseOnNextStatement(
454 StringView breakReason, StringView breakDetails) {
455 std::vector<uint8_t> cbor;
456 ConvertToCBOR(breakDetails, &cbor);
457 m_debuggerAgent->schedulePauseOnNextStatement(
458 toString16(breakReason),
459 protocol::DictionaryValue::cast(
460 protocol::Value::parseBinary(cbor.data(), cbor.size())));
461}
462
463void V8InspectorSessionImpl::cancelPauseOnNextStatement() {
464 m_debuggerAgent->cancelPauseOnNextStatement();
465}
466
467void V8InspectorSessionImpl::breakProgram(StringView breakReason,
468 StringView breakDetails) {
469 std::vector<uint8_t> cbor;
470 ConvertToCBOR(breakDetails, &cbor);
471 m_debuggerAgent->breakProgram(
472 toString16(breakReason),
473 protocol::DictionaryValue::cast(
474 protocol::Value::parseBinary(cbor.data(), cbor.size())));
475}
476
477void V8InspectorSessionImpl::setSkipAllPauses(bool skip) {
478 m_debuggerAgent->setSkipAllPauses(skip);
479}
480
481void V8InspectorSessionImpl::resume(bool terminateOnResume) {
482 m_debuggerAgent->resume(terminateOnResume);
483}
484
485void V8InspectorSessionImpl::stepOver() { m_debuggerAgent->stepOver({}); }
486
487std::vector<std::unique_ptr<protocol::Debugger::API::SearchMatch>>
488V8InspectorSessionImpl::searchInTextByLines(StringView text, StringView query,
489 bool caseSensitive, bool isRegex) {
490 // TODO(dgozman): search may operate on StringView and avoid copying |text|.
491 std::vector<std::unique_ptr<protocol::Debugger::SearchMatch>> matches =
492 searchInTextByLinesImpl(this, toString16(text), toString16(query),
493 caseSensitive, isRegex);
494 std::vector<std::unique_ptr<protocol::Debugger::API::SearchMatch>> result;
495 for (size_t i = 0; i < matches.size(); ++i)
496 result.push_back(std::move(matches[i]));
497 return result;
498}
499
500void V8InspectorSessionImpl::triggerPreciseCoverageDeltaUpdate(
501 StringView occasion) {
502 m_profilerAgent->triggerPreciseCoverageDeltaUpdate(toString16(occasion));
503}
504
505} // namespace v8_inspector

/usr/lib/gcc/x86_64-redhat-linux/8/../../../../include/c++/8/bits/basic_string.h

1// Components for manipulating sequences of characters -*- C++ -*-
2
3// Copyright (C) 1997-2018 Free Software Foundation, Inc.
4//
5// This file is part of the GNU ISO C++ Library. This library is free
6// software; you can redistribute it and/or modify it under the
7// terms of the GNU General Public License as published by the
8// Free Software Foundation; either version 3, or (at your option)
9// any later version.
10
11// This library is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
16// Under Section 7 of GPL version 3, you are granted additional
17// permissions described in the GCC Runtime Library Exception, version
18// 3.1, as published by the Free Software Foundation.
19
20// You should have received a copy of the GNU General Public License and
21// a copy of the GCC Runtime Library Exception along with this program;
22// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23// <http://www.gnu.org/licenses/>.
24
25/** @file bits/basic_string.h
26 * This is an internal header file, included by other library headers.
27 * Do not attempt to use it directly. @headername{string}
28 */
29
30//
31// ISO C++ 14882: 21 Strings library
32//
33
34#ifndef _BASIC_STRING_H1
35#define _BASIC_STRING_H1 1
36
37#pragma GCC system_header
38
39#include <ext/atomicity.h>
40#include <ext/alloc_traits.h>
41#include <debug/debug.h>
42
43#if __cplusplus201703L >= 201103L
44#include <initializer_list>
45#endif
46
47#if __cplusplus201703L > 201402L
48# include <string_view>
49#endif
50
51namespace std _GLIBCXX_VISIBILITY(default)__attribute__ ((__visibility__ ("default")))
52{
53_GLIBCXX_BEGIN_NAMESPACE_VERSION
54
55#if __cplusplus201703L >= 201703L
56// Support P0426R1 changes to char_traits in C++17.
57# define __cpp_lib_constexpr_string201611L 201611L
58#endif
59
60#if _GLIBCXX_USE_CXX11_ABI1
61_GLIBCXX_BEGIN_NAMESPACE_CXX11namespace __cxx11 {
62 /**
63 * @class basic_string basic_string.h <string>
64 * @brief Managing sequences of characters and character-like objects.
65 *
66 * @ingroup strings
67 * @ingroup sequences
68 *
69 * @tparam _CharT Type of character
70 * @tparam _Traits Traits for character type, defaults to
71 * char_traits<_CharT>.
72 * @tparam _Alloc Allocator type, defaults to allocator<_CharT>.
73 *
74 * Meets the requirements of a <a href="tables.html#65">container</a>, a
75 * <a href="tables.html#66">reversible container</a>, and a
76 * <a href="tables.html#67">sequence</a>. Of the
77 * <a href="tables.html#68">optional sequence requirements</a>, only
78 * @c push_back, @c at, and @c %array access are supported.
79 */
80 template<typename _CharT, typename _Traits, typename _Alloc>
81 class basic_string
82 {
83 typedef typename __gnu_cxx::__alloc_traits<_Alloc>::template
84 rebind<_CharT>::other _Char_alloc_type;
85 typedef __gnu_cxx::__alloc_traits<_Char_alloc_type> _Alloc_traits;
86
87 // Types:
88 public:
89 typedef _Traits traits_type;
90 typedef typename _Traits::char_type value_type;
91 typedef _Char_alloc_type allocator_type;
92 typedef typename _Alloc_traits::size_type size_type;
93 typedef typename _Alloc_traits::difference_type difference_type;
94 typedef typename _Alloc_traits::reference reference;
95 typedef typename _Alloc_traits::const_reference const_reference;
96 typedef typename _Alloc_traits::pointer pointer;
97 typedef typename _Alloc_traits::const_pointer const_pointer;
98 typedef __gnu_cxx::__normal_iterator<pointer, basic_string> iterator;
99 typedef __gnu_cxx::__normal_iterator<const_pointer, basic_string>
100 const_iterator;
101 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
102 typedef std::reverse_iterator<iterator> reverse_iterator;
103
104 /// Value returned by various member functions when they fail.
105 static const size_type npos = static_cast<size_type>(-1);
106
107 private:
108 // type used for positions in insert, erase etc.
109#if __cplusplus201703L < 201103L
110 typedef iterator __const_iterator;
111#else
112 typedef const_iterator __const_iterator;
113#endif
114
115#if __cplusplus201703L > 201402L
116 // A helper type for avoiding boiler-plate.
117 typedef basic_string_view<_CharT, _Traits> __sv_type;
118
119 template<typename _Tp, typename _Res>
120 using _If_sv = enable_if_t<
121 __and_<is_convertible<const _Tp&, __sv_type>,
122 __not_<is_convertible<const _Tp*, const basic_string*>>,
123 __not_<is_convertible<const _Tp&, const _CharT*>>>::value,
124 _Res>;
125
126 // Allows an implicit conversion to __sv_type.
127 static __sv_type
128 _S_to_string_view(__sv_type __svt) noexcept
129 { return __svt; }
130
131 // Wraps a string_view by explicit conversion and thus
132 // allows to add an internal constructor that does not
133 // participate in overload resolution when a string_view
134 // is provided.
135 struct __sv_wrapper
136 {
137 explicit __sv_wrapper(__sv_type __sv) noexcept : _M_sv(__sv) { }
138 __sv_type _M_sv;
139 };
140#endif
141
142 // Use empty-base optimization: http://www.cantrip.org/emptyopt.html
143 struct _Alloc_hider : allocator_type // TODO check __is_final
144 {
145#if __cplusplus201703L < 201103L
146 _Alloc_hider(pointer __dat, const _Alloc& __a = _Alloc())
147 : allocator_type(__a), _M_p(__dat) { }
148#else
149 _Alloc_hider(pointer __dat, const _Alloc& __a)
150 : allocator_type(__a), _M_p(__dat) { }
151
152 _Alloc_hider(pointer __dat, _Alloc&& __a = _Alloc())
153 : allocator_type(std::move(__a)), _M_p(__dat) { }
154#endif
155
156 pointer _M_p; // The actual data.
157 };
158
159 _Alloc_hider _M_dataplus;
160 size_type _M_string_length;
161
162 enum { _S_local_capacity = 15 / sizeof(_CharT) };
163
164 union
165 {
166 _CharT _M_local_buf[_S_local_capacity + 1];
167 size_type _M_allocated_capacity;
168 };
169
170 void
171 _M_data(pointer __p)
172 { _M_dataplus._M_p = __p; }
173
174 void
175 _M_length(size_type __length)
176 { _M_string_length = __length; }
177
178 pointer
179 _M_data() const
180 { return _M_dataplus._M_p; }
181
182 pointer
183 _M_local_data()
184 {
185#if __cplusplus201703L >= 201103L
186 return std::pointer_traits<pointer>::pointer_to(*_M_local_buf);
187#else
188 return pointer(_M_local_buf);
189#endif
190 }
191
192 const_pointer
193 _M_local_data() const
194 {
195#if __cplusplus201703L >= 201103L
196 return std::pointer_traits<const_pointer>::pointer_to(*_M_local_buf);
197#else
198 return const_pointer(_M_local_buf);
199#endif
200 }
201
202 void
203 _M_capacity(size_type __capacity)
204 { _M_allocated_capacity = __capacity; }
205
206 void
207 _M_set_length(size_type __n)
208 {
209 _M_length(__n);
210 traits_type::assign(_M_data()[__n], _CharT());
211 }
212
213 bool
214 _M_is_local() const
215 { return _M_data() == _M_local_data(); }
216
217 // Create & Destroy
218 pointer
219 _M_create(size_type&, size_type);
220
221 void
222 _M_dispose()
223 {
224 if (!_M_is_local())
225 _M_destroy(_M_allocated_capacity);
226 }
227
228 void
229 _M_destroy(size_type __size) throw()
230 { _Alloc_traits::deallocate(_M_get_allocator(), _M_data(), __size + 1); }
231
232 // _M_construct_aux is used to implement the 21.3.1 para 15 which
233 // requires special behaviour if _InIterator is an integral type
234 template<typename _InIterator>
235 void
236 _M_construct_aux(_InIterator __beg, _InIterator __end,
237 std::__false_type)
238 {
239 typedef typename iterator_traits<_InIterator>::iterator_category _Tag;
240 _M_construct(__beg, __end, _Tag());
241 }
242
243 // _GLIBCXX_RESOLVE_LIB_DEFECTS
244 // 438. Ambiguity in the "do the right thing" clause
245 template<typename _Integer>
246 void
247 _M_construct_aux(_Integer __beg, _Integer __end, std::__true_type)
248 { _M_construct_aux_2(static_cast<size_type>(__beg), __end); }
249
250 void
251 _M_construct_aux_2(size_type __req, _CharT __c)
252 { _M_construct(__req, __c); }
253
254 template<typename _InIterator>
255 void
256 _M_construct(_InIterator __beg, _InIterator __end)
257 {
258 typedef typename std::__is_integer<_InIterator>::__type _Integral;
259 _M_construct_aux(__beg, __end, _Integral());
260 }
261
262 // For Input Iterators, used in istreambuf_iterators, etc.
263 template<typename _InIterator>
264 void
265 _M_construct(_InIterator __beg, _InIterator __end,
266 std::input_iterator_tag);
267
268 // For forward_iterators up to random_access_iterators, used for
269 // string::iterator, _CharT*, etc.
270 template<typename _FwdIterator>
271 void
272 _M_construct(_FwdIterator __beg, _FwdIterator __end,
273 std::forward_iterator_tag);
274
275 void
276 _M_construct(size_type __req, _CharT __c);
277
278 allocator_type&
279 _M_get_allocator()
280 { return _M_dataplus; }
281
282 const allocator_type&
283 _M_get_allocator() const
284 { return _M_dataplus; }
285
286 private:
287
288#ifdef _GLIBCXX_DISAMBIGUATE_REPLACE_INST
289 // The explicit instantiations in misc-inst.cc require this due to
290 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=64063
291 template<typename _Tp, bool _Requires =
292 !__are_same<_Tp, _CharT*>::__value
293 && !__are_same<_Tp, const _CharT*>::__value
294 && !__are_same<_Tp, iterator>::__value
295 && !__are_same<_Tp, const_iterator>::__value>
296 struct __enable_if_not_native_iterator
297 { typedef basic_string& __type; };
298 template<typename _Tp>
299 struct __enable_if_not_native_iterator<_Tp, false> { };
300#endif
301
302 size_type
303 _M_check(size_type __pos, const char* __s) const
304 {
305 if (__pos > this->size())
306 __throw_out_of_range_fmt(__N("%s: __pos (which is %zu) > "("%s: __pos (which is %zu) > " "this->size() (which is %zu)"
)
307 "this->size() (which is %zu)")("%s: __pos (which is %zu) > " "this->size() (which is %zu)"
)
,
308 __s, __pos, this->size());
309 return __pos;
310 }
311
312 void
313 _M_check_length(size_type __n1, size_type __n2, const char* __s) const
314 {
315 if (this->max_size() - (this->size() - __n1) < __n2)
316 __throw_length_error(__N(__s)(__s));
317 }
318
319
320 // NB: _M_limit doesn't check for a bad __pos value.
321 size_type
322 _M_limit(size_type __pos, size_type __off) const _GLIBCXX_NOEXCEPTnoexcept
323 {
324 const bool __testoff = __off < this->size() - __pos;
325 return __testoff ? __off : this->size() - __pos;
326 }
327
328 // True if _Rep and source do not overlap.
329 bool
330 _M_disjunct(const _CharT* __s) const _GLIBCXX_NOEXCEPTnoexcept
331 {
332 return (less<const _CharT*>()(__s, _M_data())
333 || less<const _CharT*>()(_M_data() + this->size(), __s));
334 }
335
336 // When __n = 1 way faster than the general multichar
337 // traits_type::copy/move/assign.
338 static void
339 _S_copy(_CharT* __d, const _CharT* __s, size_type __n)
340 {
341 if (__n == 1)
342 traits_type::assign(*__d, *__s);
343 else
344 traits_type::copy(__d, __s, __n);
345 }
346
347 static void
348 _S_move(_CharT* __d, const _CharT* __s, size_type __n)
349 {
350 if (__n == 1)
351 traits_type::assign(*__d, *__s);
352 else
353 traits_type::move(__d, __s, __n);
354 }
355
356 static void
357 _S_assign(_CharT* __d, size_type __n, _CharT __c)
358 {
359 if (__n == 1)
360 traits_type::assign(*__d, __c);
361 else
362 traits_type::assign(__d, __n, __c);
363 }
364
365 // _S_copy_chars is a separate template to permit specialization
366 // to optimize for the common case of pointers as iterators.
367 template<class _Iterator>
368 static void
369 _S_copy_chars(_CharT* __p, _Iterator __k1, _Iterator __k2)
370 {
371 for (; __k1 != __k2; ++__k1, (void)++__p)
372 traits_type::assign(*__p, *__k1); // These types are off.
373 }
374
375 static void
376 _S_copy_chars(_CharT* __p, iterator __k1, iterator __k2) _GLIBCXX_NOEXCEPTnoexcept
377 { _S_copy_chars(__p, __k1.base(), __k2.base()); }
378
379 static void
380 _S_copy_chars(_CharT* __p, const_iterator __k1, const_iterator __k2)
381 _GLIBCXX_NOEXCEPTnoexcept
382 { _S_copy_chars(__p, __k1.base(), __k2.base()); }
383
384 static void
385 _S_copy_chars(_CharT* __p, _CharT* __k1, _CharT* __k2) _GLIBCXX_NOEXCEPTnoexcept
386 { _S_copy(__p, __k1, __k2 - __k1); }
387
388 static void
389 _S_copy_chars(_CharT* __p, const _CharT* __k1, const _CharT* __k2)
390 _GLIBCXX_NOEXCEPTnoexcept
391 { _S_copy(__p, __k1, __k2 - __k1); }
392
393 static int
394 _S_compare(size_type __n1, size_type __n2) _GLIBCXX_NOEXCEPTnoexcept
395 {
396 const difference_type __d = difference_type(__n1 - __n2);
397
398 if (__d > __gnu_cxx::__numeric_traits<int>::__max)
399 return __gnu_cxx::__numeric_traits<int>::__max;
400 else if (__d < __gnu_cxx::__numeric_traits<int>::__min)
401 return __gnu_cxx::__numeric_traits<int>::__min;
402 else
403 return int(__d);
404 }
405
406 void
407 _M_assign(const basic_string&);
408
409 void
410 _M_mutate(size_type __pos, size_type __len1, const _CharT* __s,
411 size_type __len2);
412
413 void
414 _M_erase(size_type __pos, size_type __n);
415
416 public:
417 // Construct/copy/destroy:
418 // NB: We overload ctors in some cases instead of using default
419 // arguments, per 17.4.4.4 para. 2 item 2.
420
421 /**
422 * @brief Default constructor creates an empty string.
423 */
424 basic_string()
425 _GLIBCXX_NOEXCEPT_IF(is_nothrow_default_constructible<_Alloc>::value)noexcept(is_nothrow_default_constructible<_Alloc>::value
)
426 : _M_dataplus(_M_local_data())
427 { _M_set_length(0); }
428
429 /**
430 * @brief Construct an empty string using allocator @a a.
431 */
432 explicit
433 basic_string(const _Alloc& __a) _GLIBCXX_NOEXCEPTnoexcept
434 : _M_dataplus(_M_local_data(), __a)
435 { _M_set_length(0); }
436
437 /**
438 * @brief Construct string with copy of value of @a __str.
439 * @param __str Source string.
440 */
441 basic_string(const basic_string& __str)
442 : _M_dataplus(_M_local_data(),
443 _Alloc_traits::_S_select_on_copy(__str._M_get_allocator()))
444 { _M_construct(__str._M_data(), __str._M_data() + __str.length()); }
445
446 // _GLIBCXX_RESOLVE_LIB_DEFECTS
447 // 2583. no way to supply an allocator for basic_string(str, pos)
448 /**
449 * @brief Construct string as copy of a substring.
450 * @param __str Source string.
451 * @param __pos Index of first character to copy from.
452 * @param __a Allocator to use.
453 */
454 basic_string(const basic_string& __str, size_type __pos,
455 const _Alloc& __a = _Alloc())
456 : _M_dataplus(_M_local_data(), __a)
457 {
458 const _CharT* __start = __str._M_data()
459 + __str._M_check(__pos, "basic_string::basic_string");
460 _M_construct(__start, __start + __str._M_limit(__pos, npos));
461 }
462
463 /**
464 * @brief Construct string as copy of a substring.
465 * @param __str Source string.
466 * @param __pos Index of first character to copy from.
467 * @param __n Number of characters to copy.
468 */
469 basic_string(const basic_string& __str, size_type __pos,
470 size_type __n)
471 : _M_dataplus(_M_local_data())
472 {
473 const _CharT* __start = __str._M_data()
474 + __str._M_check(__pos, "basic_string::basic_string");
475 _M_construct(__start, __start + __str._M_limit(__pos, __n));
476 }
477
478 /**
479 * @brief Construct string as copy of a substring.
480 * @param __str Source string.
481 * @param __pos Index of first character to copy from.
482 * @param __n Number of characters to copy.
483 * @param __a Allocator to use.
484 */
485 basic_string(const basic_string& __str, size_type __pos,
486 size_type __n, const _Alloc& __a)
487 : _M_dataplus(_M_local_data(), __a)
488 {
489 const _CharT* __start
490 = __str._M_data() + __str._M_check(__pos, "string::string");
491 _M_construct(__start, __start + __str._M_limit(__pos, __n));
492 }
493
494 /**
495 * @brief Construct string initialized by a character %array.
496 * @param __s Source character %array.
497 * @param __n Number of characters to copy.
498 * @param __a Allocator to use (default is default allocator).
499 *
500 * NB: @a __s must have at least @a __n characters, &apos;\\0&apos;
501 * has no special meaning.
502 */
503 basic_string(const _CharT* __s, size_type __n,
504 const _Alloc& __a = _Alloc())
505 : _M_dataplus(_M_local_data(), __a)
506 { _M_construct(__s, __s + __n); }
507
508 /**
509 * @brief Construct string as copy of a C string.
510 * @param __s Source C string.
511 * @param __a Allocator to use (default is default allocator).
512 */
513#if __cpp_deduction_guides201703L && ! defined _GLIBCXX_DEFINING_STRING_INSTANTIATIONS
514 // _GLIBCXX_RESOLVE_LIB_DEFECTS
515 // 3076. basic_string CTAD ambiguity
516 template<typename = _RequireAllocator<_Alloc>>
517#endif
518 basic_string(const _CharT* __s, const _Alloc& __a = _Alloc())
519 : _M_dataplus(_M_local_data(), __a)
520 { _M_construct(__s, __s ? __s + traits_type::length(__s) : __s+npos); }
521
522 /**
523 * @brief Construct string as multiple characters.
524 * @param __n Number of characters.
525 * @param __c Character to use.
526 * @param __a Allocator to use (default is default allocator).
527 */
528#if __cpp_deduction_guides201703L && ! defined _GLIBCXX_DEFINING_STRING_INSTANTIATIONS
529 // _GLIBCXX_RESOLVE_LIB_DEFECTS
530 // 3076. basic_string CTAD ambiguity
531 template<typename = _RequireAllocator<_Alloc>>
532#endif
533 basic_string(size_type __n, _CharT __c, const _Alloc& __a = _Alloc())
534 : _M_dataplus(_M_local_data(), __a)
535 { _M_construct(__n, __c); }
536
537#if __cplusplus201703L >= 201103L
538 /**
539 * @brief Move construct string.
540 * @param __str Source string.
541 *
542 * The newly-created string contains the exact contents of @a __str.
543 * @a __str is a valid, but unspecified string.
544 **/
545 basic_string(basic_string&& __str) noexcept
546 : _M_dataplus(_M_local_data(), std::move(__str._M_get_allocator()))
547 {
548 if (__str._M_is_local())
549 {
550 traits_type::copy(_M_local_buf, __str._M_local_buf,
551 _S_local_capacity + 1);
552 }
553 else
554 {
555 _M_data(__str._M_data());
556 _M_capacity(__str._M_allocated_capacity);
557 }
558
559 // Must use _M_length() here not _M_set_length() because
560 // basic_stringbuf relies on writing into unallocated capacity so
561 // we mess up the contents if we put a '\0' in the string.
562 _M_length(__str.length());
563 __str._M_data(__str._M_local_data());
564 __str._M_set_length(0);
565 }
566
567 /**
568 * @brief Construct string from an initializer %list.
569 * @param __l std::initializer_list of characters.
570 * @param __a Allocator to use (default is default allocator).
571 */
572 basic_string(initializer_list<_CharT> __l, const _Alloc& __a = _Alloc())
573 : _M_dataplus(_M_local_data(), __a)
574 { _M_construct(__l.begin(), __l.end()); }
575
576 basic_string(const basic_string& __str, const _Alloc& __a)
577 : _M_dataplus(_M_local_data(), __a)
578 { _M_construct(__str.begin(), __str.end()); }
579
580 basic_string(basic_string&& __str, const _Alloc& __a)
581 noexcept(_Alloc_traits::_S_always_equal())
582 : _M_dataplus(_M_local_data(), __a)
583 {
584 if (__str._M_is_local())
585 {
586 traits_type::copy(_M_local_buf, __str._M_local_buf,
587 _S_local_capacity + 1);
588 _M_length(__str.length());
589 __str._M_set_length(0);
590 }
591 else if (_Alloc_traits::_S_always_equal()
592 || __str.get_allocator() == __a)
593 {
594 _M_data(__str._M_data());
595 _M_length(__str.length());
596 _M_capacity(__str._M_allocated_capacity);
597 __str._M_data(__str._M_local_buf);
598 __str._M_set_length(0);
599 }
600 else
601 _M_construct(__str.begin(), __str.end());
602 }
603
604#endif // C++11
605
606 /**
607 * @brief Construct string as copy of a range.
608 * @param __beg Start of range.
609 * @param __end End of range.
610 * @param __a Allocator to use (default is default allocator).
611 */
612#if __cplusplus201703L >= 201103L
613 template<typename _InputIterator,
614 typename = std::_RequireInputIter<_InputIterator>>
615#else
616 template<typename _InputIterator>
617#endif
618 basic_string(_InputIterator __beg, _InputIterator __end,
619 const _Alloc& __a = _Alloc())
620 : _M_dataplus(_M_local_data(), __a)
621 { _M_construct(__beg, __end); }
622
623#if __cplusplus201703L > 201402L
624 /**
625 * @brief Construct string from a substring of a string_view.
626 * @param __t Source object convertible to string view.
627 * @param __pos The index of the first character to copy from __t.
628 * @param __n The number of characters to copy from __t.
629 * @param __a Allocator to use.
630 */
631 template<typename _Tp, typename = _If_sv<_Tp, void>>
632 basic_string(const _Tp& __t, size_type __pos, size_type __n,
633 const _Alloc& __a = _Alloc())
634 : basic_string(_S_to_string_view(__t).substr(__pos, __n), __a) { }
635
636 /**
637 * @brief Construct string from a string_view.
638 * @param __t Source object convertible to string view.
639 * @param __a Allocator to use (default is default allocator).
640 */
641 template<typename _Tp, typename = _If_sv<_Tp, void>>
642 explicit
643 basic_string(const _Tp& __t, const _Alloc& __a = _Alloc())
644 : basic_string(__sv_wrapper(_S_to_string_view(__t)), __a) { }
645
646 /**
647 * @brief Only internally used: Construct string from a string view
648 * wrapper.
649 * @param __svw string view wrapper.
650 * @param __a Allocator to use.
651 */
652 explicit
653 basic_string(__sv_wrapper __svw, const _Alloc& __a)
654 : basic_string(__svw._M_sv.data(), __svw._M_sv.size(), __a) { }
655#endif // C++17
656
657 /**
658 * @brief Destroy the string instance.
659 */
660 ~basic_string()
661 { _M_dispose(); }
662
663 /**
664 * @brief Assign the value of @a str to this string.
665 * @param __str Source string.
666 */
667 basic_string&
668 operator=(const basic_string& __str)
669 {
670#if __cplusplus201703L >= 201103L
671 if (_Alloc_traits::_S_propagate_on_copy_assign())
672 {
673 if (!_Alloc_traits::_S_always_equal() && !_M_is_local()
674 && _M_get_allocator() != __str._M_get_allocator())
675 {
676 // Propagating allocator cannot free existing storage so must
677 // deallocate it before replacing current allocator.
678 if (__str.size() <= _S_local_capacity)
679 {
680 _M_destroy(_M_allocated_capacity);
681 _M_data(_M_local_data());
682 _M_set_length(0);
683 }
684 else
685 {
686 const auto __len = __str.size();
687 auto __alloc = __str._M_get_allocator();
688 // If this allocation throws there are no effects:
689 auto __ptr = _Alloc_traits::allocate(__alloc, __len + 1);
690 _M_destroy(_M_allocated_capacity);
691 _M_data(__ptr);
692 _M_capacity(__len);
693 _M_set_length(__len);
694 }
695 }
696 std::__alloc_on_copy(_M_get_allocator(), __str._M_get_allocator());
697 }
698#endif
699 return this->assign(__str);
700 }
701
702 /**
703 * @brief Copy contents of @a s into this string.
704 * @param __s Source null-terminated string.
705 */
706 basic_string&
707 operator=(const _CharT* __s)
708 { return this->assign(__s); }
709
710 /**
711 * @brief Set value to string of length 1.
712 * @param __c Source character.
713 *
714 * Assigning to a character makes this string length 1 and
715 * (*this)[0] == @a c.
716 */
717 basic_string&
718 operator=(_CharT __c)
719 {
720 this->assign(1, __c);
721 return *this;
722 }
723
724#if __cplusplus201703L >= 201103L
725 /**
726 * @brief Move assign the value of @a str to this string.
727 * @param __str Source string.
728 *
729 * The contents of @a str are moved into this string (without copying).
730 * @a str is a valid, but unspecified string.
731 **/
732 // PR 58265, this should be noexcept.
733 // _GLIBCXX_RESOLVE_LIB_DEFECTS
734 // 2063. Contradictory requirements for string move assignment
735 basic_string&
736 operator=(basic_string&& __str)
737 noexcept(_Alloc_traits::_S_nothrow_move())
738 {
739 if (!_M_is_local() && _Alloc_traits::_S_propagate_on_move_assign()
740 && !_Alloc_traits::_S_always_equal()
741 && _M_get_allocator() != __str._M_get_allocator())
742 {
743 // Destroy existing storage before replacing allocator.
744 _M_destroy(_M_allocated_capacity);
745 _M_data(_M_local_data());
746 _M_set_length(0);
747 }
748 // Replace allocator if POCMA is true.
749 std::__alloc_on_move(_M_get_allocator(), __str._M_get_allocator());
750
751 if (__str._M_is_local())
752 {
753 // We've always got room for a short string, just copy it.
754 if (__str.size())
755 this->_S_copy(_M_data(), __str._M_data(), __str.size());
756 _M_set_length(__str.size());
757 }
758 else if (_Alloc_traits::_S_propagate_on_move_assign()
759 || _Alloc_traits::_S_always_equal()
760 || _M_get_allocator() == __str._M_get_allocator())
761 {
762 // Just move the allocated pointer, our allocator can free it.
763 pointer __data = nullptr;
764 size_type __capacity;
765 if (!_M_is_local())
766 {
767 if (_Alloc_traits::_S_always_equal())
768 {
769 // __str can reuse our existing storage.
770 __data = _M_data();
771 __capacity = _M_allocated_capacity;
772 }
773 else // __str can't use it, so free it.
774 _M_destroy(_M_allocated_capacity);
775 }
776
777 _M_data(__str._M_data());
778 _M_length(__str.length());
779 _M_capacity(__str._M_allocated_capacity);
780 if (__data)
781 {
782 __str._M_data(__data);
783 __str._M_capacity(__capacity);
784 }
785 else
786 __str._M_data(__str._M_local_buf);
787 }
788 else // Need to do a deep copy
789 assign(__str);
790 __str.clear();
791 return *this;
792 }
793
794 /**
795 * @brief Set value to string constructed from initializer %list.
796 * @param __l std::initializer_list.
797 */
798 basic_string&
799 operator=(initializer_list<_CharT> __l)
800 {
801 this->assign(__l.begin(), __l.size());
802 return *this;
803 }
804#endif // C++11
805
806#if __cplusplus201703L > 201402L
807 /**
808 * @brief Set value to string constructed from a string_view.
809 * @param __svt An object convertible to string_view.
810 */
811 template<typename _Tp>
812 _If_sv<_Tp, basic_string&>
813 operator=(const _Tp& __svt)
814 { return this->assign(__svt); }
815
816 /**
817 * @brief Convert to a string_view.
818 * @return A string_view.
819 */
820 operator __sv_type() const noexcept
821 { return __sv_type(data(), size()); }
822#endif // C++17
823
824 // Iterators:
825 /**
826 * Returns a read/write iterator that points to the first character in
827 * the %string.
828 */
829 iterator
830 begin() _GLIBCXX_NOEXCEPTnoexcept
831 { return iterator(_M_data()); }
832
833 /**
834 * Returns a read-only (constant) iterator that points to the first
835 * character in the %string.
836 */
837 const_iterator
838 begin() const _GLIBCXX_NOEXCEPTnoexcept
839 { return const_iterator(_M_data()); }
840
841 /**
842 * Returns a read/write iterator that points one past the last
843 * character in the %string.
844 */
845 iterator
846 end() _GLIBCXX_NOEXCEPTnoexcept
847 { return iterator(_M_data() + this->size()); }
848
849 /**
850 * Returns a read-only (constant) iterator that points one past the
851 * last character in the %string.
852 */
853 const_iterator
854 end() const _GLIBCXX_NOEXCEPTnoexcept
855 { return const_iterator(_M_data() + this->size()); }
856
857 /**
858 * Returns a read/write reverse iterator that points to the last
859 * character in the %string. Iteration is done in reverse element
860 * order.
861 */
862 reverse_iterator
863 rbegin() _GLIBCXX_NOEXCEPTnoexcept
864 { return reverse_iterator(this->end()); }
865
866 /**
867 * Returns a read-only (constant) reverse iterator that points
868 * to the last character in the %string. Iteration is done in
869 * reverse element order.
870 */
871 const_reverse_iterator
872 rbegin() const _GLIBCXX_NOEXCEPTnoexcept
873 { return const_reverse_iterator(this->end()); }
874
875 /**
876 * Returns a read/write reverse iterator that points to one before the
877 * first character in the %string. Iteration is done in reverse
878 * element order.
879 */
880 reverse_iterator
881 rend() _GLIBCXX_NOEXCEPTnoexcept
882 { return reverse_iterator(this->begin()); }
883
884 /**
885 * Returns a read-only (constant) reverse iterator that points
886 * to one before the first character in the %string. Iteration
887 * is done in reverse element order.
888 */
889 const_reverse_iterator
890 rend() const _GLIBCXX_NOEXCEPTnoexcept
891 { return const_reverse_iterator(this->begin()); }
892
893#if __cplusplus201703L >= 201103L
894 /**
895 * Returns a read-only (constant) iterator that points to the first
896 * character in the %string.
897 */
898 const_iterator
899 cbegin() const noexcept
900 { return const_iterator(this->_M_data()); }
901
902 /**
903 * Returns a read-only (constant) iterator that points one past the
904 * last character in the %string.
905 */
906 const_iterator
907 cend() const noexcept
908 { return const_iterator(this->_M_data() + this->size()); }
909
910 /**
911 * Returns a read-only (constant) reverse iterator that points
912 * to the last character in the %string. Iteration is done in
913 * reverse element order.
914 */
915 const_reverse_iterator
916 crbegin() const noexcept
917 { return const_reverse_iterator(this->end()); }
918
919 /**
920 * Returns a read-only (constant) reverse iterator that points
921 * to one before the first character in the %string. Iteration
922 * is done in reverse element order.
923 */
924 const_reverse_iterator
925 crend() const noexcept
926 { return const_reverse_iterator(this->begin()); }
927#endif
928
929 public:
930 // Capacity:
931 /// Returns the number of characters in the string, not including any
932 /// null-termination.
933 size_type
934 size() const _GLIBCXX_NOEXCEPTnoexcept
935 { return _M_string_length; }
936
937 /// Returns the number of characters in the string, not including any
938 /// null-termination.
939 size_type
940 length() const _GLIBCXX_NOEXCEPTnoexcept
941 { return _M_string_length; }
942
943 /// Returns the size() of the largest possible %string.
944 size_type
945 max_size() const _GLIBCXX_NOEXCEPTnoexcept
946 { return (_Alloc_traits::max_size(_M_get_allocator()) - 1) / 2; }
947
948 /**
949 * @brief Resizes the %string to the specified number of characters.
950 * @param __n Number of characters the %string should contain.
951 * @param __c Character to fill any new elements.
952 *
953 * This function will %resize the %string to the specified
954 * number of characters. If the number is smaller than the
955 * %string's current size the %string is truncated, otherwise
956 * the %string is extended and new elements are %set to @a __c.
957 */
958 void
959 resize(size_type __n, _CharT __c);
960
961 /**
962 * @brief Resizes the %string to the specified number of characters.
963 * @param __n Number of characters the %string should contain.
964 *
965 * This function will resize the %string to the specified length. If
966 * the new size is smaller than the %string's current size the %string
967 * is truncated, otherwise the %string is extended and new characters
968 * are default-constructed. For basic types such as char, this means
969 * setting them to 0.
970 */
971 void
972 resize(size_type __n)
973 { this->resize(__n, _CharT()); }
974
975#if __cplusplus201703L >= 201103L
976 /// A non-binding request to reduce capacity() to size().
977 void
978 shrink_to_fit() noexcept
979 {
980#if __cpp_exceptions
981 if (capacity() > size())
982 {
983 try
984 { reserve(0); }
985 catch(...)
986 { }
987 }
988#endif
989 }
990#endif
991
992 /**
993 * Returns the total number of characters that the %string can hold
994 * before needing to allocate more memory.
995 */
996 size_type
997 capacity() const _GLIBCXX_NOEXCEPTnoexcept
998 {
999 return _M_is_local() ? size_type(_S_local_capacity)
1000 : _M_allocated_capacity;
1001 }
1002
1003 /**
1004 * @brief Attempt to preallocate enough memory for specified number of
1005 * characters.
1006 * @param __res_arg Number of characters required.
1007 * @throw std::length_error If @a __res_arg exceeds @c max_size().
1008 *
1009 * This function attempts to reserve enough memory for the
1010 * %string to hold the specified number of characters. If the
1011 * number requested is more than max_size(), length_error is
1012 * thrown.
1013 *
1014 * The advantage of this function is that if optimal code is a
1015 * necessity and the user can determine the string length that will be
1016 * required, the user can reserve the memory in %advance, and thus
1017 * prevent a possible reallocation of memory and copying of %string
1018 * data.
1019 */
1020 void
1021 reserve(size_type __res_arg = 0);
1022
1023 /**
1024 * Erases the string, making it empty.
1025 */
1026 void
1027 clear() _GLIBCXX_NOEXCEPTnoexcept
1028 { _M_set_length(0); }
1029
1030 /**
1031 * Returns true if the %string is empty. Equivalent to
1032 * <code>*this == ""</code>.
1033 */
1034 bool
1035 empty() const _GLIBCXX_NOEXCEPTnoexcept
1036 { return this->size() == 0; }
1037
1038 // Element access:
1039 /**
1040 * @brief Subscript access to the data contained in the %string.
1041 * @param __pos The index of the character to access.
1042 * @return Read-only (constant) reference to the character.
1043 *
1044 * This operator allows for easy, array-style, data access.
1045 * Note that data access with this operator is unchecked and
1046 * out_of_range lookups are not defined. (For checked lookups
1047 * see at().)
1048 */
1049 const_reference
1050 operator[] (size_type __pos) const _GLIBCXX_NOEXCEPTnoexcept
1051 {
1052 __glibcxx_assert(__pos <= size());
1053 return _M_data()[__pos];
1054 }
1055
1056 /**
1057 * @brief Subscript access to the data contained in the %string.
1058 * @param __pos The index of the character to access.
1059 * @return Read/write reference to the character.
1060 *
1061 * This operator allows for easy, array-style, data access.
1062 * Note that data access with this operator is unchecked and
1063 * out_of_range lookups are not defined. (For checked lookups
1064 * see at().)
1065 */
1066 reference
1067 operator[](size_type __pos)
1068 {
1069 // Allow pos == size() both in C++98 mode, as v3 extension,
1070 // and in C++11 mode.
1071 __glibcxx_assert(__pos <= size());
1072 // In pedantic mode be strict in C++98 mode.
1073 _GLIBCXX_DEBUG_PEDASSERT(__cplusplus >= 201103L || __pos < size());
1074 return _M_data()[__pos];
1075 }
1076
1077 /**
1078 * @brief Provides access to the data contained in the %string.
1079 * @param __n The index of the character to access.
1080 * @return Read-only (const) reference to the character.
1081 * @throw std::out_of_range If @a n is an invalid index.
1082 *
1083 * This function provides for safer data access. The parameter is
1084 * first checked that it is in the range of the string. The function
1085 * throws out_of_range if the check fails.
1086 */
1087 const_reference
1088 at(size_type __n) const
1089 {
1090 if (__n >= this->size())
1091 __throw_out_of_range_fmt(__N("basic_string::at: __n "("basic_string::at: __n " "(which is %zu) >= this->size() "
"(which is %zu)")
1092 "(which is %zu) >= this->size() "("basic_string::at: __n " "(which is %zu) >= this->size() "
"(which is %zu)")
1093 "(which is %zu)")("basic_string::at: __n " "(which is %zu) >= this->size() "
"(which is %zu)")
,
1094 __n, this->size());
1095 return _M_data()[__n];
1096 }
1097
1098 /**
1099 * @brief Provides access to the data contained in the %string.
1100 * @param __n The index of the character to access.
1101 * @return Read/write reference to the character.
1102 * @throw std::out_of_range If @a n is an invalid index.
1103 *
1104 * This function provides for safer data access. The parameter is
1105 * first checked that it is in the range of the string. The function
1106 * throws out_of_range if the check fails.
1107 */
1108 reference
1109 at(size_type __n)
1110 {
1111 if (__n >= size())
1112 __throw_out_of_range_fmt(__N("basic_string::at: __n "("basic_string::at: __n " "(which is %zu) >= this->size() "
"(which is %zu)")
1113 "(which is %zu) >= this->size() "("basic_string::at: __n " "(which is %zu) >= this->size() "
"(which is %zu)")
1114 "(which is %zu)")("basic_string::at: __n " "(which is %zu) >= this->size() "
"(which is %zu)")
,
1115 __n, this->size());
1116 return _M_data()[__n];
1117 }
1118
1119#if __cplusplus201703L >= 201103L
1120 /**
1121 * Returns a read/write reference to the data at the first
1122 * element of the %string.
1123 */
1124 reference
1125 front() noexcept
1126 {
1127 __glibcxx_assert(!empty());
1128 return operator[](0);
1129 }
1130
1131 /**
1132 * Returns a read-only (constant) reference to the data at the first
1133 * element of the %string.
1134 */
1135 const_reference
1136 front() const noexcept
1137 {
1138 __glibcxx_assert(!empty());
1139 return operator[](0);
1140 }
1141
1142 /**
1143 * Returns a read/write reference to the data at the last
1144 * element of the %string.
1145 */
1146 reference
1147 back() noexcept
1148 {
1149 __glibcxx_assert(!empty());
1150 return operator[](this->size() - 1);
1151 }
1152
1153 /**
1154 * Returns a read-only (constant) reference to the data at the
1155 * last element of the %string.
1156 */
1157 const_reference
1158 back() const noexcept
1159 {
1160 __glibcxx_assert(!empty());
1161 return operator[](this->size() - 1);
1162 }
1163#endif
1164
1165 // Modifiers:
1166 /**
1167 * @brief Append a string to this string.
1168 * @param __str The string to append.
1169 * @return Reference to this string.
1170 */
1171 basic_string&
1172 operator+=(const basic_string& __str)
1173 { return this->append(__str); }
1174
1175 /**
1176 * @brief Append a C string.
1177 * @param __s The C string to append.
1178 * @return Reference to this string.
1179 */
1180 basic_string&
1181 operator+=(const _CharT* __s)
1182 { return this->append(__s); }
1183
1184 /**
1185 * @brief Append a character.
1186 * @param __c The character to append.
1187 * @return Reference to this string.
1188 */
1189 basic_string&
1190 operator+=(_CharT __c)
1191 {
1192 this->push_back(__c);
1193 return *this;
1194 }
1195
1196#if __cplusplus201703L >= 201103L
1197 /**
1198 * @brief Append an initializer_list of characters.
1199 * @param __l The initializer_list of characters to be appended.
1200 * @return Reference to this string.
1201 */
1202 basic_string&
1203 operator+=(initializer_list<_CharT> __l)
1204 { return this->append(__l.begin(), __l.size()); }
1205#endif // C++11
1206
1207#if __cplusplus201703L > 201402L
1208 /**
1209 * @brief Append a string_view.
1210 * @param __svt An object convertible to string_view to be appended.
1211 * @return Reference to this string.
1212 */
1213 template<typename _Tp>
1214 _If_sv<_Tp, basic_string&>
1215 operator+=(const _Tp& __svt)
1216 { return this->append(__svt); }
1217#endif // C++17
1218
1219 /**
1220 * @brief Append a string to this string.
1221 * @param __str The string to append.
1222 * @return Reference to this string.
1223 */
1224 basic_string&
1225 append(const basic_string& __str)
1226 { return _M_append(__str._M_data(), __str.size()); }
1227
1228 /**
1229 * @brief Append a substring.
1230 * @param __str The string to append.
1231 * @param __pos Index of the first character of str to append.
1232 * @param __n The number of characters to append.
1233 * @return Reference to this string.
1234 * @throw std::out_of_range if @a __pos is not a valid index.
1235 *
1236 * This function appends @a __n characters from @a __str
1237 * starting at @a __pos to this string. If @a __n is is larger
1238 * than the number of available characters in @a __str, the
1239 * remainder of @a __str is appended.
1240 */
1241 basic_string&
1242 append(const basic_string& __str, size_type __pos, size_type __n = npos)
1243 { return _M_append(__str._M_data()
1244 + __str._M_check(__pos, "basic_string::append"),
1245 __str._M_limit(__pos, __n)); }
1246
1247 /**
1248 * @brief Append a C substring.
1249 * @param __s The C string to append.
1250 * @param __n The number of characters to append.
1251 * @return Reference to this string.
1252 */
1253 basic_string&
1254 append(const _CharT* __s, size_type __n)
1255 {
1256 __glibcxx_requires_string_len(__s, __n);
1257 _M_check_length(size_type(0), __n, "basic_string::append");
1258 return _M_append(__s, __n);
1259 }
1260
1261 /**
1262 * @brief Append a C string.
1263 * @param __s The C string to append.
1264 * @return Reference to this string.
1265 */
1266 basic_string&
1267 append(const _CharT* __s)
1268 {
1269 __glibcxx_requires_string(__s);
1270 const size_type __n = traits_type::length(__s);
1271 _M_check_length(size_type(0), __n, "basic_string::append");
1272 return _M_append(__s, __n);
1273 }
1274
1275 /**
1276 * @brief Append multiple characters.
1277 * @param __n The number of characters to append.
1278 * @param __c The character to use.
1279 * @return Reference to this string.
1280 *
1281 * Appends __n copies of __c to this string.
1282 */
1283 basic_string&
1284 append(size_type __n, _CharT __c)
1285 { return _M_replace_aux(this->size(), size_type(0), __n, __c); }
1286
1287#if __cplusplus201703L >= 201103L
1288 /**
1289 * @brief Append an initializer_list of characters.
1290 * @param __l The initializer_list of characters to append.
1291 * @return Reference to this string.
1292 */
1293 basic_string&
1294 append(initializer_list<_CharT> __l)
1295 { return this->append(__l.begin(), __l.size()); }
1296#endif // C++11
1297
1298 /**
1299 * @brief Append a range of characters.
1300 * @param __first Iterator referencing the first character to append.
1301 * @param __last Iterator marking the end of the range.
1302 * @return Reference to this string.
1303 *
1304 * Appends characters in the range [__first,__last) to this string.
1305 */
1306#if __cplusplus201703L >= 201103L
1307 template<class _InputIterator,
1308 typename = std::_RequireInputIter<_InputIterator>>
1309#else
1310 template<class _InputIterator>
1311#endif
1312 basic_string&
1313 append(_InputIterator __first, _InputIterator __last)
1314 { return this->replace(end(), end(), __first, __last); }
1315
1316#if __cplusplus201703L > 201402L
1317 /**
1318 * @brief Append a string_view.
1319 * @param __svt An object convertible to string_view to be appended.
1320 * @return Reference to this string.
1321 */
1322 template<typename _Tp>
1323 _If_sv<_Tp, basic_string&>
1324 append(const _Tp& __svt)
1325 {
1326 __sv_type __sv = __svt;
1327 return this->append(__sv.data(), __sv.size());
1328 }
1329
1330 /**
1331 * @brief Append a range of characters from a string_view.
1332 * @param __svt An object convertible to string_view to be appended from.
1333 * @param __pos The position in the string_view to append from.
1334 * @param __n The number of characters to append from the string_view.
1335 * @return Reference to this string.
1336 */
1337 template<typename _Tp>
1338 _If_sv<_Tp, basic_string&>
1339 append(const _Tp& __svt, size_type __pos, size_type __n = npos)
1340 {
1341 __sv_type __sv = __svt;
1342 return _M_append(__sv.data()
1343 + __sv._M_check(__pos, "basic_string::append"),
1344 __sv._M_limit(__pos, __n));
1345 }
1346#endif // C++17
1347
1348 /**
1349 * @brief Append a single character.
1350 * @param __c Character to append.
1351 */
1352 void
1353 push_back(_CharT __c)
1354 {
1355 const size_type __size = this->size();
1356 if (__size + 1 > this->capacity())
1357 this->_M_mutate(__size, size_type(0), 0, size_type(1));
1358 traits_type::assign(this->_M_data()[__size], __c);
1359 this->_M_set_length(__size + 1);
1360 }
1361
1362 /**
1363 * @brief Set value to contents of another string.
1364 * @param __str Source string to use.
1365 * @return Reference to this string.
1366 */
1367 basic_string&
1368 assign(const basic_string& __str)
1369 {
1370 this->_M_assign(__str);
1371 return *this;
1372 }
1373
1374#if __cplusplus201703L >= 201103L
1375 /**
1376 * @brief Set value to contents of another string.
1377 * @param __str Source string to use.
1378 * @return Reference to this string.
1379 *
1380 * This function sets this string to the exact contents of @a __str.
1381 * @a __str is a valid, but unspecified string.
1382 */
1383 basic_string&
1384 assign(basic_string&& __str)
1385 noexcept(_Alloc_traits::_S_nothrow_move())
1386 {
1387 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1388 // 2063. Contradictory requirements for string move assignment
1389 return *this = std::move(__str);
1390 }
1391#endif // C++11
1392
1393 /**
1394 * @brief Set value to a substring of a string.
1395 * @param __str The string to use.
1396 * @param __pos Index of the first character of str.
1397 * @param __n Number of characters to use.
1398 * @return Reference to this string.
1399 * @throw std::out_of_range if @a pos is not a valid index.
1400 *
1401 * This function sets this string to the substring of @a __str
1402 * consisting of @a __n characters at @a __pos. If @a __n is
1403 * is larger than the number of available characters in @a
1404 * __str, the remainder of @a __str is used.
1405 */
1406 basic_string&
1407 assign(const basic_string& __str, size_type __pos, size_type __n = npos)
1408 { return _M_replace(size_type(0), this->size(), __str._M_data()
1409 + __str._M_check(__pos, "basic_string::assign"),
1410 __str._M_limit(__pos, __n)); }
1411
1412 /**
1413 * @brief Set value to a C substring.
1414 * @param __s The C string to use.
1415 * @param __n Number of characters to use.
1416 * @return Reference to this string.
1417 *
1418 * This function sets the value of this string to the first @a __n
1419 * characters of @a __s. If @a __n is is larger than the number of
1420 * available characters in @a __s, the remainder of @a __s is used.
1421 */
1422 basic_string&
1423 assign(const _CharT* __s, size_type __n)
1424 {
1425 __glibcxx_requires_string_len(__s, __n);
1426 return _M_replace(size_type(0), this->size(), __s, __n);
1427 }
1428
1429 /**
1430 * @brief Set value to contents of a C string.
1431 * @param __s The C string to use.
1432 * @return Reference to this string.
1433 *
1434 * This function sets the value of this string to the value of @a __s.
1435 * The data is copied, so there is no dependence on @a __s once the
1436 * function returns.
1437 */
1438 basic_string&
1439 assign(const _CharT* __s)
1440 {
1441 __glibcxx_requires_string(__s);
1442 return _M_replace(size_type(0), this->size(), __s,
1443 traits_type::length(__s));
1444 }
1445
1446 /**
1447 * @brief Set value to multiple characters.
1448 * @param __n Length of the resulting string.
1449 * @param __c The character to use.
1450 * @return Reference to this string.
1451 *
1452 * This function sets the value of this string to @a __n copies of
1453 * character @a __c.
1454 */
1455 basic_string&
1456 assign(size_type __n, _CharT __c)
1457 { return _M_replace_aux(size_type(0), this->size(), __n, __c); }
1458
1459 /**
1460 * @brief Set value to a range of characters.
1461 * @param __first Iterator referencing the first character to append.
1462 * @param __last Iterator marking the end of the range.
1463 * @return Reference to this string.
1464 *
1465 * Sets value of string to characters in the range [__first,__last).
1466 */
1467#if __cplusplus201703L >= 201103L
1468 template<class _InputIterator,
1469 typename = std::_RequireInputIter<_InputIterator>>
1470#else
1471 template<class _InputIterator>
1472#endif
1473 basic_string&
1474 assign(_InputIterator __first, _InputIterator __last)
1475 { return this->replace(begin(), end(), __first, __last); }
1476
1477#if __cplusplus201703L >= 201103L
1478 /**
1479 * @brief Set value to an initializer_list of characters.
1480 * @param __l The initializer_list of characters to assign.
1481 * @return Reference to this string.
1482 */
1483 basic_string&
1484 assign(initializer_list<_CharT> __l)
1485 { return this->assign(__l.begin(), __l.size()); }
1486#endif // C++11
1487
1488#if __cplusplus201703L > 201402L
1489 /**
1490 * @brief Set value from a string_view.
1491 * @param __svt The source object convertible to string_view.
1492 * @return Reference to this string.
1493 */
1494 template<typename _Tp>
1495 _If_sv<_Tp, basic_string&>
1496 assign(const _Tp& __svt)
1497 {
1498 __sv_type __sv = __svt;
1499 return this->assign(__sv.data(), __sv.size());
1500 }
1501
1502 /**
1503 * @brief Set value from a range of characters in a string_view.
1504 * @param __svt The source object convertible to string_view.
1505 * @param __pos The position in the string_view to assign from.
1506 * @param __n The number of characters to assign.
1507 * @return Reference to this string.
1508 */
1509 template<typename _Tp>
1510 _If_sv<_Tp, basic_string&>
1511 assign(const _Tp& __svt, size_type __pos, size_type __n = npos)
1512 {
1513 __sv_type __sv = __svt;
1514 return _M_replace(size_type(0), this->size(), __sv.data()
1515 + __sv._M_check(__pos, "basic_string::assign"),
1516 __sv._M_limit(__pos, __n));
1517 }
1518#endif // C++17
1519
1520#if __cplusplus201703L >= 201103L
1521 /**
1522 * @brief Insert multiple characters.
1523 * @param __p Const_iterator referencing location in string to
1524 * insert at.
1525 * @param __n Number of characters to insert
1526 * @param __c The character to insert.
1527 * @return Iterator referencing the first inserted char.
1528 * @throw std::length_error If new length exceeds @c max_size().
1529 *
1530 * Inserts @a __n copies of character @a __c starting at the
1531 * position referenced by iterator @a __p. If adding
1532 * characters causes the length to exceed max_size(),
1533 * length_error is thrown. The value of the string doesn't
1534 * change if an error is thrown.
1535 */
1536 iterator
1537 insert(const_iterator __p, size_type __n, _CharT __c)
1538 {
1539 _GLIBCXX_DEBUG_PEDASSERT(__p >= begin() && __p <= end());
1540 const size_type __pos = __p - begin();
1541 this->replace(__p, __p, __n, __c);
1542 return iterator(this->_M_data() + __pos);
1543 }
1544#else
1545 /**
1546 * @brief Insert multiple characters.
1547 * @param __p Iterator referencing location in string to insert at.
1548 * @param __n Number of characters to insert
1549 * @param __c The character to insert.
1550 * @throw std::length_error If new length exceeds @c max_size().
1551 *
1552 * Inserts @a __n copies of character @a __c starting at the
1553 * position referenced by iterator @a __p. If adding
1554 * characters causes the length to exceed max_size(),
1555 * length_error is thrown. The value of the string doesn't
1556 * change if an error is thrown.
1557 */
1558 void
1559 insert(iterator __p, size_type __n, _CharT __c)
1560 { this->replace(__p, __p, __n, __c); }
1561#endif
1562
1563#if __cplusplus201703L >= 201103L
1564 /**
1565 * @brief Insert a range of characters.
1566 * @param __p Const_iterator referencing location in string to
1567 * insert at.
1568 * @param __beg Start of range.
1569 * @param __end End of range.
1570 * @return Iterator referencing the first inserted char.
1571 * @throw std::length_error If new length exceeds @c max_size().
1572 *
1573 * Inserts characters in range [beg,end). If adding characters
1574 * causes the length to exceed max_size(), length_error is
1575 * thrown. The value of the string doesn't change if an error
1576 * is thrown.
1577 */
1578 template<class _InputIterator,
1579 typename = std::_RequireInputIter<_InputIterator>>
1580 iterator
1581 insert(const_iterator __p, _InputIterator __beg, _InputIterator __end)
1582 {
1583 _GLIBCXX_DEBUG_PEDASSERT(__p >= begin() && __p <= end());
1584 const size_type __pos = __p - begin();
1585 this->replace(__p, __p, __beg, __end);
1586 return iterator(this->_M_data() + __pos);
1587 }
1588#else
1589 /**
1590 * @brief Insert a range of characters.
1591 * @param __p Iterator referencing location in string to insert at.
1592 * @param __beg Start of range.
1593 * @param __end End of range.
1594 * @throw std::length_error If new length exceeds @c max_size().
1595 *
1596 * Inserts characters in range [__beg,__end). If adding
1597 * characters causes the length to exceed max_size(),
1598 * length_error is thrown. The value of the string doesn't
1599 * change if an error is thrown.
1600 */
1601 template<class _InputIterator>
1602 void
1603 insert(iterator __p, _InputIterator __beg, _InputIterator __end)
1604 { this->replace(__p, __p, __beg, __end); }
1605#endif
1606
1607#if __cplusplus201703L >= 201103L
1608 /**
1609 * @brief Insert an initializer_list of characters.
1610 * @param __p Iterator referencing location in string to insert at.
1611 * @param __l The initializer_list of characters to insert.
1612 * @throw std::length_error If new length exceeds @c max_size().
1613 */
1614 void
1615 insert(iterator __p, initializer_list<_CharT> __l)
1616 {
1617 _GLIBCXX_DEBUG_PEDASSERT(__p >= begin() && __p <= end());
1618 this->insert(__p - begin(), __l.begin(), __l.size());
1619 }
1620#endif // C++11
1621
1622 /**
1623 * @brief Insert value of a string.
1624 * @param __pos1 Iterator referencing location in string to insert at.
1625 * @param __str The string to insert.
1626 * @return Reference to this string.
1627 * @throw std::length_error If new length exceeds @c max_size().
1628 *
1629 * Inserts value of @a __str starting at @a __pos1. If adding
1630 * characters causes the length to exceed max_size(),
1631 * length_error is thrown. The value of the string doesn't
1632 * change if an error is thrown.
1633 */
1634 basic_string&
1635 insert(size_type __pos1, const basic_string& __str)
1636 { return this->replace(__pos1, size_type(0),
1637 __str._M_data(), __str.size()); }
1638
1639 /**
1640 * @brief Insert a substring.
1641 * @param __pos1 Iterator referencing location in string to insert at.
1642 * @param __str The string to insert.
1643 * @param __pos2 Start of characters in str to insert.
1644 * @param __n Number of characters to insert.
1645 * @return Reference to this string.
1646 * @throw std::length_error If new length exceeds @c max_size().
1647 * @throw std::out_of_range If @a pos1 > size() or
1648 * @a __pos2 > @a str.size().
1649 *
1650 * Starting at @a pos1, insert @a __n character of @a __str
1651 * beginning with @a __pos2. If adding characters causes the
1652 * length to exceed max_size(), length_error is thrown. If @a
1653 * __pos1 is beyond the end of this string or @a __pos2 is
1654 * beyond the end of @a __str, out_of_range is thrown. The
1655 * value of the string doesn't change if an error is thrown.
1656 */
1657 basic_string&
1658 insert(size_type __pos1, const basic_string& __str,
1659 size_type __pos2, size_type __n = npos)
1660 { return this->replace(__pos1, size_type(0), __str._M_data()
1661 + __str._M_check(__pos2, "basic_string::insert"),
1662 __str._M_limit(__pos2, __n)); }
1663
1664 /**
1665 * @brief Insert a C substring.
1666 * @param __pos Iterator referencing location in string to insert at.
1667 * @param __s The C string to insert.
1668 * @param __n The number of characters to insert.
1669 * @return Reference to this string.
1670 * @throw std::length_error If new length exceeds @c max_size().
1671 * @throw std::out_of_range If @a __pos is beyond the end of this
1672 * string.
1673 *
1674 * Inserts the first @a __n characters of @a __s starting at @a
1675 * __pos. If adding characters causes the length to exceed
1676 * max_size(), length_error is thrown. If @a __pos is beyond
1677 * end(), out_of_range is thrown. The value of the string
1678 * doesn't change if an error is thrown.
1679 */
1680 basic_string&
1681 insert(size_type __pos, const _CharT* __s, size_type __n)
1682 { return this->replace(__pos, size_type(0), __s, __n); }
1683
1684 /**
1685 * @brief Insert a C string.
1686 * @param __pos Iterator referencing location in string to insert at.
1687 * @param __s The C string to insert.
1688 * @return Reference to this string.
1689 * @throw std::length_error If new length exceeds @c max_size().
1690 * @throw std::out_of_range If @a pos is beyond the end of this
1691 * string.
1692 *
1693 * Inserts the first @a n characters of @a __s starting at @a __pos. If
1694 * adding characters causes the length to exceed max_size(),
1695 * length_error is thrown. If @a __pos is beyond end(), out_of_range is
1696 * thrown. The value of the string doesn't change if an error is
1697 * thrown.
1698 */
1699 basic_string&
1700 insert(size_type __pos, const _CharT* __s)
1701 {
1702 __glibcxx_requires_string(__s);
1703 return this->replace(__pos, size_type(0), __s,
1704 traits_type::length(__s));
1705 }
1706
1707 /**
1708 * @brief Insert multiple characters.
1709 * @param __pos Index in string to insert at.
1710 * @param __n Number of characters to insert
1711 * @param __c The character to insert.
1712 * @return Reference to this string.
1713 * @throw std::length_error If new length exceeds @c max_size().
1714 * @throw std::out_of_range If @a __pos is beyond the end of this
1715 * string.
1716 *
1717 * Inserts @a __n copies of character @a __c starting at index
1718 * @a __pos. If adding characters causes the length to exceed
1719 * max_size(), length_error is thrown. If @a __pos > length(),
1720 * out_of_range is thrown. The value of the string doesn't
1721 * change if an error is thrown.
1722 */
1723 basic_string&
1724 insert(size_type __pos, size_type __n, _CharT __c)
1725 { return _M_replace_aux(_M_check(__pos, "basic_string::insert"),
1726 size_type(0), __n, __c); }
1727
1728 /**
1729 * @brief Insert one character.
1730 * @param __p Iterator referencing position in string to insert at.
1731 * @param __c The character to insert.
1732 * @return Iterator referencing newly inserted char.
1733 * @throw std::length_error If new length exceeds @c max_size().
1734 *
1735 * Inserts character @a __c at position referenced by @a __p.
1736 * If adding character causes the length to exceed max_size(),
1737 * length_error is thrown. If @a __p is beyond end of string,
1738 * out_of_range is thrown. The value of the string doesn't
1739 * change if an error is thrown.
1740 */
1741 iterator
1742 insert(__const_iterator __p, _CharT __c)
1743 {
1744 _GLIBCXX_DEBUG_PEDASSERT(__p >= begin() && __p <= end());
1745 const size_type __pos = __p - begin();
1746 _M_replace_aux(__pos, size_type(0), size_type(1), __c);
1747 return iterator(_M_data() + __pos);
1748 }
1749
1750#if __cplusplus201703L > 201402L
1751 /**
1752 * @brief Insert a string_view.
1753 * @param __pos Iterator referencing position in string to insert at.
1754 * @param __svt The object convertible to string_view to insert.
1755 * @return Reference to this string.
1756 */
1757 template<typename _Tp>
1758 _If_sv<_Tp, basic_string&>
1759 insert(size_type __pos, const _Tp& __svt)
1760 {
1761 __sv_type __sv = __svt;
1762 return this->insert(__pos, __sv.data(), __sv.size());
1763 }
1764
1765 /**
1766 * @brief Insert a string_view.
1767 * @param __pos Iterator referencing position in string to insert at.
1768 * @param __svt The object convertible to string_view to insert from.
1769 * @param __pos Iterator referencing position in string_view to insert
1770 * from.
1771 * @param __n The number of characters to insert.
1772 * @return Reference to this string.
1773 */
1774 template<typename _Tp>
1775 _If_sv<_Tp, basic_string&>
1776 insert(size_type __pos1, const _Tp& __svt,
1777 size_type __pos2, size_type __n = npos)
1778 {
1779 __sv_type __sv = __svt;
1780 return this->replace(__pos1, size_type(0), __sv.data()
1781 + __sv._M_check(__pos2, "basic_string::insert"),
1782 __sv._M_limit(__pos2, __n));
1783 }
1784#endif // C++17
1785
1786 /**
1787 * @brief Remove characters.
1788 * @param __pos Index of first character to remove (default 0).
1789 * @param __n Number of characters to remove (default remainder).
1790 * @return Reference to this string.
1791 * @throw std::out_of_range If @a pos is beyond the end of this
1792 * string.
1793 *
1794 * Removes @a __n characters from this string starting at @a
1795 * __pos. The length of the string is reduced by @a __n. If
1796 * there are < @a __n characters to remove, the remainder of
1797 * the string is truncated. If @a __p is beyond end of string,
1798 * out_of_range is thrown. The value of the string doesn't
1799 * change if an error is thrown.
1800 */
1801 basic_string&
1802 erase(size_type __pos = 0, size_type __n = npos)
1803 {
1804 _M_check(__pos, "basic_string::erase");
1805 if (__n == npos)
1806 this->_M_set_length(__pos);
1807 else if (__n != 0)
1808 this->_M_erase(__pos, _M_limit(__pos, __n));
1809 return *this;
1810 }
1811
1812 /**
1813 * @brief Remove one character.
1814 * @param __position Iterator referencing the character to remove.
1815 * @return iterator referencing same location after removal.
1816 *
1817 * Removes the character at @a __position from this string. The value
1818 * of the string doesn't change if an error is thrown.
1819 */
1820 iterator
1821 erase(__const_iterator __position)
1822 {
1823 _GLIBCXX_DEBUG_PEDASSERT(__position >= begin()
1824 && __position < end());
1825 const size_type __pos = __position - begin();
1826 this->_M_erase(__pos, size_type(1));
1827 return iterator(_M_data() + __pos);
1828 }
1829
1830 /**
1831 * @brief Remove a range of characters.
1832 * @param __first Iterator referencing the first character to remove.
1833 * @param __last Iterator referencing the end of the range.
1834 * @return Iterator referencing location of first after removal.
1835 *
1836 * Removes the characters in the range [first,last) from this string.
1837 * The value of the string doesn't change if an error is thrown.
1838 */
1839 iterator
1840 erase(__const_iterator __first, __const_iterator __last)
1841 {
1842 _GLIBCXX_DEBUG_PEDASSERT(__first >= begin() && __first <= __last
1843 && __last <= end());
1844 const size_type __pos = __first - begin();
1845 if (__last == end())
1846 this->_M_set_length(__pos);
1847 else
1848 this->_M_erase(__pos, __last - __first);
1849 return iterator(this->_M_data() + __pos);
1850 }
1851
1852#if __cplusplus201703L >= 201103L
1853 /**
1854 * @brief Remove the last character.
1855 *
1856 * The string must be non-empty.
1857 */
1858 void
1859 pop_back() noexcept
1860 {
1861 __glibcxx_assert(!empty());
1862 _M_erase(size() - 1, 1);
1863 }
1864#endif // C++11
1865
1866 /**
1867 * @brief Replace characters with value from another string.
1868 * @param __pos Index of first character to replace.
1869 * @param __n Number of characters to be replaced.
1870 * @param __str String to insert.
1871 * @return Reference to this string.
1872 * @throw std::out_of_range If @a pos is beyond the end of this
1873 * string.
1874 * @throw std::length_error If new length exceeds @c max_size().
1875 *
1876 * Removes the characters in the range [__pos,__pos+__n) from
1877 * this string. In place, the value of @a __str is inserted.
1878 * If @a __pos is beyond end of string, out_of_range is thrown.
1879 * If the length of the result exceeds max_size(), length_error
1880 * is thrown. The value of the string doesn't change if an
1881 * error is thrown.
1882 */
1883 basic_string&
1884 replace(size_type __pos, size_type __n, const basic_string& __str)
1885 { return this->replace(__pos, __n, __str._M_data(), __str.size()); }
1886
1887 /**
1888 * @brief Replace characters with value from another string.
1889 * @param __pos1 Index of first character to replace.
1890 * @param __n1 Number of characters to be replaced.
1891 * @param __str String to insert.
1892 * @param __pos2 Index of first character of str to use.
1893 * @param __n2 Number of characters from str to use.
1894 * @return Reference to this string.
1895 * @throw std::out_of_range If @a __pos1 > size() or @a __pos2 >
1896 * __str.size().
1897 * @throw std::length_error If new length exceeds @c max_size().
1898 *
1899 * Removes the characters in the range [__pos1,__pos1 + n) from this
1900 * string. In place, the value of @a __str is inserted. If @a __pos is
1901 * beyond end of string, out_of_range is thrown. If the length of the
1902 * result exceeds max_size(), length_error is thrown. The value of the
1903 * string doesn't change if an error is thrown.
1904 */
1905 basic_string&
1906 replace(size_type __pos1, size_type __n1, const basic_string& __str,
1907 size_type __pos2, size_type __n2 = npos)
1908 { return this->replace(__pos1, __n1, __str._M_data()
1909 + __str._M_check(__pos2, "basic_string::replace"),
1910 __str._M_limit(__pos2, __n2)); }
1911
1912 /**
1913 * @brief Replace characters with value of a C substring.
1914 * @param __pos Index of first character to replace.
1915 * @param __n1 Number of characters to be replaced.
1916 * @param __s C string to insert.
1917 * @param __n2 Number of characters from @a s to use.
1918 * @return Reference to this string.
1919 * @throw std::out_of_range If @a pos1 > size().
1920 * @throw std::length_error If new length exceeds @c max_size().
1921 *
1922 * Removes the characters in the range [__pos,__pos + __n1)
1923 * from this string. In place, the first @a __n2 characters of
1924 * @a __s are inserted, or all of @a __s if @a __n2 is too large. If
1925 * @a __pos is beyond end of string, out_of_range is thrown. If
1926 * the length of result exceeds max_size(), length_error is
1927 * thrown. The value of the string doesn't change if an error
1928 * is thrown.
1929 */
1930 basic_string&
1931 replace(size_type __pos, size_type __n1, const _CharT* __s,
1932 size_type __n2)
1933 {
1934 __glibcxx_requires_string_len(__s, __n2);
1935 return _M_replace(_M_check(__pos, "basic_string::replace"),
1936 _M_limit(__pos, __n1), __s, __n2);
1937 }
1938
1939 /**
1940 * @brief Replace characters with value of a C string.
1941 * @param __pos Index of first character to replace.
1942 * @param __n1 Number of characters to be replaced.
1943 * @param __s C string to insert.
1944 * @return Reference to this string.
1945 * @throw std::out_of_range If @a pos > size().
1946 * @throw std::length_error If new length exceeds @c max_size().
1947 *
1948 * Removes the characters in the range [__pos,__pos + __n1)
1949 * from this string. In place, the characters of @a __s are
1950 * inserted. If @a __pos is beyond end of string, out_of_range
1951 * is thrown. If the length of result exceeds max_size(),
1952 * length_error is thrown. The value of the string doesn't
1953 * change if an error is thrown.
1954 */
1955 basic_string&
1956 replace(size_type __pos, size_type __n1, const _CharT* __s)
1957 {
1958 __glibcxx_requires_string(__s);
1959 return this->replace(__pos, __n1, __s, traits_type::length(__s));
1960 }
1961
1962 /**
1963 * @brief Replace characters with multiple characters.
1964 * @param __pos Index of first character to replace.
1965 * @param __n1 Number of characters to be replaced.
1966 * @param __n2 Number of characters to insert.
1967 * @param __c Character to insert.
1968 * @return Reference to this string.
1969 * @throw std::out_of_range If @a __pos > size().
1970 * @throw std::length_error If new length exceeds @c max_size().
1971 *
1972 * Removes the characters in the range [pos,pos + n1) from this
1973 * string. In place, @a __n2 copies of @a __c are inserted.
1974 * If @a __pos is beyond end of string, out_of_range is thrown.
1975 * If the length of result exceeds max_size(), length_error is
1976 * thrown. The value of the string doesn't change if an error
1977 * is thrown.
1978 */
1979 basic_string&
1980 replace(size_type __pos, size_type __n1, size_type __n2, _CharT __c)
1981 { return _M_replace_aux(_M_check(__pos, "basic_string::replace"),
1982 _M_limit(__pos, __n1), __n2, __c); }
1983
1984 /**
1985 * @brief Replace range of characters with string.
1986 * @param __i1 Iterator referencing start of range to replace.
1987 * @param __i2 Iterator referencing end of range to replace.
1988 * @param __str String value to insert.
1989 * @return Reference to this string.
1990 * @throw std::length_error If new length exceeds @c max_size().
1991 *
1992 * Removes the characters in the range [__i1,__i2). In place,
1993 * the value of @a __str is inserted. If the length of result
1994 * exceeds max_size(), length_error is thrown. The value of
1995 * the string doesn't change if an error is thrown.
1996 */
1997 basic_string&
1998 replace(__const_iterator __i1, __const_iterator __i2,
1999 const basic_string& __str)
2000 { return this->replace(__i1, __i2, __str._M_data(), __str.size()); }
2001
2002 /**
2003 * @brief Replace range of characters with C substring.
2004 * @param __i1 Iterator referencing start of range to replace.
2005 * @param __i2 Iterator referencing end of range to replace.
2006 * @param __s C string value to insert.
2007 * @param __n Number of characters from s to insert.
2008 * @return Reference to this string.
2009 * @throw std::length_error If new length exceeds @c max_size().
2010 *
2011 * Removes the characters in the range [__i1,__i2). In place,
2012 * the first @a __n characters of @a __s are inserted. If the
2013 * length of result exceeds max_size(), length_error is thrown.
2014 * The value of the string doesn't change if an error is
2015 * thrown.
2016 */
2017 basic_string&
2018 replace(__const_iterator __i1, __const_iterator __i2,
2019 const _CharT* __s, size_type __n)
2020 {
2021 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2
2022 && __i2 <= end());
2023 return this->replace(__i1 - begin(), __i2 - __i1, __s, __n);
2024 }
2025
2026 /**
2027 * @brief Replace range of characters with C string.
2028 * @param __i1 Iterator referencing start of range to replace.
2029 * @param __i2 Iterator referencing end of range to replace.
2030 * @param __s C string value to insert.
2031 * @return Reference to this string.
2032 * @throw std::length_error If new length exceeds @c max_size().
2033 *
2034 * Removes the characters in the range [__i1,__i2). In place,
2035 * the characters of @a __s are inserted. If the length of
2036 * result exceeds max_size(), length_error is thrown. The
2037 * value of the string doesn't change if an error is thrown.
2038 */
2039 basic_string&
2040 replace(__const_iterator __i1, __const_iterator __i2, const _CharT* __s)
2041 {
2042 __glibcxx_requires_string(__s);
2043 return this->replace(__i1, __i2, __s, traits_type::length(__s));
2044 }
2045
2046 /**
2047 * @brief Replace range of characters with multiple characters
2048 * @param __i1 Iterator referencing start of range to replace.
2049 * @param __i2 Iterator referencing end of range to replace.
2050 * @param __n Number of characters to insert.
2051 * @param __c Character to insert.
2052 * @return Reference to this string.
2053 * @throw std::length_error If new length exceeds @c max_size().
2054 *
2055 * Removes the characters in the range [__i1,__i2). In place,
2056 * @a __n copies of @a __c are inserted. If the length of
2057 * result exceeds max_size(), length_error is thrown. The
2058 * value of the string doesn't change if an error is thrown.
2059 */
2060 basic_string&
2061 replace(__const_iterator __i1, __const_iterator __i2, size_type __n,
2062 _CharT __c)
2063 {
2064 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2
2065 && __i2 <= end());
2066 return _M_replace_aux(__i1 - begin(), __i2 - __i1, __n, __c);
2067 }
2068
2069 /**
2070 * @brief Replace range of characters with range.
2071 * @param __i1 Iterator referencing start of range to replace.
2072 * @param __i2 Iterator referencing end of range to replace.
2073 * @param __k1 Iterator referencing start of range to insert.
2074 * @param __k2 Iterator referencing end of range to insert.
2075 * @return Reference to this string.
2076 * @throw std::length_error If new length exceeds @c max_size().
2077 *
2078 * Removes the characters in the range [__i1,__i2). In place,
2079 * characters in the range [__k1,__k2) are inserted. If the
2080 * length of result exceeds max_size(), length_error is thrown.
2081 * The value of the string doesn't change if an error is
2082 * thrown.
2083 */
2084#if __cplusplus201703L >= 201103L
2085 template<class _InputIterator,
2086 typename = std::_RequireInputIter<_InputIterator>>
2087 basic_string&
2088 replace(const_iterator __i1, const_iterator __i2,
2089 _InputIterator __k1, _InputIterator __k2)
2090 {
2091 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2
2092 && __i2 <= end());
2093 __glibcxx_requires_valid_range(__k1, __k2);
2094 return this->_M_replace_dispatch(__i1, __i2, __k1, __k2,
2095 std::__false_type());
2096 }
2097#else
2098 template<class _InputIterator>
2099#ifdef _GLIBCXX_DISAMBIGUATE_REPLACE_INST
2100 typename __enable_if_not_native_iterator<_InputIterator>::__type
2101#else
2102 basic_string&
2103#endif
2104 replace(iterator __i1, iterator __i2,
2105 _InputIterator __k1, _InputIterator __k2)
2106 {
2107 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2
2108 && __i2 <= end());
2109 __glibcxx_requires_valid_range(__k1, __k2);
2110 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
2111 return _M_replace_dispatch(__i1, __i2, __k1, __k2, _Integral());
2112 }
2113#endif
2114
2115 // Specializations for the common case of pointer and iterator:
2116 // useful to avoid the overhead of temporary buffering in _M_replace.
2117 basic_string&
2118 replace(__const_iterator __i1, __const_iterator __i2,
2119 _CharT* __k1, _CharT* __k2)
2120 {
2121 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2
2122 && __i2 <= end());
2123 __glibcxx_requires_valid_range(__k1, __k2);
2124 return this->replace(__i1 - begin(), __i2 - __i1,
2125 __k1, __k2 - __k1);
2126 }
2127
2128 basic_string&
2129 replace(__const_iterator __i1, __const_iterator __i2,
2130 const _CharT* __k1, const _CharT* __k2)
2131 {
2132 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2
2133 && __i2 <= end());
2134 __glibcxx_requires_valid_range(__k1, __k2);
2135 return this->replace(__i1 - begin(), __i2 - __i1,
2136 __k1, __k2 - __k1);
2137 }
2138
2139 basic_string&
2140 replace(__const_iterator __i1, __const_iterator __i2,
2141 iterator __k1, iterator __k2)
2142 {
2143 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2
2144 && __i2 <= end());
2145 __glibcxx_requires_valid_range(__k1, __k2);
2146 return this->replace(__i1 - begin(), __i2 - __i1,
2147 __k1.base(), __k2 - __k1);
2148 }
2149
2150 basic_string&
2151 replace(__const_iterator __i1, __const_iterator __i2,
2152 const_iterator __k1, const_iterator __k2)
2153 {
2154 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2
2155 && __i2 <= end());
2156 __glibcxx_requires_valid_range(__k1, __k2);
2157 return this->replace(__i1 - begin(), __i2 - __i1,
2158 __k1.base(), __k2 - __k1);
2159 }
2160
2161#if __cplusplus201703L >= 201103L
2162 /**
2163 * @brief Replace range of characters with initializer_list.
2164 * @param __i1 Iterator referencing start of range to replace.
2165 * @param __i2 Iterator referencing end of range to replace.
2166 * @param __l The initializer_list of characters to insert.
2167 * @return Reference to this string.
2168 * @throw std::length_error If new length exceeds @c max_size().
2169 *
2170 * Removes the characters in the range [__i1,__i2). In place,
2171 * characters in the range [__k1,__k2) are inserted. If the
2172 * length of result exceeds max_size(), length_error is thrown.
2173 * The value of the string doesn't change if an error is
2174 * thrown.
2175 */
2176 basic_string& replace(const_iterator __i1, const_iterator __i2,
2177 initializer_list<_CharT> __l)
2178 { return this->replace(__i1, __i2, __l.begin(), __l.size()); }
2179#endif // C++11
2180
2181#if __cplusplus201703L > 201402L
2182 /**
2183 * @brief Replace range of characters with string_view.
2184 * @param __pos The position to replace at.
2185 * @param __n The number of characters to replace.
2186 * @param __svt The object convertible to string_view to insert.
2187 * @return Reference to this string.
2188 */
2189 template<typename _Tp>
2190 _If_sv<_Tp, basic_string&>
2191 replace(size_type __pos, size_type __n, const _Tp& __svt)
2192 {
2193 __sv_type __sv = __svt;
2194 return this->replace(__pos, __n, __sv.data(), __sv.size());
2195 }
2196
2197 /**
2198 * @brief Replace range of characters with string_view.
2199 * @param __pos1 The position to replace at.
2200 * @param __n1 The number of characters to replace.
2201 * @param __svt The object convertible to string_view to insert from.
2202 * @param __pos2 The position in the string_view to insert from.
2203 * @param __n2 The number of characters to insert.
2204 * @return Reference to this string.
2205 */
2206 template<typename _Tp>
2207 _If_sv<_Tp, basic_string&>
2208 replace(size_type __pos1, size_type __n1, const _Tp& __svt,
2209 size_type __pos2, size_type __n2 = npos)
2210 {
2211 __sv_type __sv = __svt;
2212 return this->replace(__pos1, __n1, __sv.data()
2213 + __sv._M_check(__pos2, "basic_string::replace"),
2214 __sv._M_limit(__pos2, __n2));
2215 }
2216
2217 /**
2218 * @brief Replace range of characters with string_view.
2219 * @param __i1 An iterator referencing the start position
2220 to replace at.
2221 * @param __i2 An iterator referencing the end position
2222 for the replace.
2223 * @param __svt The object convertible to string_view to insert from.
2224 * @return Reference to this string.
2225 */
2226 template<typename _Tp>
2227 _If_sv<_Tp, basic_string&>
2228 replace(const_iterator __i1, const_iterator __i2, const _Tp& __svt)
2229 {
2230 __sv_type __sv = __svt;
2231 return this->replace(__i1 - begin(), __i2 - __i1, __sv);
2232 }
2233#endif // C++17
2234
2235 private:
2236 template<class _Integer>
2237 basic_string&
2238 _M_replace_dispatch(const_iterator __i1, const_iterator __i2,
2239 _Integer __n, _Integer __val, __true_type)
2240 { return _M_replace_aux(__i1 - begin(), __i2 - __i1, __n, __val); }
2241
2242 template<class _InputIterator>
2243 basic_string&
2244 _M_replace_dispatch(const_iterator __i1, const_iterator __i2,
2245 _InputIterator __k1, _InputIterator __k2,
2246 __false_type);
2247
2248 basic_string&
2249 _M_replace_aux(size_type __pos1, size_type __n1, size_type __n2,
2250 _CharT __c);
2251
2252 basic_string&
2253 _M_replace(size_type __pos, size_type __len1, const _CharT* __s,
2254 const size_type __len2);
2255
2256 basic_string&
2257 _M_append(const _CharT* __s, size_type __n);
2258
2259 public:
2260
2261 /**
2262 * @brief Copy substring into C string.
2263 * @param __s C string to copy value into.
2264 * @param __n Number of characters to copy.
2265 * @param __pos Index of first character to copy.
2266 * @return Number of characters actually copied
2267 * @throw std::out_of_range If __pos > size().
2268 *
2269 * Copies up to @a __n characters starting at @a __pos into the
2270 * C string @a __s. If @a __pos is %greater than size(),
2271 * out_of_range is thrown.
2272 */
2273 size_type
2274 copy(_CharT* __s, size_type __n, size_type __pos = 0) const;
2275
2276 /**
2277 * @brief Swap contents with another string.
2278 * @param __s String to swap with.
2279 *
2280 * Exchanges the contents of this string with that of @a __s in constant
2281 * time.
2282 */
2283 void
2284 swap(basic_string& __s) _GLIBCXX_NOEXCEPTnoexcept;
2285
2286 // String operations:
2287 /**
2288 * @brief Return const pointer to null-terminated contents.
2289 *
2290 * This is a handle to internal data. Do not modify or dire things may
2291 * happen.
2292 */
2293 const _CharT*
2294 c_str() const _GLIBCXX_NOEXCEPTnoexcept
2295 { return _M_data(); }
2296
2297 /**
2298 * @brief Return const pointer to contents.
2299 *
2300 * This is a pointer to internal data. It is undefined to modify
2301 * the contents through the returned pointer. To get a pointer that
2302 * allows modifying the contents use @c &str[0] instead,
2303 * (or in C++17 the non-const @c str.data() overload).
2304 */
2305 const _CharT*
2306 data() const _GLIBCXX_NOEXCEPTnoexcept
2307 { return _M_data(); }
2308
2309#if __cplusplus201703L > 201402L
2310 /**
2311 * @brief Return non-const pointer to contents.
2312 *
2313 * This is a pointer to the character sequence held by the string.
2314 * Modifying the characters in the sequence is allowed.
2315 */
2316 _CharT*
2317 data() noexcept
2318 { return _M_data(); }
2319#endif
2320
2321 /**
2322 * @brief Return copy of allocator used to construct this string.
2323 */
2324 allocator_type
2325 get_allocator() const _GLIBCXX_NOEXCEPTnoexcept
2326 { return _M_get_allocator(); }
2327
2328 /**
2329 * @brief Find position of a C substring.
2330 * @param __s C string to locate.
2331 * @param __pos Index of character to search from.
2332 * @param __n Number of characters from @a s to search for.
2333 * @return Index of start of first occurrence.
2334 *
2335 * Starting from @a __pos, searches forward for the first @a
2336 * __n characters in @a __s within this string. If found,
2337 * returns the index where it begins. If not found, returns
2338 * npos.
2339 */
2340 size_type
2341 find(const _CharT* __s, size_type __pos, size_type __n) const
2342 _GLIBCXX_NOEXCEPTnoexcept;
2343
2344 /**
2345 * @brief Find position of a string.
2346 * @param __str String to locate.
2347 * @param __pos Index of character to search from (default 0).
2348 * @return Index of start of first occurrence.
2349 *
2350 * Starting from @a __pos, searches forward for value of @a __str within
2351 * this string. If found, returns the index where it begins. If not
2352 * found, returns npos.
2353 */
2354 size_type
2355 find(const basic_string& __str, size_type __pos = 0) const
2356 _GLIBCXX_NOEXCEPTnoexcept
2357 { return this->find(__str.data(), __pos, __str.size()); }
2358
2359#if __cplusplus201703L > 201402L
2360 /**
2361 * @brief Find position of a string_view.
2362 * @param __svt The object convertible to string_view to locate.
2363 * @param __pos Index of character to search from (default 0).
2364 * @return Index of start of first occurrence.
2365 */
2366 template<typename _Tp>
2367 _If_sv<_Tp, size_type>
2368 find(const _Tp& __svt, size_type __pos = 0) const
2369 noexcept(is_same<_Tp, __sv_type>::value)
2370 {
2371 __sv_type __sv = __svt;
2372 return this->find(__sv.data(), __pos, __sv.size());
2373 }
2374#endif // C++17
2375
2376 /**
2377 * @brief Find position of a C string.
2378 * @param __s C string to locate.
2379 * @param __pos Index of character to search from (default 0).
2380 * @return Index of start of first occurrence.
2381 *
2382 * Starting from @a __pos, searches forward for the value of @a
2383 * __s within this string. If found, returns the index where
2384 * it begins. If not found, returns npos.
2385 */
2386 size_type
2387 find(const _CharT* __s, size_type __pos = 0) const _GLIBCXX_NOEXCEPTnoexcept
2388 {
2389 __glibcxx_requires_string(__s);
2390 return this->find(__s, __pos, traits_type::length(__s));
2391 }
2392
2393 /**
2394 * @brief Find position of a character.
2395 * @param __c Character to locate.
2396 * @param __pos Index of character to search from (default 0).
2397 * @return Index of first occurrence.
2398 *
2399 * Starting from @a __pos, searches forward for @a __c within
2400 * this string. If found, returns the index where it was
2401 * found. If not found, returns npos.
2402 */
2403 size_type
2404 find(_CharT __c, size_type __pos = 0) const _GLIBCXX_NOEXCEPTnoexcept;
2405
2406 /**
2407 * @brief Find last position of a string.
2408 * @param __str String to locate.
2409 * @param __pos Index of character to search back from (default end).
2410 * @return Index of start of last occurrence.
2411 *
2412 * Starting from @a __pos, searches backward for value of @a
2413 * __str within this string. If found, returns the index where
2414 * it begins. If not found, returns npos.
2415 */
2416 size_type
2417 rfind(const basic_string& __str, size_type __pos = npos) const
2418 _GLIBCXX_NOEXCEPTnoexcept
2419 { return this->rfind(__str.data(), __pos, __str.size()); }
2420
2421#if __cplusplus201703L > 201402L
2422 /**
2423 * @brief Find last position of a string_view.
2424 * @param __svt The object convertible to string_view to locate.
2425 * @param __pos Index of character to search back from (default end).
2426 * @return Index of start of last occurrence.
2427 */
2428 template<typename _Tp>
2429 _If_sv<_Tp, size_type>
2430 rfind(const _Tp& __svt, size_type __pos = npos) const
2431 noexcept(is_same<_Tp, __sv_type>::value)
2432 {
2433 __sv_type __sv = __svt;
2434 return this->rfind(__sv.data(), __pos, __sv.size());
2435 }
2436#endif // C++17
2437
2438 /**
2439 * @brief Find last position of a C substring.
2440 * @param __s C string to locate.
2441 * @param __pos Index of character to search back from.
2442 * @param __n Number of characters from s to search for.
2443 * @return Index of start of last occurrence.
2444 *
2445 * Starting from @a __pos, searches backward for the first @a
2446 * __n characters in @a __s within this string. If found,
2447 * returns the index where it begins. If not found, returns
2448 * npos.
2449 */
2450 size_type
2451 rfind(const _CharT* __s, size_type __pos, size_type __n) const
2452 _GLIBCXX_NOEXCEPTnoexcept;
2453
2454 /**
2455 * @brief Find last position of a C string.
2456 * @param __s C string to locate.
2457 * @param __pos Index of character to start search at (default end).
2458 * @return Index of start of last occurrence.
2459 *
2460 * Starting from @a __pos, searches backward for the value of
2461 * @a __s within this string. If found, returns the index
2462 * where it begins. If not found, returns npos.
2463 */
2464 size_type
2465 rfind(const _CharT* __s, size_type __pos = npos) const
2466 {
2467 __glibcxx_requires_string(__s);
2468 return this->rfind(__s, __pos, traits_type::length(__s));
2469 }
2470
2471 /**
2472 * @brief Find last position of a character.
2473 * @param __c Character to locate.
2474 * @param __pos Index of character to search back from (default end).
2475 * @return Index of last occurrence.
2476 *
2477 * Starting from @a __pos, searches backward for @a __c within
2478 * this string. If found, returns the index where it was
2479 * found. If not found, returns npos.
2480 */
2481 size_type
2482 rfind(_CharT __c, size_type __pos = npos) const _GLIBCXX_NOEXCEPTnoexcept;
2483
2484 /**
2485 * @brief Find position of a character of string.
2486 * @param __str String containing characters to locate.
2487 * @param __pos Index of character to search from (default 0).
2488 * @return Index of first occurrence.
2489 *
2490 * Starting from @a __pos, searches forward for one of the
2491 * characters of @a __str within this string. If found,
2492 * returns the index where it was found. If not found, returns
2493 * npos.
2494 */
2495 size_type
2496 find_first_of(const basic_string& __str, size_type __pos = 0) const
2497 _GLIBCXX_NOEXCEPTnoexcept
2498 { return this->find_first_of(__str.data(), __pos, __str.size()); }
2499
2500#if __cplusplus201703L > 201402L
2501 /**
2502 * @brief Find position of a character of a string_view.
2503 * @param __svt An object convertible to string_view containing
2504 * characters to locate.
2505 * @param __pos Index of character to search from (default 0).
2506 * @return Index of first occurrence.
2507 */
2508 template<typename _Tp>
2509 _If_sv<_Tp, size_type>
2510 find_first_of(const _Tp& __svt, size_type __pos = 0) const
2511 noexcept(is_same<_Tp, __sv_type>::value)
2512 {
2513 __sv_type __sv = __svt;
2514 return this->find_first_of(__sv.data(), __pos, __sv.size());
2515 }
2516#endif // C++17
2517
2518 /**
2519 * @brief Find position of a character of C substring.
2520 * @param __s String containing characters to locate.
2521 * @param __pos Index of character to search from.
2522 * @param __n Number of characters from s to search for.
2523 * @return Index of first occurrence.
2524 *
2525 * Starting from @a __pos, searches forward for one of the
2526 * first @a __n characters of @a __s within this string. If
2527 * found, returns the index where it was found. If not found,
2528 * returns npos.
2529 */
2530 size_type
2531 find_first_of(const _CharT* __s, size_type __pos, size_type __n) const
2532 _GLIBCXX_NOEXCEPTnoexcept;
2533
2534 /**
2535 * @brief Find position of a character of C string.
2536 * @param __s String containing characters to locate.
2537 * @param __pos Index of character to search from (default 0).
2538 * @return Index of first occurrence.
2539 *
2540 * Starting from @a __pos, searches forward for one of the
2541 * characters of @a __s within this string. If found, returns
2542 * the index where it was found. If not found, returns npos.
2543 */
2544 size_type
2545 find_first_of(const _CharT* __s, size_type __pos = 0) const
2546 _GLIBCXX_NOEXCEPTnoexcept
2547 {
2548 __glibcxx_requires_string(__s);
2549 return this->find_first_of(__s, __pos, traits_type::length(__s));
2550 }
2551
2552 /**
2553 * @brief Find position of a character.
2554 * @param __c Character to locate.
2555 * @param __pos Index of character to search from (default 0).
2556 * @return Index of first occurrence.
2557 *
2558 * Starting from @a __pos, searches forward for the character
2559 * @a __c within this string. If found, returns the index
2560 * where it was found. If not found, returns npos.
2561 *
2562 * Note: equivalent to find(__c, __pos).
2563 */
2564 size_type
2565 find_first_of(_CharT __c, size_type __pos = 0) const _GLIBCXX_NOEXCEPTnoexcept
2566 { return this->find(__c, __pos); }
2567
2568 /**
2569 * @brief Find last position of a character of string.
2570 * @param __str String containing characters to locate.
2571 * @param __pos Index of character to search back from (default end).
2572 * @return Index of last occurrence.
2573 *
2574 * Starting from @a __pos, searches backward for one of the
2575 * characters of @a __str within this string. If found,
2576 * returns the index where it was found. If not found, returns
2577 * npos.
2578 */
2579 size_type
2580 find_last_of(const basic_string& __str, size_type __pos = npos) const
2581 _GLIBCXX_NOEXCEPTnoexcept
2582 { return this->find_last_of(__str.data(), __pos, __str.size()); }
2583
2584#if __cplusplus201703L > 201402L
2585 /**
2586 * @brief Find last position of a character of string.
2587 * @param __svt An object convertible to string_view containing
2588 * characters to locate.
2589 * @param __pos Index of character to search back from (default end).
2590 * @return Index of last occurrence.
2591 */
2592 template<typename _Tp>
2593 _If_sv<_Tp, size_type>
2594 find_last_of(const _Tp& __svt, size_type __pos = npos) const
2595 noexcept(is_same<_Tp, __sv_type>::value)
2596 {
2597 __sv_type __sv = __svt;
2598 return this->find_last_of(__sv.data(), __pos, __sv.size());
2599 }
2600#endif // C++17
2601
2602 /**
2603 * @brief Find last position of a character of C substring.
2604 * @param __s C string containing characters to locate.
2605 * @param __pos Index of character to search back from.
2606 * @param __n Number of characters from s to search for.
2607 * @return Index of last occurrence.
2608 *
2609 * Starting from @a __pos, searches backward for one of the
2610 * first @a __n characters of @a __s within this string. If
2611 * found, returns the index where it was found. If not found,
2612 * returns npos.
2613 */
2614 size_type
2615 find_last_of(const _CharT* __s, size_type __pos, size_type __n) const
2616 _GLIBCXX_NOEXCEPTnoexcept;
2617
2618 /**
2619 * @brief Find last position of a character of C string.
2620 * @param __s C string containing characters to locate.
2621 * @param __pos Index of character to search back from (default end).
2622 * @return Index of last occurrence.
2623 *
2624 * Starting from @a __pos, searches backward for one of the
2625 * characters of @a __s within this string. If found, returns
2626 * the index where it was found. If not found, returns npos.
2627 */
2628 size_type
2629 find_last_of(const _CharT* __s, size_type __pos = npos) const
2630 _GLIBCXX_NOEXCEPTnoexcept
2631 {
2632 __glibcxx_requires_string(__s);
2633 return this->find_last_of(__s, __pos, traits_type::length(__s));
2634 }
2635
2636 /**
2637 * @brief Find last position of a character.
2638 * @param __c Character to locate.
2639 * @param __pos Index of character to search back from (default end).
2640 * @return Index of last occurrence.
2641 *
2642 * Starting from @a __pos, searches backward for @a __c within
2643 * this string. If found, returns the index where it was
2644 * found. If not found, returns npos.
2645 *
2646 * Note: equivalent to rfind(__c, __pos).
2647 */
2648 size_type
2649 find_last_of(_CharT __c, size_type __pos = npos) const _GLIBCXX_NOEXCEPTnoexcept
2650 { return this->rfind(__c, __pos); }
2651
2652 /**
2653 * @brief Find position of a character not in string.
2654 * @param __str String containing characters to avoid.
2655 * @param __pos Index of character to search from (default 0).
2656 * @return Index of first occurrence.
2657 *
2658 * Starting from @a __pos, searches forward for a character not contained
2659 * in @a __str within this string. If found, returns the index where it
2660 * was found. If not found, returns npos.
2661 */
2662 size_type
2663 find_first_not_of(const basic_string& __str, size_type __pos = 0) const
2664 _GLIBCXX_NOEXCEPTnoexcept
2665 { return this->find_first_not_of(__str.data(), __pos, __str.size()); }
2666
2667#if __cplusplus201703L > 201402L
2668 /**
2669 * @brief Find position of a character not in a string_view.
2670 * @param __svt A object convertible to string_view containing
2671 * characters to avoid.
2672 * @param __pos Index of character to search from (default 0).
2673 * @return Index of first occurrence.
2674 */
2675 template<typename _Tp>
2676 _If_sv<_Tp, size_type>
2677 find_first_not_of(const _Tp& __svt, size_type __pos = 0) const
2678 noexcept(is_same<_Tp, __sv_type>::value)
2679 {
2680 __sv_type __sv = __svt;
2681 return this->find_first_not_of(__sv.data(), __pos, __sv.size());
2682 }
2683#endif // C++17
2684
2685 /**
2686 * @brief Find position of a character not in C substring.
2687 * @param __s C string containing characters to avoid.
2688 * @param __pos Index of character to search from.
2689 * @param __n Number of characters from __s to consider.
2690 * @return Index of first occurrence.
2691 *
2692 * Starting from @a __pos, searches forward for a character not
2693 * contained in the first @a __n characters of @a __s within
2694 * this string. If found, returns the index where it was
2695 * found. If not found, returns npos.
2696 */
2697 size_type
2698 find_first_not_of(const _CharT* __s, size_type __pos,
2699 size_type __n) const _GLIBCXX_NOEXCEPTnoexcept;
2700
2701 /**
2702 * @brief Find position of a character not in C string.
2703 * @param __s C string containing characters to avoid.
2704 * @param __pos Index of character to search from (default 0).
2705 * @return Index of first occurrence.
2706 *
2707 * Starting from @a __pos, searches forward for a character not
2708 * contained in @a __s within this string. If found, returns
2709 * the index where it was found. If not found, returns npos.
2710 */
2711 size_type
2712 find_first_not_of(const _CharT* __s, size_type __pos = 0) const
2713 _GLIBCXX_NOEXCEPTnoexcept
2714 {
2715 __glibcxx_requires_string(__s);
2716 return this->find_first_not_of(__s, __pos, traits_type::length(__s));
2717 }
2718
2719 /**
2720 * @brief Find position of a different character.
2721 * @param __c Character to avoid.
2722 * @param __pos Index of character to search from (default 0).
2723 * @return Index of first occurrence.
2724 *
2725 * Starting from @a __pos, searches forward for a character
2726 * other than @a __c within this string. If found, returns the
2727 * index where it was found. If not found, returns npos.
2728 */
2729 size_type
2730 find_first_not_of(_CharT __c, size_type __pos = 0) const
2731 _GLIBCXX_NOEXCEPTnoexcept;
2732
2733 /**
2734 * @brief Find last position of a character not in string.
2735 * @param __str String containing characters to avoid.
2736 * @param __pos Index of character to search back from (default end).
2737 * @return Index of last occurrence.
2738 *
2739 * Starting from @a __pos, searches backward for a character
2740 * not contained in @a __str within this string. If found,
2741 * returns the index where it was found. If not found, returns
2742 * npos.
2743 */
2744 size_type
2745 find_last_not_of(const basic_string& __str, size_type __pos = npos) const
2746 _GLIBCXX_NOEXCEPTnoexcept
2747 { return this->find_last_not_of(__str.data(), __pos, __str.size()); }
2748
2749#if __cplusplus201703L > 201402L
2750 /**
2751 * @brief Find last position of a character not in a string_view.
2752 * @param __svt An object convertible to string_view containing
2753 * characters to avoid.
2754 * @param __pos Index of character to search back from (default end).
2755 * @return Index of last occurrence.
2756 */
2757 template<typename _Tp>
2758 _If_sv<_Tp, size_type>
2759 find_last_not_of(const _Tp& __svt, size_type __pos = npos) const
2760 noexcept(is_same<_Tp, __sv_type>::value)
2761 {
2762 __sv_type __sv = __svt;
2763 return this->find_last_not_of(__sv.data(), __pos, __sv.size());
2764 }
2765#endif // C++17
2766
2767 /**
2768 * @brief Find last position of a character not in C substring.
2769 * @param __s C string containing characters to avoid.
2770 * @param __pos Index of character to search back from.
2771 * @param __n Number of characters from s to consider.
2772 * @return Index of last occurrence.
2773 *
2774 * Starting from @a __pos, searches backward for a character not
2775 * contained in the first @a __n characters of @a __s within this string.
2776 * If found, returns the index where it was found. If not found,
2777 * returns npos.
2778 */
2779 size_type
2780 find_last_not_of(const _CharT* __s, size_type __pos,
2781 size_type __n) const _GLIBCXX_NOEXCEPTnoexcept;
2782 /**
2783 * @brief Find last position of a character not in C string.
2784 * @param __s C string containing characters to avoid.
2785 * @param __pos Index of character to search back from (default end).
2786 * @return Index of last occurrence.
2787 *
2788 * Starting from @a __pos, searches backward for a character
2789 * not contained in @a __s within this string. If found,
2790 * returns the index where it was found. If not found, returns
2791 * npos.
2792 */
2793 size_type
2794 find_last_not_of(const _CharT* __s, size_type __pos = npos) const
2795 _GLIBCXX_NOEXCEPTnoexcept
2796 {
2797 __glibcxx_requires_string(__s);
2798 return this->find_last_not_of(__s, __pos, traits_type::length(__s));
2799 }
2800
2801 /**
2802 * @brief Find last position of a different character.
2803 * @param __c Character to avoid.
2804 * @param __pos Index of character to search back from (default end).
2805 * @return Index of last occurrence.
2806 *
2807 * Starting from @a __pos, searches backward for a character other than
2808 * @a __c within this string. If found, returns the index where it was
2809 * found. If not found, returns npos.
2810 */
2811 size_type
2812 find_last_not_of(_CharT __c, size_type __pos = npos) const
2813 _GLIBCXX_NOEXCEPTnoexcept;
2814
2815 /**
2816 * @brief Get a substring.
2817 * @param __pos Index of first character (default 0).
2818 * @param __n Number of characters in substring (default remainder).
2819 * @return The new string.
2820 * @throw std::out_of_range If __pos > size().
2821 *
2822 * Construct and return a new string using the @a __n
2823 * characters starting at @a __pos. If the string is too
2824 * short, use the remainder of the characters. If @a __pos is
2825 * beyond the end of the string, out_of_range is thrown.
2826 */
2827 basic_string
2828 substr(size_type __pos = 0, size_type __n = npos) const
2829 { return basic_string(*this,
2830 _M_check(__pos, "basic_string::substr"), __n); }
2831
2832 /**
2833 * @brief Compare to a string.
2834 * @param __str String to compare against.
2835 * @return Integer < 0, 0, or > 0.
2836 *
2837 * Returns an integer < 0 if this string is ordered before @a
2838 * __str, 0 if their values are equivalent, or > 0 if this
2839 * string is ordered after @a __str. Determines the effective
2840 * length rlen of the strings to compare as the smallest of
2841 * size() and str.size(). The function then compares the two
2842 * strings by calling traits::compare(data(), str.data(),rlen).
2843 * If the result of the comparison is nonzero returns it,
2844 * otherwise the shorter one is ordered first.
2845 */
2846 int
2847 compare(const basic_string& __str) const
2848 {
2849 const size_type __size = this->size();
2850 const size_type __osize = __str.size();
2851 const size_type __len = std::min(__size, __osize);
2852
2853 int __r = traits_type::compare(_M_data(), __str.data(), __len);
2854 if (!__r)
2855 __r = _S_compare(__size, __osize);
2856 return __r;
2857 }
2858
2859#if __cplusplus201703L > 201402L
2860 /**
2861 * @brief Compare to a string_view.
2862 * @param __svt An object convertible to string_view to compare against.
2863 * @return Integer < 0, 0, or > 0.
2864 */
2865 template<typename _Tp>
2866 _If_sv<_Tp, int>
2867 compare(const _Tp& __svt) const
2868 noexcept(is_same<_Tp, __sv_type>::value)
2869 {
2870 __sv_type __sv = __svt;
2871 const size_type __size = this->size();
2872 const size_type __osize = __sv.size();
2873 const size_type __len = std::min(__size, __osize);
2874
2875 int __r = traits_type::compare(_M_data(), __sv.data(), __len);
2876 if (!__r)
2877 __r = _S_compare(__size, __osize);
2878 return __r;
2879 }
2880
2881 /**
2882 * @brief Compare to a string_view.
2883 * @param __pos A position in the string to start comparing from.
2884 * @param __n The number of characters to compare.
2885 * @param __svt An object convertible to string_view to compare
2886 * against.
2887 * @return Integer < 0, 0, or > 0.
2888 */
2889 template<typename _Tp>
2890 _If_sv<_Tp, int>
2891 compare(size_type __pos, size_type __n, const _Tp& __svt) const
2892 noexcept(is_same<_Tp, __sv_type>::value)
2893 {
2894 __sv_type __sv = __svt;
2895 return __sv_type(*this).substr(__pos, __n).compare(__sv);
2896 }
2897
2898 /**
2899 * @brief Compare to a string_view.
2900 * @param __pos1 A position in the string to start comparing from.
2901 * @param __n1 The number of characters to compare.
2902 * @param __svt An object convertible to string_view to compare
2903 * against.
2904 * @param __pos2 A position in the string_view to start comparing from.
2905 * @param __n2 The number of characters to compare.
2906 * @return Integer < 0, 0, or > 0.
2907 */
2908 template<typename _Tp>
2909 _If_sv<_Tp, int>
2910 compare(size_type __pos1, size_type __n1, const _Tp& __svt,
2911 size_type __pos2, size_type __n2 = npos) const
2912 noexcept(is_same<_Tp, __sv_type>::value)
2913 {
2914 __sv_type __sv = __svt;
2915 return __sv_type(*this)
2916 .substr(__pos1, __n1).compare(__sv.substr(__pos2, __n2));
2917 }
2918#endif // C++17
2919
2920 /**
2921 * @brief Compare substring to a string.
2922 * @param __pos Index of first character of substring.
2923 * @param __n Number of characters in substring.
2924 * @param __str String to compare against.
2925 * @return Integer < 0, 0, or > 0.
2926 *
2927 * Form the substring of this string from the @a __n characters
2928 * starting at @a __pos. Returns an integer < 0 if the
2929 * substring is ordered before @a __str, 0 if their values are
2930 * equivalent, or > 0 if the substring is ordered after @a
2931 * __str. Determines the effective length rlen of the strings
2932 * to compare as the smallest of the length of the substring
2933 * and @a __str.size(). The function then compares the two
2934 * strings by calling
2935 * traits::compare(substring.data(),str.data(),rlen). If the
2936 * result of the comparison is nonzero returns it, otherwise
2937 * the shorter one is ordered first.
2938 */
2939 int
2940 compare(size_type __pos, size_type __n, const basic_string& __str) const;
2941
2942 /**
2943 * @brief Compare substring to a substring.
2944 * @param __pos1 Index of first character of substring.
2945 * @param __n1 Number of characters in substring.
2946 * @param __str String to compare against.
2947 * @param __pos2 Index of first character of substring of str.
2948 * @param __n2 Number of characters in substring of str.
2949 * @return Integer < 0, 0, or > 0.
2950 *
2951 * Form the substring of this string from the @a __n1
2952 * characters starting at @a __pos1. Form the substring of @a
2953 * __str from the @a __n2 characters starting at @a __pos2.
2954 * Returns an integer < 0 if this substring is ordered before
2955 * the substring of @a __str, 0 if their values are equivalent,
2956 * or > 0 if this substring is ordered after the substring of
2957 * @a __str. Determines the effective length rlen of the
2958 * strings to compare as the smallest of the lengths of the
2959 * substrings. The function then compares the two strings by
2960 * calling
2961 * traits::compare(substring.data(),str.substr(pos2,n2).data(),rlen).
2962 * If the result of the comparison is nonzero returns it,
2963 * otherwise the shorter one is ordered first.
2964 */
2965 int
2966 compare(size_type __pos1, size_type __n1, const basic_string& __str,
2967 size_type __pos2, size_type __n2 = npos) const;
2968
2969 /**
2970 * @brief Compare to a C string.
2971 * @param __s C string to compare against.
2972 * @return Integer < 0, 0, or > 0.
2973 *
2974 * Returns an integer < 0 if this string is ordered before @a __s, 0 if
2975 * their values are equivalent, or > 0 if this string is ordered after
2976 * @a __s. Determines the effective length rlen of the strings to
2977 * compare as the smallest of size() and the length of a string
2978 * constructed from @a __s. The function then compares the two strings
2979 * by calling traits::compare(data(),s,rlen). If the result of the
2980 * comparison is nonzero returns it, otherwise the shorter one is
2981 * ordered first.
2982 */
2983 int
2984 compare(const _CharT* __s) const _GLIBCXX_NOEXCEPTnoexcept;
2985
2986 // _GLIBCXX_RESOLVE_LIB_DEFECTS
2987 // 5 String::compare specification questionable
2988 /**
2989 * @brief Compare substring to a C string.
2990 * @param __pos Index of first character of substring.
2991 * @param __n1 Number of characters in substring.
2992 * @param __s C string to compare against.
2993 * @return Integer < 0, 0, or > 0.
2994 *
2995 * Form the substring of this string from the @a __n1
2996 * characters starting at @a pos. Returns an integer < 0 if
2997 * the substring is ordered before @a __s, 0 if their values
2998 * are equivalent, or > 0 if the substring is ordered after @a
2999 * __s. Determines the effective length rlen of the strings to
3000 * compare as the smallest of the length of the substring and
3001 * the length of a string constructed from @a __s. The
3002 * function then compares the two string by calling
3003 * traits::compare(substring.data(),__s,rlen). If the result of
3004 * the comparison is nonzero returns it, otherwise the shorter
3005 * one is ordered first.
3006 */
3007 int
3008 compare(size_type __pos, size_type __n1, const _CharT* __s) const;
3009
3010 /**
3011 * @brief Compare substring against a character %array.
3012 * @param __pos Index of first character of substring.
3013 * @param __n1 Number of characters in substring.
3014 * @param __s character %array to compare against.
3015 * @param __n2 Number of characters of s.
3016 * @return Integer < 0, 0, or > 0.
3017 *
3018 * Form the substring of this string from the @a __n1
3019 * characters starting at @a __pos. Form a string from the
3020 * first @a __n2 characters of @a __s. Returns an integer < 0
3021 * if this substring is ordered before the string from @a __s,
3022 * 0 if their values are equivalent, or > 0 if this substring
3023 * is ordered after the string from @a __s. Determines the
3024 * effective length rlen of the strings to compare as the
3025 * smallest of the length of the substring and @a __n2. The
3026 * function then compares the two strings by calling
3027 * traits::compare(substring.data(),s,rlen). If the result of
3028 * the comparison is nonzero returns it, otherwise the shorter
3029 * one is ordered first.
3030 *
3031 * NB: s must have at least n2 characters, &apos;\\0&apos; has
3032 * no special meaning.
3033 */
3034 int
3035 compare(size_type __pos, size_type __n1, const _CharT* __s,
3036 size_type __n2) const;
3037
3038 // Allow basic_stringbuf::__xfer_bufptrs to call _M_length:
3039 template<typename, typename, typename> friend class basic_stringbuf;
3040 };
3041_GLIBCXX_END_NAMESPACE_CXX11}
3042#else // !_GLIBCXX_USE_CXX11_ABI
3043 // Reference-counted COW string implentation
3044
3045 /**
3046 * @class basic_string basic_string.h <string>
3047 * @brief Managing sequences of characters and character-like objects.
3048 *
3049 * @ingroup strings
3050 * @ingroup sequences
3051 *
3052 * @tparam _CharT Type of character
3053 * @tparam _Traits Traits for character type, defaults to
3054 * char_traits<_CharT>.
3055 * @tparam _Alloc Allocator type, defaults to allocator<_CharT>.
3056 *
3057 * Meets the requirements of a <a href="tables.html#65">container</a>, a
3058 * <a href="tables.html#66">reversible container</a>, and a
3059 * <a href="tables.html#67">sequence</a>. Of the
3060 * <a href="tables.html#68">optional sequence requirements</a>, only
3061 * @c push_back, @c at, and @c %array access are supported.
3062 *
3063 * @doctodo
3064 *
3065 *
3066 * Documentation? What's that?
3067 * Nathan Myers <ncm@cantrip.org>.
3068 *
3069 * A string looks like this:
3070 *
3071 * @code
3072 * [_Rep]
3073 * _M_length
3074 * [basic_string<char_type>] _M_capacity
3075 * _M_dataplus _M_refcount
3076 * _M_p ----------------> unnamed array of char_type
3077 * @endcode
3078 *
3079 * Where the _M_p points to the first character in the string, and
3080 * you cast it to a pointer-to-_Rep and subtract 1 to get a
3081 * pointer to the header.
3082 *
3083 * This approach has the enormous advantage that a string object
3084 * requires only one allocation. All the ugliness is confined
3085 * within a single %pair of inline functions, which each compile to
3086 * a single @a add instruction: _Rep::_M_data(), and
3087 * string::_M_rep(); and the allocation function which gets a
3088 * block of raw bytes and with room enough and constructs a _Rep
3089 * object at the front.
3090 *
3091 * The reason you want _M_data pointing to the character %array and
3092 * not the _Rep is so that the debugger can see the string
3093 * contents. (Probably we should add a non-inline member to get
3094 * the _Rep for the debugger to use, so users can check the actual
3095 * string length.)
3096 *
3097 * Note that the _Rep object is a POD so that you can have a
3098 * static <em>empty string</em> _Rep object already @a constructed before
3099 * static constructors have run. The reference-count encoding is
3100 * chosen so that a 0 indicates one reference, so you never try to
3101 * destroy the empty-string _Rep object.
3102 *
3103 * All but the last paragraph is considered pretty conventional
3104 * for a C++ string implementation.
3105 */
3106 // 21.3 Template class basic_string
3107 template<typename _CharT, typename _Traits, typename _Alloc>
3108 class basic_string
3109 {
3110 typedef typename _Alloc::template rebind<_CharT>::other _CharT_alloc_type;
3111
3112 // Types:
3113 public:
3114 typedef _Traits traits_type;
3115 typedef typename _Traits::char_type value_type;
3116 typedef _Alloc allocator_type;
3117 typedef typename _CharT_alloc_type::size_type size_type;
3118 typedef typename _CharT_alloc_type::difference_type difference_type;
3119 typedef typename _CharT_alloc_type::reference reference;
3120 typedef typename _CharT_alloc_type::const_reference const_reference;
3121 typedef typename _CharT_alloc_type::pointer pointer;
3122 typedef typename _CharT_alloc_type::const_pointer const_pointer;
3123 typedef __gnu_cxx::__normal_iterator<pointer, basic_string> iterator;
3124 typedef __gnu_cxx::__normal_iterator<const_pointer, basic_string>
3125 const_iterator;
3126 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
3127 typedef std::reverse_iterator<iterator> reverse_iterator;
3128
3129 private:
3130 // _Rep: string representation
3131 // Invariants:
3132 // 1. String really contains _M_length + 1 characters: due to 21.3.4
3133 // must be kept null-terminated.
3134 // 2. _M_capacity >= _M_length
3135 // Allocated memory is always (_M_capacity + 1) * sizeof(_CharT).
3136 // 3. _M_refcount has three states:
3137 // -1: leaked, one reference, no ref-copies allowed, non-const.
3138 // 0: one reference, non-const.
3139 // n>0: n + 1 references, operations require a lock, const.
3140 // 4. All fields==0 is an empty string, given the extra storage
3141 // beyond-the-end for a null terminator; thus, the shared
3142 // empty string representation needs no constructor.
3143
3144 struct _Rep_base
3145 {
3146 size_type _M_length;
3147 size_type _M_capacity;
3148 _Atomic_word _M_refcount;
3149 };
3150
3151 struct _Rep : _Rep_base
3152 {
3153 // Types:
3154 typedef typename _Alloc::template rebind<char>::other _Raw_bytes_alloc;
3155
3156 // (Public) Data members:
3157
3158 // The maximum number of individual char_type elements of an
3159 // individual string is determined by _S_max_size. This is the
3160 // value that will be returned by max_size(). (Whereas npos
3161 // is the maximum number of bytes the allocator can allocate.)
3162 // If one was to divvy up the theoretical largest size string,
3163 // with a terminating character and m _CharT elements, it'd
3164 // look like this:
3165 // npos = sizeof(_Rep) + (m * sizeof(_CharT)) + sizeof(_CharT)
3166 // Solving for m:
3167 // m = ((npos - sizeof(_Rep))/sizeof(CharT)) - 1
3168 // In addition, this implementation quarters this amount.
3169 static const size_type _S_max_size;
3170 static const _CharT _S_terminal;
3171
3172 // The following storage is init'd to 0 by the linker, resulting
3173 // (carefully) in an empty string with one reference.
3174 static size_type _S_empty_rep_storage[];
3175
3176 static _Rep&
3177 _S_empty_rep() _GLIBCXX_NOEXCEPTnoexcept
3178 {
3179 // NB: Mild hack to avoid strict-aliasing warnings. Note that
3180 // _S_empty_rep_storage is never modified and the punning should
3181 // be reasonably safe in this case.
3182 void* __p = reinterpret_cast<void*>(&_S_empty_rep_storage);
3183 return *reinterpret_cast<_Rep*>(__p);
3184 }
3185
3186 bool
3187 _M_is_leaked() const _GLIBCXX_NOEXCEPTnoexcept
3188 {
3189#if defined(__GTHREADS1)
3190 // _M_refcount is mutated concurrently by _M_refcopy/_M_dispose,
3191 // so we need to use an atomic load. However, _M_is_leaked
3192 // predicate does not change concurrently (i.e. the string is either
3193 // leaked or not), so a relaxed load is enough.
3194 return __atomic_load_n(&this->_M_refcount, __ATOMIC_RELAXED0) < 0;
3195#else
3196 return this->_M_refcount < 0;
3197#endif
3198 }
3199
3200 bool
3201 _M_is_shared() const _GLIBCXX_NOEXCEPTnoexcept
3202 {
3203#if defined(__GTHREADS1)
3204 // _M_refcount is mutated concurrently by _M_refcopy/_M_dispose,
3205 // so we need to use an atomic load. Another thread can drop last
3206 // but one reference concurrently with this check, so we need this
3207 // load to be acquire to synchronize with release fetch_and_add in
3208 // _M_dispose.
3209 return __atomic_load_n(&this->_M_refcount, __ATOMIC_ACQUIRE2) > 0;
3210#else
3211 return this->_M_refcount > 0;
3212#endif
3213 }
3214
3215 void
3216 _M_set_leaked() _GLIBCXX_NOEXCEPTnoexcept
3217 { this->_M_refcount = -1; }
3218
3219 void
3220 _M_set_sharable() _GLIBCXX_NOEXCEPTnoexcept
3221 { this->_M_refcount = 0; }
3222
3223 void
3224 _M_set_length_and_sharable(size_type __n) _GLIBCXX_NOEXCEPTnoexcept
3225 {
3226#if _GLIBCXX_FULLY_DYNAMIC_STRING0 == 0
3227 if (__builtin_expect(this != &_S_empty_rep(), false))
3228#endif
3229 {
3230 this->_M_set_sharable(); // One reference.
3231 this->_M_length = __n;
3232 traits_type::assign(this->_M_refdata()[__n], _S_terminal);
3233 // grrr. (per 21.3.4)
3234 // You cannot leave those LWG people alone for a second.
3235 }
3236 }
3237
3238 _CharT*
3239 _M_refdata() throw()
3240 { return reinterpret_cast<_CharT*>(this + 1); }
3241
3242 _CharT*
3243 _M_grab(const _Alloc& __alloc1, const _Alloc& __alloc2)
3244 {
3245 return (!_M_is_leaked() && __alloc1 == __alloc2)
3246 ? _M_refcopy() : _M_clone(__alloc1);
3247 }
3248
3249 // Create & Destroy
3250 static _Rep*
3251 _S_create(size_type, size_type, const _Alloc&);
3252
3253 void
3254 _M_dispose(const _Alloc& __a) _GLIBCXX_NOEXCEPTnoexcept
3255 {
3256#if _GLIBCXX_FULLY_DYNAMIC_STRING0 == 0
3257 if (__builtin_expect(this != &_S_empty_rep(), false))
3258#endif
3259 {
3260 // Be race-detector-friendly. For more info see bits/c++config.
3261 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&this->_M_refcount);
3262 // Decrement of _M_refcount is acq_rel, because:
3263 // - all but last decrements need to release to synchronize with
3264 // the last decrement that will delete the object.
3265 // - the last decrement needs to acquire to synchronize with
3266 // all the previous decrements.
3267 // - last but one decrement needs to release to synchronize with
3268 // the acquire load in _M_is_shared that will conclude that
3269 // the object is not shared anymore.
3270 if (__gnu_cxx::__exchange_and_add_dispatch(&this->_M_refcount,
3271 -1) <= 0)
3272 {
3273 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&this->_M_refcount);
3274 _M_destroy(__a);
3275 }
3276 }
3277 } // XXX MT
3278
3279 void
3280 _M_destroy(const _Alloc&) throw();
3281
3282 _CharT*
3283 _M_refcopy() throw()
3284 {
3285#if _GLIBCXX_FULLY_DYNAMIC_STRING0 == 0
3286 if (__builtin_expect(this != &_S_empty_rep(), false))
3287#endif
3288 __gnu_cxx::__atomic_add_dispatch(&this->_M_refcount, 1);
3289 return _M_refdata();
3290 } // XXX MT
3291
3292 _CharT*
3293 _M_clone(const _Alloc&, size_type __res = 0);
3294 };
3295
3296 // Use empty-base optimization: http://www.cantrip.org/emptyopt.html
3297 struct _Alloc_hider : _Alloc
3298 {
3299 _Alloc_hider(_CharT* __dat, const _Alloc& __a) _GLIBCXX_NOEXCEPTnoexcept
3300 : _Alloc(__a), _M_p(__dat) { }
3301
3302 _CharT* _M_p; // The actual data.
3303 };
3304
3305 public:
3306 // Data Members (public):
3307 // NB: This is an unsigned type, and thus represents the maximum
3308 // size that the allocator can hold.
3309 /// Value returned by various member functions when they fail.
3310 static const size_type npos = static_cast<size_type>(-1);
3311
3312 private:
3313 // Data Members (private):
3314 mutable _Alloc_hider _M_dataplus;
3315
3316 _CharT*
3317 _M_data() const _GLIBCXX_NOEXCEPTnoexcept
3318 { return _M_dataplus._M_p; }
3319
3320 _CharT*
3321 _M_data(_CharT* __p) _GLIBCXX_NOEXCEPTnoexcept
3322 { return (_M_dataplus._M_p = __p); }
3323
3324 _Rep*
3325 _M_rep() const _GLIBCXX_NOEXCEPTnoexcept
3326 { return &((reinterpret_cast<_Rep*> (_M_data()))[-1]); }
3327
3328 // For the internal use we have functions similar to `begin'/`end'
3329 // but they do not call _M_leak.
3330 iterator
3331 _M_ibegin() const _GLIBCXX_NOEXCEPTnoexcept
3332 { return iterator(_M_data()); }
3333
3334 iterator
3335 _M_iend() const _GLIBCXX_NOEXCEPTnoexcept
3336 { return iterator(_M_data() + this->size()); }
3337
3338 void
3339 _M_leak() // for use in begin() & non-const op[]
3340 {
3341 if (!_M_rep()->_M_is_leaked())
3342 _M_leak_hard();
3343 }
3344
3345 size_type
3346 _M_check(size_type __pos, const char* __s) const
3347 {
3348 if (__pos > this->size())
3349 __throw_out_of_range_fmt(__N("%s: __pos (which is %zu) > "("%s: __pos (which is %zu) > " "this->size() (which is %zu)"
)
3350 "this->size() (which is %zu)")("%s: __pos (which is %zu) > " "this->size() (which is %zu)"
)
,
3351 __s, __pos, this->size());
3352 return __pos;
3353 }
3354
3355 void
3356 _M_check_length(size_type __n1, size_type __n2, const char* __s) const
3357 {
3358 if (this->max_size() - (this->size() - __n1) < __n2)
3359 __throw_length_error(__N(__s)(__s));
3360 }
3361
3362 // NB: _M_limit doesn't check for a bad __pos value.
3363 size_type
3364 _M_limit(size_type __pos, size_type __off) const _GLIBCXX_NOEXCEPTnoexcept
3365 {
3366 const bool __testoff = __off < this->size() - __pos;
3367 return __testoff ? __off : this->size() - __pos;
3368 }
3369
3370 // True if _Rep and source do not overlap.
3371 bool
3372 _M_disjunct(const _CharT* __s) const _GLIBCXX_NOEXCEPTnoexcept
3373 {
3374 return (less<const _CharT*>()(__s, _M_data())
3375 || less<const _CharT*>()(_M_data() + this->size(), __s));
3376 }
3377
3378 // When __n = 1 way faster than the general multichar
3379 // traits_type::copy/move/assign.
3380 static void
3381 _M_copy(_CharT* __d, const _CharT* __s, size_type __n) _GLIBCXX_NOEXCEPTnoexcept
3382 {
3383 if (__n == 1)
3384 traits_type::assign(*__d, *__s);
3385 else
3386 traits_type::copy(__d, __s, __n);
3387 }
3388
3389 static void
3390 _M_move(_CharT* __d, const _CharT* __s, size_type __n) _GLIBCXX_NOEXCEPTnoexcept
3391 {
3392 if (__n == 1)
3393 traits_type::assign(*__d, *__s);
3394 else
3395 traits_type::move(__d, __s, __n);
3396 }
3397
3398 static void
3399 _M_assign(_CharT* __d, size_type __n, _CharT __c) _GLIBCXX_NOEXCEPTnoexcept
3400 {
3401 if (__n == 1)
3402 traits_type::assign(*__d, __c);
3403 else
3404 traits_type::assign(__d, __n, __c);
3405 }
3406
3407 // _S_copy_chars is a separate template to permit specialization
3408 // to optimize for the common case of pointers as iterators.
3409 template<class _Iterator>
3410 static void
3411 _S_copy_chars(_CharT* __p, _Iterator __k1, _Iterator __k2)
3412 {
3413 for (; __k1 != __k2; ++__k1, (void)++__p)
3414 traits_type::assign(*__p, *__k1); // These types are off.
3415 }
3416
3417 static void
3418 _S_copy_chars(_CharT* __p, iterator __k1, iterator __k2) _GLIBCXX_NOEXCEPTnoexcept
3419 { _S_copy_chars(__p, __k1.base(), __k2.base()); }
3420
3421 static void
3422 _S_copy_chars(_CharT* __p, const_iterator __k1, const_iterator __k2)
3423 _GLIBCXX_NOEXCEPTnoexcept
3424 { _S_copy_chars(__p, __k1.base(), __k2.base()); }
3425
3426 static void
3427 _S_copy_chars(_CharT* __p, _CharT* __k1, _CharT* __k2) _GLIBCXX_NOEXCEPTnoexcept
3428 { _M_copy(__p, __k1, __k2 - __k1); }
3429
3430 static void
3431 _S_copy_chars(_CharT* __p, const _CharT* __k1, const _CharT* __k2)
3432 _GLIBCXX_NOEXCEPTnoexcept
3433 { _M_copy(__p, __k1, __k2 - __k1); }
3434
3435 static int
3436 _S_compare(size_type __n1, size_type __n2) _GLIBCXX_NOEXCEPTnoexcept
3437 {
3438 const difference_type __d = difference_type(__n1 - __n2);
3439
3440 if (__d > __gnu_cxx::__numeric_traits<int>::__max)
3441 return __gnu_cxx::__numeric_traits<int>::__max;
3442 else if (__d < __gnu_cxx::__numeric_traits<int>::__min)
3443 return __gnu_cxx::__numeric_traits<int>::__min;
3444 else
3445 return int(__d);
3446 }
3447
3448 void
3449 _M_mutate(size_type __pos, size_type __len1, size_type __len2);
3450
3451 void
3452 _M_leak_hard();
3453
3454 static _Rep&
3455 _S_empty_rep() _GLIBCXX_NOEXCEPTnoexcept
3456 { return _Rep::_S_empty_rep(); }
3457
3458#if __cplusplus201703L > 201402L
3459 // A helper type for avoiding boiler-plate.
3460 typedef basic_string_view<_CharT, _Traits> __sv_type;
3461
3462 template<typename _Tp, typename _Res>
3463 using _If_sv = enable_if_t<
3464 __and_<is_convertible<const _Tp&, __sv_type>,
3465 __not_<is_convertible<const _Tp*, const basic_string*>>,
3466 __not_<is_convertible<const _Tp&, const _CharT*>>>::value,
3467 _Res>;
3468
3469 // Allows an implicit conversion to __sv_type.
3470 static __sv_type
3471 _S_to_string_view(__sv_type __svt) noexcept
3472 { return __svt; }
3473
3474 // Wraps a string_view by explicit conversion and thus
3475 // allows to add an internal constructor that does not
3476 // participate in overload resolution when a string_view
3477 // is provided.
3478 struct __sv_wrapper
3479 {
3480 explicit __sv_wrapper(__sv_type __sv) noexcept : _M_sv(__sv) { }
3481 __sv_type _M_sv;
3482 };
3483#endif
3484
3485 public:
3486 // Construct/copy/destroy:
3487 // NB: We overload ctors in some cases instead of using default
3488 // arguments, per 17.4.4.4 para. 2 item 2.
3489
3490 /**
3491 * @brief Default constructor creates an empty string.
3492 */
3493 basic_string()
3494#if _GLIBCXX_FULLY_DYNAMIC_STRING0 == 0
3495 : _M_dataplus(_S_empty_rep()._M_refdata(), _Alloc()) { }
3496#else
3497 : _M_dataplus(_S_construct(size_type(), _CharT(), _Alloc()), _Alloc()){ }
3498#endif
3499
3500 /**
3501 * @brief Construct an empty string using allocator @a a.
3502 */
3503 explicit
3504 basic_string(const _Alloc& __a);
3505
3506 // NB: per LWG issue 42, semantics different from IS:
3507 /**
3508 * @brief Construct string with copy of value of @a str.
3509 * @param __str Source string.
3510 */
3511 basic_string(const basic_string& __str);
3512
3513 // _GLIBCXX_RESOLVE_LIB_DEFECTS
3514 // 2583. no way to supply an allocator for basic_string(str, pos)
3515 /**
3516 * @brief Construct string as copy of a substring.
3517 * @param __str Source string.
3518 * @param __pos Index of first character to copy from.
3519 * @param __a Allocator to use.
3520 */
3521 basic_string(const basic_string& __str, size_type __pos,
3522 const _Alloc& __a = _Alloc());
3523
3524 /**
3525 * @brief Construct string as copy of a substring.
3526 * @param __str Source string.
3527 * @param __pos Index of first character to copy from.
3528 * @param __n Number of characters to copy.
3529 */
3530 basic_string(const basic_string& __str, size_type __pos,
3531 size_type __n);
3532 /**
3533 * @brief Construct string as copy of a substring.
3534 * @param __str Source string.
3535 * @param __pos Index of first character to copy from.
3536 * @param __n Number of characters to copy.
3537 * @param __a Allocator to use.
3538 */
3539 basic_string(const basic_string& __str, size_type __pos,
3540 size_type __n, const _Alloc& __a);
3541
3542 /**
3543 * @brief Construct string initialized by a character %array.
3544 * @param __s Source character %array.
3545 * @param __n Number of characters to copy.
3546 * @param __a Allocator to use (default is default allocator).
3547 *
3548 * NB: @a __s must have at least @a __n characters, &apos;\\0&apos;
3549 * has no special meaning.
3550 */
3551 basic_string(const _CharT* __s, size_type __n,
3552 const _Alloc& __a = _Alloc());
3553 /**
3554 * @brief Construct string as copy of a C string.
3555 * @param __s Source C string.
3556 * @param __a Allocator to use (default is default allocator).
3557 */
3558 basic_string(const _CharT* __s, const _Alloc& __a = _Alloc());
3559 /**
3560 * @brief Construct string as multiple characters.
3561 * @param __n Number of characters.
3562 * @param __c Character to use.
3563 * @param __a Allocator to use (default is default allocator).
3564 */
3565 basic_string(size_type __n, _CharT __c, const _Alloc& __a = _Alloc());
3566
3567#if __cplusplus201703L >= 201103L
3568 /**
3569 * @brief Move construct string.
3570 * @param __str Source string.
3571 *
3572 * The newly-created string contains the exact contents of @a __str.
3573 * @a __str is a valid, but unspecified string.
3574 **/
3575 basic_string(basic_string&& __str)
3576#if _GLIBCXX_FULLY_DYNAMIC_STRING0 == 0
3577 noexcept // FIXME C++11: should always be noexcept.
3578#endif
3579 : _M_dataplus(__str._M_dataplus)
3580 {
3581#if _GLIBCXX_FULLY_DYNAMIC_STRING0 == 0
3582 __str._M_data(_S_empty_rep()._M_refdata());
3583#else
3584 __str._M_data(_S_construct(size_type(), _CharT(), get_allocator()));
3585#endif
3586 }
3587
3588 /**
3589 * @brief Construct string from an initializer %list.
3590 * @param __l std::initializer_list of characters.
3591 * @param __a Allocator to use (default is default allocator).
3592 */
3593 basic_string(initializer_list<_CharT> __l, const _Alloc& __a = _Alloc());
3594#endif // C++11
3595
3596 /**
3597 * @brief Construct string as copy of a range.
3598 * @param __beg Start of range.
3599 * @param __end End of range.
3600 * @param __a Allocator to use (default is default allocator).
3601 */
3602 template<class _InputIterator>
3603 basic_string(_InputIterator __beg, _InputIterator __end,
3604 const _Alloc& __a = _Alloc());
3605
3606#if __cplusplus201703L > 201402L
3607 /**
3608 * @brief Construct string from a substring of a string_view.
3609 * @param __t Source object convertible to string view.
3610 * @param __pos The index of the first character to copy from __t.
3611 * @param __n The number of characters to copy from __t.
3612 * @param __a Allocator to use.
3613 */
3614 template<typename _Tp, typename = _If_sv<_Tp, void>>
3615 basic_string(const _Tp& __t, size_type __pos, size_type __n,
3616 const _Alloc& __a = _Alloc())
3617 : basic_string(_S_to_string_view(__t).substr(__pos, __n), __a) { }
3618
3619 /**
3620 * @brief Construct string from a string_view.
3621 * @param __t Source object convertible to string view.
3622 * @param __a Allocator to use (default is default allocator).
3623 */
3624 template<typename _Tp, typename = _If_sv<_Tp, void>>
3625 explicit
3626 basic_string(const _Tp& __t, const _Alloc& __a = _Alloc())
3627 : basic_string(__sv_wrapper(_S_to_string_view(__t)), __a) { }
3628
3629 /**
3630 * @brief Only internally used: Construct string from a string view
3631 * wrapper.
3632 * @param __svw string view wrapper.
3633 * @param __a Allocator to use.
3634 */
3635 explicit
3636 basic_string(__sv_wrapper __svw, const _Alloc& __a)
3637 : basic_string(__svw._M_sv.data(), __svw._M_sv.size(), __a) { }
3638#endif // C++17
3639
3640 /**
3641 * @brief Destroy the string instance.
3642 */
3643 ~basic_string() _GLIBCXX_NOEXCEPTnoexcept
3644 { _M_rep()->_M_dispose(this->get_allocator()); }
3645
3646 /**
3647 * @brief Assign the value of @a str to this string.
3648 * @param __str Source string.
3649 */
3650 basic_string&
3651 operator=(const basic_string& __str)
3652 { return this->assign(__str); }
3653
3654 /**
3655 * @brief Copy contents of @a s into this string.
3656 * @param __s Source null-terminated string.
3657 */
3658 basic_string&
3659 operator=(const _CharT* __s)
3660 { return this->assign(__s); }
3661
3662 /**
3663 * @brief Set value to string of length 1.
3664 * @param __c Source character.
3665 *
3666 * Assigning to a character makes this string length 1 and
3667 * (*this)[0] == @a c.
3668 */
3669 basic_string&
3670 operator=(_CharT __c)
3671 {
3672 this->assign(1, __c);
3673 return *this;
3674 }
3675
3676#if __cplusplus201703L >= 201103L
3677 /**
3678 * @brief Move assign the value of @a str to this string.
3679 * @param __str Source string.
3680 *
3681 * The contents of @a str are moved into this string (without copying).
3682 * @a str is a valid, but unspecified string.
3683 **/
3684 // PR 58265, this should be noexcept.
3685 basic_string&
3686 operator=(basic_string&& __str)
3687 {
3688 // NB: DR 1204.
3689 this->swap(__str);
3690 return *this;
3691 }
3692
3693 /**
3694 * @brief Set value to string constructed from initializer %list.
3695 * @param __l std::initializer_list.
3696 */
3697 basic_string&
3698 operator=(initializer_list<_CharT> __l)
3699 {
3700 this->assign(__l.begin(), __l.size());
3701 return *this;
3702 }
3703#endif // C++11
3704
3705#if __cplusplus201703L > 201402L
3706 /**
3707 * @brief Set value to string constructed from a string_view.
3708 * @param __svt An object convertible to string_view.
3709 */
3710 template<typename _Tp>
3711 _If_sv<_Tp, basic_string&>
3712 operator=(const _Tp& __svt)
3713 { return this->assign(__svt); }
3714
3715 /**
3716 * @brief Convert to a string_view.
3717 * @return A string_view.
3718 */
3719 operator __sv_type() const noexcept
3720 { return __sv_type(data(), size()); }
3721#endif // C++17
3722
3723 // Iterators:
3724 /**
3725 * Returns a read/write iterator that points to the first character in
3726 * the %string. Unshares the string.
3727 */
3728 iterator
3729 begin() // FIXME C++11: should be noexcept.
3730 {
3731 _M_leak();
3732 return iterator(_M_data());
3733 }
3734
3735 /**
3736 * Returns a read-only (constant) iterator that points to the first
3737 * character in the %string.
3738 */
3739 const_iterator
3740 begin() const _GLIBCXX_NOEXCEPTnoexcept
3741 { return const_iterator(_M_data()); }
3742
3743 /**
3744 * Returns a read/write iterator that points one past the last
3745 * character in the %string. Unshares the string.
3746 */
3747 iterator
3748 end() // FIXME C++11: should be noexcept.
3749 {
3750 _M_leak();
3751 return iterator(_M_data() + this->size());
3752 }
3753
3754 /**
3755 * Returns a read-only (constant) iterator that points one past the
3756 * last character in the %string.
3757 */
3758 const_iterator
3759 end() const _GLIBCXX_NOEXCEPTnoexcept
3760 { return const_iterator(_M_data() + this->size()); }
3761
3762 /**
3763 * Returns a read/write reverse iterator that points to the last
3764 * character in the %string. Iteration is done in reverse element
3765 * order. Unshares the string.
3766 */
3767 reverse_iterator
3768 rbegin() // FIXME C++11: should be noexcept.
3769 { return reverse_iterator(this->end()); }
3770
3771 /**
3772 * Returns a read-only (constant) reverse iterator that points
3773 * to the last character in the %string. Iteration is done in
3774 * reverse element order.
3775 */
3776 const_reverse_iterator
3777 rbegin() const _GLIBCXX_NOEXCEPTnoexcept
3778 { return const_reverse_iterator(this->end()); }
3779
3780 /**
3781 * Returns a read/write reverse iterator that points to one before the
3782 * first character in the %string. Iteration is done in reverse
3783 * element order. Unshares the string.
3784 */
3785 reverse_iterator
3786 rend() // FIXME C++11: should be noexcept.
3787 { return reverse_iterator(this->begin()); }
3788
3789 /**
3790 * Returns a read-only (constant) reverse iterator that points
3791 * to one before the first character in the %string. Iteration
3792 * is done in reverse element order.
3793 */
3794 const_reverse_iterator
3795 rend() const _GLIBCXX_NOEXCEPTnoexcept
3796 { return const_reverse_iterator(this->begin()); }
3797
3798#if __cplusplus201703L >= 201103L
3799 /**
3800 * Returns a read-only (constant) iterator that points to the first
3801 * character in the %string.
3802 */
3803 const_iterator
3804 cbegin() const noexcept
3805 { return const_iterator(this->_M_data()); }
3806
3807 /**
3808 * Returns a read-only (constant) iterator that points one past the
3809 * last character in the %string.
3810 */
3811 const_iterator
3812 cend() const noexcept
3813 { return const_iterator(this->_M_data() + this->size()); }
3814
3815 /**
3816 * Returns a read-only (constant) reverse iterator that points
3817 * to the last character in the %string. Iteration is done in
3818 * reverse element order.
3819 */
3820 const_reverse_iterator
3821 crbegin() const noexcept
3822 { return const_reverse_iterator(this->end()); }
3823
3824 /**
3825 * Returns a read-only (constant) reverse iterator that points
3826 * to one before the first character in the %string. Iteration
3827 * is done in reverse element order.
3828 */
3829 const_reverse_iterator
3830 crend() const noexcept
3831 { return const_reverse_iterator(this->begin()); }
3832#endif
3833
3834 public:
3835 // Capacity:
3836 /// Returns the number of characters in the string, not including any
3837 /// null-termination.
3838 size_type
3839 size() const _GLIBCXX_NOEXCEPTnoexcept
3840 { return _M_rep()->_M_length; }
3841
3842 /// Returns the number of characters in the string, not including any
3843 /// null-termination.
3844 size_type
3845 length() const _GLIBCXX_NOEXCEPTnoexcept
3846 { return _M_rep()->_M_length; }
3847
3848 /// Returns the size() of the largest possible %string.
3849 size_type
3850 max_size() const _GLIBCXX_NOEXCEPTnoexcept
3851 { return _Rep::_S_max_size; }
3852
3853 /**
3854 * @brief Resizes the %string to the specified number of characters.
3855 * @param __n Number of characters the %string should contain.
3856 * @param __c Character to fill any new elements.
3857 *
3858 * This function will %resize the %string to the specified
3859 * number of characters. If the number is smaller than the
3860 * %string's current size the %string is truncated, otherwise
3861 * the %string is extended and new elements are %set to @a __c.
3862 */
3863 void
3864 resize(size_type __n, _CharT __c);
3865
3866 /**
3867 * @brief Resizes the %string to the specified number of characters.
3868 * @param __n Number of characters the %string should contain.
3869 *
3870 * This function will resize the %string to the specified length. If
3871 * the new size is smaller than the %string's current size the %string
3872 * is truncated, otherwise the %string is extended and new characters
3873 * are default-constructed. For basic types such as char, this means
3874 * setting them to 0.
3875 */
3876 void
3877 resize(size_type __n)
3878 { this->resize(__n, _CharT()); }
3879
3880#if __cplusplus201703L >= 201103L
3881 /// A non-binding request to reduce capacity() to size().
3882 void
3883 shrink_to_fit() _GLIBCXX_NOEXCEPTnoexcept
3884 {
3885#if __cpp_exceptions
3886 if (capacity() > size())
3887 {
3888 try
3889 { reserve(0); }
3890 catch(...)
3891 { }
3892 }
3893#endif
3894 }
3895#endif
3896
3897 /**
3898 * Returns the total number of characters that the %string can hold
3899 * before needing to allocate more memory.
3900 */
3901 size_type
3902 capacity() const _GLIBCXX_NOEXCEPTnoexcept
3903 { return _M_rep()->_M_capacity; }
3904
3905 /**
3906 * @brief Attempt to preallocate enough memory for specified number of
3907 * characters.
3908 * @param __res_arg Number of characters required.
3909 * @throw std::length_error If @a __res_arg exceeds @c max_size().
3910 *
3911 * This function attempts to reserve enough memory for the
3912 * %string to hold the specified number of characters. If the
3913 * number requested is more than max_size(), length_error is
3914 * thrown.
3915 *
3916 * The advantage of this function is that if optimal code is a
3917 * necessity and the user can determine the string length that will be
3918 * required, the user can reserve the memory in %advance, and thus
3919 * prevent a possible reallocation of memory and copying of %string
3920 * data.
3921 */
3922 void
3923 reserve(size_type __res_arg = 0);
3924
3925 /**
3926 * Erases the string, making it empty.
3927 */
3928#if _GLIBCXX_FULLY_DYNAMIC_STRING0 == 0
3929 void
3930 clear() _GLIBCXX_NOEXCEPTnoexcept
3931 {
3932 if (_M_rep()->_M_is_shared())
3933 {
3934 _M_rep()->_M_dispose(this->get_allocator());
3935 _M_data(_S_empty_rep()._M_refdata());
3936 }
3937 else
3938 _M_rep()->_M_set_length_and_sharable(0);
3939 }
3940#else
3941 // PR 56166: this should not throw.
3942 void
3943 clear()
3944 { _M_mutate(0, this->size(), 0); }
3945#endif
3946
3947 /**
3948 * Returns true if the %string is empty. Equivalent to
3949 * <code>*this == ""</code>.
3950 */
3951 bool
3952 empty() const _GLIBCXX_NOEXCEPTnoexcept
3953 { return this->size() == 0; }
3954
3955 // Element access:
3956 /**
3957 * @brief Subscript access to the data contained in the %string.
3958 * @param __pos The index of the character to access.
3959 * @return Read-only (constant) reference to the character.
3960 *
3961 * This operator allows for easy, array-style, data access.
3962 * Note that data access with this operator is unchecked and
3963 * out_of_range lookups are not defined. (For checked lookups
3964 * see at().)
3965 */
3966 const_reference
3967 operator[] (size_type __pos) const _GLIBCXX_NOEXCEPTnoexcept
3968 {
3969 __glibcxx_assert(__pos <= size());
3970 return _M_data()[__pos];
3971 }
3972
3973 /**
3974 * @brief Subscript access to the data contained in the %string.
3975 * @param __pos The index of the character to access.
3976 * @return Read/write reference to the character.
3977 *
3978 * This operator allows for easy, array-style, data access.
3979 * Note that data access with this operator is unchecked and
3980 * out_of_range lookups are not defined. (For checked lookups
3981 * see at().) Unshares the string.
3982 */
3983 reference
3984 operator[](size_type __pos)
3985 {
3986 // Allow pos == size() both in C++98 mode, as v3 extension,
3987 // and in C++11 mode.
3988 __glibcxx_assert(__pos <= size());
3989 // In pedantic mode be strict in C++98 mode.
3990 _GLIBCXX_DEBUG_PEDASSERT(__cplusplus >= 201103L || __pos < size());
3991 _M_leak();
3992 return _M_data()[__pos];
3993 }
3994
3995 /**
3996 * @brief Provides access to the data contained in the %string.
3997 * @param __n The index of the character to access.
3998 * @return Read-only (const) reference to the character.
3999 * @throw std::out_of_range If @a n is an invalid index.
4000 *
4001 * This function provides for safer data access. The parameter is
4002 * first checked that it is in the range of the string. The function
4003 * throws out_of_range if the check fails.
4004 */
4005 const_reference
4006 at(size_type __n) const
4007 {
4008 if (__n >= this->size())
4009 __throw_out_of_range_fmt(__N("basic_string::at: __n "("basic_string::at: __n " "(which is %zu) >= this->size() "
"(which is %zu)")
4010 "(which is %zu) >= this->size() "("basic_string::at: __n " "(which is %zu) >= this->size() "
"(which is %zu)")
4011 "(which is %zu)")("basic_string::at: __n " "(which is %zu) >= this->size() "
"(which is %zu)")
,
4012 __n, this->size());
4013 return _M_data()[__n];
4014 }
4015
4016 /**
4017 * @brief Provides access to the data contained in the %string.
4018 * @param __n The index of the character to access.
4019 * @return Read/write reference to the character.
4020 * @throw std::out_of_range If @a n is an invalid index.
4021 *
4022 * This function provides for safer data access. The parameter is
4023 * first checked that it is in the range of the string. The function
4024 * throws out_of_range if the check fails. Success results in
4025 * unsharing the string.
4026 */
4027 reference
4028 at(size_type __n)
4029 {
4030 if (__n >= size())
4031 __throw_out_of_range_fmt(__N("basic_string::at: __n "("basic_string::at: __n " "(which is %zu) >= this->size() "
"(which is %zu)")
4032 "(which is %zu) >= this->size() "("basic_string::at: __n " "(which is %zu) >= this->size() "
"(which is %zu)")
4033 "(which is %zu)")("basic_string::at: __n " "(which is %zu) >= this->size() "
"(which is %zu)")
,
4034 __n, this->size());
4035 _M_leak();
4036 return _M_data()[__n];
4037 }
4038
4039#if __cplusplus201703L >= 201103L
4040 /**
4041 * Returns a read/write reference to the data at the first
4042 * element of the %string.
4043 */
4044 reference
4045 front()
4046 {
4047 __glibcxx_assert(!empty());
4048 return operator[](0);
4049 }
4050
4051 /**
4052 * Returns a read-only (constant) reference to the data at the first
4053 * element of the %string.
4054 */
4055 const_reference
4056 front() const noexcept
4057 {
4058 __glibcxx_assert(!empty());
4059 return operator[](0);
4060 }
4061
4062 /**
4063 * Returns a read/write reference to the data at the last
4064 * element of the %string.
4065 */
4066 reference
4067 back()
4068 {
4069 __glibcxx_assert(!empty());
4070 return operator[](this->size() - 1);
4071 }
4072
4073 /**
4074 * Returns a read-only (constant) reference to the data at the
4075 * last element of the %string.
4076 */
4077 const_reference
4078 back() const noexcept
4079 {
4080 __glibcxx_assert(!empty());
4081 return operator[](this->size() - 1);
4082 }
4083#endif
4084
4085 // Modifiers:
4086 /**
4087 * @brief Append a string to this string.
4088 * @param __str The string to append.
4089 * @return Reference to this string.
4090 */
4091 basic_string&
4092 operator+=(const basic_string& __str)
4093 { return this->append(__str); }
4094
4095 /**
4096 * @brief Append a C string.
4097 * @param __s The C string to append.
4098 * @return Reference to this string.
4099 */
4100 basic_string&
4101 operator+=(const _CharT* __s)
4102 { return this->append(__s); }
4103
4104 /**
4105 * @brief Append a character.
4106 * @param __c The character to append.
4107 * @return Reference to this string.
4108 */
4109 basic_string&
4110 operator+=(_CharT __c)
4111 {
4112 this->push_back(__c);
4113 return *this;
4114 }
4115
4116#if __cplusplus201703L >= 201103L
4117 /**
4118 * @brief Append an initializer_list of characters.
4119 * @param __l The initializer_list of characters to be appended.
4120 * @return Reference to this string.
4121 */
4122 basic_string&
4123 operator+=(initializer_list<_CharT> __l)
4124 { return this->append(__l.begin(), __l.size()); }
4125#endif // C++11
4126
4127#if __cplusplus201703L > 201402L
4128 /**
4129 * @brief Append a string_view.
4130 * @param __svt The object convertible to string_view to be appended.
4131 * @return Reference to this string.
4132 */
4133 template<typename _Tp>
4134 _If_sv<_Tp, basic_string&>
4135 operator+=(const _Tp& __svt)
4136 { return this->append(__svt); }
4137#endif // C++17
4138
4139 /**
4140 * @brief Append a string to this string.
4141 * @param __str The string to append.
4142 * @return Reference to this string.
4143 */
4144 basic_string&
4145 append(const basic_string& __str);
4146
4147 /**
4148 * @brief Append a substring.
4149 * @param __str The string to append.
4150 * @param __pos Index of the first character of str to append.
4151 * @param __n The number of characters to append.
4152 * @return Reference to this string.
4153 * @throw std::out_of_range if @a __pos is not a valid index.
4154 *
4155 * This function appends @a __n characters from @a __str
4156 * starting at @a __pos to this string. If @a __n is is larger
4157 * than the number of available characters in @a __str, the
4158 * remainder of @a __str is appended.
4159 */
4160 basic_string&
4161 append(const basic_string& __str, size_type __pos, size_type __n = npos);
4162
4163 /**
4164 * @brief Append a C substring.
4165 * @param __s The C string to append.
4166 * @param __n The number of characters to append.
4167 * @return Reference to this string.
4168 */
4169 basic_string&
4170 append(const _CharT* __s, size_type __n);
4171
4172 /**
4173 * @brief Append a C string.
4174 * @param __s The C string to append.
4175 * @return Reference to this string.
4176 */
4177 basic_string&
4178 append(const _CharT* __s)
4179 {
4180 __glibcxx_requires_string(__s);
4181 return this->append(__s, traits_type::length(__s));
4182 }
4183
4184 /**
4185 * @brief Append multiple characters.
4186 * @param __n The number of characters to append.
4187 * @param __c The character to use.
4188 * @return Reference to this string.
4189 *
4190 * Appends __n copies of __c to this string.
4191 */
4192 basic_string&
4193 append(size_type __n, _CharT __c);
4194
4195#if __cplusplus201703L >= 201103L
4196 /**
4197 * @brief Append an initializer_list of characters.
4198 * @param __l The initializer_list of characters to append.
4199 * @return Reference to this string.
4200 */
4201 basic_string&
4202 append(initializer_list<_CharT> __l)
4203 { return this->append(__l.begin(), __l.size()); }
4204#endif // C++11
4205
4206 /**
4207 * @brief Append a range of characters.
4208 * @param __first Iterator referencing the first character to append.
4209 * @param __last Iterator marking the end of the range.
4210 * @return Reference to this string.
4211 *
4212 * Appends characters in the range [__first,__last) to this string.
4213 */
4214 template<class _InputIterator>
4215 basic_string&
4216 append(_InputIterator __first, _InputIterator __last)
4217 { return this->replace(_M_iend(), _M_iend(), __first, __last); }
4218
4219#if __cplusplus201703L > 201402L
4220 /**
4221 * @brief Append a string_view.
4222 * @param __svt The object convertible to string_view to be appended.
4223 * @return Reference to this string.
4224 */
4225 template<typename _Tp>
4226 _If_sv<_Tp, basic_string&>
4227 append(const _Tp& __svt)
4228 {
4229 __sv_type __sv = __svt;
4230 return this->append(__sv.data(), __sv.size());
4231 }
4232
4233 /**
4234 * @brief Append a range of characters from a string_view.
4235 * @param __svt The object convertible to string_view to be appended
4236 * from.
4237 * @param __pos The position in the string_view to append from.
4238 * @param __n The number of characters to append from the string_view.
4239 * @return Reference to this string.
4240 */
4241 template<typename _Tp>
4242 _If_sv<_Tp, basic_string&>
4243 append(const _Tp& __svt, size_type __pos, size_type __n = npos)
4244 {
4245 __sv_type __sv = __svt;
4246 return append(__sv.data()
4247 + __sv._M_check(__pos, "basic_string::append"),
4248 __sv._M_limit(__pos, __n));
4249 }
4250#endif // C++17
4251
4252 /**
4253 * @brief Append a single character.
4254 * @param __c Character to append.
4255 */
4256 void
4257 push_back(_CharT __c)
4258 {
4259 const size_type __len = 1 + this->size();
4260 if (__len > this->capacity() || _M_rep()->_M_is_shared())
4261 this->reserve(__len);
4262 traits_type::assign(_M_data()[this->size()], __c);
4263 _M_rep()->_M_set_length_and_sharable(__len);
4264 }
4265
4266 /**
4267 * @brief Set value to contents of another string.
4268 * @param __str Source string to use.
4269 * @return Reference to this string.
4270 */
4271 basic_string&
4272 assign(const basic_string& __str);
4273
4274#if __cplusplus201703L >= 201103L
4275 /**
4276 * @brief Set value to contents of another string.
4277 * @param __str Source string to use.
4278 * @return Reference to this string.
4279 *
4280 * This function sets this string to the exact contents of @a __str.
4281 * @a __str is a valid, but unspecified string.
4282 */
4283 // PR 58265, this should be noexcept.
4284 basic_string&
4285 assign(basic_string&& __str)
4286 {
4287 this->swap(__str);
4288 return *this;
4289 }
4290#endif // C++11
4291
4292 /**
4293 * @brief Set value to a substring of a string.
4294 * @param __str The string to use.
4295 * @param __pos Index of the first character of str.
4296 * @param __n Number of characters to use.
4297 * @return Reference to this string.
4298 * @throw std::out_of_range if @a pos is not a valid index.
4299 *
4300 * This function sets this string to the substring of @a __str
4301 * consisting of @a __n characters at @a __pos. If @a __n is
4302 * is larger than the number of available characters in @a
4303 * __str, the remainder of @a __str is used.
4304 */
4305 basic_string&
4306 assign(const basic_string& __str, size_type __pos, size_type __n = npos)
4307 { return this->assign(__str._M_data()
4308 + __str._M_check(__pos, "basic_string::assign"),
4309 __str._M_limit(__pos, __n)); }
4310
4311 /**
4312 * @brief Set value to a C substring.
4313 * @param __s The C string to use.
4314 * @param __n Number of characters to use.
4315 * @return Reference to this string.
4316 *
4317 * This function sets the value of this string to the first @a __n
4318 * characters of @a __s. If @a __n is is larger than the number of
4319 * available characters in @a __s, the remainder of @a __s is used.
4320 */
4321 basic_string&
4322 assign(const _CharT* __s, size_type __n);
4323
4324 /**
4325 * @brief Set value to contents of a C string.
4326 * @param __s The C string to use.
4327 * @return Reference to this string.
4328 *
4329 * This function sets the value of this string to the value of @a __s.
4330 * The data is copied, so there is no dependence on @a __s once the
4331 * function returns.
4332 */
4333 basic_string&
4334 assign(const _CharT* __s)
4335 {
4336 __glibcxx_requires_string(__s);
4337 return this->assign(__s, traits_type::length(__s));
4338 }
4339
4340 /**
4341 * @brief Set value to multiple characters.
4342 * @param __n Length of the resulting string.
4343 * @param __c The character to use.
4344 * @return Reference to this string.
4345 *
4346 * This function sets the value of this string to @a __n copies of
4347 * character @a __c.
4348 */
4349 basic_string&
4350 assign(size_type __n, _CharT __c)
4351 { return _M_replace_aux(size_type(0), this->size(), __n, __c); }
4352
4353 /**
4354 * @brief Set value to a range of characters.
4355 * @param __first Iterator referencing the first character to append.
4356 * @param __last Iterator marking the end of the range.
4357 * @return Reference to this string.
4358 *
4359 * Sets value of string to characters in the range [__first,__last).
4360 */
4361 template<class _InputIterator>
4362 basic_string&
4363 assign(_InputIterator __first, _InputIterator __last)
4364 { return this->replace(_M_ibegin(), _M_iend(), __first, __last); }
4365
4366#if __cplusplus201703L >= 201103L
4367 /**
4368 * @brief Set value to an initializer_list of characters.
4369 * @param __l The initializer_list of characters to assign.
4370 * @return Reference to this string.
4371 */
4372 basic_string&
4373 assign(initializer_list<_CharT> __l)
4374 { return this->assign(__l.begin(), __l.size()); }
4375#endif // C++11
4376
4377#if __cplusplus201703L > 201402L
4378 /**
4379 * @brief Set value from a string_view.
4380 * @param __svt The source object convertible to string_view.
4381 * @return Reference to this string.
4382 */
4383 template<typename _Tp>
4384 _If_sv<_Tp, basic_string&>
4385 assign(const _Tp& __svt)
4386 {
4387 __sv_type __sv = __svt;
4388 return this->assign(__sv.data(), __sv.size());
4389 }
4390
4391 /**
4392 * @brief Set value from a range of characters in a string_view.
4393 * @param __svt The source object convertible to string_view.
4394 * @param __pos The position in the string_view to assign from.
4395 * @param __n The number of characters to assign.
4396 * @return Reference to this string.
4397 */
4398 template<typename _Tp>
4399 _If_sv<_Tp, basic_string&>
4400 assign(const _Tp& __svt, size_type __pos, size_type __n = npos)
4401 {
4402 __sv_type __sv = __svt;
4403 return assign(__sv.data()
4404 + __sv._M_check(__pos, "basic_string::assign"),
4405 __sv._M_limit(__pos, __n));
4406 }
4407#endif // C++17
4408
4409 /**
4410 * @brief Insert multiple characters.
4411 * @param __p Iterator referencing location in string to insert at.
4412 * @param __n Number of characters to insert
4413 * @param __c The character to insert.
4414 * @throw std::length_error If new length exceeds @c max_size().
4415 *
4416 * Inserts @a __n copies of character @a __c starting at the
4417 * position referenced by iterator @a __p. If adding
4418 * characters causes the length to exceed max_size(),
4419 * length_error is thrown. The value of the string doesn't
4420 * change if an error is thrown.
4421 */
4422 void
4423 insert(iterator __p, size_type __n, _CharT __c)
4424 { this->replace(__p, __p, __n, __c); }
4425
4426 /**
4427 * @brief Insert a range of characters.
4428 * @param __p Iterator referencing location in string to insert at.
4429 * @param __beg Start of range.
4430 * @param __end End of range.
4431 * @throw std::length_error If new length exceeds @c max_size().
4432 *
4433 * Inserts characters in range [__beg,__end). If adding
4434 * characters causes the length to exceed max_size(),
4435 * length_error is thrown. The value of the string doesn't
4436 * change if an error is thrown.
4437 */
4438 template<class _InputIterator>
4439 void
4440 insert(iterator __p, _InputIterator __beg, _InputIterator __end)
4441 { this->replace(__p, __p, __beg, __end); }
4442
4443#if __cplusplus201703L >= 201103L
4444 /**
4445 * @brief Insert an initializer_list of characters.
4446 * @param __p Iterator referencing location in string to insert at.
4447 * @param __l The initializer_list of characters to insert.
4448 * @throw std::length_error If new length exceeds @c max_size().
4449 */
4450 void
4451 insert(iterator __p, initializer_list<_CharT> __l)
4452 {
4453 _GLIBCXX_DEBUG_PEDASSERT(__p >= _M_ibegin() && __p <= _M_iend());
4454 this->insert(__p - _M_ibegin(), __l.begin(), __l.size());
4455 }
4456#endif // C++11
4457
4458 /**
4459 * @brief Insert value of a string.
4460 * @param __pos1 Iterator referencing location in string to insert at.
4461 * @param __str The string to insert.
4462 * @return Reference to this string.
4463 * @throw std::length_error If new length exceeds @c max_size().
4464 *
4465 * Inserts value of @a __str starting at @a __pos1. If adding
4466 * characters causes the length to exceed max_size(),
4467 * length_error is thrown. The value of the string doesn't
4468 * change if an error is thrown.
4469 */
4470 basic_string&
4471 insert(size_type __pos1, const basic_string& __str)
4472 { return this->insert(__pos1, __str, size_type(0), __str.size()); }
4473
4474 /**
4475 * @brief Insert a substring.
4476 * @param __pos1 Iterator referencing location in string to insert at.
4477 * @param __str The string to insert.
4478 * @param __pos2 Start of characters in str to insert.
4479 * @param __n Number of characters to insert.
4480 * @return Reference to this string.
4481 * @throw std::length_error If new length exceeds @c max_size().
4482 * @throw std::out_of_range If @a pos1 > size() or
4483 * @a __pos2 > @a str.size().
4484 *
4485 * Starting at @a pos1, insert @a __n character of @a __str
4486 * beginning with @a __pos2. If adding characters causes the
4487 * length to exceed max_size(), length_error is thrown. If @a
4488 * __pos1 is beyond the end of this string or @a __pos2 is
4489 * beyond the end of @a __str, out_of_range is thrown. The
4490 * value of the string doesn't change if an error is thrown.
4491 */
4492 basic_string&
4493 insert(size_type __pos1, const basic_string& __str,
4494 size_type __pos2, size_type __n = npos)
4495 { return this->insert(__pos1, __str._M_data()
4496 + __str._M_check(__pos2, "basic_string::insert"),
4497 __str._M_limit(__pos2, __n)); }
4498
4499 /**
4500 * @brief Insert a C substring.
4501 * @param __pos Iterator referencing location in string to insert at.
4502 * @param __s The C string to insert.
4503 * @param __n The number of characters to insert.
4504 * @return Reference to this string.
4505 * @throw std::length_error If new length exceeds @c max_size().
4506 * @throw std::out_of_range If @a __pos is beyond the end of this
4507 * string.
4508 *
4509 * Inserts the first @a __n characters of @a __s starting at @a
4510 * __pos. If adding characters causes the length to exceed
4511 * max_size(), length_error is thrown. If @a __pos is beyond
4512 * end(), out_of_range is thrown. The value of the string
4513 * doesn't change if an error is thrown.
4514 */
4515 basic_string&
4516 insert(size_type __pos, const _CharT* __s, size_type __n);
4517
4518 /**
4519 * @brief Insert a C string.
4520 * @param __pos Iterator referencing location in string to insert at.
4521 * @param __s The C string to insert.
4522 * @return Reference to this string.
4523 * @throw std::length_error If new length exceeds @c max_size().
4524 * @throw std::out_of_range If @a pos is beyond the end of this
4525 * string.
4526 *
4527 * Inserts the first @a n characters of @a __s starting at @a __pos. If
4528 * adding characters causes the length to exceed max_size(),
4529 * length_error is thrown. If @a __pos is beyond end(), out_of_range is
4530 * thrown. The value of the string doesn't change if an error is
4531 * thrown.
4532 */
4533 basic_string&
4534 insert(size_type __pos, const _CharT* __s)
4535 {
4536 __glibcxx_requires_string(__s);
4537 return this->insert(__pos, __s, traits_type::length(__s));
4538 }
4539
4540 /**
4541 * @brief Insert multiple characters.
4542 * @param __pos Index in string to insert at.
4543 * @param __n Number of characters to insert
4544 * @param __c The character to insert.
4545 * @return Reference to this string.
4546 * @throw std::length_error If new length exceeds @c max_size().
4547 * @throw std::out_of_range If @a __pos is beyond the end of this
4548 * string.
4549 *
4550 * Inserts @a __n copies of character @a __c starting at index
4551 * @a __pos. If adding characters causes the length to exceed
4552 * max_size(), length_error is thrown. If @a __pos > length(),
4553 * out_of_range is thrown. The value of the string doesn't
4554 * change if an error is thrown.
4555 */
4556 basic_string&
4557 insert(size_type __pos, size_type __n, _CharT __c)
4558 { return _M_replace_aux(_M_check(__pos, "basic_string::insert"),
4559 size_type(0), __n, __c); }
4560
4561 /**
4562 * @brief Insert one character.
4563 * @param __p Iterator referencing position in string to insert at.
4564 * @param __c The character to insert.
4565 * @return Iterator referencing newly inserted char.
4566 * @throw std::length_error If new length exceeds @c max_size().
4567 *
4568 * Inserts character @a __c at position referenced by @a __p.
4569 * If adding character causes the length to exceed max_size(),
4570 * length_error is thrown. If @a __p is beyond end of string,
4571 * out_of_range is thrown. The value of the string doesn't
4572 * change if an error is thrown.
4573 */
4574 iterator
4575 insert(iterator __p, _CharT __c)
4576 {
4577 _GLIBCXX_DEBUG_PEDASSERT(__p >= _M_ibegin() && __p <= _M_iend());
4578 const size_type __pos = __p - _M_ibegin();
4579 _M_replace_aux(__pos, size_type(0), size_type(1), __c);
4580 _M_rep()->_M_set_leaked();
4581 return iterator(_M_data() + __pos);
4582 }
4583
4584#if __cplusplus201703L > 201402L
4585 /**
4586 * @brief Insert a string_view.
4587 * @param __pos Iterator referencing position in string to insert at.
4588 * @param __svt The object convertible to string_view to insert.
4589 * @return Reference to this string.
4590 */
4591 template<typename _Tp>
4592 _If_sv<_Tp, basic_string&>
4593 insert(size_type __pos, const _Tp& __svt)
4594 {
4595 __sv_type __sv = __svt;
4596 return this->insert(__pos, __sv.data(), __sv.size());
4597 }
4598
4599 /**
4600 * @brief Insert a string_view.
4601 * @param __pos Iterator referencing position in string to insert at.
4602 * @param __svt The object convertible to string_view to insert from.
4603 * @param __pos Iterator referencing position in string_view to insert
4604 * from.
4605 * @param __n The number of characters to insert.
4606 * @return Reference to this string.
4607 */
4608 template<typename _Tp>
4609 _If_sv<_Tp, basic_string&>
4610 insert(size_type __pos1, const _Tp& __svt,
4611 size_type __pos2, size_type __n = npos)
4612 {
4613 __sv_type __sv = __svt;
4614 return this->replace(__pos1, size_type(0), __sv.data()
4615 + __sv._M_check(__pos2, "basic_string::insert"),
4616 __sv._M_limit(__pos2, __n));
4617 }
4618#endif // C++17
4619
4620 /**
4621 * @brief Remove characters.
4622 * @param __pos Index of first character to remove (default 0).
4623 * @param __n Number of characters to remove (default remainder).
4624 * @return Reference to this string.
4625 * @throw std::out_of_range If @a pos is beyond the end of this
4626 * string.
4627 *
4628 * Removes @a __n characters from this string starting at @a
4629 * __pos. The length of the string is reduced by @a __n. If
4630 * there are < @a __n characters to remove, the remainder of
4631 * the string is truncated. If @a __p is beyond end of string,
4632 * out_of_range is thrown. The value of the string doesn't
4633 * change if an error is thrown.
4634 */
4635 basic_string&
4636 erase(size_type __pos = 0, size_type __n = npos)
4637 {
4638 _M_mutate(_M_check(__pos, "basic_string::erase"),
4639 _M_limit(__pos, __n), size_type(0));
4640 return *this;
4641 }
4642
4643 /**
4644 * @brief Remove one character.
4645 * @param __position Iterator referencing the character to remove.
4646 * @return iterator referencing same location after removal.
4647 *
4648 * Removes the character at @a __position from this string. The value
4649 * of the string doesn't change if an error is thrown.
4650 */
4651 iterator
4652 erase(iterator __position)
4653 {
4654 _GLIBCXX_DEBUG_PEDASSERT(__position >= _M_ibegin()
4655 && __position < _M_iend());
4656 const size_type __pos = __position - _M_ibegin();
4657 _M_mutate(__pos, size_type(1), size_type(0));
4658 _M_rep()->_M_set_leaked();
4659 return iterator(_M_data() + __pos);
4660 }
4661
4662 /**
4663 * @brief Remove a range of characters.
4664 * @param __first Iterator referencing the first character to remove.
4665 * @param __last Iterator referencing the end of the range.
4666 * @return Iterator referencing location of first after removal.
4667 *
4668 * Removes the characters in the range [first,last) from this string.
4669 * The value of the string doesn't change if an error is thrown.
4670 */
4671 iterator
4672 erase(iterator __first, iterator __last);
4673
4674#if __cplusplus201703L >= 201103L
4675 /**
4676 * @brief Remove the last character.
4677 *
4678 * The string must be non-empty.
4679 */
4680 void
4681 pop_back() // FIXME C++11: should be noexcept.
4682 {
4683 __glibcxx_assert(!empty());
4684 erase(size() - 1, 1);
4685 }
4686#endif // C++11
4687
4688 /**
4689 * @brief Replace characters with value from another string.
4690 * @param __pos Index of first character to replace.
4691 * @param __n Number of characters to be replaced.
4692 * @param __str String to insert.
4693 * @return Reference to this string.
4694 * @throw std::out_of_range If @a pos is beyond the end of this
4695 * string.
4696 * @throw std::length_error If new length exceeds @c max_size().
4697 *
4698 * Removes the characters in the range [__pos,__pos+__n) from
4699 * this string. In place, the value of @a __str is inserted.
4700 * If @a __pos is beyond end of string, out_of_range is thrown.
4701 * If the length of the result exceeds max_size(), length_error
4702 * is thrown. The value of the string doesn't change if an
4703 * error is thrown.
4704 */
4705 basic_string&
4706 replace(size_type __pos, size_type __n, const basic_string& __str)
4707 { return this->replace(__pos, __n, __str._M_data(), __str.size()); }
4708
4709 /**
4710 * @brief Replace characters with value from another string.
4711 * @param __pos1 Index of first character to replace.
4712 * @param __n1 Number of characters to be replaced.
4713 * @param __str String to insert.
4714 * @param __pos2 Index of first character of str to use.
4715 * @param __n2 Number of characters from str to use.
4716 * @return Reference to this string.
4717 * @throw std::out_of_range If @a __pos1 > size() or @a __pos2 >
4718 * __str.size().
4719 * @throw std::length_error If new length exceeds @c max_size().
4720 *
4721 * Removes the characters in the range [__pos1,__pos1 + n) from this
4722 * string. In place, the value of @a __str is inserted. If @a __pos is
4723 * beyond end of string, out_of_range is thrown. If the length of the
4724 * result exceeds max_size(), length_error is thrown. The value of the
4725 * string doesn't change if an error is thrown.
4726 */
4727 basic_string&
4728 replace(size_type __pos1, size_type __n1, const basic_string& __str,
4729 size_type __pos2, size_type __n2 = npos)
4730 { return this->replace(__pos1, __n1, __str._M_data()
4731 + __str._M_check(__pos2, "basic_string::replace"),
4732 __str._M_limit(__pos2, __n2)); }
4733
4734 /**
4735 * @brief Replace characters with value of a C substring.
4736 * @param __pos Index of first character to replace.
4737 * @param __n1 Number of characters to be replaced.
4738 * @param __s C string to insert.
4739 * @param __n2 Number of characters from @a s to use.
4740 * @return Reference to this string.
4741 * @throw std::out_of_range If @a pos1 > size().
4742 * @throw std::length_error If new length exceeds @c max_size().
4743 *
4744 * Removes the characters in the range [__pos,__pos + __n1)
4745 * from this string. In place, the first @a __n2 characters of
4746 * @a __s are inserted, or all of @a __s if @a __n2 is too large. If
4747 * @a __pos is beyond end of string, out_of_range is thrown. If
4748 * the length of result exceeds max_size(), length_error is
4749 * thrown. The value of the string doesn't change if an error
4750 * is thrown.
4751 */
4752 basic_string&
4753 replace(size_type __pos, size_type __n1, const _CharT* __s,
4754 size_type __n2);
4755
4756 /**
4757 * @brief Replace characters with value of a C string.
4758 * @param __pos Index of first character to replace.
4759 * @param __n1 Number of characters to be replaced.
4760 * @param __s C string to insert.
4761 * @return Reference to this string.
4762 * @throw std::out_of_range If @a pos > size().
4763 * @throw std::length_error If new length exceeds @c max_size().
4764 *
4765 * Removes the characters in the range [__pos,__pos + __n1)
4766 * from this string. In place, the characters of @a __s are
4767 * inserted. If @a __pos is beyond end of string, out_of_range
4768 * is thrown. If the length of result exceeds max_size(),
4769 * length_error is thrown. The value of the string doesn't
4770 * change if an error is thrown.
4771 */
4772 basic_string&
4773 replace(size_type __pos, size_type __n1, const _CharT* __s)
4774 {
4775 __glibcxx_requires_string(__s);
4776 return this->replace(__pos, __n1, __s, traits_type::length(__s));
4777 }
4778
4779 /**
4780 * @brief Replace characters with multiple characters.
4781 * @param __pos Index of first character to replace.
4782 * @param __n1 Number of characters to be replaced.
4783 * @param __n2 Number of characters to insert.
4784 * @param __c Character to insert.
4785 * @return Reference to this string.
4786 * @throw std::out_of_range If @a __pos > size().
4787 * @throw std::length_error If new length exceeds @c max_size().
4788 *
4789 * Removes the characters in the range [pos,pos + n1) from this
4790 * string. In place, @a __n2 copies of @a __c are inserted.
4791 * If @a __pos is beyond end of string, out_of_range is thrown.
4792 * If the length of result exceeds max_size(), length_error is
4793 * thrown. The value of the string doesn't change if an error
4794 * is thrown.
4795 */
4796 basic_string&
4797 replace(size_type __pos, size_type __n1, size_type __n2, _CharT __c)
4798 { return _M_replace_aux(_M_check(__pos, "basic_string::replace"),
4799 _M_limit(__pos, __n1), __n2, __c); }
4800
4801 /**
4802 * @brief Replace range of characters with string.
4803 * @param __i1 Iterator referencing start of range to replace.
4804 * @param __i2 Iterator referencing end of range to replace.
4805 * @param __str String value to insert.
4806 * @return Reference to this string.
4807 * @throw std::length_error If new length exceeds @c max_size().
4808 *
4809 * Removes the characters in the range [__i1,__i2). In place,
4810 * the value of @a __str is inserted. If the length of result
4811 * exceeds max_size(), length_error is thrown. The value of
4812 * the string doesn't change if an error is thrown.
4813 */
4814 basic_string&
4815 replace(iterator __i1, iterator __i2, const basic_string& __str)
4816 { return this->replace(__i1, __i2, __str._M_data(), __str.size()); }
4817
4818 /**
4819 * @brief Replace range of characters with C substring.
4820 * @param __i1 Iterator referencing start of range to replace.
4821 * @param __i2 Iterator referencing end of range to replace.
4822 * @param __s C string value to insert.
4823 * @param __n Number of characters from s to insert.
4824 * @return Reference to this string.
4825 * @throw std::length_error If new length exceeds @c max_size().
4826 *
4827 * Removes the characters in the range [__i1,__i2). In place,
4828 * the first @a __n characters of @a __s are inserted. If the
4829 * length of result exceeds max_size(), length_error is thrown.
4830 * The value of the string doesn't change if an error is
4831 * thrown.
4832 */
4833 basic_string&
4834 replace(iterator __i1, iterator __i2, const _CharT* __s, size_type __n)
4835 {
4836 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
4837 && __i2 <= _M_iend());
4838 return this->replace(__i1 - _M_ibegin(), __i2 - __i1, __s, __n);
4839 }
4840
4841 /**
4842 * @brief Replace range of characters with C string.
4843 * @param __i1 Iterator referencing start of range to replace.
4844 * @param __i2 Iterator referencing end of range to replace.
4845 * @param __s C string value to insert.
4846 * @return Reference to this string.
4847 * @throw std::length_error If new length exceeds @c max_size().
4848 *
4849 * Removes the characters in the range [__i1,__i2). In place,
4850 * the characters of @a __s are inserted. If the length of
4851 * result exceeds max_size(), length_error is thrown. The
4852 * value of the string doesn't change if an error is thrown.
4853 */
4854 basic_string&
4855 replace(iterator __i1, iterator __i2, const _CharT* __s)
4856 {
4857 __glibcxx_requires_string(__s);
4858 return this->replace(__i1, __i2, __s, traits_type::length(__s));
4859 }
4860
4861 /**
4862 * @brief Replace range of characters with multiple characters
4863 * @param __i1 Iterator referencing start of range to replace.
4864 * @param __i2 Iterator referencing end of range to replace.
4865 * @param __n Number of characters to insert.
4866 * @param __c Character to insert.
4867 * @return Reference to this string.
4868 * @throw std::length_error If new length exceeds @c max_size().
4869 *
4870 * Removes the characters in the range [__i1,__i2). In place,
4871 * @a __n copies of @a __c are inserted. If the length of
4872 * result exceeds max_size(), length_error is thrown. The
4873 * value of the string doesn't change if an error is thrown.
4874 */
4875 basic_string&
4876 replace(iterator __i1, iterator __i2, size_type __n, _CharT __c)
4877 {
4878 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
4879 && __i2 <= _M_iend());
4880 return _M_replace_aux(__i1 - _M_ibegin(), __i2 - __i1, __n, __c);
4881 }
4882
4883 /**
4884 * @brief Replace range of characters with range.
4885 * @param __i1 Iterator referencing start of range to replace.
4886 * @param __i2 Iterator referencing end of range to replace.
4887 * @param __k1 Iterator referencing start of range to insert.
4888 * @param __k2 Iterator referencing end of range to insert.
4889 * @return Reference to this string.
4890 * @throw std::length_error If new length exceeds @c max_size().
4891 *
4892 * Removes the characters in the range [__i1,__i2). In place,
4893 * characters in the range [__k1,__k2) are inserted. If the
4894 * length of result exceeds max_size(), length_error is thrown.
4895 * The value of the string doesn't change if an error is
4896 * thrown.
4897 */
4898 template<class _InputIterator>
4899 basic_string&
4900 replace(iterator __i1, iterator __i2,
4901 _InputIterator __k1, _InputIterator __k2)
4902 {
4903 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
4904 && __i2 <= _M_iend());
4905 __glibcxx_requires_valid_range(__k1, __k2);
4906 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
4907 return _M_replace_dispatch(__i1, __i2, __k1, __k2, _Integral());
4908 }
4909
4910 // Specializations for the common case of pointer and iterator:
4911 // useful to avoid the overhead of temporary buffering in _M_replace.
4912 basic_string&
4913 replace(iterator __i1, iterator __i2, _CharT* __k1, _CharT* __k2)
4914 {
4915 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
4916 && __i2 <= _M_iend());
4917 __glibcxx_requires_valid_range(__k1, __k2);
4918 return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
4919 __k1, __k2 - __k1);
4920 }
4921
4922 basic_string&
4923 replace(iterator __i1, iterator __i2,
4924 const _CharT* __k1, const _CharT* __k2)
4925 {
4926 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
4927 && __i2 <= _M_iend());
4928 __glibcxx_requires_valid_range(__k1, __k2);
4929 return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
4930 __k1, __k2 - __k1);
4931 }
4932
4933 basic_string&
4934 replace(iterator __i1, iterator __i2, iterator __k1, iterator __k2)
4935 {
4936 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
4937 && __i2 <= _M_iend());
4938 __glibcxx_requires_valid_range(__k1, __k2);
4939 return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
4940 __k1.base(), __k2 - __k1);
4941 }
4942
4943 basic_string&
4944 replace(iterator __i1, iterator __i2,
4945 const_iterator __k1, const_iterator __k2)
4946 {
4947 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
4948 && __i2 <= _M_iend());
4949 __glibcxx_requires_valid_range(__k1, __k2);
4950 return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
4951 __k1.base(), __k2 - __k1);
4952 }
4953
4954#if __cplusplus201703L >= 201103L
4955 /**
4956 * @brief Replace range of characters with initializer_list.
4957 * @param __i1 Iterator referencing start of range to replace.
4958 * @param __i2 Iterator referencing end of range to replace.
4959 * @param __l The initializer_list of characters to insert.
4960 * @return Reference to this string.
4961 * @throw std::length_error If new length exceeds @c max_size().
4962 *
4963 * Removes the characters in the range [__i1,__i2). In place,
4964 * characters in the range [__k1,__k2) are inserted. If the
4965 * length of result exceeds max_size(), length_error is thrown.
4966 * The value of the string doesn't change if an error is
4967 * thrown.
4968 */
4969 basic_string& replace(iterator __i1, iterator __i2,
4970 initializer_list<_CharT> __l)
4971 { return this->replace(__i1, __i2, __l.begin(), __l.end()); }
4972#endif // C++11
4973
4974#if __cplusplus201703L > 201402L
4975 /**
4976 * @brief Replace range of characters with string_view.
4977 * @param __pos The position to replace at.
4978 * @param __n The number of characters to replace.
4979 * @param __svt The object convertible to string_view to insert.
4980 * @return Reference to this string.
4981 */
4982 template<typename _Tp>
4983 _If_sv<_Tp, basic_string&>
4984 replace(size_type __pos, size_type __n, const _Tp& __svt)
4985 {
4986 __sv_type __sv = __svt;
4987 return this->replace(__pos, __n, __sv.data(), __sv.size());
4988 }
4989
4990 /**
4991 * @brief Replace range of characters with string_view.
4992 * @param __pos1 The position to replace at.
4993 * @param __n1 The number of characters to replace.
4994 * @param __svt The object convertible to string_view to insert from.
4995 * @param __pos2 The position in the string_view to insert from.
4996 * @param __n2 The number of characters to insert.
4997 * @return Reference to this string.
4998 */
4999 template<typename _Tp>
5000 _If_sv<_Tp, basic_string&>
5001 replace(size_type __pos1, size_type __n1, const _Tp& __svt,
5002 size_type __pos2, size_type __n2 = npos)
5003 {
5004 __sv_type __sv = __svt;
5005 return this->replace(__pos1, __n1,
5006 __sv.data() + __sv._M_check(__pos2, "basic_string::replace"),
5007 __sv._M_limit(__pos2, __n2));
5008 }
5009
5010 /**
5011 * @brief Replace range of characters with string_view.
5012 * @param __i1 An iterator referencing the start position
5013 to replace at.
5014 * @param __i2 An iterator referencing the end position
5015 for the replace.
5016 * @param __svt The object convertible to string_view to insert from.
5017 * @return Reference to this string.
5018 */
5019 template<typename _Tp>
5020 _If_sv<_Tp, basic_string&>
5021 replace(const_iterator __i1, const_iterator __i2, const _Tp& __svt)
5022 {
5023 __sv_type __sv = __svt;
5024 return this->replace(__i1 - begin(), __i2 - __i1, __sv);
5025 }
5026#endif // C++17
5027
5028 private:
5029 template<class _Integer>
5030 basic_string&
5031 _M_replace_dispatch(iterator __i1, iterator __i2, _Integer __n,
5032 _Integer __val, __true_type)
5033 { return _M_replace_aux(__i1 - _M_ibegin(), __i2 - __i1, __n, __val); }
5034
5035 template<class _InputIterator>
5036 basic_string&
5037 _M_replace_dispatch(iterator __i1, iterator __i2, _InputIterator __k1,
5038 _InputIterator __k2, __false_type);
5039
5040 basic_string&
5041 _M_replace_aux(size_type __pos1, size_type __n1, size_type __n2,
5042 _CharT __c);
5043
5044 basic_string&
5045 _M_replace_safe(size_type __pos1, size_type __n1, const _CharT* __s,
5046 size_type __n2);
5047
5048 // _S_construct_aux is used to implement the 21.3.1 para 15 which
5049 // requires special behaviour if _InIter is an integral type
5050 template<class _InIterator>
5051 static _CharT*
5052 _S_construct_aux(_InIterator __beg, _InIterator __end,
5053 const _Alloc& __a, __false_type)
5054 {
5055 typedef typename iterator_traits<_InIterator>::iterator_category _Tag;
5056 return _S_construct(__beg, __end, __a, _Tag());
5057 }
5058
5059 // _GLIBCXX_RESOLVE_LIB_DEFECTS
5060 // 438. Ambiguity in the "do the right thing" clause
5061 template<class _Integer>
5062 static _CharT*
5063 _S_construct_aux(_Integer __beg, _Integer __end,
5064 const _Alloc& __a, __true_type)
5065 { return _S_construct_aux_2(static_cast<size_type>(__beg),
5066 __end, __a); }
5067
5068 static _CharT*
5069 _S_construct_aux_2(size_type __req, _CharT __c, const _Alloc& __a)
5070 { return _S_construct(__req, __c, __a); }
5071
5072 template<class _InIterator>
5073 static _CharT*
5074 _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a)
5075 {
5076 typedef typename std::__is_integer<_InIterator>::__type _Integral;
5077 return _S_construct_aux(__beg, __end, __a, _Integral());
5078 }
5079
5080 // For Input Iterators, used in istreambuf_iterators, etc.
5081 template<class _InIterator>
5082 static _CharT*
5083 _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a,
5084 input_iterator_tag);
5085
5086 // For forward_iterators up to random_access_iterators, used for
5087 // string::iterator, _CharT*, etc.
5088 template<class _FwdIterator>
5089 static _CharT*
5090 _S_construct(_FwdIterator __beg, _FwdIterator __end, const _Alloc& __a,
5091 forward_iterator_tag);
5092
5093 static _CharT*
5094 _S_construct(size_type __req, _CharT __c, const _Alloc& __a);
5095
5096 public:
5097
5098 /**
5099 * @brief Copy substring into C string.
5100 * @param __s C string to copy value into.
5101 * @param __n Number of characters to copy.
5102 * @param __pos Index of first character to copy.
5103 * @return Number of characters actually copied
5104 * @throw std::out_of_range If __pos > size().
5105 *
5106 * Copies up to @a __n characters starting at @a __pos into the
5107 * C string @a __s. If @a __pos is %greater than size(),
5108 * out_of_range is thrown.
5109 */
5110 size_type
5111 copy(_CharT* __s, size_type __n, size_type __pos = 0) const;
5112
5113 /**
5114 * @brief Swap contents with another string.
5115 * @param __s String to swap with.
5116 *
5117 * Exchanges the contents of this string with that of @a __s in constant
5118 * time.
5119 */
5120 // PR 58265, this should be noexcept.
5121 void
5122 swap(basic_string& __s);
5123
5124 // String operations:
5125 /**
5126 * @brief Return const pointer to null-terminated contents.
5127 *
5128 * This is a handle to internal data. Do not modify or dire things may
5129 * happen.
5130 */
5131 const _CharT*
5132 c_str() const _GLIBCXX_NOEXCEPTnoexcept
5133 { return _M_data(); }
5134
5135 /**
5136 * @brief Return const pointer to contents.
5137 *
5138 * This is a pointer to internal data. It is undefined to modify
5139 * the contents through the returned pointer. To get a pointer that
5140 * allows modifying the contents use @c &str[0] instead,
5141 * (or in C++17 the non-const @c str.data() overload).
5142 */
5143 const _CharT*
5144 data() const _GLIBCXX_NOEXCEPTnoexcept
5145 { return _M_data(); }
5146
5147#if __cplusplus201703L > 201402L
5148 /**
5149 * @brief Return non-const pointer to contents.
5150 *
5151 * This is a pointer to the character sequence held by the string.
5152 * Modifying the characters in the sequence is allowed.
5153 */
5154 _CharT*
5155 data() noexcept
5156 {
5157 _M_leak();
5158 return _M_data();
5159 }
5160#endif
5161
5162 /**
5163 * @brief Return copy of allocator used to construct this string.
5164 */
5165 allocator_type
5166 get_allocator() const _GLIBCXX_NOEXCEPTnoexcept
5167 { return _M_dataplus; }
5168
5169 /**
5170 * @brief Find position of a C substring.
5171 * @param __s C string to locate.
5172 * @param __pos Index of character to search from.
5173 * @param __n Number of characters from @a s to search for.
5174 * @return Index of start of first occurrence.
5175 *
5176 * Starting from @a __pos, searches forward for the first @a
5177 * __n characters in @a __s within this string. If found,
5178 * returns the index where it begins. If not found, returns
5179 * npos.
5180 */
5181 size_type
5182 find(const _CharT* __s, size_type __pos, size_type __n) const
5183 _GLIBCXX_NOEXCEPTnoexcept;
5184
5185 /**
5186 * @brief Find position of a string.
5187 * @param __str String to locate.
5188 * @param __pos Index of character to search from (default 0).
5189 * @return Index of start of first occurrence.
5190 *
5191 * Starting from @a __pos, searches forward for value of @a __str within
5192 * this string. If found, returns the index where it begins. If not
5193 * found, returns npos.
5194 */
5195 size_type
5196 find(const basic_string& __str, size_type __pos = 0) const
5197 _GLIBCXX_NOEXCEPTnoexcept
5198 { return this->find(__str.data(), __pos, __str.size()); }
5199
5200 /**
5201 * @brief Find position of a C string.
5202 * @param __s C string to locate.
5203 * @param __pos Index of character to search from (default 0).
5204 * @return Index of start of first occurrence.
5205 *
5206 * Starting from @a __pos, searches forward for the value of @a
5207 * __s within this string. If found, returns the index where
5208 * it begins. If not found, returns npos.
5209 */
5210 size_type
5211 find(const _CharT* __s, size_type __pos = 0) const _GLIBCXX_NOEXCEPTnoexcept
5212 {
5213 __glibcxx_requires_string(__s);
5214 return this->find(__s, __pos, traits_type::length(__s));
5215 }
5216
5217 /**
5218 * @brief Find position of a character.
5219 * @param __c Character to locate.
5220 * @param __pos Index of character to search from (default 0).
5221 * @return Index of first occurrence.
5222 *
5223 * Starting from @a __pos, searches forward for @a __c within
5224 * this string. If found, returns the index where it was
5225 * found. If not found, returns npos.
5226 */
5227 size_type
5228 find(_CharT __c, size_type __pos = 0) const _GLIBCXX_NOEXCEPTnoexcept;
5229
5230#if __cplusplus201703L > 201402L
5231 /**
5232 * @brief Find position of a string_view.
5233 * @param __svt The object convertible to string_view to locate.
5234 * @param __pos Index of character to search from (default 0).
5235 * @return Index of start of first occurrence.
5236 */
5237 template<typename _Tp>
5238 _If_sv<_Tp, size_type>
5239 find(const _Tp& __svt, size_type __pos = 0) const
5240 noexcept(is_same<_Tp, __sv_type>::value)
5241 {
5242 __sv_type __sv = __svt;
5243 return this->find(__sv.data(), __pos, __sv.size());
5244 }
5245#endif // C++17
5246
5247 /**
5248 * @brief Find last position of a string.
5249 * @param __str String to locate.
5250 * @param __pos Index of character to search back from (default end).
5251 * @return Index of start of last occurrence.
5252 *
5253 * Starting from @a __pos, searches backward for value of @a
5254 * __str within this string. If found, returns the index where
5255 * it begins. If not found, returns npos.
5256 */
5257 size_type
5258 rfind(const basic_string& __str, size_type __pos = npos) const
5259 _GLIBCXX_NOEXCEPTnoexcept
5260 { return this->rfind(__str.data(), __pos, __str.size()); }
5261
5262 /**
5263 * @brief Find last position of a C substring.
5264 * @param __s C string to locate.
5265 * @param __pos Index of character to search back from.
5266 * @param __n Number of characters from s to search for.
5267 * @return Index of start of last occurrence.
5268 *
5269 * Starting from @a __pos, searches backward for the first @a
5270 * __n characters in @a __s within this string. If found,
5271 * returns the index where it begins. If not found, returns
5272 * npos.
5273 */
5274 size_type
5275 rfind(const _CharT* __s, size_type __pos, size_type __n) const
5276 _GLIBCXX_NOEXCEPTnoexcept;
5277
5278 /**
5279 * @brief Find last position of a C string.
5280 * @param __s C string to locate.
5281 * @param __pos Index of character to start search at (default end).
5282 * @return Index of start of last occurrence.
5283 *
5284 * Starting from @a __pos, searches backward for the value of
5285 * @a __s within this string. If found, returns the index
5286 * where it begins. If not found, returns npos.
5287 */
5288 size_type
5289 rfind(const _CharT* __s, size_type __pos = npos) const _GLIBCXX_NOEXCEPTnoexcept
5290 {
5291 __glibcxx_requires_string(__s);
5292 return this->rfind(__s, __pos, traits_type::length(__s));
5293 }
5294
5295 /**
5296 * @brief Find last position of a character.
5297 * @param __c Character to locate.
5298 * @param __pos Index of character to search back from (default end).
5299 * @return Index of last occurrence.
5300 *
5301 * Starting from @a __pos, searches backward for @a __c within
5302 * this string. If found, returns the index where it was
5303 * found. If not found, returns npos.
5304 */
5305 size_type
5306 rfind(_CharT __c, size_type __pos = npos) const _GLIBCXX_NOEXCEPTnoexcept;
5307
5308#if __cplusplus201703L > 201402L
5309 /**
5310 * @brief Find last position of a string_view.
5311 * @param __svt The object convertible to string_view to locate.
5312 * @param __pos Index of character to search back from (default end).
5313 * @return Index of start of last occurrence.
5314 */
5315 template<typename _Tp>
5316 _If_sv<_Tp, size_type>
5317 rfind(const _Tp& __svt, size_type __pos = npos) const
5318 noexcept(is_same<_Tp, __sv_type>::value)
5319 {
5320 __sv_type __sv = __svt;
5321 return this->rfind(__sv.data(), __pos, __sv.size());
5322 }
5323#endif // C++17
5324
5325 /**
5326 * @brief Find position of a character of string.
5327 * @param __str String containing characters to locate.
5328 * @param __pos Index of character to search from (default 0).
5329 * @return Index of first occurrence.
5330 *
5331 * Starting from @a __pos, searches forward for one of the
5332 * characters of @a __str within this string. If found,
5333 * returns the index where it was found. If not found, returns
5334 * npos.
5335 */
5336 size_type
5337 find_first_of(const basic_string& __str, size_type __pos = 0) const
5338 _GLIBCXX_NOEXCEPTnoexcept
5339 { return this->find_first_of(__str.data(), __pos, __str.size()); }
5340
5341 /**
5342 * @brief Find position of a character of C substring.
5343 * @param __s String containing characters to locate.
5344 * @param __pos Index of character to search from.
5345 * @param __n Number of characters from s to search for.
5346 * @return Index of first occurrence.
5347 *
5348 * Starting from @a __pos, searches forward for one of the
5349 * first @a __n characters of @a __s within this string. If
5350 * found, returns the index where it was found. If not found,
5351 * returns npos.
5352 */
5353 size_type
5354 find_first_of(const _CharT* __s, size_type __pos, size_type __n) const
5355 _GLIBCXX_NOEXCEPTnoexcept;
5356
5357 /**
5358 * @brief Find position of a character of C string.
5359 * @param __s String containing characters to locate.
5360 * @param __pos Index of character to search from (default 0).
5361 * @return Index of first occurrence.
5362 *
5363 * Starting from @a __pos, searches forward for one of the
5364 * characters of @a __s within this string. If found, returns
5365 * the index where it was found. If not found, returns npos.
5366 */
5367 size_type
5368 find_first_of(const _CharT* __s, size_type __pos = 0) const
5369 _GLIBCXX_NOEXCEPTnoexcept
5370 {
5371 __glibcxx_requires_string(__s);
5372 return this->find_first_of(__s, __pos, traits_type::length(__s));
5373 }
5374
5375 /**
5376 * @brief Find position of a character.
5377 * @param __c Character to locate.
5378 * @param __pos Index of character to search from (default 0).
5379 * @return Index of first occurrence.
5380 *
5381 * Starting from @a __pos, searches forward for the character
5382 * @a __c within this string. If found, returns the index
5383 * where it was found. If not found, returns npos.
5384 *
5385 * Note: equivalent to find(__c, __pos).
5386 */
5387 size_type
5388 find_first_of(_CharT __c, size_type __pos = 0) const _GLIBCXX_NOEXCEPTnoexcept
5389 { return this->find(__c, __pos); }
5390
5391#if __cplusplus201703L > 201402L
5392 /**
5393 * @brief Find position of a character of a string_view.
5394 * @param __svt An object convertible to string_view containing
5395 * characters to locate.
5396 * @param __pos Index of character to search from (default 0).
5397 * @return Index of first occurrence.
5398 */
5399 template<typename _Tp>
5400 _If_sv<_Tp, size_type>
5401 find_first_of(const _Tp& __svt, size_type __pos = 0) const
5402 noexcept(is_same<_Tp, __sv_type>::value)
5403 {
5404 __sv_type __sv = __svt;
5405 return this->find_first_of(__sv.data(), __pos, __sv.size());
5406 }
5407#endif // C++17
5408
5409 /**
5410 * @brief Find last position of a character of string.
5411 * @param __str String containing characters to locate.
5412 * @param __pos Index of character to search back from (default end).
5413 * @return Index of last occurrence.
5414 *
5415 * Starting from @a __pos, searches backward for one of the
5416 * characters of @a __str within this string. If found,
5417 * returns the index where it was found. If not found, returns
5418 * npos.
5419 */
5420 size_type
5421 find_last_of(const basic_string& __str, size_type __pos = npos) const
5422 _GLIBCXX_NOEXCEPTnoexcept
5423 { return this->find_last_of(__str.data(), __pos, __str.size()); }
5424
5425 /**
5426 * @brief Find last position of a character of C substring.
5427 * @param __s C string containing characters to locate.
5428 * @param __pos Index of character to search back from.
5429 * @param __n Number of characters from s to search for.
5430 * @return Index of last occurrence.
5431 *
5432 * Starting from @a __pos, searches backward for one of the
5433 * first @a __n characters of @a __s within this string. If
5434 * found, returns the index where it was found. If not found,
5435 * returns npos.
5436 */
5437 size_type
5438 find_last_of(const _CharT* __s, size_type __pos, size_type __n) const
5439 _GLIBCXX_NOEXCEPTnoexcept;
5440
5441 /**
5442 * @brief Find last position of a character of C string.
5443 * @param __s C string containing characters to locate.
5444 * @param __pos Index of character to search back from (default end).
5445 * @return Index of last occurrence.
5446 *
5447 * Starting from @a __pos, searches backward for one of the
5448 * characters of @a __s within this string. If found, returns
5449 * the index where it was found. If not found, returns npos.
5450 */
5451 size_type
5452 find_last_of(const _CharT* __s, size_type __pos = npos) const
5453 _GLIBCXX_NOEXCEPTnoexcept
5454 {
5455 __glibcxx_requires_string(__s);
5456 return this->find_last_of(__s, __pos, traits_type::length(__s));
5457 }
5458
5459 /**
5460 * @brief Find last position of a character.
5461 * @param __c Character to locate.
5462 * @param __pos Index of character to search back from (default end).
5463 * @return Index of last occurrence.
5464 *
5465 * Starting from @a __pos, searches backward for @a __c within
5466 * this string. If found, returns the index where it was
5467 * found. If not found, returns npos.
5468 *
5469 * Note: equivalent to rfind(__c, __pos).
5470 */
5471 size_type
5472 find_last_of(_CharT __c, size_type __pos = npos) const _GLIBCXX_NOEXCEPTnoexcept
5473 { return this->rfind(__c, __pos); }
5474
5475#if __cplusplus201703L > 201402L
5476 /**
5477 * @brief Find last position of a character of string.
5478 * @param __svt An object convertible to string_view containing
5479 * characters to locate.
5480 * @param __pos Index of character to search back from (default end).
5481 * @return Index of last occurrence.
5482 */
5483 template<typename _Tp>
5484 _If_sv<_Tp, size_type>
5485 find_last_of(const _Tp& __svt, size_type __pos = npos) const
5486 noexcept(is_same<_Tp, __sv_type>::value)
5487 {
5488 __sv_type __sv = __svt;
5489 return this->find_last_of(__sv.data(), __pos, __sv.size());
5490 }
5491#endif // C++17
5492
5493 /**
5494 * @brief Find position of a character not in string.
5495 * @param __str String containing characters to avoid.
5496 * @param __pos Index of character to search from (default 0).
5497 * @return Index of first occurrence.
5498 *
5499 * Starting from @a __pos, searches forward for a character not contained
5500 * in @a __str within this string. If found, returns the index where it
5501 * was found. If not found, returns npos.
5502 */
5503 size_type
5504 find_first_not_of(const basic_string& __str, size_type __pos = 0) const
5505 _GLIBCXX_NOEXCEPTnoexcept
5506 { return this->find_first_not_of(__str.data(), __pos, __str.size()); }
5507
5508 /**
5509 * @brief Find position of a character not in C substring.
5510 * @param __s C string containing characters to avoid.
5511 * @param __pos Index of character to search from.
5512 * @param __n Number of characters from __s to consider.
5513 * @return Index of first occurrence.
5514 *
5515 * Starting from @a __pos, searches forward for a character not
5516 * contained in the first @a __n characters of @a __s within
5517 * this string. If found, returns the index where it was
5518 * found. If not found, returns npos.
5519 */
5520 size_type
5521 find_first_not_of(const _CharT* __s, size_type __pos,
5522 size_type __n) const _GLIBCXX_NOEXCEPTnoexcept;
5523
5524 /**
5525 * @brief Find position of a character not in C string.
5526 * @param __s C string containing characters to avoid.
5527 * @param __pos Index of character to search from (default 0).
5528 * @return Index of first occurrence.
5529 *
5530 * Starting from @a __pos, searches forward for a character not
5531 * contained in @a __s within this string. If found, returns
5532 * the index where it was found. If not found, returns npos.
5533 */
5534 size_type
5535 find_first_not_of(const _CharT* __s, size_type __pos = 0) const
5536 _GLIBCXX_NOEXCEPTnoexcept
5537 {
5538 __glibcxx_requires_string(__s);
5539 return this->find_first_not_of(__s, __pos, traits_type::length(__s));
5540 }
5541
5542 /**
5543 * @brief Find position of a different character.
5544 * @param __c Character to avoid.
5545 * @param __pos Index of character to search from (default 0).
5546 * @return Index of first occurrence.
5547 *
5548 * Starting from @a __pos, searches forward for a character
5549 * other than @a __c within this string. If found, returns the
5550 * index where it was found. If not found, returns npos.
5551 */
5552 size_type
5553 find_first_not_of(_CharT __c, size_type __pos = 0) const
5554 _GLIBCXX_NOEXCEPTnoexcept;
5555
5556#if __cplusplus201703L > 201402L
5557 /**
5558 * @brief Find position of a character not in a string_view.
5559 * @param __svt An object convertible to string_view containing
5560 * characters to avoid.
5561 * @param __pos Index of character to search from (default 0).
5562 * @return Index of first occurrence.
5563 */
5564 template<typename _Tp>
5565 _If_sv<_Tp, size_type>
5566 find_first_not_of(const _Tp& __svt, size_type __pos = 0) const
5567 noexcept(is_same<_Tp, __sv_type>::value)
5568 {
5569 __sv_type __sv = __svt;
5570 return this->find_first_not_of(__sv.data(), __pos, __sv.size());
5571 }
5572#endif // C++17
5573
5574 /**
5575 * @brief Find last position of a character not in string.
5576 * @param __str String containing characters to avoid.
5577 * @param __pos Index of character to search back from (default end).
5578 * @return Index of last occurrence.
5579 *
5580 * Starting from @a __pos, searches backward for a character
5581 * not contained in @a __str within this string. If found,
5582 * returns the index where it was found. If not found, returns
5583 * npos.
5584 */
5585 size_type
5586 find_last_not_of(const basic_string& __str, size_type __pos = npos) const
5587 _GLIBCXX_NOEXCEPTnoexcept
5588 { return this->find_last_not_of(__str.data(), __pos, __str.size()); }
5589
5590 /**
5591 * @brief Find last position of a character not in C substring.
5592 * @param __s C string containing characters to avoid.
5593 * @param __pos Index of character to search back from.
5594 * @param __n Number of characters from s to consider.
5595 * @return Index of last occurrence.
5596 *
5597 * Starting from @a __pos, searches backward for a character not
5598 * contained in the first @a __n characters of @a __s within this string.
5599 * If found, returns the index where it was found. If not found,
5600 * returns npos.
5601 */
5602 size_type
5603 find_last_not_of(const _CharT* __s, size_type __pos,
5604 size_type __n) const _GLIBCXX_NOEXCEPTnoexcept;
5605 /**
5606 * @brief Find last position of a character not in C string.
5607 * @param __s C string containing characters to avoid.
5608 * @param __pos Index of character to search back from (default end).
5609 * @return Index of last occurrence.
5610 *
5611 * Starting from @a __pos, searches backward for a character
5612 * not contained in @a __s within this string. If found,
5613 * returns the index where it was found. If not found, returns
5614 * npos.
5615 */
5616 size_type
5617 find_last_not_of(const _CharT* __s, size_type __pos = npos) const
5618 _GLIBCXX_NOEXCEPTnoexcept
5619 {
5620 __glibcxx_requires_string(__s);
5621 return this->find_last_not_of(__s, __pos, traits_type::length(__s));
5622 }
5623
5624 /**
5625 * @brief Find last position of a different character.
5626 * @param __c Character to avoid.
5627 * @param __pos Index of character to search back from (default end).
5628 * @return Index of last occurrence.
5629 *
5630 * Starting from @a __pos, searches backward for a character other than
5631 * @a __c within this string. If found, returns the index where it was
5632 * found. If not found, returns npos.
5633 */
5634 size_type
5635 find_last_not_of(_CharT __c, size_type __pos = npos) const
5636 _GLIBCXX_NOEXCEPTnoexcept;
5637
5638#if __cplusplus201703L > 201402L
5639 /**
5640 * @brief Find last position of a character not in a string_view.
5641 * @param __svt An object convertible to string_view containing
5642 * characters to avoid.
5643 * @param __pos Index of character to search back from (default end).
5644 * @return Index of last occurrence.
5645 */
5646 template<typename _Tp>
5647 _If_sv<_Tp, size_type>
5648 find_last_not_of(const _Tp& __svt, size_type __pos = npos) const
5649 noexcept(is_same<_Tp, __sv_type>::value)
5650 {
5651 __sv_type __sv = __svt;
5652 return this->find_last_not_of(__sv.data(), __pos, __sv.size());
5653 }
5654#endif // C++17
5655
5656 /**
5657 * @brief Get a substring.
5658 * @param __pos Index of first character (default 0).
5659 * @param __n Number of characters in substring (default remainder).
5660 * @return The new string.
5661 * @throw std::out_of_range If __pos > size().
5662 *
5663 * Construct and return a new string using the @a __n
5664 * characters starting at @a __pos. If the string is too
5665 * short, use the remainder of the characters. If @a __pos is
5666 * beyond the end of the string, out_of_range is thrown.
5667 */
5668 basic_string
5669 substr(size_type __pos = 0, size_type __n = npos) const
5670 { return basic_string(*this,
5671 _M_check(__pos, "basic_string::substr"), __n); }
5672
5673 /**
5674 * @brief Compare to a string.
5675 * @param __str String to compare against.
5676 * @return Integer < 0, 0, or > 0.
5677 *
5678 * Returns an integer < 0 if this string is ordered before @a
5679 * __str, 0 if their values are equivalent, or > 0 if this
5680 * string is ordered after @a __str. Determines the effective
5681 * length rlen of the strings to compare as the smallest of
5682 * size() and str.size(). The function then compares the two
5683 * strings by calling traits::compare(data(), str.data(),rlen).
5684 * If the result of the comparison is nonzero returns it,
5685 * otherwise the shorter one is ordered first.
5686 */
5687 int
5688 compare(const basic_string& __str) const
5689 {
5690 const size_type __size = this->size();
5691 const size_type __osize = __str.size();
5692 const size_type __len = std::min(__size, __osize);
5693
5694 int __r = traits_type::compare(_M_data(), __str.data(), __len);
5695 if (!__r)
5696 __r = _S_compare(__size, __osize);
5697 return __r;
5698 }
5699
5700#if __cplusplus201703L > 201402L
5701 /**
5702 * @brief Compare to a string_view.
5703 * @param __svt An object convertible to string_view to compare against.
5704 * @return Integer < 0, 0, or > 0.
5705 */
5706 template<typename _Tp>
5707 _If_sv<_Tp, int>
5708 compare(const _Tp& __svt) const
5709 noexcept(is_same<_Tp, __sv_type>::value)
5710 {
5711 __sv_type __sv = __svt;
5712 const size_type __size = this->size();
5713 const size_type __osize = __sv.size();
5714 const size_type __len = std::min(__size, __osize);
5715
5716 int __r = traits_type::compare(_M_data(), __sv.data(), __len);
5717 if (!__r)
5718 __r = _S_compare(__size, __osize);
5719 return __r;
5720 }
5721
5722 /**
5723 * @brief Compare to a string_view.
5724 * @param __pos A position in the string to start comparing from.
5725 * @param __n The number of characters to compare.
5726 * @param __svt An object convertible to string_view to compare
5727 * against.
5728 * @return Integer < 0, 0, or > 0.
5729 */
5730 template<typename _Tp>
5731 _If_sv<_Tp, int>
5732 compare(size_type __pos, size_type __n, const _Tp& __svt) const
5733 noexcept(is_same<_Tp, __sv_type>::value)
5734 {
5735 __sv_type __sv = __svt;
5736 return __sv_type(*this).substr(__pos, __n).compare(__sv);
5737 }
5738
5739 /**
5740 * @brief Compare to a string_view.
5741 * @param __pos1 A position in the string to start comparing from.
5742 * @param __n1 The number of characters to compare.
5743 * @param __svt An object convertible to string_view to compare
5744 * against.
5745 * @param __pos2 A position in the string_view to start comparing from.
5746 * @param __n2 The number of characters to compare.
5747 * @return Integer < 0, 0, or > 0.
5748 */
5749 template<typename _Tp>
5750 _If_sv<_Tp, int>
5751 compare(size_type __pos1, size_type __n1, const _Tp& __svt,
5752 size_type __pos2, size_type __n2 = npos) const
5753 noexcept(is_same<_Tp, __sv_type>::value)
5754 {
5755 __sv_type __sv = __svt;
5756 return __sv_type(*this)
5757 .substr(__pos1, __n1).compare(__sv.substr(__pos2, __n2));
5758 }
5759#endif // C++17
5760
5761 /**
5762 * @brief Compare substring to a string.
5763 * @param __pos Index of first character of substring.
5764 * @param __n Number of characters in substring.
5765 * @param __str String to compare against.
5766 * @return Integer < 0, 0, or > 0.
5767 *
5768 * Form the substring of this string from the @a __n characters
5769 * starting at @a __pos. Returns an integer < 0 if the
5770 * substring is ordered before @a __str, 0 if their values are
5771 * equivalent, or > 0 if the substring is ordered after @a
5772 * __str. Determines the effective length rlen of the strings
5773 * to compare as the smallest of the length of the substring
5774 * and @a __str.size(). The function then compares the two
5775 * strings by calling
5776 * traits::compare(substring.data(),str.data(),rlen). If the
5777 * result of the comparison is nonzero returns it, otherwise
5778 * the shorter one is ordered first.
5779 */
5780 int
5781 compare(size_type __pos, size_type __n, const basic_string& __str) const;
5782
5783 /**
5784 * @brief Compare substring to a substring.
5785 * @param __pos1 Index of first character of substring.
5786 * @param __n1 Number of characters in substring.
5787 * @param __str String to compare against.
5788 * @param __pos2 Index of first character of substring of str.
5789 * @param __n2 Number of characters in substring of str.
5790 * @return Integer < 0, 0, or > 0.
5791 *
5792 * Form the substring of this string from the @a __n1
5793 * characters starting at @a __pos1. Form the substring of @a
5794 * __str from the @a __n2 characters starting at @a __pos2.
5795 * Returns an integer < 0 if this substring is ordered before
5796 * the substring of @a __str, 0 if their values are equivalent,
5797 * or > 0 if this substring is ordered after the substring of
5798 * @a __str. Determines the effective length rlen of the
5799 * strings to compare as the smallest of the lengths of the
5800 * substrings. The function then compares the two strings by
5801 * calling
5802 * traits::compare(substring.data(),str.substr(pos2,n2).data(),rlen).
5803 * If the result of the comparison is nonzero returns it,
5804 * otherwise the shorter one is ordered first.
5805 */
5806 int
5807 compare(size_type __pos1, size_type __n1, const basic_string& __str,
5808 size_type __pos2, size_type __n2 = npos) const;
5809
5810 /**
5811 * @brief Compare to a C string.
5812 * @param __s C string to compare against.
5813 * @return Integer < 0, 0, or > 0.
5814 *
5815 * Returns an integer < 0 if this string is ordered before @a __s, 0 if
5816 * their values are equivalent, or > 0 if this string is ordered after
5817 * @a __s. Determines the effective length rlen of the strings to
5818 * compare as the smallest of size() and the length of a string
5819 * constructed from @a __s. The function then compares the two strings
5820 * by calling traits::compare(data(),s,rlen). If the result of the
5821 * comparison is nonzero returns it, otherwise the shorter one is
5822 * ordered first.
5823 */
5824 int
5825 compare(const _CharT* __s) const _GLIBCXX_NOEXCEPTnoexcept;
5826
5827 // _GLIBCXX_RESOLVE_LIB_DEFECTS
5828 // 5 String::compare specification questionable
5829 /**
5830 * @brief Compare substring to a C string.
5831 * @param __pos Index of first character of substring.
5832 * @param __n1 Number of characters in substring.
5833 * @param __s C string to compare against.
5834 * @return Integer < 0, 0, or > 0.
5835 *
5836 * Form the substring of this string from the @a __n1
5837 * characters starting at @a pos. Returns an integer < 0 if
5838 * the substring is ordered before @a __s, 0 if their values
5839 * are equivalent, or > 0 if the substring is ordered after @a
5840 * __s. Determines the effective length rlen of the strings to
5841 * compare as the smallest of the length of the substring and
5842 * the length of a string constructed from @a __s. The
5843 * function then compares the two string by calling
5844 * traits::compare(substring.data(),__s,rlen). If the result of
5845 * the comparison is nonzero returns it, otherwise the shorter
5846 * one is ordered first.
5847 */
5848 int
5849 compare(size_type __pos, size_type __n1, const _CharT* __s) const;
5850
5851 /**
5852 * @brief Compare substring against a character %array.
5853 * @param __pos Index of first character of substring.
5854 * @param __n1 Number of characters in substring.
5855 * @param __s character %array to compare against.
5856 * @param __n2 Number of characters of s.
5857 * @return Integer < 0, 0, or > 0.
5858 *
5859 * Form the substring of this string from the @a __n1
5860 * characters starting at @a __pos. Form a string from the
5861 * first @a __n2 characters of @a __s. Returns an integer < 0
5862 * if this substring is ordered before the string from @a __s,
5863 * 0 if their values are equivalent, or > 0 if this substring
5864 * is ordered after the string from @a __s. Determines the
5865 * effective length rlen of the strings to compare as the
5866 * smallest of the length of the substring and @a __n2. The
5867 * function then compares the two strings by calling
5868 * traits::compare(substring.data(),s,rlen). If the result of
5869 * the comparison is nonzero returns it, otherwise the shorter
5870 * one is ordered first.
5871 *
5872 * NB: s must have at least n2 characters, &apos;\\0&apos; has
5873 * no special meaning.
5874 */
5875 int
5876 compare(size_type __pos, size_type __n1, const _CharT* __s,
5877 size_type __n2) const;
5878
5879# ifdef _GLIBCXX_TM_TS_INTERNAL
5880 friend void
5881 ::_txnal_cow_string_C1_for_exceptions(void* that, const char* s,
5882 void* exc);
5883 friend const char*
5884 ::_txnal_cow_string_c_str(const void *that);
5885 friend void
5886 ::_txnal_cow_string_D1(void *that);
5887 friend void
5888 ::_txnal_cow_string_D1_commit(void *that);
5889# endif
5890 };
5891#endif // !_GLIBCXX_USE_CXX11_ABI
5892
5893#if __cpp_deduction_guides201703L >= 201606
5894_GLIBCXX_BEGIN_NAMESPACE_CXX11namespace __cxx11 {
5895 template<typename _InputIterator, typename _CharT
5896 = typename iterator_traits<_InputIterator>::value_type,
5897 typename _Allocator = allocator<_CharT>,
5898 typename = _RequireInputIter<_InputIterator>,
5899 typename = _RequireAllocator<_Allocator>>
5900 basic_string(_InputIterator, _InputIterator, _Allocator = _Allocator())
5901 -> basic_string<_CharT, char_traits<_CharT>, _Allocator>;
5902
5903 // _GLIBCXX_RESOLVE_LIB_DEFECTS
5904 // 3075. basic_string needs deduction guides from basic_string_view
5905 template<typename _CharT, typename _Traits,
5906 typename _Allocator = allocator<_CharT>,
5907 typename = _RequireAllocator<_Allocator>>
5908 basic_string(basic_string_view<_CharT, _Traits>, const _Allocator& = _Allocator())
5909 -> basic_string<_CharT, _Traits, _Allocator>;
5910
5911 template<typename _CharT, typename _Traits,
5912 typename _Allocator = allocator<_CharT>,
5913 typename = _RequireAllocator<_Allocator>>
5914 basic_string(basic_string_view<_CharT, _Traits>,
5915 typename basic_string<_CharT, _Traits, _Allocator>::size_type,
5916 typename basic_string<_CharT, _Traits, _Allocator>::size_type,
5917 const _Allocator& = _Allocator())
5918 -> basic_string<_CharT, _Traits, _Allocator>;
5919_GLIBCXX_END_NAMESPACE_CXX11}
5920#endif
5921
5922 // operator+
5923 /**
5924 * @brief Concatenate two strings.
5925 * @param __lhs First string.
5926 * @param __rhs Last string.
5927 * @return New string with value of @a __lhs followed by @a __rhs.
5928 */
5929 template<typename _CharT, typename _Traits, typename _Alloc>
5930 basic_string<_CharT, _Traits, _Alloc>
5931 operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
5932 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
5933 {
5934 basic_string<_CharT, _Traits, _Alloc> __str(__lhs);
5935 __str.append(__rhs);
5936 return __str;
5937 }
5938
5939 /**
5940 * @brief Concatenate C string and string.
5941 * @param __lhs First string.
5942 * @param __rhs Last string.
5943 * @return New string with value of @a __lhs followed by @a __rhs.
5944 */
5945 template<typename _CharT, typename _Traits, typename _Alloc>
5946 basic_string<_CharT,_Traits,_Alloc>
5947 operator+(const _CharT* __lhs,
5948 const basic_string<_CharT,_Traits,_Alloc>& __rhs);
5949
5950 /**
5951 * @brief Concatenate character and string.
5952 * @param __lhs First string.
5953 * @param __rhs Last string.
5954 * @return New string with @a __lhs followed by @a __rhs.
5955 */
5956 template<typename _CharT, typename _Traits, typename _Alloc>
5957 basic_string<_CharT,_Traits,_Alloc>
5958 operator+(_CharT __lhs, const basic_string<_CharT,_Traits,_Alloc>& __rhs);
5959
5960 /**
5961 * @brief Concatenate string and C string.
5962 * @param __lhs First string.
5963 * @param __rhs Last string.
5964 * @return New string with @a __lhs followed by @a __rhs.
5965 */
5966 template<typename _CharT, typename _Traits, typename _Alloc>
5967 inline basic_string<_CharT, _Traits, _Alloc>
5968 operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
5969 const _CharT* __rhs)
5970 {
5971 basic_string<_CharT, _Traits, _Alloc> __str(__lhs);
5972 __str.append(__rhs);
5973 return __str;
5974 }
5975
5976 /**
5977 * @brief Concatenate string and character.
5978 * @param __lhs First string.
5979 * @param __rhs Last string.
5980 * @return New string with @a __lhs followed by @a __rhs.
5981 */
5982 template<typename _CharT, typename _Traits, typename _Alloc>
5983 inline basic_string<_CharT, _Traits, _Alloc>
5984 operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs, _CharT __rhs)
5985 {
5986 typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
5987 typedef typename __string_type::size_type __size_type;
5988 __string_type __str(__lhs);
5989 __str.append(__size_type(1), __rhs);
5990 return __str;
5991 }
5992
5993#if __cplusplus201703L >= 201103L
5994 template<typename _CharT, typename _Traits, typename _Alloc>
5995 inline basic_string<_CharT, _Traits, _Alloc>
5996 operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs,
5997 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
5998 { return std::move(__lhs.append(__rhs)); }
5999
6000 template<typename _CharT, typename _Traits, typename _Alloc>
6001 inline basic_string<_CharT, _Traits, _Alloc>
6002 operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
6003 basic_string<_CharT, _Traits, _Alloc>&& __rhs)
6004 { return std::move(__rhs.insert(0, __lhs)); }
6005
6006 template<typename _CharT, typename _Traits, typename _Alloc>
6007 inline basic_string<_CharT, _Traits, _Alloc>
6008 operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs,
6009 basic_string<_CharT, _Traits, _Alloc>&& __rhs)
6010 {
6011 const auto __size = __lhs.size() + __rhs.size();
6012 const bool __cond = (__size > __lhs.capacity()
6013 && __size <= __rhs.capacity());
6014 return __cond ? std::move(__rhs.insert(0, __lhs))
6015 : std::move(__lhs.append(__rhs));
6016 }
6017
6018 template<typename _CharT, typename _Traits, typename _Alloc>
6019 inline basic_string<_CharT, _Traits, _Alloc>
6020 operator+(const _CharT* __lhs,
6021 basic_string<_CharT, _Traits, _Alloc>&& __rhs)
6022 { return std::move(__rhs.insert(0, __lhs)); }
6023
6024 template<typename _CharT, typename _Traits, typename _Alloc>
6025 inline basic_string<_CharT, _Traits, _Alloc>
6026 operator+(_CharT __lhs,
6027 basic_string<_CharT, _Traits, _Alloc>&& __rhs)
6028 { return std::move(__rhs.insert(0, 1, __lhs)); }
6029
6030 template<typename _CharT, typename _Traits, typename _Alloc>
6031 inline basic_string<_CharT, _Traits, _Alloc>
6032 operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs,
6033 const _CharT* __rhs)
6034 { return std::move(__lhs.append(__rhs)); }
6035
6036 template<typename _CharT, typename _Traits, typename _Alloc>
6037 inline basic_string<_CharT, _Traits, _Alloc>
6038 operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs,
6039 _CharT __rhs)
6040 { return std::move(__lhs.append(1, __rhs)); }
6041#endif
6042
6043 // operator ==
6044 /**
6045 * @brief Test equivalence of two strings.
6046 * @param __lhs First string.
6047 * @param __rhs Second string.
6048 * @return True if @a __lhs.compare(@a __rhs) == 0. False otherwise.
6049 */
6050 template<typename _CharT, typename _Traits, typename _Alloc>
6051 inline bool
6052 operator==(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
6053 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
6054 _GLIBCXX_NOEXCEPTnoexcept
6055 { return __lhs.compare(__rhs) == 0; }
6056
6057 template<typename _CharT>
6058 inline
6059 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value, bool>::__type
6060 operator==(const basic_string<_CharT>& __lhs,
6061 const basic_string<_CharT>& __rhs) _GLIBCXX_NOEXCEPTnoexcept
6062 { return (__lhs.size() == __rhs.size()
6063 && !std::char_traits<_CharT>::compare(__lhs.data(), __rhs.data(),
6064 __lhs.size())); }
6065
6066 /**
6067 * @brief Test equivalence of C string and string.
6068 * @param __lhs C string.
6069 * @param __rhs String.
6070 * @return True if @a __rhs.compare(@a __lhs) == 0. False otherwise.
6071 */
6072 template<typename _CharT, typename _Traits, typename _Alloc>
6073 inline bool
6074 operator==(const _CharT* __lhs,
6075 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
6076 { return __rhs.compare(__lhs) == 0; }
6077
6078 /**
6079 * @brief Test equivalence of string and C string.
6080 * @param __lhs String.
6081 * @param __rhs C string.
6082 * @return True if @a __lhs.compare(@a __rhs) == 0. False otherwise.
6083 */
6084 template<typename _CharT, typename _Traits, typename _Alloc>
6085 inline bool
6086 operator==(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
6087 const _CharT* __rhs)
6088 { return __lhs.compare(__rhs) == 0; }
6089
6090 // operator !=
6091 /**
6092 * @brief Test difference of two strings.
6093 * @param __lhs First string.
6094 * @param __rhs Second string.
6095 * @return True if @a __lhs.compare(@a __rhs) != 0. False otherwise.
6096 */
6097 template<typename _CharT, typename _Traits, typename _Alloc>
6098 inline bool
6099 operator!=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
6100 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
6101 _GLIBCXX_NOEXCEPTnoexcept
6102 { return !(__lhs == __rhs); }
6103
6104 /**
6105 * @brief Test difference of C string and string.
6106 * @param __lhs C string.
6107 * @param __rhs String.
6108 * @return True if @a __rhs.compare(@a __lhs) != 0. False otherwise.
6109 */
6110 template<typename _CharT, typename _Traits, typename _Alloc>
6111 inline bool
6112 operator!=(const _CharT* __lhs,
6113 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
6114 { return !(__lhs == __rhs); }
6115
6116 /**
6117 * @brief Test difference of string and C string.
6118 * @param __lhs String.
6119 * @param __rhs C string.
6120 * @return True if @a __lhs.compare(@a __rhs) != 0. False otherwise.
6121 */
6122 template<typename _CharT, typename _Traits, typename _Alloc>
6123 inline bool
6124 operator!=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
6125 const _CharT* __rhs)
6126 { return !(__lhs == __rhs); }
6127
6128 // operator <
6129 /**
6130 * @brief Test if string precedes string.
6131 * @param __lhs First string.
6132 * @param __rhs Second string.
6133 * @return True if @a __lhs precedes @a __rhs. False otherwise.
6134 */
6135 template<typename _CharT, typename _Traits, typename _Alloc>
6136 inline bool
6137 operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
6138 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
6139 _GLIBCXX_NOEXCEPTnoexcept
6140 { return __lhs.compare(__rhs) < 0; }
6141
6142 /**
6143 * @brief Test if string precedes C string.
6144 * @param __lhs String.
6145 * @param __rhs C string.
6146 * @return True if @a __lhs precedes @a __rhs. False otherwise.
6147 */
6148 template<typename _CharT, typename _Traits, typename _Alloc>
6149 inline bool
6150 operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
6151 const _CharT* __rhs)
6152 { return __lhs.compare(__rhs) < 0; }
6153
6154 /**
6155 * @brief Test if C string precedes string.
6156 * @param __lhs C string.
6157 * @param __rhs String.
6158 * @return True if @a __lhs precedes @a __rhs. False otherwise.
6159 */
6160 template<typename _CharT, typename _Traits, typename _Alloc>
6161 inline bool
6162 operator<(const _CharT* __lhs,
6163 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
6164 { return __rhs.compare(__lhs) > 0; }
6165
6166 // operator >
6167 /**
6168 * @brief Test if string follows string.
6169 * @param __lhs First string.
6170 * @param __rhs Second string.
6171 * @return True if @a __lhs follows @a __rhs. False otherwise.
6172 */
6173 template<typename _CharT, typename _Traits, typename _Alloc>
6174 inline bool
6175 operator>(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
6176 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
6177 _GLIBCXX_NOEXCEPTnoexcept
6178 { return __lhs.compare(__rhs) > 0; }
6179
6180 /**
6181 * @brief Test if string follows C string.
6182 * @param __lhs String.
6183 * @param __rhs C string.
6184 * @return True if @a __lhs follows @a __rhs. False otherwise.
6185 */
6186 template<typename _CharT, typename _Traits, typename _Alloc>
6187 inline bool
6188 operator>(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
6189 const _CharT* __rhs)
6190 { return __lhs.compare(__rhs) > 0; }
6191
6192 /**
6193 * @brief Test if C string follows string.
6194 * @param __lhs C string.
6195 * @param __rhs String.
6196 * @return True if @a __lhs follows @a __rhs. False otherwise.
6197 */
6198 template<typename _CharT, typename _Traits, typename _Alloc>
6199 inline bool
6200 operator>(const _CharT* __lhs,
6201 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
6202 { return __rhs.compare(__lhs) < 0; }
6203
6204 // operator <=
6205 /**
6206 * @brief Test if string doesn't follow string.
6207 * @param __lhs First string.
6208 * @param __rhs Second string.
6209 * @return True if @a __lhs doesn't follow @a __rhs. False otherwise.
6210 */
6211 template<typename _CharT, typename _Traits, typename _Alloc>
6212 inline bool
6213 operator<=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
6214 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
6215 _GLIBCXX_NOEXCEPTnoexcept
6216 { return __lhs.compare(__rhs) <= 0; }
6217
6218 /**
6219 * @brief Test if string doesn't follow C string.
6220 * @param __lhs String.
6221 * @param __rhs C string.
6222 * @return True if @a __lhs doesn't follow @a __rhs. False otherwise.
6223 */
6224 template<typename _CharT, typename _Traits, typename _Alloc>
6225 inline bool
6226 operator<=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
6227 const _CharT* __rhs)
6228 { return __lhs.compare(__rhs) <= 0; }
6229
6230 /**
6231 * @brief Test if C string doesn't follow string.
6232 * @param __lhs C string.
6233 * @param __rhs String.
6234 * @return True if @a __lhs doesn't follow @a __rhs. False otherwise.
6235 */
6236 template<typename _CharT, typename _Traits, typename _Alloc>
6237 inline bool
6238 operator<=(const _CharT* __lhs,
6239 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
6240 { return __rhs.compare(__lhs) >= 0; }
6241
6242 // operator >=
6243 /**
6244 * @brief Test if string doesn't precede string.
6245 * @param __lhs First string.
6246 * @param __rhs Second string.
6247 * @return True if @a __lhs doesn't precede @a __rhs. False otherwise.
6248 */
6249 template<typename _CharT, typename _Traits, typename _Alloc>
6250 inline bool
6251 operator>=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
6252 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
6253 _GLIBCXX_NOEXCEPTnoexcept
6254 { return __lhs.compare(__rhs) >= 0; }
6255
6256 /**
6257 * @brief Test if string doesn't precede C string.
6258 * @param __lhs String.
6259 * @param __rhs C string.
6260 * @return True if @a __lhs doesn't precede @a __rhs. False otherwise.
6261 */
6262 template<typename _CharT, typename _Traits, typename _Alloc>
6263 inline bool
6264 operator>=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
6265 const _CharT* __rhs)
6266 { return __lhs.compare(__rhs) >= 0; }
6267
6268 /**
6269 * @brief Test if C string doesn't precede string.
6270 * @param __lhs C string.
6271 * @param __rhs String.
6272 * @return True if @a __lhs doesn't precede @a __rhs. False otherwise.
6273 */
6274 template<typename _CharT, typename _Traits, typename _Alloc>
6275 inline bool
6276 operator>=(const _CharT* __lhs,
6277 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
6278 { return __rhs.compare(__lhs) <= 0; }
6279
6280 /**
6281 * @brief Swap contents of two strings.
6282 * @param __lhs First string.
6283 * @param __rhs Second string.
6284 *
6285 * Exchanges the contents of @a __lhs and @a __rhs in constant time.
6286 */
6287 template<typename _CharT, typename _Traits, typename _Alloc>
6288 inline void
6289 swap(basic_string<_CharT, _Traits, _Alloc>& __lhs,
6290 basic_string<_CharT, _Traits, _Alloc>& __rhs)
6291 _GLIBCXX_NOEXCEPT_IF(noexcept(__lhs.swap(__rhs)))noexcept(noexcept(__lhs.swap(__rhs)))
6292 { __lhs.swap(__rhs); }
6293
6294
6295 /**
6296 * @brief Read stream into a string.
6297 * @param __is Input stream.
6298 * @param __str Buffer to store into.
6299 * @return Reference to the input stream.
6300 *
6301 * Stores characters from @a __is into @a __str until whitespace is
6302 * found, the end of the stream is encountered, or str.max_size()
6303 * is reached. If is.width() is non-zero, that is the limit on the
6304 * number of characters stored into @a __str. Any previous
6305 * contents of @a __str are erased.
6306 */
6307 template<typename _CharT, typename _Traits, typename _Alloc>
6308 basic_istream<_CharT, _Traits>&
6309 operator>>(basic_istream<_CharT, _Traits>& __is,
6310 basic_string<_CharT, _Traits, _Alloc>& __str);
6311
6312 template<>
6313 basic_istream<char>&
6314 operator>>(basic_istream<char>& __is, basic_string<char>& __str);
6315
6316 /**
6317 * @brief Write string to a stream.
6318 * @param __os Output stream.
6319 * @param __str String to write out.
6320 * @return Reference to the output stream.
6321 *
6322 * Output characters of @a __str into os following the same rules as for
6323 * writing a C string.
6324 */
6325 template<typename _CharT, typename _Traits, typename _Alloc>
6326 inline basic_ostream<_CharT, _Traits>&
6327 operator<<(basic_ostream<_CharT, _Traits>& __os,
6328 const basic_string<_CharT, _Traits, _Alloc>& __str)
6329 {
6330 // _GLIBCXX_RESOLVE_LIB_DEFECTS
6331 // 586. string inserter not a formatted function
6332 return __ostream_insert(__os, __str.data(), __str.size());
6333 }
6334
6335 /**
6336 * @brief Read a line from stream into a string.
6337 * @param __is Input stream.
6338 * @param __str Buffer to store into.
6339 * @param __delim Character marking end of line.
6340 * @return Reference to the input stream.
6341 *
6342 * Stores characters from @a __is into @a __str until @a __delim is
6343 * found, the end of the stream is encountered, or str.max_size()
6344 * is reached. Any previous contents of @a __str are erased. If
6345 * @a __delim is encountered, it is extracted but not stored into
6346 * @a __str.
6347 */
6348 template<typename _CharT, typename _Traits, typename _Alloc>
6349 basic_istream<_CharT, _Traits>&
6350 getline(basic_istream<_CharT, _Traits>& __is,
6351 basic_string<_CharT, _Traits, _Alloc>& __str, _CharT __delim);
6352
6353 /**
6354 * @brief Read a line from stream into a string.
6355 * @param __is Input stream.
6356 * @param __str Buffer to store into.
6357 * @return Reference to the input stream.
6358 *
6359 * Stores characters from is into @a __str until &apos;\n&apos; is
6360 * found, the end of the stream is encountered, or str.max_size()
6361 * is reached. Any previous contents of @a __str are erased. If
6362 * end of line is encountered, it is extracted but not stored into
6363 * @a __str.
6364 */
6365 template<typename _CharT, typename _Traits, typename _Alloc>
6366 inline basic_istream<_CharT, _Traits>&
6367 getline(basic_istream<_CharT, _Traits>& __is,
6368 basic_string<_CharT, _Traits, _Alloc>& __str)
6369 { return std::getline(__is, __str, __is.widen('\n')); }
6370
6371#if __cplusplus201703L >= 201103L
6372 /// Read a line from an rvalue stream into a string.
6373 template<typename _CharT, typename _Traits, typename _Alloc>
6374 inline basic_istream<_CharT, _Traits>&
6375 getline(basic_istream<_CharT, _Traits>&& __is,
6376 basic_string<_CharT, _Traits, _Alloc>& __str, _CharT __delim)
6377 { return std::getline(__is, __str, __delim); }
6378
6379 /// Read a line from an rvalue stream into a string.
6380 template<typename _CharT, typename _Traits, typename _Alloc>
6381 inline basic_istream<_CharT, _Traits>&
6382 getline(basic_istream<_CharT, _Traits>&& __is,
6383 basic_string<_CharT, _Traits, _Alloc>& __str)
6384 { return std::getline(__is, __str); }
6385#endif
6386
6387 template<>
6388 basic_istream<char>&
6389 getline(basic_istream<char>& __in, basic_string<char>& __str,
6390 char __delim);
6391
6392#ifdef _GLIBCXX_USE_WCHAR_T1
6393 template<>
6394 basic_istream<wchar_t>&
6395 getline(basic_istream<wchar_t>& __in, basic_string<wchar_t>& __str,
6396 wchar_t __delim);
6397#endif
6398
6399_GLIBCXX_END_NAMESPACE_VERSION
6400} // namespace
6401
6402#if __cplusplus201703L >= 201103L
6403
6404#include <ext/string_conversions.h>
6405
6406namespace std _GLIBCXX_VISIBILITY(default)__attribute__ ((__visibility__ ("default")))
6407{
6408_GLIBCXX_BEGIN_NAMESPACE_VERSION
6409_GLIBCXX_BEGIN_NAMESPACE_CXX11namespace __cxx11 {
6410
6411#if _GLIBCXX_USE_C99_STDLIB1
6412 // 21.4 Numeric Conversions [string.conversions].
6413 inline int
6414 stoi(const string& __str, size_t* __idx = 0, int __base = 10)
6415 { return __gnu_cxx::__stoa<long, int>(&std::strtol, "stoi", __str.c_str(),
6416 __idx, __base); }
6417
6418 inline long
6419 stol(const string& __str, size_t* __idx = 0, int __base = 10)
6420 { return __gnu_cxx::__stoa(&std::strtol, "stol", __str.c_str(),
6421 __idx, __base); }
6422
6423 inline unsigned long
6424 stoul(const string& __str, size_t* __idx = 0, int __base = 10)
6425 { return __gnu_cxx::__stoa(&std::strtoul, "stoul", __str.c_str(),
6426 __idx, __base); }
6427
6428 inline long long
6429 stoll(const string& __str, size_t* __idx = 0, int __base = 10)
6430 { return __gnu_cxx::__stoa(&std::strtoll, "stoll", __str.c_str(),
6431 __idx, __base); }
6432
6433 inline unsigned long long
6434 stoull(const string& __str, size_t* __idx = 0, int __base = 10)
6435 { return __gnu_cxx::__stoa(&std::strtoull, "stoull", __str.c_str(),
6436 __idx, __base); }
6437
6438 // NB: strtof vs strtod.
6439 inline float
6440 stof(const string& __str, size_t* __idx = 0)
6441 { return __gnu_cxx::__stoa(&std::strtof, "stof", __str.c_str(), __idx); }
6442
6443 inline double
6444 stod(const string& __str, size_t* __idx = 0)
6445 { return __gnu_cxx::__stoa(&std::strtod, "stod", __str.c_str(), __idx); }
6446
6447 inline long double
6448 stold(const string& __str, size_t* __idx = 0)
6449 { return __gnu_cxx::__stoa(&std::strtold, "stold", __str.c_str(), __idx); }
6450#endif // _GLIBCXX_USE_C99_STDLIB
6451
6452#if _GLIBCXX_USE_C99_STDIO1
6453 // NB: (v)snprintf vs sprintf.
6454
6455 // DR 1261.
6456 inline string
6457 to_string(int __val)
6458 { return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, 4 * sizeof(int),
6459 "%d", __val); }
6460
6461 inline string
6462 to_string(unsigned __val)
6463 { return __gnu_cxx::__to_xstring<string>(&std::vsnprintf,
6464 4 * sizeof(unsigned),
6465 "%u", __val); }
6466
6467 inline string
6468 to_string(long __val)
6469 { return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, 4 * sizeof(long),
6470 "%ld", __val); }
6471
6472 inline string
6473 to_string(unsigned long __val)
6474 { return __gnu_cxx::__to_xstring<string>(&std::vsnprintf,
6475 4 * sizeof(unsigned long),
6476 "%lu", __val); }
6477
6478 inline string
6479 to_string(long long __val)
6480 { return __gnu_cxx::__to_xstring<string>(&std::vsnprintf,
6481 4 * sizeof(long long),
6482 "%lld", __val); }
6483
6484 inline string
6485 to_string(unsigned long long __val)
6486 { return __gnu_cxx::__to_xstring<string>(&std::vsnprintf,
6487 4 * sizeof(unsigned long long),
6488 "%llu", __val); }
6489
6490 inline string
6491 to_string(float __val)
6492 {
6493 const int __n =
6494 __gnu_cxx::__numeric_traits<float>::__max_exponent10 + 20;
6495 return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, __n,
6496 "%f", __val);
6497 }
6498
6499 inline string
6500 to_string(double __val)
6501 {
6502 const int __n =
6503 __gnu_cxx::__numeric_traits<double>::__max_exponent10 + 20;
6504 return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, __n,
6505 "%f", __val);
6506 }
6507
6508 inline string
6509 to_string(long double __val)
6510 {
6511 const int __n =
6512 __gnu_cxx::__numeric_traits<long double>::__max_exponent10 + 20;
6513 return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, __n,
6514 "%Lf", __val);
6515 }
6516#endif // _GLIBCXX_USE_C99_STDIO
6517
6518#if defined(_GLIBCXX_USE_WCHAR_T1) && _GLIBCXX_USE_C99_WCHAR1
6519 inline int
6520 stoi(const wstring& __str, size_t* __idx = 0, int __base = 10)
6521 { return __gnu_cxx::__stoa<long, int>(&std::wcstol, "stoi", __str.c_str(),
6522 __idx, __base); }
6523
6524 inline long
6525 stol(const wstring& __str, size_t* __idx = 0, int __base = 10)
6526 { return __gnu_cxx::__stoa(&std::wcstol, "stol", __str.c_str(),
6527 __idx, __base); }
6528
6529 inline unsigned long
6530 stoul(const wstring& __str, size_t* __idx = 0, int __base = 10)
6531 { return __gnu_cxx::__stoa(&std::wcstoul, "stoul", __str.c_str(),
6532 __idx, __base); }
6533
6534 inline long long
6535 stoll(const wstring& __str, size_t* __idx = 0, int __base = 10)
6536 { return __gnu_cxx::__stoa(&std::wcstoll, "stoll", __str.c_str(),
6537 __idx, __base); }
6538
6539 inline unsigned long long
6540 stoull(const wstring& __str, size_t* __idx = 0, int __base = 10)
6541 { return __gnu_cxx::__stoa(&std::wcstoull, "stoull", __str.c_str(),
6542 __idx, __base); }
6543
6544 // NB: wcstof vs wcstod.
6545 inline float
6546 stof(const wstring& __str, size_t* __idx = 0)
6547 { return __gnu_cxx::__stoa(&std::wcstof, "stof", __str.c_str(), __idx); }
6548
6549 inline double
6550 stod(const wstring& __str, size_t* __idx = 0)
6551 { return __gnu_cxx::__stoa(&std::wcstod, "stod", __str.c_str(), __idx); }
6552
6553 inline long double
6554 stold(const wstring& __str, size_t* __idx = 0)
6555 { return __gnu_cxx::__stoa(&std::wcstold, "stold", __str.c_str(), __idx); }
6556
6557#ifndef _GLIBCXX_HAVE_BROKEN_VSWPRINTF
6558 // DR 1261.
6559 inline wstring
6560 to_wstring(int __val)
6561 { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, 4 * sizeof(int),
6562 L"%d", __val); }
6563
6564 inline wstring
6565 to_wstring(unsigned __val)
6566 { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf,
6567 4 * sizeof(unsigned),
6568 L"%u", __val); }
6569
6570 inline wstring
6571 to_wstring(long __val)
6572 { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, 4 * sizeof(long),
6573 L"%ld", __val); }
6574
6575 inline wstring
6576 to_wstring(unsigned long __val)
6577 { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf,
6578 4 * sizeof(unsigned long),
6579 L"%lu", __val); }
6580
6581 inline wstring
6582 to_wstring(long long __val)
6583 { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf,
6584 4 * sizeof(long long),
6585 L"%lld", __val); }
6586
6587 inline wstring
6588 to_wstring(unsigned long long __val)
6589 { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf,
6590 4 * sizeof(unsigned long long),
6591 L"%llu", __val); }
6592
6593 inline wstring
6594 to_wstring(float __val)
6595 {
6596 const int __n =
6597 __gnu_cxx::__numeric_traits<float>::__max_exponent10 + 20;
6598 return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, __n,
6599 L"%f", __val);
6600 }
6601
6602 inline wstring
6603 to_wstring(double __val)
6604 {
6605 const int __n =
6606 __gnu_cxx::__numeric_traits<double>::__max_exponent10 + 20;
6607 return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, __n,
6608 L"%f", __val);
6609 }
6610
6611 inline wstring
6612 to_wstring(long double __val)
6613 {
6614 const int __n =
6615 __gnu_cxx::__numeric_traits<long double>::__max_exponent10 + 20;
6616 return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, __n,
6617 L"%Lf", __val);
6618 }
6619#endif // _GLIBCXX_HAVE_BROKEN_VSWPRINTF
6620#endif // _GLIBCXX_USE_WCHAR_T && _GLIBCXX_USE_C99_WCHAR
6621
6622_GLIBCXX_END_NAMESPACE_CXX11}
6623_GLIBCXX_END_NAMESPACE_VERSION
6624} // namespace
6625
6626#endif /* C++11 */
6627
6628#if __cplusplus201703L >= 201103L
6629
6630#include <bits/functional_hash.h>
6631
6632namespace std _GLIBCXX_VISIBILITY(default)__attribute__ ((__visibility__ ("default")))
6633{
6634_GLIBCXX_BEGIN_NAMESPACE_VERSION
6635
6636 // DR 1182.
6637
6638#ifndef _GLIBCXX_COMPATIBILITY_CXX0X
6639 /// std::hash specialization for string.
6640 template<>
6641 struct hash<string>
6642 : public __hash_base<size_t, string>
6643 {
6644 size_t
6645 operator()(const string& __s) const noexcept
6646 { return std::_Hash_impl::hash(__s.data(), __s.length()); }
6647 };
6648
6649 template<>
6650 struct __is_fast_hash<hash<string>> : std::false_type
6651 { };
6652
6653#ifdef _GLIBCXX_USE_WCHAR_T1
6654 /// std::hash specialization for wstring.
6655 template<>
6656 struct hash<wstring>
6657 : public __hash_base<size_t, wstring>
6658 {
6659 size_t
6660 operator()(const wstring& __s) const noexcept
6661 { return std::_Hash_impl::hash(__s.data(),
6662 __s.length() * sizeof(wchar_t)); }
6663 };
6664
6665 template<>
6666 struct __is_fast_hash<hash<wstring>> : std::false_type
6667 { };
6668#endif
6669#endif /* _GLIBCXX_COMPATIBILITY_CXX0X */
6670
6671#ifdef _GLIBCXX_USE_C99_STDINT_TR11
6672 /// std::hash specialization for u16string.
6673 template<>
6674 struct hash<u16string>
6675 : public __hash_base<size_t, u16string>
6676 {
6677 size_t
6678 operator()(const u16string& __s) const noexcept
6679 { return std::_Hash_impl::hash(__s.data(),
6680 __s.length() * sizeof(char16_t)); }
6681 };
6682
6683 template<>
6684 struct __is_fast_hash<hash<u16string>> : std::false_type
6685 { };
6686
6687 /// std::hash specialization for u32string.
6688 template<>
6689 struct hash<u32string>
6690 : public __hash_base<size_t, u32string>
6691 {
6692 size_t
6693 operator()(const u32string& __s) const noexcept
6694 { return std::_Hash_impl::hash(__s.data(),
6695 __s.length() * sizeof(char32_t)); }
6696 };
6697
6698 template<>
6699 struct __is_fast_hash<hash<u32string>> : std::false_type
6700 { };
6701#endif
6702
6703#if __cplusplus201703L > 201103L
6704
6705#define __cpp_lib_string_udls201304 201304
6706
6707 inline namespace literals
6708 {
6709 inline namespace string_literals
6710 {
6711#pragma GCC diagnostic push
6712#pragma GCC diagnostic ignored "-Wliteral-suffix"
6713 _GLIBCXX_DEFAULT_ABI_TAG__attribute ((__abi_tag__ ("cxx11")))
6714 inline basic_string<char>
6715 operator""s(const char* __str, size_t __len)
6716 { return basic_string<char>{__str, __len}; }
6717
6718#ifdef _GLIBCXX_USE_WCHAR_T1
6719 _GLIBCXX_DEFAULT_ABI_TAG__attribute ((__abi_tag__ ("cxx11")))
6720 inline basic_string<wchar_t>
6721 operator""s(const wchar_t* __str, size_t __len)
6722 { return basic_string<wchar_t>{__str, __len}; }
6723#endif
6724
6725#ifdef _GLIBCXX_USE_C99_STDINT_TR11
6726 _GLIBCXX_DEFAULT_ABI_TAG__attribute ((__abi_tag__ ("cxx11")))
6727 inline basic_string<char16_t>
6728 operator""s(const char16_t* __str, size_t __len)
6729 { return basic_string<char16_t>{__str, __len}; }
6730
6731 _GLIBCXX_DEFAULT_ABI_TAG__attribute ((__abi_tag__ ("cxx11")))
6732 inline basic_string<char32_t>
6733 operator""s(const char32_t* __str, size_t __len)
6734 { return basic_string<char32_t>{__str, __len}; }
6735#endif
6736
6737#pragma GCC diagnostic pop
6738 } // inline namespace string_literals
6739 } // inline namespace literals
6740
6741#endif // __cplusplus > 201103L
6742
6743_GLIBCXX_END_NAMESPACE_VERSION
6744} // namespace std
6745
6746#endif // C++11
6747
6748#endif /* _BASIC_STRING_H */