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