annotate win32-mingw/include/kj/string-tree.h @ 50:37d53a7e8262

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