1// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors
2// Licensed under the MIT License:
3//
4// Permission is hereby granted, free of charge, to any person obtaining a copy
5// of this software and associated documentation files (the "Software"), to deal
6// in the Software without restriction, including without limitation the rights
7// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8// copies of the Software, and to permit persons to whom the Software is
9// furnished to do so, subject to the following conditions:
10//
11// The above copyright notice and this permission notice shall be included in
12// all copies or substantial portions of the Software.
13//
14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20// THE SOFTWARE.
21
22#pragma once
23
24#if defined(__GNUC__) && !KJ_HEADER_WARNINGS
25#pragma GCC system_header
26#endif
27
28#include <initializer_list>
29#include "array.h"
30#include <string.h>
31
32namespace kj {
33 class StringPtr;
34 class String;
35
36 class StringTree; // string-tree.h
37}
38
39constexpr kj::StringPtr operator "" _kj(const char* str, size_t n);
40// You can append _kj to a string literal to make its type be StringPtr. There are a few cases
41// where you must do this for correctness:
42// - When you want to declare a constexpr StringPtr. Without _kj, this is a compile error.
43// - When you want to initialize a static/global StringPtr from a string literal without forcing
44// global constructor code to run at dynamic initialization time.
45// - When you have a string literal that contains NUL characters. Without _kj, the string will
46// be considered to end at the first NUL.
47// - When you want to initialize an ArrayPtr<const char> from a string literal, without including
48// the NUL terminator in the data. (Initializing an ArrayPtr from a regular string literal is
49// a compile error specifically due to this ambiguity.)
50//
51// In other cases, there should be no difference between initializing a StringPtr from a regular
52// string literal vs. one with _kj (assuming the compiler is able to optimize away strlen() on a
53// string literal).
54
55namespace kj {
56
57// Our STL string SFINAE trick does not work with GCC 4.7, but it works with Clang and GCC 4.8, so
58// we'll just preprocess it out if not supported.
59#if __clang__ || __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8) || _MSC_VER
60#define KJ_COMPILER_SUPPORTS_STL_STRING_INTEROP 1
61#endif
62
63// =======================================================================================
64// StringPtr -- A NUL-terminated ArrayPtr<const char> containing UTF-8 text.
65//
66// NUL bytes are allowed to appear before the end of the string. The only requirement is that
67// a NUL byte appear immediately after the last byte of the content. This terminator byte is not
68// counted in the string's size.
69
70class StringPtr {
71public:
72 inline StringPtr(): content("", 1) {}
73 inline StringPtr(decltype(nullptr)): content("", 1) {}
74 inline StringPtr(const char* value): content(value, strlen(value) + 1) {}
75 inline StringPtr(const char* value, size_t size): content(value, size + 1) {
76 KJ_IREQUIRE(value[size] == '\0', "StringPtr must be NUL-terminated.");
77 }
78 inline StringPtr(const char* begin, const char* end): StringPtr(begin, end - begin) {}
79 inline StringPtr(const String& value);
80
81#if KJ_COMPILER_SUPPORTS_STL_STRING_INTEROP
82 template <typename T, typename = decltype(instance<T>().c_str())>
83 inline StringPtr(const T& t): StringPtr(t.c_str()) {}
84 // Allow implicit conversion from any class that has a c_str() method (namely, std::string).
85 // We use a template trick to detect std::string in order to avoid including the header for
86 // those who don't want it.
87
88 template <typename T, typename = decltype(instance<T>().c_str())>
89 inline operator T() const { return cStr(); }
90 // Allow implicit conversion to any class that has a c_str() method (namely, std::string).
91 // We use a template trick to detect std::string in order to avoid including the header for
92 // those who don't want it.
93#endif
94
95 inline constexpr operator ArrayPtr<const char>() const;
96 inline constexpr ArrayPtr<const char> asArray() const;
97 inline ArrayPtr<const byte> asBytes() const { return asArray().asBytes(); }
98 // Result does not include NUL terminator.
99
100 inline const char* cStr() const { return content.begin(); }
101 // Returns NUL-terminated string.
102
103 inline size_t size() const { return content.size() - 1; }
104 // Result does not include NUL terminator.
105
106 inline char operator[](size_t index) const { return content[index]; }
107
108 inline const char* begin() const { return content.begin(); }
109 inline const char* end() const { return content.end() - 1; }
110
111 inline bool operator==(decltype(nullptr)) const { return content.size() <= 1; }
112 inline bool operator!=(decltype(nullptr)) const { return content.size() > 1; }
113
114 inline bool operator==(const StringPtr& other) const;
115 inline bool operator!=(const StringPtr& other) const { return !(*this == other); }
116 inline bool operator< (const StringPtr& other) const;
117 inline bool operator> (const StringPtr& other) const { return other < *this; }
118 inline bool operator<=(const StringPtr& other) const { return !(other < *this); }
119 inline bool operator>=(const StringPtr& other) const { return !(*this < other); }
120
121 inline StringPtr slice(size_t start) const;
122 inline ArrayPtr<const char> slice(size_t start, size_t end) const;
123 // A string slice is only NUL-terminated if it is a suffix, so slice() has a one-parameter
124 // version that assumes end = size().
125
126 inline bool startsWith(const StringPtr& other) const;
127 inline bool endsWith(const StringPtr& other) const;
128
129 inline Maybe<size_t> findFirst(char c) const;
130 inline Maybe<size_t> findLast(char c) const;
131
132 template <typename T>
133 T parseAs() const;
134 // Parse string as template number type.
135 // Integer numbers prefixed by "0x" and "0X" are parsed in base 16 (like strtoi with base 0).
136 // Integer numbers prefixed by "0" are parsed in base 10 (unlike strtoi with base 0).
137 // Overflowed integer numbers throw exception.
138 // Overflowed floating numbers return inf.
139
140private:
141 inline constexpr StringPtr(ArrayPtr<const char> content): content(content) {}
142
143 ArrayPtr<const char> content;
144
145 friend constexpr kj::StringPtr (::operator "" _kj)(const char* str, size_t n);
146};
147
148inline bool operator==(const char* a, const StringPtr& b) { return b == a; }
149inline bool operator!=(const char* a, const StringPtr& b) { return b != a; }
150
151template <> char StringPtr::parseAs<char>() const;
152template <> signed char StringPtr::parseAs<signed char>() const;
153template <> unsigned char StringPtr::parseAs<unsigned char>() const;
154template <> short StringPtr::parseAs<short>() const;
155template <> unsigned short StringPtr::parseAs<unsigned short>() const;
156template <> int StringPtr::parseAs<int>() const;
157template <> unsigned StringPtr::parseAs<unsigned>() const;
158template <> long StringPtr::parseAs<long>() const;
159template <> unsigned long StringPtr::parseAs<unsigned long>() const;
160template <> long long StringPtr::parseAs<long long>() const;
161template <> unsigned long long StringPtr::parseAs<unsigned long long>() const;
162template <> float StringPtr::parseAs<float>() const;
163template <> double StringPtr::parseAs<double>() const;
164
165// =======================================================================================
166// String -- A NUL-terminated Array<char> containing UTF-8 text.
167//
168// NUL bytes are allowed to appear before the end of the string. The only requirement is that
169// a NUL byte appear immediately after the last byte of the content. This terminator byte is not
170// counted in the string's size.
171//
172// To allocate a String, you must call kj::heapString(). We do not implement implicit copying to
173// the heap because this hides potential inefficiency from the developer.
174
175class String {
176public:
177 String() = default;
178 inline String(decltype(nullptr)): content(nullptr) {}
179 inline String(char* value, size_t size, const ArrayDisposer& disposer);
180 // Does not copy. `size` does not include NUL terminator, but `value` must be NUL-terminated.
181 inline explicit String(Array<char> buffer);
182 // Does not copy. Requires `buffer` ends with `\0`.
183
184 inline operator ArrayPtr<char>();
185 inline operator ArrayPtr<const char>() const;
186 inline ArrayPtr<char> asArray();
187 inline ArrayPtr<const char> asArray() const;
188 inline ArrayPtr<byte> asBytes() { return asArray().asBytes(); }
189 inline ArrayPtr<const byte> asBytes() const { return asArray().asBytes(); }
190 // Result does not include NUL terminator.
191
192 inline Array<char> releaseArray() { return kj::mv(content); }
193 // Disowns the backing array (which includes the NUL terminator) and returns it. The String value
194 // is clobbered (as if moved away).
195
196 inline const char* cStr() const;
197
198 inline size_t size() const;
199 // Result does not include NUL terminator.
200
201 inline char operator[](size_t index) const;
202 inline char& operator[](size_t index);
203
204 inline char* begin();
205 inline char* end();
206 inline const char* begin() const;
207 inline const char* end() const;
208
209 inline bool operator==(decltype(nullptr)) const { return content.size() <= 1; }
210 inline bool operator!=(decltype(nullptr)) const { return content.size() > 1; }
211
212 inline bool operator==(const StringPtr& other) const { return StringPtr(*this) == other; }
213 inline bool operator!=(const StringPtr& other) const { return StringPtr(*this) != other; }
214 inline bool operator< (const StringPtr& other) const { return StringPtr(*this) < other; }
215 inline bool operator> (const StringPtr& other) const { return StringPtr(*this) > other; }
216 inline bool operator<=(const StringPtr& other) const { return StringPtr(*this) <= other; }
217 inline bool operator>=(const StringPtr& other) const { return StringPtr(*this) >= other; }
218
219 inline bool startsWith(const StringPtr& other) const { return StringPtr(*this).startsWith(other);}
220 inline bool endsWith(const StringPtr& other) const { return StringPtr(*this).endsWith(other); }
221
222 inline StringPtr slice(size_t start) const { return StringPtr(*this).slice(start); }
223 inline ArrayPtr<const char> slice(size_t start, size_t end) const {
224 return StringPtr(*this).slice(start, end);
225 }
226
227 inline Maybe<size_t> findFirst(char c) const { return StringPtr(*this).findFirst(c); }
228 inline Maybe<size_t> findLast(char c) const { return StringPtr(*this).findLast(c); }
229
230 template <typename T>
231 T parseAs() const { return StringPtr(*this).parseAs<T>(); }
232 // Parse as number
233
234private:
235 Array<char> content;
236};
237
238inline bool operator==(const char* a, const String& b) { return b == a; }
239inline bool operator!=(const char* a, const String& b) { return b != a; }
240
241String heapString(size_t size);
242// Allocate a String of the given size on the heap, not including NUL terminator. The NUL
243// terminator will be initialized automatically but the rest of the content is not initialized.
244
245String heapString(const char* value);
246String heapString(const char* value, size_t size);
247String heapString(StringPtr value);
248String heapString(const String& value);
249String heapString(ArrayPtr<const char> value);
250// Allocates a copy of the given value on the heap.
251
252// =======================================================================================
253// Magic str() function which transforms parameters to text and concatenates them into one big
254// String.
255
256namespace _ { // private
257
258inline size_t sum(std::initializer_list<size_t> nums) {
259 size_t result = 0;
260 for (auto num: nums) {
261 result += num;
262 }
263 return result;
264}
265
266inline char* fill(char* ptr) { return ptr; }
267inline char* fillLimited(char* ptr, char* limit) { return ptr; }
268
269template <typename... Rest>
270char* fill(char* __restrict__ target, const StringTree& first, Rest&&... rest);
271template <typename... Rest>
272char* fillLimited(char* __restrict__ target, char* limit, const StringTree& first, Rest&&... rest);
273// Make str() work with stringifiers that return StringTree by patching fill().
274//
275// Defined in string-tree.h.
276
277template <typename First, typename... Rest>
278char* fill(char* __restrict__ target, const First& first, Rest&&... rest) {
279 auto i = first.begin();
280 auto end = first.end();
281 while (i != end) {
282 *target++ = *i++;
283 }
284 return fill(target, kj::fwd<Rest>(rest)...);
285}
286
287template <typename... Params>
288String concat(Params&&... params) {
289 // Concatenate a bunch of containers into a single Array. The containers can be anything that
290 // is iterable and whose elements can be converted to `char`.
291
292 String result = heapString(sum({params.size()...}));
293 fill(result.begin(), kj::fwd<Params>(params)...);
294 return result;
295}
296
297inline String concat(String&& arr) {
298 return kj::mv(arr);
299}
300
301template <typename First, typename... Rest>
302char* fillLimited(char* __restrict__ target, char* limit, const First& first, Rest&&... rest) {
303 auto i = first.begin();
304 auto end = first.end();
305 while (i != end) {
306 if (target == limit) return target;
307 *target++ = *i++;
308 }
309 return fillLimited(target, limit, kj::fwd<Rest>(rest)...);
310}
311
312template <typename T>
313class Delimited;
314// Delimits a sequence of type T with a string delimiter. Implements kj::delimited().
315
316template <typename T, typename... Rest>
317char* fill(char* __restrict__ target, Delimited<T> first, Rest&&... rest);
318template <typename T, typename... Rest>
319char* fillLimited(char* __restrict__ target, char* limit, Delimited<T> first,Rest&&... rest);
320// As with StringTree, we special-case Delimited<T>.
321
322struct Stringifier {
323 // This is a dummy type with only one instance: STR (below). To make an arbitrary type
324 // stringifiable, define `operator*(Stringifier, T)` to return an iterable container of `char`.
325 // The container type must have a `size()` method. Be sure to declare the operator in the same
326 // namespace as `T` **or** in the global scope.
327 //
328 // A more usual way to accomplish what we're doing here would be to require that you define
329 // a function like `toString(T)` and then rely on argument-dependent lookup. However, this has
330 // the problem that it pollutes other people's namespaces and even the global namespace. For
331 // example, some other project may already have functions called `toString` which do something
332 // different. Declaring `operator*` with `Stringifier` as the left operand cannot conflict with
333 // anything.
334
335 inline ArrayPtr<const char> operator*(ArrayPtr<const char> s) const { return s; }
336 inline ArrayPtr<const char> operator*(ArrayPtr<char> s) const { return s; }
337 inline ArrayPtr<const char> operator*(const Array<const char>& s) const { return s; }
338 inline ArrayPtr<const char> operator*(const Array<char>& s) const { return s; }
339 template<size_t n>
340 inline ArrayPtr<const char> operator*(const CappedArray<char, n>& s) const { return s; }
341 template<size_t n>
342 inline ArrayPtr<const char> operator*(const FixedArray<char, n>& s) const { return s; }
343 inline ArrayPtr<const char> operator*(const char* s) const { return arrayPtr(s, strlen(s)); }
344 inline ArrayPtr<const char> operator*(const String& s) const { return s.asArray(); }
345 inline ArrayPtr<const char> operator*(const StringPtr& s) const { return s.asArray(); }
346
347 inline Range<char> operator*(const Range<char>& r) const { return r; }
348 inline Repeat<char> operator*(const Repeat<char>& r) const { return r; }
349
350 inline FixedArray<char, 1> operator*(char c) const {
351 FixedArray<char, 1> result;
352 result[0] = c;
353 return result;
354 }
355
356 StringPtr operator*(decltype(nullptr)) const;
357 StringPtr operator*(bool b) const;
358
359 CappedArray<char, 5> operator*(signed char i) const;
360 CappedArray<char, 5> operator*(unsigned char i) const;
361 CappedArray<char, sizeof(short) * 3 + 2> operator*(short i) const;
362 CappedArray<char, sizeof(unsigned short) * 3 + 2> operator*(unsigned short i) const;
363 CappedArray<char, sizeof(int) * 3 + 2> operator*(int i) const;
364 CappedArray<char, sizeof(unsigned int) * 3 + 2> operator*(unsigned int i) const;
365 CappedArray<char, sizeof(long) * 3 + 2> operator*(long i) const;
366 CappedArray<char, sizeof(unsigned long) * 3 + 2> operator*(unsigned long i) const;
367 CappedArray<char, sizeof(long long) * 3 + 2> operator*(long long i) const;
368 CappedArray<char, sizeof(unsigned long long) * 3 + 2> operator*(unsigned long long i) const;
369 CappedArray<char, 24> operator*(float f) const;
370 CappedArray<char, 32> operator*(double f) const;
371 CappedArray<char, sizeof(const void*) * 2 + 1> operator*(const void* s) const;
372
373 template <typename T>
374 _::Delimited<ArrayPtr<T>> operator*(ArrayPtr<T> arr) const;
375 template <typename T>
376 _::Delimited<ArrayPtr<const T>> operator*(const Array<T>& arr) const;
377
378#if KJ_COMPILER_SUPPORTS_STL_STRING_INTEROP // supports expression SFINAE?
379 template <typename T, typename Result = decltype(instance<T>().toString())>
380 inline Result operator*(T&& value) const { return kj::fwd<T>(value).toString(); }
381#endif
382};
383static KJ_CONSTEXPR(const) Stringifier STR = Stringifier();
384
385} // namespace _ (private)
386
387template <typename T>
388auto toCharSequence(T&& value) -> decltype(_::STR * kj::fwd<T>(value)) {
389 // Returns an iterable of chars that represent a textual representation of the value, suitable
390 // for debugging.
391 //
392 // Most users should use str() instead, but toCharSequence() may occasionally be useful to avoid
393 // heap allocation overhead that str() implies.
394 //
395 // To specialize this function for your type, see KJ_STRINGIFY.
396
397 return _::STR * kj::fwd<T>(value);
398}
399
400CappedArray<char, sizeof(unsigned char) * 2 + 1> hex(unsigned char i);
401CappedArray<char, sizeof(unsigned short) * 2 + 1> hex(unsigned short i);
402CappedArray<char, sizeof(unsigned int) * 2 + 1> hex(unsigned int i);
403CappedArray<char, sizeof(unsigned long) * 2 + 1> hex(unsigned long i);
404CappedArray<char, sizeof(unsigned long long) * 2 + 1> hex(unsigned long long i);
405
406template <typename... Params>
407String str(Params&&... params) {
408 // Magic function which builds a string from a bunch of arbitrary values. Example:
409 // str(1, " / ", 2, " = ", 0.5)
410 // returns:
411 // "1 / 2 = 0.5"
412 // To teach `str` how to stringify a type, see `Stringifier`.
413
414 return _::concat(toCharSequence(kj::fwd<Params>(params))...);
415}
416
417inline String str(String&& s) { return mv(s); }
418// Overload to prevent redundant allocation.
419
420template <typename T>
421_::Delimited<T> delimited(T&& arr, kj::StringPtr delim);
422// Use to stringify an array.
423
424template <typename T>
425String strArray(T&& arr, const char* delim) {
426 size_t delimLen = strlen(delim);
427 KJ_STACK_ARRAY(decltype(_::STR * arr[0]), pieces, kj::size(arr), 8, 32);
428 size_t size = 0;
429 for (size_t i = 0; i < kj::size(arr); i++) {
430 if (i > 0) size += delimLen;
431 pieces[i] = _::STR * arr[i];
432 size += pieces[i].size();
433 }
434
435 String result = heapString(size);
436 char* pos = result.begin();
437 for (size_t i = 0; i < kj::size(arr); i++) {
438 if (i > 0) {
439 memcpy(pos, delim, delimLen);
440 pos += delimLen;
441 }
442 pos = _::fill(pos, pieces[i]);
443 }
444 return result;
445}
446
447template <typename... Params>
448StringPtr strPreallocated(ArrayPtr<char> buffer, Params&&... params) {
449 // Like str() but writes into a preallocated buffer. If the buffer is not long enough, the result
450 // is truncated (but still NUL-terminated).
451 //
452 // This can be used like:
453 //
454 // char buffer[256];
455 // StringPtr text = strPreallocated(buffer, params...);
456 //
457 // This is useful for optimization. It can also potentially be used safely in async signal
458 // handlers. HOWEVER, to use in an async signal handler, all of the stringifiers for the inputs
459 // must also be signal-safe. KJ guarantees signal safety when stringifying any built-in integer
460 // type (but NOT floating-points), basic char/byte sequences (ArrayPtr<byte>, String, etc.), as
461 // well as Array<T> as long as T can also be stringified safely. To safely stringify a delimited
462 // array, you must use kj::delimited(arr, delim) rather than the deprecated
463 // kj::strArray(arr, delim).
464
465 char* end = _::fillLimited(buffer.begin(), buffer.end() - 1,
466 toCharSequence(kj::fwd<Params>(params))...);
467 *end = '\0';
468 return StringPtr(buffer.begin(), end);
469}
470
471namespace _ { // private
472
473template <typename T>
474inline _::Delimited<ArrayPtr<T>> Stringifier::operator*(ArrayPtr<T> arr) const {
475 return _::Delimited<ArrayPtr<T>>(arr, ", ");
476}
477
478template <typename T>
479inline _::Delimited<ArrayPtr<const T>> Stringifier::operator*(const Array<T>& arr) const {
480 return _::Delimited<ArrayPtr<const T>>(arr, ", ");
481}
482
483} // namespace _ (private)
484
485#define KJ_STRINGIFY(...) operator*(::kj::_::Stringifier, __VA_ARGS__)
486// Defines a stringifier for a custom type. Example:
487//
488// class Foo {...};
489// inline StringPtr KJ_STRINGIFY(const Foo& foo) { return foo.name(); }
490//
491// This allows Foo to be passed to str().
492//
493// The function should be declared either in the same namespace as the target type or in the global
494// namespace. It can return any type which is an iterable container of chars.
495
496// =======================================================================================
497// Inline implementation details.
498
499inline StringPtr::StringPtr(const String& value): content(value.cStr(), value.size() + 1) {}
500
501inline constexpr StringPtr::operator ArrayPtr<const char>() const {
502 return ArrayPtr<const char>(content.begin(), content.size() - 1);
503}
504
505inline constexpr ArrayPtr<const char> StringPtr::asArray() const {
506 return ArrayPtr<const char>(content.begin(), content.size() - 1);
507}
508
509inline bool StringPtr::operator==(const StringPtr& other) const {
510 return content.size() == other.content.size() &&
511 memcmp(content.begin(), other.content.begin(), content.size() - 1) == 0;
512}
513
514inline bool StringPtr::operator<(const StringPtr& other) const {
515 bool shorter = content.size() < other.content.size();
516 int cmp = memcmp(content.begin(), other.content.begin(),
517 shorter ? content.size() : other.content.size());
518 return cmp < 0 || (cmp == 0 && shorter);
519}
520
521inline StringPtr StringPtr::slice(size_t start) const {
522 return StringPtr(content.slice(start, content.size()));
523}
524inline ArrayPtr<const char> StringPtr::slice(size_t start, size_t end) const {
525 return content.slice(start, end);
526}
527
528inline bool StringPtr::startsWith(const StringPtr& other) const {
529 return other.content.size() <= content.size() &&
530 memcmp(content.begin(), other.content.begin(), other.size()) == 0;
531}
532inline bool StringPtr::endsWith(const StringPtr& other) const {
533 return other.content.size() <= content.size() &&
534 memcmp(end() - other.size(), other.content.begin(), other.size()) == 0;
535}
536
537inline Maybe<size_t> StringPtr::findFirst(char c) const {
538 const char* pos = reinterpret_cast<const char*>(memchr(content.begin(), c, size()));
539 if (pos == nullptr) {
540 return nullptr;
541 } else {
542 return pos - content.begin();
543 }
544}
545
546inline Maybe<size_t> StringPtr::findLast(char c) const {
547 for (size_t i = size(); i > 0; --i) {
548 if (content[i-1] == c) {
549 return i-1;
550 }
551 }
552 return nullptr;
553}
554
555inline String::operator ArrayPtr<char>() {
556 return content == nullptr ? ArrayPtr<char>(nullptr) : content.slice(0, content.size() - 1);
557}
558inline String::operator ArrayPtr<const char>() const {
559 return content == nullptr ? ArrayPtr<const char>(nullptr) : content.slice(0, content.size() - 1);
560}
561
562inline ArrayPtr<char> String::asArray() {
563 return content == nullptr ? ArrayPtr<char>(nullptr) : content.slice(0, content.size() - 1);
564}
565inline ArrayPtr<const char> String::asArray() const {
566 return content == nullptr ? ArrayPtr<const char>(nullptr) : content.slice(0, content.size() - 1);
567}
568
569inline const char* String::cStr() const { return content == nullptr ? "" : content.begin(); }
570
571inline size_t String::size() const { return content == nullptr ? 0 : content.size() - 1; }
572
573inline char String::operator[](size_t index) const { return content[index]; }
574inline char& String::operator[](size_t index) { return content[index]; }
575
576inline char* String::begin() { return content == nullptr ? nullptr : content.begin(); }
577inline char* String::end() { return content == nullptr ? nullptr : content.end() - 1; }
578inline const char* String::begin() const { return content == nullptr ? nullptr : content.begin(); }
579inline const char* String::end() const { return content == nullptr ? nullptr : content.end() - 1; }
580
581inline String::String(char* value, size_t size, const ArrayDisposer& disposer)
582 : content(value, size + 1, disposer) {
583 KJ_IREQUIRE(value[size] == '\0', "String must be NUL-terminated.");
584}
585
586inline String::String(Array<char> buffer): content(kj::mv(buffer)) {
587 KJ_IREQUIRE(content.size() > 0 && content.back() == '\0', "String must be NUL-terminated.");
588}
589
590inline String heapString(const char* value) {
591 return heapString(value, strlen(value));
592}
593inline String heapString(StringPtr value) {
594 return heapString(value.begin(), value.size());
595}
596inline String heapString(const String& value) {
597 return heapString(value.begin(), value.size());
598}
599inline String heapString(ArrayPtr<const char> value) {
600 return heapString(value.begin(), value.size());
601}
602
603namespace _ { // private
604
605template <typename T>
606class Delimited {
607public:
608 Delimited(T array, kj::StringPtr delimiter)
609 : array(kj::fwd<T>(array)), delimiter(delimiter) {}
610
611 // TODO(someday): In theory we should support iteration as a character sequence, but the iterator
612 // will be pretty complicated.
613
614 size_t size() {
615 ensureStringifiedInitialized();
616
617 size_t result = 0;
618 bool first = true;
619 for (auto& e: stringified) {
620 if (first) {
621 first = false;
622 } else {
623 result += delimiter.size();
624 }
625 result += e.size();
626 }
627 return result;
628 }
629
630 char* flattenTo(char* __restrict__ target) {
631 ensureStringifiedInitialized();
632
633 bool first = true;
634 for (auto& elem: stringified) {
635 if (first) {
636 first = false;
637 } else {
638 target = fill(target, delimiter);
639 }
640 target = fill(target, elem);
641 }
642 return target;
643 }
644
645 char* flattenTo(char* __restrict__ target, char* limit) {
646 // This is called in the strPreallocated(). We want to avoid allocation. size() will not have
647 // been called in this case, so hopefully `stringified` is still uninitialized. We will
648 // stringify each item and immediately use it.
649 bool first = true;
650 for (auto&& elem: array) {
651 if (target == limit) return target;
652 if (first) {
653 first = false;
654 } else {
655 target = fillLimited(target, limit, delimiter);
656 }
657 target = fillLimited(target, limit, kj::toCharSequence(elem));
658 }
659 return target;
660 }
661
662private:
663 typedef decltype(toCharSequence(*instance<T>().begin())) StringifiedItem;
664 T array;
665 kj::StringPtr delimiter;
666 Array<StringifiedItem> stringified;
667
668 void ensureStringifiedInitialized() {
669 if (array.size() > 0 && stringified.size() == 0) {
670 stringified = KJ_MAP(e, array) { return toCharSequence(e); };
671 }
672 }
673};
674
675template <typename T, typename... Rest>
676char* fill(char* __restrict__ target, Delimited<T> first, Rest&&... rest) {
677 target = first.flattenTo(target);
678 return fill(target, kj::fwd<Rest>(rest)...);
679}
680template <typename T, typename... Rest>
681char* fillLimited(char* __restrict__ target, char* limit, Delimited<T> first, Rest&&... rest) {
682 target = first.flattenTo(target, limit);
683 return fillLimited(target, limit, kj::fwd<Rest>(rest)...);
684}
685
686template <typename T>
687inline Delimited<T>&& KJ_STRINGIFY(Delimited<T>&& delimited) { return kj::mv(delimited); }
688template <typename T>
689inline const Delimited<T>& KJ_STRINGIFY(const Delimited<T>& delimited) { return delimited; }
690
691} // namespace _ (private)
692
693template <typename T>
694_::Delimited<T> delimited(T&& arr, kj::StringPtr delim) {
695 return _::Delimited<T>(kj::fwd<T>(arr), delim);
696}
697
698} // namespace kj
699
700constexpr kj::StringPtr operator "" _kj(const char* str, size_t n) {
701 return kj::StringPtr(kj::ArrayPtr<const char>(str, n + 1));
702};
703