comparison win64-msvc/include/kj/string-tree.h @ 47:d93140aac40b

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