GCC Code Coverage Report


Directory: ./
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 98.0% 50 / 0 / 51
Functions: 100.0% 3 / 0 / 3
Branches: 94.3% 66 / 0 / 70

src/utilities/idd/Comments.cpp
Line Branch Exec Source
1 /***********************************************************************************************************************
2 * OpenStudio(R), Copyright (c) Alliance for Energy Innovation, LLC.
3 * See also https://openstudio.net/license
4 ***********************************************************************************************************************/
5
6 #include "Comments.hpp"
7 #include "CommentRegex.hpp"
8
9 #include <boost/algorithm/string.hpp>
10
11 #include <sstream>
12
13 namespace openstudio {
14
15 namespace detail {
16
17 8621940 std::string makeCommentHelper(const std::string& str, const std::string& linePrefix, int indentSize) {
18 // istream for parsing, ostream for composing comment
19
1/1
✓ Branch 2 taken 8621940 times.
8621940 std::stringstream ss(str);
20
1/1
✓ Branch 1 taken 8621940 times.
8621940 std::stringstream comment;
21
22 // helper variables
23
1/1
✓ Branch 1 taken 8621940 times.
8621940 boost::smatch m;
24 8621940 std::string line;
25 8621940 bool first = true;
26 8621940 bool commentStarted = false;
27 8621940 unsigned blankLineQueue = 0; // add blank lines in groups
28
29 // parse and compose comment line-by-line
30
4/4
✓ Branch 1 taken 17243901 times.
✓ Branch 4 taken 17243901 times.
✓ Branch 6 taken 8621961 times.
✓ Branch 7 taken 8621940 times.
17243901 while (std::getline(ss, line)) {
31 // capture comment already marked as such
32
4/4
✓ Branch 1 taken 8621961 times.
✓ Branch 4 taken 8621961 times.
✓ Branch 6 taken 9 times.
✓ Branch 7 taken 8621952 times.
8621961 if (boost::regex_match(line, m, commentRegex::editorCommentWhitespaceOnlyLine())) {
33
3/3
✓ Branch 1 taken 9 times.
✓ Branch 4 taken 9 times.
✓ Branch 7 taken 9 times.
18 line = std::string(m[1].first, m[1].second);
34 }
35
4/4
✓ Branch 1 taken 8621961 times.
✓ Branch 4 taken 8621961 times.
✓ Branch 6 taken 11 times.
✓ Branch 7 taken 8621950 times.
8621961 if (boost::regex_match(line, m, commentRegex::commentWhitespaceOnlyLine())) {
36
3/3
✓ Branch 1 taken 11 times.
✓ Branch 4 taken 11 times.
✓ Branch 7 taken 11 times.
22 line = std::string(m[1].first, m[1].second);
37 }
38 // handle blank lines separately
39
4/4
✓ Branch 1 taken 8621961 times.
✓ Branch 4 taken 8621961 times.
✓ Branch 6 taken 7 times.
✓ Branch 7 taken 8621954 times.
8621961 if (boost::regex_match(line, commentRegex::whitespaceOnlyLine())) {
40
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 4 times.
7 if (commentStarted) {
41 3 ++blankLineQueue;
42 } // may need to prepend with linePrefix
43 else {
44 // no comment yet--leave as pure blank line
45
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 if (!first) {
46
1/1
✓ Branch 1 taken 2 times.
2 comment << '\n';
47 }
48 }
49 } else {
50 // line has non-whitespace characters. prepend each queued blank line with linePrefix
51
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 8621954 times.
8621956 for (unsigned i = 0; i < blankLineQueue; ++i) {
52
2/2
✓ Branch 1 taken 2 times.
✓ Branch 4 taken 2 times.
2 comment << '\n' << linePrefix;
53 }
54 8621954 blankLineQueue = 0;
55
56 // align lefthand edge if not indented by at least indentSize
57
1/1
✓ Branch 2 taken 8621954 times.
8621954 std::string temp = boost::trim_left_copy(line);
58
1/2
✓ Branch 2 taken 8621954 times.
✗ Branch 3 not taken.
8621954 if (temp.size() > line.size() - indentSize) {
59
1/1
✓ Branch 1 taken 8621954 times.
8621954 line = temp;
60 }
61 // go to next line if this is not the first line
62
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 8621938 times.
8621954 if (!first) {
63
1/1
✓ Branch 1 taken 16 times.
16 comment << '\n';
64 }
65 // write out comment line
66
2/2
✓ Branch 1 taken 8621954 times.
✓ Branch 4 taken 8621954 times.
8621954 comment << linePrefix << line;
67 8621954 commentStarted = true;
68 8621954 }
69 8621961 first = false;
70 }
71
72 // write out remaining blank lines w/o commenting them
73
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 8621940 times.
8621941 for (unsigned i = 0; i < blankLineQueue; ++i) {
74
1/1
✓ Branch 1 taken 1 time.
1 comment << '\n';
75 }
76
77 // append '\n' if str ends with \n or \r
78
1/2
✓ Branch 1 taken 8621940 times.
✗ Branch 2 not taken.
8621940 if (!str.empty()) {
79
1/1
✓ Branch 1 taken 8621940 times.
8621940 boost::regex re("[\\n\\r]");
80
3/3
✓ Branch 4 taken 8621940 times.
✓ Branch 6 taken 7 times.
✓ Branch 7 taken 8621933 times.
8621940 if (boost::regex_match(str.end() - 1, str.end(), re)) {
81
1/1
✓ Branch 1 taken 7 times.
7 comment << '\n';
82 }
83 8621940 }
84
85
1/1
✓ Branch 1 taken 8621940 times.
17243880 return comment.str();
86 8621940 }
87
88 } // namespace detail
89
90 1711 std::string makeComment(const std::string& str) {
91
92 // make sure each line starts with !
93
1/1
✓ Branch 1 taken 1711 times.
1711 boost::smatch m;
94
4/4
✓ Branch 1 taken 1711 times.
✓ Branch 4 taken 1711 times.
✓ Branch 6 taken 1420 times.
✓ Branch 7 taken 291 times.
1711 if (boost::regex_match(str, m, commentRegex::commentWhitespaceOnlyBlock())) {
95 // already does, return as-is
96
1/1
✓ Branch 1 taken 1420 times.
1420 return str;
97 }
98
99 // prepend '! ' to each line, as needed
100
2/2
✓ Branch 1 taken 291 times.
✓ Branch 4 taken 291 times.
582 return detail::makeCommentHelper(str, "! ", 2);
101 1711 }
102
103 8621649 std::string makeIdfEditorComment(const std::string& str) {
104 // make sure each line starts with !-
105
1/1
✓ Branch 1 taken 8621649 times.
8621649 boost::smatch m;
106
3/4
✓ Branch 1 taken 8621649 times.
✓ Branch 4 taken 8621649 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 8621649 times.
8621649 if (boost::regex_match(str, m, commentRegex::editorCommentWhitespaceOnlyBlock())) {
107 // already does, return as-is
108 ✗ return str;
109 }
110
111 // prepend '!- ' to each line, as needed
112
2/2
✓ Branch 1 taken 8621649 times.
✓ Branch 4 taken 8621649 times.
17243298 return detail::makeCommentHelper(str, "!- ", 2);
113 8621649 }
114
115 } // namespace openstudio
116