// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. #pragma once #include #include "array.h" #include "kj/common.h" #include KJ_BEGIN_HEADER namespace kj { class StringPtr; class LiteralStringConst; class String; class ConstString; class StringTree; // string-tree.h } constexpr kj::StringPtr operator "" _kj(const char* str, size_t n); // You can append _kj to a string literal to make its type be StringPtr. There are a few cases // where you must do this for correctness: // - When you want to declare a constexpr StringPtr. Without _kj, this is a compile error. // - When you want to initialize a static/global StringPtr from a string literal without forcing // global constructor code to run at dynamic initialization time. // - When you have a string literal that contains NUL characters. Without _kj, the string will // be considered to end at the first NUL. // - When you want to initialize an ArrayPtr from a string literal, without including // the NUL terminator in the data. (Initializing an ArrayPtr from a regular string literal is // a compile error specifically due to this ambiguity.) // // In other cases, there should be no difference between initializing a StringPtr from a regular // string literal vs. one with _kj (assuming the compiler is able to optimize away strlen() on a // string literal). constexpr kj::LiteralStringConst operator "" _kjc(const char* str, size_t n); namespace kj { // Our STL string SFINAE trick does not work with GCC 4.7, but it works with Clang and GCC 4.8, so // we'll just preprocess it out if not supported. #if __clang__ || __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8) #define KJ_COMPILER_SUPPORTS_STL_STRING_INTEROP 1 #endif // ======================================================================================= // StringPtr -- A NUL-terminated ArrayPtr containing UTF-8 text. // // NUL bytes are allowed to appear before the end of the string. The only requirement is that // a NUL byte appear immediately after the last byte of the content. This terminator byte is not // counted in the string's size. class StringPtr { public: inline StringPtr(): content("", 1) {} inline StringPtr(decltype(nullptr)): content("", 1) {} inline StringPtr(const char* value KJ_LIFETIMEBOUND): content(value, strlen(value) + 1) {} inline StringPtr(const char* value KJ_LIFETIMEBOUND, size_t size): content(value, size + 1) { KJ_IREQUIRE(value[size] == '\0', "StringPtr must be NUL-terminated."); } inline StringPtr(const char* begin KJ_LIFETIMEBOUND, const char* end KJ_LIFETIMEBOUND): StringPtr(begin, end - begin) {} inline StringPtr(String&& value KJ_LIFETIMEBOUND) : StringPtr(value) {} inline StringPtr(const String& value KJ_LIFETIMEBOUND); inline StringPtr(const ConstString& value KJ_LIFETIMEBOUND); StringPtr& operator=(String&& value) = delete; inline StringPtr& operator=(decltype(nullptr)) { content = ArrayPtr("", 1); return *this; } #if __cpp_char8_t inline StringPtr(const char8_t* value KJ_LIFETIMEBOUND): StringPtr(reinterpret_cast(value)) {} inline StringPtr(const char8_t* value KJ_LIFETIMEBOUND, size_t size) : StringPtr(reinterpret_cast(value), size) {} inline StringPtr(const char8_t* begin KJ_LIFETIMEBOUND, const char8_t* end KJ_LIFETIMEBOUND) : StringPtr(reinterpret_cast(begin), reinterpret_cast(end)) {} // KJ strings are and always have been UTF-8, so screw this C++20 char8_t stuff. #endif #if KJ_COMPILER_SUPPORTS_STL_STRING_INTEROP template < typename T, typename = EnableIf().c_str()), const char*>()>, typename = decltype(instance().size())> inline StringPtr(const T& t KJ_LIFETIMEBOUND): StringPtr(t.c_str(), t.size()) {} // Allow implicit conversion from any class that has a c_str() and a size() method (namely, std::string). // We use a template trick to detect std::string in order to avoid including the header for // those who don't want it. template < typename T, typename = EnableIf().c_str()), const char*>()>, typename = decltype(instance().size())> inline operator T() const { return {cStr(), size()}; } // Allow implicit conversion to any class that has a c_str() method and a size() method (namely, std::string). // We use a template trick to detect std::string in order to avoid including the header for // those who don't want it. #endif inline constexpr operator ArrayPtr() const; inline constexpr ArrayPtr asArray() const; inline ArrayPtr asBytes() const { return asArray().asBytes(); } // Result does not include NUL terminator. inline const char* cStr() const { return content.begin(); } // Returns NUL-terminated string. inline size_t size() const { return content.size() - 1; } // Result does not include NUL terminator. inline char operator[](size_t index) const { return content[index]; } inline constexpr const char* begin() const { return content.begin(); } inline constexpr const char* end() const { return content.end() - 1; } inline constexpr bool operator==(decltype(nullptr)) const { return content.size() <= 1; } #if !__cpp_impl_three_way_comparison inline constexpr bool operator!=(decltype(nullptr)) const { return content.size() > 1; } #endif inline bool operator==(const StringPtr& other) const; #if !__cpp_impl_three_way_comparison inline bool operator!=(const StringPtr& other) const { return !(*this == other); } #endif inline bool operator< (const StringPtr& other) const; inline bool operator> (const StringPtr& other) const { return other < *this; } inline bool operator<=(const StringPtr& other) const { return !(other < *this); } inline bool operator>=(const StringPtr& other) const { return !(*this < other); } inline StringPtr slice(size_t start) const; inline ArrayPtr slice(size_t start, size_t end) const; // A string slice is only NUL-terminated if it is a suffix, so slice() has a one-parameter // version that assumes end = size(). inline bool startsWith(const StringPtr& other) const { return asArray().startsWith(other);} inline bool endsWith(const StringPtr& other) const { return asArray().endsWith(other); } inline Maybe findFirst(char c) const { return asArray().findFirst(c); } inline Maybe findLast(char c) const { return asArray().findLast(c); } template T parseAs() const; // Parse string as template number type. // Integer numbers prefixed by "0x" and "0X" are parsed in base 16 (like strtoi with base 0). // Integer numbers prefixed by "0" are parsed in base 10 (unlike strtoi with base 0). // Overflowed integer numbers throw exception. // Overflowed floating numbers return inf. template Maybe tryParseAs() const; // Same as parseAs, but rather than throwing an exception we return NULL. template ConstString attach(Attachments&&... attachments) const KJ_WARN_UNUSED_RESULT; ConstString attach() const KJ_WARN_UNUSED_RESULT; // Like ArrayPtr::attach(), but instead promotes a StringPtr into a ConstString. Generally the // attachment should be an object that somehow owns the String that the StringPtr is pointing at. private: inline explicit constexpr StringPtr(ArrayPtr content): content(content) {} friend constexpr StringPtr (::operator "" _kj)(const char* str, size_t n); friend class LiteralStringConst; ArrayPtr content; friend class SourceLocation; }; #if !__cpp_impl_three_way_comparison inline bool operator==(const char* a, const StringPtr& b) { return b == a; } inline bool operator!=(const char* a, const StringPtr& b) { return b != a; } #endif template <> char StringPtr::parseAs() const; template <> signed char StringPtr::parseAs() const; template <> unsigned char StringPtr::parseAs() const; template <> short StringPtr::parseAs() const; template <> unsigned short StringPtr::parseAs() const; template <> int StringPtr::parseAs() const; template <> unsigned StringPtr::parseAs() const; template <> long StringPtr::parseAs() const; template <> unsigned long StringPtr::parseAs() const; template <> long long StringPtr::parseAs() const; template <> unsigned long long StringPtr::parseAs() const; template <> float StringPtr::parseAs() const; template <> double StringPtr::parseAs() const; template <> Maybe StringPtr::tryParseAs() const; template <> Maybe StringPtr::tryParseAs() const; template <> Maybe StringPtr::tryParseAs() const; template <> Maybe StringPtr::tryParseAs() const; template <> Maybe StringPtr::tryParseAs() const; template <> Maybe StringPtr::tryParseAs() const; template <> Maybe StringPtr::tryParseAs() const; template <> Maybe StringPtr::tryParseAs() const; template <> Maybe StringPtr::tryParseAs() const; template <> Maybe StringPtr::tryParseAs() const; template <> Maybe StringPtr::tryParseAs() const; template <> Maybe StringPtr::tryParseAs() const; template <> Maybe StringPtr::tryParseAs() const; class LiteralStringConst: public StringPtr { public: inline operator ConstString() const; private: inline explicit constexpr LiteralStringConst(ArrayPtr content): StringPtr(content) {} friend constexpr LiteralStringConst (::operator "" _kjc)(const char* str, size_t n); }; // ======================================================================================= // String -- A NUL-terminated Array containing UTF-8 text. // // NUL bytes are allowed to appear before the end of the string. The only requirement is that // a NUL byte appear immediately after the last byte of the content. This terminator byte is not // counted in the string's size. // // To allocate a String, you must call kj::heapString(). We do not implement implicit copying to // the heap because this hides potential inefficiency from the developer. class String { public: String() = default; inline String(decltype(nullptr)): content(nullptr) {} inline String(char* value, size_t size, const ArrayDisposer& disposer); // Does not copy. `size` does not include NUL terminator, but `value` must be NUL-terminated. inline explicit String(Array buffer); // Does not copy. Requires `buffer` ends with `\0`. inline operator ArrayPtr() KJ_LIFETIMEBOUND; inline operator ArrayPtr() const KJ_LIFETIMEBOUND; inline ArrayPtr asArray() KJ_LIFETIMEBOUND; inline ArrayPtr asArray() const KJ_LIFETIMEBOUND; inline ArrayPtr asBytes() KJ_LIFETIMEBOUND { return asArray().asBytes(); } inline ArrayPtr asBytes() const KJ_LIFETIMEBOUND { return asArray().asBytes(); } // Result does not include NUL terminator. inline StringPtr asPtr() const KJ_LIFETIMEBOUND { // Convenience operator to return a StringPtr. return StringPtr{*this}; } inline Array releaseArray() { return kj::mv(content); } // Disowns the backing array (which includes the NUL terminator) and returns it. The String value // is clobbered (as if moved away). inline const char* cStr() const KJ_LIFETIMEBOUND; inline size_t size() const; // Result does not include NUL terminator. inline char operator[](size_t index) const; inline char& operator[](size_t index) KJ_LIFETIMEBOUND; inline char* begin() KJ_LIFETIMEBOUND; inline char* end() KJ_LIFETIMEBOUND; inline const char* begin() const KJ_LIFETIMEBOUND; inline const char* end() const KJ_LIFETIMEBOUND; inline bool operator==(decltype(nullptr)) const { return content.size() <= 1; } inline bool operator!=(decltype(nullptr)) const { return content.size() > 1; } inline bool operator==(const StringPtr& other) const { return StringPtr(*this) == other; } #if !__cpp_impl_three_way_comparison inline bool operator!=(const StringPtr& other) const { return StringPtr(*this) != other; } #endif inline bool operator< (const StringPtr& other) const { return StringPtr(*this) < other; } inline bool operator> (const StringPtr& other) const { return StringPtr(*this) > other; } inline bool operator<=(const StringPtr& other) const { return StringPtr(*this) <= other; } inline bool operator>=(const StringPtr& other) const { return StringPtr(*this) >= other; } inline bool operator==(const String& other) const { return StringPtr(*this) == StringPtr(other); } #if !__cpp_impl_three_way_comparison inline bool operator!=(const String& other) const { return StringPtr(*this) != StringPtr(other); } #endif inline bool operator< (const String& other) const { return StringPtr(*this) < StringPtr(other); } inline bool operator> (const String& other) const { return StringPtr(*this) > StringPtr(other); } inline bool operator<=(const String& other) const { return StringPtr(*this) <= StringPtr(other); } inline bool operator>=(const String& other) const { return StringPtr(*this) >= StringPtr(other); } // Note that if we don't overload for `const String&` specifically, then C++20 will decide that // comparisons between two strings are ambiguous. (Clang turns this into a warning, // -Wambiguous-reversed-operator, due to the stupidity...) inline bool operator==(const ConstString& other) const { return StringPtr(*this) == StringPtr(other); } #if !__cpp_impl_three_way_comparison inline bool operator!=(const ConstString& other) const { return StringPtr(*this) != StringPtr(other); } #endif inline bool operator< (const ConstString& other) const { return StringPtr(*this) < StringPtr(other); } inline bool operator> (const ConstString& other) const { return StringPtr(*this) > StringPtr(other); } inline bool operator<=(const ConstString& other) const { return StringPtr(*this) <= StringPtr(other); } inline bool operator>=(const ConstString& other) const { return StringPtr(*this) >= StringPtr(other); } inline bool startsWith(const StringPtr& other) const { return asArray().startsWith(other);} inline bool endsWith(const StringPtr& other) const { return asArray().endsWith(other); } inline StringPtr slice(size_t start) const KJ_LIFETIMEBOUND { return StringPtr(*this).slice(start); } inline ArrayPtr slice(size_t start, size_t end) const KJ_LIFETIMEBOUND { return StringPtr(*this).slice(start, end); } inline Maybe findFirst(char c) const { return asArray().findFirst(c); } inline Maybe findLast(char c) const { return asArray().findLast(c); } template T parseAs() const { return StringPtr(*this).parseAs(); } // Parse as number template Maybe tryParseAs() const { return StringPtr(*this).tryParseAs(); } private: Array content; }; // ======================================================================================= // ConstString -- Same as String, but the backing buffer is const. // // This has the useful property that it can reference a string literal without allocating // a copy. Any String can also convert (by move) to ConstString, transferring ownership of // the buffer. class ConstString { public: ConstString() = default; inline ConstString(decltype(nullptr)): content(nullptr) {} inline ConstString(const char* value, size_t size, const ArrayDisposer& disposer); // Does not copy. `size` does not include NUL terminator, but `value` must be NUL-terminated. inline explicit ConstString(Array buffer); // Does not copy. Requires `buffer` ends with `\0`. inline explicit ConstString(String&& string): content(string.releaseArray()) {} // Does not copy. Ownership is transfered. inline operator ArrayPtr() const KJ_LIFETIMEBOUND; inline ArrayPtr asArray() const KJ_LIFETIMEBOUND; inline ArrayPtr asBytes() const KJ_LIFETIMEBOUND { return asArray().asBytes(); } // Result does not include NUL terminator. inline StringPtr asPtr() const KJ_LIFETIMEBOUND { // Convenience operator to return a StringPtr. return StringPtr{*this}; } inline Array releaseArray() { return kj::mv(content); } // Disowns the backing array (which includes the NUL terminator) and returns it. The ConstString value // is clobbered (as if moved away). inline const char* cStr() const KJ_LIFETIMEBOUND; inline size_t size() const; // Result does not include NUL terminator. inline char operator[](size_t index) const; inline char& operator[](size_t index) KJ_LIFETIMEBOUND; inline const char* begin() const KJ_LIFETIMEBOUND; inline const char* end() const KJ_LIFETIMEBOUND; inline bool operator==(decltype(nullptr)) const { return content.size() <= 1; } inline bool operator!=(decltype(nullptr)) const { return content.size() > 1; } inline bool operator==(const StringPtr& other) const { return StringPtr(*this) == other; } #if !__cpp_impl_three_way_comparison inline bool operator!=(const StringPtr& other) const { return StringPtr(*this) != other; } #endif inline bool operator< (const StringPtr& other) const { return StringPtr(*this) < other; } inline bool operator> (const StringPtr& other) const { return StringPtr(*this) > other; } inline bool operator<=(const StringPtr& other) const { return StringPtr(*this) <= other; } inline bool operator>=(const StringPtr& other) const { return StringPtr(*this) >= other; } inline bool operator==(const String& other) const { return StringPtr(*this) == StringPtr(other); } #if !__cpp_impl_three_way_comparison inline bool operator!=(const String& other) const { return StringPtr(*this) != StringPtr(other); } #endif inline bool operator< (const String& other) const { return StringPtr(*this) < StringPtr(other); } inline bool operator> (const String& other) const { return StringPtr(*this) > StringPtr(other); } inline bool operator<=(const String& other) const { return StringPtr(*this) <= StringPtr(other); } inline bool operator>=(const String& other) const { return StringPtr(*this) >= StringPtr(other); } inline bool operator==(const ConstString& other) const { return StringPtr(*this) == StringPtr(other); } #if !__cpp_impl_three_way_comparison inline bool operator!=(const ConstString& other) const { return StringPtr(*this) != StringPtr(other); } #endif inline bool operator< (const ConstString& other) const { return StringPtr(*this) < StringPtr(other); } inline bool operator> (const ConstString& other) const { return StringPtr(*this) > StringPtr(other); } inline bool operator<=(const ConstString& other) const { return StringPtr(*this) <= StringPtr(other); } inline bool operator>=(const ConstString& other) const { return StringPtr(*this) >= StringPtr(other); } // Note that if we don't overload for `const ConstString&` specifically, then C++20 will decide that // comparisons between two strings are ambiguous. (Clang turns this into a warning, // -Wambiguous-reversed-operator, due to the stupidity...) inline bool startsWith(const StringPtr& other) const { return asArray().startsWith(other);} inline bool endsWith(const StringPtr& other) const { return asArray().endsWith(other); } inline StringPtr slice(size_t start) const KJ_LIFETIMEBOUND { return StringPtr(*this).slice(start); } inline ArrayPtr slice(size_t start, size_t end) const KJ_LIFETIMEBOUND { return StringPtr(*this).slice(start, end); } inline Maybe findFirst(char c) const { return asArray().findFirst(c); } inline Maybe findLast(char c) const { return asArray().findLast(c); } template T parseAs() const { return StringPtr(*this).parseAs(); } // Parse as number template Maybe tryParseAs() const { return StringPtr(*this).tryParseAs(); } private: Array content; }; #if !__cpp_impl_three_way_comparison inline bool operator==(const char* a, const String& b) { return b == a; } inline bool operator!=(const char* a, const String& b) { return b != a; } #endif String heapString(size_t size); // Allocate a String of the given size on the heap, not including NUL terminator. The NUL // terminator will be initialized automatically but the rest of the content is not initialized. String heapString(const char* value); String heapString(const char* value, size_t size); String heapString(StringPtr value); String heapString(const String& value); String heapString(ArrayPtr value); // Allocates a copy of the given value on the heap. // ======================================================================================= // Magic str() function which transforms parameters to text and concatenates them into one big // String. namespace _ { // private inline size_t sum(std::initializer_list nums) { size_t result = 0; for (auto num: nums) { result += num; } return result; } inline char* fill(char* ptr) { return ptr; } inline char* fillLimited(char* ptr, char* limit) { return ptr; } template char* fill(char* __restrict__ target, const StringTree& first, Rest&&... rest); template char* fillLimited(char* __restrict__ target, char* limit, const StringTree& first, Rest&&... rest); // Make str() work with stringifiers that return StringTree by patching fill(). // // Defined in string-tree.h. template char* fill(char* __restrict__ target, const First& first, Rest&&... rest) { auto i = first.begin(); auto end = first.end(); while (i != end) { *target++ = *i++; } return fill(target, kj::fwd(rest)...); } template String concat(Params&&... params) { // Concatenate a bunch of containers into a single Array. The containers can be anything that // is iterable and whose elements can be converted to `char`. String result = heapString(sum({params.size()...})); fill(result.begin(), kj::fwd(params)...); return result; } inline String concat(String&& arr) { return kj::mv(arr); } template char* fillLimited(char* __restrict__ target, char* limit, const First& first, Rest&&... rest) { auto i = first.begin(); auto end = first.end(); while (i != end) { if (target == limit) return target; *target++ = *i++; } return fillLimited(target, limit, kj::fwd(rest)...); } template class Delimited; // Delimits a sequence of type T with a string delimiter. Implements kj::delimited(). template char* fill(char* __restrict__ target, Delimited&& first, Rest&&... rest); template char* fillLimited(char* __restrict__ target, char* limit, Delimited&& first,Rest&&... rest); template char* fill(char* __restrict__ target, Delimited& first, Rest&&... rest); template char* fillLimited(char* __restrict__ target, char* limit, Delimited& first,Rest&&... rest); // As with StringTree, we special-case Delimited. struct Stringifier { // This is a dummy type with only one instance: STR (below). To make an arbitrary type // stringifiable, define `operator*(Stringifier, T)` to return an iterable container of `char`. // The container type must have a `size()` method. Be sure to declare the operator in the same // namespace as `T` **or** in the global scope. // // A more usual way to accomplish what we're doing here would be to require that you define // a function like `toString(T)` and then rely on argument-dependent lookup. However, this has // the problem that it pollutes other people's namespaces and even the global namespace. For // example, some other project may already have functions called `toString` which do something // different. Declaring `operator*` with `Stringifier` as the left operand cannot conflict with // anything. inline ArrayPtr operator*(ArrayPtr s) const { return s; } inline ArrayPtr operator*(ArrayPtr s) const { return s; } inline ArrayPtr operator*(const Array& s) const KJ_LIFETIMEBOUND { return s; } inline ArrayPtr operator*(const Array& s) const KJ_LIFETIMEBOUND { return s; } template inline ArrayPtr operator*(const CappedArray& s) const KJ_LIFETIMEBOUND { return s; } template inline ArrayPtr operator*(const FixedArray& s) const KJ_LIFETIMEBOUND { return s; } inline ArrayPtr operator*(const char* s) const KJ_LIFETIMEBOUND { return arrayPtr(s, strlen(s)); } #if __cpp_char8_t inline ArrayPtr operator*(const char8_t* s) const KJ_LIFETIMEBOUND { return operator*(reinterpret_cast(s)); } #endif inline ArrayPtr operator*(const String& s) const KJ_LIFETIMEBOUND { return s.asArray(); } inline ArrayPtr operator*(const StringPtr& s) const { return s.asArray(); } inline ArrayPtr operator*(const ConstString& s) const { return s.asArray(); } inline Range operator*(const Range& r) const { return r; } inline Repeat operator*(const Repeat& r) const { return r; } inline FixedArray operator*(char c) const { FixedArray result; result[0] = c; return result; } StringPtr operator*(decltype(nullptr)) const; StringPtr operator*(bool b) const; CappedArray operator*(signed char i) const; CappedArray operator*(unsigned char i) const; CappedArray operator*(short i) const; CappedArray operator*(unsigned short i) const; CappedArray operator*(int i) const; CappedArray operator*(unsigned int i) const; CappedArray operator*(long i) const; CappedArray operator*(unsigned long i) const; CappedArray operator*(long long i) const; CappedArray operator*(unsigned long long i) const; CappedArray operator*(float f) const; CappedArray operator*(double f) const; CappedArray operator*(const void* s) const; #if KJ_COMPILER_SUPPORTS_STL_STRING_INTEROP // supports expression SFINAE? template ().toString())> inline Result operator*(T&& value) const { return kj::fwd(value).toString(); } #endif }; static KJ_CONSTEXPR(const) Stringifier STR = Stringifier(); } // namespace _ (private) template auto toCharSequence(T&& value) -> decltype(_::STR * kj::fwd(value)) { // Returns an iterable of chars that represent a textual representation of the value, suitable // for debugging. // // Most users should use str() instead, but toCharSequence() may occasionally be useful to avoid // heap allocation overhead that str() implies. // // To specialize this function for your type, see KJ_STRINGIFY. return _::STR * kj::fwd(value); } CappedArray hex(unsigned char i); CappedArray hex(unsigned short i); CappedArray hex(unsigned int i); CappedArray hex(unsigned long i); CappedArray hex(unsigned long long i); template String str(Params&&... params) { // Magic function which builds a string from a bunch of arbitrary values. Example: // str(1, " / ", 2, " = ", 0.5) // returns: // "1 / 2 = 0.5" // To teach `str` how to stringify a type, see `Stringifier`. return _::concat(toCharSequence(kj::fwd(params))...); } inline String str(String&& s) { return mv(s); } // Overload to prevent redundant allocation. template _::Delimited delimited(T&& arr, kj::StringPtr delim); // Use to stringify an array. template String strArray(T&& arr, const char* delim) { size_t delimLen = strlen(delim); KJ_STACK_ARRAY(decltype(_::STR * arr[0]), pieces, kj::size(arr), 8, 32); size_t size = 0; for (size_t i = 0; i < kj::size(arr); i++) { if (i > 0) size += delimLen; pieces[i] = _::STR * arr[i]; size += pieces[i].size(); } String result = heapString(size); char* pos = result.begin(); for (size_t i = 0; i < kj::size(arr); i++) { if (i > 0) { memcpy(pos, delim, delimLen); pos += delimLen; } pos = _::fill(pos, pieces[i]); } return result; } template StringPtr strPreallocated(ArrayPtr buffer, Params&&... params) { // Like str() but writes into a preallocated buffer. If the buffer is not long enough, the result // is truncated (but still NUL-terminated). // // This can be used like: // // char buffer[256]; // StringPtr text = strPreallocated(buffer, params...); // // This is useful for optimization. It can also potentially be used safely in async signal // handlers. HOWEVER, to use in an async signal handler, all of the stringifiers for the inputs // must also be signal-safe. KJ guarantees signal safety when stringifying any built-in integer // type (but NOT floating-points), basic char/byte sequences (ArrayPtr, String, etc.), as // well as Array as long as T can also be stringified safely. To safely stringify a delimited // array, you must use kj::delimited(arr, delim) rather than the deprecated // kj::strArray(arr, delim). char* end = _::fillLimited(buffer.begin(), buffer.end() - 1, toCharSequence(kj::fwd(params))...); *end = '\0'; return StringPtr(buffer.begin(), end); } template ()))> inline _::Delimited> operator*(const _::Stringifier&, ArrayPtr arr) { return _::Delimited>(arr, ", "); } template ()))> inline _::Delimited> operator*(const _::Stringifier&, const Array& arr) { return _::Delimited>(arr, ", "); } #define KJ_STRINGIFY(...) operator*(::kj::_::Stringifier, __VA_ARGS__) // Defines a stringifier for a custom type. Example: // // class Foo {...}; // inline StringPtr KJ_STRINGIFY(const Foo& foo) { return foo.name(); } // // or perhaps // inline String KJ_STRINGIFY(const Foo& foo) { return kj::str(foo.fld1(), ",", foo.fld2()); } // // This allows Foo to be passed to str(). // // The function should be declared either in the same namespace as the target type or in the global // namespace. It can return any type which is an iterable container of chars. // ======================================================================================= // Inline implementation details. inline StringPtr::StringPtr(const String& value): content(value.cStr(), value.size() + 1) {} inline StringPtr::StringPtr(const ConstString& value): content(value.cStr(), value.size() + 1) {} inline constexpr StringPtr::operator ArrayPtr() const { return ArrayPtr(content.begin(), content.size() - 1); } inline constexpr ArrayPtr StringPtr::asArray() const { return ArrayPtr(content.begin(), content.size() - 1); } inline bool StringPtr::operator==(const StringPtr& other) const { return content.size() == other.content.size() && memcmp(content.begin(), other.content.begin(), content.size() - 1) == 0; } inline bool StringPtr::operator<(const StringPtr& other) const { bool shorter = content.size() < other.content.size(); int cmp = memcmp(content.begin(), other.content.begin(), shorter ? content.size() : other.content.size()); return cmp < 0 || (cmp == 0 && shorter); } inline StringPtr StringPtr::slice(size_t start) const { return StringPtr(content.slice(start, content.size())); } inline ArrayPtr StringPtr::slice(size_t start, size_t end) const { return content.slice(start, end); } inline LiteralStringConst::operator ConstString() const { return ConstString(begin(), size(), NullArrayDisposer::instance); } inline ConstString StringPtr::attach() const { // This is meant as a roundabout way to make a ConstString from a StringPtr return ConstString(begin(), size(), NullArrayDisposer::instance); } template inline ConstString StringPtr::attach(Attachments&&... attachments) const { return ConstString { content.attach(kj::fwd(attachments)...) }; } inline String::operator ArrayPtr() { return content == nullptr ? ArrayPtr(nullptr) : content.slice(0, content.size() - 1); } inline String::operator ArrayPtr() const { return content == nullptr ? ArrayPtr(nullptr) : content.slice(0, content.size() - 1); } inline ConstString::operator ArrayPtr() const { return content == nullptr ? ArrayPtr(nullptr) : content.slice(0, content.size() - 1); } inline ArrayPtr String::asArray() { return content == nullptr ? ArrayPtr(nullptr) : content.slice(0, content.size() - 1); } inline ArrayPtr String::asArray() const { return content == nullptr ? ArrayPtr(nullptr) : content.slice(0, content.size() - 1); } inline ArrayPtr ConstString::asArray() const { return content == nullptr ? ArrayPtr(nullptr) : content.slice(0, content.size() - 1); } inline const char* String::cStr() const { return content == nullptr ? "" : content.begin(); } inline const char* ConstString::cStr() const { return content == nullptr ? "" : content.begin(); } inline size_t String::size() const { return content == nullptr ? 0 : content.size() - 1; } inline size_t ConstString::size() const { return content == nullptr ? 0 : content.size() - 1; } inline char String::operator[](size_t index) const { return content[index]; } inline char& String::operator[](size_t index) { return content[index]; } inline char ConstString::operator[](size_t index) const { return content[index]; } inline char* String::begin() { return content == nullptr ? nullptr : content.begin(); } inline char* String::end() { return content == nullptr ? nullptr : content.end() - 1; } inline const char* String::begin() const { return content == nullptr ? nullptr : content.begin(); } inline const char* String::end() const { return content == nullptr ? nullptr : content.end() - 1; } inline const char* ConstString::begin() const { return content == nullptr ? nullptr : content.begin(); } inline const char* ConstString::end() const { return content == nullptr ? nullptr : content.end() - 1; } inline String::String(char* value, size_t size, const ArrayDisposer& disposer) : content(value, size + 1, disposer) { KJ_IREQUIRE(value[size] == '\0', "String must be NUL-terminated."); } inline ConstString::ConstString(const char* value, size_t size, const ArrayDisposer& disposer) : content(value, size + 1, disposer) { KJ_IREQUIRE(value[size] == '\0', "String must be NUL-terminated."); } inline String::String(Array buffer): content(kj::mv(buffer)) { KJ_IREQUIRE(content.size() > 0 && content.back() == '\0', "String must be NUL-terminated."); } inline ConstString::ConstString(Array buffer): content(kj::mv(buffer)) { KJ_IREQUIRE(content.size() > 0 && content.back() == '\0', "String must be NUL-terminated."); } inline String heapString(const char* value) { return heapString(value, strlen(value)); } inline String heapString(StringPtr value) { return heapString(value.begin(), value.size()); } inline String heapString(const String& value) { return heapString(value.begin(), value.size()); } inline String heapString(ArrayPtr value) { return heapString(value.begin(), value.size()); } namespace _ { // private template class Delimited { public: Delimited(T array, kj::StringPtr delimiter) : array(kj::fwd(array)), delimiter(delimiter) {} // TODO(someday): In theory we should support iteration as a character sequence, but the iterator // will be pretty complicated. size_t size() { ensureStringifiedInitialized(); size_t result = 0; bool first = true; for (auto& e: stringified) { if (first) { first = false; } else { result += delimiter.size(); } result += e.size(); } return result; } char* flattenTo(char* __restrict__ target) { ensureStringifiedInitialized(); bool first = true; for (auto& elem: stringified) { if (first) { first = false; } else { target = fill(target, delimiter); } target = fill(target, elem); } return target; } char* flattenTo(char* __restrict__ target, char* limit) { // This is called in the strPreallocated(). We want to avoid allocation. size() will not have // been called in this case, so hopefully `stringified` is still uninitialized. We will // stringify each item and immediately use it. bool first = true; for (auto&& elem: array) { if (target == limit) return target; if (first) { first = false; } else { target = fillLimited(target, limit, delimiter); } target = fillLimited(target, limit, kj::toCharSequence(elem)); } return target; } private: typedef decltype(toCharSequence(*instance().begin())) StringifiedItem; T array; kj::StringPtr delimiter; Array stringified; void ensureStringifiedInitialized() { if (array.size() > 0 && stringified.size() == 0) { stringified = KJ_MAP(e, array) { return toCharSequence(e); }; } } }; template char* fill(char* __restrict__ target, Delimited&& first, Rest&&... rest) { target = first.flattenTo(target); return fill(target, kj::fwd(rest)...); } template char* fillLimited(char* __restrict__ target, char* limit, Delimited&& first, Rest&&... rest) { target = first.flattenTo(target, limit); return fillLimited(target, limit, kj::fwd(rest)...); } template char* fill(char* __restrict__ target, Delimited& first, Rest&&... rest) { target = first.flattenTo(target); return fill(target, kj::fwd(rest)...); } template char* fillLimited(char* __restrict__ target, char* limit, Delimited& first, Rest&&... rest) { target = first.flattenTo(target, limit); return fillLimited(target, limit, kj::fwd(rest)...); } template inline Delimited&& KJ_STRINGIFY(Delimited&& delimited) { return kj::mv(delimited); } template inline const Delimited& KJ_STRINGIFY(const Delimited& delimited) { return delimited; } } // namespace _ (private) template _::Delimited delimited(T&& arr, kj::StringPtr delim) { return _::Delimited(kj::fwd(arr), delim); } } // namespace kj constexpr kj::StringPtr operator "" _kj(const char* str, size_t n) { return kj::StringPtr(kj::ArrayPtr(str, n + 1)); }; constexpr kj::LiteralStringConst operator "" _kjc(const char* str, size_t n) { return kj::LiteralStringConst(kj::ArrayPtr(str, n + 1)); }; KJ_END_HEADER