annotate win64-msvc/include/kj/string-tree.h @ 169:223a55898ab9 tip default

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