Bug Summary

File:root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp
Warning:line 67, column 32
Method called on moved-from object 'simple_devs' of type 'std::vector'

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-pc-linux-gnu -O2 -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name ggml-backend-meta.cpp -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 -analyzer-config-compatibility-mode=true -mrelocation-model pic -pic-level 2 -fhalf-no-semantic-interposition -mframe-pointer=all -relaxed-aliasing -ffp-contract=off -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -target-feature +avx -target-feature +avx2 -target-feature +bmi2 -target-feature +f16c -target-feature +fma -target-feature +sse4.2 -tune-cpu generic -debugger-tuning=gdb -fdebug-compilation-dir=/root/firefox-clang/obj-x86_64-pc-linux-gnu/third_party/llama.cpp -fcoverage-compilation-dir=/root/firefox-clang/obj-x86_64-pc-linux-gnu/third_party/llama.cpp -resource-dir /usr/lib/llvm-23/lib/clang/23 -include /root/firefox-clang/config/gcc_hidden.h -include /root/firefox-clang/obj-x86_64-pc-linux-gnu/mozilla-config.h -I /root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/stl_wrappers -D _GLIBCXX_ASSERTIONS=1 -I /root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/system_wrappers -U _FORTIFY_SOURCE -D _FORTIFY_SOURCE=2 -D DEBUG=1 -D _GNU_SOURCE=1 -D GGML_USE_CPU=1 -D GGML_VERSION="GGML_VERSION" -D GGML_COMMIT="GGML_COMMIT" -D GGML_SHARED=1 -D LLAMA_SHARED=1 -D GGML_BUILD=1 -D LLAMA_BUILD=1 -D GGML_BACKEND_SHARED=1 -D GGML_BACKEND_BUILD=1 -D MOZ_HAS_MOZGLUE -I /root/firefox-clang/third_party/llama.cpp -I /root/firefox-clang/obj-x86_64-pc-linux-gnu/third_party/llama.cpp -I /root/firefox-clang/third_party/llama.cpp/ggml -I /root/firefox-clang/third_party/llama.cpp/ggml/include -I /root/firefox-clang/third_party/llama.cpp/ggml/src -I /root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-cpu -I /root/firefox-clang/third_party/llama.cpp/include -I /root/firefox-clang/third_party/llama.cpp/src -I /root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include -I /root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/nspr -I /root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/nss -D MOZILLA_CLIENT -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/16/../../../../include/c++/16 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/16/../../../../include/x86_64-linux-gnu/c++/16 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/16/../../../../include/c++/16/backward -internal-isystem /usr/lib/llvm-23/lib/clang/23/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/16/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -Wno-error=pessimizing-move -Wno-error=large-by-value-copy=128 -Wno-error=implicit-int-float-conversion -Wno-error=thread-safety-analysis -Wno-error=tautological-type-limit-compare -Wno-invalid-offsetof -Wno-range-loop-analysis -Wno-deprecated-anon-enum-enum-conversion -Wno-deprecated-enum-enum-conversion -Wno-inline-new-delete -Wno-error=deprecated-declarations -Wno-error=array-bounds -Wno-error=free-nonheap-object -Wno-error=atomic-alignment -Wno-error=deprecated-builtins -Wno-psabi -Wno-error=builtin-macro-redefined -Wno-vla-cxx-extension -Wno-unknown-warning-option -Wno-character-conversion -Wno-sign-compare -Wno-unused-function -Wno-tautological-unsigned-enum-zero-compare -Wno-implicit-fallthrough -Wno-unreachable-code -std=gnu++20 -fdeprecated-macro -ferror-limit 19 -fstrict-flex-arrays=1 -stack-protector 2 -fstack-clash-protection -ftrivial-auto-var-init=pattern -fno-rtti -fgnuc-version=4.2.1 -fno-implicit-modules -fskip-odr-check-in-gmf -fno-sized-deallocation -fno-aligned-allocation -fdiagnostics-absolute-paths -vectorize-loops -vectorize-slp -analyzer-checker optin.performance.Padding -analyzer-output=html -analyzer-config stable-report-filename=true -mllvm -dwarf-linkage-names=Abstract -faddrsig -fdwarf2-cfi-asm -o /tmp/scan-build-2026-09-01-224014-2642839-1 -x c++ /root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp

/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp

1#include "ggml.h"
2#include "ggml-impl.h"
3#include "ggml-backend.h"
4#include "ggml-backend-impl.h"
5#include "ggml-alloc.h"
6#include "ggml-cpp.h"
7
8#include <algorithm>
9#include <cassert>
10#include <cmath>
11#include <cstddef>
12#include <cstdint>
13#include <cstring>
14#include <map>
15#include <memory>
16#include <set>
17#include <string>
18#include <tuple>
19#include <utility>
20#include <vector>
21
22struct ggml_backend_meta_device;
23struct ggml_backend_meta_buffer_type;
24struct ggml_backend_meta_buffer;
25struct ggml_backend_meta;
26
27const char * ggml_backend_meta_split_axis_name(enum ggml_backend_meta_split_axis split_axis) {
28 switch (split_axis) {
29 case GGML_BACKEND_SPLIT_AXIS_0:
30 return "0";
31 case GGML_BACKEND_SPLIT_AXIS_1:
32 return "1";
33 case GGML_BACKEND_SPLIT_AXIS_2:
34 return "2";
35 case GGML_BACKEND_SPLIT_AXIS_3:
36 return "3";
37 case GGML_BACKEND_SPLIT_AXIS_MIRRORED:
38 return "MIRRORED";
39 case GGML_BACKEND_SPLIT_AXIS_PARTIAL:
40 return "PARTIAL";
41 case GGML_BACKEND_SPLIT_AXIS_NONE:
42 return "NONE";
43 case GGML_BACKEND_SPLIT_AXIS_UNKNOWN:
44 return "UNKNOWN";
45 default:
46 GGML_ABORT("fatal error")ggml_abort("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 46, "fatal error")
;
47 }
48}
49
50//
51// meta backend device
52//
53
54struct ggml_backend_meta_device_context {
55 std::vector<ggml_backend_dev_t> simple_devs;
56 ggml_backend_meta_get_split_state_t get_split_state;
57 void * get_split_state_ud;
58
59 std::string name;
60 std::string description;
61
62 ggml_backend_meta_device_context(
63 std::vector<ggml_backend_dev_t> simple_devs, ggml_backend_meta_get_split_state_t get_split_state, void * get_split_state_ud) :
64 simple_devs(std::move(simple_devs)), get_split_state(get_split_state), get_split_state_ud(get_split_state_ud) {
6
Object 'simple_devs' of type 'std::vector' is left in a valid but unspecified state after move
65 name = std::string("Meta(");
66 description = std::string("Meta(");
67 for (size_t i = 0; i < simple_devs.size(); i++) {
7
Method called on moved-from object 'simple_devs' of type 'std::vector'
68 if (i > 0) {
69 name += ",";
70 description += ",";
71 }
72 name += ggml_backend_dev_name (simple_devs[i]);
73 description += ggml_backend_dev_description(simple_devs[i]);
74 }
75 name += ")";
76 description += ")";
77 }
78
79 bool operator<(const ggml_backend_meta_device_context & other) const {
80 return std::tie(simple_devs, get_split_state, get_split_state_ud)
81 < std::tie(other.simple_devs, other.get_split_state, other.get_split_state_ud);
82 }
83};
84
85static bool ggml_backend_dev_is_meta(ggml_backend_dev_t dev);
86
87static const char * ggml_backend_meta_device_get_name(ggml_backend_dev_t dev) {
88 GGML_ASSERT(ggml_backend_dev_is_meta(dev))if (!(ggml_backend_dev_is_meta(dev))) ggml_abort("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 88, "GGML_ASSERT(%s) failed", "ggml_backend_dev_is_meta(dev)"
)
;
89 const ggml_backend_meta_device_context * meta_dev_ctx = (const ggml_backend_meta_device_context *) dev->context;
90 return meta_dev_ctx->name.c_str();
91}
92
93static const char * ggml_backend_meta_device_get_description(ggml_backend_dev_t dev) {
94 GGML_ASSERT(ggml_backend_dev_is_meta(dev))if (!(ggml_backend_dev_is_meta(dev))) ggml_abort("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 94, "GGML_ASSERT(%s) failed", "ggml_backend_dev_is_meta(dev)"
)
;
95 const ggml_backend_meta_device_context * meta_dev_ctx = (const ggml_backend_meta_device_context *) dev->context;
96 return meta_dev_ctx->description.c_str();
97}
98
99static void ggml_backend_meta_device_get_memory(ggml_backend_dev_t dev, size_t * free, size_t * total) {
100 GGML_ASSERT(ggml_backend_dev_is_meta(dev))if (!(ggml_backend_dev_is_meta(dev))) ggml_abort("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 100, "GGML_ASSERT(%s) failed", "ggml_backend_dev_is_meta(dev)"
)
;
101 const ggml_backend_meta_device_context * meta_dev_ctx = (const ggml_backend_meta_device_context *) dev->context;
102 *free = 0;
103 *total = 0;
104 for (ggml_backend_dev_t dev : meta_dev_ctx->simple_devs) {
105 size_t tmp_free, tmp_total;
106 ggml_backend_dev_memory(dev, &tmp_free, &tmp_total);
107 *free += tmp_free;
108 *total += tmp_total;
109 }
110}
111
112static enum ggml_backend_dev_type ggml_backend_meta_device_get_type(ggml_backend_dev_t dev) {
113 return GGML_BACKEND_DEVICE_TYPE_META;
114
115 GGML_UNUSED(dev)(void)(dev);
116}
117
118static void ggml_backend_meta_device_get_props(ggml_backend_dev_t dev, ggml_backend_dev_props * props) {
119 GGML_ASSERT(ggml_backend_dev_is_meta(dev))if (!(ggml_backend_dev_is_meta(dev))) ggml_abort("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 119, "GGML_ASSERT(%s) failed", "ggml_backend_dev_is_meta(dev)"
)
;
120 const ggml_backend_meta_device_context * meta_dev_ctx = (const ggml_backend_meta_device_context *) dev->context;
121
122 // TODO replace placeholders
123 props->name = ggml_backend_meta_device_get_name(dev);
124 props->description = ggml_backend_meta_device_get_description(dev);
125 props->type = ggml_backend_meta_device_get_type(dev);
126 props->device_id = 0;
127
128 ggml_backend_meta_device_get_memory(dev, &props->memory_free, &props->memory_total);
129
130 props->caps = {
131 /* .async = */ true,
132 /* .host_buffer = */ false, // Not implemented.
133 /* .buffer_from_host_ptr = */ false, // Not implemented.
134 /* .events = */ false, // Not implemented.
135 };
136 for (ggml_backend_dev_t simple_dev : meta_dev_ctx->simple_devs) {
137 ggml_backend_dev_props tmp_props;
138 ggml_backend_dev_get_props(simple_dev, &tmp_props);
139 props->caps.async = props->caps.async && tmp_props.caps.async;
140 props->caps.host_buffer = props->caps.host_buffer && tmp_props.caps.host_buffer;
141 props->caps.buffer_from_host_ptr = props->caps.buffer_from_host_ptr && tmp_props.caps.buffer_from_host_ptr;
142 props->caps.events = props->caps.events && tmp_props.caps.events;
143 }
144}
145
146static ggml_backend_t ggml_backend_meta_device_init_backend(ggml_backend_dev_t dev, const char * params);
147
148static ggml_backend_buffer_type_t ggml_backend_meta_device_get_buffer_type(ggml_backend_dev_t dev);
149
150static ggml_backend_buffer_type_t ggml_backend_meta_device_get_host_buffer_type(ggml_backend_dev_t dev);
151
152static bool ggml_backend_meta_device_supports_op(ggml_backend_dev_t dev, const ggml_tensor * op) {
153 GGML_ASSERT(ggml_backend_dev_is_meta(dev))if (!(ggml_backend_dev_is_meta(dev))) ggml_abort("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 153, "GGML_ASSERT(%s) failed", "ggml_backend_dev_is_meta(dev)"
)
;
154 const ggml_backend_meta_device_context * meta_dev_ctx = (const ggml_backend_meta_device_context *) dev->context;
155 return std::all_of(meta_dev_ctx->simple_devs.begin(), meta_dev_ctx->simple_devs.end(),
156 [op](ggml_backend_dev_t simple_dev) { return ggml_backend_dev_supports_op(simple_dev, op); });
157}
158
159static bool ggml_backend_meta_device_supports_buft(ggml_backend_dev_t dev, ggml_backend_buffer_type_t buft) {
160 GGML_ASSERT(ggml_backend_dev_is_meta(dev))if (!(ggml_backend_dev_is_meta(dev))) ggml_abort("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 160, "GGML_ASSERT(%s) failed", "ggml_backend_dev_is_meta(dev)"
)
;
161 ggml_backend_dev_t dev_buft = ggml_backend_buft_get_device(buft);
162 if (!ggml_backend_dev_is_meta(dev_buft)) {
163 return false;
164 }
165 const ggml_backend_meta_device_context * meta_dev_ctx = (const ggml_backend_meta_device_context *) dev->context;
166 const ggml_backend_meta_device_context * meta_buft_dev_ctx = (const ggml_backend_meta_device_context *) dev_buft->context;
167 if (meta_dev_ctx->simple_devs.size() != meta_buft_dev_ctx->simple_devs.size()) {
168 return false;
169 }
170 for (size_t i = 0; i < meta_dev_ctx->simple_devs.size(); i++) {
171 if (meta_dev_ctx->simple_devs[i] != meta_buft_dev_ctx->simple_devs[i]) {
172 return false;
173 }
174 }
175 return true;
176}
177
178static const ggml_backend_device_i ggml_backend_meta_device_iface = {
179 /* .get_name = */ ggml_backend_meta_device_get_name,
180 /* .get_description = */ ggml_backend_meta_device_get_description,
181 /* .get_memory = */ ggml_backend_meta_device_get_memory,
182 /* .get_type = */ ggml_backend_meta_device_get_type,
183 /* .get_props = */ ggml_backend_meta_device_get_props,
184 /* .init_backend = */ ggml_backend_meta_device_init_backend,
185 /* .get_buffer_type = */ ggml_backend_meta_device_get_buffer_type,
186 /* .get_host_buffer_type = */ ggml_backend_meta_device_get_host_buffer_type,
187 /* .buffer_from_host_ptr = */ nullptr,
188 /* .supports_op = */ ggml_backend_meta_device_supports_op,
189 /* .supports_buft = */ ggml_backend_meta_device_supports_buft,
190 /* .offload_op = */ nullptr,
191 /* .event_new = */ nullptr,
192 /* .event_free = */ nullptr,
193 /* .event_synchronize = */ nullptr,
194};
195
196static bool ggml_backend_dev_is_meta(ggml_backend_dev_t dev) {
197 return dev != nullptr && dev->iface.get_name == ggml_backend_meta_device_iface.get_name;
198}
199
200static size_t ggml_backend_meta_dev_n_devs(ggml_backend_dev_t meta_dev) {
201 GGML_ASSERT(ggml_backend_dev_is_meta(meta_dev))if (!(ggml_backend_dev_is_meta(meta_dev))) ggml_abort("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 201, "GGML_ASSERT(%s) failed", "ggml_backend_dev_is_meta(meta_dev)"
)
;
202 const ggml_backend_meta_device_context * meta_dev_ctx = (const ggml_backend_meta_device_context *) meta_dev->context;
203 return meta_dev_ctx->simple_devs.size();
204}
205
206static ggml_backend_dev_t ggml_backend_meta_dev_simple_dev(ggml_backend_dev_t meta_dev, size_t index) {
207 GGML_ASSERT(ggml_backend_dev_is_meta(meta_dev))if (!(ggml_backend_dev_is_meta(meta_dev))) ggml_abort("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 207, "GGML_ASSERT(%s) failed", "ggml_backend_dev_is_meta(meta_dev)"
)
;
208 const ggml_backend_meta_device_context * meta_dev_ctx = (const ggml_backend_meta_device_context *) meta_dev->context;
209 GGML_ASSERT(index < meta_dev_ctx->simple_devs.size())if (!(index < meta_dev_ctx->simple_devs.size())) ggml_abort
("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 209, "GGML_ASSERT(%s) failed", "index < meta_dev_ctx->simple_devs.size()"
)
;
210 return meta_dev_ctx->simple_devs[index];
211}
212
213ggml_backend_dev_t ggml_backend_meta_device(
214 ggml_backend_dev_t * devs, size_t n_devs, ggml_backend_meta_get_split_state_t get_split_state, void * get_split_state_ud) {
215 GGML_ASSERT(n_devs <= GGML_BACKEND_META_MAX_DEVICES)if (!(n_devs <= 16)) ggml_abort("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 215, "GGML_ASSERT(%s) failed", "n_devs <= GGML_BACKEND_META_MAX_DEVICES"
)
;
1
Assuming 'n_devs' is <= 16
2
Taking false branch
216 // TODO: this is not thread-safe - needs to be fixed
217 static std::vector<std::unique_ptr<ggml_backend_meta_device_context>> ctxs;
218 static std::map<ggml_backend_meta_device_context, struct ggml_backend_device> meta_devs;
219
220 std::vector<ggml_backend_dev_t> simple_devs;
221 simple_devs.reserve(n_devs);
222 for (size_t i = 0; i < n_devs; i++) {
3
Assuming 'i' is >= 'n_devs'
4
Loop condition is false. Execution continues on line 225
223 simple_devs.push_back(devs[i]);
224 }
225 ggml_backend_meta_device_context ctx(simple_devs, get_split_state, get_split_state_ud);
5
Calling constructor for 'ggml_backend_meta_device_context'
226
227 {
228 auto it = meta_devs.find(ctx);
229 if (it != meta_devs.end()) {
230 return &it->second;
231 }
232 }
233 ctxs.push_back(std::make_unique<ggml_backend_meta_device_context>(ctx));
234
235 struct ggml_backend_device meta_dev = {
236 /*iface =*/ ggml_backend_meta_device_iface,
237 /*reg =*/ nullptr,
238 /*ctx =*/ ctxs.back().get(),
239 };
240
241 auto result = meta_devs.emplace(*ctxs.back(), meta_dev);
242 return &result.first->second;
243}
244
245//
246// meta backend buffer type
247//
248
249struct ggml_backend_meta_buffer_type_context {
250 std::vector<ggml_backend_buffer_type_t> simple_bufts;
251
252 std::string name;
253
254 ggml_backend_meta_buffer_type_context(std::vector<ggml_backend_buffer_type_t> simple_bufts) : simple_bufts(std::move(simple_bufts)) {
255 name = "Meta(";
256 for (size_t i = 0; i < simple_bufts.size(); i++) {
257 if (i > 0) {
258 name += ",";
259 }
260 name += ggml_backend_buft_name(simple_bufts[i]);
261 }
262 name += ")";
263 }
264
265 bool operator<(const ggml_backend_meta_buffer_type_context & other) const {
266 return simple_bufts < other.simple_bufts;
267 }
268};
269
270static size_t ggml_backend_meta_buft_n_bufts(ggml_backend_buffer_type_t meta_buft) {
271 GGML_ASSERT(ggml_backend_buft_is_meta(meta_buft))if (!(ggml_backend_buft_is_meta(meta_buft))) ggml_abort("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 271, "GGML_ASSERT(%s) failed", "ggml_backend_buft_is_meta(meta_buft)"
)
;
272 const ggml_backend_meta_buffer_type_context * meta_buft_ctx = (const ggml_backend_meta_buffer_type_context *) meta_buft->context;
273 return meta_buft_ctx->simple_bufts.size();
274}
275
276static const char * ggml_backend_meta_buffer_type_get_name(ggml_backend_buffer_type_t buft) {
277 GGML_ASSERT(ggml_backend_buft_is_meta(buft))if (!(ggml_backend_buft_is_meta(buft))) ggml_abort("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 277, "GGML_ASSERT(%s) failed", "ggml_backend_buft_is_meta(buft)"
)
;
278 const ggml_backend_meta_buffer_type_context * meta_buft_ctx = (const ggml_backend_meta_buffer_type_context *) buft->context;
279 return meta_buft_ctx->name.c_str();
280}
281
282static ggml_backend_buffer_type_t ggml_backend_meta_buft_simple_buft(ggml_backend_buffer_type_t meta_buft, size_t index) {
283 GGML_ASSERT(ggml_backend_buft_is_meta(meta_buft))if (!(ggml_backend_buft_is_meta(meta_buft))) ggml_abort("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 283, "GGML_ASSERT(%s) failed", "ggml_backend_buft_is_meta(meta_buft)"
)
;
284 const ggml_backend_meta_buffer_type_context * meta_buft_ctx = (const ggml_backend_meta_buffer_type_context *) meta_buft->context;
285 GGML_ASSERT(index < meta_buft_ctx->simple_bufts.size())if (!(index < meta_buft_ctx->simple_bufts.size())) ggml_abort
("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 285, "GGML_ASSERT(%s) failed", "index < meta_buft_ctx->simple_bufts.size()"
)
;
286 return meta_buft_ctx->simple_bufts[index];
287}
288
289static ggml_backend_buffer_t ggml_backend_meta_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size);
290
291static size_t ggml_backend_meta_buffer_type_get_alignment(ggml_backend_buffer_type_t buft) {
292 const size_t n_simple_bufts = ggml_backend_meta_buft_n_bufts(buft);
293 size_t max_alignment = 1;
294 for (size_t i = 0; i < n_simple_bufts; i++) {
295 const size_t alignment = ggml_backend_buft_get_alignment(ggml_backend_meta_buft_simple_buft(buft, i));
296 max_alignment = std::max(max_alignment, alignment);
297 GGML_ASSERT(max_alignment % alignment == 0)if (!(max_alignment % alignment == 0)) ggml_abort("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 297, "GGML_ASSERT(%s) failed", "max_alignment % alignment == 0"
)
;
298 }
299 return max_alignment;
300}
301
302static size_t ggml_backend_meta_buffer_type_get_max_size(ggml_backend_buffer_type_t buft) {
303 const size_t n_simple_bufts = ggml_backend_meta_buft_n_bufts(buft);
304 size_t max_size = SIZE_MAX(18446744073709551615UL);
305 for (size_t i = 0; i < n_simple_bufts; i++) {
306 max_size = std::min(max_size, ggml_backend_buft_get_max_size(ggml_backend_meta_buft_simple_buft(buft, i)));
307 }
308 return max_size;
309}
310
311static size_t ggml_backend_meta_buffer_type_get_alloc_size(ggml_backend_buffer_type_t buft, const ggml_tensor * tensor) {
312 const size_t n_simple_bufts = ggml_backend_meta_buft_n_bufts(buft);
313 size_t max_alloc_size = 0;
314 for (size_t i = 0; i < n_simple_bufts; i++) {
315 const size_t alloc_size = ggml_backend_buft_get_alloc_size(ggml_backend_meta_buft_simple_buft(buft, i), tensor);
316 max_alloc_size = std::max(max_alloc_size, alloc_size);
317 }
318 return max_alloc_size;
319}
320
321static bool ggml_backend_meta_buffer_type_is_host(ggml_backend_buffer_type_t buft) {
322 const size_t n_simple_bufts = ggml_backend_meta_buft_n_bufts(buft);
323 for (size_t i = 0; i < n_simple_bufts; i++) {
324 if (!ggml_backend_buft_is_host(ggml_backend_meta_buft_simple_buft(buft, i))) {
325 return false;
326 }
327 }
328 return true;
329}
330
331static const struct ggml_backend_buffer_type_i ggml_backend_meta_buffer_type_iface = {
332 /* .get_name = */ ggml_backend_meta_buffer_type_get_name,
333 /* .alloc_buffer = */ ggml_backend_meta_buffer_type_alloc_buffer,
334 /* .get_alignment = */ ggml_backend_meta_buffer_type_get_alignment,
335 /* .get_max_size = */ ggml_backend_meta_buffer_type_get_max_size,
336 /* .get_alloc_size = */ ggml_backend_meta_buffer_type_get_alloc_size,
337 /* .is_host = */ ggml_backend_meta_buffer_type_is_host,
338};
339
340bool ggml_backend_buft_is_meta(ggml_backend_buffer_type_t buft) {
341 return buft != nullptr && buft->iface.get_name == ggml_backend_meta_buffer_type_iface.get_name;
342}
343
344static ggml_backend_buffer_type_t ggml_backend_meta_device_get_buffer_type(ggml_backend_dev_t dev) {
345 static std::map<ggml_backend_dev_t, struct ggml_backend_buffer_type> meta_bufts;
346 GGML_ASSERT(ggml_backend_dev_is_meta(dev))if (!(ggml_backend_dev_is_meta(dev))) ggml_abort("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 346, "GGML_ASSERT(%s) failed", "ggml_backend_dev_is_meta(dev)"
)
;
347 {
348 auto it = meta_bufts.find(dev);
349 if (it != meta_bufts.end()) {
350 return &it->second;
351 }
352 }
353
354 const size_t n_devs = ggml_backend_meta_dev_n_devs(dev);
355 std::vector<ggml_backend_buffer_type_t> simple_bufts;
356 simple_bufts.reserve(n_devs);
357 for (size_t i = 0; i < n_devs; i++) {
358 simple_bufts.push_back(ggml_backend_dev_buffer_type(ggml_backend_meta_dev_simple_dev(dev, i)));
359 }
360 ggml_backend_meta_buffer_type_context * buft_ctx = new ggml_backend_meta_buffer_type_context(simple_bufts);
361
362 struct ggml_backend_buffer_type meta_buft = {
363 /*iface =*/ ggml_backend_meta_buffer_type_iface,
364 /*device =*/ dev,
365 /*ctx =*/ buft_ctx,
366 };
367 auto result = meta_bufts.emplace(dev, meta_buft);
368 return &result.first->second;
369}
370
371static ggml_backend_buffer_type_t ggml_backend_meta_device_get_host_buffer_type(ggml_backend_dev_t dev) {
372 GGML_ASSERT(ggml_backend_dev_is_meta(dev))if (!(ggml_backend_dev_is_meta(dev))) ggml_abort("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 372, "GGML_ASSERT(%s) failed", "ggml_backend_dev_is_meta(dev)"
)
;
373 const ggml_backend_meta_device_context * meta_dev_ctx = (const ggml_backend_meta_device_context *) dev->context;
374
375 ggml_backend_buffer_type_t host_buft = nullptr;
376 for (ggml_backend_dev_t simple_dev : meta_dev_ctx->simple_devs) {
377 ggml_backend_buffer_type_t simple_host_buft = ggml_backend_dev_host_buffer_type(simple_dev);
378 if (simple_host_buft == nullptr) {
379 return nullptr;
380 }
381 if (host_buft == nullptr) {
382 host_buft = simple_host_buft;
383 } else if (host_buft != simple_host_buft) {
384 // if different simple devices have different host buffer types,
385 // we cannot provide a single host buffer type for the meta device
386 return nullptr;
387 }
388 }
389 return host_buft;
390}
391
392//
393// meta backend buffer
394//
395
396// Container to hold the tensor slices per simple ggml backend buffer.
397struct ggml_backend_meta_simple_tensor_container {
398 std::vector<ggml_context_ptr> ctxs;
399 std::map<const ggml_tensor *, std::vector<ggml_tensor *>> simple_tensors;
400
401 ggml_backend_meta_simple_tensor_container(const ggml_init_params & params, const int n_simple) {
402 ctxs.reserve(n_simple);
403 for (int i = 0; i < n_simple; i++) {
404 ctxs.emplace_back(ggml_init(params));
405 }
406 }
407 ggml_backend_meta_simple_tensor_container() {}
408};
409
410struct ggml_backend_meta_buffer_context {
411 // FIXME
412 // Most tensors can simply be stored statically in their own buffer.
413 // Externally created views however also need a mapping to simple tensors but they use the buffer of the view source.
414 // If external views are simply using that buffer they will slowly deplete its memory.
415 // Current solution: rotating set of 2 "compute" containers to hold external views, works correctly for llama.cpp.
416 // Long-term: tie the lifetime of external views to the meta backend executing the graph instead,
417 // currently not possible due to graph-external operations in the backend scheduler.
418 ggml_backend_meta_simple_tensor_container stc_static;
419 ggml_backend_meta_simple_tensor_container stc_compute[2];
420 int stc_compute_index = 0;
421 int stc_compute_index_next = 0;
422 std::vector<ggml_backend_buffer_ptr> bufs;
423
424 // FIXME
425 // The size of the split state cache is unbounded and can theoretically grow infinitely large.
426 // However, it is also expensive to build and clearing it on every rebuild in ggml_backend_meta_graph_compute is too expensive.
427 static constexpr size_t nbtc = GGML_TENSOR_SIZE - sizeof(ggml_tensor::padding);
428 std::map<std::pair<const ggml_tensor *, bool>, std::pair<ggml_backend_meta_split_state, char[nbtc]>> split_state_cache;
429
430 int debug;
431
432 ggml_backend_meta_buffer_context(
433 ggml_backend_meta_simple_tensor_container & stc_static,
434 ggml_backend_meta_simple_tensor_container & stc_compute_0,
435 ggml_backend_meta_simple_tensor_container & stc_compute_1,
436 const std::vector<ggml_backend_buffer_t> & bufs)
437 : stc_static(std::move(stc_static)), stc_compute{std::move(stc_compute_0), std::move(stc_compute_1)} {
438 this->bufs.reserve(bufs.size());
439 for (ggml_backend_buffer_t buf : bufs) {
440 this->bufs.emplace_back(buf);
441 }
442 const char * GGML_META_DEBUG = getenv("GGML_META_DEBUG");
443 debug = GGML_META_DEBUG ? atoi(GGML_META_DEBUG) : 0;
444 }
445
446 ggml_backend_meta_simple_tensor_container & get_simple_tensor_container(const ggml_tensor * tensor) {
447 if (stc_static.simple_tensors.find(tensor) != stc_static.simple_tensors.end()) {
448 return stc_static;
449 }
450 return stc_compute[stc_compute_index];
451 }
452};
453
454static void ggml_backend_meta_buffer_free_buffer(ggml_backend_buffer_t buffer) {
455 GGML_ASSERT(ggml_backend_buffer_is_meta(buffer))if (!(ggml_backend_buffer_is_meta(buffer))) ggml_abort("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 455, "GGML_ASSERT(%s) failed", "ggml_backend_buffer_is_meta(buffer)"
)
;
456 ggml_backend_meta_buffer_context * buf_ctx = (ggml_backend_meta_buffer_context *) buffer->context;
457 delete buf_ctx;
458}
459
460static size_t ggml_backend_meta_buffer_n_bufs(ggml_backend_buffer_t meta_buf) {
461 GGML_ASSERT(ggml_backend_buffer_is_meta(meta_buf))if (!(ggml_backend_buffer_is_meta(meta_buf))) ggml_abort("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 461, "GGML_ASSERT(%s) failed", "ggml_backend_buffer_is_meta(meta_buf)"
)
;
462 ggml_backend_meta_buffer_context * buf_ctx = (ggml_backend_meta_buffer_context *) meta_buf->context;
463 return buf_ctx->bufs.size();
464}
465
466static ggml_backend_buffer_t ggml_backend_meta_buffer_simple_buffer(ggml_backend_buffer_t meta_buf, size_t index) {
467 GGML_ASSERT(ggml_backend_buffer_is_meta(meta_buf))if (!(ggml_backend_buffer_is_meta(meta_buf))) ggml_abort("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 467, "GGML_ASSERT(%s) failed", "ggml_backend_buffer_is_meta(meta_buf)"
)
;
468 ggml_backend_meta_buffer_context * buf_ctx = (ggml_backend_meta_buffer_context *) meta_buf->context;
469 GGML_ASSERT(index < buf_ctx->bufs.size())if (!(index < buf_ctx->bufs.size())) ggml_abort("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 469, "GGML_ASSERT(%s) failed", "index < buf_ctx->bufs.size()"
)
;
470 return buf_ctx->bufs[index].get();
471}
472
473static struct ggml_tensor * ggml_backend_meta_buffer_simple_tensor(const struct ggml_tensor * tensor, size_t index) {
474 GGML_ASSERT(ggml_backend_buffer_is_meta(tensor->buffer))if (!(ggml_backend_buffer_is_meta(tensor->buffer))) ggml_abort
("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 474, "GGML_ASSERT(%s) failed", "ggml_backend_buffer_is_meta(tensor->buffer)"
)
;
475 ggml_backend_meta_buffer_context * buf_ctx = (ggml_backend_meta_buffer_context *) tensor->buffer->context;
476 GGML_ASSERT(index < buf_ctx->bufs.size())if (!(index < buf_ctx->bufs.size())) ggml_abort("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 476, "GGML_ASSERT(%s) failed", "index < buf_ctx->bufs.size()"
)
;
477
478 ggml_backend_meta_simple_tensor_container & stc = buf_ctx->get_simple_tensor_container(tensor);
479 auto it = stc.simple_tensors.find(tensor);
480 if (it == stc.simple_tensors.end()) {
481 return nullptr;
482 }
483 return it->second[index];
484}
485
486static struct ggml_backend_meta_split_state ggml_backend_meta_get_split_state(const struct ggml_tensor * tensor, bool assume_sync);
487
488static struct ggml_backend_meta_split_state ggml_backend_meta_get_split_state(
489 ggml_backend_meta_simple_tensor_container & stc, const struct ggml_tensor * tensor, bool assume_sync) {
490 // FIXME Currently this function preserves/erases the information in n_segments and nr in an inconsistent way.
491 // Since the operations in question are developed specifically for llama.cpp this currently does not manifest as a bug there.
492 // However, in a broader ggml context with arbitrary ggml graphs this can lead to unexpected results.
493 const size_t n_bufs = ggml_backend_meta_buffer_n_bufs(tensor->buffer);
494 ggml_backend_meta_buffer_context * buf_ctx = (ggml_backend_meta_buffer_context *) tensor->buffer->context;
495
496 auto split_states_equal = [&](const ggml_backend_meta_split_state & a, const ggml_backend_meta_split_state & b) -> bool {
497 if (a.axis != b.axis) {
498 return false;
499 }
500 for (size_t j = 0; j < n_bufs; j++) {
501 int64_t sum_a = 0;
502 for (size_t s = 0; s < a.n_segments; s++) {
503 sum_a += a.ne[s*n_bufs + j] * a.nr[s];
504 }
505 int64_t sum_b = 0;
506 for (size_t s = 0; s < b.n_segments; s++) {
507 sum_b += b.ne[s*n_bufs + j] * b.nr[s];
508 }
509 if (sum_a != sum_b) {
510 return false;
511 }
512 }
513 return true;
514 };
515
516 auto handle_generic = [&](const std::vector<ggml_backend_meta_split_state> & src_ss, bool scalar_only) -> ggml_backend_meta_split_state {
517 ggml_backend_meta_split_state ret = {GGML_BACKEND_SPLIT_AXIS_NONE, {0}, {1}, 1};
518 for (size_t i = 0; i < GGML_MAX_SRC10; i++) {
519 if (tensor->src[i] == nullptr || tensor->src[i] == tensor) {
520 continue;
521 }
522 if (ret.axis == GGML_BACKEND_SPLIT_AXIS_NONE) {
523 ret = src_ss[i];
524 } else if (!split_states_equal(src_ss[i], ret)) {
525 ret = {GGML_BACKEND_SPLIT_AXIS_UNKNOWN, {0}, {1}, 1};
526 break;
527 }
528 }
529 if (ret.axis == GGML_BACKEND_SPLIT_AXIS_NONE) {
530 ret = {GGML_BACKEND_SPLIT_AXIS_UNKNOWN, {0}, {1}, 1};
531 }
532 if (scalar_only && ret.axis >= 0 && ret.axis < GGML_MAX_DIMS4) {
533 ret = {GGML_BACKEND_SPLIT_AXIS_UNKNOWN, {0}, {1}, 1};
534 }
535 GGML_ASSERT(ret.axis != GGML_BACKEND_SPLIT_AXIS_UNKNOWN)if (!(ret.axis != GGML_BACKEND_SPLIT_AXIS_UNKNOWN)) ggml_abort
("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 535, "GGML_ASSERT(%s) failed", "ret.axis != GGML_BACKEND_SPLIT_AXIS_UNKNOWN"
)
;
536 return ret;
537 };
538
539 // Some ops process data on a per-row bases:
540 auto handle_per_row = [&](const std::vector<ggml_backend_meta_split_state> & src_ss) -> ggml_backend_meta_split_state {
541 GGML_ASSERT(src_ss[0].axis != GGML_BACKEND_SPLIT_AXIS_0)if (!(src_ss[0].axis != GGML_BACKEND_SPLIT_AXIS_0)) ggml_abort
("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 541, "GGML_ASSERT(%s) failed", "src_ss[0].axis != GGML_BACKEND_SPLIT_AXIS_0"
)
;
542 return src_ss[0];
543 };
544
545 // Some ops broadcast the src1 data across src0:
546 auto handle_bin_bcast = [&](const std::vector<ggml_backend_meta_split_state> & src_ss) -> ggml_backend_meta_split_state {
547 if (src_ss[0].axis >= 0 && src_ss[0].axis < GGML_MAX_DIMS4 &&
548 tensor->src[1]->ne[src_ss[0].axis] == 1 && src_ss[1].axis == GGML_BACKEND_SPLIT_AXIS_MIRRORED) {
549 return src_ss[0];
550 }
551 if (src_ss[2].axis == GGML_BACKEND_SPLIT_AXIS_MIRRORED && (src_ss[0].axis == src_ss[1].axis ||
552 (src_ss[0].axis == GGML_BACKEND_SPLIT_AXIS_MIRRORED && (src_ss[1].axis == GGML_BACKEND_SPLIT_AXIS_PARTIAL)))) {
553 return src_ss[0]; // GGML_OP_ADD_ID
554 }
555 GGML_ASSERT(tensor->src[2] == nullptr || src_ss[2].axis == GGML_BACKEND_SPLIT_AXIS_MIRRORED)if (!(tensor->src[2] == nullptr || src_ss[2].axis == GGML_BACKEND_SPLIT_AXIS_MIRRORED
)) ggml_abort("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 555, "GGML_ASSERT(%s) failed", "tensor->src[2] == nullptr || src_ss[2].axis == GGML_BACKEND_SPLIT_AXIS_MIRRORED"
)
;
556 return handle_generic(src_ss, /*scalar_only =*/ false);
557 };
558
559 auto handle_concat = [&](const std::vector<ggml_backend_meta_split_state> & src_ss) -> ggml_backend_meta_split_state {
560 const ggml_backend_meta_split_axis concat_axis = ggml_backend_meta_split_axis(ggml_get_op_params_i32(tensor, 0));
561 if (src_ss[0].axis == GGML_BACKEND_SPLIT_AXIS_MIRRORED && src_ss[1].axis >= 0 && src_ss[1].axis < GGML_MAX_DIMS4) {
562 GGML_ASSERT(concat_axis != src_ss[1].axis)if (!(concat_axis != src_ss[1].axis)) ggml_abort("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 562, "GGML_ASSERT(%s) failed", "concat_axis != src_ss[1].axis"
)
;
563 return src_ss[1];
564 }
565 if (src_ss[1].axis == GGML_BACKEND_SPLIT_AXIS_MIRRORED && src_ss[0].axis >= 0 && src_ss[0].axis < GGML_MAX_DIMS4) {
566 GGML_ASSERT(concat_axis != src_ss[0].axis)if (!(concat_axis != src_ss[0].axis)) ggml_abort("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 566, "GGML_ASSERT(%s) failed", "concat_axis != src_ss[0].axis"
)
;
567 return src_ss[0];
568 }
569 if (src_ss[0].axis == src_ss[1].axis && src_ss[0].axis != concat_axis) {
570 return src_ss[0];
571 }
572 return handle_generic(src_ss, /*scalar_only =*/ true);
573 };
574
575 auto handle_mul_mat = [&](const std::vector<ggml_backend_meta_split_state> & src_ss) -> ggml_backend_meta_split_state {
576 if (src_ss[0].axis == GGML_BACKEND_SPLIT_AXIS_MIRRORED && src_ss[1].axis == GGML_BACKEND_SPLIT_AXIS_MIRRORED) {
577 return {GGML_BACKEND_SPLIT_AXIS_MIRRORED, {0}, {1}, 1};
578 }
579 if (src_ss[0].axis == GGML_BACKEND_SPLIT_AXIS_1 && src_ss[1].axis == GGML_BACKEND_SPLIT_AXIS_MIRRORED) {
580 ggml_backend_meta_split_state ret = src_ss[0];
581 ret.axis = GGML_BACKEND_SPLIT_AXIS_0;
582 ret.nr[0] = 1;
583 ret.n_segments = 1;
584 return ret;
585 }
586 if (src_ss[1].axis == GGML_BACKEND_SPLIT_AXIS_1 && src_ss[0].axis == GGML_BACKEND_SPLIT_AXIS_MIRRORED) {
587 return src_ss[1];
588 }
589 if (src_ss[0].axis == GGML_BACKEND_SPLIT_AXIS_0 && src_ss[1].axis == GGML_BACKEND_SPLIT_AXIS_0) {
590 GGML_ASSERT(split_states_equal(src_ss[0], src_ss[1]))if (!(split_states_equal(src_ss[0], src_ss[1]))) ggml_abort("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 590, "GGML_ASSERT(%s) failed", "split_states_equal(src_ss[0], src_ss[1])"
)
;
591 return {assume_sync ? GGML_BACKEND_SPLIT_AXIS_MIRRORED : GGML_BACKEND_SPLIT_AXIS_PARTIAL, {0}, {1}, 1};
592 }
593 GGML_ABORT("fatal error")ggml_abort("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 593, "fatal error")
;
594 //return {GGML_BACKEND_SPLIT_AXIS_UNKNOWN, {0}, {1}, 1};
595 };
596
597 auto handle_reshape = [&](const std::vector<ggml_backend_meta_split_state> & src_ss) -> ggml_backend_meta_split_state {
598 switch (src_ss[0].axis) {
599 case GGML_BACKEND_SPLIT_AXIS_0:
600 case GGML_BACKEND_SPLIT_AXIS_1:
601 case GGML_BACKEND_SPLIT_AXIS_2:
602 case GGML_BACKEND_SPLIT_AXIS_3: {
603 GGML_ASSERT(src_ss[0].n_segments == 1)if (!(src_ss[0].n_segments == 1)) ggml_abort("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 603, "GGML_ASSERT(%s) failed", "src_ss[0].n_segments == 1")
;
604 if (src_ss[0].axis == ggml_n_dims(tensor->src[0]) - 1 && src_ss[0].nr[0] == 1) {
605 return {ggml_backend_meta_split_axis(ggml_n_dims(tensor) - 1), {0}, {1}, 1};
606 }
607 int64_t base_ne_in = tensor->src[0]->ne[0];
608 for (int dim = 1; dim <= src_ss[0].axis; dim++) {
609 base_ne_in *= tensor->src[0]->ne[dim];
610 }
611 base_ne_in /= src_ss[0].nr[0];
612 int64_t base_ne_out = 1;
613 for (int dim = 0; dim < GGML_MAX_DIMS4; dim++) {
614 const int64_t base_ne_out_next = base_ne_out *= tensor->ne[dim];
615 if (base_ne_out_next % base_ne_in == 0) {
616 return {ggml_backend_meta_split_axis(dim), {0}, {uint32_t(base_ne_out_next/base_ne_in)}, 1};
617 }
618 if (base_ne_out_next > base_ne_in) {
619 GGML_ASSERT(src_ss[0].n_segments == 1)if (!(src_ss[0].n_segments == 1)) ggml_abort("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 619, "GGML_ASSERT(%s) failed", "src_ss[0].n_segments == 1")
;
620 GGML_ASSERT(src_ss[0].nr[0] == 1)if (!(src_ss[0].nr[0] == 1)) ggml_abort("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 620, "GGML_ASSERT(%s) failed", "src_ss[0].nr[0] == 1")
;
621 return {ggml_backend_meta_split_axis(dim), {0}, {1}, 1};
622 }
623 base_ne_out = base_ne_out_next;
624 }
625 GGML_ABORT("shape mismatch for %s", ggml_op_name(tensor->op))ggml_abort("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 625, "shape mismatch for %s", ggml_op_name(tensor->op))
;
626 }
627 case GGML_BACKEND_SPLIT_AXIS_MIRRORED:
628 case GGML_BACKEND_SPLIT_AXIS_PARTIAL: {
629 return src_ss[0];
630 }
631 default: {
632 GGML_ABORT("fatal error")ggml_abort("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 632, "fatal error")
;
633 //return {GGML_BACKEND_SPLIT_AXIS_UNKNOWN, {0}, {1}, 1};
634 }
635 }
636 };
637
638 auto handle_cpy = [&](const std::vector<ggml_backend_meta_split_state> & src_ss) -> ggml_backend_meta_split_state {
639 if (src_ss[0].axis >= 0 && src_ss[0].axis < GGML_MAX_DIMS4) {
640 return handle_reshape(src_ss);
641 }
642 return handle_generic(src_ss, /*scalar_only =*/ false);
643 };
644
645 auto handle_view = [&](const std::vector<ggml_backend_meta_split_state> & src_ss) -> ggml_backend_meta_split_state {
646 if (ggml_is_contiguous(tensor) && ggml_is_contiguous(tensor->src[0])) {
647 return handle_reshape(src_ss);
648 }
649 const int axis = src_ss[0].axis;
650 {
651 bool all_strides_the_same = true;
652 for (int dim = 0; dim < GGML_MAX_DIMS4; dim++) {
653 if (tensor->ne[dim] == 1 && tensor->src[0]->ne[dim] == 1) {
654 continue;
655 }
656 if (tensor->nb[dim] != tensor->src[0]->nb[dim]) {
657 all_strides_the_same = false;
658 break;
659 }
660 }
661 if (all_strides_the_same) {
662 return src_ss[0];
663 }
664 }
665 if (!ggml_is_permuted(tensor) && !ggml_is_permuted(tensor->src[0]) && axis >= 0 && axis < GGML_MAX_DIMS4-1) {
666 for (int dim = 0; dim < GGML_MAX_DIMS4-1; dim++) {
667 if (tensor->nb[dim+1] == tensor->src[0]->nb[axis+1]) {
668 return {ggml_backend_meta_split_axis(dim), {0}, {1}, 1};
669 }
670 }
671 GGML_ABORT("fatal error")ggml_abort("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 671, "fatal error")
;
672 }
673 if (src_ss[0].axis == GGML_BACKEND_SPLIT_AXIS_MIRRORED || src_ss[0].axis == GGML_BACKEND_SPLIT_AXIS_PARTIAL) {
674 return src_ss[0];
675 }
676 GGML_ABORT("view of permuted tensor not implemented")ggml_abort("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 676, "view of permuted tensor not implemented")
;
677 //return {GGML_BACKEND_SPLIT_AXIS_UNKNOWN, {0}, {1}, 1};
678 };
679
680 auto handle_permute = [&](const std::vector<ggml_backend_meta_split_state> & src_ss) -> ggml_backend_meta_split_state {
681 switch (src_ss[0].axis) {
682 case GGML_BACKEND_SPLIT_AXIS_0:
683 case GGML_BACKEND_SPLIT_AXIS_1:
684 case GGML_BACKEND_SPLIT_AXIS_2:
685 case GGML_BACKEND_SPLIT_AXIS_3: {
686 GGML_ASSERT(src_ss[0].n_segments == 1 || src_ss[0].nr[0] == 1)if (!(src_ss[0].n_segments == 1 || src_ss[0].nr[0] == 1)) ggml_abort
("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 686, "GGML_ASSERT(%s) failed", "src_ss[0].n_segments == 1 || src_ss[0].nr[0] == 1"
)
;
687 return {ggml_backend_meta_split_axis(tensor->op_params[src_ss[0].axis]), {0}, {src_ss[0].nr[0]}, 1};
688 }
689 case GGML_BACKEND_SPLIT_AXIS_MIRRORED:
690 case GGML_BACKEND_SPLIT_AXIS_PARTIAL: {
691 return src_ss[0];
692 }
693 default: {
694 GGML_ABORT("fatal error")ggml_abort("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 694, "fatal error")
;
695 //return {GGML_BACKEND_SPLIT_AXIS_UNKNOWN, {0}, {1}, 1};
696 }
697 }
698 };
699
700 auto handle_transpose = [&](const std::vector<ggml_backend_meta_split_state> & src_ss) -> ggml_backend_meta_split_state {
701 switch (src_ss[0].axis) {
702 case GGML_BACKEND_SPLIT_AXIS_0:
703 case GGML_BACKEND_SPLIT_AXIS_1: {
704 GGML_ASSERT(src_ss[0].n_segments == 1 || src_ss[0].nr[0] == 1)if (!(src_ss[0].n_segments == 1 || src_ss[0].nr[0] == 1)) ggml_abort
("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 704, "GGML_ASSERT(%s) failed", "src_ss[0].n_segments == 1 || src_ss[0].nr[0] == 1"
)
;
705 return {ggml_backend_meta_split_axis(int(src_ss[0].axis) ^ 1), {0}, {src_ss[0].nr[0]}, 1};
706 }
707 case GGML_BACKEND_SPLIT_AXIS_2:
708 case GGML_BACKEND_SPLIT_AXIS_3:
709 case GGML_BACKEND_SPLIT_AXIS_MIRRORED:
710 case GGML_BACKEND_SPLIT_AXIS_PARTIAL: {
711 return src_ss[0];
712 }
713 default: {
714 GGML_ABORT("fatal error")ggml_abort("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 714, "fatal error")
;
715 //return {GGML_BACKEND_SPLIT_AXIS_UNKNOWN, {0}, {1}, 1};
716 }
717 }
718 };
719
720 auto handle_get_rows = [&](const std::vector<ggml_backend_meta_split_state> & src_ss) -> ggml_backend_meta_split_state {
721 if (src_ss[0].axis == GGML_BACKEND_SPLIT_AXIS_0 && src_ss[1].axis == GGML_BACKEND_SPLIT_AXIS_MIRRORED) {
722 return src_ss[0];
723 }
724 return handle_generic(src_ss, /*scalar_only =*/ true);
725 };
726
727 auto handle_set_rows = [&](const std::vector<ggml_backend_meta_split_state> & src_ss) -> ggml_backend_meta_split_state {
728 GGML_ASSERT(src_ss[0].axis != GGML_BACKEND_SPLIT_AXIS_1)if (!(src_ss[0].axis != GGML_BACKEND_SPLIT_AXIS_1)) ggml_abort
("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 728, "GGML_ASSERT(%s) failed", "src_ss[0].axis != GGML_BACKEND_SPLIT_AXIS_1"
)
;
729 GGML_ASSERT(src_ss[1].axis == GGML_BACKEND_SPLIT_AXIS_MIRRORED)if (!(src_ss[1].axis == GGML_BACKEND_SPLIT_AXIS_MIRRORED)) ggml_abort
("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 729, "GGML_ASSERT(%s) failed", "src_ss[1].axis == GGML_BACKEND_SPLIT_AXIS_MIRRORED"
)
;
730 GGML_ASSERT(split_states_equal(src_ss[0], src_ss[2]))if (!(split_states_equal(src_ss[0], src_ss[2]))) ggml_abort("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 730, "GGML_ASSERT(%s) failed", "split_states_equal(src_ss[0], src_ss[2])"
)
;
731 return src_ss[0];
732 };
733
734 auto handle_rope = [&](const std::vector<ggml_backend_meta_split_state> & src_ss) -> ggml_backend_meta_split_state {
735 GGML_ASSERT(src_ss[1].axis == GGML_BACKEND_SPLIT_AXIS_MIRRORED)if (!(src_ss[1].axis == GGML_BACKEND_SPLIT_AXIS_MIRRORED)) ggml_abort
("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 735, "GGML_ASSERT(%s) failed", "src_ss[1].axis == GGML_BACKEND_SPLIT_AXIS_MIRRORED"
)
;
736 return src_ss[0];
737 };
738
739 auto handle_pad = [&](const std::vector<ggml_backend_meta_split_state> & src_ss) -> ggml_backend_meta_split_state {
740 if (src_ss[0].axis >= 0 && src_ss[0].axis < GGML_MAX_DIMS4) {
741 GGML_ASSERT(tensor->op_params[2*src_ss[0].axis + 0] == 0)if (!(tensor->op_params[2*src_ss[0].axis + 0] == 0)) ggml_abort
("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 741, "GGML_ASSERT(%s) failed", "tensor->op_params[2*src_ss[0].axis + 0] == 0"
)
;
742 GGML_ASSERT(tensor->op_params[2*src_ss[0].axis + 1] == 0)if (!(tensor->op_params[2*src_ss[0].axis + 1] == 0)) ggml_abort
("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 742, "GGML_ASSERT(%s) failed", "tensor->op_params[2*src_ss[0].axis + 1] == 0"
)
;
743 }
744 return src_ss[0];
745 };
746
747 auto handle_flash_attn_ext = [&](const std::vector<ggml_backend_meta_split_state> & src_ss) -> ggml_backend_meta_split_state {
748 GGML_ASSERT( src_ss[0].axis == GGML_BACKEND_SPLIT_AXIS_2)if (!(src_ss[0].axis == GGML_BACKEND_SPLIT_AXIS_2)) ggml_abort
("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 748, "GGML_ASSERT(%s) failed", "src_ss[0].axis == GGML_BACKEND_SPLIT_AXIS_2"
)
;
749 GGML_ASSERT( src_ss[1].axis == GGML_BACKEND_SPLIT_AXIS_2)if (!(src_ss[1].axis == GGML_BACKEND_SPLIT_AXIS_2)) ggml_abort
("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 749, "GGML_ASSERT(%s) failed", "src_ss[1].axis == GGML_BACKEND_SPLIT_AXIS_2"
)
;
750 GGML_ASSERT( src_ss[2].axis == GGML_BACKEND_SPLIT_AXIS_2)if (!(src_ss[2].axis == GGML_BACKEND_SPLIT_AXIS_2)) ggml_abort
("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 750, "GGML_ASSERT(%s) failed", "src_ss[2].axis == GGML_BACKEND_SPLIT_AXIS_2"
)
;
751 GGML_ASSERT(tensor->src[4] == nullptr || src_ss[3].axis == GGML_BACKEND_SPLIT_AXIS_MIRRORED)if (!(tensor->src[4] == nullptr || src_ss[3].axis == GGML_BACKEND_SPLIT_AXIS_MIRRORED
)) ggml_abort("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 751, "GGML_ASSERT(%s) failed", "tensor->src[4] == nullptr || src_ss[3].axis == GGML_BACKEND_SPLIT_AXIS_MIRRORED"
)
;
752 GGML_ASSERT(tensor->src[4] == nullptr || src_ss[4].axis == GGML_BACKEND_SPLIT_AXIS_0)if (!(tensor->src[4] == nullptr || src_ss[4].axis == GGML_BACKEND_SPLIT_AXIS_0
)) ggml_abort("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 752, "GGML_ASSERT(%s) failed", "tensor->src[4] == nullptr || src_ss[4].axis == GGML_BACKEND_SPLIT_AXIS_0"
)
;
753 return {GGML_BACKEND_SPLIT_AXIS_1, {0}, {1}, 1};
754 };
755
756 auto handle_ssm_conv = [&](const std::vector<ggml_backend_meta_split_state> & src_ss) -> ggml_backend_meta_split_state {
757 if (src_ss[0].axis == src_ss[1].axis) {
758 if (src_ss[0].axis == GGML_BACKEND_SPLIT_AXIS_0) {
759 return {GGML_BACKEND_SPLIT_AXIS_1, {0}, {1}, 1};
760 }
761 if (src_ss[0].axis == GGML_BACKEND_SPLIT_AXIS_1) {
762 return {GGML_BACKEND_SPLIT_AXIS_0, {0}, {1}, 1};
763 }
764 }
765 return handle_generic(src_ss, /*scalar_only =*/ false);
766 };
767
768 auto handle_gated_delta_net = [&](const std::vector<ggml_backend_meta_split_state> & src_ss) -> ggml_backend_meta_split_state {
769 if (src_ss[0].axis == GGML_BACKEND_SPLIT_AXIS_MIRRORED && src_ss[1].axis == GGML_BACKEND_SPLIT_AXIS_MIRRORED &&
770 src_ss[2].axis == GGML_BACKEND_SPLIT_AXIS_MIRRORED && src_ss[3].axis == GGML_BACKEND_SPLIT_AXIS_MIRRORED &&
771 src_ss[4].axis == GGML_BACKEND_SPLIT_AXIS_MIRRORED && src_ss[5].axis == GGML_BACKEND_SPLIT_AXIS_MIRRORED) {
772 return src_ss[0];
773 }
774 GGML_ASSERT(src_ss[0].axis == GGML_BACKEND_SPLIT_AXIS_1)if (!(src_ss[0].axis == GGML_BACKEND_SPLIT_AXIS_1)) ggml_abort
("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 774, "GGML_ASSERT(%s) failed", "src_ss[0].axis == GGML_BACKEND_SPLIT_AXIS_1"
)
;
775 GGML_ASSERT(src_ss[1].axis == GGML_BACKEND_SPLIT_AXIS_1)if (!(src_ss[1].axis == GGML_BACKEND_SPLIT_AXIS_1)) ggml_abort
("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 775, "GGML_ASSERT(%s) failed", "src_ss[1].axis == GGML_BACKEND_SPLIT_AXIS_1"
)
;
776 GGML_ASSERT(src_ss[2].axis == GGML_BACKEND_SPLIT_AXIS_1)if (!(src_ss[2].axis == GGML_BACKEND_SPLIT_AXIS_1)) ggml_abort
("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 776, "GGML_ASSERT(%s) failed", "src_ss[2].axis == GGML_BACKEND_SPLIT_AXIS_1"
)
;
777 GGML_ASSERT(src_ss[3].axis == GGML_BACKEND_SPLIT_AXIS_1)if (!(src_ss[3].axis == GGML_BACKEND_SPLIT_AXIS_1)) ggml_abort
("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 777, "GGML_ASSERT(%s) failed", "src_ss[3].axis == GGML_BACKEND_SPLIT_AXIS_1"
)
;
778 GGML_ASSERT(src_ss[4].axis == GGML_BACKEND_SPLIT_AXIS_1)if (!(src_ss[4].axis == GGML_BACKEND_SPLIT_AXIS_1)) ggml_abort
("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 778, "GGML_ASSERT(%s) failed", "src_ss[4].axis == GGML_BACKEND_SPLIT_AXIS_1"
)
;
779 // state shape is [S_v, S_v, H_v, n_seqs] (s0 only); the heads dim is its own axis 2,
780 // so a head-aligned split on the input cache lands on axis 2 here.
781 GGML_ASSERT(src_ss[5].axis == GGML_BACKEND_SPLIT_AXIS_2 || src_ss[5].axis == GGML_BACKEND_SPLIT_AXIS_1 || src_ss[5].axis == GGML_BACKEND_SPLIT_AXIS_0)if (!(src_ss[5].axis == GGML_BACKEND_SPLIT_AXIS_2 || src_ss[5
].axis == GGML_BACKEND_SPLIT_AXIS_1 || src_ss[5].axis == GGML_BACKEND_SPLIT_AXIS_0
)) ggml_abort("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 781, "GGML_ASSERT(%s) failed", "src_ss[5].axis == GGML_BACKEND_SPLIT_AXIS_2 || src_ss[5].axis == GGML_BACKEND_SPLIT_AXIS_1 || src_ss[5].axis == GGML_BACKEND_SPLIT_AXIS_0"
)
;
782 return {GGML_BACKEND_SPLIT_AXIS_0, {0}, {1}, 1};
783 };
784
785 auto calculate_split_state = [&]() -> ggml_backend_meta_split_state {
786 if (ggml_nelements(tensor) == 0) {
787 return {GGML_BACKEND_SPLIT_AXIS_UNKNOWN, {0}, {1}, 1};
788 }
789 if (ggml_backend_buffer_get_usage(tensor->buffer) != GGML_BACKEND_BUFFER_USAGE_COMPUTE && tensor->view_src == nullptr) {
790 ggml_backend_dev_t dev = ggml_backend_buft_get_device(ggml_backend_buffer_get_type(tensor->buffer));
791 const ggml_backend_meta_device_context * dev_ctx = (const ggml_backend_meta_device_context *) dev->context;
792 ggml_backend_meta_split_state ret = dev_ctx->get_split_state(tensor, dev_ctx->get_split_state_ud);
793 if (ret.axis >= 0 && ret.axis <= GGML_MAX_DIMS4) {
794 const int64_t granularity = ret.axis == GGML_BACKEND_SPLIT_AXIS_0 ? ggml_blck_size(tensor->type) : 1;
795 int64_t ne_sum = 0;
796 for (size_t s = 0; s < ret.n_segments; s++) {
797 for (size_t j = 0; j < n_bufs; j++) {
798 GGML_ASSERT(ret.ne[s*n_bufs + j] % granularity == 0)if (!(ret.ne[s*n_bufs + j] % granularity == 0)) ggml_abort("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 798, "GGML_ASSERT(%s) failed", "ret.ne[s*n_bufs + j] % granularity == 0"
)
;
799 ne_sum += ret.ne[s*n_bufs + j] * ret.nr[s];
800 }
801 }
802 GGML_ASSERT(ne_sum == tensor->ne[ret.axis])if (!(ne_sum == tensor->ne[ret.axis])) ggml_abort("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 802, "GGML_ASSERT(%s) failed", "ne_sum == tensor->ne[ret.axis]"
)
;
803 }
804 return ret;
805 }
806
807 std::vector<ggml_backend_meta_split_state> src_ss(GGML_MAX_SRC10, {GGML_BACKEND_SPLIT_AXIS_NONE, {0}, {1}, 1});
808 for (size_t i = 0; i < GGML_MAX_SRC10; i++) {
809 if (tensor->src[i] == nullptr || tensor->src[i] == tensor) {
810 src_ss[i] = {GGML_BACKEND_SPLIT_AXIS_UNKNOWN, {0}, {1}, 1};
811 continue;
812 }
813 src_ss[i] = ggml_backend_meta_get_split_state(stc, tensor->src[i], /*assume_sync =*/ true);
814 GGML_ASSERT(src_ss[i].axis != GGML_BACKEND_SPLIT_AXIS_UNKNOWN)if (!(src_ss[i].axis != GGML_BACKEND_SPLIT_AXIS_UNKNOWN)) ggml_abort
("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 814, "GGML_ASSERT(%s) failed", "src_ss[i].axis != GGML_BACKEND_SPLIT_AXIS_UNKNOWN"
)
;
815 }
816
817 ggml_backend_meta_split_state split_state;
818 switch (tensor->op) {
819 case GGML_OP_NONE: {
820 split_state = {GGML_BACKEND_SPLIT_AXIS_MIRRORED, {0}, {1}, 1};
821 } break;
822 case GGML_OP_DUP: {
823 split_state = handle_generic(src_ss, /*scalar_only =*/ true);
824 } break;
825 case GGML_OP_ADD:
826 case GGML_OP_ADD_ID: {
827 split_state = handle_bin_bcast(src_ss);
828 } break;
829 case GGML_OP_ADD1:
830 case GGML_OP_ACC: {
831 split_state = handle_generic(src_ss, /*scalar_only =*/ true);
832 } break;
833 case GGML_OP_SUB:
834 case GGML_OP_MUL:
835 case GGML_OP_DIV: {
836 split_state = handle_bin_bcast(src_ss);
837 } break;
838 case GGML_OP_SQR:
839 case GGML_OP_SQRT:
840 case GGML_OP_LOG:
841 case GGML_OP_SIN:
842 case GGML_OP_COS: {
843 split_state = handle_generic(src_ss, /*scalar_only =*/ false);
844 } break;
845 case GGML_OP_SUM: {
846 split_state = handle_generic(src_ss, /*scalar_only =*/ true);
847 } break;
848 case GGML_OP_SUM_ROWS:
849 case GGML_OP_CUMSUM:
850 case GGML_OP_MEAN:
851 case GGML_OP_ARGMAX:
852 case GGML_OP_COUNT_EQUAL: {
853 split_state = handle_per_row(src_ss);
854 } break;
855 case GGML_OP_REPEAT:
856 case GGML_OP_REPEAT_BACK: {
857 split_state = handle_generic(src_ss, /*scalar_only =*/ false);
858 } break;
859 case GGML_OP_CONCAT: {
860 split_state = handle_concat(src_ss);
861 } break;
862 case GGML_OP_SILU_BACK: {
863 split_state = handle_generic(src_ss, /*scalar_only =*/ false);
864 } break;
865 case GGML_OP_NORM:
866 case GGML_OP_RMS_NORM:
867 case GGML_OP_RMS_NORM_BACK:
868 case GGML_OP_GROUP_NORM:
869 case GGML_OP_L2_NORM: {
870 split_state = handle_per_row(src_ss);
871 } break;
872 case GGML_OP_MUL_MAT:
873 case GGML_OP_MUL_MAT_ID: {
874 split_state = handle_mul_mat(src_ss);
875 } break;
876 case GGML_OP_OUT_PROD: {
877 split_state = handle_generic(src_ss, /*scalar_only =*/ true);
878 } break;
879 case GGML_OP_SCALE: {
880 split_state = handle_generic(src_ss, /*scalar_only =*/ false);
881 } break;
882 case GGML_OP_SET: {
883 split_state = handle_generic(src_ss, /*scalar_only =*/ true);
884 } break;
885 case GGML_OP_CPY: {
886 split_state = handle_cpy(src_ss);
887 } break;
888 case GGML_OP_CONT:
889 case GGML_OP_RESHAPE: {
890 split_state = handle_reshape(src_ss);
891 } break;
892 case GGML_OP_VIEW: {
893 split_state = handle_view(src_ss);
894 } break;
895 case GGML_OP_PERMUTE: {
896 split_state = handle_permute(src_ss);
897 } break;
898 case GGML_OP_TRANSPOSE: {
899 split_state = handle_transpose(src_ss);
900 } break;
901 case GGML_OP_GET_ROWS: {
902 split_state = handle_get_rows(src_ss);
903 } break;
904 case GGML_OP_GET_ROWS_BACK: {
905 split_state = handle_generic(src_ss, /*scalar_only =*/ true);
906 } break;
907 case GGML_OP_SET_ROWS: {
908 split_state = handle_set_rows(src_ss);
909 } break;
910 case GGML_OP_DIAG:
911 case GGML_OP_DIAG_MASK_INF:
912 case GGML_OP_DIAG_MASK_ZERO: {
913 split_state = handle_generic(src_ss, /*scalar_only =*/ true);
914 } break;
915 case GGML_OP_SOFT_MAX:
916 case GGML_OP_SOFT_MAX_BACK: {
917 split_state = handle_generic(src_ss, /*scalar_only =*/ false);
918 } break;
919 case GGML_OP_ROPE: {
920 split_state = handle_rope(src_ss);
921 } break;
922 case GGML_OP_ROPE_BACK: {
923 split_state = handle_generic(src_ss, /*scalar_only =*/ true);
924 } break;
925 case GGML_OP_CLAMP: {
926 split_state = handle_generic(src_ss, /*scalar_only =*/ false);
927 } break;
928 case GGML_OP_CONV_TRANSPOSE_1D:
929 case GGML_OP_IM2COL:
930 case GGML_OP_IM2COL_BACK:
931 case GGML_OP_IM2COL_3D:
932 case GGML_OP_CONV_2D:
933 case GGML_OP_CONV_3D:
934 case GGML_OP_CONV_2D_DW:
935 case GGML_OP_CONV_TRANSPOSE_2D:
936 case GGML_OP_POOL_1D:
937 case GGML_OP_POOL_2D:
938 case GGML_OP_POOL_2D_BACK:
939 case GGML_OP_UPSCALE: {
940 split_state = handle_generic(src_ss, /*scalar_only =*/ true);
941 } break;
942 case GGML_OP_PAD: {
943 split_state = handle_pad(src_ss);
944 } break;
945 case GGML_OP_PAD_REFLECT_1D:
946 case GGML_OP_ROLL:
947 case GGML_OP_ARANGE:
948 case GGML_OP_TIMESTEP_EMBEDDING: {
949 split_state = handle_generic(src_ss, /*scalar_only =*/ true);
950 } break;
951 case GGML_OP_ARGSORT:
952 case GGML_OP_TOP_K: {
953 split_state = handle_per_row(src_ss);
954 } break;
955 case GGML_OP_LEAKY_RELU: {
956 split_state = handle_generic(src_ss, /*scalar_only =*/ false);
957 } break;
958 case GGML_OP_TRI: {
959 split_state = handle_generic(src_ss, /*scalar_only =*/ true);
960 } break;
961 case GGML_OP_FILL: {
962 split_state = handle_generic(src_ss, /*scalar_only =*/ false);
963 } break;
964 case GGML_OP_FLASH_ATTN_EXT: {
965 split_state = handle_flash_attn_ext(src_ss);
966 } break;
967 case GGML_OP_FLASH_ATTN_BACK: {
968 split_state = handle_generic(src_ss, /*scalar_only =*/ true);
969 } break;
970 case GGML_OP_SSM_CONV: {
971 split_state = handle_ssm_conv(src_ss);
972 } break;
973 case GGML_OP_SSM_SCAN:
974 case GGML_OP_WIN_PART:
975 case GGML_OP_WIN_UNPART:
976 case GGML_OP_GET_REL_POS:
977 case GGML_OP_ADD_REL_POS:
978 case GGML_OP_RWKV_WKV6:
979 case GGML_OP_GATED_LINEAR_ATTN:
980 case GGML_OP_RWKV_WKV7:
981 case GGML_OP_SOLVE_TRI: {
982 split_state = handle_generic(src_ss, /*scalar_only =*/ true);
983 } break;
984 case GGML_OP_GATED_DELTA_NET: {
985 split_state = handle_gated_delta_net(src_ss);
986 } break;
987 case GGML_OP_UNARY: {
988 split_state = handle_generic(src_ss, /*scalar_only =*/ false);
989 } break;
990 case GGML_OP_MAP_CUSTOM1:
991 case GGML_OP_MAP_CUSTOM2:
992 case GGML_OP_MAP_CUSTOM3:
993 case GGML_OP_CUSTOM: {
994 split_state = handle_generic(src_ss, /*scalar_only =*/ true);
995 } break;
996 case GGML_OP_CROSS_ENTROPY_LOSS:
997 case GGML_OP_CROSS_ENTROPY_LOSS_BACK: {
998 split_state = handle_per_row(src_ss);
999 } break;
1000 case GGML_OP_OPT_STEP_ADAMW:
1001 case GGML_OP_OPT_STEP_SGD:
1002 case GGML_OP_GLU: {
1003 split_state = handle_generic(src_ss, /*scalar_only =*/ false);
1004 } break;
1005 default: {
1006 GGML_ABORT("ggml op not implemented: %s", ggml_op_name(tensor->op))ggml_abort("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 1006, "ggml op not implemented: %s", ggml_op_name(tensor->
op))
;
1007 split_state = {GGML_BACKEND_SPLIT_AXIS_UNKNOWN, {0}, {1}, 1};
1008 } break;
1009 }
1010 if (split_state.axis >= 0 && split_state.axis < GGML_MAX_DIMS4) {
1011 bool first_src_split_by_axis = true;
1012 const size_t n_bufs = ggml_backend_meta_buffer_n_bufs(tensor->buffer);
1013
1014 for (size_t i = 0; i < GGML_MAX_SRC10; i++) {
1015 if (tensor->src[i] == nullptr || src_ss[i].axis < 0 || src_ss[i].axis >= GGML_MAX_DIMS4) {
1016 continue;
1017 }
1018 if (first_src_split_by_axis) {
1019 for (size_t j = 0; j < n_bufs; j++) {
1020 // Take over ratio from src:
1021 for (size_t s = 0; s < src_ss[i].n_segments; s++) {
1022 split_state.ne[s*n_bufs + j] = 0;
1023 }
1024 for (size_t s = 0; s < src_ss[i].n_segments; s++) {
1025 split_state.ne[j] += src_ss[i].ne[s*n_bufs + j] * src_ss[i].nr[s];
1026 }
1027 split_state.ne[j] *= tensor->ne[split_state.axis];
1028 if (split_state.ne[j] != 0 || tensor->src[i]->ne[src_ss[i].axis] != 0) {
1029 const int64_t div = tensor->src[i]->ne[src_ss[i].axis] * split_state.nr[0];
1030 GGML_ASSERT(split_state.ne[j] % div == 0)if (!(split_state.ne[j] % div == 0)) ggml_abort("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 1030, "GGML_ASSERT(%s) failed", "split_state.ne[j] % div == 0"
)
;
1031 split_state.ne[j] /= div;
1032 }
1033 }
1034 } else {
1035 GGML_ASSERT(split_state.n_segments == 1)if (!(split_state.n_segments == 1)) ggml_abort("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 1035, "GGML_ASSERT(%s) failed", "split_state.n_segments == 1"
)
;
1036 for (size_t j = 0; j < n_bufs; j++) {
1037 // Assert that ratio is consistent:
1038 int64_t sum = 0;
1039 for (size_t s = 0; s < src_ss[i].n_segments; s++) {
1040 sum += src_ss[i].ne[s*n_bufs + j] * src_ss[i].nr[s];
1041 }
1042 GGML_ASSERT(split_state.ne[j]*split_state.nr[0] * tensor->src[i]->ne[src_ss[i].axis]if (!(split_state.ne[j]*split_state.nr[0] * tensor->src[i]
->ne[src_ss[i].axis] == sum * tensor->ne[split_state.axis
])) ggml_abort("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 1043, "GGML_ASSERT(%s) failed", "split_state.ne[j]*split_state.nr[0] * tensor->src[i]->ne[src_ss[i].axis] == sum * tensor->ne[split_state.axis]"
)
1043 == sum * tensor->ne[split_state.axis])if (!(split_state.ne[j]*split_state.nr[0] * tensor->src[i]
->ne[src_ss[i].axis] == sum * tensor->ne[split_state.axis
])) ggml_abort("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 1043, "GGML_ASSERT(%s) failed", "split_state.ne[j]*split_state.nr[0] * tensor->src[i]->ne[src_ss[i].axis] == sum * tensor->ne[split_state.axis]"
)
;
1044 }
1045 }
1046 first_src_split_by_axis = false;
1047 }
1048 GGML_ASSERT(!first_src_split_by_axis)if (!(!first_src_split_by_axis)) ggml_abort("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 1048, "GGML_ASSERT(%s) failed", "!first_src_split_by_axis")
;
1049 }
1050 return split_state;
1051 };
1052
1053 const std::pair key = std::make_pair(tensor, assume_sync);
1054 auto it = buf_ctx->split_state_cache.find(key);
1055 if (it != buf_ctx->split_state_cache.end() && memcmp(it->second.second, (const char *) tensor, sizeof(it->second.second)) != 0) {
1056 buf_ctx->split_state_cache.clear();
1057 it = buf_ctx->split_state_cache.end();
1058 }
1059
1060 if (it == buf_ctx->split_state_cache.end()) {
1061 buf_ctx->split_state_cache[key].first = calculate_split_state();
1062 memcpy(buf_ctx->split_state_cache[key].second, tensor, sizeof(buf_ctx->split_state_cache[key].second));
1063 if (buf_ctx->debug > 0) {
1064 std::string srcs_info;
1065 for (size_t i = 0; i < GGML_MAX_SRC10; i++) {
1066 if (tensor->src[i] == nullptr) {
1067 continue;
1068 }
1069 if (!srcs_info.empty()) {
1070 srcs_info += ", ";
1071 }
1072 const ggml_backend_meta_split_state split_state = ggml_backend_meta_get_split_state(tensor->src[0], true);
1073 GGML_ASSERT(split_state.n_segments == 1)if (!(split_state.n_segments == 1)) ggml_abort("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 1073, "GGML_ASSERT(%s) failed", "split_state.n_segments == 1"
)
;
1074 const char * axis_name = ggml_backend_meta_split_axis_name(split_state.axis);
1075 std::string ne_info;
1076 for (size_t j = 0; j < n_bufs; j++) {
1077 if (!ne_info.empty()) {
1078 ne_info += ", ";
1079 }
1080 ne_info += std::to_string(split_state.ne[j]) + "x" + std::to_string(split_state.nr[0]);
1081 }
1082 srcs_info += std::string(tensor->src[i]->name) + "[" + ggml_op_name(tensor->src[i]->op) + ", " + axis_name + ", {" + ne_info + "}]";
1083 }
1084 std::string ne_info;
1085 for (size_t j = 0; j < n_bufs; j++) {
1086 if (!ne_info.empty()) {
1087 ne_info += ", ";
1088 }
1089 const ggml_backend_meta_split_state & ss = buf_ctx->split_state_cache[key].first;
1090 ne_info += std::to_string(ss.ne[j]) + "x" + std::to_string(ss.nr[0]);
1091 }
1092 GGML_LOG_DEBUG("SPLIT_STATE: {%s} -> %s[%s, %s, {%s}]\n", srcs_info.c_str(), tensor->name, ggml_op_name(tensor->op),ggml_log_internal(GGML_LOG_LEVEL_DEBUG, "SPLIT_STATE: {%s} -> %s[%s, %s, {%s}]\n"
, srcs_info.c_str(), tensor->name, ggml_op_name(tensor->
op), ggml_backend_meta_split_axis_name(buf_ctx->split_state_cache
[key].first.axis), ne_info.c_str())
1093 ggml_backend_meta_split_axis_name(buf_ctx->split_state_cache[key].first.axis), ne_info.c_str())ggml_log_internal(GGML_LOG_LEVEL_DEBUG, "SPLIT_STATE: {%s} -> %s[%s, %s, {%s}]\n"
, srcs_info.c_str(), tensor->name, ggml_op_name(tensor->
op), ggml_backend_meta_split_axis_name(buf_ctx->split_state_cache
[key].first.axis), ne_info.c_str())
;
1094 }
1095 }
1096
1097 ggml_backend_meta_split_state ret = buf_ctx->split_state_cache[key].first;
1098 GGML_ASSERT(ret.axis != GGML_BACKEND_SPLIT_AXIS_NONE)if (!(ret.axis != GGML_BACKEND_SPLIT_AXIS_NONE)) ggml_abort("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 1098, "GGML_ASSERT(%s) failed", "ret.axis != GGML_BACKEND_SPLIT_AXIS_NONE"
)
;
1099#ifndef NDEBUG
1100 if (ret.axis >= 0 && ret.axis < GGML_MAX_DIMS4) {
1101 int64_t ne_ret = 0;
1102 for (size_t s = 0; s < ret.n_segments; s++) {
1103 for (size_t j = 0; j < n_bufs; j++) {
1104 ne_ret += ret.ne[s*n_bufs + j] * ret.nr[s];
1105 }
1106 }
1107 assert(ne_ret == tensor->ne[int(ret.axis)])(static_cast <bool> (ne_ret == tensor->ne[int(ret.axis
)]) ? void (0) : __assert_fail ("ne_ret == tensor->ne[int(ret.axis)]"
, __builtin_FILE (), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__
))
;
1108 }
1109#endif // NDEBUG
1110 return ret;
1111}
1112
1113static struct ggml_backend_meta_split_state ggml_backend_meta_get_split_state(const struct ggml_tensor * tensor, bool assume_sync) {
1114 GGML_ASSERT(ggml_backend_buffer_is_meta(tensor->buffer))if (!(ggml_backend_buffer_is_meta(tensor->buffer))) ggml_abort
("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 1114, "GGML_ASSERT(%s) failed", "ggml_backend_buffer_is_meta(tensor->buffer)"
)
;
1115 ggml_backend_meta_buffer_context * buf_ctx = (ggml_backend_meta_buffer_context *) tensor->buffer->context;
1116 return ggml_backend_meta_get_split_state(buf_ctx->get_simple_tensor_container(tensor), tensor, assume_sync);
1117}
1118
1119static void * ggml_backend_meta_buffer_get_base(ggml_backend_buffer_t buffer) {
1120 GGML_UNUSED(buffer)(void)(buffer);
1121 return (void *) 0x1000000000000000; // FIXME
1122}
1123
1124static enum ggml_status ggml_backend_meta_buffer_init_tensor_impl(ggml_backend_meta_simple_tensor_container & stc, ggml_tensor * tensor) {
1125 GGML_ASSERT(ggml_backend_buffer_is_meta(tensor->buffer))if (!(ggml_backend_buffer_is_meta(tensor->buffer))) ggml_abort
("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 1125, "GGML_ASSERT(%s) failed", "ggml_backend_buffer_is_meta(tensor->buffer)"
)
;
1126 ggml_backend_meta_buffer_context * buf_ctx = (ggml_backend_meta_buffer_context *) tensor->buffer->context;
1127 const size_t n_simple_bufs = ggml_backend_meta_buffer_n_bufs(tensor->buffer);
1128
1129 const ggml_backend_meta_split_state split_state = ggml_backend_meta_get_split_state(stc, tensor, /*assume_sync =*/ true);
1130 GGML_ASSERT(ggml_nelements(tensor) == 0 || split_state.axis != GGML_BACKEND_SPLIT_AXIS_UNKNOWN)if (!(ggml_nelements(tensor) == 0 || split_state.axis != GGML_BACKEND_SPLIT_AXIS_UNKNOWN
)) ggml_abort("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 1130, "GGML_ASSERT(%s) failed", "ggml_nelements(tensor) == 0 || split_state.axis != GGML_BACKEND_SPLIT_AXIS_UNKNOWN"
)
;
1131 GGML_ASSERT(split_state.n_segments <= 16)if (!(split_state.n_segments <= 16)) ggml_abort("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 1131, "GGML_ASSERT(%s) failed", "split_state.n_segments <= 16"
)
;
1132
1133 int split_dim = split_state.axis;
1134 int64_t ne[GGML_MAX_DIMS4];
1135 size_t nb[GGML_MAX_DIMS4];
1136 for (size_t k = 0; k < GGML_MAX_DIMS4; k++) {
1137 ne[k] = tensor->ne[k];
1138 nb[k] = tensor->nb[k];
1139 }
1140
1141 std::vector<ggml_tensor *> simple_tensors;
1142 simple_tensors.reserve(n_simple_bufs);
1143 for (size_t j = 0; j < n_simple_bufs; j++) {
1144 ggml_context * simple_ctx = stc.ctxs[j].get();
1145 ggml_backend_buffer_t simple_buf = buf_ctx->bufs[j].get();
1146
1147 if (split_dim >= 0 && split_dim < GGML_MAX_DIMS4) {
1148 // TODO: the following assert fails for llama-parallel even though the results are correct:
1149 // GGML_ASSERT(ggml_is_contiguously_allocated(tensor));
1150 ne[split_dim] = 0;
1151 for (size_t s = 0; s < split_state.n_segments; s++) {
1152 ne[split_dim] += split_state.ne[s*n_simple_bufs + j] * split_state.nr[s];
1153 }
1154 for (int i = 0; i < GGML_MAX_DIMS4; i++) {
1155 if (tensor->nb[i] > tensor->nb[split_dim]) {
1156 nb[i] = tensor->nb[i] * ne[split_dim]/tensor->ne[split_dim];
1157 }
1158 }
1159 }
1160
1161 ggml_tensor * t_ij = ggml_new_tensor(simple_ctx, tensor->type, GGML_MAX_DIMS4, ne);
1162 t_ij->op = tensor->op;
1163 for (int i = 0; i < GGML_MAX_DIMS4; i++) {
1164 t_ij->nb[i] = nb[i];
1165 }
1166 t_ij->flags = tensor->flags;
1167 memcpy(t_ij->op_params, tensor->op_params, sizeof(tensor->op_params));
1168 ggml_set_name(t_ij, tensor->name);
1169 t_ij->buffer = simple_buf;
1170 t_ij->view_src = tensor->view_src;
1171 t_ij->view_offs = tensor->view_offs;
1172 if (t_ij->view_src != nullptr && ggml_backend_buffer_is_meta(t_ij->view_src->buffer)) {
1173 t_ij->view_src = ggml_backend_meta_buffer_simple_tensor(tensor->view_src, j);
1174 if (t_ij->view_offs > 0 && split_dim >= 0 && split_dim < GGML_MAX_DIMS4) {
1175 GGML_ASSERT(tensor->ne[split_dim] != 0)if (!(tensor->ne[split_dim] != 0)) ggml_abort("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 1175, "GGML_ASSERT(%s) failed", "tensor->ne[split_dim] != 0"
)
;
1176 const int split_dim_view_src = ggml_backend_meta_get_split_state(tensor->view_src, /*assume_sync =*/ true).axis;
1177 GGML_ASSERT(split_dim_view_src >= 0 && split_dim_view_src < GGML_MAX_DIMS)if (!(split_dim_view_src >= 0 && split_dim_view_src
< 4)) ggml_abort("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 1177, "GGML_ASSERT(%s) failed", "split_dim_view_src >= 0 && split_dim_view_src < GGML_MAX_DIMS"
)
;
1178
1179 // The offset can be internal to the data split, in those cases the view offset should not be scaled.
1180 // If however, the offset is larger than the data split then it needs to be scaled proportionally.
1181 bool split_internal_offset = t_ij->view_offs <= tensor->view_src->nb[split_dim_view_src];
1182 for (int i = 0; i < GGML_MAX_DIMS4; i++) {
1183 const size_t dim_size = tensor->ne[i] * tensor->nb[i];
1184 if (tensor->view_offs <= dim_size && dim_size < tensor->nb[split_dim]) {
1185 split_internal_offset = true;
1186 break;
1187 }
1188 }
1189 if (!split_internal_offset) {
1190 t_ij->view_offs = t_ij->view_offs * ne[split_dim]/tensor->ne[split_dim];
1191 }
1192 }
1193 }
1194 if (t_ij->view_src != nullptr) {
1195 t_ij->data = (char *) t_ij->view_src->data + t_ij->view_offs;
1196 } else if (simple_buf != nullptr) {
1197 t_ij->data = (char *) ggml_backend_buffer_get_base(simple_buf)
1198 + size_t(tensor->data) - size_t(ggml_backend_buffer_get_base(tensor->buffer));
1199 }
1200 t_ij->extra = tensor->extra;
1201 for (int i = 0; i < GGML_MAX_SRC10; i++) {
1202 t_ij->src[i] = tensor->src[i];
1203 if (tensor->src[i] == tensor) {
1204 t_ij->src[i] = t_ij;
1205 } else if (t_ij->src[i] != nullptr && ggml_backend_buffer_is_meta(t_ij->src[i]->buffer)) {
1206 t_ij->src[i] = ggml_backend_meta_buffer_simple_tensor(tensor->src[i], j);
1207 }
1208 }
1209
1210 simple_tensors.push_back(t_ij);
1211 }
1212
1213 // If one of the sources has a zero-sized slice, disable the computation:
1214 for (int i = 0; i < GGML_MAX_SRC10; i++) {
1215 if (tensor->src[i] == nullptr || !ggml_backend_buffer_is_meta(tensor->src[i]->buffer)) {
1216 continue;
1217 }
1218
1219 const ggml_backend_meta_split_state split_state_src = ggml_backend_meta_get_split_state(tensor->src[i], /*assume_sync =*/ true);
1220 if (split_state_src.axis < 0 || split_state_src.axis >= GGML_MAX_DIMS4) {
1221 continue;
1222 }
1223 for (size_t j = 0; j < n_simple_bufs; j++) {
1224 int64_t ne_sum = 0;
1225 for (size_t s = 0; s < split_state_src.n_segments; s++) {
1226 ne_sum += split_state_src.ne[s*n_simple_bufs + j] * split_state_src.nr[s];
1227 }
1228 if (ne_sum == 0) {
1229 simple_tensors[j]->flags &= ~GGML_TENSOR_FLAG_COMPUTE;
1230 }
1231 }
1232 }
1233
1234 stc.simple_tensors[tensor] = simple_tensors;
1235
1236 return GGML_STATUS_SUCCESS;
1237}
1238
1239static enum ggml_status ggml_backend_meta_buffer_init_tensor(ggml_backend_buffer_t buffer, ggml_tensor * tensor) {
1240 GGML_ASSERT(ggml_backend_buffer_is_meta(buffer))if (!(ggml_backend_buffer_is_meta(buffer))) ggml_abort("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 1240, "GGML_ASSERT(%s) failed", "ggml_backend_buffer_is_meta(buffer)"
)
;
1241 ggml_backend_meta_buffer_context * buf_ctx = (ggml_backend_meta_buffer_context *) buffer->context;
1242 buf_ctx->stc_compute_index = buf_ctx->stc_compute_index_next;
1243 return ggml_backend_meta_buffer_init_tensor_impl(buf_ctx->get_simple_tensor_container(tensor), tensor);
1244}
1245
1246static void ggml_backend_meta_buffer_set_tensor(ggml_backend_buffer_t buffer, ggml_tensor * tensor, const void * data, size_t offset, size_t size) {
1247 const size_t n_bufs = ggml_backend_meta_buffer_n_bufs(buffer);
1248 GGML_ASSERT(ggml_is_contiguous(tensor))if (!(ggml_is_contiguous(tensor))) ggml_abort("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 1248, "GGML_ASSERT(%s) failed", "ggml_is_contiguous(tensor)"
)
;
1249
1250 const ggml_backend_meta_split_state split_state = ggml_backend_meta_get_split_state(tensor, /*assume_sync =*/ false);
1251
1252 if (split_state.n_segments != 1 || split_state.nr[0] != 1) {
1253 GGML_ASSERT(split_state.axis >= 0 && split_state.axis < GGML_MAX_DIMS)if (!(split_state.axis >= 0 && split_state.axis <
4)) ggml_abort("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 1253, "GGML_ASSERT(%s) failed", "split_state.axis >= 0 && split_state.axis < GGML_MAX_DIMS"
)
;
1254 GGML_ASSERT(split_state.nr[0] != 0)if (!(split_state.nr[0] != 0)) ggml_abort("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 1254, "GGML_ASSERT(%s) failed", "split_state.nr[0] != 0")
;
1255 GGML_ASSERT(tensor->ne[3] == 1)if (!(tensor->ne[3] == 1)) ggml_abort("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 1255, "GGML_ASSERT(%s) failed", "tensor->ne[3] == 1")
;
1256
1257 size_t offset_data = 0;
1258 std::vector<size_t> simple_offsets(n_bufs, 0);
1259 if (split_state.axis == GGML_BACKEND_SPLIT_AXIS_0) {
1260 GGML_ASSERT(tensor->ne[2] == 1)if (!(tensor->ne[2] == 1)) ggml_abort("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 1260, "GGML_ASSERT(%s) failed", "tensor->ne[2] == 1")
;
1261
1262 const size_t row_stride = tensor->nb[1];
1263 GGML_ASSERT(offset % row_stride == 0)if (!(offset % row_stride == 0)) ggml_abort("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 1263, "GGML_ASSERT(%s) failed", "offset % row_stride == 0")
;
1264 GGML_ASSERT(size % row_stride == 0)if (!(size % row_stride == 0)) ggml_abort("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 1264, "GGML_ASSERT(%s) failed", "size % row_stride == 0")
;
1265 const int64_t row_start = offset / row_stride;
1266 const int64_t row_count = size / row_stride;
1267 GGML_ASSERT(row_start + row_count <= tensor->ne[1])if (!(row_start + row_count <= tensor->ne[1])) ggml_abort
("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 1267, "GGML_ASSERT(%s) failed", "row_start + row_count <= tensor->ne[1]"
)
;
1268
1269 const int64_t blck_size = ggml_blck_size(tensor->type);
1270 for (size_t s = 0; s < split_state.n_segments; s++) {
1271 for (size_t r = 0; r < split_state.nr[s]; r++) {
1272 for (size_t j = 0; j < n_bufs; j++) {
1273 ggml_tensor * simple_tensor = ggml_backend_meta_buffer_simple_tensor(tensor, j);
1274 GGML_ASSERT(split_state.ne[s*n_bufs + j] % blck_size == 0)if (!(split_state.ne[s*n_bufs + j] % blck_size == 0)) ggml_abort
("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 1274, "GGML_ASSERT(%s) failed", "split_state.ne[s*n_bufs + j] % blck_size == 0"
)
;
1275 const size_t nbytes = split_state.ne[s*n_bufs + j]/blck_size * tensor->nb[0];
1276 ggml_backend_tensor_set_2d(simple_tensor, (const char *) data + offset_data,
1277 simple_offsets[j] + row_start * simple_tensor->nb[1], nbytes,
1278 row_count, simple_tensor->nb[1], tensor->nb[1]);
1279 offset_data += nbytes;
1280 simple_offsets[j] += nbytes;
1281 }
1282 }
1283 }
1284 GGML_ASSERT(offset_data*row_count == size)if (!(offset_data*row_count == size)) ggml_abort("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 1284, "GGML_ASSERT(%s) failed", "offset_data*row_count == size"
)
;
1285 return;
1286 }
1287 GGML_ASSERT(split_state.axis == GGML_BACKEND_SPLIT_AXIS_1)if (!(split_state.axis == GGML_BACKEND_SPLIT_AXIS_1)) ggml_abort
("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 1287, "GGML_ASSERT(%s) failed", "split_state.axis == GGML_BACKEND_SPLIT_AXIS_1"
)
;
1288
1289 const size_t row_stride = tensor->nb[2];
1290 GGML_ASSERT(offset % row_stride == 0)if (!(offset % row_stride == 0)) ggml_abort("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 1290, "GGML_ASSERT(%s) failed", "offset % row_stride == 0")
;
1291 GGML_ASSERT(size % row_stride == 0)if (!(size % row_stride == 0)) ggml_abort("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 1291, "GGML_ASSERT(%s) failed", "size % row_stride == 0")
;
1292 const int64_t row_start = offset / row_stride;
1293 const int64_t row_count = size / row_stride;
1294 GGML_ASSERT(row_start + row_count <= tensor->ne[2])if (!(row_start + row_count <= tensor->ne[2])) ggml_abort
("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 1294, "GGML_ASSERT(%s) failed", "row_start + row_count <= tensor->ne[2]"
)
;
1295
1296 for (size_t s = 0; s < split_state.n_segments; s++) {
1297 for (size_t r = 0; r < split_state.nr[s]; r++) {
1298 for (size_t j = 0; j < n_bufs; j++) {
1299 ggml_tensor * simple_tensor = ggml_backend_meta_buffer_simple_tensor(tensor, j);
1300 const size_t nbytes = split_state.ne[s*n_bufs + j] * tensor->nb[1];
1301 ggml_backend_tensor_set_2d(simple_tensor, (const char *) data + offset_data,
1302 simple_offsets[j] + row_start * simple_tensor->nb[2], nbytes,
1303 row_count, simple_tensor->nb[2], tensor->nb[2]);
1304 offset_data += nbytes;
1305 simple_offsets[j] += nbytes;
1306 }
1307 }
1308 }
1309 GGML_ASSERT(offset_data*row_count == size)if (!(offset_data*row_count == size)) ggml_abort("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 1309, "GGML_ASSERT(%s) failed", "offset_data*row_count == size"
)
;
1310 return;
1311 }
1312
1313 switch (split_state.axis) {
1314 case GGML_BACKEND_SPLIT_AXIS_0:
1315 case GGML_BACKEND_SPLIT_AXIS_1:
1316 case GGML_BACKEND_SPLIT_AXIS_2: {
1317 // Exploit that tensors are contiguous to splice it with simple tensors as "chunks".
1318 const size_t chunk_size_full = tensor->nb[split_state.axis + 1];
1319 GGML_ASSERT(offset % chunk_size_full == 0)if (!(offset % chunk_size_full == 0)) ggml_abort("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 1319, "GGML_ASSERT(%s) failed", "offset % chunk_size_full == 0"
)
;
1320 GGML_ASSERT(size % chunk_size_full == 0)if (!(size % chunk_size_full == 0)) ggml_abort("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 1320, "GGML_ASSERT(%s) failed", "size % chunk_size_full == 0"
)
;
1321 const int64_t i_start = offset /chunk_size_full;
1322 const int64_t i_stop = (offset + size)/chunk_size_full;
1323 size_t offset_j = 0;
1324 for (size_t j = 0; j < n_bufs; j++) {
1325 ggml_tensor * simple_tensor = ggml_backend_meta_buffer_simple_tensor(tensor, j);
1326 const size_t chunk_size_j = simple_tensor->nb[split_state.axis + 1];
1327 if (chunk_size_j == 0) {
1328 continue;
1329 }
1330 const size_t simple_offset = i_start * chunk_size_j;
1331 ggml_backend_tensor_set_2d(simple_tensor, (const char *) data + offset_j, simple_offset, chunk_size_j, i_stop - i_start, chunk_size_j, chunk_size_full);
1332 offset_j += chunk_size_j;
1333 }
1334 GGML_ASSERT(offset_j == chunk_size_full)if (!(offset_j == chunk_size_full)) ggml_abort("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 1334, "GGML_ASSERT(%s) failed", "offset_j == chunk_size_full"
)
;
1335 } break;
1336 case GGML_BACKEND_SPLIT_AXIS_MIRRORED: {
1337 for (size_t j = 0; j < n_bufs; j++) {
1338 ggml_tensor * simple_tensor = ggml_backend_meta_buffer_simple_tensor(tensor, j);
1339 ggml_backend_tensor_set(simple_tensor, data, offset, size);
1340 }
1341 } break;
1342 case GGML_BACKEND_SPLIT_AXIS_PARTIAL: {
1343 GGML_ASSERT(tensor->type == GGML_TYPE_F32)if (!(tensor->type == GGML_TYPE_F32)) ggml_abort("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 1343, "GGML_ASSERT(%s) failed", "tensor->type == GGML_TYPE_F32"
)
;
1344 const int64_t ne = ggml_nelements(tensor);
1345 std::vector<float> tmp;
1346 tmp.reserve(ne);
1347 for (int64_t i = 0; i < ne; i++) {
1348 tmp.push_back(((const float *) data)[i] / n_bufs);
1349 }
1350 for (size_t j = 0; j < n_bufs; j++) {
1351 ggml_tensor * simple_tensor = ggml_backend_meta_buffer_simple_tensor(tensor, j);
1352 ggml_backend_tensor_set(simple_tensor, tmp.data(), offset, size);
1353 }
1354 } break;
1355 default: {
1356 GGML_ABORT("fatal error")ggml_abort("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 1356, "fatal error")
;
1357 }
1358 }
1359}
1360
1361static void ggml_backend_meta_buffer_get_tensor(ggml_backend_buffer_t buffer, const ggml_tensor * tensor, void * data, size_t offset, size_t size) {
1362 const size_t n_bufs = ggml_backend_meta_buffer_n_bufs(buffer);
1363 GGML_ASSERT(ggml_is_contiguous(tensor))if (!(ggml_is_contiguous(tensor))) ggml_abort("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 1363, "GGML_ASSERT(%s) failed", "ggml_is_contiguous(tensor)"
)
;
1364
1365 const ggml_backend_meta_split_state split_state = ggml_backend_meta_get_split_state(tensor, /*assume_sync =*/ false);
1366
1367 if (split_state.n_segments != 1 || split_state.nr[0] != 1) {
1368 GGML_ASSERT(split_state.axis >= 0 && split_state.axis < GGML_MAX_DIMS)if (!(split_state.axis >= 0 && split_state.axis <
4)) ggml_abort("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 1368, "GGML_ASSERT(%s) failed", "split_state.axis >= 0 && split_state.axis < GGML_MAX_DIMS"
)
;
1369 GGML_ASSERT(split_state.nr[0] != 0)if (!(split_state.nr[0] != 0)) ggml_abort("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 1369, "GGML_ASSERT(%s) failed", "split_state.nr[0] != 0")
;
1370 GGML_ASSERT(tensor->ne[3] == 1)if (!(tensor->ne[3] == 1)) ggml_abort("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 1370, "GGML_ASSERT(%s) failed", "tensor->ne[3] == 1")
;
1371
1372 size_t offset_data = 0;
1373 std::vector<size_t> simple_offsets(n_bufs, 0);
1374 if (split_state.axis == GGML_BACKEND_SPLIT_AXIS_0) {
1375 GGML_ASSERT(tensor->ne[2] == 1)if (!(tensor->ne[2] == 1)) ggml_abort("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 1375, "GGML_ASSERT(%s) failed", "tensor->ne[2] == 1")
;
1376
1377 const size_t row_stride = tensor->nb[1];
1378 GGML_ASSERT(offset % row_stride == 0)if (!(offset % row_stride == 0)) ggml_abort("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 1378, "GGML_ASSERT(%s) failed", "offset % row_stride == 0")
;
1379 GGML_ASSERT(size % row_stride == 0)if (!(size % row_stride == 0)) ggml_abort("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 1379, "GGML_ASSERT(%s) failed", "size % row_stride == 0")
;
1380 const int64_t row_start = offset / row_stride;
1381 const int64_t row_count = size / row_stride;
1382 GGML_ASSERT(row_start + row_count <= tensor->ne[1])if (!(row_start + row_count <= tensor->ne[1])) ggml_abort
("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 1382, "GGML_ASSERT(%s) failed", "row_start + row_count <= tensor->ne[1]"
)
;
1383
1384 const int64_t blck_size = ggml_blck_size(tensor->type);
1385 for (size_t s = 0; s < split_state.n_segments; s++) {
1386 for (size_t r = 0; r < split_state.nr[s]; r++) {
1387 for (size_t j = 0; j < n_bufs; j++) {
1388 const ggml_tensor * simple_tensor = ggml_backend_meta_buffer_simple_tensor(tensor, j);
1389 GGML_ASSERT(split_state.ne[s*n_bufs + j] % blck_size == 0)if (!(split_state.ne[s*n_bufs + j] % blck_size == 0)) ggml_abort
("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 1389, "GGML_ASSERT(%s) failed", "split_state.ne[s*n_bufs + j] % blck_size == 0"
)
;
1390 const size_t nbytes = split_state.ne[s*n_bufs + j]/blck_size * tensor->nb[0];
1391 ggml_backend_tensor_get_2d(simple_tensor, (char *) data + offset_data,
1392 simple_offsets[j] + row_start * simple_tensor->nb[1], nbytes,
1393 row_count, simple_tensor->nb[1], tensor->nb[1]);
1394 offset_data += nbytes;
1395 simple_offsets[j] += nbytes;
1396 }
1397 }
1398 }
1399 GGML_ASSERT(offset_data*row_count == size)if (!(offset_data*row_count == size)) ggml_abort("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 1399, "GGML_ASSERT(%s) failed", "offset_data*row_count == size"
)
;
1400 return;
1401 }
1402 GGML_ASSERT(split_state.axis == GGML_BACKEND_SPLIT_AXIS_1)if (!(split_state.axis == GGML_BACKEND_SPLIT_AXIS_1)) ggml_abort
("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 1402, "GGML_ASSERT(%s) failed", "split_state.axis == GGML_BACKEND_SPLIT_AXIS_1"
)
;
1403
1404 const size_t row_stride = tensor->nb[2];
1405 GGML_ASSERT(offset % row_stride == 0)if (!(offset % row_stride == 0)) ggml_abort("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 1405, "GGML_ASSERT(%s) failed", "offset % row_stride == 0")
;
1406 GGML_ASSERT(size % row_stride == 0)if (!(size % row_stride == 0)) ggml_abort("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 1406, "GGML_ASSERT(%s) failed", "size % row_stride == 0")
;
1407 const int64_t row_start = offset / row_stride;
1408 const int64_t row_count = size / row_stride;
1409 GGML_ASSERT(row_start + row_count <= tensor->ne[2])if (!(row_start + row_count <= tensor->ne[2])) ggml_abort
("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 1409, "GGML_ASSERT(%s) failed", "row_start + row_count <= tensor->ne[2]"
)
;
1410
1411 for (size_t s = 0; s < split_state.n_segments; s++) {
1412 for (size_t r = 0; r < split_state.nr[s]; r++) {
1413 for (size_t j = 0; j < n_bufs; j++) {
1414 const ggml_tensor * simple_tensor = ggml_backend_meta_buffer_simple_tensor(tensor, j);
1415 const size_t nbytes = split_state.ne[s*n_bufs + j] * tensor->nb[1];
1416 ggml_backend_tensor_get_2d(simple_tensor, (char *) data + offset_data,
1417 simple_offsets[j] + row_start * simple_tensor->nb[2], nbytes,
1418 row_count, simple_tensor->nb[2], tensor->nb[2]);
1419 offset_data += nbytes;
1420 simple_offsets[j] += nbytes;
1421 }
1422 }
1423 }
1424 GGML_ASSERT(offset_data*row_count == size)if (!(offset_data*row_count == size)) ggml_abort("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 1424, "GGML_ASSERT(%s) failed", "offset_data*row_count == size"
)
;
1425 return;
1426 }
1427
1428 switch (split_state.axis) {
1429 case GGML_BACKEND_SPLIT_AXIS_0:
1430 case GGML_BACKEND_SPLIT_AXIS_1:
1431 case GGML_BACKEND_SPLIT_AXIS_2: {
1432 // Exploit that tensors are contiguous to splice it with simple tensors as "chunks".
1433 const size_t chunk_size_full = tensor->nb[split_state.axis + 1];
1434 GGML_ASSERT(offset % chunk_size_full == 0)if (!(offset % chunk_size_full == 0)) ggml_abort("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 1434, "GGML_ASSERT(%s) failed", "offset % chunk_size_full == 0"
)
;
1435 GGML_ASSERT(size % chunk_size_full == 0)if (!(size % chunk_size_full == 0)) ggml_abort("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 1435, "GGML_ASSERT(%s) failed", "size % chunk_size_full == 0"
)
;
1436 const int64_t i_start = offset /chunk_size_full;
1437 const int64_t i_stop = (offset + size)/chunk_size_full;
1438 size_t offset_j = 0;
1439 for (size_t j = 0; j < n_bufs; j++){
1440 const ggml_tensor * simple_tensor = ggml_backend_meta_buffer_simple_tensor(tensor, j);
1441 const size_t chunk_size_j = simple_tensor->nb[split_state.axis + 1];
1442 if (chunk_size_j == 0) {
1443 continue;
1444 }
1445 const size_t simple_offset = i_start * chunk_size_j;
1446 ggml_backend_tensor_get_2d(simple_tensor, (char *) data + offset_j, simple_offset, chunk_size_j, i_stop - i_start, chunk_size_j, chunk_size_full);
1447 offset_j += chunk_size_j;
1448 }
1449 GGML_ASSERT(offset_j == chunk_size_full)if (!(offset_j == chunk_size_full)) ggml_abort("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 1449, "GGML_ASSERT(%s) failed", "offset_j == chunk_size_full"
)
;
1450 } break;
1451 case GGML_BACKEND_SPLIT_AXIS_MIRRORED: {
1452 // TODO other simple backend may be better
1453 const ggml_tensor * simple_tensor = ggml_backend_meta_buffer_simple_tensor(tensor, 0);
1454 ggml_backend_tensor_get(simple_tensor, data, offset, size);
1455 } break;
1456 default: {
1457 GGML_ABORT("fatal error")ggml_abort("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 1457, "fatal error")
;
1458 }
1459 }
1460}
1461
1462static void ggml_backend_meta_buffer_clear(ggml_backend_buffer_t buffer, uint8_t value) {
1463 const size_t n_buffers = ggml_backend_meta_buffer_n_bufs(buffer);
1464 for (size_t i = 0; i < n_buffers; i++) {
1465 ggml_backend_buffer_clear(ggml_backend_meta_buffer_simple_buffer(buffer, i), value);
1466 }
1467}
1468
1469static void ggml_backend_meta_buffer_reset(ggml_backend_buffer_t buffer) {
1470 GGML_ASSERT(ggml_backend_buffer_is_meta(buffer))if (!(ggml_backend_buffer_is_meta(buffer))) ggml_abort("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 1470, "GGML_ASSERT(%s) failed", "ggml_backend_buffer_is_meta(buffer)"
)
;
1471 ggml_backend_meta_buffer_context * buf_ctx = (ggml_backend_meta_buffer_context *) buffer->context;
1472 for (size_t i = 0; i < buf_ctx->bufs.size(); i++) {
1473 ggml_backend_buffer_reset(ggml_backend_meta_buffer_simple_buffer(buffer, i));
1474 }
1475}
1476
1477static const ggml_backend_buffer_i ggml_backend_meta_buffer_iface = {
1478 /* .free_buffer = */ ggml_backend_meta_buffer_free_buffer,
1479 /* .get_base = */ ggml_backend_meta_buffer_get_base,
1480 /* .init_tensor = */ ggml_backend_meta_buffer_init_tensor,
1481 /* .memset_tensor = */ nullptr, // TODO implement
1482 /* .set_tensor = */ ggml_backend_meta_buffer_set_tensor,
1483 /* .get_tensor = */ ggml_backend_meta_buffer_get_tensor,
1484 /* .set_tensor_2d = */ nullptr,
1485 /* .get_tensor_2d = */ nullptr,
1486 /* .cpy_tensor = */ nullptr,
1487 /* .clear = */ ggml_backend_meta_buffer_clear,
1488 /* .reset = */ ggml_backend_meta_buffer_reset,
1489};
1490
1491bool ggml_backend_buffer_is_meta(ggml_backend_buffer_t buf) {
1492 return buf != nullptr && buf->iface.free_buffer == ggml_backend_meta_buffer_iface.free_buffer;
1493}
1494
1495static ggml_backend_buffer_t ggml_backend_meta_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) {
1496 const size_t n_simple_bufts = ggml_backend_meta_buft_n_bufts(buft);
1497
1498 const ggml_init_params params = {
1499 /*.mem_size =*/ 1024*1024*ggml_tensor_overhead(), // FIXME
1500 /*.mem_buffer =*/ nullptr,
1501 /*.no_alloc =*/ true,
1502 };
1503 ggml_backend_meta_simple_tensor_container stc_static;
1504 ggml_backend_meta_simple_tensor_container stc_compute_0(params, n_simple_bufts);
1505 ggml_backend_meta_simple_tensor_container stc_compute_1(params, n_simple_bufts);
1506
1507 size_t max_size = 0;
1508 std::vector<ggml_backend_buffer_t> bufs;
1509 bufs.reserve(n_simple_bufts);
1510 for (size_t i = 0; i < n_simple_bufts; i++) {
1511 bufs.push_back(ggml_backend_buft_alloc_buffer(ggml_backend_meta_buft_simple_buft(buft, i), size));
1512 GGML_ASSERT(bufs.back() != nullptr)if (!(bufs.back() != nullptr)) ggml_abort("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 1512, "GGML_ASSERT(%s) failed", "bufs.back() != nullptr")
;
1513 max_size = std::max(max_size, ggml_backend_buffer_get_size(bufs.back()));
1514 }
1515 ggml_backend_meta_buffer_context * buf_ctx = new ggml_backend_meta_buffer_context(stc_static, stc_compute_0, stc_compute_1, bufs);
1516
1517 return ggml_backend_buffer_init(buft, ggml_backend_meta_buffer_iface, buf_ctx, max_size);
1518}
1519
1520struct ggml_backend_buffer * ggml_backend_meta_alloc_ctx_tensors_from_buft(struct ggml_context * ctx, ggml_backend_buffer_type_t buft) {
1521 const size_t n_simple_bufts = ggml_backend_meta_buft_n_bufts(buft);
1522
1523 constexpr size_t compute_headroom = 16; // Maximum number of views per statically allocated tensor that can be created between evals.
1524 const ggml_init_params params_static = {
1525 /*.mem_size =*/ ggml_get_mem_size(ctx),
1526 /*.mem_buffer =*/ nullptr,
1527 /*.no_alloc =*/ true,
1528 };
1529 const ggml_init_params params_compute = {
1530 /*.mem_size =*/ compute_headroom*ggml_get_mem_size(ctx),
1531 /*.mem_buffer =*/ nullptr,
1532 /*.no_alloc =*/ true,
1533 };
1534 ggml_backend_meta_simple_tensor_container stc_static (params_static, n_simple_bufts);
1535 ggml_backend_meta_simple_tensor_container stc_compute_0(params_compute, n_simple_bufts);
1536 ggml_backend_meta_simple_tensor_container stc_compute_1(params_compute, n_simple_bufts);
1537
1538 std::vector<ggml_backend_buffer_t> bufs(n_simple_bufts, nullptr);
1539 ggml_backend_meta_buffer_context * meta_buf_ctx = new ggml_backend_meta_buffer_context(stc_static, stc_compute_0, stc_compute_1, bufs);
1540
1541 ggml_backend_buffer_t meta_buf = ggml_backend_buffer_init(buft, ggml_backend_meta_buffer_iface, meta_buf_ctx, 0);
1542 for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != nullptr; t = ggml_get_next_tensor(ctx, t)) {
1543 t->buffer = meta_buf;
1544 ggml_backend_meta_buffer_init_tensor_impl(meta_buf_ctx->stc_static, t);
1545 t->data = (void *) 0x2000000000000000; // FIXME
1546 }
1547 for (size_t i = 0; i < n_simple_bufts; i++) {
1548 ggml_context * ctx = meta_buf_ctx->stc_static.ctxs[i].get();
1549 ggml_backend_buffer_type_t simple_buft = ggml_backend_meta_buft_simple_buft(buft, i);
1550
1551 // If a ggml_context only has zero-sized tensors, ggml_backend_alloc_ctx_tensors_from_buft returns NULL.
1552 // For those edge cases, allocate a dummy buffer instead.
1553 bool any_nonzero_slice = false;
1554 for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != nullptr; t = ggml_get_next_tensor(ctx, t)) {
1555 if (ggml_nelements(t) != 0) {
1556 any_nonzero_slice = true;
1557 break;
1558 }
1559 }
1560 if (any_nonzero_slice) {
1561 meta_buf_ctx->bufs[i].reset(ggml_backend_alloc_ctx_tensors_from_buft(ctx, simple_buft));
1562 } else {
1563 meta_buf_ctx->bufs[i].reset(ggml_backend_buft_alloc_buffer(simple_buft, 0));
1564 for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != nullptr; t = ggml_get_next_tensor(ctx, t)) {
1565 t->buffer = meta_buf_ctx->bufs[i].get();
1566 }
1567 }
1568 GGML_ASSERT(meta_buf_ctx->bufs[i])if (!(meta_buf_ctx->bufs[i])) ggml_abort("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 1568, "GGML_ASSERT(%s) failed", "meta_buf_ctx->bufs[i]")
;
1569 meta_buf->size = std::max(meta_buf->size, ggml_backend_buffer_get_size(meta_buf_ctx->bufs[i].get()));
1570 }
1571 return meta_buf;
1572}
1573
1574//
1575// meta backend
1576//
1577
1578static ggml_guid_t ggml_backend_meta_guid() {
1579 static ggml_guid guid = {0xf1, 0x0e, 0x34, 0xcf, 0x9c, 0x6f, 0x43, 0xcb, 0x96, 0x92, 0xbe, 0x8e, 0xbb, 0x71, 0x3f, 0xda};
1580 return &guid;
1581}
1582
1583struct ggml_backend_meta_context {
1584 struct cgraph_config {
1585 ggml_cgraph * cgraph_main = nullptr;
1586 int offset = 0; // Node offset vs. original graph
1587
1588 std::vector<ggml_cgraph *> cgraphs_aux;
1589 };
1590 struct backend_config {
1591 ggml_backend_t backend;
1592
1593 std::vector<cgraph_config> cgraphs;
1594 std::vector<ggml_tensor *> nodes;
1595 std::vector<ggml_backend_buffer_ptr> bufs;
1596
1597 backend_config(ggml_backend_t backend, const size_t n_reduce_steps) : backend(backend) {
1598 bufs.resize(n_reduce_steps);
1599 }
1600 };
1601 std::string name;
1602 std::vector<backend_config> backend_configs;
1603 ggml_context_ptr ctx;
1604 std::vector<ggml_cgraph *> cgraphs_aux;
1605 std::vector<ggml_tensor *> nodes_aux;
1606 size_t n_reduce_steps;
1607 int max_nnodes = 0;
1608 size_t max_tmp_size = 0;
1609 size_t max_subgraphs = 0;
1610 size_t n_subgraphs = 0;
1611 uint64_t uid = 0;
1612
1613 void * comm_ctx = nullptr;
1614 ggml_backend_comm_allreduce_tensor_t comm_allreduce = nullptr;
1615
1616 ggml_backend_meta_context(ggml_backend_dev_t meta_dev, const char * params) {
1617 const size_t n_devs = ggml_backend_meta_dev_n_devs(meta_dev);
1618 n_reduce_steps = std::ceil(std::log2(n_devs));
1619 name = "Meta(";
1620 std::vector<ggml_backend_t> simple_backends;
1621 backend_configs.reserve(n_devs);
1622 simple_backends.reserve(n_devs);
1623 for (size_t i = 0; i < n_devs; i++) {
1624 ggml_backend_dev_t simple_dev = ggml_backend_meta_dev_simple_dev(meta_dev, i);
1625 if (i > 0) {
1626 name += ",";
1627 }
1628 name += ggml_backend_dev_name(simple_dev);
1629 simple_backends.push_back(ggml_backend_dev_init(simple_dev, params));
1630 backend_configs.emplace_back(simple_backends.back(), n_reduce_steps);
1631 }
1632 name += ")";
1633
1634 if (n_devs > 1) {
1635 ggml_backend_comm_init_t comm_init = (ggml_backend_comm_init_t) ggml_backend_reg_get_proc_address(
1636 ggml_backend_dev_backend_reg(ggml_backend_get_device(simple_backends[0])), "ggml_backend_comm_init");
1637 if (comm_init != nullptr) {
1638 comm_ctx = comm_init(simple_backends.data(), simple_backends.size());
1639 }
1640 }
1641 if (comm_ctx != nullptr) {
1642 comm_allreduce = (ggml_backend_comm_allreduce_tensor_t)
1643 ggml_backend_reg_get_proc_address(ggml_backend_dev_backend_reg(
1644 ggml_backend_get_device(simple_backends[0])), "ggml_backend_comm_allreduce_tensor");
1645 GGML_ASSERT(comm_allreduce != nullptr)if (!(comm_allreduce != nullptr)) ggml_abort("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 1645, "GGML_ASSERT(%s) failed", "comm_allreduce != nullptr"
)
;
1646 }
1647 }
1648
1649 ~ggml_backend_meta_context() {
1650 if (comm_ctx != nullptr) {
1651 ggml_backend_comm_free_t comm_free = (ggml_backend_comm_free_t) ggml_backend_reg_get_proc_address(
1652 ggml_backend_dev_backend_reg(ggml_backend_get_device(backend_configs[0].backend)), "ggml_backend_comm_free");
1653 GGML_ASSERT(comm_free != nullptr)if (!(comm_free != nullptr)) ggml_abort("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 1653, "GGML_ASSERT(%s) failed", "comm_free != nullptr")
;
1654 comm_free(comm_ctx);
1655 }
1656 for (auto & bc : backend_configs) {
1657 ggml_backend_free(bc.backend);
1658 }
1659 }
1660};
1661
1662static const char * ggml_backend_meta_get_name(ggml_backend_t backend) {
1663 GGML_ASSERT(ggml_backend_is_meta(backend))if (!(ggml_backend_is_meta(backend))) ggml_abort("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 1663, "GGML_ASSERT(%s) failed", "ggml_backend_is_meta(backend)"
)
;
1664 const ggml_backend_meta_context * backend_ctx = (const ggml_backend_meta_context *) backend->context;
1665 return backend_ctx->name.c_str();
1666}
1667
1668static void ggml_backend_meta_free(ggml_backend_t backend) {
1669 GGML_ASSERT(ggml_backend_is_meta(backend))if (!(ggml_backend_is_meta(backend))) ggml_abort("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 1669, "GGML_ASSERT(%s) failed", "ggml_backend_is_meta(backend)"
)
;
1670 ggml_backend_meta_context * backend_ctx = (ggml_backend_meta_context *) backend->context;
1671 delete backend_ctx;
1672 delete backend;
1673}
1674
1675static void ggml_backend_meta_set_tensor_async(ggml_backend_t backend, ggml_tensor * tensor, const void * data, size_t offset, size_t size) {
1676 const size_t n_backends = ggml_backend_meta_n_backends(backend);
1677 GGML_ASSERT(offset == 0)if (!(offset == 0)) ggml_abort("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 1677, "GGML_ASSERT(%s) failed", "offset == 0")
;
1678 GGML_ASSERT(ggml_is_contiguous(tensor))if (!(ggml_is_contiguous(tensor))) ggml_abort("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 1678, "GGML_ASSERT(%s) failed", "ggml_is_contiguous(tensor)"
)
;
1679
1680 const ggml_backend_meta_split_state split_state = ggml_backend_meta_get_split_state(tensor, /*assume_sync =*/ false);
1681 GGML_ASSERT(split_state.n_segments == 1)if (!(split_state.n_segments == 1)) ggml_abort("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 1681, "GGML_ASSERT(%s) failed", "split_state.n_segments == 1"
)
;
1682 GGML_ASSERT(split_state.nr[0] == 1)if (!(split_state.nr[0] == 1)) ggml_abort("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 1682, "GGML_ASSERT(%s) failed", "split_state.nr[0] == 1")
;
1683
1684 switch (split_state.axis) {
1685 case GGML_BACKEND_SPLIT_AXIS_0:
1686 case GGML_BACKEND_SPLIT_AXIS_1:
1687 case GGML_BACKEND_SPLIT_AXIS_2: {
1688 // Exploit that tensors are contiguous to splice it with simple tensors as "chunks".
1689 const size_t chunk_size_full = tensor->nb[split_state.axis + 1];
1690 GGML_ASSERT(offset % chunk_size_full == 0)if (!(offset % chunk_size_full == 0)) ggml_abort("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 1690, "GGML_ASSERT(%s) failed", "offset % chunk_size_full == 0"
)
;
1691 GGML_ASSERT(size % chunk_size_full == 0)if (!(size % chunk_size_full == 0)) ggml_abort("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 1691, "GGML_ASSERT(%s) failed", "size % chunk_size_full == 0"
)
;
1692 const int64_t i_start = offset /chunk_size_full;
1693 const int64_t i_stop = (offset + size)/chunk_size_full;
1694 size_t offset_j = 0;
1695 for (size_t j = 0; j < n_backends; j++){
1696 ggml_backend_t simple_backend = ggml_backend_meta_simple_backend(backend, j);
1697 ggml_tensor * simple_tensor = ggml_backend_meta_buffer_simple_tensor(tensor, j);
1698 const size_t chunk_size_j = simple_tensor->nb[split_state.axis + 1];
1699 if (chunk_size_j == 0) {
1700 continue;
1701 }
1702 ggml_backend_tensor_set_2d_async(simple_backend, simple_tensor, (const char *) data + offset_j, offset, chunk_size_j,
1703 i_stop - i_start, chunk_size_j, chunk_size_full);
1704 offset_j += chunk_size_j;
1705 }
1706 GGML_ASSERT(offset_j == chunk_size_full)if (!(offset_j == chunk_size_full)) ggml_abort("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 1706, "GGML_ASSERT(%s) failed", "offset_j == chunk_size_full"
)
;
1707 } break;
1708 case GGML_BACKEND_SPLIT_AXIS_MIRRORED: {
1709 for (size_t j = 0; j < n_backends; j++) {
1710 ggml_backend_tensor_set_async(
1711 ggml_backend_meta_simple_backend(backend, j), ggml_backend_meta_buffer_simple_tensor(tensor, j), data, offset, size);
1712 }
1713 } break;
1714 default: {
1715 GGML_ABORT("fatal error")ggml_abort("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 1715, "fatal error")
;
1716 }
1717 }
1718}
1719
1720static void ggml_backend_meta_get_tensor_async(ggml_backend_t backend, const ggml_tensor * tensor, void * data, size_t offset, size_t size) {
1721 const size_t n_backends = ggml_backend_meta_n_backends(backend);
1722 GGML_ASSERT(offset == 0)if (!(offset == 0)) ggml_abort("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 1722, "GGML_ASSERT(%s) failed", "offset == 0")
;
1723 GGML_ASSERT(ggml_is_contiguous(tensor))if (!(ggml_is_contiguous(tensor))) ggml_abort("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 1723, "GGML_ASSERT(%s) failed", "ggml_is_contiguous(tensor)"
)
;
1724
1725 const ggml_backend_meta_split_state split_state = ggml_backend_meta_get_split_state(tensor, /*assume_sync =*/ false);
1726 GGML_ASSERT(split_state.n_segments == 1)if (!(split_state.n_segments == 1)) ggml_abort("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 1726, "GGML_ASSERT(%s) failed", "split_state.n_segments == 1"
)
;
1727 GGML_ASSERT(split_state.nr[0] == 1)if (!(split_state.nr[0] == 1)) ggml_abort("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 1727, "GGML_ASSERT(%s) failed", "split_state.nr[0] == 1")
;
1728
1729 switch (split_state.axis) {
1730 case GGML_BACKEND_SPLIT_AXIS_0:
1731 case GGML_BACKEND_SPLIT_AXIS_1:
1732 case GGML_BACKEND_SPLIT_AXIS_2: {
1733 // Exploit that tensors are contiguous to splice it with simple tensors as "chunks".
1734 const size_t chunk_size_full = tensor->nb[split_state.axis + 1];
1735 GGML_ASSERT(offset % chunk_size_full == 0)if (!(offset % chunk_size_full == 0)) ggml_abort("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 1735, "GGML_ASSERT(%s) failed", "offset % chunk_size_full == 0"
)
;
1736 GGML_ASSERT(size % chunk_size_full == 0)if (!(size % chunk_size_full == 0)) ggml_abort("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 1736, "GGML_ASSERT(%s) failed", "size % chunk_size_full == 0"
)
;
1737 const int64_t i_start = offset /chunk_size_full;
1738 const int64_t i_stop = (offset + size)/chunk_size_full;
1739 size_t offset_j = 0;
1740 for (size_t j = 0; j < n_backends; j++){
1741 ggml_backend_t simple_backend = ggml_backend_meta_simple_backend(backend, j);
1742 const ggml_tensor * simple_tensor = ggml_backend_meta_buffer_simple_tensor(tensor, j);
1743 const size_t chunk_size_j = simple_tensor->nb[split_state.axis + 1];
1744 if (chunk_size_j == 0) {
1745 continue;
1746 }
1747 ggml_backend_tensor_get_2d_async(simple_backend, simple_tensor, (char *) data + offset_j, offset, chunk_size_j,
1748 i_stop - i_start, chunk_size_j, chunk_size_full);
1749 offset_j += chunk_size_j;
1750 }
1751 GGML_ASSERT(offset_j == chunk_size_full)if (!(offset_j == chunk_size_full)) ggml_abort("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 1751, "GGML_ASSERT(%s) failed", "offset_j == chunk_size_full"
)
;
1752 } break;
1753 case GGML_BACKEND_SPLIT_AXIS_MIRRORED: {
1754 // TODO other simple backend may be better
1755 ggml_backend_t simple_backend = ggml_backend_meta_simple_backend(backend, 0);
1756 const ggml_tensor * simple_tensor = ggml_backend_meta_buffer_simple_tensor(tensor, 0);
1757 ggml_backend_tensor_get_async(simple_backend, simple_tensor, data, offset, size);
1758 } break;
1759 default: {
1760 GGML_ABORT("fatal error")ggml_abort("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 1760, "fatal error")
;
1761 }
1762 }
1763}
1764
1765static void ggml_backend_meta_synchronize(ggml_backend_t backend) {
1766 const size_t n_backends = ggml_backend_meta_n_backends(backend);
1767 for (size_t i = 0; i < n_backends; i++) {
1768 ggml_backend_synchronize(ggml_backend_meta_simple_backend(backend, i));
1769 }
1770}
1771
1772static enum ggml_status ggml_backend_meta_graph_compute(ggml_backend_t backend, struct ggml_cgraph * cgraph) {
1773 GGML_ASSERT(cgraph->grads == nullptr)if (!(cgraph->grads == nullptr)) ggml_abort("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 1773, "GGML_ASSERT(%s) failed", "cgraph->grads == nullptr"
)
;
1774 const size_t n_backends = ggml_backend_meta_n_backends(backend);
1775 ggml_backend_meta_context * backend_ctx = (ggml_backend_meta_context *) backend->context;
1776
1777 // If the previous cgraph had a defined UID it can be used to skip rebuilding the subgraphs per simple backend.
1778 const bool needs_rebuild = (cgraph->uid == 0) || (cgraph->uid != backend_ctx->uid);
1779
1780 bool max_nnodes_raised = false;
1781 if (cgraph->n_nodes > backend_ctx->max_nnodes) {
1782 for (size_t j = 0; j < n_backends; j++) {
1783 auto & bcj = backend_ctx->backend_configs[j];
1784 bcj.nodes.resize(cgraph->n_nodes);
1785 bcj.cgraphs.resize(cgraph->n_nodes);
1786 }
1787 backend_ctx->max_nnodes = cgraph->n_nodes;
1788 max_nnodes_raised = true;
1789 assert(needs_rebuild)(static_cast <bool> (needs_rebuild) ? void (0) : __assert_fail
("needs_rebuild", __builtin_FILE (), __builtin_LINE (), __extension__
__PRETTY_FUNCTION__))
;
1790 }
1791
1792 if (needs_rebuild) {
1793 std::set<ggml_backend_buffer_t> used_buffers;
1794 for (int i = 0; i < cgraph->n_leafs; i++) {
1795 if (ggml_backend_buffer_is_meta(cgraph->leafs[i]->buffer)) {
1796 used_buffers.emplace(cgraph->leafs[i]->buffer);
1797 }
1798 }
1799 for (int i = 0; i < cgraph->n_nodes; i++) {
1800 if (ggml_backend_buffer_is_meta(cgraph->nodes[i]->buffer)) {
1801 used_buffers.emplace(cgraph->nodes[i]->buffer);
1802 }
1803 }
1804 for (ggml_backend_buffer_t buf : used_buffers) {
1805 ggml_backend_meta_buffer_context * buf_ctx = (ggml_backend_meta_buffer_context *) buf->context;
1806 buf_ctx->stc_compute_index_next = buf_ctx->stc_compute_index ^ 1;
1807 ggml_backend_meta_simple_tensor_container & stc = buf_ctx->stc_compute[buf_ctx->stc_compute_index_next];
1808 for (ggml_context_ptr & ctx : stc.ctxs) {
1809 ggml_reset(ctx.get());
1810 }
1811 stc.simple_tensors.clear();
1812 }
1813 size_t n_subgraphs = 0;
1814 size_t max_tmp_size = 0;
1815
1816 for (size_t j = 0; j < n_backends; j++) {
1817 auto & bcj = backend_ctx->backend_configs[j];
1818
1819 for (int i = 0; i < cgraph->n_nodes; i++) {
1820 ggml_tensor * node = cgraph->nodes[i];
1821 if (node->view_src != nullptr && node->view_src->op == GGML_OP_NONE && ggml_backend_buffer_is_host(node->view_src->buffer)) {
1822 // FIXME s_copy_main is on the CPU and its view seems to be incorrectly added to the graph nodes.
1823 // For regular usage this doesn't matter since it's a noop but trying to call ggml_backend_meta_buffer_simple_tensor results in a crash.
1824 bcj.nodes[i] = node;
1825 continue;
1826 }
1827 bcj.nodes[i] = ggml_backend_meta_buffer_simple_tensor(node, j);
1828 GGML_ASSERT(bcj.nodes[i])if (!(bcj.nodes[i])) ggml_abort("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 1828, "GGML_ASSERT(%s) failed", "bcj.nodes[i]")
;
1829 }
1830 }
1831
1832 {
1833 // For MoE models it may make sense to delay the AllReduce in order to reduce I/O:
1834 auto get_i_delayed = [&](const int i) -> int {
1835 int id = i; // i_delayed
1836 int idr = i; // i_delayed return, last safe return value
1837
1838 ggml_tensor * node = cgraph->nodes[id];
1839 int32_t n_used = ggml_node_get_use_count(cgraph, id);
1840
1841 // Skip MIRRORED nodes that don't consume node
1842 auto skip_unrelated = [&]() {
1843 while (id + 1 < cgraph->n_nodes) {
1844 ggml_tensor * next = cgraph->nodes[id+1];
1845 if (ggml_backend_meta_get_split_state(next, false).axis != GGML_BACKEND_SPLIT_AXIS_MIRRORED) {
1846 break;
1847 }
1848 bool safe = true;
1849 for (int s = 0; s < GGML_MAX_SRC10; s++) {
1850 if (next->src[s] == nullptr) {
1851 continue;
1852 }
1853 if (next->src[s] == node) {
1854 safe = false;
1855 break;
1856 }
1857 if (ggml_backend_meta_get_split_state(next->src[s], false).axis != GGML_BACKEND_SPLIT_AXIS_MIRRORED) {
1858 safe = false;
1859 break;
1860 }
1861 }
1862 if (!safe) {
1863 break;
1864 }
1865 id++;
1866 }
1867 };
1868
1869 skip_unrelated();
1870 if (id + 1 >= cgraph->n_nodes) {
1871 return idr;
1872 }
1873 {
1874 ggml_tensor * next = cgraph->nodes[id+1];
1875 if (next->op == GGML_OP_ADD_ID && next->src[0] == node &&
1876 ggml_backend_meta_get_split_state(next->src[1], false).axis == GGML_BACKEND_SPLIT_AXIS_PARTIAL &&
1877 ggml_backend_meta_get_split_state(next->src[2], false).axis == GGML_BACKEND_SPLIT_AXIS_MIRRORED) {
1878 node = next;
1879 id++;
1880 idr = id;
1881 n_used = ggml_node_get_use_count(cgraph, id);
1882 }
1883 }
1884 // Chain of MULs with MIRRORED src[1]
1885 while (true) {
1886 skip_unrelated();
1887 if (id + 1 >= cgraph->n_nodes) {
1888 return idr;
1889 }
1890 ggml_tensor * next = cgraph->nodes[id+1];
1891 if (next->op == GGML_OP_MUL && next->src[0] == node &&
1892 ggml_backend_meta_get_split_state(next->src[1], false).axis == GGML_BACKEND_SPLIT_AXIS_MIRRORED) {
1893 node = next;
1894 id++;
1895 idr = id;
1896 n_used = ggml_node_get_use_count(cgraph, id);
1897 } else {
1898 break;
1899 }
1900 }
1901
1902 if (n_used != node->ne[1] || id + 2*n_used-1 >= cgraph->n_nodes) {
1903 return idr;
1904 }
1905 for (int32_t k = 0; k < n_used; k++) {
1906 ggml_tensor * next = cgraph->nodes[id+1];
1907 if (next->op != GGML_OP_VIEW || next->view_src != node || next->view_offs != k*node->nb[1] ||
1908 next->ne[0] != node->ne[0] || next->ne[1] != node->ne[2] || next->nb[1] != node->nb[2] ||
1909 ggml_node_get_use_count(cgraph, id+1) != 1) {
1910 return idr;
1911 }
1912 id++;
1913 }
1914 {
1915 ggml_tensor * next = cgraph->nodes[id+1];
1916 if (next->op != GGML_OP_ADD || next->src[0] != cgraph->nodes[id - (n_used-1)] ||
1917 next->src[1] != cgraph->nodes[id - (n_used-2)] || ggml_node_get_use_count(cgraph, id+1) != 1) {
1918 return idr;
1919 }
1920 id++;
1921 }
1922 for (int32_t k = 0; k < n_used - 2; k++) {
1923 ggml_tensor * next = cgraph->nodes[id+1];
1924 if (next->op != GGML_OP_ADD || next->src[0] != cgraph->nodes[id] ||
1925 next->src[1] != cgraph->nodes[id - (n_used-2)] || ggml_node_get_use_count(cgraph, id+1) != 1) {
1926 return idr;
1927 }
1928 id++;
1929 }
1930 idr = id;
1931 return idr;
1932 };
1933
1934 int i_start = 0;
1935 for (int i = 0; i < cgraph->n_nodes; i++) {
1936 ggml_tensor * node = cgraph->nodes[i];
1937 if (node->view_src != nullptr && node->view_src->op == GGML_OP_NONE && ggml_backend_buffer_is_host(node->view_src->buffer)) {
1938 continue;
1939 }
1940 const ggml_backend_meta_split_state split_state = ggml_backend_meta_get_split_state(node, /*assume_sync =*/ false);
1941 if (split_state.axis == GGML_BACKEND_SPLIT_AXIS_PARTIAL) {
1942 max_tmp_size = std::max(max_tmp_size, ggml_nbytes(node));
1943 }
1944 const bool new_subgraph = i + 1 == cgraph->n_nodes || split_state.axis == GGML_BACKEND_SPLIT_AXIS_PARTIAL;
1945 if (!new_subgraph) {
1946 continue;
1947 }
1948
1949 const int i_delayed = get_i_delayed(i);
1950
1951 // If we can delay the AllReduce we need to consider the interaction with zero-sized tensor slices.
1952 // A backend with such a slice would normally have valid data after participating in the AllReduce with a node that has
1953 // its compute flag disabled and thus gets its data zeroed out.
1954 // If the AllReduce is delayed then the nodes until that point also need to have their compute flag disabled.
1955 if (i_delayed > i) {
1956 for (size_t j = 0; j < n_backends; j++) {
1957 auto & bcj = backend_ctx->backend_configs[j];
1958 if ((bcj.nodes[i]->flags & GGML_TENSOR_FLAG_COMPUTE) == 0) {
1959 for (int ii = i + 1; ii <= i_delayed; ii++) {
1960 bcj.nodes[ii]->flags &= ~GGML_TENSOR_FLAG_COMPUTE;
1961 }
1962 }
1963 }
1964 }
1965
1966 i = i_delayed;
1967
1968 for (size_t j = 0; j < n_backends; j++) {
1969 auto & bcj = backend_ctx->backend_configs[j];
1970 bcj.cgraphs[n_subgraphs].offset = i_start;
1971 }
1972 n_subgraphs++;
1973 i_start = i + 1;
1974 }
1975 GGML_ASSERT(i_start == cgraph->n_nodes)if (!(i_start == cgraph->n_nodes)) ggml_abort("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 1975, "GGML_ASSERT(%s) failed", "i_start == cgraph->n_nodes"
)
;
1976 }
1977
1978 backend_ctx->uid = cgraph->uid;
1979 backend_ctx->n_subgraphs = n_subgraphs;
1980
1981 if (max_tmp_size > backend_ctx->max_tmp_size) {
1982 for (size_t j = 0; j < n_backends; j++) {
1983 auto & bcj = backend_ctx->backend_configs[j];
1984 for (size_t i = 0; i < backend_ctx->n_reduce_steps; i++) {
1985 bcj.bufs[i].reset(ggml_backend_alloc_buffer(bcj.backend, max_tmp_size));
1986 }
1987 }
1988 backend_ctx->max_tmp_size = max_tmp_size;
1989 }
1990
1991 if (max_nnodes_raised || n_subgraphs > backend_ctx->max_subgraphs) {
1992 backend_ctx->max_subgraphs = std::max(backend_ctx->max_subgraphs, n_subgraphs);
1993 const size_t n_nodes_per_device = 3 * backend_ctx->n_reduce_steps; // tmp + ADD (+zeroing) graph per step and device
1994 const size_t n_cgraphs_per_device = 2 * backend_ctx->n_reduce_steps; // ADD ( + zeroing) graph per step and device
1995 const size_t mem_per_device_graphs_main = backend_ctx->max_subgraphs*ggml_graph_overhead_custom(backend_ctx->max_nnodes, cgraph->grads);
1996 const size_t mem_per_device_graphs_aux = n_cgraphs_per_device*backend_ctx->max_subgraphs*ggml_graph_overhead_custom(1, cgraph->grads);
1997 const size_t mem_per_device_nodes_aux = n_nodes_per_device*backend_ctx->max_subgraphs*ggml_tensor_overhead();
1998 const ggml_init_params params = {
1999 /*.mem_size =*/ n_backends * (mem_per_device_graphs_main + mem_per_device_graphs_aux + mem_per_device_nodes_aux),
2000 /*.mem_buffer =*/ nullptr,
2001 /*.no_alloc =*/ true,
2002 };
2003 backend_ctx->ctx.reset(ggml_init(params));
2004 for (size_t j = 0; j < n_backends; j++) {
2005 auto & bcj = backend_ctx->backend_configs[j];
2006 for (size_t i = 0; i < n_subgraphs; i++) {
2007 bcj.cgraphs[i].cgraph_main = ggml_new_graph_custom(backend_ctx->ctx.get(), cgraph->n_nodes, /*grads =*/ false);
2008 }
2009 }
2010 backend_ctx->cgraphs_aux.resize(n_backends*n_cgraphs_per_device*backend_ctx->max_subgraphs);
2011 for (size_t k = 0; k < backend_ctx->cgraphs_aux.size(); k++) {
2012 backend_ctx->cgraphs_aux[k] = ggml_new_graph_custom(backend_ctx->ctx.get(), 1, cgraph->grads);
2013 }
2014 backend_ctx->nodes_aux.resize(n_backends*n_nodes_per_device*backend_ctx->max_subgraphs);
2015 for (size_t k = 0; k < backend_ctx->nodes_aux.size(); k++) {
2016 backend_ctx->nodes_aux[k] = ggml_new_tensor_1d(backend_ctx->ctx.get(), GGML_TYPE_F32, 1);
2017 }
2018 }
2019
2020 for (size_t j = 0; j < n_backends; j++) {
2021 auto & bcj = backend_ctx->backend_configs[j];
2022 for (size_t i_graph = 0; i_graph < n_subgraphs; i_graph++) {
2023 ggml_cgraph * cgraph_ij = bcj.cgraphs[i_graph].cgraph_main;
2024 const size_t i_node_start = bcj.cgraphs[i_graph].offset;
2025 const size_t i_node_stop = i_graph + 1 < n_subgraphs ? bcj.cgraphs[i_graph + 1].offset : cgraph->n_nodes;
2026 cgraph_ij->n_nodes = i_node_stop - i_node_start;
2027 ggml_hash_set_reset(&cgraph_ij->visited_hash_set);
2028 for (size_t i_node = i_node_start; i_node < i_node_stop; i_node++) {
2029 ggml_tensor * node_ij = bcj.nodes[i_node];
2030 cgraph_ij->nodes[i_node - i_node_start] = node_ij;
2031 const size_t hash_pos_orig = ggml_hash_find(&cgraph->visited_hash_set, cgraph->nodes[i_node]);
2032 const size_t hash_pos_ij = ggml_hash_insert(&cgraph_ij->visited_hash_set, node_ij);
2033 cgraph_ij->use_counts[hash_pos_ij] = cgraph->use_counts[hash_pos_orig];
2034 }
2035 cgraph_ij->uid = ggml_graph_next_uid();
2036 }
2037 }
2038 }
2039
2040 size_t iga = 0; // i graph aux
2041 size_t ina = 0; // i node aux
2042
2043 auto get_node_aux = [&](ggml_tensor * t) -> ggml_tensor * {
2044 ggml_tensor * ret = backend_ctx->nodes_aux[ina++];
2045 memset(ret, 0, sizeof(ggml_tensor));
2046 ret->op = GGML_OP_NONE;
2047 ret->type = t->type;
2048 for (size_t k = 0; k < GGML_MAX_DIMS4; k++) {
2049 ret->ne[k] = t->ne[k];
2050 ret->nb[k] = t->nb[k];
2051 }
2052 return ret;
2053 };
2054 auto set_tmp_data = [&](ggml_tensor * tensor, const size_t j, const size_t i_buf) {
2055 auto & bcj = backend_ctx->backend_configs[j];
2056 ggml_backend_buffer_ptr & buf_ptr = bcj.bufs[i_buf];
2057 if (!buf_ptr || ggml_backend_buffer_get_size(buf_ptr.get()) < backend_ctx->max_tmp_size) {
2058 buf_ptr.reset(ggml_backend_alloc_buffer(bcj.backend, backend_ctx->max_tmp_size));
2059 }
2060 tensor->buffer = buf_ptr.get();
2061 tensor->data = ggml_backend_buffer_get_base(buf_ptr.get());
2062 };
2063 // FIXME usage_counts
2064 auto get_cgraph_aux = [&]() -> ggml_cgraph * {
2065 ggml_cgraph * ret = backend_ctx->cgraphs_aux[iga++];
2066 return ret;
2067 };
2068
2069 // Preferentially use backend-specific allreduce_tensor_async (e.g. NCCL for CUDA), use a generic fallback if unavailable:
2070 auto allreduce_fallback = [&](size_t i) -> ggml_status {
2071 std::vector<ggml_cgraph *> step_cgraphs(n_backends, nullptr);
2072
2073 // Zero out nodes that were disabled due to having a zero-sized slice:
2074 for (size_t j = 0; j < n_backends; j++) {
2075 auto & bcj = backend_ctx->backend_configs[j];
2076 ggml_tensor * node = bcj.cgraphs[i].cgraph_main->nodes[bcj.cgraphs[i].cgraph_main->n_nodes - 1];
2077 if (node->flags & GGML_TENSOR_FLAG_COMPUTE) {
2078 continue;
2079 }
2080 ggml_tensor * node_zero = get_node_aux(node);
2081 node_zero->op = GGML_OP_SCALE; // FIXME 0.0f * NaN == NaN
2082 node_zero->src[0] = node;
2083 ggml_set_op_params_f32(node_zero, 0, 0.0f);
2084 node_zero->data = node->data;
2085 node_zero->buffer = node->buffer;
2086 node_zero->flags |= GGML_TENSOR_FLAG_COMPUTE;
2087
2088 step_cgraphs[j] = get_cgraph_aux();
2089 step_cgraphs[j]->nodes[0] = node_zero;
2090 step_cgraphs[j]->n_nodes = 1;
2091 const ggml_status status = ggml_backend_graph_compute_async(bcj.backend, step_cgraphs[j]);
2092 if (status != GGML_STATUS_SUCCESS) {
2093 return status;
2094 }
2095 }
2096 std::fill(step_cgraphs.begin(), step_cgraphs.end(), nullptr);
2097
2098 auto push_data = [&](const size_t j_src, const size_t j_dst, const size_t i_buf) {
2099 assert(step_cgraphs[j_dst] == nullptr)(static_cast <bool> (step_cgraphs[j_dst] == nullptr) ? void
(0) : __assert_fail ("step_cgraphs[j_dst] == nullptr", __builtin_FILE
(), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__))
;
2100 auto & bcj_src = backend_ctx->backend_configs[j_src];
2101 auto & bcj_dst = backend_ctx->backend_configs[j_dst];
2102
2103 ggml_tensor * node_src = bcj_src.cgraphs[i].cgraph_main->nodes[bcj_src.cgraphs[i].cgraph_main->n_nodes - 1];
2104 ggml_tensor * node_dst = bcj_dst.cgraphs[i].cgraph_main->nodes[bcj_dst.cgraphs[i].cgraph_main->n_nodes - 1];
2105 GGML_ASSERT(ggml_is_contiguous(node_src))if (!(ggml_is_contiguous(node_src))) ggml_abort("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 2105, "GGML_ASSERT(%s) failed", "ggml_is_contiguous(node_src)"
)
;
2106 GGML_ASSERT(ggml_is_contiguous(node_dst))if (!(ggml_is_contiguous(node_dst))) ggml_abort("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 2106, "GGML_ASSERT(%s) failed", "ggml_is_contiguous(node_dst)"
)
;
2107
2108 ggml_tensor * node_tmp = get_node_aux(node_dst);
2109 set_tmp_data(node_tmp, j_dst, i_buf);
2110
2111 ggml_backend_tensor_copy_async(bcj_src.backend, bcj_dst.backend, node_src, node_tmp);
2112
2113 ggml_tensor * node_red = get_node_aux(node_dst);
2114 node_red->view_src = node_dst->view_src == nullptr ? node_dst : node_dst->view_src;
2115 node_red->view_offs = node_dst->view_offs;
2116 node_red->op = GGML_OP_ADD;
2117 node_red->src[0] = node_dst;
2118 node_red->src[1] = node_tmp;
2119 node_red->flags |= GGML_TENSOR_FLAG_COMPUTE;
2120 ggml_backend_view_init(node_red);
2121
2122 ggml_cgraph * cgraph_aux = get_cgraph_aux();
2123 cgraph_aux->nodes[0] = node_red;
2124 cgraph_aux->n_nodes = 1;
2125 step_cgraphs[j_dst] = cgraph_aux;
2126 };
2127
2128 size_t offset_j = n_backends/2;
2129 while ((offset_j & (offset_j - 1)) != 0) {
2130 offset_j--;
2131 }
2132 const size_t offset_j_max = offset_j;
2133 size_t i_buf = 0;
2134
2135 // If n_backends is not a power of 2, fold in the excess prior to butterfly reduction:
2136 for (size_t j_src = 2*offset_j_max; j_src < n_backends; j_src++) {
2137 const size_t j_dst = j_src - 2*offset_j_max;
2138 push_data(j_src, j_dst, i_buf);
2139 const ggml_status status = ggml_backend_graph_compute_async(backend_ctx->backend_configs[j_dst].backend, step_cgraphs[j_dst]);
2140 if (status != GGML_STATUS_SUCCESS) {
2141 return status;
2142 }
2143 i_buf = 1;
2144 }
2145
2146 // Butterfly reduction:
2147 for (; offset_j >= 1; offset_j /= 2) {
2148 std::fill(step_cgraphs.begin(), step_cgraphs.end(), nullptr);
2149
2150 for (size_t j = 0; j < 2*offset_j_max; j++) {
2151 const size_t j_other = j ^ offset_j;
2152 if (j_other >= n_backends) {
2153 continue;
2154 }
2155 push_data(j, j_other, i_buf);
2156 }
2157
2158 for (size_t j = 0; j < 2*offset_j_max; j++) {
2159 if (step_cgraphs[j] == nullptr) {
2160 continue;
2161 }
2162 auto & bcj = backend_ctx->backend_configs[j];
2163 const ggml_status status = ggml_backend_graph_compute_async(bcj.backend, step_cgraphs[j]);
2164 if (status != GGML_STATUS_SUCCESS) {
2165 return status;
2166 }
2167 }
2168 i_buf++;
2169 }
2170 assert(i_buf == backend_ctx->n_reduce_steps)(static_cast <bool> (i_buf == backend_ctx->n_reduce_steps
) ? void (0) : __assert_fail ("i_buf == backend_ctx->n_reduce_steps"
, __builtin_FILE (), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__
))
;
2171
2172 // If n_backends is not a power of 2, copy back the reduced tensors to the excess:
2173 for (size_t j = 2*offset_j_max; j < n_backends; j++) {
2174 auto & bcj_src = backend_ctx->backend_configs[j - 2*offset_j_max];
2175 auto & bcj_dst = backend_ctx->backend_configs[j];
2176
2177 ggml_tensor * node_src = bcj_src.cgraphs[i].cgraph_main->nodes[bcj_src.cgraphs[i].cgraph_main->n_nodes - 1];
2178 ggml_tensor * node_dst = bcj_dst.cgraphs[i].cgraph_main->nodes[bcj_dst.cgraphs[i].cgraph_main->n_nodes - 1];
2179 ggml_backend_tensor_copy_async(bcj_src.backend, bcj_dst.backend, node_src, node_dst);
2180 }
2181
2182 return GGML_STATUS_SUCCESS;
2183 };
2184
2185
2186 for (size_t i = 0; i < backend_ctx->n_subgraphs; i++) {
2187 for (size_t j = 0; j < n_backends; j++) {
2188 auto & bcj = backend_ctx->backend_configs[j];
2189 const ggml_status status = ggml_backend_graph_compute_async(bcj.backend, bcj.cgraphs[i].cgraph_main);
2190 if (status != GGML_STATUS_SUCCESS) {
2191 return status;
2192 }
2193 }
2194
2195 if (n_backends > 1 && i < backend_ctx->n_subgraphs - 1) {
2196 bool backend_allreduce_success = false;
2197 if (backend_ctx->comm_ctx) {
2198 std::vector<ggml_tensor *> nodes;
2199 nodes.reserve(n_backends);
2200 for (size_t j = 0; j < n_backends; j++) {
2201 auto & bcj = backend_ctx->backend_configs[j];
2202 ggml_cgraph * cgraph_ij = bcj.cgraphs[i].cgraph_main;
2203 nodes.push_back(cgraph_ij->nodes[cgraph_ij->n_nodes-1]);
2204 }
2205 backend_allreduce_success = backend_ctx->comm_allreduce(backend_ctx->comm_ctx, nodes.data());
2206 }
2207
2208 if (!backend_allreduce_success) {
2209 const ggml_status status = allreduce_fallback(i);
2210 if (status != GGML_STATUS_SUCCESS) {
2211 return status;
2212 }
2213 }
2214 }
2215 }
2216 return GGML_STATUS_SUCCESS;
2217}
2218
2219static const ggml_backend_i ggml_backend_meta_i = {
2220 /* .get_name = */ ggml_backend_meta_get_name,
2221 /* .free = */ ggml_backend_meta_free,
2222 /* .set_tensor_async = */ ggml_backend_meta_set_tensor_async,
2223 /* .get_tensor_async = */ ggml_backend_meta_get_tensor_async,
2224 /* .set_tensor_2d_async = */ nullptr,
2225 /* .get_tensor_2d_async = */ nullptr,
2226 /* .cpy_tensor_async = */ nullptr,
2227 /* .synchronize = */ ggml_backend_meta_synchronize,
2228 /* .graph_plan_create = */ nullptr,
2229 /* .graph_plan_free = */ nullptr,
2230 /* .graph_plan_update = */ nullptr,
2231 /* .graph_plan_compute = */ nullptr,
2232 /* .graph_compute = */ ggml_backend_meta_graph_compute,
2233 /* .event_record = */ nullptr,
2234 /* .event_wait = */ nullptr,
2235 /* .graph_optimize = */ nullptr,
2236};
2237
2238bool ggml_backend_is_meta(ggml_backend_t backend) {
2239 return backend != nullptr && backend->iface.get_name == ggml_backend_meta_i.get_name;
2240}
2241
2242static ggml_backend_t ggml_backend_meta_device_init_backend(ggml_backend_dev_t dev, const char * params) {
2243 ggml_backend_meta_context * backend_ctx = new ggml_backend_meta_context(dev, params);
2244
2245 ggml_backend_t backend = new struct ggml_backend;
2246 backend->guid = ggml_backend_meta_guid();
2247 backend->iface = ggml_backend_meta_i;
2248 backend->device = dev;
2249 backend->context = backend_ctx;
2250 return backend;
2251}
2252
2253size_t ggml_backend_meta_n_backends(ggml_backend_t meta_backend) {
2254 GGML_ASSERT(ggml_backend_is_meta(meta_backend))if (!(ggml_backend_is_meta(meta_backend))) ggml_abort("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 2254, "GGML_ASSERT(%s) failed", "ggml_backend_is_meta(meta_backend)"
)
;
2255 const ggml_backend_meta_context * backend_ctx = (const ggml_backend_meta_context *) meta_backend->context;
2256 return backend_ctx->backend_configs.size();
2257}
2258
2259ggml_backend_t ggml_backend_meta_simple_backend(ggml_backend_t meta_backend, size_t index) {
2260 GGML_ASSERT(ggml_backend_is_meta(meta_backend))if (!(ggml_backend_is_meta(meta_backend))) ggml_abort("/root/firefox-clang/third_party/llama.cpp/ggml/src/ggml-backend-meta.cpp"
, 2260, "GGML_ASSERT(%s) failed", "ggml_backend_is_meta(meta_backend)"
)
;
2261 const ggml_backend_meta_context * backend_ctx = (const ggml_backend_meta_context *) meta_backend->context;
2262 return backend_ctx->backend_configs[index].backend;
2263}

/usr/lib/gcc/x86_64-linux-gnu/16/../../../../include/c++/16/bits/basic_string.h

1// Components for manipulating sequences of characters -*- C++ -*-
2
3// Copyright (C) 1997-2026 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#ifdef _GLIBCXX_SYSHDR
38#pragma GCC system_header
39#endif
40
41#include <ext/alloc_traits.h>
42#include <debug/debug.h>
43
44#if __cplusplus202002L >= 201103L
45#include <initializer_list>
46#endif
47
48#include <bits/version.h>
49
50#ifdef __glibcxx_string_view201803L // >= C++17
51# include <string_view>
52#endif
53
54#if __glibcxx_containers_ranges // C++ >= 23
55# include <bits/ranges_algobase.h> // ranges::copy
56# include <bits/ranges_util.h> // ranges::subrange
57#endif
58
59#if __glibcxx_to_string >= 202306L // C++ >= 26
60# include <charconv>
61#endif
62
63#if ! _GLIBCXX_USE_CXX11_ABI1
64# include "cow_string.h"
65#else
66
67namespace std _GLIBCXX_VISIBILITY(default)__attribute__ ((__visibility__ ("default")))
68{
69_GLIBCXX_BEGIN_NAMESPACE_VERSION
70_GLIBCXX_BEGIN_NAMESPACE_CXX11namespace __cxx11 {
71
72 /**
73 * @class basic_string basic_string.h <string>
74 * @brief Managing sequences of characters and character-like objects.
75 *
76 * @ingroup strings
77 * @ingroup sequences
78 * @headerfile string
79 * @since C++98
80 *
81 * @tparam _CharT Type of character
82 * @tparam _Traits Traits for character type, defaults to
83 * char_traits<_CharT>.
84 * @tparam _Alloc Allocator type, defaults to allocator<_CharT>.
85 *
86 * Meets the requirements of a <a href="tables.html#65">container</a>, a
87 * <a href="tables.html#66">reversible container</a>, and a
88 * <a href="tables.html#67">sequence</a>. Of the
89 * <a href="tables.html#68">optional sequence requirements</a>, only
90 * @c push_back, @c at, and @c %array access are supported.
91 */
92 template<typename _CharT, typename _Traits, typename _Alloc>
93 class basic_string
94 {
95#if __cplusplus202002L >= 202002L
96 static_assert(is_trivially_copyable_v<_CharT>
97 && is_trivially_default_constructible_v<_CharT>
98 && is_standard_layout_v<_CharT>);
99 static_assert(is_same_v<_CharT, typename _Traits::char_type>);
100 static_assert(is_same_v<_CharT, typename _Alloc::value_type>);
101 using _Char_alloc_type = _Alloc;
102#else
103 typedef typename __gnu_cxx::__alloc_traits<_Alloc>::template
104 rebind<_CharT>::other _Char_alloc_type;
105#endif
106
107 typedef __gnu_cxx::__alloc_traits<_Char_alloc_type> _Alloc_traits;
108
109 // Types:
110 public:
111 typedef _Traits traits_type;
112 typedef typename _Traits::char_type value_type;
113 typedef _Char_alloc_type allocator_type;
114 typedef typename _Alloc_traits::size_type size_type;
115 typedef typename _Alloc_traits::difference_type difference_type;
116 typedef typename _Alloc_traits::reference reference;
117 typedef typename _Alloc_traits::const_reference const_reference;
118 typedef typename _Alloc_traits::pointer pointer;
119 typedef typename _Alloc_traits::const_pointer const_pointer;
120 typedef __gnu_cxx::__normal_iterator<pointer, basic_string> iterator;
121 typedef __gnu_cxx::__normal_iterator<const_pointer, basic_string>
122 const_iterator;
123 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
124 typedef std::reverse_iterator<iterator> reverse_iterator;
125
126 /// Value returned by various member functions when they fail.
127 static const size_type npos = static_cast<size_type>(-1);
128
129 protected:
130 // type used for positions in insert, erase etc.
131#if __cplusplus202002L < 201103L
132 typedef iterator __const_iterator;
133#else
134 typedef const_iterator __const_iterator;
135#endif
136
137 private:
138 static _GLIBCXX20_CONSTEXPRconstexpr pointer
139 _S_allocate(_Char_alloc_type& __a, size_type __n)
140 {
141 pointer __p = _Alloc_traits::allocate(__a, __n);
142#if __glibcxx_constexpr_string201907L >= 201907L
143 // std::char_traits begins the lifetime of characters,
144 // but custom traits might not, so do it here.
145 if constexpr (!is_same_v<_Traits, char_traits<_CharT>>)
146 if (std::__is_constant_evaluated())
147 // Begin the lifetime of characters in allocated storage.
148 for (size_type __i = 0; __i < __n; ++__i)
149 std::construct_at(__builtin_addressof(__p[__i]));
150#endif
151 return __p;
152 }
153
154#ifdef __glibcxx_string_view201803L // >= C++17
155 // A helper type for avoiding boiler-plate.
156 typedef basic_string_view<_CharT, _Traits> __sv_type;
157
158 template<typename _Tp, typename _Res>
159 using _If_sv = enable_if_t<
160 __and_<is_convertible<const _Tp&, __sv_type>,
161 __not_<is_convertible<const _Tp*, const basic_string*>>,
162 __not_<is_convertible<const _Tp&, const _CharT*>>>::value,
163 _Res>;
164
165 // Allows an implicit conversion to __sv_type.
166 _GLIBCXX20_CONSTEXPRconstexpr
167 static __sv_type
168 _S_to_string_view(__sv_type __svt) noexcept
169 { return __svt; }
170
171 // Wraps a string_view by explicit conversion and thus
172 // allows to add an internal constructor that does not
173 // participate in overload resolution when a string_view
174 // is provided.
175 struct __sv_wrapper
176 {
177 _GLIBCXX20_CONSTEXPRconstexpr explicit
178 __sv_wrapper(__sv_type __sv) noexcept : _M_sv(__sv) { }
179
180 __sv_type _M_sv;
181 };
182
183 /**
184 * @brief Only internally used: Construct string from a string view
185 * wrapper.
186 * @param __svw string view wrapper.
187 * @param __a Allocator to use.
188 */
189 _GLIBCXX20_CONSTEXPRconstexpr
190 explicit
191 basic_string(__sv_wrapper __svw, const _Alloc& __a)
192 : basic_string(__svw._M_sv.data(), __svw._M_sv.size(), __a) { }
193#endif
194
195 // Use empty-base optimization: http://www.cantrip.org/emptyopt.html
196 struct _Alloc_hider : allocator_type // TODO check __is_final
197 {
198#if __cplusplus202002L < 201103L
199 _Alloc_hider(pointer __dat, const _Alloc& __a = _Alloc())
200 : allocator_type(__a), _M_p(__dat) { }
201#else
202 _GLIBCXX20_CONSTEXPRconstexpr
203 _Alloc_hider(pointer __dat, const _Alloc& __a)
204 : allocator_type(__a), _M_p(__dat) { }
205
206 _GLIBCXX20_CONSTEXPRconstexpr
207 _Alloc_hider(pointer __dat, _Alloc&& __a = _Alloc())
208 : allocator_type(std::move(__a)), _M_p(__dat) { }
209#endif
210
211 pointer _M_p; // The actual data.
212 };
213
214 _Alloc_hider _M_dataplus;
215 size_type _M_string_length;
216
217 enum { _S_local_capacity = 15 / sizeof(_CharT) };
218
219 union
220 {
221 _CharT _M_local_buf[_S_local_capacity + 1];
222 size_type _M_allocated_capacity;
223 };
224
225 _GLIBCXX20_CONSTEXPRconstexpr
226 void
227 _M_data(pointer __p)
228 { _M_dataplus._M_p = __p; }
229
230 _GLIBCXX20_CONSTEXPRconstexpr
231 void
232 _M_length(size_type __length)
233 { _M_string_length = __length; }
234
235 _GLIBCXX20_CONSTEXPRconstexpr
236 pointer
237 _M_data() const
238 { return _M_dataplus._M_p; }
239
240 _GLIBCXX20_CONSTEXPRconstexpr
241 pointer
242 _M_local_data()
243 {
244#if __cplusplus202002L >= 201103L
245 return std::pointer_traits<pointer>::pointer_to(*_M_local_buf);
246#else
247 return pointer(_M_local_buf);
248#endif
249 }
250
251 _GLIBCXX20_CONSTEXPRconstexpr
252 const_pointer
253 _M_local_data() const
254 {
255#if __cplusplus202002L >= 201103L
256 return std::pointer_traits<const_pointer>::pointer_to(*_M_local_buf);
257#else
258 return const_pointer(_M_local_buf);
259#endif
260 }
261
262 _GLIBCXX20_CONSTEXPRconstexpr
263 void
264 _M_capacity(size_type __capacity)
265 { _M_allocated_capacity = __capacity; }
266
267 _GLIBCXX20_CONSTEXPRconstexpr
268 void
269 _M_set_length(size_type __n)
270 {
271 traits_type::assign(_M_data()[__n], _CharT());
272 _M_length(__n);
273 }
274
275 _GLIBCXX20_CONSTEXPRconstexpr
276 bool
277 _M_is_local() const
278 {
279 if (_M_data() == _M_local_data())
280 {
281 if (_M_string_length > _S_local_capacity)
282 __builtin_unreachable();
283 return true;
284 }
285 return false;
286 }
287
288 // Create & Destroy
289 _GLIBCXX20_CONSTEXPRconstexpr
290 pointer
291 _M_create(size_type&, size_type);
292
293 _GLIBCXX20_CONSTEXPRconstexpr
294 void
295 _M_dispose()
296 {
297 if (!_M_is_local())
298 _M_destroy(_M_allocated_capacity);
299 }
300
301 _GLIBCXX20_CONSTEXPRconstexpr
302 void
303 _M_destroy(size_type __size) throw()
304 { _Alloc_traits::deallocate(_M_get_allocator(), _M_data(), __size + 1); }
305
306#if __cplusplus202002L < 201103L || defined _GLIBCXX_DEFINING_STRING_INSTANTIATIONS
307 // _M_construct_aux is used to implement the 21.3.1 para 15 which
308 // requires special behaviour if _InIterator is an integral type
309 template<typename _InIterator>
310 void
311 _M_construct_aux(_InIterator __beg, _InIterator __end,
312 std::__false_type)
313 {
314 typedef typename iterator_traits<_InIterator>::iterator_category _Tag;
315 _M_construct(__beg, __end, _Tag());
316 }
317
318 // _GLIBCXX_RESOLVE_LIB_DEFECTS
319 // 438. Ambiguity in the "do the right thing" clause
320 template<typename _Integer>
321 void
322 _M_construct_aux(_Integer __beg, _Integer __end, std::__true_type)
323 { _M_construct_aux_2(static_cast<size_type>(__beg), __end); }
324
325 void
326 _M_construct_aux_2(size_type __req, _CharT __c)
327 { _M_construct(__req, __c); }
328#endif
329
330 // For Input Iterators, used in istreambuf_iterators, etc.
331 template<typename _InIterator>
332 _GLIBCXX20_CONSTEXPRconstexpr
333 void
334 _M_construct(_InIterator __beg, _InIterator __end,
335 std::input_iterator_tag);
336
337 // For forward_iterators up to random_access_iterators, used for
338 // string::iterator, _CharT*, etc.
339 template<typename _FwdIterator>
340 _GLIBCXX20_CONSTEXPRconstexpr
341 void
342 _M_construct(_FwdIterator __beg, _FwdIterator __end,
343 std::forward_iterator_tag);
344
345 _GLIBCXX20_CONSTEXPRconstexpr
346 void
347 _M_construct(size_type __req, _CharT __c);
348
349 // Construct using block of memory of known size.
350 // If _Terminated is true assume that source is already 0 terminated.
351 template<bool _Terminated>
352 _GLIBCXX20_CONSTEXPRconstexpr
353 void
354 _M_construct(const _CharT *__c, size_type __n);
355
356#if __cplusplus202002L >= 202302L
357 constexpr void
358 _M_construct(basic_string&& __str, size_type __pos, size_type __n);
359#endif
360
361 _GLIBCXX20_CONSTEXPRconstexpr
362 allocator_type&
363 _M_get_allocator()
364 { return _M_dataplus; }
365
366 _GLIBCXX20_CONSTEXPRconstexpr
367 const allocator_type&
368 _M_get_allocator() const
369 { return _M_dataplus; }
370
371 // Ensure that _M_local_buf is the active member of the union.
372 __attribute__((__always_inline__))
373 _GLIBCXX14_CONSTEXPRconstexpr
374 void
375 _M_init_local_buf() _GLIBCXX_NOEXCEPTnoexcept
376 {
377#if __glibcxx_is_constant_evaluated201811L
378 if (std::is_constant_evaluated())
379 for (size_type __i = 0; __i <= _S_local_capacity; ++__i)
380 _M_local_buf[__i] = _CharT();
381#endif
382 }
383
384 __attribute__((__always_inline__))
385 _GLIBCXX14_CONSTEXPRconstexpr
386 pointer
387 _M_use_local_data() _GLIBCXX_NOEXCEPTnoexcept
388 {
389#if __cpp_lib_is_constant_evaluated201811L
390 _M_init_local_buf();
391#endif
392 return _M_local_data();
393 }
394
395 private:
396
397#ifdef _GLIBCXX_DISAMBIGUATE_REPLACE_INST
398 // The explicit instantiations in misc-inst.cc require this due to
399 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=64063
400 template<typename _Tp, bool _Requires =
401 !__are_same<_Tp, _CharT*>::__value
402 && !__are_same<_Tp, const _CharT*>::__value
403 && !__are_same<_Tp, iterator>::__value
404 && !__are_same<_Tp, const_iterator>::__value>
405 struct __enable_if_not_native_iterator
406 { typedef basic_string& __type; };
407 template<typename _Tp>
408 struct __enable_if_not_native_iterator<_Tp, false> { };
409#endif
410
411 _GLIBCXX20_CONSTEXPRconstexpr
412 size_type
413 _M_check(size_type __pos, const char* __s) const
414 {
415 if (__pos > this->size())
416 __throw_out_of_range_fmt(__N("%s: __pos (which is %zu) > "("%s: __pos (which is %zu) > " "this->size() (which is %zu)"
)
417 "this->size() (which is %zu)")("%s: __pos (which is %zu) > " "this->size() (which is %zu)"
)
,
418 __s, (size_t)__pos, (size_t)this->size());
419 return __pos;
420 }
421
422 _GLIBCXX20_CONSTEXPRconstexpr
423 void
424 _M_check_length(size_type __n1, size_type __n2, const char* __s) const
425 {
426 if (this->max_size() - (this->size() - __n1) < __n2)
427 __throw_length_error(__N(__s)(__s));
428 }
429
430
431 // NB: _M_limit doesn't check for a bad __pos value.
432 _GLIBCXX20_CONSTEXPRconstexpr
433 size_type
434 _M_limit(size_type __pos, size_type __off) const _GLIBCXX_NOEXCEPTnoexcept
435 {
436 const bool __testoff = __off < this->size() - __pos;
437 return __testoff ? __off : this->size() - __pos;
438 }
439
440 // True if _Rep and source do not overlap.
441 bool
442 _M_disjunct(const _CharT* __s) const _GLIBCXX_NOEXCEPTnoexcept
443 {
444 return (less<const _CharT*>()(__s, _M_data())
445 || less<const _CharT*>()(_M_data() + this->size(), __s));
446 }
447
448 // When __n = 1 way faster than the general multichar
449 // traits_type::copy/move/assign.
450 _GLIBCXX20_CONSTEXPRconstexpr
451 static void
452 _S_copy(_CharT* __d, const _CharT* __s, size_type __n)
453 {
454 if (__n == 1)
455 traits_type::assign(*__d, *__s);
456 else
457 traits_type::copy(__d, __s, __n);
458 }
459
460 _GLIBCXX20_CONSTEXPRconstexpr
461 static void
462 _S_move(_CharT* __d, const _CharT* __s, size_type __n)
463 {
464 if (__n == 1)
465 traits_type::assign(*__d, *__s);
466 else
467 traits_type::move(__d, __s, __n);
468 }
469
470 _GLIBCXX20_CONSTEXPRconstexpr
471 static void
472 _S_assign(_CharT* __d, size_type __n, _CharT __c)
473 {
474 if (__n == 1)
475 traits_type::assign(*__d, __c);
476 else
477 traits_type::assign(__d, __n, __c);
478 }
479
480#pragma GCC diagnostic push
481#pragma GCC diagnostic ignored "-Wc++17-extensions"
482 // _S_copy_chars is a separate template to permit specialization
483 // to optimize for the common case of pointers as iterators.
484 template<class _Iterator>
485 _GLIBCXX20_CONSTEXPRconstexpr
486 static void
487 _S_copy_chars(_CharT* __p, _Iterator __k1, _Iterator __k2)
488 {
489#if __cplusplus202002L >= 201103L
490 using _IterBase = decltype(std::__niter_base(__k1));
491 if constexpr (__or_<is_same<_IterBase, _CharT*>,
492 is_same<_IterBase, const _CharT*>>::value)
493 _S_copy(__p, std::__niter_base(__k1), __k2 - __k1);
494#if __cpp_lib_concepts202207L
495 else if constexpr (requires {
496 requires contiguous_iterator<_Iterator>;
497 { std::to_address(__k1) }
498 -> convertible_to<const _CharT*>;
499 })
500 {
501 const auto __d = __k2 - __k1;
502 (void) (__k1 + __d); // See P3349R1
503 _S_copy(__p, std::to_address(__k1), static_cast<size_type>(__d));
504 }
505#endif
506 else
507#endif
508 for (; __k1 != __k2; ++__k1, (void)++__p)
509 traits_type::assign(*__p, static_cast<_CharT>(*__k1));
510 }
511#pragma GCC diagnostic pop
512
513#if __cplusplus202002L < 201103L || defined _GLIBCXX_DEFINING_STRING_INSTANTIATIONS
514 static void
515 _S_copy_chars(_CharT* __p, iterator __k1, iterator __k2)
516 { _S_copy_chars(__p, __k1.base(), __k2.base()); }
517
518 static void
519 _S_copy_chars(_CharT* __p, const_iterator __k1, const_iterator __k2)
520 { _S_copy_chars(__p, __k1.base(), __k2.base()); }
521
522 static void
523 _S_copy_chars(_CharT* __p, _CharT* __k1, _CharT* __k2)
524 { _S_copy(__p, __k1, __k2 - __k1); }
525
526 static void
527 _S_copy_chars(_CharT* __p, const _CharT* __k1, const _CharT* __k2)
528 { _S_copy(__p, __k1, __k2 - __k1); }
529#endif
530
531#if __glibcxx_containers_ranges // C++ >= 23
532 // pre: __n == ranges::distance(__rg). __p+[0,__n) is a valid range.
533 template<typename _Rg>
534 static constexpr void
535 _S_copy_range(pointer __p, _Rg&& __rg, size_type __n)
536 {
537 if constexpr (requires {
538 requires ranges::contiguous_range<_Rg>;
539 { ranges::data(std::forward<_Rg>(__rg)) }
540 -> convertible_to<const _CharT*>;
541 })
542 _S_copy(__p, ranges::data(std::forward<_Rg>(__rg)), __n);
543 else
544 {
545 auto __first = ranges::begin(__rg);
546 const auto __last = ranges::end(__rg);
547 for (; __first != __last; ++__first)
548 traits_type::assign(*__p++, static_cast<_CharT>(*__first));
549 }
550 }
551#endif
552
553 _GLIBCXX20_CONSTEXPRconstexpr
554 static int
555 _S_compare(size_type __n1, size_type __n2) _GLIBCXX_NOEXCEPTnoexcept
556 {
557 const difference_type __d = difference_type(__n1 - __n2);
558
559 if (__d > __gnu_cxx::__numeric_traits<int>::__max)
560 return __gnu_cxx::__numeric_traits<int>::__max;
561 else if (__d < __gnu_cxx::__numeric_traits<int>::__min)
562 return __gnu_cxx::__numeric_traits<int>::__min;
563 else
564 return int(__d);
565 }
566
567 _GLIBCXX20_CONSTEXPRconstexpr
568 void
569 _M_assign(const basic_string&);
570
571 _GLIBCXX20_CONSTEXPRconstexpr
572 void
573 _M_mutate(size_type __pos, size_type __len1, const _CharT* __s,
574 size_type __len2);
575
576 _GLIBCXX20_CONSTEXPRconstexpr
577 void
578 _M_erase(size_type __pos, size_type __n);
579
580 public:
581 // Construct/copy/destroy:
582 // NB: We overload ctors in some cases instead of using default
583 // arguments, per 17.4.4.4 para. 2 item 2.
584
585 /**
586 * @brief Default constructor creates an empty string.
587 */
588 _GLIBCXX20_CONSTEXPRconstexpr
589 basic_string()
590 _GLIBCXX_NOEXCEPT_IF(is_nothrow_default_constructible<_Alloc>::value)noexcept(is_nothrow_default_constructible<_Alloc>::value
)
591#if __cpp_concepts202002 && __glibcxx_type_trait_variable_templates201510L
592 requires is_default_constructible_v<_Alloc>
593#endif
594 : _M_dataplus(_M_local_data())
595 {
596 _M_init_local_buf();
597 _M_set_length(0);
598 }
599
600 /**
601 * @brief Construct an empty string using allocator @a a.
602 */
603 _GLIBCXX20_CONSTEXPRconstexpr
604 explicit
605 basic_string(const _Alloc& __a) _GLIBCXX_NOEXCEPTnoexcept
606 : _M_dataplus(_M_local_data(), __a)
607 {
608 _M_init_local_buf();
609 _M_set_length(0);
610 }
611
612 /**
613 * @brief Construct string with copy of value of @a __str.
614 * @param __str Source string.
615 */
616 _GLIBCXX20_CONSTEXPRconstexpr
617 basic_string(const basic_string& __str)
618 : _M_dataplus(_M_local_data(),
619 _Alloc_traits::_S_select_on_copy(__str._M_get_allocator()))
620 {
621 _M_construct<true>(__str._M_data(), __str.length());
622 }
623
624 // _GLIBCXX_RESOLVE_LIB_DEFECTS
625 // 2583. no way to supply an allocator for basic_string(str, pos)
626 /**
627 * @brief Construct string as copy of a substring.
628 * @param __str Source string.
629 * @param __pos Index of first character to copy from.
630 * @param __a Allocator to use.
631 */
632 _GLIBCXX20_CONSTEXPRconstexpr
633 basic_string(const basic_string& __str, size_type __pos,
634 const _Alloc& __a = _Alloc())
635 : _M_dataplus(_M_local_data(), __a)
636 {
637 const _CharT* __start = __str._M_data()
638 + __str._M_check(__pos, "basic_string::basic_string");
639 _M_construct(__start, __start + __str._M_limit(__pos, npos),
640 std::forward_iterator_tag());
641 }
642
643 /**
644 * @brief Construct string as copy of a substring.
645 * @param __str Source string.
646 * @param __pos Index of first character to copy from.
647 * @param __n Number of characters to copy.
648 */
649 _GLIBCXX20_CONSTEXPRconstexpr
650 basic_string(const basic_string& __str, size_type __pos,
651 size_type __n)
652 : _M_dataplus(_M_local_data())
653 {
654 const _CharT* __start = __str._M_data()
655 + __str._M_check(__pos, "basic_string::basic_string");
656 _M_construct(__start, __start + __str._M_limit(__pos, __n),
657 std::forward_iterator_tag());
658 }
659
660 /**
661 * @brief Construct string as copy of a substring.
662 * @param __str Source string.
663 * @param __pos Index of first character to copy from.
664 * @param __n Number of characters to copy.
665 * @param __a Allocator to use.
666 */
667 _GLIBCXX20_CONSTEXPRconstexpr
668 basic_string(const basic_string& __str, size_type __pos,
669 size_type __n, const _Alloc& __a)
670 : _M_dataplus(_M_local_data(), __a)
671 {
672 const _CharT* __start
673 = __str._M_data() + __str._M_check(__pos, "string::string");
674 _M_construct(__start, __start + __str._M_limit(__pos, __n),
675 std::forward_iterator_tag());
676 }
677
678#if __cplusplus202002L >= 202302L
679 _GLIBCXX20_CONSTEXPRconstexpr
680 basic_string(basic_string&& __str, size_type __pos,
681 const _Alloc& __a = _Alloc())
682 : _M_dataplus(_M_local_data(), __a)
683 {
684 __pos = __str._M_check(__pos, "string::string");
685 _M_construct(std::move(__str), __pos, __str.length() - __pos);
686 }
687
688 _GLIBCXX20_CONSTEXPRconstexpr
689 basic_string(basic_string&& __str, size_type __pos, size_type __n,
690 const _Alloc& __a = _Alloc())
691 : _M_dataplus(_M_local_data(), __a)
692 {
693 __pos = __str._M_check(__pos, "string::string");
694 _M_construct(std::move(__str), __pos, __str._M_limit(__pos, __n));
695 }
696#endif // C++23
697
698 /**
699 * @brief Construct string initialized by a character %array.
700 * @param __s Source character %array.
701 * @param __n Number of characters to copy.
702 * @param __a Allocator to use (default is default allocator).
703 *
704 * NB: @a __s must have at least @a __n characters, &apos;\\0&apos;
705 * has no special meaning.
706 */
707 _GLIBCXX20_CONSTEXPRconstexpr
708 basic_string(const _CharT* __s, size_type __n,
709 const _Alloc& __a = _Alloc())
710 : _M_dataplus(_M_local_data(), __a)
711 {
712 // NB: Not required, but considered best practice.
713 if (__s == 0 && __n > 0)
714 std::__throw_logic_error(__N("basic_string: "("basic_string: " "construction from null is not valid")
715 "construction from null is not valid")("basic_string: " "construction from null is not valid"));
716 _M_construct(__s, __s + __n, std::forward_iterator_tag());
717 }
718
719 /**
720 * @brief Construct string as copy of a C string.
721 * @param __s Source C string.
722 * @param __a Allocator to use (default is default allocator).
723 */
724#if __cpp_deduction_guides201703L && ! defined _GLIBCXX_DEFINING_STRING_INSTANTIATIONS
725 // _GLIBCXX_RESOLVE_LIB_DEFECTS
726 // 3076. basic_string CTAD ambiguity
727 template<typename = _RequireAllocator<_Alloc>>
728#endif
729 _GLIBCXX20_CONSTEXPRconstexpr
730 basic_string(const _CharT* __s, const _Alloc& __a = _Alloc())
731 : _M_dataplus(_M_local_data(), __a)
732 {
733 // NB: Not required, but considered best practice.
734 if (__s == 0)
735 std::__throw_logic_error(__N("basic_string: "("basic_string: " "construction from null is not valid")
736 "construction from null is not valid")("basic_string: " "construction from null is not valid"));
737 const _CharT* __end = __s + traits_type::length(__s);
738 _M_construct(__s, __end, forward_iterator_tag());
739 }
740
741 /**
742 * @brief Construct string as multiple characters.
743 * @param __n Number of characters.
744 * @param __c Character to use.
745 * @param __a Allocator to use (default is default allocator).
746 */
747#if __cpp_deduction_guides201703L && ! defined _GLIBCXX_DEFINING_STRING_INSTANTIATIONS
748 // _GLIBCXX_RESOLVE_LIB_DEFECTS
749 // 3076. basic_string CTAD ambiguity
750 template<typename = _RequireAllocator<_Alloc>>
751#endif
752 _GLIBCXX20_CONSTEXPRconstexpr
753 basic_string(size_type __n, _CharT __c, const _Alloc& __a = _Alloc())
754 : _M_dataplus(_M_local_data(), __a)
755 { _M_construct(__n, __c); }
756
757#if __cplusplus202002L >= 201103L
758 /**
759 * @brief Move construct string.
760 * @param __str Source string.
761 *
762 * The newly-created string contains the exact contents of @a __str.
763 * @a __str is a valid, but unspecified string.
764 */
765 _GLIBCXX20_CONSTEXPRconstexpr
766 basic_string(basic_string&& __str) noexcept
767 : _M_dataplus(_M_local_data(), std::move(__str._M_get_allocator()))
768 {
769 if (__str._M_is_local())
770 {
771 _M_init_local_buf();
772 traits_type::copy(_M_local_buf, __str._M_local_buf,
773 __str.length() + 1);
774 }
775 else
776 {
777 _M_data(__str._M_data());
778 _M_capacity(__str._M_allocated_capacity);
779 }
780
781 // Must use _M_length() here not _M_set_length() because
782 // basic_stringbuf relies on writing into unallocated capacity so
783 // we mess up the contents if we put a '\0' in the string.
784 _M_length(__str.length());
785 __str._M_data(__str._M_use_local_data());
786 __str._M_set_length(0);
787 }
788
789#if __glibcxx_containers_ranges // C++ >= 23
790 /**
791 * @brief Construct a string from a range.
792 * @since C++23
793 */
794 template<__detail::__container_compatible_range<_CharT> _Rg>
795 constexpr
796 basic_string(from_range_t, _Rg&& __rg, const _Alloc& __a = _Alloc())
797 : basic_string(__a)
798 {
799 if constexpr (ranges::forward_range<_Rg> || ranges::sized_range<_Rg>)
800 {
801 const auto __n = static_cast<size_type>(ranges::distance(__rg));
802 reserve(__n);
803 _S_copy_range(_M_data(), std::forward<_Rg>(__rg), __n);
804 _M_set_length(__n);
805 }
806 else
807 {
808 auto __first = ranges::begin(__rg);
809 const auto __last = ranges::end(__rg);
810 for (; __first != __last; ++__first)
811 push_back(*__first);
812 }
813 }
814#endif
815
816 /**
817 * @brief Construct string from an initializer %list.
818 * @param __l std::initializer_list of characters.
819 * @param __a Allocator to use (default is default allocator).
820 */
821 _GLIBCXX20_CONSTEXPRconstexpr
822 basic_string(initializer_list<_CharT> __l, const _Alloc& __a = _Alloc())
823 : _M_dataplus(_M_local_data(), __a)
824 { _M_construct(__l.begin(), __l.end(), std::forward_iterator_tag()); }
825
826 _GLIBCXX20_CONSTEXPRconstexpr
827 basic_string(const basic_string& __str, const _Alloc& __a)
828 : _M_dataplus(_M_local_data(), __a)
829 { _M_construct(__str.begin(), __str.end(), std::forward_iterator_tag()); }
830
831 _GLIBCXX20_CONSTEXPRconstexpr
832 basic_string(basic_string&& __str, const _Alloc& __a)
833 noexcept(_Alloc_traits::_S_always_equal())
834 : _M_dataplus(_M_local_data(), __a)
835 {
836 if (__str._M_is_local())
837 {
838 _M_init_local_buf();
839 traits_type::copy(_M_local_buf, __str._M_local_buf,
840 __str.length() + 1);
841 _M_length(__str.length());
842 __str._M_set_length(0);
843 }
844 else if (_Alloc_traits::_S_always_equal()
845 || __str.get_allocator() == __a)
846 {
847 _M_data(__str._M_data());
848 _M_length(__str.length());
849 _M_capacity(__str._M_allocated_capacity);
850 __str._M_data(__str._M_use_local_data());
851 __str._M_set_length(0);
852 }
853 else
854 _M_construct(__str.begin(), __str.end(), std::forward_iterator_tag());
855 }
856#endif // C++11
857
858#if __cplusplus202002L >= 202100L
859 basic_string(nullptr_t) = delete;
860 basic_string& operator=(nullptr_t) = delete;
861#endif // C++23
862
863 /**
864 * @brief Construct string as copy of a range.
865 * @param __beg Start of range.
866 * @param __end End of range.
867 * @param __a Allocator to use (default is default allocator).
868 */
869#if __cplusplus202002L >= 201103L
870 template<typename _InputIterator,
871 typename = std::_RequireInputIter<_InputIterator>>
872#else
873 template<typename _InputIterator>
874#endif
875 _GLIBCXX20_CONSTEXPRconstexpr
876 basic_string(_InputIterator __beg, _InputIterator __end,
877 const _Alloc& __a = _Alloc())
878 : _M_dataplus(_M_local_data(), __a), _M_string_length(0)
879 {
880#if __cplusplus202002L >= 201103L
881 _M_construct(__beg, __end, std::__iterator_category(__beg));
882#else
883 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
884 _M_construct_aux(__beg, __end, _Integral());
885#endif
886 }
887
888#ifdef __glibcxx_string_view201803L // >= C++17
889 /**
890 * @brief Construct string from a substring of a string_view.
891 * @param __t Source object convertible to string view.
892 * @param __pos The index of the first character to copy from __t.
893 * @param __n The number of characters to copy from __t.
894 * @param __a Allocator to use.
895 */
896 template<typename _Tp,
897 typename = enable_if_t<is_convertible_v<const _Tp&, __sv_type>>>
898 _GLIBCXX20_CONSTEXPRconstexpr
899 basic_string(const _Tp& __t, size_type __pos, size_type __n,
900 const _Alloc& __a = _Alloc())
901 : basic_string(_S_to_string_view(__t).substr(__pos, __n), __a) { }
902
903 /**
904 * @brief Construct string from a string_view.
905 * @param __t Source object convertible to string view.
906 * @param __a Allocator to use (default is default allocator).
907 */
908 template<typename _Tp, typename = _If_sv<_Tp, void>>
909 _GLIBCXX20_CONSTEXPRconstexpr
910 explicit
911 basic_string(const _Tp& __t, const _Alloc& __a = _Alloc())
912 : basic_string(__sv_wrapper(_S_to_string_view(__t)), __a) { }
913#endif // C++17
914
915 /**
916 * @brief Destroy the string instance.
917 */
918 _GLIBCXX20_CONSTEXPRconstexpr
919 ~basic_string()
920 { _M_dispose(); }
921
922 /**
923 * @brief Assign the value of @a str to this string.
924 * @param __str Source string.
925 */
926 _GLIBCXX20_CONSTEXPRconstexpr
927 basic_string&
928 operator=(const basic_string& __str)
929 {
930 return this->assign(__str);
931 }
932
933 /**
934 * @brief Copy contents of @a s into this string.
935 * @param __s Source null-terminated string.
936 */
937 _GLIBCXX20_CONSTEXPRconstexpr
938 basic_string&
939 operator=(const _CharT* __s)
940 { return this->assign(__s); }
941
942 /**
943 * @brief Set value to string of length 1.
944 * @param __c Source character.
945 *
946 * Assigning to a character makes this string length 1 and
947 * (*this)[0] == @a c.
948 */
949 _GLIBCXX20_CONSTEXPRconstexpr
950 basic_string&
951 operator=(_CharT __c)
952 {
953 this->assign(1, __c);
954 return *this;
955 }
956
957#if __cplusplus202002L >= 201103L
958 /**
959 * @brief Move assign the value of @a str to this string.
960 * @param __str Source string.
961 *
962 * The contents of @a str are moved into this string (without copying).
963 * @a str is a valid, but unspecified string.
964 */
965 // _GLIBCXX_RESOLVE_LIB_DEFECTS
966 // 2063. Contradictory requirements for string move assignment
967 _GLIBCXX20_CONSTEXPRconstexpr
968 basic_string&
969 operator=(basic_string&& __str)
970 noexcept(_Alloc_traits::_S_nothrow_move())
971 {
972 const bool __equal_allocs = _Alloc_traits::_S_always_equal()
973 || _M_get_allocator() == __str._M_get_allocator();
974 if (!_M_is_local() && _Alloc_traits::_S_propagate_on_move_assign()
975 && !__equal_allocs)
976 {
977 // Destroy existing storage before replacing allocator.
978 _M_destroy(_M_allocated_capacity);
979 _M_data(_M_local_data());
980 _M_set_length(0);
981 }
982 // Replace allocator if POCMA is true.
983 std::__alloc_on_move(_M_get_allocator(), __str._M_get_allocator());
984
985 if (__str._M_is_local())
986 {
987 // We've always got room for a short string, just copy it
988 // (unless this is a self-move, because that would violate the
989 // char_traits::copy precondition that the ranges don't overlap).
990 if (__builtin_expect(std::__addressof(__str) != this, true))
991 {
992 if (__str.size())
993 this->_S_copy(_M_data(), __str._M_data(), __str.size());
994 _M_set_length(__str.size());
995 }
996 }
997 else if (_Alloc_traits::_S_propagate_on_move_assign() || __equal_allocs)
998 {
999 // Just move the allocated pointer, our allocator can free it.
1000 pointer __data = nullptr;
1001 size_type __capacity;
1002 if (!_M_is_local())
1003 {
1004 if (__equal_allocs)
1005 {
1006 // __str can reuse our existing storage.
1007 __data = _M_data();
1008 __capacity = _M_allocated_capacity;
1009 }
1010 else // __str can't use it, so free it.
1011 _M_destroy(_M_allocated_capacity);
1012 }
1013
1014 _M_data(__str._M_data());
1015 _M_length(__str.length());
1016 _M_capacity(__str._M_allocated_capacity);
1017 if (__data)
1018 {
1019 __str._M_data(__data);
1020 __str._M_capacity(__capacity);
1021 }
1022 else
1023 __str._M_data(__str._M_use_local_data());
1024 }
1025 else // Need to do a deep copy
1026 _M_assign(__str);
1027 __str.clear();
1028 return *this;
1029 }
1030
1031 /**
1032 * @brief Set value to string constructed from initializer %list.
1033 * @param __l std::initializer_list.
1034 */
1035 _GLIBCXX20_CONSTEXPRconstexpr
1036 basic_string&
1037 operator=(initializer_list<_CharT> __l)
1038 {
1039 this->assign(__l.begin(), __l.size());
1040 return *this;
1041 }
1042#endif // C++11
1043
1044#ifdef __glibcxx_string_view201803L // >= C++17
1045 /**
1046 * @brief Set value to string constructed from a string_view.
1047 * @param __svt An object convertible to string_view.
1048 */
1049 template<typename _Tp>
1050 _GLIBCXX20_CONSTEXPRconstexpr
1051 _If_sv<_Tp, basic_string&>
1052 operator=(const _Tp& __svt)
1053 { return this->assign(__svt); }
1054
1055 /**
1056 * @brief Convert to a string_view.
1057 * @return A string_view.
1058 */
1059 _GLIBCXX20_CONSTEXPRconstexpr
1060 operator __sv_type() const noexcept
1061 { return __sv_type(data(), size()); }
1062#endif // C++17
1063
1064 // Iterators:
1065 /**
1066 * Returns a read/write iterator that points to the first character in
1067 * the %string.
1068 */
1069 _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr
1070 iterator
1071 begin() _GLIBCXX_NOEXCEPTnoexcept
1072 { return iterator(_M_data()); }
1073
1074 /**
1075 * Returns a read-only (constant) iterator that points to the first
1076 * character in the %string.
1077 */
1078 _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr
1079 const_iterator
1080 begin() const _GLIBCXX_NOEXCEPTnoexcept
1081 { return const_iterator(_M_data()); }
1082
1083 /**
1084 * Returns a read/write iterator that points one past the last
1085 * character in the %string.
1086 */
1087 _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr
1088 iterator
1089 end() _GLIBCXX_NOEXCEPTnoexcept
1090 { return iterator(_M_data() + this->size()); }
1091
1092 /**
1093 * Returns a read-only (constant) iterator that points one past the
1094 * last character in the %string.
1095 */
1096 _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr
1097 const_iterator
1098 end() const _GLIBCXX_NOEXCEPTnoexcept
1099 { return const_iterator(_M_data() + this->size()); }
1100
1101 /**
1102 * Returns a read/write reverse iterator that points to the last
1103 * character in the %string. Iteration is done in reverse element
1104 * order.
1105 */
1106 _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr
1107 reverse_iterator
1108 rbegin() _GLIBCXX_NOEXCEPTnoexcept
1109 { return reverse_iterator(this->end()); }
1110
1111 /**
1112 * Returns a read-only (constant) reverse iterator that points
1113 * to the last character in the %string. Iteration is done in
1114 * reverse element order.
1115 */
1116 _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr
1117 const_reverse_iterator
1118 rbegin() const _GLIBCXX_NOEXCEPTnoexcept
1119 { return const_reverse_iterator(this->end()); }
1120
1121 /**
1122 * Returns a read/write reverse iterator that points to one before the
1123 * first character in the %string. Iteration is done in reverse
1124 * element order.
1125 */
1126 _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr
1127 reverse_iterator
1128 rend() _GLIBCXX_NOEXCEPTnoexcept
1129 { return reverse_iterator(this->begin()); }
1130
1131 /**
1132 * Returns a read-only (constant) reverse iterator that points
1133 * to one before the first character in the %string. Iteration
1134 * is done in reverse element order.
1135 */
1136 _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr
1137 const_reverse_iterator
1138 rend() const _GLIBCXX_NOEXCEPTnoexcept
1139 { return const_reverse_iterator(this->begin()); }
1140
1141#if __cplusplus202002L >= 201103L
1142 /**
1143 * Returns a read-only (constant) iterator that points to the first
1144 * character in the %string.
1145 */
1146 _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr
1147 const_iterator
1148 cbegin() const noexcept
1149 { return const_iterator(this->_M_data()); }
1150
1151 /**
1152 * Returns a read-only (constant) iterator that points one past the
1153 * last character in the %string.
1154 */
1155 _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr
1156 const_iterator
1157 cend() const noexcept
1158 { return const_iterator(this->_M_data() + this->size()); }
1159
1160 /**
1161 * Returns a read-only (constant) reverse iterator that points
1162 * to the last character in the %string. Iteration is done in
1163 * reverse element order.
1164 */
1165 _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr
1166 const_reverse_iterator
1167 crbegin() const noexcept
1168 { return const_reverse_iterator(this->end()); }
1169
1170 /**
1171 * Returns a read-only (constant) reverse iterator that points
1172 * to one before the first character in the %string. Iteration
1173 * is done in reverse element order.
1174 */
1175 _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr
1176 const_reverse_iterator
1177 crend() const noexcept
1178 { return const_reverse_iterator(this->begin()); }
1179#endif
1180
1181 public:
1182 // Capacity:
1183 /// Returns the number of characters in the string, not including any
1184 /// null-termination.
1185 _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr
1186 size_type
1187 size() const _GLIBCXX_NOEXCEPTnoexcept
1188 {
1189 size_type __sz = _M_string_length;
1190 if (__sz > max_size ())
1191 __builtin_unreachable();
1192 return __sz;
1193 }
1194
1195 /// Returns the number of characters in the string, not including any
1196 /// null-termination.
1197 _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr
1198 size_type
1199 length() const _GLIBCXX_NOEXCEPTnoexcept
1200 { return size(); }
1201
1202 /// Returns the size() of the largest possible %string.
1203 _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr
1204 size_type
1205 max_size() const _GLIBCXX_NOEXCEPTnoexcept
1206 {
1207 const size_t __diffmax
1208 = __gnu_cxx::__numeric_traits<ptrdiff_t>::__max / sizeof(_CharT);
1209 const size_t __allocmax = _Alloc_traits::max_size(_M_get_allocator());
1210 return (std::min)(__diffmax, __allocmax) - 1;
1211 }
1212
1213 /**
1214 * @brief Resizes the %string to the specified number of characters.
1215 * @param __n Number of characters the %string should contain.
1216 * @param __c Character to fill any new elements.
1217 *
1218 * This function will %resize the %string to the specified
1219 * number of characters. If the number is smaller than the
1220 * %string's current size the %string is truncated, otherwise
1221 * the %string is extended and new elements are %set to @a __c.
1222 */
1223 _GLIBCXX20_CONSTEXPRconstexpr
1224 void
1225 resize(size_type __n, _CharT __c);
1226
1227 /**
1228 * @brief Resizes the %string to the specified number of characters.
1229 * @param __n Number of characters the %string should contain.
1230 *
1231 * This function will resize the %string to the specified length. If
1232 * the new size is smaller than the %string's current size the %string
1233 * is truncated, otherwise the %string is extended and new characters
1234 * are default-constructed. For basic types such as char, this means
1235 * setting them to 0.
1236 */
1237 _GLIBCXX20_CONSTEXPRconstexpr
1238 void
1239 resize(size_type __n)
1240 { this->resize(__n, _CharT()); }
1241
1242#if __cplusplus202002L >= 201103L
1243#pragma GCC diagnostic push
1244#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
1245 /// A non-binding request to reduce capacity() to size().
1246 _GLIBCXX20_CONSTEXPRconstexpr
1247 void
1248 shrink_to_fit() noexcept
1249 { reserve(); }
1250#pragma GCC diagnostic pop
1251#endif
1252
1253#ifdef __glibcxx_string_resize_and_overwrite // C++ >= 23
1254 /** Resize the string and call a function to fill it.
1255 *
1256 * @param __n The maximum size requested.
1257 * @param __op A callable object that writes characters to the string.
1258 *
1259 * This is a low-level function that is easy to misuse, be careful.
1260 *
1261 * Calling `str.resize_and_overwrite(n, op)` will reserve at least `n`
1262 * characters in `str`, evaluate `n2 = std::move(op)(str.data(), n)`,
1263 * and finally set the string length to `n2` (adding a null terminator
1264 * at the end). The function object `op` is allowed to write to the
1265 * extra capacity added by the initial reserve operation, which is not
1266 * allowed if you just call `str.reserve(n)` yourself.
1267 *
1268 * This can be used to efficiently fill a `string` buffer without the
1269 * overhead of zero-initializing characters that will be overwritten
1270 * anyway.
1271 *
1272 * The callable `op` must not access the string directly (only through
1273 * the pointer passed as its first argument), must not write more than
1274 * `n` characters to the string, must return a value no greater than `n`,
1275 * and must ensure that all characters up to the returned length are
1276 * valid after it returns (i.e. there must be no uninitialized values
1277 * left in the string after the call, because accessing them would
1278 * have undefined behaviour). If `op` exits by throwing an exception
1279 * the behaviour is undefined.
1280 *
1281 * @since C++23
1282 */
1283 template<typename _Operation>
1284 constexpr void
1285 resize_and_overwrite(size_type __n, _Operation __op);
1286#endif
1287
1288#if __cplusplus202002L >= 201103L
1289 /// Non-standard version of resize_and_overwrite for C++11 and above.
1290 template<typename _Operation>
1291 _GLIBCXX20_CONSTEXPRconstexpr void
1292 __resize_and_overwrite(size_type __n, _Operation __op);
1293#endif
1294
1295 /**
1296 * Returns the total number of characters that the %string can hold
1297 * before needing to allocate more memory.
1298 */
1299 _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr
1300 size_type
1301 capacity() const _GLIBCXX_NOEXCEPTnoexcept
1302 {
1303 size_t __sz = _M_is_local() ? size_type(_S_local_capacity)
1304 : _M_allocated_capacity;
1305 if (__sz < _S_local_capacity || __sz > max_size ())
1306 __builtin_unreachable();
1307 return __sz;
1308 }
1309
1310 /**
1311 * @brief Attempt to preallocate enough memory for specified number of
1312 * characters.
1313 * @param __res_arg Number of characters required.
1314 * @throw std::length_error If @a __res_arg exceeds @c max_size().
1315 *
1316 * This function attempts to reserve enough memory for the
1317 * %string to hold the specified number of characters. If the
1318 * number requested is more than max_size(), length_error is
1319 * thrown.
1320 *
1321 * The advantage of this function is that if optimal code is a
1322 * necessity and the user can determine the string length that will be
1323 * required, the user can reserve the memory in %advance, and thus
1324 * prevent a possible reallocation of memory and copying of %string
1325 * data.
1326 */
1327 _GLIBCXX20_CONSTEXPRconstexpr
1328 void
1329 reserve(size_type __res_arg);
1330
1331 /**
1332 * Equivalent to shrink_to_fit().
1333 */
1334#if __cplusplus202002L >= 202002L
1335 [[deprecated("use shrink_to_fit() instead")]]
1336#endif
1337 _GLIBCXX20_CONSTEXPRconstexpr
1338 void
1339 reserve();
1340
1341 /**
1342 * Erases the string, making it empty.
1343 */
1344 _GLIBCXX20_CONSTEXPRconstexpr
1345 void
1346 clear() _GLIBCXX_NOEXCEPTnoexcept
1347 { _M_set_length(0); }
1348
1349 /**
1350 * Returns true if the %string is empty. Equivalent to
1351 * <code>*this == ""</code>.
1352 */
1353 _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr
1354 bool
1355 empty() const _GLIBCXX_NOEXCEPTnoexcept
1356 { return _M_string_length == 0; }
1357
1358 // Element access:
1359 /**
1360 * @brief Subscript access to the data contained in the %string.
1361 * @param __pos The index of the character to access.
1362 * @return Read-only (constant) reference to the character.
1363 *
1364 * This operator allows for easy, array-style, data access.
1365 * Note that data access with this operator is unchecked and
1366 * out_of_range lookups are not defined. (For checked lookups
1367 * see at().)
1368 */
1369 _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr
1370 const_reference
1371 operator[] (size_type __pos) const _GLIBCXX_NOEXCEPTnoexcept
1372 {
1373 __glibcxx_assert(__pos <= size())do { if (__builtin_expect(!bool(__pos <= size()), false)) std
::__glibcxx_assert_fail("/usr/lib/gcc/x86_64-linux-gnu/16/../../../../include/c++/16/bits/basic_string.h"
, 1373, __PRETTY_FUNCTION__, "__pos <= size()"); } while (
false)
;
1374 return _M_data()[__pos];
1375 }
1376
1377 /**
1378 * @brief Subscript access to the data contained in the %string.
1379 * @param __pos The index of the character to access.
1380 * @return Read/write reference to the character.
1381 *
1382 * This operator allows for easy, array-style, data access.
1383 * Note that data access with this operator is unchecked and
1384 * out_of_range lookups are not defined. (For checked lookups
1385 * see at().)
1386 */
1387 _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr
1388 reference
1389 operator[](size_type __pos)
1390 {
1391 // Allow pos == size() both in C++98 mode, as v3 extension,
1392 // and in C++11 mode.
1393 __glibcxx_assert(__pos <= size())do { if (__builtin_expect(!bool(__pos <= size()), false)) std
::__glibcxx_assert_fail("/usr/lib/gcc/x86_64-linux-gnu/16/../../../../include/c++/16/bits/basic_string.h"
, 1393, __PRETTY_FUNCTION__, "__pos <= size()"); } while (
false)
;
1394 // In pedantic mode be strict in C++98 mode.
1395 _GLIBCXX_DEBUG_PEDASSERT(__cplusplus >= 201103L || __pos < size());
1396 return _M_data()[__pos];
1397 }
1398
1399 /**
1400 * @brief Provides access to the data contained in the %string.
1401 * @param __n The index of the character to access.
1402 * @return Read-only (const) reference to the character.
1403 * @throw std::out_of_range If @a n is an invalid index.
1404 *
1405 * This function provides for safer data access. The parameter is
1406 * first checked that it is in the range of the string. The function
1407 * throws out_of_range if the check fails.
1408 */
1409 _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr
1410 const_reference
1411 at(size_type __n) const
1412 {
1413 if (__n >= this->size())
1414 __throw_out_of_range_fmt(__N("basic_string::at: __n "("basic_string::at: __n " "(which is %zu) >= this->size() "
"(which is %zu)")
1415 "(which is %zu) >= this->size() "("basic_string::at: __n " "(which is %zu) >= this->size() "
"(which is %zu)")
1416 "(which is %zu)")("basic_string::at: __n " "(which is %zu) >= this->size() "
"(which is %zu)")
,
1417 __n, this->size());
1418 return _M_data()[__n];
1419 }
1420
1421 /**
1422 * @brief Provides access to the data contained in the %string.
1423 * @param __n The index of the character to access.
1424 * @return Read/write reference to the character.
1425 * @throw std::out_of_range If @a n is an invalid index.
1426 *
1427 * This function provides for safer data access. The parameter is
1428 * first checked that it is in the range of the string. The function
1429 * throws out_of_range if the check fails.
1430 */
1431 _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr
1432 reference
1433 at(size_type __n)
1434 {
1435 if (__n >= size())
1436 __throw_out_of_range_fmt(__N("basic_string::at: __n "("basic_string::at: __n " "(which is %zu) >= this->size() "
"(which is %zu)")
1437 "(which is %zu) >= this->size() "("basic_string::at: __n " "(which is %zu) >= this->size() "
"(which is %zu)")
1438 "(which is %zu)")("basic_string::at: __n " "(which is %zu) >= this->size() "
"(which is %zu)")
,
1439 __n, this->size());
1440 return _M_data()[__n];
1441 }
1442
1443#if __cplusplus202002L >= 201103L
1444 /**
1445 * Returns a read/write reference to the data at the first
1446 * element of the %string.
1447 */
1448 _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr
1449 reference
1450 front() noexcept
1451 {
1452 __glibcxx_assert(!empty())do { if (__builtin_expect(!bool(!empty()), false)) std::__glibcxx_assert_fail
("/usr/lib/gcc/x86_64-linux-gnu/16/../../../../include/c++/16/bits/basic_string.h"
, 1452, __PRETTY_FUNCTION__, "!empty()"); } while (false)
;
1453 return operator[](0);
1454 }
1455
1456 /**
1457 * Returns a read-only (constant) reference to the data at the first
1458 * element of the %string.
1459 */
1460 _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr
1461 const_reference
1462 front() const noexcept
1463 {
1464 __glibcxx_assert(!empty())do { if (__builtin_expect(!bool(!empty()), false)) std::__glibcxx_assert_fail
("/usr/lib/gcc/x86_64-linux-gnu/16/../../../../include/c++/16/bits/basic_string.h"
, 1464, __PRETTY_FUNCTION__, "!empty()"); } while (false)
;
1465 return operator[](0);
1466 }
1467
1468 /**
1469 * Returns a read/write reference to the data at the last
1470 * element of the %string.
1471 */
1472 _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr
1473 reference
1474 back() noexcept
1475 {
1476 __glibcxx_assert(!empty())do { if (__builtin_expect(!bool(!empty()), false)) std::__glibcxx_assert_fail
("/usr/lib/gcc/x86_64-linux-gnu/16/../../../../include/c++/16/bits/basic_string.h"
, 1476, __PRETTY_FUNCTION__, "!empty()"); } while (false)
;
1477 return operator[](this->size() - 1);
1478 }
1479
1480 /**
1481 * Returns a read-only (constant) reference to the data at the
1482 * last element of the %string.
1483 */
1484 _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr
1485 const_reference
1486 back() const noexcept
1487 {
1488 __glibcxx_assert(!empty())do { if (__builtin_expect(!bool(!empty()), false)) std::__glibcxx_assert_fail
("/usr/lib/gcc/x86_64-linux-gnu/16/../../../../include/c++/16/bits/basic_string.h"
, 1488, __PRETTY_FUNCTION__, "!empty()"); } while (false)
;
1489 return operator[](this->size() - 1);
1490 }
1491#endif
1492
1493 // Modifiers:
1494 /**
1495 * @brief Append a string to this string.
1496 * @param __str The string to append.
1497 * @return Reference to this string.
1498 */
1499 _GLIBCXX20_CONSTEXPRconstexpr
1500 basic_string&
1501 operator+=(const basic_string& __str)
1502 { return this->append(__str); }
1503
1504 /**
1505 * @brief Append a C string.
1506 * @param __s The C string to append.
1507 * @return Reference to this string.
1508 */
1509 _GLIBCXX20_CONSTEXPRconstexpr
1510 basic_string&
1511 operator+=(const _CharT* __s)
1512 { return this->append(__s); }
1513
1514 /**
1515 * @brief Append a character.
1516 * @param __c The character to append.
1517 * @return Reference to this string.
1518 */
1519 _GLIBCXX20_CONSTEXPRconstexpr
1520 basic_string&
1521 operator+=(_CharT __c)
1522 {
1523 this->push_back(__c);
1524 return *this;
1525 }
1526
1527#if __cplusplus202002L >= 201103L
1528 /**
1529 * @brief Append an initializer_list of characters.
1530 * @param __l The initializer_list of characters to be appended.
1531 * @return Reference to this string.
1532 */
1533 _GLIBCXX20_CONSTEXPRconstexpr
1534 basic_string&
1535 operator+=(initializer_list<_CharT> __l)
1536 { return this->append(__l.begin(), __l.size()); }
1537#endif // C++11
1538
1539#ifdef __glibcxx_string_view201803L // >= C++17
1540 /**
1541 * @brief Append a string_view.
1542 * @param __svt An object convertible to string_view to be appended.
1543 * @return Reference to this string.
1544 */
1545 template<typename _Tp>
1546 _GLIBCXX20_CONSTEXPRconstexpr
1547 _If_sv<_Tp, basic_string&>
1548 operator+=(const _Tp& __svt)
1549 { return this->append(__svt); }
1550#endif // C++17
1551
1552 /**
1553 * @brief Append a string to this string.
1554 * @param __str The string to append.
1555 * @return Reference to this string.
1556 */
1557 _GLIBCXX20_CONSTEXPRconstexpr
1558 basic_string&
1559 append(const basic_string& __str)
1560 { return this->append(__str._M_data(), __str.size()); }
1561
1562 /**
1563 * @brief Append a substring.
1564 * @param __str The string to append.
1565 * @param __pos Index of the first character of str to append.
1566 * @param __n The number of characters to append.
1567 * @return Reference to this string.
1568 * @throw std::out_of_range if @a __pos is not a valid index.
1569 *
1570 * This function appends @a __n characters from @a __str
1571 * starting at @a __pos to this string. If @a __n is is larger
1572 * than the number of available characters in @a __str, the
1573 * remainder of @a __str is appended.
1574 */
1575 _GLIBCXX20_CONSTEXPRconstexpr
1576 basic_string&
1577 append(const basic_string& __str, size_type __pos, size_type __n = npos)
1578 { return this->append(__str._M_data()
1579 + __str._M_check(__pos, "basic_string::append"),
1580 __str._M_limit(__pos, __n)); }
1581
1582 /**
1583 * @brief Append a C substring.
1584 * @param __s The C string to append.
1585 * @param __n The number of characters to append.
1586 * @return Reference to this string.
1587 */
1588 _GLIBCXX20_CONSTEXPRconstexpr
1589 basic_string&
1590 append(const _CharT* __s, size_type __n)
1591 {
1592 __glibcxx_requires_string_len(__s, __n);
1593 _M_check_length(size_type(0), __n, "basic_string::append");
1594 return _M_append(__s, __n);
1595 }
1596
1597 /**
1598 * @brief Append a C string.
1599 * @param __s The C string to append.
1600 * @return Reference to this string.
1601 */
1602 _GLIBCXX20_CONSTEXPRconstexpr
1603 basic_string&
1604 append(const _CharT* __s)
1605 {
1606 __glibcxx_requires_string(__s);
1607 const size_type __n = traits_type::length(__s);
1608 _M_check_length(size_type(0), __n, "basic_string::append");
1609 return _M_append(__s, __n);
1610 }
1611
1612 /**
1613 * @brief Append multiple characters.
1614 * @param __n The number of characters to append.
1615 * @param __c The character to use.
1616 * @return Reference to this string.
1617 *
1618 * Appends __n copies of __c to this string.
1619 */
1620 _GLIBCXX20_CONSTEXPRconstexpr
1621 basic_string&
1622 append(size_type __n, _CharT __c)
1623 { return _M_replace_aux(this->size(), size_type(0), __n, __c); }
1624
1625#if __glibcxx_containers_ranges // C++ >= 23
1626 /**
1627 * @brief Append a range to the string.
1628 * @param __rg A range of values that are convertible to `value_type`.
1629 * @since C++23
1630 *
1631 * The range `__rg` is allowed to overlap with `*this`.
1632 */
1633 template<__detail::__container_compatible_range<_CharT> _Rg>
1634 constexpr basic_string&
1635 append_range(_Rg&& __rg)
1636 {
1637 // N.B. __rg may overlap with *this, so we must copy from __rg before
1638 // existing elements or iterators referring to *this are invalidated.
1639 // e.g. in s.append_range(views::concat(s, str)), rg overlaps s.
1640 if constexpr (ranges::forward_range<_Rg> || ranges::sized_range<_Rg>)
1641 {
1642 const auto __len = size_type(ranges::distance(__rg));
1643
1644 // Don't care if this addition wraps around, we check it below:
1645 const size_type __newlen = size() + __len;
1646
1647 if ((capacity() - size()) >= __len)
1648 _S_copy_range(_M_data() + size(), std::forward<_Rg>(__rg),
1649 __len);
1650 else
1651 {
1652 _M_check_length(0, __len, "basic_string::append_range");
1653 basic_string __s(_M_get_allocator());
1654 __s.reserve(__newlen);
1655 _S_copy_range(__s._M_data() + size(), std::forward<_Rg>(__rg),
1656 __len);
1657 _S_copy(__s._M_data(), _M_data(), size());
1658 if (!_M_is_local())
1659 _M_destroy(_M_allocated_capacity);
1660 _M_data(__s._M_data());
1661 _M_capacity(__s._M_allocated_capacity);
1662 __s._M_data(__s._M_local_data());
1663 __s._M_length(0);
1664 }
1665 _M_set_length(__newlen); // adds null-terminator
1666 }
1667 else
1668 {
1669 basic_string __s(from_range, std::forward<_Rg>(__rg),
1670 _M_get_allocator());
1671 append(__s);
1672 }
1673 return *this;
1674 }
1675#endif
1676
1677#if __cplusplus202002L >= 201103L
1678 /**
1679 * @brief Append an initializer_list of characters.
1680 * @param __l The initializer_list of characters to append.
1681 * @return Reference to this string.
1682 */
1683 _GLIBCXX20_CONSTEXPRconstexpr
1684 basic_string&
1685 append(initializer_list<_CharT> __l)
1686 { return this->append(__l.begin(), __l.size()); }
1687#endif // C++11
1688
1689 /**
1690 * @brief Append a range of characters.
1691 * @param __first Iterator referencing the first character to append.
1692 * @param __last Iterator marking the end of the range.
1693 * @return Reference to this string.
1694 *
1695 * Appends characters in the range [__first,__last) to this string.
1696 */
1697#if __cplusplus202002L >= 201103L
1698 template<class _InputIterator,
1699 typename = std::_RequireInputIter<_InputIterator>>
1700 _GLIBCXX20_CONSTEXPRconstexpr
1701#else
1702 template<class _InputIterator>
1703#endif
1704 basic_string&
1705 append(_InputIterator __first, _InputIterator __last)
1706 { return this->replace(end(), end(), __first, __last); }
1707
1708#ifdef __glibcxx_string_view201803L
1709 /**
1710 * @brief Append a string_view.
1711 * @param __svt An object convertible to string_view to be appended.
1712 * @return Reference to this string.
1713 */
1714 template<typename _Tp>
1715 _GLIBCXX20_CONSTEXPRconstexpr
1716 _If_sv<_Tp, basic_string&>
1717 append(const _Tp& __svt)
1718 {
1719 __sv_type __sv = __svt;
1720 return this->append(__sv.data(), __sv.size());
1721 }
1722
1723 /**
1724 * @brief Append a range of characters from a string_view.
1725 * @param __svt An object convertible to string_view to be appended from.
1726 * @param __pos The position in the string_view to append from.
1727 * @param __n The number of characters to append from the string_view.
1728 * @return Reference to this string.
1729 */
1730 template<typename _Tp>
1731 _GLIBCXX20_CONSTEXPRconstexpr
1732 _If_sv<_Tp, basic_string&>
1733 append(const _Tp& __svt, size_type __pos, size_type __n = npos)
1734 {
1735 __sv_type __sv = __svt;
1736 return _M_append(__sv.data()
1737 + std::__sv_check(__sv.size(), __pos, "basic_string::append"),
1738 std::__sv_limit(__sv.size(), __pos, __n));
1739 }
1740#endif // C++17
1741
1742 /**
1743 * @brief Append a single character.
1744 * @param __c Character to append.
1745 */
1746 _GLIBCXX20_CONSTEXPRconstexpr
1747 void
1748 push_back(_CharT __c)
1749 {
1750 const size_type __size = this->size();
1751 if (__size + 1 > this->capacity())
1752 this->_M_mutate(__size, size_type(0), 0, size_type(1));
1753 traits_type::assign(this->_M_data()[__size], __c);
1754 this->_M_set_length(__size + 1);
1755 }
1756
1757 /**
1758 * @brief Set value to contents of another string.
1759 * @param __str Source string to use.
1760 * @return Reference to this string.
1761 */
1762 _GLIBCXX20_CONSTEXPRconstexpr
1763 basic_string&
1764 assign(const basic_string& __str)
1765 {
1766#if __cplusplus202002L >= 201103L
1767 if (_Alloc_traits::_S_propagate_on_copy_assign())
1768 {
1769 if (!_Alloc_traits::_S_always_equal() && !_M_is_local()
1770 && _M_get_allocator() != __str._M_get_allocator())
1771 {
1772 // Propagating allocator cannot free existing storage so must
1773 // deallocate it before replacing current allocator.
1774 if (__str.size() <= _S_local_capacity)
1775 {
1776 _M_destroy(_M_allocated_capacity);
1777 _M_data(_M_use_local_data());
1778 _M_set_length(0);
1779 }
1780 else
1781 {
1782 const auto __len = __str.size();
1783 auto __alloc = __str._M_get_allocator();
1784 // If this allocation throws there are no effects:
1785 auto __ptr = _S_allocate(__alloc, __len + 1);
1786 _M_destroy(_M_allocated_capacity);
1787 _M_data(__ptr);
1788 _M_capacity(__len);
1789 _M_set_length(__len);
1790 }
1791 }
1792 std::__alloc_on_copy(_M_get_allocator(), __str._M_get_allocator());
1793 }
1794#endif
1795 this->_M_assign(__str);
1796 return *this;
1797 }
1798
1799#if __cplusplus202002L >= 201103L
1800 /**
1801 * @brief Set value to contents of another string.
1802 * @param __str Source string to use.
1803 * @return Reference to this string.
1804 *
1805 * This function sets this string to the exact contents of @a __str.
1806 * @a __str is a valid, but unspecified string.
1807 */
1808 _GLIBCXX20_CONSTEXPRconstexpr
1809 basic_string&
1810 assign(basic_string&& __str)
1811 noexcept(_Alloc_traits::_S_nothrow_move())
1812 {
1813 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1814 // 2063. Contradictory requirements for string move assignment
1815 return *this = std::move(__str);
1816 }
1817#endif // C++11
1818
1819 /**
1820 * @brief Set value to a substring of a string.
1821 * @param __str The string to use.
1822 * @param __pos Index of the first character of str.
1823 * @param __n Number of characters to use.
1824 * @return Reference to this string.
1825 * @throw std::out_of_range if @a pos is not a valid index.
1826 *
1827 * This function sets this string to the substring of @a __str
1828 * consisting of @a __n characters at @a __pos. If @a __n is
1829 * is larger than the number of available characters in @a
1830 * __str, the remainder of @a __str is used.
1831 */
1832 _GLIBCXX20_CONSTEXPRconstexpr
1833 basic_string&
1834 assign(const basic_string& __str, size_type __pos, size_type __n = npos)
1835 { return _M_replace(size_type(0), this->size(), __str._M_data()
1836 + __str._M_check(__pos, "basic_string::assign"),
1837 __str._M_limit(__pos, __n)); }
1838
1839 /**
1840 * @brief Set value to a C substring.
1841 * @param __s The C string to use.
1842 * @param __n Number of characters to use.
1843 * @return Reference to this string.
1844 *
1845 * This function sets the value of this string to the first @a __n
1846 * characters of @a __s. If @a __n is is larger than the number of
1847 * available characters in @a __s, the remainder of @a __s is used.
1848 */
1849 _GLIBCXX20_CONSTEXPRconstexpr
1850 basic_string&
1851 assign(const _CharT* __s, size_type __n)
1852 {
1853 __glibcxx_requires_string_len(__s, __n);
1854 return _M_replace(size_type(0), this->size(), __s, __n);
1855 }
1856
1857 /**
1858 * @brief Set value to contents of a C string.
1859 * @param __s The C string to use.
1860 * @return Reference to this string.
1861 *
1862 * This function sets the value of this string to the value of @a __s.
1863 * The data is copied, so there is no dependence on @a __s once the
1864 * function returns.
1865 */
1866 _GLIBCXX20_CONSTEXPRconstexpr
1867 basic_string&
1868 assign(const _CharT* __s)
1869 {
1870 __glibcxx_requires_string(__s);
1871 return _M_replace(size_type(0), this->size(), __s,
1872 traits_type::length(__s));
1873 }
1874
1875 /**
1876 * @brief Set value to multiple characters.
1877 * @param __n Length of the resulting string.
1878 * @param __c The character to use.
1879 * @return Reference to this string.
1880 *
1881 * This function sets the value of this string to @a __n copies of
1882 * character @a __c.
1883 */
1884 _GLIBCXX20_CONSTEXPRconstexpr
1885 basic_string&
1886 assign(size_type __n, _CharT __c)
1887 { return _M_replace_aux(size_type(0), this->size(), __n, __c); }
1888
1889 /**
1890 * @brief Set value to a range of characters.
1891 * @param __first Iterator referencing the first character to append.
1892 * @param __last Iterator marking the end of the range.
1893 * @return Reference to this string.
1894 *
1895 * Sets value of string to characters in the range [__first,__last).
1896 */
1897#if __cplusplus202002L >= 201103L
1898#pragma GCC diagnostic push
1899#pragma GCC diagnostic ignored "-Wc++17-extensions"
1900 template<class _InputIterator,
1901 typename = std::_RequireInputIter<_InputIterator>>
1902 _GLIBCXX20_CONSTEXPRconstexpr
1903 basic_string&
1904 assign(_InputIterator __first, _InputIterator __last)
1905 {
1906 using _IterTraits = iterator_traits<_InputIterator>;
1907 if constexpr (is_pointer<decltype(std::__niter_base(__first))>::value
1908 && is_same<typename _IterTraits::value_type,
1909 _CharT>::value)
1910 {
1911 __glibcxx_requires_valid_range(__first, __last);
1912 return _M_replace(size_type(0), size(),
1913 std::__niter_base(__first), __last - __first);
1914 }
1915#if __cplusplus202002L >= 202002L
1916 else if constexpr (contiguous_iterator<_InputIterator>
1917 && is_same_v<iter_value_t<_InputIterator>,
1918 _CharT>)
1919 {
1920 __glibcxx_requires_valid_range(__first, __last);
1921 return _M_replace(size_type(0), size(),
1922 std::to_address(__first), __last - __first);
1923 }
1924#endif
1925 else
1926 return *this = basic_string(__first, __last, get_allocator());
1927 }
1928#pragma GCC diagnostic pop
1929#else
1930 template<class _InputIterator>
1931 basic_string&
1932 assign(_InputIterator __first, _InputIterator __last)
1933 { return this->replace(begin(), end(), __first, __last); }
1934#endif
1935
1936#if __glibcxx_containers_ranges // C++ >= 23
1937 /**
1938 * @brief Assign a range to the string.
1939 * @param __rg A range of values that are convertible to `value_type`.
1940 * @since C++23
1941 *
1942 * The range `__rg` is allowed to overlap with `*this`.
1943 */
1944 template<__detail::__container_compatible_range<_CharT> _Rg>
1945 constexpr basic_string&
1946 assign_range(_Rg&& __rg)
1947 {
1948 basic_string __s(from_range, std::forward<_Rg>(__rg),
1949 _M_get_allocator());
1950 assign(std::move(__s));
1951 return *this;
1952 }
1953#endif
1954
1955#if __cplusplus202002L >= 201103L
1956 /**
1957 * @brief Set value to an initializer_list of characters.
1958 * @param __l The initializer_list of characters to assign.
1959 * @return Reference to this string.
1960 */
1961 _GLIBCXX20_CONSTEXPRconstexpr
1962 basic_string&
1963 assign(initializer_list<_CharT> __l)
1964 {
1965 // The initializer_list array cannot alias the characters in *this
1966 // so we don't need to use replace to that case.
1967 const size_type __n = __l.size();
1968 if (__n > capacity())
1969 *this = basic_string(__l.begin(), __l.end(), get_allocator());
1970 else
1971 {
1972 if (__n)
1973 _S_copy(_M_data(), __l.begin(), __n);
1974 _M_set_length(__n);
1975 }
1976 return *this;
1977 }
1978#endif // C++11
1979
1980#ifdef __glibcxx_string_view201803L // >= C++17
1981 /**
1982 * @brief Set value from a string_view.
1983 * @param __svt The source object convertible to string_view.
1984 * @return Reference to this string.
1985 */
1986 template<typename _Tp>
1987 _GLIBCXX20_CONSTEXPRconstexpr
1988 _If_sv<_Tp, basic_string&>
1989 assign(const _Tp& __svt)
1990 {
1991 __sv_type __sv = __svt;
1992 return this->assign(__sv.data(), __sv.size());
1993 }
1994
1995 /**
1996 * @brief Set value from a range of characters in a string_view.
1997 * @param __svt The source object convertible to string_view.
1998 * @param __pos The position in the string_view to assign from.
1999 * @param __n The number of characters to assign.
2000 * @return Reference to this string.
2001 */
2002 template<typename _Tp>
2003 _GLIBCXX20_CONSTEXPRconstexpr
2004 _If_sv<_Tp, basic_string&>
2005 assign(const _Tp& __svt, size_type __pos, size_type __n = npos)
2006 {
2007 __sv_type __sv = __svt;
2008 return _M_replace(size_type(0), this->size(),
2009 __sv.data()
2010 + std::__sv_check(__sv.size(), __pos, "basic_string::assign"),
2011 std::__sv_limit(__sv.size(), __pos, __n));
2012 }
2013#endif // C++17
2014
2015#if __cplusplus202002L >= 201103L
2016 /**
2017 * @brief Insert multiple characters.
2018 * @param __p Const_iterator referencing location in string to
2019 * insert at.
2020 * @param __n Number of characters to insert
2021 * @param __c The character to insert.
2022 * @return Iterator referencing the first inserted char.
2023 * @throw std::length_error If new length exceeds @c max_size().
2024 *
2025 * Inserts @a __n copies of character @a __c starting at the
2026 * position referenced by iterator @a __p. If adding
2027 * characters causes the length to exceed max_size(),
2028 * length_error is thrown. The value of the string doesn't
2029 * change if an error is thrown.
2030 */
2031 _GLIBCXX20_CONSTEXPRconstexpr
2032 iterator
2033 insert(const_iterator __p, size_type __n, _CharT __c)
2034 {
2035 _GLIBCXX_DEBUG_PEDASSERT(__p >= begin() && __p <= end());
2036 const size_type __pos = __p - begin();
2037 this->replace(__p, __p, __n, __c);
2038 return iterator(this->_M_data() + __pos);
2039 }
2040#else
2041 /**
2042 * @brief Insert multiple characters.
2043 * @param __p Iterator referencing location in string to insert at.
2044 * @param __n Number of characters to insert
2045 * @param __c The character to insert.
2046 * @throw std::length_error If new length exceeds @c max_size().
2047 *
2048 * Inserts @a __n copies of character @a __c starting at the
2049 * position referenced by iterator @a __p. If adding
2050 * characters causes the length to exceed max_size(),
2051 * length_error is thrown. The value of the string doesn't
2052 * change if an error is thrown.
2053 */
2054 void
2055 insert(iterator __p, size_type __n, _CharT __c)
2056 { this->replace(__p, __p, __n, __c); }
2057#endif
2058
2059#if __cplusplus202002L >= 201103L
2060 /**
2061 * @brief Insert a range of characters.
2062 * @param __p Const_iterator referencing location in string to
2063 * insert at.
2064 * @param __beg Start of range.
2065 * @param __end End of range.
2066 * @return Iterator referencing the first inserted char.
2067 * @throw std::length_error If new length exceeds @c max_size().
2068 *
2069 * Inserts characters in range [beg,end). If adding characters
2070 * causes the length to exceed max_size(), length_error is
2071 * thrown. The value of the string doesn't change if an error
2072 * is thrown.
2073 */
2074 template<class _InputIterator,
2075 typename = std::_RequireInputIter<_InputIterator>>
2076 _GLIBCXX20_CONSTEXPRconstexpr
2077 iterator
2078 insert(const_iterator __p, _InputIterator __beg, _InputIterator __end)
2079 {
2080 _GLIBCXX_DEBUG_PEDASSERT(__p >= begin() && __p <= end());
2081 const size_type __pos = __p - begin();
2082 this->replace(__p, __p, __beg, __end);
2083 return iterator(this->_M_data() + __pos);
2084 }
2085#else
2086 /**
2087 * @brief Insert a range of characters.
2088 * @param __p Iterator referencing location in string to insert at.
2089 * @param __beg Start of range.
2090 * @param __end End of range.
2091 * @throw std::length_error If new length exceeds @c max_size().
2092 *
2093 * Inserts characters in range [__beg,__end). If adding
2094 * characters causes the length to exceed max_size(),
2095 * length_error is thrown. The value of the string doesn't
2096 * change if an error is thrown.
2097 */
2098 template<class _InputIterator>
2099 void
2100 insert(iterator __p, _InputIterator __beg, _InputIterator __end)
2101 { this->replace(__p, __p, __beg, __end); }
2102#endif
2103
2104#if __glibcxx_containers_ranges // C++ >= 23
2105 /**
2106 * @brief Insert a range into the string.
2107 * @param __rg A range of values that are convertible to `value_type`.
2108 * @since C++23
2109 *
2110 * The range `__rg` is allowed to overlap with `*this`.
2111 */
2112 template<__detail::__container_compatible_range<_CharT> _Rg>
2113 constexpr iterator
2114 insert_range(const_iterator __p, _Rg&& __rg)
2115 {
2116 auto __pos = __p - cbegin();
2117
2118 if constexpr (ranges::forward_range<_Rg>)
2119 if (ranges::empty(__rg))
2120 return begin() + __pos;
2121
2122
2123 if (__p == cend())
2124 append_range(std::forward<_Rg>(__rg));
2125 else
2126 {
2127 basic_string __s(from_range, std::forward<_Rg>(__rg),
2128 _M_get_allocator());
2129 insert(__pos, __s);
2130 }
2131 return begin() + __pos;
2132 }
2133#endif
2134
2135#if __cplusplus202002L >= 201103L
2136 /**
2137 * @brief Insert an initializer_list of characters.
2138 * @param __p Iterator referencing location in string to insert at.
2139 * @param __l The initializer_list of characters to insert.
2140 * @throw std::length_error If new length exceeds @c max_size().
2141 */
2142 _GLIBCXX20_CONSTEXPRconstexpr
2143 iterator
2144 insert(const_iterator __p, initializer_list<_CharT> __l)
2145 { return this->insert(__p, __l.begin(), __l.end()); }
2146
2147#ifdef _GLIBCXX_DEFINING_STRING_INSTANTIATIONS
2148 // See PR libstdc++/83328
2149 void
2150 insert(iterator __p, initializer_list<_CharT> __l)
2151 {
2152 _GLIBCXX_DEBUG_PEDASSERT(__p >= begin() && __p <= end());
2153 this->insert(__p - begin(), __l.begin(), __l.size());
2154 }
2155#endif
2156#endif // C++11
2157
2158 /**
2159 * @brief Insert value of a string.
2160 * @param __pos1 Position in string to insert at.
2161 * @param __str The string to insert.
2162 * @return Reference to this string.
2163 * @throw std::length_error If new length exceeds @c max_size().
2164 *
2165 * Inserts value of @a __str starting at @a __pos1. If adding
2166 * characters causes the length to exceed max_size(),
2167 * length_error is thrown. The value of the string doesn't
2168 * change if an error is thrown.
2169 */
2170 _GLIBCXX20_CONSTEXPRconstexpr
2171 basic_string&
2172 insert(size_type __pos1, const basic_string& __str)
2173 { return this->replace(__pos1, size_type(0),
2174 __str._M_data(), __str.size()); }
2175
2176 /**
2177 * @brief Insert a substring.
2178 * @param __pos1 Position in string to insert at.
2179 * @param __str The string to insert.
2180 * @param __pos2 Start of characters in str to insert.
2181 * @param __n Number of characters to insert.
2182 * @return Reference to this string.
2183 * @throw std::length_error If new length exceeds @c max_size().
2184 * @throw std::out_of_range If @a pos1 > size() or
2185 * @a __pos2 > @a str.size().
2186 *
2187 * Starting at @a pos1, insert @a __n character of @a __str
2188 * beginning with @a __pos2. If adding characters causes the
2189 * length to exceed max_size(), length_error is thrown. If @a
2190 * __pos1 is beyond the end of this string or @a __pos2 is
2191 * beyond the end of @a __str, out_of_range is thrown. The
2192 * value of the string doesn't change if an error is thrown.
2193 */
2194 _GLIBCXX20_CONSTEXPRconstexpr
2195 basic_string&
2196 insert(size_type __pos1, const basic_string& __str,
2197 size_type __pos2, size_type __n = npos)
2198 { return this->replace(__pos1, size_type(0), __str._M_data()
2199 + __str._M_check(__pos2, "basic_string::insert"),
2200 __str._M_limit(__pos2, __n)); }
2201
2202 /**
2203 * @brief Insert a C substring.
2204 * @param __pos Position in string to insert at.
2205 * @param __s The C string to insert.
2206 * @param __n The number of characters to insert.
2207 * @return Reference to this string.
2208 * @throw std::length_error If new length exceeds @c max_size().
2209 * @throw std::out_of_range If @a __pos is beyond the end of this
2210 * string.
2211 *
2212 * Inserts the first @a __n characters of @a __s starting at @a
2213 * __pos. If adding characters causes the length to exceed
2214 * max_size(), length_error is thrown. If @a __pos is beyond
2215 * end(), out_of_range is thrown. The value of the string
2216 * doesn't change if an error is thrown.
2217 */
2218 _GLIBCXX20_CONSTEXPRconstexpr
2219 basic_string&
2220 insert(size_type __pos, const _CharT* __s, size_type __n)
2221 { return this->replace(__pos, size_type(0), __s, __n); }
2222
2223 /**
2224 * @brief Insert a C string.
2225 * @param __pos Position in string to insert at.
2226 * @param __s The C string to insert.
2227 * @return Reference to this string.
2228 * @throw std::length_error If new length exceeds @c max_size().
2229 * @throw std::out_of_range If @a pos is beyond the end of this
2230 * string.
2231 *
2232 * Inserts the first @a n characters of @a __s starting at @a __pos. If
2233 * adding characters causes the length to exceed max_size(),
2234 * length_error is thrown. If @a __pos is beyond end(), out_of_range is
2235 * thrown. The value of the string doesn't change if an error is
2236 * thrown.
2237 */
2238 _GLIBCXX20_CONSTEXPRconstexpr
2239 basic_string&
2240 insert(size_type __pos, const _CharT* __s)
2241 {
2242 __glibcxx_requires_string(__s);
2243 return this->replace(__pos, size_type(0), __s,
2244 traits_type::length(__s));
2245 }
2246
2247 /**
2248 * @brief Insert multiple characters.
2249 * @param __pos Index in string to insert at.
2250 * @param __n Number of characters to insert
2251 * @param __c The character to insert.
2252 * @return Reference to this string.
2253 * @throw std::length_error If new length exceeds @c max_size().
2254 * @throw std::out_of_range If @a __pos is beyond the end of this
2255 * string.
2256 *
2257 * Inserts @a __n copies of character @a __c starting at index
2258 * @a __pos. If adding characters causes the length to exceed
2259 * max_size(), length_error is thrown. If @a __pos > length(),
2260 * out_of_range is thrown. The value of the string doesn't
2261 * change if an error is thrown.
2262 */
2263 _GLIBCXX20_CONSTEXPRconstexpr
2264 basic_string&
2265 insert(size_type __pos, size_type __n, _CharT __c)
2266 { return _M_replace_aux(_M_check(__pos, "basic_string::insert"),
2267 size_type(0), __n, __c); }
2268
2269 /**
2270 * @brief Insert one character.
2271 * @param __p Iterator referencing position in string to insert at.
2272 * @param __c The character to insert.
2273 * @return Iterator referencing newly inserted char.
2274 * @throw std::length_error If new length exceeds @c max_size().
2275 *
2276 * Inserts character @a __c at position referenced by @a __p.
2277 * If adding character causes the length to exceed max_size(),
2278 * length_error is thrown. If @a __p is beyond end of string,
2279 * out_of_range is thrown. The value of the string doesn't
2280 * change if an error is thrown.
2281 */
2282 _GLIBCXX20_CONSTEXPRconstexpr
2283 iterator
2284 insert(__const_iterator __p, _CharT __c)
2285 {
2286 _GLIBCXX_DEBUG_PEDASSERT(__p >= begin() && __p <= end());
2287 const size_type __pos = __p - begin();
2288 _M_replace_aux(__pos, size_type(0), size_type(1), __c);
2289 return iterator(_M_data() + __pos);
2290 }
2291
2292#ifdef __glibcxx_string_view201803L // >= C++17
2293 /**
2294 * @brief Insert a string_view.
2295 * @param __pos Position in string to insert at.
2296 * @param __svt The object convertible to string_view to insert.
2297 * @return Reference to this string.
2298 */
2299 template<typename _Tp>
2300 _GLIBCXX20_CONSTEXPRconstexpr
2301 _If_sv<_Tp, basic_string&>
2302 insert(size_type __pos, const _Tp& __svt)
2303 {
2304 __sv_type __sv = __svt;
2305 return this->insert(__pos, __sv.data(), __sv.size());
2306 }
2307
2308 /**
2309 * @brief Insert a string_view.
2310 * @param __pos1 Position in string to insert at.
2311 * @param __svt The object convertible to string_view to insert from.
2312 * @param __pos2 Start of characters in str to insert.
2313 * @param __n The number of characters to insert.
2314 * @return Reference to this string.
2315 */
2316 template<typename _Tp>
2317 _GLIBCXX20_CONSTEXPRconstexpr
2318 _If_sv<_Tp, basic_string&>
2319 insert(size_type __pos1, const _Tp& __svt,
2320 size_type __pos2, size_type __n = npos)
2321 {
2322 __sv_type __sv = __svt;
2323 return this->replace(__pos1, size_type(0),
2324 __sv.data()
2325 + std::__sv_check(__sv.size(), __pos2, "basic_string::insert"),
2326 std::__sv_limit(__sv.size(), __pos2, __n));
2327 }
2328#endif // C++17
2329
2330 /**
2331 * @brief Remove characters.
2332 * @param __pos Index of first character to remove (default 0).
2333 * @param __n Number of characters to remove (default remainder).
2334 * @return Reference to this string.
2335 * @throw std::out_of_range If @a pos is beyond the end of this
2336 * string.
2337 *
2338 * Removes @a __n characters from this string starting at @a
2339 * __pos. The length of the string is reduced by @a __n. If
2340 * there are < @a __n characters to remove, the remainder of
2341 * the string is truncated. If @a __p is beyond end of string,
2342 * out_of_range is thrown. The value of the string doesn't
2343 * change if an error is thrown.
2344 */
2345 _GLIBCXX20_CONSTEXPRconstexpr
2346 basic_string&
2347 erase(size_type __pos = 0, size_type __n = npos)
2348 {
2349 _M_check(__pos, "basic_string::erase");
2350 if (__n == npos)
2351 this->_M_set_length(__pos);
2352 else if (__n != 0)
2353 this->_M_erase(__pos, _M_limit(__pos, __n));
2354 return *this;
2355 }
2356
2357 /**
2358 * @brief Remove one character.
2359 * @param __position Iterator referencing the character to remove.
2360 * @return iterator referencing same location after removal.
2361 *
2362 * Removes the character at @a __position from this string. The value
2363 * of the string doesn't change if an error is thrown.
2364 */
2365 _GLIBCXX20_CONSTEXPRconstexpr
2366 iterator
2367 erase(__const_iterator __position)
2368 {
2369 _GLIBCXX_DEBUG_PEDASSERT(__position >= begin()
2370 && __position < end());
2371 const size_type __pos = __position - begin();
2372 this->_M_erase(__pos, size_type(1));
2373 return iterator(_M_data() + __pos);
2374 }
2375
2376 /**
2377 * @brief Remove a range of characters.
2378 * @param __first Iterator referencing the first character to remove.
2379 * @param __last Iterator referencing the end of the range.
2380 * @return Iterator referencing location of first after removal.
2381 *
2382 * Removes the characters in the range [first,last) from this string.
2383 * The value of the string doesn't change if an error is thrown.
2384 */
2385 _GLIBCXX20_CONSTEXPRconstexpr
2386 iterator
2387 erase(__const_iterator __first, __const_iterator __last)
2388 {
2389 _GLIBCXX_DEBUG_PEDASSERT(__first >= begin() && __first <= __last
2390 && __last <= end());
2391 const size_type __pos = __first - begin();
2392 if (__last == end())
2393 this->_M_set_length(__pos);
2394 else
2395 this->_M_erase(__pos, __last - __first);
2396 return iterator(this->_M_data() + __pos);
2397 }
2398
2399#if __cplusplus202002L >= 201103L
2400 /**
2401 * @brief Remove the last character.
2402 *
2403 * The string must be non-empty.
2404 */
2405 _GLIBCXX20_CONSTEXPRconstexpr
2406 void
2407 pop_back() noexcept
2408 {
2409 __glibcxx_assert(!empty())do { if (__builtin_expect(!bool(!empty()), false)) std::__glibcxx_assert_fail
("/usr/lib/gcc/x86_64-linux-gnu/16/../../../../include/c++/16/bits/basic_string.h"
, 2409, __PRETTY_FUNCTION__, "!empty()"); } while (false)
;
2410 _M_erase(size() - 1, 1);
2411 }
2412#endif // C++11
2413
2414 /**
2415 * @brief Replace characters with value from another string.
2416 * @param __pos Index of first character to replace.
2417 * @param __n Number of characters to be replaced.
2418 * @param __str String to insert.
2419 * @return Reference to this string.
2420 * @throw std::out_of_range If @a pos is beyond the end of this
2421 * string.
2422 * @throw std::length_error If new length exceeds @c max_size().
2423 *
2424 * Removes the characters in the range [__pos,__pos+__n) from
2425 * this string. In place, the value of @a __str is inserted.
2426 * If @a __pos is beyond end of string, out_of_range is thrown.
2427 * If the length of the result exceeds max_size(), length_error
2428 * is thrown. The value of the string doesn't change if an
2429 * error is thrown.
2430 */
2431 _GLIBCXX20_CONSTEXPRconstexpr
2432 basic_string&
2433 replace(size_type __pos, size_type __n, const basic_string& __str)
2434 { return this->replace(__pos, __n, __str._M_data(), __str.size()); }
2435
2436 /**
2437 * @brief Replace characters with value from another string.
2438 * @param __pos1 Index of first character to replace.
2439 * @param __n1 Number of characters to be replaced.
2440 * @param __str String to insert.
2441 * @param __pos2 Index of first character of str to use.
2442 * @param __n2 Number of characters from str to use.
2443 * @return Reference to this string.
2444 * @throw std::out_of_range If @a __pos1 > size() or @a __pos2 >
2445 * __str.size().
2446 * @throw std::length_error If new length exceeds @c max_size().
2447 *
2448 * Removes the characters in the range [__pos1,__pos1 + n) from this
2449 * string. In place, the value of @a __str is inserted. If @a __pos is
2450 * beyond end of string, out_of_range is thrown. If the length of the
2451 * result exceeds max_size(), length_error is thrown. The value of the
2452 * string doesn't change if an error is thrown.
2453 */
2454 _GLIBCXX20_CONSTEXPRconstexpr
2455 basic_string&
2456 replace(size_type __pos1, size_type __n1, const basic_string& __str,
2457 size_type __pos2, size_type __n2 = npos)
2458 { return this->replace(__pos1, __n1, __str._M_data()
2459 + __str._M_check(__pos2, "basic_string::replace"),
2460 __str._M_limit(__pos2, __n2)); }
2461
2462 /**
2463 * @brief Replace characters with value of a C substring.
2464 * @param __pos Index of first character to replace.
2465 * @param __n1 Number of characters to be replaced.
2466 * @param __s C string to insert.
2467 * @param __n2 Number of characters from @a s to use.
2468 * @return Reference to this string.
2469 * @throw std::out_of_range If @a pos1 > size().
2470 * @throw std::length_error If new length exceeds @c max_size().
2471 *
2472 * Removes the characters in the range [__pos,__pos + __n1)
2473 * from this string. In place, the first @a __n2 characters of
2474 * @a __s are inserted, or all of @a __s if @a __n2 is too large. If
2475 * @a __pos is beyond end of string, out_of_range is thrown. If
2476 * the length of result exceeds max_size(), length_error is
2477 * thrown. The value of the string doesn't change if an error
2478 * is thrown.
2479 */
2480 _GLIBCXX20_CONSTEXPRconstexpr
2481 basic_string&
2482 replace(size_type __pos, size_type __n1, const _CharT* __s,
2483 size_type __n2)
2484 {
2485 __glibcxx_requires_string_len(__s, __n2);
2486 return _M_replace(_M_check(__pos, "basic_string::replace"),
2487 _M_limit(__pos, __n1), __s, __n2);
2488 }
2489
2490 /**
2491 * @brief Replace characters with value of a C string.
2492 * @param __pos Index of first character to replace.
2493 * @param __n1 Number of characters to be replaced.
2494 * @param __s C string to insert.
2495 * @return Reference to this string.
2496 * @throw std::out_of_range If @a pos > size().
2497 * @throw std::length_error If new length exceeds @c max_size().
2498 *
2499 * Removes the characters in the range [__pos,__pos + __n1)
2500 * from this string. In place, the characters of @a __s are
2501 * inserted. If @a __pos is beyond end of string, out_of_range
2502 * is thrown. If the length of result exceeds max_size(),
2503 * length_error is thrown. The value of the string doesn't
2504 * change if an error is thrown.
2505 */
2506 _GLIBCXX20_CONSTEXPRconstexpr
2507 basic_string&
2508 replace(size_type __pos, size_type __n1, const _CharT* __s)
2509 {
2510 __glibcxx_requires_string(__s);
2511 return this->replace(__pos, __n1, __s, traits_type::length(__s));
2512 }
2513
2514 /**
2515 * @brief Replace characters with multiple characters.
2516 * @param __pos Index of first character to replace.
2517 * @param __n1 Number of characters to be replaced.
2518 * @param __n2 Number of characters to insert.
2519 * @param __c Character to insert.
2520 * @return Reference to this string.
2521 * @throw std::out_of_range If @a __pos > size().
2522 * @throw std::length_error If new length exceeds @c max_size().
2523 *
2524 * Removes the characters in the range [pos,pos + n1) from this
2525 * string. In place, @a __n2 copies of @a __c are inserted.
2526 * If @a __pos is beyond end of string, out_of_range is thrown.
2527 * If the length of result exceeds max_size(), length_error is
2528 * thrown. The value of the string doesn't change if an error
2529 * is thrown.
2530 */
2531 _GLIBCXX20_CONSTEXPRconstexpr
2532 basic_string&
2533 replace(size_type __pos, size_type __n1, size_type __n2, _CharT __c)
2534 { return _M_replace_aux(_M_check(__pos, "basic_string::replace"),
2535 _M_limit(__pos, __n1), __n2, __c); }
2536
2537 /**
2538 * @brief Replace range of characters with string.
2539 * @param __i1 Iterator referencing start of range to replace.
2540 * @param __i2 Iterator referencing end of range to replace.
2541 * @param __str String value to insert.
2542 * @return Reference to this string.
2543 * @throw std::length_error If new length exceeds @c max_size().
2544 *
2545 * Removes the characters in the range [__i1,__i2). In place,
2546 * the value of @a __str is inserted. If the length of result
2547 * exceeds max_size(), length_error is thrown. The value of
2548 * the string doesn't change if an error is thrown.
2549 */
2550 _GLIBCXX20_CONSTEXPRconstexpr
2551 basic_string&
2552 replace(__const_iterator __i1, __const_iterator __i2,
2553 const basic_string& __str)
2554 { return this->replace(__i1, __i2, __str._M_data(), __str.size()); }
2555
2556 /**
2557 * @brief Replace range of characters with C substring.
2558 * @param __i1 Iterator referencing start of range to replace.
2559 * @param __i2 Iterator referencing end of range to replace.
2560 * @param __s C string value to insert.
2561 * @param __n Number of characters from s to insert.
2562 * @return Reference to this string.
2563 * @throw std::length_error If new length exceeds @c max_size().
2564 *
2565 * Removes the characters in the range [__i1,__i2). In place,
2566 * the first @a __n characters of @a __s are inserted. If the
2567 * length of result exceeds max_size(), length_error is thrown.
2568 * The value of the string doesn't change if an error is
2569 * thrown.
2570 */
2571 _GLIBCXX20_CONSTEXPRconstexpr
2572 basic_string&
2573 replace(__const_iterator __i1, __const_iterator __i2,
2574 const _CharT* __s, size_type __n)
2575 {
2576 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2
2577 && __i2 <= end());
2578 return this->replace(__i1 - begin(), __i2 - __i1, __s, __n);
2579 }
2580
2581 /**
2582 * @brief Replace range of characters with C string.
2583 * @param __i1 Iterator referencing start of range to replace.
2584 * @param __i2 Iterator referencing end of range to replace.
2585 * @param __s C string value to insert.
2586 * @return Reference to this string.
2587 * @throw std::length_error If new length exceeds @c max_size().
2588 *
2589 * Removes the characters in the range [__i1,__i2). In place,
2590 * the characters of @a __s are inserted. If the length of
2591 * result exceeds max_size(), length_error is thrown. The
2592 * value of the string doesn't change if an error is thrown.
2593 */
2594 _GLIBCXX20_CONSTEXPRconstexpr
2595 basic_string&
2596 replace(__const_iterator __i1, __const_iterator __i2, const _CharT* __s)
2597 {
2598 __glibcxx_requires_string(__s);
2599 return this->replace(__i1, __i2, __s, traits_type::length(__s));
2600 }
2601
2602 /**
2603 * @brief Replace range of characters with multiple characters
2604 * @param __i1 Iterator referencing start of range to replace.
2605 * @param __i2 Iterator referencing end of range to replace.
2606 * @param __n Number of characters to insert.
2607 * @param __c Character to insert.
2608 * @return Reference to this string.
2609 * @throw std::length_error If new length exceeds @c max_size().
2610 *
2611 * Removes the characters in the range [__i1,__i2). In place,
2612 * @a __n copies of @a __c are inserted. If the length of
2613 * result exceeds max_size(), length_error is thrown. The
2614 * value of the string doesn't change if an error is thrown.
2615 */
2616 _GLIBCXX20_CONSTEXPRconstexpr
2617 basic_string&
2618 replace(__const_iterator __i1, __const_iterator __i2, size_type __n,
2619 _CharT __c)
2620 {
2621 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2
2622 && __i2 <= end());
2623 return _M_replace_aux(__i1 - begin(), __i2 - __i1, __n, __c);
2624 }
2625
2626 /**
2627 * @brief Replace range of characters with range.
2628 * @param __i1 Iterator referencing start of range to replace.
2629 * @param __i2 Iterator referencing end of range to replace.
2630 * @param __k1 Iterator referencing start of range to insert.
2631 * @param __k2 Iterator referencing end of range to insert.
2632 * @return Reference to this string.
2633 * @throw std::length_error If new length exceeds @c max_size().
2634 *
2635 * Removes the characters in the range [__i1,__i2). In place,
2636 * characters in the range [__k1,__k2) are inserted. If the
2637 * length of result exceeds max_size(), length_error is thrown.
2638 * The value of the string doesn't change if an error is
2639 * thrown.
2640 */
2641#if __cplusplus202002L >= 201103L
2642 template<class _InputIterator,
2643 typename = std::_RequireInputIter<_InputIterator>>
2644 _GLIBCXX20_CONSTEXPRconstexpr
2645 basic_string&
2646 replace(const_iterator __i1, const_iterator __i2,
2647 _InputIterator __k1, _InputIterator __k2)
2648 {
2649 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2
2650 && __i2 <= end());
2651 __glibcxx_requires_valid_range(__k1, __k2);
2652 return this->_M_replace_dispatch(__i1, __i2, __k1, __k2,
2653 std::__false_type());
2654 }
2655#else
2656 template<class _InputIterator>
2657#ifdef _GLIBCXX_DISAMBIGUATE_REPLACE_INST
2658 typename __enable_if_not_native_iterator<_InputIterator>::__type
2659#else
2660 basic_string&
2661#endif
2662 replace(iterator __i1, iterator __i2,
2663 _InputIterator __k1, _InputIterator __k2)
2664 {
2665 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2
2666 && __i2 <= end());
2667 __glibcxx_requires_valid_range(__k1, __k2);
2668 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
2669 return _M_replace_dispatch(__i1, __i2, __k1, __k2, _Integral());
2670 }
2671#endif
2672
2673 // Specializations for the common case of pointer and iterator:
2674 // useful to avoid the overhead of temporary buffering in _M_replace.
2675 _GLIBCXX20_CONSTEXPRconstexpr
2676 basic_string&
2677 replace(__const_iterator __i1, __const_iterator __i2,
2678 _CharT* __k1, _CharT* __k2)
2679 {
2680 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2
2681 && __i2 <= end());
2682 __glibcxx_requires_valid_range(__k1, __k2);
2683 return this->replace(__i1 - begin(), __i2 - __i1,
2684 __k1, __k2 - __k1);
2685 }
2686
2687 _GLIBCXX20_CONSTEXPRconstexpr
2688 basic_string&
2689 replace(__const_iterator __i1, __const_iterator __i2,
2690 const _CharT* __k1, const _CharT* __k2)
2691 {
2692 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2
2693 && __i2 <= end());
2694 __glibcxx_requires_valid_range(__k1, __k2);
2695 return this->replace(__i1 - begin(), __i2 - __i1,
2696 __k1, __k2 - __k1);
2697 }
2698
2699 _GLIBCXX20_CONSTEXPRconstexpr
2700 basic_string&
2701 replace(__const_iterator __i1, __const_iterator __i2,
2702 iterator __k1, iterator __k2)
2703 {
2704 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2
2705 && __i2 <= end());
2706 __glibcxx_requires_valid_range(__k1, __k2);
2707 return this->replace(__i1 - begin(), __i2 - __i1,
2708 __k1.base(), __k2 - __k1);
2709 }
2710
2711 _GLIBCXX20_CONSTEXPRconstexpr
2712 basic_string&
2713 replace(__const_iterator __i1, __const_iterator __i2,
2714 const_iterator __k1, const_iterator __k2)
2715 {
2716 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2
2717 && __i2 <= end());
2718 __glibcxx_requires_valid_range(__k1, __k2);
2719 return this->replace(__i1 - begin(), __i2 - __i1,
2720 __k1.base(), __k2 - __k1);
2721 }
2722
2723#if __glibcxx_containers_ranges // C++ >= 23
2724 /**
2725 * @brief Replace part of the string with a range.
2726 * @param __rg A range of values that are convertible to `value_type`.
2727 * @since C++23
2728 *
2729 * The range `__rg` is allowed to overlap with `*this`.
2730 */
2731 template<__detail::__container_compatible_range<_CharT> _Rg>
2732 constexpr basic_string&
2733 replace_with_range(const_iterator __i1, const_iterator __i2, _Rg&& __rg)
2734 {
2735 if (__i1 == cend())
2736 append_range(std::forward<_Rg>(__rg));
2737 else
2738 {
2739 basic_string __s(from_range, std::forward<_Rg>(__rg),
2740 _M_get_allocator());
2741 replace(__i1, __i2, __s);
2742 }
2743 return *this;
2744 }
2745#endif
2746
2747#if __cplusplus202002L >= 201103L
2748 /**
2749 * @brief Replace range of characters with initializer_list.
2750 * @param __i1 Iterator referencing start of range to replace.
2751 * @param __i2 Iterator referencing end of range to replace.
2752 * @param __l The initializer_list of characters to insert.
2753 * @return Reference to this string.
2754 * @throw std::length_error If new length exceeds @c max_size().
2755 *
2756 * Removes the characters in the range [__i1,__i2). In place,
2757 * characters in the range [__k1,__k2) are inserted. If the
2758 * length of result exceeds max_size(), length_error is thrown.
2759 * The value of the string doesn't change if an error is
2760 * thrown.
2761 */
2762 _GLIBCXX20_CONSTEXPRconstexpr
2763 basic_string& replace(const_iterator __i1, const_iterator __i2,
2764 initializer_list<_CharT> __l)
2765 { return this->replace(__i1, __i2, __l.begin(), __l.size()); }
2766#endif // C++11
2767
2768#ifdef __glibcxx_string_view201803L // >= C++17
2769 /**
2770 * @brief Replace range of characters with string_view.
2771 * @param __pos The position to replace at.
2772 * @param __n The number of characters to replace.
2773 * @param __svt The object convertible to string_view to insert.
2774 * @return Reference to this string.
2775 */
2776 template<typename _Tp>
2777 _GLIBCXX20_CONSTEXPRconstexpr
2778 _If_sv<_Tp, basic_string&>
2779 replace(size_type __pos, size_type __n, const _Tp& __svt)
2780 {
2781 __sv_type __sv = __svt;
2782 return this->replace(__pos, __n, __sv.data(), __sv.size());
2783 }
2784
2785 /**
2786 * @brief Replace range of characters with string_view.
2787 * @param __pos1 The position to replace at.
2788 * @param __n1 The number of characters to replace.
2789 * @param __svt The object convertible to string_view to insert from.
2790 * @param __pos2 The position in the string_view to insert from.
2791 * @param __n2 The number of characters to insert.
2792 * @return Reference to this string.
2793 */
2794 template<typename _Tp>
2795 _GLIBCXX20_CONSTEXPRconstexpr
2796 _If_sv<_Tp, basic_string&>
2797 replace(size_type __pos1, size_type __n1, const _Tp& __svt,
2798 size_type __pos2, size_type __n2 = npos)
2799 {
2800 __sv_type __sv = __svt;
2801 return this->replace(__pos1, __n1,
2802 __sv.data()
2803 + std::__sv_check(__sv.size(), __pos2, "basic_string::replace"),
2804 std::__sv_limit(__sv.size(), __pos2, __n2));
2805 }
2806
2807 /**
2808 * @brief Replace range of characters with string_view.
2809 * @param __i1 An iterator referencing the start position
2810 to replace at.
2811 * @param __i2 An iterator referencing the end position
2812 for the replace.
2813 * @param __svt The object convertible to string_view to insert from.
2814 * @return Reference to this string.
2815 */
2816 template<typename _Tp>
2817 _GLIBCXX20_CONSTEXPRconstexpr
2818 _If_sv<_Tp, basic_string&>
2819 replace(const_iterator __i1, const_iterator __i2, const _Tp& __svt)
2820 {
2821 __sv_type __sv = __svt;
2822 return this->replace(__i1 - begin(), __i2 - __i1, __sv);
2823 }
2824#endif // C++17
2825
2826 private:
2827 template<class _Integer>
2828 _GLIBCXX20_CONSTEXPRconstexpr
2829 basic_string&
2830 _M_replace_dispatch(const_iterator __i1, const_iterator __i2,
2831 _Integer __n, _Integer __val, __true_type)
2832 { return _M_replace_aux(__i1 - begin(), __i2 - __i1, __n, __val); }
2833
2834 template<class _InputIterator>
2835 _GLIBCXX20_CONSTEXPRconstexpr
2836 basic_string&
2837 _M_replace_dispatch(const_iterator __i1, const_iterator __i2,
2838 _InputIterator __k1, _InputIterator __k2,
2839 __false_type);
2840
2841 _GLIBCXX20_CONSTEXPRconstexpr
2842 basic_string&
2843 _M_replace_aux(size_type __pos1, size_type __n1, size_type __n2,
2844 _CharT __c);
2845
2846 __attribute__((__noinline__, __noclone__, __cold__)) void
2847 _M_replace_cold(pointer __p, size_type __len1, const _CharT* __s,
2848 const size_type __len2, const size_type __how_much);
2849
2850 _GLIBCXX20_CONSTEXPRconstexpr
2851 basic_string&
2852 _M_replace(size_type __pos, size_type __len1, const _CharT* __s,
2853 const size_type __len2);
2854
2855 _GLIBCXX20_CONSTEXPRconstexpr
2856 basic_string&
2857 _M_append(const _CharT* __s, size_type __n);
2858
2859 public:
2860
2861 /**
2862 * @brief Copy substring into C string.
2863 * @param __s C string to copy value into.
2864 * @param __n Number of characters to copy.
2865 * @param __pos Index of first character to copy.
2866 * @return Number of characters actually copied
2867 * @throw std::out_of_range If __pos > size().
2868 *
2869 * Copies up to @a __n characters starting at @a __pos into the
2870 * C string @a __s. If @a __pos is %greater than size(),
2871 * out_of_range is thrown.
2872 */
2873 _GLIBCXX20_CONSTEXPRconstexpr
2874 size_type
2875 copy(_CharT* __s, size_type __n, size_type __pos = 0) const;
2876
2877 /**
2878 * @brief Swap contents with another string.
2879 * @param __s String to swap with.
2880 *
2881 * Exchanges the contents of this string with that of @a __s in constant
2882 * time.
2883 */
2884 _GLIBCXX20_CONSTEXPRconstexpr
2885 void
2886 swap(basic_string& __s) _GLIBCXX_NOEXCEPTnoexcept;
2887
2888 // String operations:
2889 /**
2890 * @brief Return const pointer to null-terminated contents.
2891 *
2892 * This is a handle to internal data. Do not modify or dire things may
2893 * happen.
2894 */
2895 _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr
2896 const _CharT*
2897 c_str() const _GLIBCXX_NOEXCEPTnoexcept
2898 { return _M_data(); }
2899
2900 /**
2901 * @brief Return const pointer to contents.
2902 *
2903 * This is a pointer to internal data. It is undefined to modify
2904 * the contents through the returned pointer. To get a pointer that
2905 * allows modifying the contents use @c &str[0] instead,
2906 * (or in C++17 the non-const @c str.data() overload).
2907 */
2908 _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr
2909 const _CharT*
2910 data() const _GLIBCXX_NOEXCEPTnoexcept
2911 { return _M_data(); }
2912
2913#if __cplusplus202002L >= 201703L
2914 /**
2915 * @brief Return non-const pointer to contents.
2916 *
2917 * This is a pointer to the character sequence held by the string.
2918 * Modifying the characters in the sequence is allowed.
2919 */
2920 _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr
2921 _CharT*
2922 data() noexcept
2923 { return _M_data(); }
2924#endif
2925
2926 /**
2927 * @brief Return copy of allocator used to construct this string.
2928 */
2929 _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr
2930 allocator_type
2931 get_allocator() const _GLIBCXX_NOEXCEPTnoexcept
2932 { return _M_get_allocator(); }
2933
2934 /**
2935 * @brief Find position of a C substring.
2936 * @param __s C string to locate.
2937 * @param __pos Index of character to search from.
2938 * @param __n Number of characters from @a s to search for.
2939 * @return Index of start of first occurrence.
2940 *
2941 * Starting from @a __pos, searches forward for the first @a
2942 * __n characters in @a __s within this string. If found,
2943 * returns the index where it begins. If not found, returns
2944 * npos.
2945 */
2946 _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr
2947 size_type
2948 find(const _CharT* __s, size_type __pos, size_type __n) const
2949 _GLIBCXX_NOEXCEPTnoexcept;
2950
2951 /**
2952 * @brief Find position of a string.
2953 * @param __str String to locate.
2954 * @param __pos Index of character to search from (default 0).
2955 * @return Index of start of first occurrence.
2956 *
2957 * Starting from @a __pos, searches forward for value of @a __str within
2958 * this string. If found, returns the index where it begins. If not
2959 * found, returns npos.
2960 */
2961 _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr
2962 size_type
2963 find(const basic_string& __str, size_type __pos = 0) const
2964 _GLIBCXX_NOEXCEPTnoexcept
2965 { return this->find(__str.data(), __pos, __str.size()); }
2966
2967#ifdef __glibcxx_string_view201803L // >= C++17
2968 /**
2969 * @brief Find position of a string_view.
2970 * @param __svt The object convertible to string_view to locate.
2971 * @param __pos Index of character to search from (default 0).
2972 * @return Index of start of first occurrence.
2973 */
2974 template<typename _Tp>
2975 _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr
2976 _If_sv<_Tp, size_type>
2977 find(const _Tp& __svt, size_type __pos = 0) const
2978 noexcept(is_same<_Tp, __sv_type>::value)
2979 {
2980 __sv_type __sv = __svt;
2981 return this->find(__sv.data(), __pos, __sv.size());
2982 }
2983#endif // C++17
2984
2985 /**
2986 * @brief Find position of a C string.
2987 * @param __s C string to locate.
2988 * @param __pos Index of character to search from (default 0).
2989 * @return Index of start of first occurrence.
2990 *
2991 * Starting from @a __pos, searches forward for the value of @a
2992 * __s within this string. If found, returns the index where
2993 * it begins. If not found, returns npos.
2994 */
2995 _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr
2996 size_type
2997 find(const _CharT* __s, size_type __pos = 0) const _GLIBCXX_NOEXCEPTnoexcept
2998 {
2999 __glibcxx_requires_string(__s);
3000 return this->find(__s, __pos, traits_type::length(__s));
3001 }
3002
3003 /**
3004 * @brief Find position of a character.
3005 * @param __c Character to locate.
3006 * @param __pos Index of character to search from (default 0).
3007 * @return Index of first occurrence.
3008 *
3009 * Starting from @a __pos, searches forward for @a __c within
3010 * this string. If found, returns the index where it was
3011 * found. If not found, returns npos.
3012 */
3013 _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr
3014 size_type
3015 find(_CharT __c, size_type __pos = 0) const _GLIBCXX_NOEXCEPTnoexcept;
3016
3017 /**
3018 * @brief Find last position of a string.
3019 * @param __str String to locate.
3020 * @param __pos Index of character to search back from (default end).
3021 * @return Index of start of last occurrence.
3022 *
3023 * Starting from @a __pos, searches backward for value of @a
3024 * __str within this string. If found, returns the index where
3025 * it begins. If not found, returns npos.
3026 */
3027 _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr
3028 size_type
3029 rfind(const basic_string& __str, size_type __pos = npos) const
3030 _GLIBCXX_NOEXCEPTnoexcept
3031 { return this->rfind(__str.data(), __pos, __str.size()); }
3032
3033#ifdef __glibcxx_string_view201803L // >= C++17
3034 /**
3035 * @brief Find last position of a string_view.
3036 * @param __svt The object convertible to string_view to locate.
3037 * @param __pos Index of character to search back from (default end).
3038 * @return Index of start of last occurrence.
3039 */
3040 template<typename _Tp>
3041 _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr
3042 _If_sv<_Tp, size_type>
3043 rfind(const _Tp& __svt, size_type __pos = npos) const
3044 noexcept(is_same<_Tp, __sv_type>::value)
3045 {
3046 __sv_type __sv = __svt;
3047 return this->rfind(__sv.data(), __pos, __sv.size());
3048 }
3049#endif // C++17
3050
3051 /**
3052 * @brief Find last position of a C substring.
3053 * @param __s C string to locate.
3054 * @param __pos Index of character to search back from.
3055 * @param __n Number of characters from s to search for.
3056 * @return Index of start of last occurrence.
3057 *
3058 * Starting from @a __pos, searches backward for the first @a
3059 * __n characters in @a __s within this string. If found,
3060 * returns the index where it begins. If not found, returns
3061 * npos.
3062 */
3063 _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr
3064 size_type
3065 rfind(const _CharT* __s, size_type __pos, size_type __n) const
3066 _GLIBCXX_NOEXCEPTnoexcept;
3067
3068 /**
3069 * @brief Find last position of a C string.
3070 * @param __s C string to locate.
3071 * @param __pos Index of character to start search at (default end).
3072 * @return Index of start of last occurrence.
3073 *
3074 * Starting from @a __pos, searches backward for the value of
3075 * @a __s within this string. If found, returns the index
3076 * where it begins. If not found, returns npos.
3077 */
3078 _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr
3079 size_type
3080 rfind(const _CharT* __s, size_type __pos = npos) const
3081 {
3082 __glibcxx_requires_string(__s);
3083 return this->rfind(__s, __pos, traits_type::length(__s));
3084 }
3085
3086 /**
3087 * @brief Find last position of a character.
3088 * @param __c Character to locate.
3089 * @param __pos Index of character to search back from (default end).
3090 * @return Index of last occurrence.
3091 *
3092 * Starting from @a __pos, searches backward for @a __c within
3093 * this string. If found, returns the index where it was
3094 * found. If not found, returns npos.
3095 */
3096 _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr
3097 size_type
3098 rfind(_CharT __c, size_type __pos = npos) const _GLIBCXX_NOEXCEPTnoexcept;
3099
3100 /**
3101 * @brief Find position of a character of string.
3102 * @param __str String containing characters to locate.
3103 * @param __pos Index of character to search from (default 0).
3104 * @return Index of first occurrence.
3105 *
3106 * Starting from @a __pos, searches forward for one of the
3107 * characters of @a __str within this string. If found,
3108 * returns the index where it was found. If not found, returns
3109 * npos.
3110 */
3111 _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr
3112 size_type
3113 find_first_of(const basic_string& __str, size_type __pos = 0) const
3114 _GLIBCXX_NOEXCEPTnoexcept
3115 { return this->find_first_of(__str.data(), __pos, __str.size()); }
3116
3117#ifdef __glibcxx_string_view201803L // >= C++17
3118 /**
3119 * @brief Find position of a character of a string_view.
3120 * @param __svt An object convertible to string_view containing
3121 * characters to locate.
3122 * @param __pos Index of character to search from (default 0).
3123 * @return Index of first occurrence.
3124 */
3125 template<typename _Tp>
3126 _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr
3127 _If_sv<_Tp, size_type>
3128 find_first_of(const _Tp& __svt, size_type __pos = 0) const
3129 noexcept(is_same<_Tp, __sv_type>::value)
3130 {
3131 __sv_type __sv = __svt;
3132 return this->find_first_of(__sv.data(), __pos, __sv.size());
3133 }
3134#endif // C++17
3135
3136 /**
3137 * @brief Find position of a character of C substring.
3138 * @param __s String containing characters to locate.
3139 * @param __pos Index of character to search from.
3140 * @param __n Number of characters from s to search for.
3141 * @return Index of first occurrence.
3142 *
3143 * Starting from @a __pos, searches forward for one of the
3144 * first @a __n characters of @a __s within this string. If
3145 * found, returns the index where it was found. If not found,
3146 * returns npos.
3147 */
3148 _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr
3149 size_type
3150 find_first_of(const _CharT* __s, size_type __pos, size_type __n) const
3151 _GLIBCXX_NOEXCEPTnoexcept;
3152
3153 /**
3154 * @brief Find position of a character of C string.
3155 * @param __s String containing characters to locate.
3156 * @param __pos Index of character to search from (default 0).
3157 * @return Index of first occurrence.
3158 *
3159 * Starting from @a __pos, searches forward for one of the
3160 * characters of @a __s within this string. If found, returns
3161 * the index where it was found. If not found, returns npos.
3162 */
3163 _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr
3164 size_type
3165 find_first_of(const _CharT* __s, size_type __pos = 0) const
3166 _GLIBCXX_NOEXCEPTnoexcept
3167 {
3168 __glibcxx_requires_string(__s);
3169 return this->find_first_of(__s, __pos, traits_type::length(__s));
3170 }
3171
3172 /**
3173 * @brief Find position of a character.
3174 * @param __c Character to locate.
3175 * @param __pos Index of character to search from (default 0).
3176 * @return Index of first occurrence.
3177 *
3178 * Starting from @a __pos, searches forward for the character
3179 * @a __c within this string. If found, returns the index
3180 * where it was found. If not found, returns npos.
3181 *
3182 * Note: equivalent to find(__c, __pos).
3183 */
3184 _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr
3185 size_type
3186 find_first_of(_CharT __c, size_type __pos = 0) const _GLIBCXX_NOEXCEPTnoexcept
3187 { return this->find(__c, __pos); }
3188
3189 /**
3190 * @brief Find last position of a character of string.
3191 * @param __str String containing characters to locate.
3192 * @param __pos Index of character to search back from (default end).
3193 * @return Index of last occurrence.
3194 *
3195 * Starting from @a __pos, searches backward for one of the
3196 * characters of @a __str within this string. If found,
3197 * returns the index where it was found. If not found, returns
3198 * npos.
3199 */
3200 _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr
3201 size_type
3202 find_last_of(const basic_string& __str, size_type __pos = npos) const
3203 _GLIBCXX_NOEXCEPTnoexcept
3204 { return this->find_last_of(__str.data(), __pos, __str.size()); }
3205
3206#ifdef __glibcxx_string_view201803L // >= C++17
3207 /**
3208 * @brief Find last position of a character of string.
3209 * @param __svt An object convertible to string_view containing
3210 * characters to locate.
3211 * @param __pos Index of character to search back from (default end).
3212 * @return Index of last occurrence.
3213 */
3214 template<typename _Tp>
3215 _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr
3216 _If_sv<_Tp, size_type>
3217 find_last_of(const _Tp& __svt, size_type __pos = npos) const
3218 noexcept(is_same<_Tp, __sv_type>::value)
3219 {
3220 __sv_type __sv = __svt;
3221 return this->find_last_of(__sv.data(), __pos, __sv.size());
3222 }
3223#endif // C++17
3224
3225 /**
3226 * @brief Find last position of a character of C substring.
3227 * @param __s C string containing characters to locate.
3228 * @param __pos Index of character to search back from.
3229 * @param __n Number of characters from s to search for.
3230 * @return Index of last occurrence.
3231 *
3232 * Starting from @a __pos, searches backward for one of the
3233 * first @a __n characters of @a __s within this string. If
3234 * found, returns the index where it was found. If not found,
3235 * returns npos.
3236 */
3237 _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr
3238 size_type
3239 find_last_of(const _CharT* __s, size_type __pos, size_type __n) const
3240 _GLIBCXX_NOEXCEPTnoexcept;
3241
3242 /**
3243 * @brief Find last position of a character of C string.
3244 * @param __s C string containing characters to locate.
3245 * @param __pos Index of character to search back from (default end).
3246 * @return Index of last occurrence.
3247 *
3248 * Starting from @a __pos, searches backward for one of the
3249 * characters of @a __s within this string. If found, returns
3250 * the index where it was found. If not found, returns npos.
3251 */
3252 _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr
3253 size_type
3254 find_last_of(const _CharT* __s, size_type __pos = npos) const
3255 _GLIBCXX_NOEXCEPTnoexcept
3256 {
3257 __glibcxx_requires_string(__s);
3258 return this->find_last_of(__s, __pos, traits_type::length(__s));
3259 }
3260
3261 /**
3262 * @brief Find last position of a character.
3263 * @param __c Character to locate.
3264 * @param __pos Index of character to search back from (default end).
3265 * @return Index of last occurrence.
3266 *
3267 * Starting from @a __pos, searches backward for @a __c within
3268 * this string. If found, returns the index where it was
3269 * found. If not found, returns npos.
3270 *
3271 * Note: equivalent to rfind(__c, __pos).
3272 */
3273 _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr
3274 size_type
3275 find_last_of(_CharT __c, size_type __pos = npos) const _GLIBCXX_NOEXCEPTnoexcept
3276 { return this->rfind(__c, __pos); }
3277
3278 /**
3279 * @brief Find position of a character not in string.
3280 * @param __str String containing characters to avoid.
3281 * @param __pos Index of character to search from (default 0).
3282 * @return Index of first occurrence.
3283 *
3284 * Starting from @a __pos, searches forward for a character not contained
3285 * in @a __str within this string. If found, returns the index where it
3286 * was found. If not found, returns npos.
3287 */
3288 _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr
3289 size_type
3290 find_first_not_of(const basic_string& __str, size_type __pos = 0) const
3291 _GLIBCXX_NOEXCEPTnoexcept
3292 { return this->find_first_not_of(__str.data(), __pos, __str.size()); }
3293
3294#ifdef __glibcxx_string_view201803L // >= C++17
3295 /**
3296 * @brief Find position of a character not in a string_view.
3297 * @param __svt A object convertible to string_view containing
3298 * characters to avoid.
3299 * @param __pos Index of character to search from (default 0).
3300 * @return Index of first occurrence.
3301 */
3302 template<typename _Tp>
3303 _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr
3304 _If_sv<_Tp, size_type>
3305 find_first_not_of(const _Tp& __svt, size_type __pos = 0) const
3306 noexcept(is_same<_Tp, __sv_type>::value)
3307 {
3308 __sv_type __sv = __svt;
3309 return this->find_first_not_of(__sv.data(), __pos, __sv.size());
3310 }
3311#endif // C++17
3312
3313 /**
3314 * @brief Find position of a character not in C substring.
3315 * @param __s C string containing characters to avoid.
3316 * @param __pos Index of character to search from.
3317 * @param __n Number of characters from __s to consider.
3318 * @return Index of first occurrence.
3319 *
3320 * Starting from @a __pos, searches forward for a character not
3321 * contained in the first @a __n characters of @a __s within
3322 * this string. If found, returns the index where it was
3323 * found. If not found, returns npos.
3324 */
3325 _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr
3326 size_type
3327 find_first_not_of(const _CharT* __s, size_type __pos,
3328 size_type __n) const _GLIBCXX_NOEXCEPTnoexcept;
3329
3330 /**
3331 * @brief Find position of a character not in C string.
3332 * @param __s C string containing characters to avoid.
3333 * @param __pos Index of character to search from (default 0).
3334 * @return Index of first occurrence.
3335 *
3336 * Starting from @a __pos, searches forward for a character not
3337 * contained in @a __s within this string. If found, returns
3338 * the index where it was found. If not found, returns npos.
3339 */
3340 _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr
3341 size_type
3342 find_first_not_of(const _CharT* __s, size_type __pos = 0) const
3343 _GLIBCXX_NOEXCEPTnoexcept
3344 {
3345 __glibcxx_requires_string(__s);
3346 return this->find_first_not_of(__s, __pos, traits_type::length(__s));
3347 }
3348
3349 /**
3350 * @brief Find position of a different character.
3351 * @param __c Character to avoid.
3352 * @param __pos Index of character to search from (default 0).
3353 * @return Index of first occurrence.
3354 *
3355 * Starting from @a __pos, searches forward for a character
3356 * other than @a __c within this string. If found, returns the
3357 * index where it was found. If not found, returns npos.
3358 */
3359 _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr
3360 size_type
3361 find_first_not_of(_CharT __c, size_type __pos = 0) const
3362 _GLIBCXX_NOEXCEPTnoexcept;
3363
3364 /**
3365 * @brief Find last position of a character not in string.
3366 * @param __str String containing characters to avoid.
3367 * @param __pos Index of character to search back from (default end).
3368 * @return Index of last occurrence.
3369 *
3370 * Starting from @a __pos, searches backward for a character
3371 * not contained in @a __str within this string. If found,
3372 * returns the index where it was found. If not found, returns
3373 * npos.
3374 */
3375 _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr
3376 size_type
3377 find_last_not_of(const basic_string& __str, size_type __pos = npos) const
3378 _GLIBCXX_NOEXCEPTnoexcept
3379 { return this->find_last_not_of(__str.data(), __pos, __str.size()); }
3380
3381#ifdef __glibcxx_string_view201803L // >= C++17
3382 /**
3383 * @brief Find last position of a character not in a string_view.
3384 * @param __svt An object convertible to string_view containing
3385 * characters to avoid.
3386 * @param __pos Index of character to search back from (default end).
3387 * @return Index of last occurrence.
3388 */
3389 template<typename _Tp>
3390 _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr
3391 _If_sv<_Tp, size_type>
3392 find_last_not_of(const _Tp& __svt, size_type __pos = npos) const
3393 noexcept(is_same<_Tp, __sv_type>::value)
3394 {
3395 __sv_type __sv = __svt;
3396 return this->find_last_not_of(__sv.data(), __pos, __sv.size());
3397 }
3398#endif // C++17
3399
3400 /**
3401 * @brief Find last position of a character not in C substring.
3402 * @param __s C string containing characters to avoid.
3403 * @param __pos Index of character to search back from.
3404 * @param __n Number of characters from s to consider.
3405 * @return Index of last occurrence.
3406 *
3407 * Starting from @a __pos, searches backward for a character not
3408 * contained in the first @a __n characters of @a __s within this string.
3409 * If found, returns the index where it was found. If not found,
3410 * returns npos.
3411 */
3412 _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr
3413 size_type
3414 find_last_not_of(const _CharT* __s, size_type __pos,
3415 size_type __n) const _GLIBCXX_NOEXCEPTnoexcept;
3416 /**
3417 * @brief Find last position of a character not in C string.
3418 * @param __s C string containing characters to avoid.
3419 * @param __pos Index of character to search back from (default end).
3420 * @return Index of last occurrence.
3421 *
3422 * Starting from @a __pos, searches backward for a character
3423 * not contained in @a __s within this string. If found,
3424 * returns the index where it was found. If not found, returns
3425 * npos.
3426 */
3427 _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr
3428 size_type
3429 find_last_not_of(const _CharT* __s, size_type __pos = npos) const
3430 _GLIBCXX_NOEXCEPTnoexcept
3431 {
3432 __glibcxx_requires_string(__s);
3433 return this->find_last_not_of(__s, __pos, traits_type::length(__s));
3434 }
3435
3436 /**
3437 * @brief Find last position of a different character.
3438 * @param __c Character to avoid.
3439 * @param __pos Index of character to search back from (default end).
3440 * @return Index of last occurrence.
3441 *
3442 * Starting from @a __pos, searches backward for a character other than
3443 * @a __c within this string. If found, returns the index where it was
3444 * found. If not found, returns npos.
3445 */
3446 _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr
3447 size_type
3448 find_last_not_of(_CharT __c, size_type __pos = npos) const
3449 _GLIBCXX_NOEXCEPTnoexcept;
3450
3451 /**
3452 * @brief Get a substring.
3453 * @param __pos Index of first character (default 0).
3454 * @param __n Number of characters in substring (default remainder).
3455 * @return The new string.
3456 * @throw std::out_of_range If __pos > size().
3457 *
3458 * Construct and return a new string using the @a __n
3459 * characters starting at @a __pos. If the string is too
3460 * short, use the remainder of the characters. If @a __pos is
3461 * beyond the end of the string, out_of_range is thrown.
3462 */
3463 _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr
3464 basic_string
3465 substr(size_type __pos = 0, size_type __n = npos) const
3466 { return basic_string(*this,
3467 _M_check(__pos, "basic_string::substr"), __n); }
3468
3469#if __cplusplus202002L >= 202302L
3470 _GLIBCXX_NODISCARD[[__nodiscard__]]
3471 constexpr basic_string
3472 substr(size_type __pos = 0) &&
3473 { return basic_string(std::move(*this), __pos); }
3474
3475 _GLIBCXX_NODISCARD[[__nodiscard__]]
3476 constexpr basic_string
3477 substr(size_type __pos, size_type __n) &&
3478 { return basic_string(std::move(*this), __pos, __n); }
3479#endif // C++23
3480
3481#ifdef __glibcxx_string_subview // >= C++26
3482 /**
3483 * @brief Get a subview.
3484 * @param __pos Index of first character (default 0).
3485 * @param __n Number of characters in subview (default remainder).
3486 * @return The subview.
3487 * @throw std::out_of_range If __pos > size().
3488 *
3489 * Construct and return a subview using the `__n` characters starting at
3490 * `__pos`. If the string is too short, use the remainder of the
3491 * characters. If `__pos` is beyond the end of the string, out_of_range
3492 * is thrown.
3493 */
3494 [[nodiscard]]
3495 constexpr basic_string_view<_CharT, _Traits>
3496 subview(size_type __pos = 0, size_type __n = npos) const
3497 { return __sv_type(*this).subview(__pos, __n); }
3498#endif
3499
3500 /**
3501 * @brief Compare to a string.
3502 * @param __str String to compare against.
3503 * @return Integer < 0, 0, or > 0.
3504 *
3505 * Returns an integer < 0 if this string is ordered before @a
3506 * __str, 0 if their values are equivalent, or > 0 if this
3507 * string is ordered after @a __str. Determines the effective
3508 * length rlen of the strings to compare as the smallest of
3509 * size() and str.size(). The function then compares the two
3510 * strings by calling traits::compare(data(), str.data(),rlen).
3511 * If the result of the comparison is nonzero returns it,
3512 * otherwise the shorter one is ordered first.
3513 */
3514 _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr
3515 int
3516 compare(const basic_string& __str) const
3517 {
3518 const size_type __size = this->size();
3519 const size_type __osize = __str.size();
3520 const size_type __len = std::min(__size, __osize);
3521
3522 int __r = traits_type::compare(_M_data(), __str.data(), __len);
3523 if (!__r)
3524 __r = _S_compare(__size, __osize);
3525 return __r;
3526 }
3527
3528#ifdef __glibcxx_string_view201803L // >= C++17
3529 /**
3530 * @brief Compare to a string_view.
3531 * @param __svt An object convertible to string_view to compare against.
3532 * @return Integer < 0, 0, or > 0.
3533 */
3534 template<typename _Tp>
3535 _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr
3536 _If_sv<_Tp, int>
3537 compare(const _Tp& __svt) const
3538 noexcept(is_same<_Tp, __sv_type>::value)
3539 {
3540 __sv_type __sv = __svt;
3541 const size_type __size = this->size();
3542 const size_type __osize = __sv.size();
3543 const size_type __len = std::min(__size, __osize);
3544
3545 int __r = traits_type::compare(_M_data(), __sv.data(), __len);
3546 if (!__r)
3547 __r = _S_compare(__size, __osize);
3548 return __r;
3549 }
3550
3551 /**
3552 * @brief Compare to a string_view.
3553 * @param __pos A position in the string to start comparing from.
3554 * @param __n The number of characters to compare.
3555 * @param __svt An object convertible to string_view to compare
3556 * against.
3557 * @return Integer < 0, 0, or > 0.
3558 */
3559 template<typename _Tp>
3560 _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr
3561 _If_sv<_Tp, int>
3562 compare(size_type __pos, size_type __n, const _Tp& __svt) const
3563 {
3564 __sv_type __sv = __svt;
3565 return __sv_type(*this).substr(__pos, __n).compare(__sv);
3566 }
3567
3568 /**
3569 * @brief Compare to a string_view.
3570 * @param __pos1 A position in the string to start comparing from.
3571 * @param __n1 The number of characters to compare.
3572 * @param __svt An object convertible to string_view to compare
3573 * against.
3574 * @param __pos2 A position in the string_view to start comparing from.
3575 * @param __n2 The number of characters to compare.
3576 * @return Integer < 0, 0, or > 0.
3577 */
3578 template<typename _Tp>
3579 _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr
3580 _If_sv<_Tp, int>
3581 compare(size_type __pos1, size_type __n1, const _Tp& __svt,
3582 size_type __pos2, size_type __n2 = npos) const
3583 {
3584 __sv_type __sv = __svt;
3585 return __sv_type(*this)
3586 .substr(__pos1, __n1).compare(__sv.substr(__pos2, __n2));
3587 }
3588#endif // C++17
3589
3590 /**
3591 * @brief Compare substring to a string.
3592 * @param __pos Index of first character of substring.
3593 * @param __n Number of characters in substring.
3594 * @param __str String to compare against.
3595 * @return Integer < 0, 0, or > 0.
3596 *
3597 * Form the substring of this string from the @a __n characters
3598 * starting at @a __pos. Returns an integer < 0 if the
3599 * substring is ordered before @a __str, 0 if their values are
3600 * equivalent, or > 0 if the substring is ordered after @a
3601 * __str. Determines the effective length rlen of the strings
3602 * to compare as the smallest of the length of the substring
3603 * and @a __str.size(). The function then compares the two
3604 * strings by calling
3605 * traits::compare(substring.data(),str.data(),rlen). If the
3606 * result of the comparison is nonzero returns it, otherwise
3607 * the shorter one is ordered first.
3608 */
3609 _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr
3610 int
3611 compare(size_type __pos, size_type __n, const basic_string& __str) const
3612 {
3613 _M_check(__pos, "basic_string::compare");
3614 __n = _M_limit(__pos, __n);
3615 const size_type __osize = __str.size();
3616 const size_type __len = std::min(__n, __osize);
3617 int __r = traits_type::compare(_M_data() + __pos, __str.data(), __len);
3618 if (!__r)
3619 __r = _S_compare(__n, __osize);
3620 return __r;
3621 }
3622
3623 /**
3624 * @brief Compare substring to a substring.
3625 * @param __pos1 Index of first character of substring.
3626 * @param __n1 Number of characters in substring.
3627 * @param __str String to compare against.
3628 * @param __pos2 Index of first character of substring of str.
3629 * @param __n2 Number of characters in substring of str.
3630 * @return Integer < 0, 0, or > 0.
3631 *
3632 * Form the substring of this string from the @a __n1
3633 * characters starting at @a __pos1. Form the substring of @a
3634 * __str from the @a __n2 characters starting at @a __pos2.
3635 * Returns an integer < 0 if this substring is ordered before
3636 * the substring of @a __str, 0 if their values are equivalent,
3637 * or > 0 if this substring is ordered after the substring of
3638 * @a __str. Determines the effective length rlen of the
3639 * strings to compare as the smallest of the lengths of the
3640 * substrings. The function then compares the two strings by
3641 * calling
3642 * traits::compare(substring.data(),str.substr(pos2,n2).data(),rlen).
3643 * If the result of the comparison is nonzero returns it,
3644 * otherwise the shorter one is ordered first.
3645 */
3646 _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr
3647 int
3648 compare(size_type __pos1, size_type __n1, const basic_string& __str,
3649 size_type __pos2, size_type __n2 = npos) const
3650 {
3651 _M_check(__pos1, "basic_string::compare");
3652 __str._M_check(__pos2, "basic_string::compare");
3653 __n1 = _M_limit(__pos1, __n1);
3654 __n2 = __str._M_limit(__pos2, __n2);
3655 const size_type __len = std::min(__n1, __n2);
3656 int __r = traits_type::compare(_M_data() + __pos1,
3657 __str.data() + __pos2, __len);
3658 if (!__r)
3659 __r = _S_compare(__n1, __n2);
3660 return __r;
3661 }
3662
3663 /**
3664 * @brief Compare to a C string.
3665 * @param __s C string to compare against.
3666 * @return Integer < 0, 0, or > 0.
3667 *
3668 * Returns an integer < 0 if this string is ordered before @a __s, 0 if
3669 * their values are equivalent, or > 0 if this string is ordered after
3670 * @a __s. Determines the effective length rlen of the strings to
3671 * compare as the smallest of size() and the length of a string
3672 * constructed from @a __s. The function then compares the two strings
3673 * by calling traits::compare(data(),s,rlen). If the result of the
3674 * comparison is nonzero returns it, otherwise the shorter one is
3675 * ordered first.
3676 */
3677 _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr
3678 int
3679 compare(const _CharT* __s) const _GLIBCXX_NOEXCEPTnoexcept
3680 {
3681 __glibcxx_requires_string(__s);
3682 const size_type __size = this->size();
3683 const size_type __osize = traits_type::length(__s);
3684 const size_type __len = std::min(__size, __osize);
3685 int __r = traits_type::compare(_M_data(), __s, __len);
3686 if (!__r)
3687 __r = _S_compare(__size, __osize);
3688 return __r;
3689 }
3690
3691 // _GLIBCXX_RESOLVE_LIB_DEFECTS
3692 // 5 String::compare specification questionable
3693 /**
3694 * @brief Compare substring to a C string.
3695 * @param __pos Index of first character of substring.
3696 * @param __n1 Number of characters in substring.
3697 * @param __s C string to compare against.
3698 * @return Integer < 0, 0, or > 0.
3699 *
3700 * Form the substring of this string from the @a __n1
3701 * characters starting at @a pos. Returns an integer < 0 if
3702 * the substring is ordered before @a __s, 0 if their values
3703 * are equivalent, or > 0 if the substring is ordered after @a
3704 * __s. Determines the effective length rlen of the strings to
3705 * compare as the smallest of the length of the substring and
3706 * the length of a string constructed from @a __s. The
3707 * function then compares the two string by calling
3708 * traits::compare(substring.data(),__s,rlen). If the result of
3709 * the comparison is nonzero returns it, otherwise the shorter
3710 * one is ordered first.
3711 */
3712 _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr
3713 int
3714 compare(size_type __pos, size_type __n1, const _CharT* __s) const
3715 {
3716 __glibcxx_requires_string(__s);
3717 _M_check(__pos, "basic_string::compare");
3718 __n1 = _M_limit(__pos, __n1);
3719 const size_type __osize = traits_type::length(__s);
3720 const size_type __len = std::min(__n1, __osize);
3721 int __r = traits_type::compare(_M_data() + __pos, __s, __len);
3722 if (!__r)
3723 __r = _S_compare(__n1, __osize);
3724 return __r;
3725 }
3726
3727 /**
3728 * @brief Compare substring against a character %array.
3729 * @param __pos Index of first character of substring.
3730 * @param __n1 Number of characters in substring.
3731 * @param __s character %array to compare against.
3732 * @param __n2 Number of characters of s.
3733 * @return Integer < 0, 0, or > 0.
3734 *
3735 * Form the substring of this string from the @a __n1
3736 * characters starting at @a __pos. Form a string from the
3737 * first @a __n2 characters of @a __s. Returns an integer < 0
3738 * if this substring is ordered before the string from @a __s,
3739 * 0 if their values are equivalent, or > 0 if this substring
3740 * is ordered after the string from @a __s. Determines the
3741 * effective length rlen of the strings to compare as the
3742 * smallest of the length of the substring and @a __n2. The
3743 * function then compares the two strings by calling
3744 * traits::compare(substring.data(),s,rlen). If the result of
3745 * the comparison is nonzero returns it, otherwise the shorter
3746 * one is ordered first.
3747 *
3748 * NB: s must have at least n2 characters, &apos;\\0&apos; has
3749 * no special meaning.
3750 */
3751 _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr
3752 int
3753 compare(size_type __pos, size_type __n1, const _CharT* __s,
3754 size_type __n2) const
3755 {
3756 __glibcxx_requires_string_len(__s, __n2);
3757 _M_check(__pos, "basic_string::compare");
3758 __n1 = _M_limit(__pos, __n1);
3759 const size_type __len = std::min(__n1, __n2);
3760 int __r = traits_type::compare(_M_data() + __pos, __s, __len);
3761 if (!__r)
3762 __r = _S_compare(__n1, __n2);
3763 return __r;
3764 }
3765
3766#if __cplusplus202002L >= 202002L
3767 [[nodiscard]]
3768 constexpr bool
3769 starts_with(basic_string_view<_CharT, _Traits> __x) const noexcept
3770 { return __sv_type(this->data(), this->size()).starts_with(__x); }
3771
3772 [[nodiscard]]
3773 constexpr bool
3774 starts_with(_CharT __x) const noexcept
3775 { return __sv_type(this->data(), this->size()).starts_with(__x); }
3776
3777 [[nodiscard, __gnu__::__nonnull__]]
3778 constexpr bool
3779 starts_with(const _CharT* __x) const noexcept
3780 { return __sv_type(this->data(), this->size()).starts_with(__x); }
3781
3782 [[nodiscard]]
3783 constexpr bool
3784 ends_with(basic_string_view<_CharT, _Traits> __x) const noexcept
3785 { return __sv_type(this->data(), this->size()).ends_with(__x); }
3786
3787 [[nodiscard]]
3788 constexpr bool
3789 ends_with(_CharT __x) const noexcept
3790 { return __sv_type(this->data(), this->size()).ends_with(__x); }
3791
3792 [[nodiscard, __gnu__::__nonnull__]]
3793 constexpr bool
3794 ends_with(const _CharT* __x) const noexcept
3795 { return __sv_type(this->data(), this->size()).ends_with(__x); }
3796#endif // C++20
3797
3798#if __cplusplus202002L > 202002L
3799 [[nodiscard]]
3800 constexpr bool
3801 contains(basic_string_view<_CharT, _Traits> __x) const noexcept
3802 { return __sv_type(this->data(), this->size()).contains(__x); }
3803
3804 [[nodiscard]]
3805 constexpr bool
3806 contains(_CharT __x) const noexcept
3807 { return __sv_type(this->data(), this->size()).contains(__x); }
3808
3809 [[nodiscard, __gnu__::__nonnull__]]
3810 constexpr bool
3811 contains(const _CharT* __x) const noexcept
3812 { return __sv_type(this->data(), this->size()).contains(__x); }
3813#endif // C++23
3814
3815 // Allow basic_stringbuf::__xfer_bufptrs to call _M_length:
3816 template<typename, typename, typename> friend class basic_stringbuf;
3817 };
3818_GLIBCXX_END_NAMESPACE_CXX11}
3819_GLIBCXX_END_NAMESPACE_VERSION
3820} // namespace std
3821#endif // _GLIBCXX_USE_CXX11_ABI
3822
3823namespace std _GLIBCXX_VISIBILITY(default)__attribute__ ((__visibility__ ("default")))
3824{
3825_GLIBCXX_BEGIN_NAMESPACE_VERSION
3826
3827#if __cpp_deduction_guides201703L >= 201606
3828_GLIBCXX_BEGIN_NAMESPACE_CXX11namespace __cxx11 {
3829 template<typename _InputIterator, typename _CharT
3830 = typename iterator_traits<_InputIterator>::value_type,
3831 typename _Allocator = allocator<_CharT>,
3832 typename = _RequireInputIter<_InputIterator>,
3833 typename = _RequireAllocator<_Allocator>>
3834 basic_string(_InputIterator, _InputIterator, _Allocator = _Allocator())
3835 -> basic_string<_CharT, char_traits<_CharT>, _Allocator>;
3836
3837 // _GLIBCXX_RESOLVE_LIB_DEFECTS
3838 // 3075. basic_string needs deduction guides from basic_string_view
3839 template<typename _CharT, typename _Traits,
3840 typename _Allocator = allocator<_CharT>,
3841 typename = _RequireAllocator<_Allocator>>
3842 basic_string(basic_string_view<_CharT, _Traits>, const _Allocator& = _Allocator())
3843 -> basic_string<_CharT, _Traits, _Allocator>;
3844
3845 template<typename _CharT, typename _Traits,
3846 typename _Allocator = allocator<_CharT>,
3847 typename = _RequireAllocator<_Allocator>>
3848 basic_string(basic_string_view<_CharT, _Traits>,
3849 typename basic_string<_CharT, _Traits, _Allocator>::size_type,
3850 typename basic_string<_CharT, _Traits, _Allocator>::size_type,
3851 const _Allocator& = _Allocator())
3852 -> basic_string<_CharT, _Traits, _Allocator>;
3853
3854#if __glibcxx_containers_ranges // C++ >= 23
3855 template<ranges::input_range _Rg,
3856 typename _Allocator = allocator<ranges::range_value_t<_Rg>>>
3857 basic_string(from_range_t, _Rg&&, _Allocator = _Allocator())
3858 -> basic_string<ranges::range_value_t<_Rg>,
3859 char_traits<ranges::range_value_t<_Rg>>,
3860 _Allocator>;
3861#endif
3862_GLIBCXX_END_NAMESPACE_CXX11}
3863#endif
3864
3865 template<typename _Str>
3866 _GLIBCXX20_CONSTEXPRconstexpr
3867 inline _Str
3868 __str_concat(typename _Str::value_type const* __lhs,
3869 typename _Str::size_type __lhs_len,
3870 typename _Str::value_type const* __rhs,
3871 typename _Str::size_type __rhs_len,
3872 typename _Str::allocator_type const& __a)
3873 {
3874 typedef typename _Str::allocator_type allocator_type;
3875 typedef __gnu_cxx::__alloc_traits<allocator_type> _Alloc_traits;
3876 _Str __str(_Alloc_traits::_S_select_on_copy(__a));
3877 __str.reserve(__lhs_len + __rhs_len);
3878 __str.append(__lhs, __lhs_len);
3879 __str.append(__rhs, __rhs_len);
3880 return __str;
3881 }
3882
3883 // operator+
3884 /**
3885 * @brief Concatenate two strings.
3886 * @param __lhs First string.
3887 * @param __rhs Last string.
3888 * @return New string with value of @a __lhs followed by @a __rhs.
3889 */
3890 template<typename _CharT, typename _Traits, typename _Alloc>
3891 _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr
3892 inline basic_string<_CharT, _Traits, _Alloc>
3893 operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
3894 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
3895 {
3896 typedef basic_string<_CharT, _Traits, _Alloc> _Str;
3897 return std::__str_concat<_Str>(__lhs.c_str(), __lhs.size(),
3898 __rhs.c_str(), __rhs.size(),
3899 __lhs.get_allocator());
3900 }
3901
3902 /**
3903 * @brief Concatenate C string and string.
3904 * @param __lhs First string.
3905 * @param __rhs Last string.
3906 * @return New string with value of @a __lhs followed by @a __rhs.
3907 */
3908 template<typename _CharT, typename _Traits, typename _Alloc>
3909 _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr
3910 inline basic_string<_CharT,_Traits,_Alloc>
3911 operator+(const _CharT* __lhs,
3912 const basic_string<_CharT,_Traits,_Alloc>& __rhs)
3913 {
3914 __glibcxx_requires_string(__lhs);
3915 typedef basic_string<_CharT, _Traits, _Alloc> _Str;
3916 return std::__str_concat<_Str>(__lhs, _Traits::length(__lhs),
3917 __rhs.c_str(), __rhs.size(),
3918 __rhs.get_allocator());
3919 }
3920
3921 /**
3922 * @brief Concatenate character and string.
3923 * @param __lhs First string.
3924 * @param __rhs Last string.
3925 * @return New string with @a __lhs followed by @a __rhs.
3926 */
3927 template<typename _CharT, typename _Traits, typename _Alloc>
3928 _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr
3929 inline basic_string<_CharT,_Traits,_Alloc>
3930 operator+(_CharT __lhs, const basic_string<_CharT,_Traits,_Alloc>& __rhs)
3931 {
3932 typedef basic_string<_CharT, _Traits, _Alloc> _Str;
3933 return std::__str_concat<_Str>(__builtin_addressof(__lhs), 1,
3934 __rhs.c_str(), __rhs.size(),
3935 __rhs.get_allocator());
3936 }
3937
3938 /**
3939 * @brief Concatenate string and C string.
3940 * @param __lhs First string.
3941 * @param __rhs Last string.
3942 * @return New string with @a __lhs followed by @a __rhs.
3943 */
3944 template<typename _CharT, typename _Traits, typename _Alloc>
3945 _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr
3946 inline basic_string<_CharT, _Traits, _Alloc>
3947 operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
3948 const _CharT* __rhs)
3949 {
3950 __glibcxx_requires_string(__rhs);
3951 typedef basic_string<_CharT, _Traits, _Alloc> _Str;
3952 return std::__str_concat<_Str>(__lhs.c_str(), __lhs.size(),
3953 __rhs, _Traits::length(__rhs),
3954 __lhs.get_allocator());
3955 }
3956 /**
3957 * @brief Concatenate string and character.
3958 * @param __lhs First string.
3959 * @param __rhs Last string.
3960 * @return New string with @a __lhs followed by @a __rhs.
3961 */
3962 template<typename _CharT, typename _Traits, typename _Alloc>
3963 _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr
3964 inline basic_string<_CharT, _Traits, _Alloc>
3965 operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs, _CharT __rhs)
3966 {
3967 typedef basic_string<_CharT, _Traits, _Alloc> _Str;
3968 return std::__str_concat<_Str>(__lhs.c_str(), __lhs.size(),
3969 __builtin_addressof(__rhs), 1,
3970 __lhs.get_allocator());
3971 }
3972
3973#if __cplusplus202002L >= 201103L
3974 template<typename _CharT, typename _Traits, typename _Alloc>
3975 _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr
3976 inline basic_string<_CharT, _Traits, _Alloc>
3977 operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs,
3978 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
3979 { return std::move(__lhs.append(__rhs)); }
3980
3981 template<typename _CharT, typename _Traits, typename _Alloc>
3982 _GLIBCXX20_CONSTEXPRconstexpr
3983 inline basic_string<_CharT, _Traits, _Alloc>
3984 operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
3985 basic_string<_CharT, _Traits, _Alloc>&& __rhs)
3986 { return std::move(__rhs.insert(0, __lhs)); }
3987
3988 template<typename _CharT, typename _Traits, typename _Alloc>
3989 _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr
3990 inline basic_string<_CharT, _Traits, _Alloc>
3991 operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs,
3992 basic_string<_CharT, _Traits, _Alloc>&& __rhs)
3993 {
3994#pragma GCC diagnostic push
3995#pragma GCC diagnostic ignored "-Wc++17-extensions" // if constexpr
3996 // Return value must use __lhs.get_allocator(), but if __rhs has equal
3997 // allocator then we can choose which parameter to modify in-place.
3998 bool __use_rhs = false;
3999 if constexpr (allocator_traits<_Alloc>::is_always_equal::value)
4000 __use_rhs = true;
4001 else if (__lhs.get_allocator() == __rhs.get_allocator())
4002 __use_rhs = true;
4003 if (__use_rhs)
4004 {
4005 const auto __size = __lhs.size() + __rhs.size();
4006 if (__size > __lhs.capacity() && __size <= __rhs.capacity())
4007 return std::move(__rhs.insert(0, __lhs));
4008 }
4009 return std::move(__lhs.append(__rhs));
4010#pragma GCC diagnostic pop
4011 }
4012
4013 template<typename _CharT, typename _Traits, typename _Alloc>
4014 _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr
4015 inline basic_string<_CharT, _Traits, _Alloc>
4016 operator+(const _CharT* __lhs,
4017 basic_string<_CharT, _Traits, _Alloc>&& __rhs)
4018 { return std::move(__rhs.insert(0, __lhs)); }
4019
4020 template<typename _CharT, typename _Traits, typename _Alloc>
4021 _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr
4022 inline basic_string<_CharT, _Traits, _Alloc>
4023 operator+(_CharT __lhs,
4024 basic_string<_CharT, _Traits, _Alloc>&& __rhs)
4025 { return std::move(__rhs.insert(0, 1, __lhs)); }
4026
4027 template<typename _CharT, typename _Traits, typename _Alloc>
4028 _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr
4029 inline basic_string<_CharT, _Traits, _Alloc>
4030 operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs,
4031 const _CharT* __rhs)
4032 { return std::move(__lhs.append(__rhs)); }
4033
4034 template<typename _CharT, typename _Traits, typename _Alloc>
4035 _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr
4036 inline basic_string<_CharT, _Traits, _Alloc>
4037 operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs,
4038 _CharT __rhs)
4039 { return std::move(__lhs.append(1, __rhs)); }
4040#endif
4041
4042#if __glibcxx_string_view201803L >= 202403L
4043 // const string & + string_view
4044 template<typename _CharT, typename _Traits, typename _Alloc>
4045 [[nodiscard]]
4046 constexpr basic_string<_CharT, _Traits, _Alloc>
4047 operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
4048 type_identity_t<basic_string_view<_CharT, _Traits>> __rhs)
4049 {
4050 using _Str = basic_string<_CharT, _Traits, _Alloc>;
4051 return std::__str_concat<_Str>(__lhs.data(), __lhs.size(),
4052 __rhs.data(), __rhs.size(),
4053 __lhs.get_allocator());
4054 }
4055
4056 // string && + string_view
4057 template<typename _CharT, typename _Traits, typename _Alloc>
4058 [[nodiscard]]
4059 constexpr basic_string<_CharT, _Traits, _Alloc>
4060 operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs,
4061 type_identity_t<basic_string_view<_CharT, _Traits>> __rhs)
4062 {
4063 return std::move(__lhs.append(__rhs));
4064 }
4065
4066 // string_view + const string &
4067 template<typename _CharT, typename _Traits, typename _Alloc>
4068 [[nodiscard]]
4069 constexpr basic_string<_CharT, _Traits, _Alloc>
4070 operator+(type_identity_t<basic_string_view<_CharT, _Traits>> __lhs,
4071 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
4072 {
4073 using _Str = basic_string<_CharT, _Traits, _Alloc>;
4074 return std::__str_concat<_Str>(__lhs.data(), __lhs.size(),
4075 __rhs.data(), __rhs.size(),
4076 __rhs.get_allocator());
4077 }
4078
4079 // string_view + string &&
4080 template<typename _CharT, typename _Traits, typename _Alloc>
4081 [[nodiscard]]
4082 constexpr basic_string<_CharT, _Traits, _Alloc>
4083 operator+(type_identity_t<basic_string_view<_CharT, _Traits>> __lhs,
4084 basic_string<_CharT, _Traits, _Alloc>&& __rhs)
4085 {
4086 return std::move(__rhs.insert(0, __lhs));
4087 }
4088#endif
4089
4090 // operator ==
4091 /**
4092 * @brief Test equivalence of two strings.
4093 * @param __lhs First string.
4094 * @param __rhs Second string.
4095 * @return True if @a __lhs.compare(@a __rhs) == 0. False otherwise.
4096 */
4097 template<typename _CharT, typename _Traits, typename _Alloc>
4098 _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr
4099 inline bool
4100 operator==(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
4101 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
4102 _GLIBCXX_NOEXCEPTnoexcept
4103 {
4104 return __lhs.size() == __rhs.size()
4105 && !_Traits::compare(__lhs.data(), __rhs.data(), __lhs.size());
4106 }
4107
4108 /**
4109 * @brief Test equivalence of string and C string.
4110 * @param __lhs String.
4111 * @param __rhs C string.
4112 * @return True if @a __lhs.compare(@a __rhs) == 0. False otherwise.
4113 */
4114 template<typename _CharT, typename _Traits, typename _Alloc>
4115 _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr
4116 inline bool
4117 operator==(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
4118 const _CharT* __rhs)
4119 {
4120 return __lhs.size() == _Traits::length(__rhs)
4121 && !_Traits::compare(__lhs.data(), __rhs, __lhs.size());
4122 }
4123
4124#if __cpp_lib_three_way_comparison201907L
4125 /**
4126 * @brief Three-way comparison of a string and a C string.
4127 * @param __lhs A string.
4128 * @param __rhs A null-terminated string.
4129 * @return A value indicating whether `__lhs` is less than, equal to,
4130 * greater than, or incomparable with `__rhs`.
4131 */
4132 template<typename _CharT, typename _Traits, typename _Alloc>
4133 [[nodiscard]]
4134 constexpr auto
4135 operator<=>(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
4136 const basic_string<_CharT, _Traits, _Alloc>& __rhs) noexcept
4137 -> decltype(__detail::__char_traits_cmp_cat<_Traits>(0))
4138 { return __detail::__char_traits_cmp_cat<_Traits>(__lhs.compare(__rhs)); }
4139
4140 /**
4141 * @brief Three-way comparison of a string and a C string.
4142 * @param __lhs A string.
4143 * @param __rhs A null-terminated string.
4144 * @return A value indicating whether `__lhs` is less than, equal to,
4145 * greater than, or incomparable with `__rhs`.
4146 */
4147 template<typename _CharT, typename _Traits, typename _Alloc>
4148 [[nodiscard]]
4149 constexpr auto
4150 operator<=>(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
4151 const _CharT* __rhs) noexcept
4152 -> decltype(__detail::__char_traits_cmp_cat<_Traits>(0))
4153 { return __detail::__char_traits_cmp_cat<_Traits>(__lhs.compare(__rhs)); }
4154#else
4155 /**
4156 * @brief Test equivalence of C string and string.
4157 * @param __lhs C string.
4158 * @param __rhs String.
4159 * @return True if @a __rhs.compare(@a __lhs) == 0. False otherwise.
4160 */
4161 template<typename _CharT, typename _Traits, typename _Alloc>
4162 _GLIBCXX_NODISCARD[[__nodiscard__]]
4163 inline bool
4164 operator==(const _CharT* __lhs,
4165 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
4166 { return __rhs == __lhs; }
4167
4168 // operator !=
4169 /**
4170 * @brief Test difference of two strings.
4171 * @param __lhs First string.
4172 * @param __rhs Second string.
4173 * @return True if @a __lhs.compare(@a __rhs) != 0. False otherwise.
4174 */
4175 template<typename _CharT, typename _Traits, typename _Alloc>
4176 _GLIBCXX_NODISCARD[[__nodiscard__]]
4177 inline bool
4178 operator!=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
4179 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
4180 _GLIBCXX_NOEXCEPTnoexcept
4181 { return !(__lhs == __rhs); }
4182
4183 /**
4184 * @brief Test difference of C string and string.
4185 * @param __lhs C string.
4186 * @param __rhs String.
4187 * @return True if @a __rhs.compare(@a __lhs) != 0. False otherwise.
4188 */
4189 template<typename _CharT, typename _Traits, typename _Alloc>
4190 _GLIBCXX_NODISCARD[[__nodiscard__]]
4191 inline bool
4192 operator!=(const _CharT* __lhs,
4193 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
4194 { return !(__rhs == __lhs); }
4195
4196 /**
4197 * @brief Test difference of string and C string.
4198 * @param __lhs String.
4199 * @param __rhs C string.
4200 * @return True if @a __lhs.compare(@a __rhs) != 0. False otherwise.
4201 */
4202 template<typename _CharT, typename _Traits, typename _Alloc>
4203 _GLIBCXX_NODISCARD[[__nodiscard__]]
4204 inline bool
4205 operator!=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
4206 const _CharT* __rhs)
4207 { return !(__lhs == __rhs); }
4208
4209 // operator <
4210 /**
4211 * @brief Test if string precedes string.
4212 * @param __lhs First string.
4213 * @param __rhs Second string.
4214 * @return True if @a __lhs precedes @a __rhs. False otherwise.
4215 */
4216 template<typename _CharT, typename _Traits, typename _Alloc>
4217 _GLIBCXX_NODISCARD[[__nodiscard__]]
4218 inline bool
4219 operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
4220 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
4221 _GLIBCXX_NOEXCEPTnoexcept
4222 { return __lhs.compare(__rhs) < 0; }
4223
4224 /**
4225 * @brief Test if string precedes C string.
4226 * @param __lhs String.
4227 * @param __rhs C string.
4228 * @return True if @a __lhs precedes @a __rhs. False otherwise.
4229 */
4230 template<typename _CharT, typename _Traits, typename _Alloc>
4231 _GLIBCXX_NODISCARD[[__nodiscard__]]
4232 inline bool
4233 operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
4234 const _CharT* __rhs)
4235 { return __lhs.compare(__rhs) < 0; }
4236
4237 /**
4238 * @brief Test if C string precedes string.
4239 * @param __lhs C string.
4240 * @param __rhs String.
4241 * @return True if @a __lhs precedes @a __rhs. False otherwise.
4242 */
4243 template<typename _CharT, typename _Traits, typename _Alloc>
4244 _GLIBCXX_NODISCARD[[__nodiscard__]]
4245 inline bool
4246 operator<(const _CharT* __lhs,
4247 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
4248 { return __rhs.compare(__lhs) > 0; }
4249
4250 // operator >
4251 /**
4252 * @brief Test if string follows string.
4253 * @param __lhs First string.
4254 * @param __rhs Second string.
4255 * @return True if @a __lhs follows @a __rhs. False otherwise.
4256 */
4257 template<typename _CharT, typename _Traits, typename _Alloc>
4258 _GLIBCXX_NODISCARD[[__nodiscard__]]
4259 inline bool
4260 operator>(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
4261 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
4262 _GLIBCXX_NOEXCEPTnoexcept
4263 { return __lhs.compare(__rhs) > 0; }
4264
4265 /**
4266 * @brief Test if string follows C string.
4267 * @param __lhs String.
4268 * @param __rhs C string.
4269 * @return True if @a __lhs follows @a __rhs. False otherwise.
4270 */
4271 template<typename _CharT, typename _Traits, typename _Alloc>
4272 _GLIBCXX_NODISCARD[[__nodiscard__]]
4273 inline bool
4274 operator>(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
4275 const _CharT* __rhs)
4276 { return __lhs.compare(__rhs) > 0; }
4277
4278 /**
4279 * @brief Test if C string follows string.
4280 * @param __lhs C string.
4281 * @param __rhs String.
4282 * @return True if @a __lhs follows @a __rhs. False otherwise.
4283 */
4284 template<typename _CharT, typename _Traits, typename _Alloc>
4285 _GLIBCXX_NODISCARD[[__nodiscard__]]
4286 inline bool
4287 operator>(const _CharT* __lhs,
4288 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
4289 { return __rhs.compare(__lhs) < 0; }
4290
4291 // operator <=
4292 /**
4293 * @brief Test if string doesn't follow string.
4294 * @param __lhs First string.
4295 * @param __rhs Second string.
4296 * @return True if @a __lhs doesn't follow @a __rhs. False otherwise.
4297 */
4298 template<typename _CharT, typename _Traits, typename _Alloc>
4299 _GLIBCXX_NODISCARD[[__nodiscard__]]
4300 inline bool
4301 operator<=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
4302 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
4303 _GLIBCXX_NOEXCEPTnoexcept
4304 { return __lhs.compare(__rhs) <= 0; }
4305
4306 /**
4307 * @brief Test if string doesn't follow C string.
4308 * @param __lhs String.
4309 * @param __rhs C string.
4310 * @return True if @a __lhs doesn't follow @a __rhs. False otherwise.
4311 */
4312 template<typename _CharT, typename _Traits, typename _Alloc>
4313 _GLIBCXX_NODISCARD[[__nodiscard__]]
4314 inline bool
4315 operator<=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
4316 const _CharT* __rhs)
4317 { return __lhs.compare(__rhs) <= 0; }
4318
4319 /**
4320 * @brief Test if C string doesn't follow string.
4321 * @param __lhs C string.
4322 * @param __rhs String.
4323 * @return True if @a __lhs doesn't follow @a __rhs. False otherwise.
4324 */
4325 template<typename _CharT, typename _Traits, typename _Alloc>
4326 _GLIBCXX_NODISCARD[[__nodiscard__]]
4327 inline bool
4328 operator<=(const _CharT* __lhs,
4329 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
4330 { return __rhs.compare(__lhs) >= 0; }
4331
4332 // operator >=
4333 /**
4334 * @brief Test if string doesn't precede string.
4335 * @param __lhs First string.
4336 * @param __rhs Second string.
4337 * @return True if @a __lhs doesn't precede @a __rhs. False otherwise.
4338 */
4339 template<typename _CharT, typename _Traits, typename _Alloc>
4340 _GLIBCXX_NODISCARD[[__nodiscard__]]
4341 inline bool
4342 operator>=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
4343 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
4344 _GLIBCXX_NOEXCEPTnoexcept
4345 { return __lhs.compare(__rhs) >= 0; }
4346
4347 /**
4348 * @brief Test if string doesn't precede C string.
4349 * @param __lhs String.
4350 * @param __rhs C string.
4351 * @return True if @a __lhs doesn't precede @a __rhs. False otherwise.
4352 */
4353 template<typename _CharT, typename _Traits, typename _Alloc>
4354 _GLIBCXX_NODISCARD[[__nodiscard__]]
4355 inline bool
4356 operator>=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
4357 const _CharT* __rhs)
4358 { return __lhs.compare(__rhs) >= 0; }
4359
4360 /**
4361 * @brief Test if C string doesn't precede string.
4362 * @param __lhs C string.
4363 * @param __rhs String.
4364 * @return True if @a __lhs doesn't precede @a __rhs. False otherwise.
4365 */
4366 template<typename _CharT, typename _Traits, typename _Alloc>
4367 _GLIBCXX_NODISCARD[[__nodiscard__]]
4368 inline bool
4369 operator>=(const _CharT* __lhs,
4370 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
4371 { return __rhs.compare(__lhs) <= 0; }
4372#endif // three-way comparison
4373
4374 /**
4375 * @brief Swap contents of two strings.
4376 * @param __lhs First string.
4377 * @param __rhs Second string.
4378 *
4379 * Exchanges the contents of @a __lhs and @a __rhs in constant time.
4380 */
4381 template<typename _CharT, typename _Traits, typename _Alloc>
4382 _GLIBCXX20_CONSTEXPRconstexpr
4383 inline void
4384 swap(basic_string<_CharT, _Traits, _Alloc>& __lhs,
4385 basic_string<_CharT, _Traits, _Alloc>& __rhs)
4386 _GLIBCXX_NOEXCEPT_IF(noexcept(__lhs.swap(__rhs)))noexcept(noexcept(__lhs.swap(__rhs)))
4387 { __lhs.swap(__rhs); }
4388
4389
4390 /**
4391 * @brief Read stream into a string.
4392 * @param __is Input stream.
4393 * @param __str Buffer to store into.
4394 * @return Reference to the input stream.
4395 *
4396 * Stores characters from @a __is into @a __str until whitespace is
4397 * found, the end of the stream is encountered, or str.max_size()
4398 * is reached. If is.width() is non-zero, that is the limit on the
4399 * number of characters stored into @a __str. Any previous
4400 * contents of @a __str are erased.
4401 */
4402 template<typename _CharT, typename _Traits, typename _Alloc>
4403 basic_istream<_CharT, _Traits>&
4404 operator>>(basic_istream<_CharT, _Traits>& __is,
4405 basic_string<_CharT, _Traits, _Alloc>& __str);
4406
4407 template<>
4408 basic_istream<char>&
4409 operator>>(basic_istream<char>& __is, basic_string<char>& __str);
4410
4411 /**
4412 * @brief Write string to a stream.
4413 * @param __os Output stream.
4414 * @param __str String to write out.
4415 * @return Reference to the output stream.
4416 *
4417 * Output characters of @a __str into os following the same rules as for
4418 * writing a C string.
4419 */
4420 template<typename _CharT, typename _Traits, typename _Alloc>
4421 inline basic_ostream<_CharT, _Traits>&
4422 operator<<(basic_ostream<_CharT, _Traits>& __os,
4423 const basic_string<_CharT, _Traits, _Alloc>& __str)
4424 {
4425 // _GLIBCXX_RESOLVE_LIB_DEFECTS
4426 // 586. string inserter not a formatted function
4427 return __ostream_insert(__os, __str.data(), __str.size());
4428 }
4429
4430 /**
4431 * @brief Read a line from stream into a string.
4432 * @param __is Input stream.
4433 * @param __str Buffer to store into.
4434 * @param __delim Character marking end of line.
4435 * @return Reference to the input stream.
4436 *
4437 * Stores characters from @a __is into @a __str until @a __delim is
4438 * found, the end of the stream is encountered, or str.max_size()
4439 * is reached. Any previous contents of @a __str are erased. If
4440 * @a __delim is encountered, it is extracted but not stored into
4441 * @a __str.
4442 */
4443 template<typename _CharT, typename _Traits, typename _Alloc>
4444 basic_istream<_CharT, _Traits>&
4445 getline(basic_istream<_CharT, _Traits>& __is,
4446 basic_string<_CharT, _Traits, _Alloc>& __str, _CharT __delim);
4447
4448 /**
4449 * @brief Read a line from stream into a string.
4450 * @param __is Input stream.
4451 * @param __str Buffer to store into.
4452 * @return Reference to the input stream.
4453 *
4454 * Stores characters from is into @a __str until &apos;\n&apos; is
4455 * found, the end of the stream is encountered, or str.max_size()
4456 * is reached. Any previous contents of @a __str are erased. If
4457 * end of line is encountered, it is extracted but not stored into
4458 * @a __str.
4459 */
4460 template<typename _CharT, typename _Traits, typename _Alloc>
4461 inline basic_istream<_CharT, _Traits>&
4462 getline(basic_istream<_CharT, _Traits>& __is,
4463 basic_string<_CharT, _Traits, _Alloc>& __str)
4464 { return std::getline(__is, __str, __is.widen('\n')); }
4465
4466#if __cplusplus202002L >= 201103L
4467 /// Read a line from an rvalue stream into a string.
4468 template<typename _CharT, typename _Traits, typename _Alloc>
4469 inline basic_istream<_CharT, _Traits>&
4470 getline(basic_istream<_CharT, _Traits>&& __is,
4471 basic_string<_CharT, _Traits, _Alloc>& __str, _CharT __delim)
4472 { return std::getline(__is, __str, __delim); }
4473
4474 /// Read a line from an rvalue stream into a string.
4475 template<typename _CharT, typename _Traits, typename _Alloc>
4476 inline basic_istream<_CharT, _Traits>&
4477 getline(basic_istream<_CharT, _Traits>&& __is,
4478 basic_string<_CharT, _Traits, _Alloc>& __str)
4479 { return std::getline(__is, __str); }
4480#endif
4481
4482 template<>
4483 basic_istream<char>&
4484 getline(basic_istream<char>& __in, basic_string<char>& __str,
4485 char __delim);
4486
4487#ifdef _GLIBCXX_USE_WCHAR_T1
4488 template<>
4489 basic_istream<wchar_t>&
4490 getline(basic_istream<wchar_t>& __in, basic_string<wchar_t>& __str,
4491 wchar_t __delim);
4492#endif
4493
4494_GLIBCXX_END_NAMESPACE_VERSION
4495} // namespace
4496
4497#if __cplusplus202002L >= 201103L
4498
4499#include <ext/string_conversions.h>
4500#include <bits/charconv.h>
4501
4502namespace std _GLIBCXX_VISIBILITY(default)__attribute__ ((__visibility__ ("default")))
4503{
4504_GLIBCXX_BEGIN_NAMESPACE_VERSION
4505_GLIBCXX_BEGIN_NAMESPACE_CXX11namespace __cxx11 {
4506
4507 // 21.4 Numeric Conversions [string.conversions].
4508 inline int
4509 stoi(const string& __str, size_t* __idx = 0, int __base = 10)
4510 { return __gnu_cxx::__stoa<long, int>(&std::strtol, "stoi", __str.c_str(),
4511 __idx, __base); }
4512
4513 inline long
4514 stol(const string& __str, size_t* __idx = 0, int __base = 10)
4515 { return __gnu_cxx::__stoa(&std::strtol, "stol", __str.c_str(),
4516 __idx, __base); }
4517
4518 inline unsigned long
4519 stoul(const string& __str, size_t* __idx = 0, int __base = 10)
4520 { return __gnu_cxx::__stoa(&std::strtoul, "stoul", __str.c_str(),
4521 __idx, __base); }
4522
4523#if _GLIBCXX_USE_C99_STDLIB1
4524 inline long long
4525 stoll(const string& __str, size_t* __idx = 0, int __base = 10)
4526 { return __gnu_cxx::__stoa(&std::strtoll, "stoll", __str.c_str(),
4527 __idx, __base); }
4528
4529 inline unsigned long long
4530 stoull(const string& __str, size_t* __idx = 0, int __base = 10)
4531 { return __gnu_cxx::__stoa(&std::strtoull, "stoull", __str.c_str(),
4532 __idx, __base); }
4533#elif __LONG_WIDTH__64 == __LONG_LONG_WIDTH__
4534 inline long long
4535 stoll(const string& __str, size_t* __idx = 0, int __base = 10)
4536 { return std::stol(__str, __idx, __base); }
4537
4538 inline unsigned long long
4539 stoull(const string& __str, size_t* __idx = 0, int __base = 10)
4540 { return std::stoul(__str, __idx, __base); }
4541#endif
4542
4543 inline double
4544 stod(const string& __str, size_t* __idx = 0)
4545 { return __gnu_cxx::__stoa(&std::strtod, "stod", __str.c_str(), __idx); }
4546
4547#if _GLIBCXX_HAVE_STRTOF1
4548 // NB: strtof vs strtod.
4549 inline float
4550 stof(const string& __str, size_t* __idx = 0)
4551 { return __gnu_cxx::__stoa(&std::strtof, "stof", __str.c_str(), __idx); }
4552#else
4553 inline float
4554 stof(const string& __str, size_t* __idx = 0)
4555 {
4556 double __d = std::stod(__str, __idx);
4557 if (__builtin_isfinite(__d) && __d != 0.0)
4558 {
4559 double __abs_d = __builtin_fabs(__d);
4560 if (__abs_d < __FLT_MIN__1.17549435e-38F || __abs_d > __FLT_MAX__3.40282347e+38F)
4561 {
4562 errno(*__errno_location ()) = ERANGE34;
4563 std::__throw_out_of_range("stof");
4564 }
4565 }
4566 return __d;
4567 }
4568#endif
4569
4570#if _GLIBCXX_HAVE_STRTOLD1 && ! _GLIBCXX_HAVE_BROKEN_STRTOLD
4571 inline long double
4572 stold(const string& __str, size_t* __idx = 0)
4573 { return __gnu_cxx::__stoa(&std::strtold, "stold", __str.c_str(), __idx); }
4574#elif __DBL_MANT_DIG__53 == __LDBL_MANT_DIG__64
4575 inline long double
4576 stold(const string& __str, size_t* __idx = 0)
4577 { return std::stod(__str, __idx); }
4578#endif
4579
4580 // _GLIBCXX_RESOLVE_LIB_DEFECTS
4581 // DR 1261. Insufficent overloads for to_string / to_wstring
4582
4583 _GLIBCXX_NODISCARD[[__nodiscard__]]
4584 inline string
4585 to_string(int __val)
4586#if _GLIBCXX_USE_CXX11_ABI1 && (__CHAR_BIT__8 * __SIZEOF_INT__4) <= 32
4587 noexcept // any 32-bit value fits in the SSO buffer
4588#endif
4589 {
4590 const bool __neg = __val < 0;
4591 const unsigned __uval = __neg ? (unsigned)~__val + 1u : __val;
4592 const auto __len = __detail::__to_chars_len(__uval);
4593 string __str;
4594 __str.__resize_and_overwrite(__neg + __len, [=](char* __p, size_t __n) {
4595 __p[0] = '-';
4596 __detail::__to_chars_10_impl(__p + (int)__neg, __len, __uval);
4597 return __n;
4598 });
4599 return __str;
4600 }
4601
4602 _GLIBCXX_NODISCARD[[__nodiscard__]]
4603 inline string
4604 to_string(unsigned __val)
4605#if _GLIBCXX_USE_CXX11_ABI1 && (__CHAR_BIT__8 * __SIZEOF_INT__4) <= 32
4606 noexcept // any 32-bit value fits in the SSO buffer
4607#endif
4608 {
4609 const auto __len = __detail::__to_chars_len(__val);
4610 string __str;
4611 __str.__resize_and_overwrite(__len, [__val](char* __p, size_t __n) {
4612 __detail::__to_chars_10_impl(__p, __n, __val);
4613 return __n;
4614 });
4615 return __str;
4616 }
4617
4618 _GLIBCXX_NODISCARD[[__nodiscard__]]
4619 inline string
4620 to_string(long __val)
4621#if _GLIBCXX_USE_CXX11_ABI1 && (__CHAR_BIT__8 * __SIZEOF_LONG__8) <= 32
4622 noexcept // any 32-bit value fits in the SSO buffer
4623#endif
4624 {
4625 const bool __neg = __val < 0;
4626 const unsigned long __uval = __neg ? (unsigned long)~__val + 1ul : __val;
4627 const auto __len = __detail::__to_chars_len(__uval);
4628 string __str;
4629 __str.__resize_and_overwrite(__neg + __len, [=](char* __p, size_t __n) {
4630 __p[0] = '-';
4631 __detail::__to_chars_10_impl(__p + (int)__neg, __len, __uval);
4632 return __n;
4633 });
4634 return __str;
4635 }
4636
4637 _GLIBCXX_NODISCARD[[__nodiscard__]]
4638 inline string
4639 to_string(unsigned long __val)
4640#if _GLIBCXX_USE_CXX11_ABI1 && (__CHAR_BIT__8 * __SIZEOF_LONG__8) <= 32
4641 noexcept // any 32-bit value fits in the SSO buffer
4642#endif
4643 {
4644 const auto __len = __detail::__to_chars_len(__val);
4645 string __str;
4646 __str.__resize_and_overwrite(__len, [__val](char* __p, size_t __n) {
4647 __detail::__to_chars_10_impl(__p, __n, __val);
4648 return __n;
4649 });
4650 return __str;
4651 }
4652
4653 _GLIBCXX_NODISCARD[[__nodiscard__]]
4654 inline string
4655 to_string(long long __val)
4656 {
4657 const bool __neg = __val < 0;
4658 const unsigned long long __uval
4659 = __neg ? (unsigned long long)~__val + 1ull : __val;
4660 const auto __len = __detail::__to_chars_len(__uval);
4661 string __str;
4662 __str.__resize_and_overwrite(__neg + __len, [=](char* __p, size_t __n) {
4663 __p[0] = '-';
4664 __detail::__to_chars_10_impl(__p + (int)__neg, __len, __uval);
4665 return __n;
4666 });
4667 return __str;
4668 }
4669
4670 _GLIBCXX_NODISCARD[[__nodiscard__]]
4671 inline string
4672 to_string(unsigned long long __val)
4673 {
4674 const auto __len = __detail::__to_chars_len(__val);
4675 string __str;
4676 __str.__resize_and_overwrite(__len, [__val](char* __p, size_t __n) {
4677 __detail::__to_chars_10_impl(__p, __n, __val);
4678 return __n;
4679 });
4680 return __str;
4681 }
4682
4683#if __glibcxx_to_string >= 202306L // C++ >= 26
4684
4685 [[nodiscard]]
4686 inline string
4687 to_string(float __val)
4688 {
4689 string __str;
4690 size_t __len = 15;
4691 do {
4692 __str.resize_and_overwrite(__len,
4693 [__val, &__len] (char* __p, size_t __n) {
4694 auto [__end, __err] = std::to_chars(__p, __p + __n, __val);
4695 if (__err == errc{}) [[likely]]
4696 return __end - __p;
4697 __len *= 2;
4698 return __p - __p;;
4699 });
4700 } while (__str.empty());
4701 return __str;
4702 }
4703
4704 [[nodiscard]]
4705 inline string
4706 to_string(double __val)
4707 {
4708 string __str;
4709 size_t __len = 15;
4710 do {
4711 __str.resize_and_overwrite(__len,
4712 [__val, &__len] (char* __p, size_t __n) {
4713 auto [__end, __err] = std::to_chars(__p, __p + __n, __val);
4714 if (__err == errc{}) [[likely]]
4715 return __end - __p;
4716 __len *= 2;
4717 return __p - __p;;
4718 });
4719 } while (__str.empty());
4720 return __str;
4721 }
4722
4723 [[nodiscard]]
4724 inline string
4725 to_string(long double __val)
4726 {
4727 string __str;
4728 size_t __len = 15;
4729 do {
4730 __str.resize_and_overwrite(__len,
4731 [__val, &__len] (char* __p, size_t __n) {
4732 auto [__end, __err] = std::to_chars(__p, __p + __n, __val);
4733 if (__err == errc{}) [[likely]]
4734 return __end - __p;
4735 __len *= 2;
4736 return __p - __p;;
4737 });
4738 } while (__str.empty());
4739 return __str;
4740 }
4741#elif _GLIBCXX_USE_C99_STDIO1
4742#pragma GCC diagnostic push
4743#pragma GCC diagnostic ignored "-Wsuggest-attribute=format"
4744 // NB: (v)snprintf vs sprintf.
4745
4746 _GLIBCXX_NODISCARD[[__nodiscard__]]
4747 inline string
4748 to_string(float __val)
4749 {
4750 const int __n =
4751 __gnu_cxx::__numeric_traits<float>::__max_exponent10 + 20;
4752 return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, __n,
4753 "%f", __val);
4754 }
4755
4756 _GLIBCXX_NODISCARD[[__nodiscard__]]
4757 inline string
4758 to_string(double __val)
4759 {
4760 const int __n =
4761 __gnu_cxx::__numeric_traits<double>::__max_exponent10 + 20;
4762 return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, __n,
4763 "%f", __val);
4764 }
4765
4766 _GLIBCXX_NODISCARD[[__nodiscard__]]
4767 inline string
4768 to_string(long double __val)
4769 {
4770 const int __n =
4771 __gnu_cxx::__numeric_traits<long double>::__max_exponent10 + 20;
4772 return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, __n,
4773 "%Lf", __val);
4774 }
4775#pragma GCC diagnostic pop
4776#endif // _GLIBCXX_USE_C99_STDIO
4777
4778#if defined(_GLIBCXX_USE_WCHAR_T1) && _GLIBCXX_USE_C99_WCHAR1
4779 inline int
4780 stoi(const wstring& __str, size_t* __idx = 0, int __base = 10)
4781 { return __gnu_cxx::__stoa<long, int>(&std::wcstol, "stoi", __str.c_str(),
4782 __idx, __base); }
4783
4784 inline long
4785 stol(const wstring& __str, size_t* __idx = 0, int __base = 10)
4786 { return __gnu_cxx::__stoa(&std::wcstol, "stol", __str.c_str(),
4787 __idx, __base); }
4788
4789 inline unsigned long
4790 stoul(const wstring& __str, size_t* __idx = 0, int __base = 10)
4791 { return __gnu_cxx::__stoa(&std::wcstoul, "stoul", __str.c_str(),
4792 __idx, __base); }
4793
4794 inline long long
4795 stoll(const wstring& __str, size_t* __idx = 0, int __base = 10)
4796 { return __gnu_cxx::__stoa(&std::wcstoll, "stoll", __str.c_str(),
4797 __idx, __base); }
4798
4799 inline unsigned long long
4800 stoull(const wstring& __str, size_t* __idx = 0, int __base = 10)
4801 { return __gnu_cxx::__stoa(&std::wcstoull, "stoull", __str.c_str(),
4802 __idx, __base); }
4803
4804 // NB: wcstof vs wcstod.
4805 inline float
4806 stof(const wstring& __str, size_t* __idx = 0)
4807 { return __gnu_cxx::__stoa(&std::wcstof, "stof", __str.c_str(), __idx); }
4808
4809 inline double
4810 stod(const wstring& __str, size_t* __idx = 0)
4811 { return __gnu_cxx::__stoa(&std::wcstod, "stod", __str.c_str(), __idx); }
4812
4813 inline long double
4814 stold(const wstring& __str, size_t* __idx = 0)
4815 { return __gnu_cxx::__stoa(&std::wcstold, "stold", __str.c_str(), __idx); }
4816#endif
4817
4818#ifdef _GLIBCXX_USE_WCHAR_T1
4819#pragma GCC diagnostic push
4820#pragma GCC diagnostic ignored "-Wc++17-extensions"
4821 _GLIBCXX20_CONSTEXPRconstexpr
4822 inline void
4823 __to_wstring_numeric(const char* __s, int __len, wchar_t* __wout)
4824 {
4825 // This condition is true if exec-charset and wide-exec-charset share the
4826 // same values for the ASCII subset or the EBCDIC invariant character set.
4827 if constexpr (wchar_t('0') == L'0' && wchar_t('-') == L'-'
4828 && wchar_t('.') == L'.' && wchar_t('e') == L'e')
4829 {
4830 for (int __i = 0; __i < __len; ++__i)
4831 __wout[__i] = (wchar_t) __s[__i];
4832 }
4833 else
4834 {
4835 wchar_t __wc[256];
4836 for (int __i = '0'; __i <= '9'; ++__i)
4837 __wc[__i] = L'0' + __i;
4838 __wc['.'] = L'.';
4839 __wc['+'] = L'+';
4840 __wc['-'] = L'-';
4841 __wc['a'] = L'a';
4842 __wc['b'] = L'b';
4843 __wc['c'] = L'c';
4844 __wc['d'] = L'd';
4845 __wc['e'] = L'e';
4846 __wc['f'] = L'f';
4847 __wc['i'] = L'i'; // for "inf"
4848 __wc['n'] = L'n'; // for "nan" and "inf"
4849 __wc['p'] = L'p'; // for hexfloats "0x1p1"
4850 __wc['x'] = L'x';
4851 __wc['A'] = L'A';
4852 __wc['B'] = L'B';
4853 __wc['C'] = L'C';
4854 __wc['D'] = L'D';
4855 __wc['E'] = L'E';
4856 __wc['F'] = L'F';
4857 __wc['I'] = L'I';
4858 __wc['N'] = L'N';
4859 __wc['P'] = L'P';
4860 __wc['X'] = L'X';
4861
4862 for (int __i = 0; __i < __len; ++__i)
4863 __wout[__i] = __wc[(int)__s[__i]];
4864 }
4865 }
4866
4867#if __glibcxx_constexpr_string201907L >= 201907L
4868 constexpr
4869#endif
4870 inline wstring
4871#ifdef __glibcxx_string_view201803L // >= C++17
4872 __to_wstring_numeric(string_view __s)
4873#else
4874 __to_wstring_numeric(const string& __s)
4875#endif
4876 {
4877 if constexpr (wchar_t('0') == L'0' && wchar_t('-') == L'-'
4878 && wchar_t('.') == L'.' && wchar_t('e') == L'e')
4879 return wstring(__s.data(), __s.data() + __s.size());
4880 else
4881 {
4882 wstring __ws;
4883 auto __f = __s.data();
4884 __ws.__resize_and_overwrite(__s.size(),
4885 [__f] (wchar_t* __to, int __n) {
4886 std::__to_wstring_numeric(__f, __n, __to);
4887 return __n;
4888 });
4889 return __ws;
4890 }
4891 }
4892#pragma GCC diagnostic pop
4893
4894 _GLIBCXX_NODISCARD[[__nodiscard__]]
4895 inline wstring
4896 to_wstring(int __val)
4897 { return std::__to_wstring_numeric(std::to_string(__val)); }
4898
4899 _GLIBCXX_NODISCARD[[__nodiscard__]]
4900 inline wstring
4901 to_wstring(unsigned __val)
4902 { return std::__to_wstring_numeric(std::to_string(__val)); }
4903
4904 _GLIBCXX_NODISCARD[[__nodiscard__]]
4905 inline wstring
4906 to_wstring(long __val)
4907 { return std::__to_wstring_numeric(std::to_string(__val)); }
4908
4909 _GLIBCXX_NODISCARD[[__nodiscard__]]
4910 inline wstring
4911 to_wstring(unsigned long __val)
4912 { return std::__to_wstring_numeric(std::to_string(__val)); }
4913
4914 _GLIBCXX_NODISCARD[[__nodiscard__]]
4915 inline wstring
4916 to_wstring(long long __val)
4917 { return std::__to_wstring_numeric(std::to_string(__val)); }
4918
4919 _GLIBCXX_NODISCARD[[__nodiscard__]]
4920 inline wstring
4921 to_wstring(unsigned long long __val)
4922 { return std::__to_wstring_numeric(std::to_string(__val)); }
4923
4924#if __glibcxx_to_string || _GLIBCXX_USE_C99_STDIO1
4925 _GLIBCXX_NODISCARD[[__nodiscard__]]
4926 inline wstring
4927 to_wstring(float __val)
4928 { return std::__to_wstring_numeric(std::to_string(__val)); }
4929
4930 _GLIBCXX_NODISCARD[[__nodiscard__]]
4931 inline wstring
4932 to_wstring(double __val)
4933 { return std::__to_wstring_numeric(std::to_string(__val)); }
4934
4935 _GLIBCXX_NODISCARD[[__nodiscard__]]
4936 inline wstring
4937 to_wstring(long double __val)
4938 { return std::__to_wstring_numeric(std::to_string(__val)); }
4939#endif
4940#endif // _GLIBCXX_USE_WCHAR_T
4941
4942_GLIBCXX_END_NAMESPACE_CXX11}
4943_GLIBCXX_END_NAMESPACE_VERSION
4944} // namespace
4945
4946#endif /* C++11 */
4947
4948#if __cplusplus202002L >= 201103L
4949
4950#include <bits/functional_hash.h>
4951
4952namespace std _GLIBCXX_VISIBILITY(default)__attribute__ ((__visibility__ ("default")))
4953{
4954_GLIBCXX_BEGIN_NAMESPACE_VERSION
4955
4956 // _GLIBCXX_RESOLVE_LIB_DEFECTS
4957 // 3705. Hashability shouldn't depend on basic_string's allocator
4958
4959 template<typename _CharT, typename _Alloc,
4960 typename _StrT = basic_string<_CharT, char_traits<_CharT>, _Alloc>>
4961 struct __str_hash_base
4962 : public __hash_base<size_t, _StrT>
4963 {
4964 [[__nodiscard__]]
4965 size_t
4966 operator()(const _StrT& __s) const noexcept
4967 { return _Hash_impl::hash(__s.data(), __s.length() * sizeof(_CharT)); }
4968 };
4969
4970#ifndef _GLIBCXX_COMPATIBILITY_CXX0X
4971 /// std::hash specialization for string.
4972 template<typename _Alloc>
4973 struct hash<basic_string<char, char_traits<char>, _Alloc>>
4974 : public __str_hash_base<char, _Alloc>
4975 { };
4976
4977 /// std::hash specialization for wstring.
4978 template<typename _Alloc>
4979 struct hash<basic_string<wchar_t, char_traits<wchar_t>, _Alloc>>
4980 : public __str_hash_base<wchar_t, _Alloc>
4981 { };
4982
4983 template<typename _Alloc>
4984 struct __is_fast_hash<hash<basic_string<wchar_t, char_traits<wchar_t>,
4985 _Alloc>>>
4986 : std::false_type
4987 { };
4988#endif /* _GLIBCXX_COMPATIBILITY_CXX0X */
4989
4990#ifdef _GLIBCXX_USE_CHAR8_T1
4991 /// std::hash specialization for u8string.
4992 template<typename _Alloc>
4993 struct hash<basic_string<char8_t, char_traits<char8_t>, _Alloc>>
4994 : public __str_hash_base<char8_t, _Alloc>
4995 { };
4996#endif
4997
4998 /// std::hash specialization for u16string.
4999 template<typename _Alloc>
5000 struct hash<basic_string<char16_t, char_traits<char16_t>, _Alloc>>
5001 : public __str_hash_base<char16_t, _Alloc>
5002 { };
5003
5004 /// std::hash specialization for u32string.
5005 template<typename _Alloc>
5006 struct hash<basic_string<char32_t, char_traits<char32_t>, _Alloc>>
5007 : public __str_hash_base<char32_t, _Alloc>
5008 { };
5009
5010#if ! _GLIBCXX_INLINE_VERSION0
5011 // PR libstdc++/105907 - __is_fast_hash affects unordered container ABI.
5012 template<> struct __is_fast_hash<hash<string>> : std::false_type { };
5013 template<> struct __is_fast_hash<hash<wstring>> : std::false_type { };
5014 template<> struct __is_fast_hash<hash<u16string>> : std::false_type { };
5015 template<> struct __is_fast_hash<hash<u32string>> : std::false_type { };
5016#ifdef _GLIBCXX_USE_CHAR8_T1
5017 template<> struct __is_fast_hash<hash<u8string>> : std::false_type { };
5018#endif
5019#else
5020 // For versioned namespace, assume every std::hash<basic_string<>> is slow.
5021 template<typename _CharT, typename _Traits, typename _Alloc>
5022 struct __is_fast_hash<hash<basic_string<_CharT, _Traits, _Alloc>>>
5023 : std::false_type
5024 { };
5025#endif
5026
5027#ifdef __glibcxx_string_udls201304L // C++ >= 14
5028 inline namespace literals
5029 {
5030 inline namespace string_literals
5031 {
5032#pragma GCC diagnostic push
5033#pragma GCC diagnostic ignored "-Wliteral-suffix"
5034
5035#if __glibcxx_constexpr_string201907L >= 201907L
5036# define _GLIBCXX_STRING_CONSTEXPR constexpr
5037#else
5038# define _GLIBCXX_STRING_CONSTEXPR
5039#endif
5040
5041 _GLIBCXX_DEFAULT_ABI_TAG__attribute ((__abi_tag__ ("cxx11"))) _GLIBCXX_STRING_CONSTEXPR
5042 inline basic_string<char>
5043 operator""s(const char* __str, size_t __len)
5044 { return basic_string<char>{__str, __len}; }
5045
5046 _GLIBCXX_DEFAULT_ABI_TAG__attribute ((__abi_tag__ ("cxx11"))) _GLIBCXX_STRING_CONSTEXPR
5047 inline basic_string<wchar_t>
5048 operator""s(const wchar_t* __str, size_t __len)
5049 { return basic_string<wchar_t>{__str, __len}; }
5050
5051#ifdef _GLIBCXX_USE_CHAR8_T1
5052 _GLIBCXX_DEFAULT_ABI_TAG__attribute ((__abi_tag__ ("cxx11"))) _GLIBCXX_STRING_CONSTEXPR
5053 inline basic_string<char8_t>
5054 operator""s(const char8_t* __str, size_t __len)
5055 { return basic_string<char8_t>{__str, __len}; }
5056#endif
5057
5058 _GLIBCXX_DEFAULT_ABI_TAG__attribute ((__abi_tag__ ("cxx11"))) _GLIBCXX_STRING_CONSTEXPR
5059 inline basic_string<char16_t>
5060 operator""s(const char16_t* __str, size_t __len)
5061 { return basic_string<char16_t>{__str, __len}; }
5062
5063 _GLIBCXX_DEFAULT_ABI_TAG__attribute ((__abi_tag__ ("cxx11"))) _GLIBCXX_STRING_CONSTEXPR
5064 inline basic_string<char32_t>
5065 operator""s(const char32_t* __str, size_t __len)
5066 { return basic_string<char32_t>{__str, __len}; }
5067
5068#undef _GLIBCXX_STRING_CONSTEXPR
5069#pragma GCC diagnostic pop
5070 } // inline namespace string_literals
5071 } // inline namespace literals
5072#endif // __glibcxx_string_udls
5073
5074#ifdef __glibcxx_variant202106L // >= C++17
5075 namespace __detail::__variant
5076 {
5077 template<typename> struct _Never_valueless_alt; // see <variant>
5078
5079 // Provide the strong exception-safety guarantee when emplacing a
5080 // basic_string into a variant, but only if moving the string cannot throw.
5081 template<typename _Tp, typename _Traits, typename _Alloc>
5082 struct _Never_valueless_alt<std::basic_string<_Tp, _Traits, _Alloc>>
5083 : __and_<
5084 is_nothrow_move_constructible<std::basic_string<_Tp, _Traits, _Alloc>>,
5085 is_nothrow_move_assignable<std::basic_string<_Tp, _Traits, _Alloc>>
5086 >::type
5087 { };
5088 } // namespace __detail::__variant
5089#endif // C++17
5090
5091_GLIBCXX_END_NAMESPACE_VERSION
5092} // namespace std
5093
5094#endif // C++11
5095
5096#endif /* _BASIC_STRING_H */