| 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 | #include "string-tree.h" |
| 23 | |
| 24 | namespace kj { |
| 25 | |
| 26 | StringTree::StringTree(Array<StringTree>&& pieces, StringPtr delim) |
| 27 | : size_(0), |
| 28 | branches(heapArray<Branch>(pieces.size())) { |
| 29 | if (pieces.size() > 0) { |
| 30 | if (pieces.size() > 1 && delim.size() > 0) { |
| 31 | text = heapString((pieces.size() - 1) * delim.size()); |
| 32 | size_ = text.size(); |
| 33 | } |
| 34 | |
| 35 | branches[0].index = 0; |
| 36 | branches[0].content = kj::mv(pieces[0]); |
| 37 | size_ += pieces[0].size(); |
| 38 | |
| 39 | for (uint i = 1; i < pieces.size(); i++) { |
| 40 | if (delim.size() > 0) { |
| 41 | memcpy(text.begin() + (i - 1) * delim.size(), delim.begin(), delim.size()); |
| 42 | } |
| 43 | branches[i].index = i * delim.size(); |
| 44 | branches[i].content = kj::mv(pieces[i]); |
| 45 | size_ += pieces[i].size(); |
| 46 | } |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | String StringTree::flatten() const { |
| 51 | String result = heapString(size()); |
| 52 | flattenTo(result.begin()); |
| 53 | return result; |
| 54 | } |
| 55 | |
| 56 | char* StringTree::flattenTo(char* __restrict__ target) const { |
| 57 | visit([&target](ArrayPtr<const char> text) { |
| 58 | memcpy(target, text.begin(), text.size()); |
| 59 | target += text.size(); |
| 60 | }); |
| 61 | return target; |
| 62 | } |
| 63 | |
| 64 | char* StringTree::flattenTo(char* __restrict__ target, char* limit) const { |
| 65 | visit([&target,limit](ArrayPtr<const char> text) { |
| 66 | size_t size = kj::min(text.size(), limit - target); |
| 67 | memcpy(target, text.begin(), size); |
| 68 | target += size; |
| 69 | }); |
| 70 | return target; |
| 71 | } |
| 72 | |
| 73 | } // namespace kj |
| 74 | |