src/utilities/units/QuantityRegex.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 "QuantityRegex.hpp" | ||
| 7 | #include "../core/Logger.hpp" | ||
| 8 | |||
| 9 | #include <sstream> | ||
| 10 | |||
| 11 | namespace openstudio { | ||
| 12 | |||
| 13 | // JM@2017-12-14: | ||
| 14 | // Note from Julien Marrec: I heavily commented this file in December 2017, | ||
| 15 | // in the hope that it will make any debugging needed down the road a lot smoother | ||
| 16 | // Beware that the "standard flavor" equivalents are valid at the time I am writing this | ||
| 17 | // Especially for case where I put the "full" equivalent that adds the bit from another function returning a regex, | ||
| 18 | // if the referenced function is changed, then it'll no longer be valid | ||
| 19 | |||
| 20 | 69 | const boost::regex& regexFixedPrecisionValue() { | |
| 21 | // Equivalent to "-?\d*\.?\d+" in standard flavor (PRCE, python or JS should work) | ||
| 22 | // (optional minus sign, then digits with potentially a decimal point) | ||
| 23 | // Matches stuff like "10" "1.012", "-.12" | ||
| 24 |
4/7✓ Branch 0 taken 5 times.
✓ Branch 1 taken 64 times.
✓ Branch 3 taken 5 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 5 times.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
|
69 | static boost::regex rgx("-?[[:digit:]]*\\.?[[:digit:]]+"); |
| 25 | 69 | return rgx; | |
| 26 | } | ||
| 27 | |||
| 28 | 15 | const boost::regex& regexEmbeddedFixedPrecisionValue() { | |
| 29 |
1/1✓ Branch 1 taken 15 times.
|
15 | std::stringstream regexComposer; |
| 30 | // First bit is a non capturing group to ensure the regexFixedPrecisionValue will be found on the beginning of the string | ||
| 31 | // (or with anything but whitespace) | ||
| 32 | // Then regexFixedPrecisionValue matches a number (int or float), which is the only capturing group here | ||
| 33 | // Last bit checks it's the end of the string, or there's a space, a "." then end, a dot then space, a comma, a semi colon | ||
| 34 | // regexComposer in standard flavor is "(?:^| )(-?\d*\.?\d+)(?:$| |\.$|\. |,|;)" | ||
| 35 |
5/5✓ Branch 1 taken 15 times.
✓ Branch 4 taken 15 times.
✓ Branch 7 taken 15 times.
✓ Branch 10 taken 15 times.
✓ Branch 13 taken 15 times.
|
15 | regexComposer << "(?:^| )(" << regexFixedPrecisionValue().str() << ")(?:$| |\\.$|\\. |,|;)"; |
| 36 |
5/8✓ Branch 0 taken 1 time.
✓ Branch 1 taken 14 times.
✓ Branch 3 taken 1 time.
✗ Branch 4 not taken.
✓ Branch 6 taken 1 time.
✓ Branch 9 taken 1 time.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
|
15 | static boost::regex rgx(regexComposer.str()); |
| 37 | 15 | return rgx; | |
| 38 | 15 | } | |
| 39 | |||
| 40 | 17 | bool isFixedPrecisionValue(const std::string& s) { | |
| 41 | 17 | return boost::regex_match(s, regexFixedPrecisionValue()); | |
| 42 | } | ||
| 43 | |||
| 44 | 15 | bool containsFixedPrecisionValue(const std::string& s) { | |
| 45 | 15 | return boost::regex_search(s, regexEmbeddedFixedPrecisionValue()); | |
| 46 | } | ||
| 47 | |||
| 48 | 69 | const boost::regex& regexScientificNotationValue() { | |
| 49 | // Equivalent to "-?\d*[.]?\d+[EDed][-\+]?\d+" in standard flavor | ||
| 50 | // will match: "-10.5e5", "10d+5", ".1E10" | ||
| 51 | // Will not match: 1e0.5 (will actually match 1e0 here...) | ||
| 52 |
4/7✓ Branch 0 taken 6 times.
✓ Branch 1 taken 63 times.
✓ Branch 3 taken 6 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 6 times.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
|
69 | static boost::regex rgx("-?[[:digit:]]*[.]?[[:digit:]]+[EDed][-\\+]?[[:digit:]]+"); |
| 53 | 69 | return rgx; | |
| 54 | } | ||
| 55 | |||
| 56 | 14 | const boost::regex& regexEmbeddedScientificNotationValue() { | |
| 57 |
1/1✓ Branch 1 taken 14 times.
|
14 | std::stringstream regexComposer; |
| 58 | // Same idea as in regexEmbeddedFixedPrecisionValue | ||
| 59 |
5/5✓ Branch 1 taken 14 times.
✓ Branch 4 taken 14 times.
✓ Branch 7 taken 14 times.
✓ Branch 10 taken 14 times.
✓ Branch 13 taken 14 times.
|
14 | regexComposer << "(?:^| )(" << regexScientificNotationValue().str() << ")(?:$| |\\.$|\\. |,|;)"; |
| 60 |
5/8✓ Branch 0 taken 4 times.
✓ Branch 1 taken 10 times.
✓ Branch 3 taken 4 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 4 times.
✓ Branch 9 taken 4 times.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
|
14 | static boost::regex rgx(regexComposer.str()); |
| 61 | 14 | return rgx; | |
| 62 | 14 | } | |
| 63 | |||
| 64 | 18 | bool isScientificNotationValue(const std::string& s) { | |
| 65 | 18 | return boost::regex_match(s, regexScientificNotationValue()); | |
| 66 | } | ||
| 67 | |||
| 68 | 14 | bool containsScientificNotationValue(const std::string& s) { | |
| 69 | 14 | return boost::regex_search(s, regexEmbeddedScientificNotationValue()); | |
| 70 | } | ||
| 71 | |||
| 72 | 1425891 | const boost::regex& regexBaseUnit() { | |
| 73 | // ETH@20120320 It doesn't seem like the last [\\l\\u]{0,10} should not be necessary, but now | ||
| 74 | // is not a good time to try removing it. | ||
| 75 | // ETH@20120320 Should also tighten up to require {} if > 1 character in denominator. | ||
| 76 | // ETH@20120711 Trying to address both of the issues above. | ||
| 77 | /* JM@2017-12-14: | ||
| 78 | * \\l => [[:lower:]] => any lower case character <=> [a-z] | ||
| 79 | * \\u => [[:upper::]] => any upper case character <=> [A-Z] | ||
| 80 | * \\d => [[:digit:]] => any digit <=> [0-9] | ||
| 81 | * \\$ => litteral dollar sign? | ||
| 82 | * Eg: [\\l\\u\\d\\$] <=> [a-zA-Z0-9\$] in a PRCE flavor | ||
| 83 | * | ||
| 84 | * | ||
| 85 | * (?:...) is a non-capturing group | ||
| 86 | * Let's decompose this regex: | ||
| 87 | * Optionally, the string can start with a '\', then | ||
| 88 | * First bit looks for [a-zA-Z\$], between 1 to 8 times | ||
| 89 | * Second bit (Optional non capturing groups), looks for either _[a-zA-Z0-9\$] or _\{[a-zA-Z0-9\$]{1,10}\} | ||
| 90 | * This means that "_2" will be match, "_{2}", "_{24}" but not "2", nor "_24". | ||
| 91 | * It can NOT be present too | ||
| 92 | * Last bit looks for optional [a-zA-Z\$], between 0 and 2 times | ||
| 93 | * | ||
| 94 | * The entire regex can be tested in regular flavor using this: | ||
| 95 | * "\\?[a-zA-Z\$]{1,8}(?:_(?:[a-zA-Z0-9\$]|\{[a-zA-Z0-9\$]{1,10}\}))?[a-zA-Z\$]{0,2}" | ||
| 96 | * | ||
| 97 | * Examples: | ||
| 98 | * Matches in full: "ft_{2}O", "ftH_2O", "ftH_{24}O" "ft", "lux" | ||
| 99 | * Matches only the "m" in the following: m^3, m^-{-1} | ||
| 100 | * Doesn't match: "98", "ftH_24O" | ||
| 101 | * (Warning: will match "ftH_2" and "O" in "ft_H24O" if global flag in search) | ||
| 102 | */ | ||
| 103 |
4/7✓ Branch 0 taken 113 times.
✓ Branch 1 taken 1425778 times.
✓ Branch 3 taken 113 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 113 times.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
|
1425891 | static boost::regex rgx("\\\\?[\\l\\u\\$]{1,8}(?:_(?:[\\l\\u\\d\\$]|\\{[\\l\\u\\d\\$]{1,10}\\}))?[\\l\\u\\$]{0,2}"); |
| 104 | 1425891 | return rgx; | |
| 105 | } | ||
| 106 | |||
| 107 | 145377 | const boost::regex& regexExponent() { | |
| 108 | // This regex looks for "^" (litteral), optional "{", captures (optional "-" \d+) and finally optional "}" | ||
| 109 | // The actual exponent is in the only capturing group, so match[1] is the exponent | ||
| 110 | // Standard flavor equivalent: "(?:\^\{?)(-?\d+)(?:\})?" | ||
| 111 |
4/7✓ Branch 0 taken 107 times.
✓ Branch 1 taken 145270 times.
✓ Branch 3 taken 107 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 107 times.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
|
145377 | static boost::regex rgx("(?:\\^\\{?)(-?[[:digit:]]+)(?:\\})?"); |
| 112 | |||
| 113 | 145377 | return rgx; | |
| 114 | } | ||
| 115 | |||
| 116 | 1280514 | const boost::regex& regexAtomicUnit() { | |
| 117 |
1/1✓ Branch 1 taken 1280514 times.
|
1280514 | std::stringstream regexComposer; |
| 118 | // The portion written here (without regexBaseUnit) is equivalent to "(?:\^(?:\d+|\{-\d+\}))?" | ||
| 119 | // Meaning it will match (but not capture) stuff like "^3" or "^{-3}" but not "^{3}", and its optional | ||
| 120 | // so Overall the "m" is matched in "m", "m^3", "m^{-1}" | ||
| 121 | // | ||
| 122 | // Valid until anything above is changed: the full one is equivalent to | ||
| 123 | // "\\?[a-zA-Z\$]{1,8}(?:_(?:[a-zA-Z0-9\$]|\{[a-zA-Z0-9\$]{1,10}\}))?[a-zA-Z\$]{0,2}(?:\^(?:\d+|\{-\d+\}))?" | ||
| 124 | // | ||
| 125 | // Currently because the exponent portion here is optional, it'll match anything that matches regexBaseUnit, | ||
| 126 |
4/4✓ Branch 1 taken 1280514 times.
✓ Branch 4 taken 1280514 times.
✓ Branch 7 taken 1280514 times.
✓ Branch 10 taken 1280514 times.
|
1280514 | regexComposer << regexBaseUnit().str() << "(?:\\^(?:[[:digit:]]+|\\{-[[:digit:]]+\\}))?"; |
| 127 |
5/8✓ Branch 0 taken 113 times.
✓ Branch 1 taken 1280401 times.
✓ Branch 3 taken 113 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 113 times.
✓ Branch 9 taken 113 times.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
|
1280514 | static boost::regex rgx(regexComposer.str()); |
| 128 | 1280514 | return rgx; | |
| 129 | 1280514 | } | |
| 130 | |||
| 131 | 8 | const boost::regex& regexEmbeddedAtomicUnit() { | |
| 132 |
1/1✓ Branch 1 taken 8 times.
|
8 | std::stringstream regexComposer; |
| 133 | // First bit checks that it's the beggining of the string, or a space is before, or litteral "*", "/" or "(" | ||
| 134 | // then regexAtomicUnit | ||
| 135 | // then ensures it's the end of the string, a space, or a litteral "*", "/" or ")" | ||
| 136 |
5/5✓ Branch 1 taken 8 times.
✓ Branch 4 taken 8 times.
✓ Branch 7 taken 8 times.
✓ Branch 10 taken 8 times.
✓ Branch 13 taken 8 times.
|
8 | regexComposer << "(?:^| |\\*|/|\\()(" << regexAtomicUnit().str() << ")(?:$| |\\*|/|\\))"; |
| 137 |
5/8✓ Branch 0 taken 1 time.
✓ Branch 1 taken 7 times.
✓ Branch 3 taken 1 time.
✗ Branch 4 not taken.
✓ Branch 6 taken 1 time.
✓ Branch 9 taken 1 time.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
|
8 | static boost::regex rgx(regexComposer.str()); |
| 138 | 8 | return rgx; | |
| 139 | 8 | } | |
| 140 | |||
| 141 | 145395 | bool isAtomicUnit(const std::string& s) { | |
| 142 | 145395 | return boost::regex_match(s, regexAtomicUnit()); | |
| 143 | } | ||
| 144 | |||
| 145 | 8 | bool containsAtomicUnit(const std::string& s) { | |
| 146 | 8 | return boost::regex_search(s, regexEmbeddedAtomicUnit()); | |
| 147 | } | ||
| 148 | |||
| 149 | 494821 | const boost::regex& regexCompoundUnit() { | |
| 150 | // ETH@20120320 Somehow a quantity (0.12 kg) is getting by this regex. Now is not a good | ||
| 151 | // time to try to fix. | ||
| 152 | // JM@2017-12-14: Not fixing now, but this is because all of the groups are optional ("?"), except [a-zA-Z\$]{1,8} (from regexBaseUnit) | ||
| 153 |
1/1✓ Branch 1 taken 494821 times.
|
494821 | std::stringstream regexComposer; |
| 154 |
1/1✓ Branch 1 taken 494821 times.
|
494821 | std::stringstream tempComposer; |
| 155 | // place atomicUnit*atomicUnit*... in tempComposer | ||
| 156 |
8/8✓ Branch 1 taken 494821 times.
✓ Branch 4 taken 494821 times.
✓ Branch 7 taken 494821 times.
✓ Branch 10 taken 494821 times.
✓ Branch 13 taken 494821 times.
✓ Branch 16 taken 494821 times.
✓ Branch 19 taken 494821 times.
✓ Branch 22 taken 494821 times.
|
494821 | tempComposer << regexAtomicUnit().str() << "(?:\\*" << regexAtomicUnit().str() << ")*"; |
| 157 | // possibilities: tC.str(), tC.str()/tC.str(), 1/tC.str(). | ||
| 158 |
6/6✓ Branch 1 taken 494821 times.
✓ Branch 4 taken 494821 times.
✓ Branch 7 taken 494821 times.
✓ Branch 10 taken 494821 times.
✓ Branch 13 taken 494821 times.
✓ Branch 16 taken 494821 times.
|
494821 | regexComposer << "(?:(?:" << tempComposer.str() << "|1)/)?" << tempComposer.str(); |
| 159 |
5/8✓ Branch 0 taken 111 times.
✓ Branch 1 taken 494710 times.
✓ Branch 3 taken 111 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 111 times.
✓ Branch 9 taken 111 times.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
|
494821 | static boost::regex rgx(regexComposer.str()); |
| 160 | 494821 | return rgx; | |
| 161 | 494821 | } | |
| 162 | |||
| 163 | 18 | const boost::regex& regexEmbeddedCompoundUnit() { | |
| 164 |
1/1✓ Branch 1 taken 18 times.
|
18 | std::stringstream regexComposer; |
| 165 | // can be surrounded by ^$ (). | ||
| 166 |
5/5✓ Branch 1 taken 18 times.
✓ Branch 4 taken 18 times.
✓ Branch 7 taken 18 times.
✓ Branch 10 taken 18 times.
✓ Branch 13 taken 18 times.
|
18 | regexComposer << "(?:^| |\\()(" << regexCompoundUnit().str() << ")(?:$| |\\)|\\.$|\\. |,|;)"; |
| 167 |
5/8✓ Branch 0 taken 4 times.
✓ Branch 1 taken 14 times.
✓ Branch 3 taken 4 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 4 times.
✓ Branch 9 taken 4 times.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
|
18 | static boost::regex rgx(regexComposer.str()); |
| 168 | 18 | return rgx; | |
| 169 | 18 | } | |
| 170 | |||
| 171 | 123566 | bool isCompoundUnit(const std::string& s) { | |
| 172 | 123566 | return boost::regex_match(s, regexCompoundUnit()); | |
| 173 | } | ||
| 174 | |||
| 175 | 13 | bool containsCompoundUnit(const std::string& s) { | |
| 176 | 13 | return boost::regex_search(s, regexEmbeddedCompoundUnit()); | |
| 177 | } | ||
| 178 | |||
| 179 | 247404 | const boost::regex& regexScaledUnit() { | |
| 180 |
1/1✓ Branch 1 taken 247404 times.
|
247404 | std::stringstream regexComposer; |
| 181 | // "\\?[a-zA-Z\$]{1,5}" << regexCompoundUnit << "\)" in standard flavor | ||
| 182 |
5/5✓ Branch 1 taken 247404 times.
✓ Branch 4 taken 247404 times.
✓ Branch 7 taken 247404 times.
✓ Branch 10 taken 247404 times.
✓ Branch 13 taken 247404 times.
|
247404 | regexComposer << "\\\\?[\\l\\u]{1,5}\\(" << regexCompoundUnit().str() << "\\)"; |
| 183 |
5/8✓ Branch 0 taken 110 times.
✓ Branch 1 taken 247294 times.
✓ Branch 3 taken 110 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 110 times.
✓ Branch 9 taken 110 times.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
|
247404 | static boost::regex rgx(regexComposer.str()); |
| 184 | 247404 | return rgx; | |
| 185 | 247404 | } | |
| 186 | |||
| 187 | 10 | const boost::regex& regexEmbeddedScaledUnit() { | |
| 188 |
1/1✓ Branch 1 taken 10 times.
|
10 | std::stringstream regexComposer; |
| 189 |
5/5✓ Branch 1 taken 10 times.
✓ Branch 4 taken 10 times.
✓ Branch 7 taken 10 times.
✓ Branch 10 taken 10 times.
✓ Branch 13 taken 10 times.
|
10 | regexComposer << "(?:^| )(" << regexScaledUnit().str() << ")(?:$| |\\.$|\\. |,|;)"; |
| 190 |
5/8✓ Branch 0 taken 2 times.
✓ Branch 1 taken 8 times.
✓ Branch 3 taken 2 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 2 times.
✓ Branch 9 taken 2 times.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
|
10 | static boost::regex rgx(regexComposer.str()); |
| 191 | 10 | return rgx; | |
| 192 | 10 | } | |
| 193 | |||
| 194 | 123561 | bool isScaledUnit(const std::string& s) { | |
| 195 | 123561 | return boost::regex_match(s, regexScaledUnit()); | |
| 196 | } | ||
| 197 | |||
| 198 | 10 | bool containsScaledUnit(const std::string& s) { | |
| 199 | 10 | return boost::regex_search(s, regexEmbeddedScaledUnit()); | |
| 200 | } | ||
| 201 | |||
| 202 | 43 | const boost::regex& regexDirectScaledUnit() { | |
| 203 |
1/1✓ Branch 1 taken 43 times.
|
43 | std::stringstream regexComposer; |
| 204 |
1/1✓ Branch 1 taken 43 times.
|
43 | std::stringstream tempComposer; |
| 205 | // place atomicUnit*atomicUnit*... in tempComposer | ||
| 206 |
8/8✓ Branch 1 taken 43 times.
✓ Branch 4 taken 43 times.
✓ Branch 7 taken 43 times.
✓ Branch 10 taken 43 times.
✓ Branch 13 taken 43 times.
✓ Branch 16 taken 43 times.
✓ Branch 19 taken 43 times.
✓ Branch 22 taken 43 times.
|
43 | tempComposer << regexAtomicUnit().str() << "(?:\\*" << regexAtomicUnit().str() << ")*"; |
| 207 |
8/8✓ Branch 1 taken 43 times.
✓ Branch 4 taken 43 times.
✓ Branch 7 taken 43 times.
✓ Branch 10 taken 43 times.
✓ Branch 13 taken 43 times.
✓ Branch 16 taken 43 times.
✓ Branch 19 taken 43 times.
✓ Branch 22 taken 43 times.
|
43 | regexComposer << "((?:" << tempComposer.str() << "|1)/)" << " ?(10+)(?:(?: *|\\*)(" << tempComposer.str() << "))?"; |
| 208 |
5/8✓ Branch 0 taken 3 times.
✓ Branch 1 taken 40 times.
✓ Branch 3 taken 3 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 3 times.
✓ Branch 9 taken 3 times.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
|
43 | static boost::regex rgx(regexComposer.str()); |
| 209 | 43 | return rgx; | |
| 210 | 43 | } | |
| 211 | |||
| 212 | 8 | const boost::regex& regexEmbeddedDirectScaledUnit() { | |
| 213 |
1/1✓ Branch 1 taken 8 times.
|
8 | std::stringstream regexComposer; |
| 214 |
2/2✓ Branch 1 taken 8 times.
✓ Branch 4 taken 8 times.
|
24 | regexComposer << "(?:(?:^| )(" << regexDirectScaledUnit().str() << ")(?:$| |\\.$|\\. |,|;)|" << "\\((" << regexDirectScaledUnit().str() << ")\\)|" |
| 215 |
18/18✓ Branch 1 taken 8 times.
✓ Branch 4 taken 8 times.
✓ Branch 7 taken 8 times.
✓ Branch 10 taken 8 times.
✓ Branch 13 taken 8 times.
✓ Branch 16 taken 8 times.
✓ Branch 19 taken 8 times.
✓ Branch 22 taken 8 times.
✓ Branch 25 taken 8 times.
✓ Branch 28 taken 8 times.
✓ Branch 31 taken 8 times.
✓ Branch 34 taken 8 times.
✓ Branch 37 taken 8 times.
✓ Branch 40 taken 8 times.
✓ Branch 43 taken 8 times.
✓ Branch 46 taken 8 times.
✓ Branch 49 taken 8 times.
✓ Branch 52 taken 8 times.
|
24 | << "\\{(" << regexDirectScaledUnit().str() << ")\\}|" << "\\[(" << regexDirectScaledUnit().str() << ")\\])"; |
| 216 |
5/8✓ Branch 0 taken 3 times.
✓ Branch 1 taken 5 times.
✓ Branch 3 taken 3 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 3 times.
✓ Branch 9 taken 3 times.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
|
8 | static boost::regex rgx(regexComposer.str()); |
| 217 | 8 | return rgx; | |
| 218 | 8 | } | |
| 219 | |||
| 220 | 3 | std::pair<std::string, std::pair<unsigned, std::string>> decomposeDirectScaledUnit(const std::string& s) { | |
| 221 |
2/3✓ Branch 1 taken 3 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
|
3 | if (!isDirectScaledUnit(s)) { |
| 222 | ✗ | LOG_FREE_AND_THROW("openstudio.QuantityRegex", | |
| 223 | "Cannot decompose " << s << " into a numerator and scaled denominator because it is not an direct scaled unit."); | ||
| 224 | } | ||
| 225 |
1/1✓ Branch 1 taken 3 times.
|
3 | std::pair<std::string, std::pair<unsigned, std::string>> result; |
| 226 |
1/1✓ Branch 1 taken 3 times.
|
3 | boost::match_results<std::string::const_iterator> match; |
| 227 | |||
| 228 |
2/2✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
|
3 | boost::regex_search(s, match, regexDirectScaledUnit()); |
| 229 |
3/3✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
✓ Branch 7 taken 3 times.
|
6 | result.first = std::string(match[1].first, match[1].second); |
| 230 |
3/3✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
✓ Branch 7 taken 3 times.
|
3 | std::string powerOfTen(match[2].first, match[2].second); |
| 231 | 3 | result.second.first = powerOfTen.size() - 1; | |
| 232 |
3/3✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
✓ Branch 7 taken 3 times.
|
3 | result.second.second = std::string(match[3].first, match[3].second); |
| 233 | |||
| 234 | 6 | return result; | |
| 235 | 3 | } | |
| 236 | |||
| 237 | 8 | bool isDirectScaledUnit(const std::string& s) { | |
| 238 | 8 | return boost::regex_match(s, regexDirectScaledUnit()); | |
| 239 | } | ||
| 240 | |||
| 241 | 4 | bool containsDirectScaledUnit(const std::string& s) { | |
| 242 | 4 | return boost::regex_search(s, regexEmbeddedDirectScaledUnit()); | |
| 243 | } | ||
| 244 | |||
| 245 | 123833 | const boost::regex& regexUnit() { | |
| 246 |
1/1✓ Branch 1 taken 123833 times.
|
123833 | std::stringstream regexComposer; |
| 247 |
9/9✓ Branch 1 taken 123833 times.
✓ Branch 4 taken 123833 times.
✓ Branch 7 taken 123833 times.
✓ Branch 10 taken 123833 times.
✓ Branch 13 taken 123833 times.
✓ Branch 16 taken 123833 times.
✓ Branch 19 taken 123833 times.
✓ Branch 22 taken 123833 times.
✓ Branch 25 taken 123833 times.
|
123833 | regexComposer << "(" << regexCompoundUnit().str() << "|" << regexScaledUnit().str() << ")"; |
| 248 |
5/8✓ Branch 0 taken 109 times.
✓ Branch 1 taken 123724 times.
✓ Branch 3 taken 109 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 109 times.
✓ Branch 9 taken 109 times.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
|
123833 | static boost::regex rgx(regexComposer.str()); |
| 249 | 123833 | return rgx; | |
| 250 | 123833 | } | |
| 251 | |||
| 252 | 14 | const boost::regex& regexEmbeddedUnit() { | |
| 253 |
1/1✓ Branch 1 taken 14 times.
|
14 | std::stringstream regexComposer; |
| 254 |
3/3✓ Branch 1 taken 14 times.
✓ Branch 4 taken 14 times.
✓ Branch 7 taken 14 times.
|
56 | regexComposer << "(?:(?:^| )" << regexUnit().str() << "(?:$| |\\.$|\\. |,|;)|" << "\\(" << regexUnit().str() << "\\)|" << "\\{" << regexUnit().str() |
| 255 |
17/17✓ Branch 1 taken 14 times.
✓ Branch 4 taken 14 times.
✓ Branch 7 taken 14 times.
✓ Branch 10 taken 14 times.
✓ Branch 13 taken 14 times.
✓ Branch 16 taken 14 times.
✓ Branch 19 taken 14 times.
✓ Branch 22 taken 14 times.
✓ Branch 25 taken 14 times.
✓ Branch 28 taken 14 times.
✓ Branch 31 taken 14 times.
✓ Branch 34 taken 14 times.
✓ Branch 37 taken 14 times.
✓ Branch 40 taken 14 times.
✓ Branch 43 taken 14 times.
✓ Branch 46 taken 14 times.
✓ Branch 49 taken 14 times.
|
56 | << "\\}|" << "\\[" << regexUnit().str() << "\\])"; |
| 256 |
5/8✓ Branch 0 taken 2 times.
✓ Branch 1 taken 12 times.
✓ Branch 3 taken 2 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 2 times.
✓ Branch 9 taken 2 times.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
|
14 | static boost::regex rgx(regexComposer.str()); |
| 257 | 14 | return rgx; | |
| 258 | 14 | } | |
| 259 | |||
| 260 | 123740 | bool isUnit(const std::string& s) { | |
| 261 | 123740 | return boost::regex_match(s, regexUnit()); | |
| 262 | } | ||
| 263 | |||
| 264 | 13 | bool containsUnit(const std::string& s) { | |
| 265 | 13 | return boost::regex_search(s, regexEmbeddedUnit()); | |
| 266 | } | ||
| 267 | |||
| 268 | 37 | const boost::regex& regexQuantity() { | |
| 269 |
1/1✓ Branch 1 taken 37 times.
|
37 | std::stringstream regexComposer; |
| 270 |
12/12✓ Branch 1 taken 37 times.
✓ Branch 4 taken 37 times.
✓ Branch 7 taken 37 times.
✓ Branch 10 taken 37 times.
✓ Branch 13 taken 37 times.
✓ Branch 16 taken 37 times.
✓ Branch 19 taken 37 times.
✓ Branch 22 taken 37 times.
✓ Branch 25 taken 37 times.
✓ Branch 28 taken 37 times.
✓ Branch 31 taken 37 times.
✓ Branch 34 taken 37 times.
|
37 | regexComposer << "(" << regexFixedPrecisionValue().str() << "|" << regexScientificNotationValue().str() << ")( |/)" << regexUnit().str(); |
| 271 |
5/8✓ Branch 0 taken 2 times.
✓ Branch 1 taken 35 times.
✓ Branch 3 taken 2 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 2 times.
✓ Branch 9 taken 2 times.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
|
37 | static boost::regex rgx(regexComposer.str()); |
| 272 | 37 | return rgx; | |
| 273 | 37 | } | |
| 274 | |||
| 275 | 11 | const boost::regex& regexEmbeddedQuantity() { | |
| 276 |
1/1✓ Branch 1 taken 11 times.
|
11 | std::stringstream regexComposer; |
| 277 |
5/5✓ Branch 1 taken 11 times.
✓ Branch 4 taken 11 times.
✓ Branch 7 taken 11 times.
✓ Branch 10 taken 11 times.
✓ Branch 13 taken 11 times.
|
11 | regexComposer << "(?:^| )(" << regexQuantity().str() << ")(?:$| |\\.$|\\. |,|;)"; |
| 278 |
5/8✓ Branch 0 taken 1 time.
✓ Branch 1 taken 10 times.
✓ Branch 3 taken 1 time.
✗ Branch 4 not taken.
✓ Branch 6 taken 1 time.
✓ Branch 9 taken 1 time.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
|
11 | static boost::regex rgx(regexComposer.str()); |
| 279 | 11 | return rgx; | |
| 280 | 11 | } | |
| 281 | |||
| 282 | 14 | bool isQuantity(const std::string& s) { | |
| 283 | 14 | return boost::regex_match(s, regexQuantity()); | |
| 284 | } | ||
| 285 | |||
| 286 | 11 | bool containsQuantity(const std::string& s) { | |
| 287 | 11 | return boost::regex_search(s, regexEmbeddedQuantity()); | |
| 288 | } | ||
| 289 | |||
| 290 | 12 | std::pair<std::string, std::string> decomposeQuantityString(const std::string& s) { | |
| 291 | |||
| 292 | 12 | std::pair<std::string, std::string> result; | |
| 293 |
1/1✓ Branch 1 taken 12 times.
|
12 | boost::match_results<std::string::const_iterator> match; |
| 294 |
4/4✓ Branch 1 taken 12 times.
✓ Branch 4 taken 12 times.
✓ Branch 6 taken 6 times.
✓ Branch 7 taken 6 times.
|
12 | if (!boost::regex_match(s, match, regexQuantity())) { |
| 295 |
17/17✓ Branch 1 taken 6 times.
✓ Branch 4 taken 6 times.
✓ Branch 7 taken 6 times.
✓ Branch 10 taken 6 times.
✓ Branch 13 taken 6 times.
✓ Branch 16 taken 6 times.
✓ Branch 19 taken 6 times.
✓ Branch 25 taken 6 times.
✓ Branch 28 taken 6 times.
✓ Branch 31 taken 6 times.
✓ Branch 34 taken 6 times.
✓ Branch 37 taken 6 times.
✓ Branch 40 taken 6 times.
✓ Branch 43 taken 6 times.
✓ Branch 46 taken 6 times.
✓ Branch 50 taken 6 times.
✓ Branch 53 taken 6 times.
|
18 | LOG_FREE_AND_THROW("openstudio.QuantityRegex", "Cannot decompose " << s << " into (value,unit) because it is not a quantity."); |
| 296 | } | ||
| 297 | // pull out number | ||
| 298 |
3/3✓ Branch 1 taken 6 times.
✓ Branch 4 taken 6 times.
✓ Branch 7 taken 6 times.
|
12 | result.first = std::string(match[1].first, match[1].second); |
| 299 | // see if there is / between number and unit | ||
| 300 |
3/3✓ Branch 1 taken 6 times.
✓ Branch 4 taken 6 times.
✓ Branch 7 taken 6 times.
|
6 | std::string temp(match[2].first, match[2].second); |
| 301 |
2/3✓ Branch 1 taken 6 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 6 times.
|
6 | if (temp == "/") { |
| 302 | ✗ | result.second = "1/"; | |
| 303 | } | ||
| 304 | // pull out unit | ||
| 305 |
4/4✓ Branch 1 taken 6 times.
✓ Branch 4 taken 6 times.
✓ Branch 7 taken 6 times.
✓ Branch 10 taken 6 times.
|
6 | result.second += std::string(match[3].first, match[3].second); |
| 306 | |||
| 307 | 12 | return result; | |
| 308 | 18 | } | |
| 309 | |||
| 310 | 9 | std::pair<std::string, std::string> decomposeScaledUnitString(const std::string& s) { | |
| 311 |
3/3✓ Branch 1 taken 9 times.
✓ Branch 3 taken 4 times.
✓ Branch 4 taken 5 times.
|
9 | if (!isScaledUnit(s)) { |
| 312 |
17/17✓ Branch 1 taken 4 times.
✓ Branch 4 taken 4 times.
✓ Branch 7 taken 4 times.
✓ Branch 10 taken 4 times.
✓ Branch 13 taken 4 times.
✓ Branch 16 taken 4 times.
✓ Branch 19 taken 4 times.
✓ Branch 25 taken 4 times.
✓ Branch 28 taken 4 times.
✓ Branch 31 taken 4 times.
✓ Branch 34 taken 4 times.
✓ Branch 37 taken 4 times.
✓ Branch 40 taken 4 times.
✓ Branch 43 taken 4 times.
✓ Branch 46 taken 4 times.
✓ Branch 50 taken 4 times.
✓ Branch 53 taken 4 times.
|
12 | LOG_FREE_AND_THROW("openstudio.QuantityRegex", "Cannot decompose " << s << " into a scale and a compound unit because it is not a scaled unit."); |
| 313 | } | ||
| 314 | |||
| 315 | 5 | std::pair<std::string, std::string> result; | |
| 316 |
1/1✓ Branch 1 taken 5 times.
|
5 | boost::match_results<std::string::const_iterator> match; |
| 317 |
1/1✓ Branch 1 taken 5 times.
|
5 | boost::regex scaleRegex("(\\\\?[\\l\\u]{1,5})\\("); |
| 318 | // pull out scale | ||
| 319 |
2/3✓ Branch 1 taken 5 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 5 times.
|
5 | if (!boost::regex_search(s, match, scaleRegex, boost::match_continuous)) { |
| 320 | ✗ | LOG_FREE_AND_THROW("openstudio.QuantityRegex", "Could not extract a scale from the scaled unit " << s << "."); | |
| 321 | } | ||
| 322 |
3/3✓ Branch 1 taken 5 times.
✓ Branch 4 taken 5 times.
✓ Branch 7 taken 5 times.
|
5 | result.first = std::string(match[1].first, match[1].second); |
| 323 | |||
| 324 | // pull out compound unit | ||
| 325 |
3/4✓ Branch 1 taken 5 times.
✓ Branch 4 taken 5 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 5 times.
|
5 | if (!boost::regex_search(s, match, regexEmbeddedCompoundUnit())) { |
| 326 | ✗ | LOG_FREE_AND_THROW("openstudio.QuantityRegex", "Could not extract a compound unit from " << s << "."); | |
| 327 | } | ||
| 328 | 5 | int i = 1; | |
| 329 |
2/3✓ Branch 1 taken 5 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 5 times.
|
5 | while (!match[i].matched) { |
| 330 | ✗ | ++i; | |
| 331 | } | ||
| 332 |
3/3✓ Branch 1 taken 5 times.
✓ Branch 4 taken 5 times.
✓ Branch 7 taken 5 times.
|
5 | result.second = std::string(match[1].first, match[1].second); |
| 333 | |||
| 334 | 10 | return result; | |
| 335 | 5 | } | |
| 336 | |||
| 337 | 123550 | std::pair<std::vector<std::string>, std::vector<std::string>> decomposeCompoundUnitString(const std::string& s) { | |
| 338 | |||
| 339 |
3/3✓ Branch 1 taken 123550 times.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 123548 times.
|
123550 | if (!isCompoundUnit(s)) { |
| 340 |
17/17✓ Branch 1 taken 2 times.
✓ Branch 4 taken 2 times.
✓ Branch 7 taken 2 times.
✓ Branch 10 taken 2 times.
✓ Branch 13 taken 2 times.
✓ Branch 16 taken 2 times.
✓ Branch 19 taken 2 times.
✓ Branch 25 taken 2 times.
✓ Branch 28 taken 2 times.
✓ Branch 31 taken 2 times.
✓ Branch 34 taken 2 times.
✓ Branch 37 taken 2 times.
✓ Branch 40 taken 2 times.
✓ Branch 43 taken 2 times.
✓ Branch 46 taken 2 times.
✓ Branch 50 taken 2 times.
✓ Branch 53 taken 2 times.
|
6 | LOG_FREE_AND_THROW("openstudio.QuantityRegex", |
| 341 | "Cannot decompose " << s << " into AtomicUnits in the numerator and denominator because it is not a compound unit."); | ||
| 342 | } | ||
| 343 | 123548 | std::pair<std::vector<std::string>, std::vector<std::string>> result; | |
| 344 |
1/1✓ Branch 1 taken 123548 times.
|
123548 | boost::match_results<std::string::const_iterator> match; |
| 345 | |||
| 346 |
1/1✓ Branch 1 taken 123548 times.
|
123548 | std::string ws(s); |
| 347 | |||
| 348 | // remove leading 1, if applicable | ||
| 349 | 123548 | std::string::iterator firstCharEnd = ws.begin(); | |
| 350 | 123548 | ++firstCharEnd; | |
| 351 | 123548 | std::string::const_iterator tempStrConstIter; // for gcc | |
| 352 |
1/1✓ Branch 2 taken 123548 times.
|
123548 | std::string firstChar(ws.begin(), firstCharEnd); |
| 353 |
3/3✓ Branch 1 taken 123548 times.
✓ Branch 3 taken 209 times.
✓ Branch 4 taken 123339 times.
|
123548 | if (firstChar == "1") { |
| 354 |
1/1✓ Branch 2 taken 209 times.
|
209 | ws = std::string(firstCharEnd, ws.end()); |
| 355 | 209 | firstCharEnd = ws.begin(); | |
| 356 | 209 | ++firstCharEnd; | |
| 357 |
1/1✓ Branch 2 taken 209 times.
|
418 | firstChar = std::string(ws.begin(), firstCharEnd); |
| 358 | } | ||
| 359 |
7/7✓ Branch 1 taken 144779 times.
✓ Branch 2 taken 112029 times.
✓ Branch 4 taken 144779 times.
✓ Branch 6 taken 133260 times.
✓ Branch 7 taken 11519 times.
✓ Branch 8 taken 133260 times.
✓ Branch 9 taken 123548 times.
|
380356 | while (!ws.empty() && (firstChar != "/")) { |
| 360 | // populate numerator | ||
| 361 |
2/2✓ Branch 1 taken 133260 times.
✓ Branch 4 taken 133260 times.
|
133260 | boost::regex_search(ws, match, regexAtomicUnit()); |
| 362 |
4/4✓ Branch 1 taken 133260 times.
✓ Branch 4 taken 133260 times.
✓ Branch 7 taken 133260 times.
✓ Branch 10 taken 133260 times.
|
266520 | result.first.push_back(std::string(match[0].first, match[0].second)); |
| 363 | 133260 | tempStrConstIter = ws.end(); // for gcc | |
| 364 | // cppcheck-suppress invalidContainer | ||
| 365 |
2/2✓ Branch 1 taken 133260 times.
✓ Branch 4 taken 133260 times.
|
133260 | ws = std::string(match[0].second, tempStrConstIter); |
| 366 |
2/2✓ Branch 1 taken 21231 times.
✓ Branch 2 taken 112029 times.
|
133260 | if (!ws.empty()) { |
| 367 | 21231 | firstCharEnd = ws.begin(); | |
| 368 | 21231 | ++firstCharEnd; | |
| 369 |
1/1✓ Branch 2 taken 21231 times.
|
42462 | firstChar = std::string(ws.begin(), firstCharEnd); |
| 370 | } | ||
| 371 | } | ||
| 372 |
3/3✓ Branch 1 taken 123548 times.
✓ Branch 3 taken 11519 times.
✓ Branch 4 taken 112029 times.
|
123548 | if (firstChar == "/") { |
| 373 | // cppcheck-suppress invalidContainer | ||
| 374 |
1/1✓ Branch 2 taken 11519 times.
|
11519 | ws = std::string(firstCharEnd, ws.end()); |
| 375 |
2/2✓ Branch 1 taken 12123 times.
✓ Branch 2 taken 11519 times.
|
23642 | while (!ws.empty()) { |
| 376 | // populate denominator | ||
| 377 |
2/2✓ Branch 1 taken 12123 times.
✓ Branch 4 taken 12123 times.
|
12123 | boost::regex_search(ws, match, regexAtomicUnit()); |
| 378 |
4/4✓ Branch 1 taken 12123 times.
✓ Branch 4 taken 12123 times.
✓ Branch 7 taken 12123 times.
✓ Branch 10 taken 12123 times.
|
24246 | result.second.push_back(std::string(match[0].first, match[0].second)); |
| 379 | 12123 | tempStrConstIter = ws.end(); // for gcc | |
| 380 | // cppcheck-suppress invalidContainer | ||
| 381 |
2/2✓ Branch 1 taken 12123 times.
✓ Branch 4 taken 12123 times.
|
24246 | ws = std::string(match[0].second, tempStrConstIter); |
| 382 | } | ||
| 383 | } | ||
| 384 | |||
| 385 | 247096 | return result; | |
| 386 | 123548 | } | |
| 387 | |||
| 388 | 145379 | std::pair<std::string, int> decomposeAtomicUnitString(const std::string& s) { | |
| 389 | |||
| 390 |
3/3✓ Branch 1 taken 145379 times.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 145377 times.
|
145379 | if (!isAtomicUnit(s)) { |
| 391 |
17/17✓ Branch 1 taken 2 times.
✓ Branch 4 taken 2 times.
✓ Branch 7 taken 2 times.
✓ Branch 10 taken 2 times.
✓ Branch 13 taken 2 times.
✓ Branch 16 taken 2 times.
✓ Branch 19 taken 2 times.
✓ Branch 25 taken 2 times.
✓ Branch 28 taken 2 times.
✓ Branch 31 taken 2 times.
✓ Branch 34 taken 2 times.
✓ Branch 37 taken 2 times.
✓ Branch 40 taken 2 times.
✓ Branch 43 taken 2 times.
✓ Branch 46 taken 2 times.
✓ Branch 50 taken 2 times.
✓ Branch 53 taken 2 times.
|
6 | LOG_FREE_AND_THROW("openstudio.QuantityRegex", "Cannot decompose " << s << " into a base unit and exponent because it is not an atomic unit."); |
| 392 | } | ||
| 393 | |||
| 394 | 145377 | std::pair<std::string, int> result; | |
| 395 |
1/1✓ Branch 1 taken 145377 times.
|
145377 | boost::smatch match; |
| 396 | |||
| 397 |
2/2✓ Branch 1 taken 145377 times.
✓ Branch 4 taken 145377 times.
|
145377 | boost::regex_search(s, match, regexBaseUnit()); |
| 398 | |||
| 399 | // First is the baseUnit (match[0].first is the start of sequence that matched, match[0].second is the end of sequence) | ||
| 400 |
3/3✓ Branch 1 taken 145377 times.
✓ Branch 4 taken 145377 times.
✓ Branch 7 taken 145377 times.
|
145377 | result.first = std::string(match[0].first, match[0].second); |
| 401 | // Match an exponent and put that in result.second | ||
| 402 |
4/4✓ Branch 1 taken 145377 times.
✓ Branch 4 taken 145377 times.
✓ Branch 6 taken 19989 times.
✓ Branch 7 taken 125388 times.
|
145377 | if (boost::regex_search(s, match, regexExponent())) { |
| 403 | // We want the submatch (capturing group) | ||
| 404 |
3/3✓ Branch 1 taken 19989 times.
✓ Branch 4 taken 19989 times.
✓ Branch 7 taken 19989 times.
|
19989 | std::istringstream iss(match[1]); |
| 405 |
1/1✓ Branch 1 taken 19989 times.
|
19989 | iss >> result.second; |
| 406 | 19989 | } else { | |
| 407 | 125388 | result.second = 1; | |
| 408 | } | ||
| 409 | |||
| 410 | 290754 | return result; | |
| 411 | 145377 | } | |
| 412 | |||
| 413 | } // namespace openstudio | ||
| 414 |