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