annotate osx/include/kj/string-tree.h @ 49:3ab5a40c4e3b

Add Capnp and KJ builds for OSX
author Chris Cannam <cannam@all-day-breakfast.com>
date Tue, 25 Oct 2016 14:48:23 +0100
parents
children 0994c39f1e94
rev   line source
cannam@49 1 // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors
cannam@49 2 // Licensed under the MIT License:
cannam@49 3 //
cannam@49 4 // Permission is hereby granted, free of charge, to any person obtaining a copy
cannam@49 5 // of this software and associated documentation files (the "Software"), to deal
cannam@49 6 // in the Software without restriction, including without limitation the rights
cannam@49 7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
cannam@49 8 // copies of the Software, and to permit persons to whom the Software is
cannam@49 9 // furnished to do so, subject to the following conditions:
cannam@49 10 //
cannam@49 11 // The above copyright notice and this permission notice shall be included in
cannam@49 12 // all copies or substantial portions of the Software.
cannam@49 13 //
cannam@49 14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
cannam@49 15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
cannam@49 16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
cannam@49 17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
cannam@49 18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
cannam@49 19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
cannam@49 20 // THE SOFTWARE.
cannam@49 21
cannam@49 22 #ifndef KJ_STRING_TREE_H_
cannam@49 23 #define KJ_STRING_TREE_H_
cannam@49 24
cannam@49 25 #if defined(__GNUC__) && !KJ_HEADER_WARNINGS
cannam@49 26 #pragma GCC system_header
cannam@49 27 #endif
cannam@49 28
cannam@49 29 #include "string.h"
cannam@49 30
cannam@49 31 namespace kj {
cannam@49 32
cannam@49 33 class StringTree {
cannam@49 34 // A long string, represented internally as a tree of strings. This data structure is like a
cannam@49 35 // String, but optimized for concatenation and iteration at the expense of seek time. The
cannam@49 36 // structure is intended to be used for building large text blobs from many small pieces, where
cannam@49 37 // repeatedly concatenating smaller strings into larger ones would waste copies. This structure
cannam@49 38 // is NOT intended for use cases requiring random access or computing substrings. For those,
cannam@49 39 // you should use a Rope, which is a much more complicated data structure.
cannam@49 40 //
cannam@49 41 // The proper way to construct a StringTree is via kj::strTree(...), which works just like
cannam@49 42 // kj::str(...) but returns a StringTree rather than a String.
cannam@49 43 //
cannam@49 44 // KJ_STRINGIFY() functions that construct large strings from many smaller strings are encouraged
cannam@49 45 // to return StringTree rather than a flat char container.
cannam@49 46
cannam@49 47 public:
cannam@49 48 inline StringTree(): size_(0) {}
cannam@49 49 inline StringTree(String&& text): size_(text.size()), text(kj::mv(text)) {}
cannam@49 50
cannam@49 51 StringTree(Array<StringTree>&& pieces, StringPtr delim);
cannam@49 52 // Build a StringTree by concatenating the given pieces, delimited by the given delimiter
cannam@49 53 // (e.g. ", ").
cannam@49 54
cannam@49 55 inline size_t size() const { return size_; }
cannam@49 56
cannam@49 57 template <typename Func>
cannam@49 58 void visit(Func&& func) const;
cannam@49 59
cannam@49 60 String flatten() const;
cannam@49 61 // Return the contents as a string.
cannam@49 62
cannam@49 63 // TODO(someday): flatten() when *this is an rvalue and when branches.size() == 0 could simply
cannam@49 64 // return `kj::mv(text)`. Requires reference qualifiers (Clang 3.3 / GCC 4.8).
cannam@49 65
cannam@49 66 void flattenTo(char* __restrict__ target) const;
cannam@49 67 // Copy the contents to the given character array. Does not add a NUL terminator.
cannam@49 68
cannam@49 69 private:
cannam@49 70 size_t size_;
cannam@49 71 String text;
cannam@49 72
cannam@49 73 struct Branch;
cannam@49 74 Array<Branch> branches; // In order.
cannam@49 75
cannam@49 76 inline void fill(char* pos, size_t branchIndex);
cannam@49 77 template <typename First, typename... Rest>
cannam@49 78 void fill(char* pos, size_t branchIndex, First&& first, Rest&&... rest);
cannam@49 79 template <typename... Rest>
cannam@49 80 void fill(char* pos, size_t branchIndex, StringTree&& first, Rest&&... rest);
cannam@49 81 template <typename... Rest>
cannam@49 82 void fill(char* pos, size_t branchIndex, Array<char>&& first, Rest&&... rest);
cannam@49 83 template <typename... Rest>
cannam@49 84 void fill(char* pos, size_t branchIndex, String&& first, Rest&&... rest);
cannam@49 85
cannam@49 86 template <typename... Params>
cannam@49 87 static StringTree concat(Params&&... params);
cannam@49 88 static StringTree&& concat(StringTree&& param) { return kj::mv(param); }
cannam@49 89
cannam@49 90 template <typename T>
cannam@49 91 static inline size_t flatSize(const T& t) { return t.size(); }
cannam@49 92 static inline size_t flatSize(String&& s) { return 0; }
cannam@49 93 static inline size_t flatSize(StringTree&& s) { return 0; }
cannam@49 94
cannam@49 95 template <typename T>
cannam@49 96 static inline size_t branchCount(const T& t) { return 0; }
cannam@49 97 static inline size_t branchCount(String&& s) { return 1; }
cannam@49 98 static inline size_t branchCount(StringTree&& s) { return 1; }
cannam@49 99
cannam@49 100 template <typename... Params>
cannam@49 101 friend StringTree strTree(Params&&... params);
cannam@49 102 };
cannam@49 103
cannam@49 104 inline StringTree&& KJ_STRINGIFY(StringTree&& tree) { return kj::mv(tree); }
cannam@49 105 inline const StringTree& KJ_STRINGIFY(const StringTree& tree) { return tree; }
cannam@49 106
cannam@49 107 inline StringTree KJ_STRINGIFY(Array<StringTree>&& trees) { return StringTree(kj::mv(trees), ""); }
cannam@49 108
cannam@49 109 template <typename... Params>
cannam@49 110 StringTree strTree(Params&&... params);
cannam@49 111 // Build a StringTree by stringifying the given parameters and concatenating the results.
cannam@49 112 // If any of the parameters stringify to StringTree rvalues, they will be incorporated as
cannam@49 113 // branches to avoid a copy.
cannam@49 114
cannam@49 115 // =======================================================================================
cannam@49 116 // Inline implementation details
cannam@49 117
cannam@49 118 namespace _ { // private
cannam@49 119
cannam@49 120 template <typename... Rest>
cannam@49 121 char* fill(char* __restrict__ target, const StringTree& first, Rest&&... rest) {
cannam@49 122 // Make str() work with stringifiers that return StringTree by patching fill().
cannam@49 123
cannam@49 124 first.flattenTo(target);
cannam@49 125 return fill(target + first.size(), kj::fwd<Rest>(rest)...);
cannam@49 126 }
cannam@49 127
cannam@49 128 template <typename T> constexpr bool isStringTree() { return false; }
cannam@49 129 template <> constexpr bool isStringTree<StringTree>() { return true; }
cannam@49 130
cannam@49 131 inline StringTree&& toStringTreeOrCharSequence(StringTree&& tree) { return kj::mv(tree); }
cannam@49 132 inline StringTree toStringTreeOrCharSequence(String&& str) { return StringTree(kj::mv(str)); }
cannam@49 133
cannam@49 134 template <typename T>
cannam@49 135 inline auto toStringTreeOrCharSequence(T&& value)
cannam@49 136 -> decltype(toCharSequence(kj::fwd<T>(value))) {
cannam@49 137 static_assert(!isStringTree<Decay<T>>(),
cannam@49 138 "When passing a StringTree into kj::strTree(), either pass it by rvalue "
cannam@49 139 "(use kj::mv(value)) or explicitly call value.flatten() to make a copy.");
cannam@49 140
cannam@49 141 return toCharSequence(kj::fwd<T>(value));
cannam@49 142 }
cannam@49 143
cannam@49 144 } // namespace _ (private)
cannam@49 145
cannam@49 146 struct StringTree::Branch {
cannam@49 147 size_t index;
cannam@49 148 // Index in `text` where this branch should be inserted.
cannam@49 149
cannam@49 150 StringTree content;
cannam@49 151 };
cannam@49 152
cannam@49 153 template <typename Func>
cannam@49 154 void StringTree::visit(Func&& func) const {
cannam@49 155 size_t pos = 0;
cannam@49 156 for (auto& branch: branches) {
cannam@49 157 if (branch.index > pos) {
cannam@49 158 func(text.slice(pos, branch.index));
cannam@49 159 pos = branch.index;
cannam@49 160 }
cannam@49 161 branch.content.visit(func);
cannam@49 162 }
cannam@49 163 if (text.size() > pos) {
cannam@49 164 func(text.slice(pos, text.size()));
cannam@49 165 }
cannam@49 166 }
cannam@49 167
cannam@49 168 inline void StringTree::fill(char* pos, size_t branchIndex) {
cannam@49 169 KJ_IREQUIRE(pos == text.end() && branchIndex == branches.size(),
cannam@49 170 kj::str(text.end() - pos, ' ', branches.size() - branchIndex).cStr());
cannam@49 171 }
cannam@49 172
cannam@49 173 template <typename First, typename... Rest>
cannam@49 174 void StringTree::fill(char* pos, size_t branchIndex, First&& first, Rest&&... rest) {
cannam@49 175 pos = _::fill(pos, kj::fwd<First>(first));
cannam@49 176 fill(pos, branchIndex, kj::fwd<Rest>(rest)...);
cannam@49 177 }
cannam@49 178
cannam@49 179 template <typename... Rest>
cannam@49 180 void StringTree::fill(char* pos, size_t branchIndex, StringTree&& first, Rest&&... rest) {
cannam@49 181 branches[branchIndex].index = pos - text.begin();
cannam@49 182 branches[branchIndex].content = kj::mv(first);
cannam@49 183 fill(pos, branchIndex + 1, kj::fwd<Rest>(rest)...);
cannam@49 184 }
cannam@49 185
cannam@49 186 template <typename... Rest>
cannam@49 187 void StringTree::fill(char* pos, size_t branchIndex, String&& first, Rest&&... rest) {
cannam@49 188 branches[branchIndex].index = pos - text.begin();
cannam@49 189 branches[branchIndex].content = StringTree(kj::mv(first));
cannam@49 190 fill(pos, branchIndex + 1, kj::fwd<Rest>(rest)...);
cannam@49 191 }
cannam@49 192
cannam@49 193 template <typename... Params>
cannam@49 194 StringTree StringTree::concat(Params&&... params) {
cannam@49 195 StringTree result;
cannam@49 196 result.size_ = _::sum({params.size()...});
cannam@49 197 result.text = heapString(
cannam@49 198 _::sum({StringTree::flatSize(kj::fwd<Params>(params))...}));
cannam@49 199 result.branches = heapArray<StringTree::Branch>(
cannam@49 200 _::sum({StringTree::branchCount(kj::fwd<Params>(params))...}));
cannam@49 201 result.fill(result.text.begin(), 0, kj::fwd<Params>(params)...);
cannam@49 202 return result;
cannam@49 203 }
cannam@49 204
cannam@49 205 template <typename... Params>
cannam@49 206 StringTree strTree(Params&&... params) {
cannam@49 207 return StringTree::concat(_::toStringTreeOrCharSequence(kj::fwd<Params>(params))...);
cannam@49 208 }
cannam@49 209
cannam@49 210 } // namespace kj
cannam@49 211
cannam@49 212 #endif // KJ_STRING_TREE_H_