| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* Copyright 2025 Jonathan S. Arney | ||
| 2 | * | ||
| 3 | * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 4 | * you may not use this file except in compliance with the License. | ||
| 5 | * You may obtain a copy of the License at | ||
| 6 | * | ||
| 7 | * https://github.com/jarney/gyoji/blob/master/LICENSE | ||
| 8 | * | ||
| 9 | * Unless required by applicable law or agreed to in writing, software | ||
| 10 | * distributed under the License is distributed on an "AS IS" BASIS, | ||
| 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 12 | * See the License for the specific language governing permissions and | ||
| 13 | * limitations under the License. | ||
| 14 | */ | ||
| 15 | #include <string> | ||
| 16 | #include <gyoji-misc/xml.hpp> | ||
| 17 | #include <gyoji-misc/jstring.hpp> | ||
| 18 | |||
| 19 | using namespace Gyoji::misc; | ||
| 20 | |||
| 21 | 118 | std::string Gyoji::misc::xml_to_cdata(const std::string & str) | |
| 22 | { | ||
| 23 | 118 | std::string output; | |
| 24 | 118 | std::vector<std::string> split = string_split(str, "]]>"); | |
| 25 |
2/2✓ Branch 1 taken 124 times.
✓ Branch 2 taken 118 times.
|
242 | for (size_t i = 0; i < split.size(); i++) { |
| 26 | 124 | bool has_prev = i > 0; | |
| 27 | 124 | bool has_next = i < split.size()-1; | |
| 28 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 118 times.
|
248 | std::string next(has_next ? "]]" : ""); |
| 29 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 118 times.
|
124 | std::string prev(has_prev ? ">" : ""); |
| 30 | 124 | output = output + "<![CDATA[" + prev + split[i].c_str() + next + "]]>"; | |
| 31 | 124 | } | |
| 32 | 118 | return output; | |
| 33 | 118 | } | |
| 34 | |||
| 35 | 44552 | static std::string replace_all(const std::string& source, const std::string& from, const std::string & to) | |
| 36 | { | ||
| 37 | 44552 | std::string newString; | |
| 38 | 44552 | newString.reserve(source.length()); // avoids a few memory allocations | |
| 39 | |||
| 40 | 44552 | std::string::size_type lastPos = 0; | |
| 41 | std::string::size_type findPos; | ||
| 42 | |||
| 43 |
2/2✓ Branch 1 taken 1652 times.
✓ Branch 2 taken 44552 times.
|
46204 | while(std::string::npos != (findPos = source.find(from, lastPos))) |
| 44 | { | ||
| 45 | 1652 | newString.append(source, lastPos, findPos - lastPos); | |
| 46 | 1652 | newString += to; | |
| 47 | 1652 | lastPos = findPos + from.length(); | |
| 48 | } | ||
| 49 | |||
| 50 | // Care for the rest after last occurrence | ||
| 51 | 44552 | newString += source.substr(lastPos); | |
| 52 | |||
| 53 | 44552 | return newString; | |
| 54 | } | ||
| 55 | |||
| 56 | 18450 | std::string Gyoji::misc::xml_escape_attribute(const std::string & str) | |
| 57 | { | ||
| 58 | 147600 | return replace_all(replace_all(str, "\'", "'"), "\"", """); | |
| 59 | } | ||
| 60 | |||
| 61 | 3826 | std::string Gyoji::misc::xml_escape_whitespace(const std::string & str) | |
| 62 | { | ||
| 63 | return replace_all( | ||
| 64 | 15304 | replace_all( | |
| 65 | str, | ||
| 66 | "\n", "
" | ||
| 67 | ), | ||
| 68 | "\r", | ||
| 69 | "
" | ||
| 70 | 22956 | ); | |
| 71 | } | ||
| 72 |