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