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