Chris@64: // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors Chris@64: // Licensed under the MIT License: Chris@64: // Chris@64: // Permission is hereby granted, free of charge, to any person obtaining a copy Chris@64: // of this software and associated documentation files (the "Software"), to deal Chris@64: // in the Software without restriction, including without limitation the rights Chris@64: // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell Chris@64: // copies of the Software, and to permit persons to whom the Software is Chris@64: // furnished to do so, subject to the following conditions: Chris@64: // Chris@64: // The above copyright notice and this permission notice shall be included in Chris@64: // all copies or substantial portions of the Software. Chris@64: // Chris@64: // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR Chris@64: // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, Chris@64: // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE Chris@64: // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER Chris@64: // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, Chris@64: // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN Chris@64: // THE SOFTWARE. Chris@64: Chris@64: #ifndef KJ_STRING_H_ Chris@64: #define KJ_STRING_H_ Chris@64: Chris@64: #if defined(__GNUC__) && !KJ_HEADER_WARNINGS Chris@64: #pragma GCC system_header Chris@64: #endif Chris@64: Chris@64: #include Chris@64: #include "array.h" Chris@64: #include Chris@64: Chris@64: namespace kj { Chris@64: Chris@64: class StringPtr; Chris@64: class String; Chris@64: Chris@64: class StringTree; // string-tree.h Chris@64: Chris@64: // Our STL string SFINAE trick does not work with GCC 4.7, but it works with Clang and GCC 4.8, so Chris@64: // we'll just preprocess it out if not supported. Chris@64: #if __clang__ || __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8) || _MSC_VER Chris@64: #define KJ_COMPILER_SUPPORTS_STL_STRING_INTEROP 1 Chris@64: #endif Chris@64: Chris@64: // ======================================================================================= Chris@64: // StringPtr -- A NUL-terminated ArrayPtr containing UTF-8 text. Chris@64: // Chris@64: // NUL bytes are allowed to appear before the end of the string. The only requirement is that Chris@64: // a NUL byte appear immediately after the last byte of the content. This terminator byte is not Chris@64: // counted in the string's size. Chris@64: Chris@64: class StringPtr { Chris@64: public: Chris@64: inline StringPtr(): content("", 1) {} Chris@64: inline StringPtr(decltype(nullptr)): content("", 1) {} Chris@64: inline StringPtr(const char* value): content(value, strlen(value) + 1) {} Chris@64: inline StringPtr(const char* value, size_t size): content(value, size + 1) { Chris@64: KJ_IREQUIRE(value[size] == '\0', "StringPtr must be NUL-terminated."); Chris@64: } Chris@64: inline StringPtr(const char* begin, const char* end): StringPtr(begin, end - begin) {} Chris@64: inline StringPtr(const String& value); Chris@64: Chris@64: #if KJ_COMPILER_SUPPORTS_STL_STRING_INTEROP Chris@64: template ().c_str())> Chris@64: inline StringPtr(const T& t): StringPtr(t.c_str()) {} Chris@64: // Allow implicit conversion from any class that has a c_str() method (namely, std::string). Chris@64: // We use a template trick to detect std::string in order to avoid including the header for Chris@64: // those who don't want it. Chris@64: Chris@64: template ().c_str())> Chris@64: inline operator T() const { return cStr(); } Chris@64: // Allow implicit conversion to any class that has a c_str() method (namely, std::string). Chris@64: // We use a template trick to detect std::string in order to avoid including the header for Chris@64: // those who don't want it. Chris@64: #endif Chris@64: Chris@64: inline operator ArrayPtr() const; Chris@64: inline ArrayPtr asArray() const; Chris@64: inline ArrayPtr asBytes() const { return asArray().asBytes(); } Chris@64: // Result does not include NUL terminator. Chris@64: Chris@64: inline const char* cStr() const { return content.begin(); } Chris@64: // Returns NUL-terminated string. Chris@64: Chris@64: inline size_t size() const { return content.size() - 1; } Chris@64: // Result does not include NUL terminator. Chris@64: Chris@64: inline char operator[](size_t index) const { return content[index]; } Chris@64: Chris@64: inline const char* begin() const { return content.begin(); } Chris@64: inline const char* end() const { return content.end() - 1; } Chris@64: Chris@64: inline bool operator==(decltype(nullptr)) const { return content.size() <= 1; } Chris@64: inline bool operator!=(decltype(nullptr)) const { return content.size() > 1; } Chris@64: Chris@64: inline bool operator==(const StringPtr& other) const; Chris@64: inline bool operator!=(const StringPtr& other) const { return !(*this == other); } Chris@64: inline bool operator< (const StringPtr& other) const; Chris@64: inline bool operator> (const StringPtr& other) const { return other < *this; } Chris@64: inline bool operator<=(const StringPtr& other) const { return !(other < *this); } Chris@64: inline bool operator>=(const StringPtr& other) const { return !(*this < other); } Chris@64: Chris@64: inline StringPtr slice(size_t start) const; Chris@64: inline ArrayPtr slice(size_t start, size_t end) const; Chris@64: // A string slice is only NUL-terminated if it is a suffix, so slice() has a one-parameter Chris@64: // version that assumes end = size(). Chris@64: Chris@64: inline bool startsWith(const StringPtr& other) const; Chris@64: inline bool endsWith(const StringPtr& other) const; Chris@64: Chris@64: inline Maybe findFirst(char c) const; Chris@64: inline Maybe findLast(char c) const; Chris@64: Chris@64: template Chris@64: T parseAs() const; Chris@64: // Parse string as template number type. Chris@64: // Integer numbers prefixed by "0x" and "0X" are parsed in base 16 (like strtoi with base 0). Chris@64: // Integer numbers prefixed by "0" are parsed in base 10 (unlike strtoi with base 0). Chris@64: // Overflowed integer numbers throw exception. Chris@64: // Overflowed floating numbers return inf. Chris@64: Chris@64: private: Chris@64: inline StringPtr(ArrayPtr content): content(content) {} Chris@64: Chris@64: ArrayPtr content; Chris@64: }; Chris@64: Chris@64: inline bool operator==(const char* a, const StringPtr& b) { return b == a; } Chris@64: inline bool operator!=(const char* a, const StringPtr& b) { return b != a; } Chris@64: Chris@64: template <> char StringPtr::parseAs() const; Chris@64: template <> signed char StringPtr::parseAs() const; Chris@64: template <> unsigned char StringPtr::parseAs() const; Chris@64: template <> short StringPtr::parseAs() const; Chris@64: template <> unsigned short StringPtr::parseAs() const; Chris@64: template <> int StringPtr::parseAs() const; Chris@64: template <> unsigned StringPtr::parseAs() const; Chris@64: template <> long StringPtr::parseAs() const; Chris@64: template <> unsigned long StringPtr::parseAs() const; Chris@64: template <> long long StringPtr::parseAs() const; Chris@64: template <> unsigned long long StringPtr::parseAs() const; Chris@64: template <> float StringPtr::parseAs() const; Chris@64: template <> double StringPtr::parseAs() const; Chris@64: Chris@64: // ======================================================================================= Chris@64: // String -- A NUL-terminated Array containing UTF-8 text. Chris@64: // Chris@64: // NUL bytes are allowed to appear before the end of the string. The only requirement is that Chris@64: // a NUL byte appear immediately after the last byte of the content. This terminator byte is not Chris@64: // counted in the string's size. Chris@64: // Chris@64: // To allocate a String, you must call kj::heapString(). We do not implement implicit copying to Chris@64: // the heap because this hides potential inefficiency from the developer. Chris@64: Chris@64: class String { Chris@64: public: Chris@64: String() = default; Chris@64: inline String(decltype(nullptr)): content(nullptr) {} Chris@64: inline String(char* value, size_t size, const ArrayDisposer& disposer); Chris@64: // Does not copy. `size` does not include NUL terminator, but `value` must be NUL-terminated. Chris@64: inline explicit String(Array buffer); Chris@64: // Does not copy. Requires `buffer` ends with `\0`. Chris@64: Chris@64: inline operator ArrayPtr(); Chris@64: inline operator ArrayPtr() const; Chris@64: inline ArrayPtr asArray(); Chris@64: inline ArrayPtr asArray() const; Chris@64: inline ArrayPtr asBytes() { return asArray().asBytes(); } Chris@64: inline ArrayPtr asBytes() const { return asArray().asBytes(); } Chris@64: // Result does not include NUL terminator. Chris@64: Chris@64: inline Array releaseArray() { return kj::mv(content); } Chris@64: // Disowns the backing array (which includes the NUL terminator) and returns it. The String value Chris@64: // is clobbered (as if moved away). Chris@64: Chris@64: inline const char* cStr() const; Chris@64: Chris@64: inline size_t size() const; Chris@64: // Result does not include NUL terminator. Chris@64: Chris@64: inline char operator[](size_t index) const; Chris@64: inline char& operator[](size_t index); Chris@64: Chris@64: inline char* begin(); Chris@64: inline char* end(); Chris@64: inline const char* begin() const; Chris@64: inline const char* end() const; Chris@64: Chris@64: inline bool operator==(decltype(nullptr)) const { return content.size() <= 1; } Chris@64: inline bool operator!=(decltype(nullptr)) const { return content.size() > 1; } Chris@64: Chris@64: inline bool operator==(const StringPtr& other) const { return StringPtr(*this) == other; } Chris@64: inline bool operator!=(const StringPtr& other) const { return StringPtr(*this) != other; } Chris@64: inline bool operator< (const StringPtr& other) const { return StringPtr(*this) < other; } Chris@64: inline bool operator> (const StringPtr& other) const { return StringPtr(*this) > other; } Chris@64: inline bool operator<=(const StringPtr& other) const { return StringPtr(*this) <= other; } Chris@64: inline bool operator>=(const StringPtr& other) const { return StringPtr(*this) >= other; } Chris@64: Chris@64: inline bool startsWith(const StringPtr& other) const { return StringPtr(*this).startsWith(other);} Chris@64: inline bool endsWith(const StringPtr& other) const { return StringPtr(*this).endsWith(other); } Chris@64: Chris@64: inline StringPtr slice(size_t start) const { return StringPtr(*this).slice(start); } Chris@64: inline ArrayPtr slice(size_t start, size_t end) const { Chris@64: return StringPtr(*this).slice(start, end); Chris@64: } Chris@64: Chris@64: inline Maybe findFirst(char c) const { return StringPtr(*this).findFirst(c); } Chris@64: inline Maybe findLast(char c) const { return StringPtr(*this).findLast(c); } Chris@64: Chris@64: template Chris@64: T parseAs() const { return StringPtr(*this).parseAs(); } Chris@64: // Parse as number Chris@64: Chris@64: private: Chris@64: Array content; Chris@64: }; Chris@64: Chris@64: inline bool operator==(const char* a, const String& b) { return b == a; } Chris@64: inline bool operator!=(const char* a, const String& b) { return b != a; } Chris@64: Chris@64: String heapString(size_t size); Chris@64: // Allocate a String of the given size on the heap, not including NUL terminator. The NUL Chris@64: // terminator will be initialized automatically but the rest of the content is not initialized. Chris@64: Chris@64: String heapString(const char* value); Chris@64: String heapString(const char* value, size_t size); Chris@64: String heapString(StringPtr value); Chris@64: String heapString(const String& value); Chris@64: String heapString(ArrayPtr value); Chris@64: // Allocates a copy of the given value on the heap. Chris@64: Chris@64: // ======================================================================================= Chris@64: // Magic str() function which transforms parameters to text and concatenates them into one big Chris@64: // String. Chris@64: Chris@64: namespace _ { // private Chris@64: Chris@64: inline size_t sum(std::initializer_list nums) { Chris@64: size_t result = 0; Chris@64: for (auto num: nums) { Chris@64: result += num; Chris@64: } Chris@64: return result; Chris@64: } Chris@64: Chris@64: inline char* fill(char* ptr) { return ptr; } Chris@64: Chris@64: template Chris@64: char* fill(char* __restrict__ target, const StringTree& first, Rest&&... rest); Chris@64: // Make str() work with stringifiers that return StringTree by patching fill(). Chris@64: // Chris@64: // Defined in string-tree.h. Chris@64: Chris@64: template Chris@64: char* fill(char* __restrict__ target, const First& first, Rest&&... rest) { Chris@64: auto i = first.begin(); Chris@64: auto end = first.end(); Chris@64: while (i != end) { Chris@64: *target++ = *i++; Chris@64: } Chris@64: return fill(target, kj::fwd(rest)...); Chris@64: } Chris@64: Chris@64: template Chris@64: String concat(Params&&... params) { Chris@64: // Concatenate a bunch of containers into a single Array. The containers can be anything that Chris@64: // is iterable and whose elements can be converted to `char`. Chris@64: Chris@64: String result = heapString(sum({params.size()...})); Chris@64: fill(result.begin(), kj::fwd(params)...); Chris@64: return result; Chris@64: } Chris@64: Chris@64: inline String concat(String&& arr) { Chris@64: return kj::mv(arr); Chris@64: } Chris@64: Chris@64: struct Stringifier { Chris@64: // This is a dummy type with only one instance: STR (below). To make an arbitrary type Chris@64: // stringifiable, define `operator*(Stringifier, T)` to return an iterable container of `char`. Chris@64: // The container type must have a `size()` method. Be sure to declare the operator in the same Chris@64: // namespace as `T` **or** in the global scope. Chris@64: // Chris@64: // A more usual way to accomplish what we're doing here would be to require that you define Chris@64: // a function like `toString(T)` and then rely on argument-dependent lookup. However, this has Chris@64: // the problem that it pollutes other people's namespaces and even the global namespace. For Chris@64: // example, some other project may already have functions called `toString` which do something Chris@64: // different. Declaring `operator*` with `Stringifier` as the left operand cannot conflict with Chris@64: // anything. Chris@64: Chris@64: inline ArrayPtr operator*(ArrayPtr s) const { return s; } Chris@64: inline ArrayPtr operator*(ArrayPtr s) const { return s; } Chris@64: inline ArrayPtr operator*(const Array& s) const { return s; } Chris@64: inline ArrayPtr operator*(const Array& s) const { return s; } Chris@64: template Chris@64: inline ArrayPtr operator*(const CappedArray& s) const { return s; } Chris@64: template Chris@64: inline ArrayPtr operator*(const FixedArray& s) const { return s; } Chris@64: inline ArrayPtr operator*(const char* s) const { return arrayPtr(s, strlen(s)); } Chris@64: inline ArrayPtr operator*(const String& s) const { return s.asArray(); } Chris@64: inline ArrayPtr operator*(const StringPtr& s) const { return s.asArray(); } Chris@64: Chris@64: inline Range operator*(const Range& r) const { return r; } Chris@64: inline Repeat operator*(const Repeat& r) const { return r; } Chris@64: Chris@64: inline FixedArray operator*(char c) const { Chris@64: FixedArray result; Chris@64: result[0] = c; Chris@64: return result; Chris@64: } Chris@64: Chris@64: StringPtr operator*(decltype(nullptr)) const; Chris@64: StringPtr operator*(bool b) const; Chris@64: Chris@64: CappedArray operator*(signed char i) const; Chris@64: CappedArray operator*(unsigned char i) const; Chris@64: CappedArray operator*(short i) const; Chris@64: CappedArray operator*(unsigned short i) const; Chris@64: CappedArray operator*(int i) const; Chris@64: CappedArray operator*(unsigned int i) const; Chris@64: CappedArray operator*(long i) const; Chris@64: CappedArray operator*(unsigned long i) const; Chris@64: CappedArray operator*(long long i) const; Chris@64: CappedArray operator*(unsigned long long i) const; Chris@64: CappedArray operator*(float f) const; Chris@64: CappedArray operator*(double f) const; Chris@64: CappedArray operator*(const void* s) const; Chris@64: Chris@64: template Chris@64: String operator*(ArrayPtr arr) const; Chris@64: template Chris@64: String operator*(const Array& arr) const; Chris@64: Chris@64: #if KJ_COMPILER_SUPPORTS_STL_STRING_INTEROP // supports expression SFINAE? Chris@64: template ().toString())> Chris@64: inline Result operator*(T&& value) const { return kj::fwd(value).toString(); } Chris@64: #endif Chris@64: }; Chris@64: static KJ_CONSTEXPR(const) Stringifier STR = Stringifier(); Chris@64: Chris@64: } // namespace _ (private) Chris@64: Chris@64: template Chris@64: auto toCharSequence(T&& value) -> decltype(_::STR * kj::fwd(value)) { Chris@64: // Returns an iterable of chars that represent a textual representation of the value, suitable Chris@64: // for debugging. Chris@64: // Chris@64: // Most users should use str() instead, but toCharSequence() may occasionally be useful to avoid Chris@64: // heap allocation overhead that str() implies. Chris@64: // Chris@64: // To specialize this function for your type, see KJ_STRINGIFY. Chris@64: Chris@64: return _::STR * kj::fwd(value); Chris@64: } Chris@64: Chris@64: CappedArray hex(unsigned char i); Chris@64: CappedArray hex(unsigned short i); Chris@64: CappedArray hex(unsigned int i); Chris@64: CappedArray hex(unsigned long i); Chris@64: CappedArray hex(unsigned long long i); Chris@64: Chris@64: template Chris@64: String str(Params&&... params) { Chris@64: // Magic function which builds a string from a bunch of arbitrary values. Example: Chris@64: // str(1, " / ", 2, " = ", 0.5) Chris@64: // returns: Chris@64: // "1 / 2 = 0.5" Chris@64: // To teach `str` how to stringify a type, see `Stringifier`. Chris@64: Chris@64: return _::concat(toCharSequence(kj::fwd(params))...); Chris@64: } Chris@64: Chris@64: inline String str(String&& s) { return mv(s); } Chris@64: // Overload to prevent redundant allocation. Chris@64: Chris@64: template Chris@64: String strArray(T&& arr, const char* delim) { Chris@64: size_t delimLen = strlen(delim); Chris@64: KJ_STACK_ARRAY(decltype(_::STR * arr[0]), pieces, kj::size(arr), 8, 32); Chris@64: size_t size = 0; Chris@64: for (size_t i = 0; i < kj::size(arr); i++) { Chris@64: if (i > 0) size += delimLen; Chris@64: pieces[i] = _::STR * arr[i]; Chris@64: size += pieces[i].size(); Chris@64: } Chris@64: Chris@64: String result = heapString(size); Chris@64: char* pos = result.begin(); Chris@64: for (size_t i = 0; i < kj::size(arr); i++) { Chris@64: if (i > 0) { Chris@64: memcpy(pos, delim, delimLen); Chris@64: pos += delimLen; Chris@64: } Chris@64: pos = _::fill(pos, pieces[i]); Chris@64: } Chris@64: return result; Chris@64: } Chris@64: Chris@64: namespace _ { // private Chris@64: Chris@64: template Chris@64: inline String Stringifier::operator*(ArrayPtr arr) const { Chris@64: return strArray(arr, ", "); Chris@64: } Chris@64: Chris@64: template Chris@64: inline String Stringifier::operator*(const Array& arr) const { Chris@64: return strArray(arr, ", "); Chris@64: } Chris@64: Chris@64: } // namespace _ (private) Chris@64: Chris@64: #define KJ_STRINGIFY(...) operator*(::kj::_::Stringifier, __VA_ARGS__) Chris@64: // Defines a stringifier for a custom type. Example: Chris@64: // Chris@64: // class Foo {...}; Chris@64: // inline StringPtr KJ_STRINGIFY(const Foo& foo) { return foo.name(); } Chris@64: // Chris@64: // This allows Foo to be passed to str(). Chris@64: // Chris@64: // The function should be declared either in the same namespace as the target type or in the global Chris@64: // namespace. It can return any type which is an iterable container of chars. Chris@64: Chris@64: // ======================================================================================= Chris@64: // Inline implementation details. Chris@64: Chris@64: inline StringPtr::StringPtr(const String& value): content(value.begin(), value.size() + 1) {} Chris@64: Chris@64: inline StringPtr::operator ArrayPtr() const { Chris@64: return content.slice(0, content.size() - 1); Chris@64: } Chris@64: Chris@64: inline ArrayPtr StringPtr::asArray() const { Chris@64: return content.slice(0, content.size() - 1); Chris@64: } Chris@64: Chris@64: inline bool StringPtr::operator==(const StringPtr& other) const { Chris@64: return content.size() == other.content.size() && Chris@64: memcmp(content.begin(), other.content.begin(), content.size() - 1) == 0; Chris@64: } Chris@64: Chris@64: inline bool StringPtr::operator<(const StringPtr& other) const { Chris@64: bool shorter = content.size() < other.content.size(); Chris@64: int cmp = memcmp(content.begin(), other.content.begin(), Chris@64: shorter ? content.size() : other.content.size()); Chris@64: return cmp < 0 || (cmp == 0 && shorter); Chris@64: } Chris@64: Chris@64: inline StringPtr StringPtr::slice(size_t start) const { Chris@64: return StringPtr(content.slice(start, content.size())); Chris@64: } Chris@64: inline ArrayPtr StringPtr::slice(size_t start, size_t end) const { Chris@64: return content.slice(start, end); Chris@64: } Chris@64: Chris@64: inline bool StringPtr::startsWith(const StringPtr& other) const { Chris@64: return other.content.size() <= content.size() && Chris@64: memcmp(content.begin(), other.content.begin(), other.size()) == 0; Chris@64: } Chris@64: inline bool StringPtr::endsWith(const StringPtr& other) const { Chris@64: return other.content.size() <= content.size() && Chris@64: memcmp(end() - other.size(), other.content.begin(), other.size()) == 0; Chris@64: } Chris@64: Chris@64: inline Maybe StringPtr::findFirst(char c) const { Chris@64: const char* pos = reinterpret_cast(memchr(content.begin(), c, size())); Chris@64: if (pos == nullptr) { Chris@64: return nullptr; Chris@64: } else { Chris@64: return pos - content.begin(); Chris@64: } Chris@64: } Chris@64: Chris@64: inline Maybe StringPtr::findLast(char c) const { Chris@64: for (size_t i = size(); i > 0; --i) { Chris@64: if (content[i-1] == c) { Chris@64: return i-1; Chris@64: } Chris@64: } Chris@64: return nullptr; Chris@64: } Chris@64: Chris@64: inline String::operator ArrayPtr() { Chris@64: return content == nullptr ? ArrayPtr(nullptr) : content.slice(0, content.size() - 1); Chris@64: } Chris@64: inline String::operator ArrayPtr() const { Chris@64: return content == nullptr ? ArrayPtr(nullptr) : content.slice(0, content.size() - 1); Chris@64: } Chris@64: Chris@64: inline ArrayPtr String::asArray() { Chris@64: return content == nullptr ? ArrayPtr(nullptr) : content.slice(0, content.size() - 1); Chris@64: } Chris@64: inline ArrayPtr String::asArray() const { Chris@64: return content == nullptr ? ArrayPtr(nullptr) : content.slice(0, content.size() - 1); Chris@64: } Chris@64: Chris@64: inline const char* String::cStr() const { return content == nullptr ? "" : content.begin(); } Chris@64: Chris@64: inline size_t String::size() const { return content == nullptr ? 0 : content.size() - 1; } Chris@64: Chris@64: inline char String::operator[](size_t index) const { return content[index]; } Chris@64: inline char& String::operator[](size_t index) { return content[index]; } Chris@64: Chris@64: inline char* String::begin() { return content == nullptr ? nullptr : content.begin(); } Chris@64: inline char* String::end() { return content == nullptr ? nullptr : content.end() - 1; } Chris@64: inline const char* String::begin() const { return content == nullptr ? nullptr : content.begin(); } Chris@64: inline const char* String::end() const { return content == nullptr ? nullptr : content.end() - 1; } Chris@64: Chris@64: inline String::String(char* value, size_t size, const ArrayDisposer& disposer) Chris@64: : content(value, size + 1, disposer) { Chris@64: KJ_IREQUIRE(value[size] == '\0', "String must be NUL-terminated."); Chris@64: } Chris@64: Chris@64: inline String::String(Array buffer): content(kj::mv(buffer)) { Chris@64: KJ_IREQUIRE(content.size() > 0 && content.back() == '\0', "String must be NUL-terminated."); Chris@64: } Chris@64: Chris@64: inline String heapString(const char* value) { Chris@64: return heapString(value, strlen(value)); Chris@64: } Chris@64: inline String heapString(StringPtr value) { Chris@64: return heapString(value.begin(), value.size()); Chris@64: } Chris@64: inline String heapString(const String& value) { Chris@64: return heapString(value.begin(), value.size()); Chris@64: } Chris@64: inline String heapString(ArrayPtr value) { Chris@64: return heapString(value.begin(), value.size()); Chris@64: } Chris@64: Chris@64: } // namespace kj Chris@64: Chris@64: #endif // KJ_STRING_H_