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 | 8623941 | 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 8623941 times.
|
8623941 | std::stringstream ss(str); |
| 20 |
1/1✓ Branch 1 taken 8623941 times.
|
8623941 | std::stringstream comment; |
| 21 | |||
| 22 | // helper variables | ||
| 23 |
1/1✓ Branch 1 taken 8623941 times.
|
8623941 | boost::smatch m; |
| 24 | 8623941 | std::string line; | |
| 25 | 8623941 | bool first = true; | |
| 26 | 8623941 | bool commentStarted = false; | |
| 27 | 8623941 | unsigned blankLineQueue = 0; // add blank lines in groups | |
| 28 | |||
| 29 | // parse and compose comment line-by-line | ||
| 30 |
4/4✓ Branch 1 taken 17247903 times.
✓ Branch 4 taken 17247903 times.
✓ Branch 6 taken 8623962 times.
✓ Branch 7 taken 8623941 times.
|
17247903 | while (std::getline(ss, line)) { |
| 31 | // capture comment already marked as such | ||
| 32 |
4/4✓ Branch 1 taken 8623962 times.
✓ Branch 4 taken 8623962 times.
✓ Branch 6 taken 9 times.
✓ Branch 7 taken 8623953 times.
|
8623962 | 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 8623962 times.
✓ Branch 4 taken 8623962 times.
✓ Branch 6 taken 11 times.
✓ Branch 7 taken 8623951 times.
|
8623962 | 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 8623962 times.
✓ Branch 4 taken 8623962 times.
✓ Branch 6 taken 7 times.
✓ Branch 7 taken 8623955 times.
|
8623962 | 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 8623955 times.
|
8623957 | 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 | 8623955 | blankLineQueue = 0; | |
| 55 | |||
| 56 | // align lefthand edge if not indented by at least indentSize | ||
| 57 |
1/1✓ Branch 2 taken 8623955 times.
|
8623955 | std::string temp = boost::trim_left_copy(line); |
| 58 |
1/2✓ Branch 2 taken 8623955 times.
✗ Branch 3 not taken.
|
8623955 | if (temp.size() > line.size() - indentSize) { |
| 59 |
1/1✓ Branch 1 taken 8623955 times.
|
8623955 | 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 8623939 times.
|
8623955 | 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 8623955 times.
✓ Branch 4 taken 8623955 times.
|
8623955 | comment << linePrefix << line; |
| 67 | 8623955 | commentStarted = true; | |
| 68 | 8623955 | } | |
| 69 | 8623962 | 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 8623941 times.
|
8623942 | 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 8623941 times.
✗ Branch 2 not taken.
|
8623941 | if (!str.empty()) { |
| 79 |
1/1✓ Branch 1 taken 8623941 times.
|
8623941 | boost::regex re("[\\n\\r]"); |
| 80 |
3/3✓ Branch 4 taken 8623941 times.
✓ Branch 6 taken 7 times.
✓ Branch 7 taken 8623934 times.
|
8623941 | if (boost::regex_match(str.end() - 1, str.end(), re)) { |
| 81 |
1/1✓ Branch 1 taken 7 times.
|
7 | comment << '\n'; |
| 82 | } | ||
| 83 | 8623941 | } | |
| 84 | |||
| 85 |
1/1✓ Branch 1 taken 8623941 times.
|
17247882 | return comment.str(); |
| 86 | 8623941 | } | |
| 87 | |||
| 88 | } // namespace detail | ||
| 89 | |||
| 90 | 1722 | std::string makeComment(const std::string& str) { | |
| 91 | |||
| 92 | // make sure each line starts with ! | ||
| 93 |
1/1✓ Branch 1 taken 1722 times.
|
1722 | boost::smatch m; |
| 94 |
4/4✓ Branch 1 taken 1722 times.
✓ Branch 4 taken 1722 times.
✓ Branch 6 taken 1431 times.
✓ Branch 7 taken 291 times.
|
1722 | if (boost::regex_match(str, m, commentRegex::commentWhitespaceOnlyBlock())) { |
| 95 | // already does, return as-is | ||
| 96 |
1/1✓ Branch 1 taken 1431 times.
|
1431 | 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 | 1722 | } | |
| 102 | |||
| 103 | 8623650 | std::string makeIdfEditorComment(const std::string& str) { | |
| 104 | // make sure each line starts with !- | ||
| 105 |
1/1✓ Branch 1 taken 8623650 times.
|
8623650 | boost::smatch m; |
| 106 |
3/4✓ Branch 1 taken 8623650 times.
✓ Branch 4 taken 8623650 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 8623650 times.
|
8623650 | 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 8623650 times.
✓ Branch 4 taken 8623650 times.
|
17247300 | return detail::makeCommentHelper(str, "!- ", 2); |
| 113 | 8623650 | } | |
| 114 | |||
| 115 | } // namespace openstudio | ||
| 116 |