annotate win32-mingw/include/kj/string-tree.h @ 135:38d1c0e7850b

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