GCC Code Coverage Report


Directory: src/
File: src/misc/xml.cpp
Date: 2025-10-15 09:43:47
Exec Total Coverage
Lines: 27 27 100.0%
Functions: 4 4 100.0%
Branches: 8 8 100.0%

Line Branch Exec Source
1 #include <string>
2 #include <gyoji-misc/xml.hpp>
3 #include <gyoji-misc/jstring.hpp>
4
5 using namespace Gyoji::misc;
6
7 118 std::string Gyoji::misc::xml_to_cdata(const std::string & str)
8 {
9 118 std::string output;
10 118 std::vector<std::string> split = string_split(str, "]]>");
11
2/2
✓ Branch 1 taken 124 times.
✓ Branch 2 taken 118 times.
242 for (size_t i = 0; i < split.size(); i++) {
12 124 bool has_prev = i > 0;
13 124 bool has_next = i < split.size()-1;
14
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 118 times.
248 std::string next(has_next ? "]]" : "");
15
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 118 times.
124 std::string prev(has_prev ? ">" : "");
16 124 output = output + "<![CDATA[" + prev + split[i].c_str() + next + "]]>";
17 124 }
18 118 return output;
19 118 }
20
21 44652 static std::string replace_all(const std::string& source, const std::string& from, const std::string & to)
22 {
23 44652 std::string newString;
24 44652 newString.reserve(source.length()); // avoids a few memory allocations
25
26 44652 std::string::size_type lastPos = 0;
27 std::string::size_type findPos;
28
29
2/2
✓ Branch 1 taken 1654 times.
✓ Branch 2 taken 44652 times.
46306 while(std::string::npos != (findPos = source.find(from, lastPos)))
30 {
31 1654 newString.append(source, lastPos, findPos - lastPos);
32 1654 newString += to;
33 1654 lastPos = findPos + from.length();
34 }
35
36 // Care for the rest after last occurrence
37 44652 newString += source.substr(lastPos);
38
39 44652 return newString;
40 }
41
42 18488 std::string Gyoji::misc::xml_escape_attribute(const std::string & str)
43 {
44 147904 return replace_all(replace_all(str, "\'", "&apos;"), "\"", "&quot;");
45 }
46
47 3838 std::string Gyoji::misc::xml_escape_whitespace(const std::string & str)
48 {
49 return replace_all(
50 15352 replace_all(
51 str,
52 "\n", "&#xA;"
53 ),
54 "\r",
55 "&#xD;"
56 23028 );
57 }
58