Chris@63: // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors Chris@63: // Licensed under the MIT License: Chris@63: // Chris@63: // Permission is hereby granted, free of charge, to any person obtaining a copy Chris@63: // of this software and associated documentation files (the "Software"), to deal Chris@63: // in the Software without restriction, including without limitation the rights Chris@63: // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell Chris@63: // copies of the Software, and to permit persons to whom the Software is Chris@63: // furnished to do so, subject to the following conditions: Chris@63: // Chris@63: // The above copyright notice and this permission notice shall be included in Chris@63: // all copies or substantial portions of the Software. Chris@63: // Chris@63: // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR Chris@63: // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, Chris@63: // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE Chris@63: // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER Chris@63: // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, Chris@63: // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN Chris@63: // THE SOFTWARE. Chris@63: Chris@63: #ifndef KJ_STRING_TREE_H_ Chris@63: #define KJ_STRING_TREE_H_ Chris@63: Chris@63: #if defined(__GNUC__) && !KJ_HEADER_WARNINGS Chris@63: #pragma GCC system_header Chris@63: #endif Chris@63: Chris@63: #include "string.h" Chris@63: Chris@63: namespace kj { Chris@63: Chris@63: class StringTree { Chris@63: // A long string, represented internally as a tree of strings. This data structure is like a Chris@63: // String, but optimized for concatenation and iteration at the expense of seek time. The Chris@63: // structure is intended to be used for building large text blobs from many small pieces, where Chris@63: // repeatedly concatenating smaller strings into larger ones would waste copies. This structure Chris@63: // is NOT intended for use cases requiring random access or computing substrings. For those, Chris@63: // you should use a Rope, which is a much more complicated data structure. Chris@63: // Chris@63: // The proper way to construct a StringTree is via kj::strTree(...), which works just like Chris@63: // kj::str(...) but returns a StringTree rather than a String. Chris@63: // Chris@63: // KJ_STRINGIFY() functions that construct large strings from many smaller strings are encouraged Chris@63: // to return StringTree rather than a flat char container. Chris@63: Chris@63: public: Chris@63: inline StringTree(): size_(0) {} Chris@63: inline StringTree(String&& text): size_(text.size()), text(kj::mv(text)) {} Chris@63: Chris@63: StringTree(Array&& pieces, StringPtr delim); Chris@63: // Build a StringTree by concatenating the given pieces, delimited by the given delimiter Chris@63: // (e.g. ", "). Chris@63: Chris@63: inline size_t size() const { return size_; } Chris@63: Chris@63: template Chris@63: void visit(Func&& func) const; Chris@63: Chris@63: String flatten() const; Chris@63: // Return the contents as a string. Chris@63: Chris@63: // TODO(someday): flatten() when *this is an rvalue and when branches.size() == 0 could simply Chris@63: // return `kj::mv(text)`. Requires reference qualifiers (Clang 3.3 / GCC 4.8). Chris@63: Chris@63: void flattenTo(char* __restrict__ target) const; Chris@63: // Copy the contents to the given character array. Does not add a NUL terminator. Chris@63: Chris@63: private: Chris@63: size_t size_; Chris@63: String text; Chris@63: Chris@63: struct Branch; Chris@63: Array branches; // In order. Chris@63: Chris@63: inline void fill(char* pos, size_t branchIndex); Chris@63: template Chris@63: void fill(char* pos, size_t branchIndex, First&& first, Rest&&... rest); Chris@63: template Chris@63: void fill(char* pos, size_t branchIndex, StringTree&& first, Rest&&... rest); Chris@63: template Chris@63: void fill(char* pos, size_t branchIndex, Array&& first, Rest&&... rest); Chris@63: template Chris@63: void fill(char* pos, size_t branchIndex, String&& first, Rest&&... rest); Chris@63: Chris@63: template Chris@63: static StringTree concat(Params&&... params); Chris@63: static StringTree&& concat(StringTree&& param) { return kj::mv(param); } Chris@63: Chris@63: template Chris@63: static inline size_t flatSize(const T& t) { return t.size(); } Chris@63: static inline size_t flatSize(String&& s) { return 0; } Chris@63: static inline size_t flatSize(StringTree&& s) { return 0; } Chris@63: Chris@63: template Chris@63: static inline size_t branchCount(const T& t) { return 0; } Chris@63: static inline size_t branchCount(String&& s) { return 1; } Chris@63: static inline size_t branchCount(StringTree&& s) { return 1; } Chris@63: Chris@63: template Chris@63: friend StringTree strTree(Params&&... params); Chris@63: }; Chris@63: Chris@63: inline StringTree&& KJ_STRINGIFY(StringTree&& tree) { return kj::mv(tree); } Chris@63: inline const StringTree& KJ_STRINGIFY(const StringTree& tree) { return tree; } Chris@63: Chris@63: inline StringTree KJ_STRINGIFY(Array&& trees) { return StringTree(kj::mv(trees), ""); } Chris@63: Chris@63: template Chris@63: StringTree strTree(Params&&... params); Chris@63: // Build a StringTree by stringifying the given parameters and concatenating the results. Chris@63: // If any of the parameters stringify to StringTree rvalues, they will be incorporated as Chris@63: // branches to avoid a copy. Chris@63: Chris@63: // ======================================================================================= Chris@63: // Inline implementation details Chris@63: Chris@63: namespace _ { // private Chris@63: Chris@63: template Chris@63: char* fill(char* __restrict__ target, const StringTree& first, Rest&&... rest) { Chris@63: // Make str() work with stringifiers that return StringTree by patching fill(). Chris@63: Chris@63: first.flattenTo(target); Chris@63: return fill(target + first.size(), kj::fwd(rest)...); Chris@63: } Chris@63: Chris@63: template constexpr bool isStringTree() { return false; } Chris@63: template <> constexpr bool isStringTree() { return true; } Chris@63: Chris@63: inline StringTree&& toStringTreeOrCharSequence(StringTree&& tree) { return kj::mv(tree); } Chris@63: inline StringTree toStringTreeOrCharSequence(String&& str) { return StringTree(kj::mv(str)); } Chris@63: Chris@63: template Chris@63: inline auto toStringTreeOrCharSequence(T&& value) Chris@63: -> decltype(toCharSequence(kj::fwd(value))) { Chris@63: static_assert(!isStringTree>(), Chris@63: "When passing a StringTree into kj::strTree(), either pass it by rvalue " Chris@63: "(use kj::mv(value)) or explicitly call value.flatten() to make a copy."); Chris@63: Chris@63: return toCharSequence(kj::fwd(value)); Chris@63: } Chris@63: Chris@63: } // namespace _ (private) Chris@63: Chris@63: struct StringTree::Branch { Chris@63: size_t index; Chris@63: // Index in `text` where this branch should be inserted. Chris@63: Chris@63: StringTree content; Chris@63: }; Chris@63: Chris@63: template Chris@63: void StringTree::visit(Func&& func) const { Chris@63: size_t pos = 0; Chris@63: for (auto& branch: branches) { Chris@63: if (branch.index > pos) { Chris@63: func(text.slice(pos, branch.index)); Chris@63: pos = branch.index; Chris@63: } Chris@63: branch.content.visit(func); Chris@63: } Chris@63: if (text.size() > pos) { Chris@63: func(text.slice(pos, text.size())); Chris@63: } Chris@63: } Chris@63: Chris@63: inline void StringTree::fill(char* pos, size_t branchIndex) { Chris@63: KJ_IREQUIRE(pos == text.end() && branchIndex == branches.size(), Chris@63: kj::str(text.end() - pos, ' ', branches.size() - branchIndex).cStr()); Chris@63: } Chris@63: Chris@63: template Chris@63: void StringTree::fill(char* pos, size_t branchIndex, First&& first, Rest&&... rest) { Chris@63: pos = _::fill(pos, kj::fwd(first)); Chris@63: fill(pos, branchIndex, kj::fwd(rest)...); Chris@63: } Chris@63: Chris@63: template Chris@63: void StringTree::fill(char* pos, size_t branchIndex, StringTree&& first, Rest&&... rest) { Chris@63: branches[branchIndex].index = pos - text.begin(); Chris@63: branches[branchIndex].content = kj::mv(first); Chris@63: fill(pos, branchIndex + 1, kj::fwd(rest)...); Chris@63: } Chris@63: Chris@63: template Chris@63: void StringTree::fill(char* pos, size_t branchIndex, String&& first, Rest&&... rest) { Chris@63: branches[branchIndex].index = pos - text.begin(); Chris@63: branches[branchIndex].content = StringTree(kj::mv(first)); Chris@63: fill(pos, branchIndex + 1, kj::fwd(rest)...); Chris@63: } Chris@63: Chris@63: template Chris@63: StringTree StringTree::concat(Params&&... params) { Chris@63: StringTree result; Chris@63: result.size_ = _::sum({params.size()...}); Chris@63: result.text = heapString( Chris@63: _::sum({StringTree::flatSize(kj::fwd(params))...})); Chris@63: result.branches = heapArray( Chris@63: _::sum({StringTree::branchCount(kj::fwd(params))...})); Chris@63: result.fill(result.text.begin(), 0, kj::fwd(params)...); Chris@63: return result; Chris@63: } Chris@63: Chris@63: template Chris@63: StringTree strTree(Params&&... params) { Chris@63: return StringTree::concat(_::toStringTreeOrCharSequence(kj::fwd(params))...); Chris@63: } Chris@63: Chris@63: } // namespace kj Chris@63: Chris@63: #endif // KJ_STRING_TREE_H_