GCC Code Coverage Report


Directory: ./
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 66.4% 188 / 0 / 283
Functions: 67.6% 25 / 0 / 37
Branches: 54.6% 161 / 0 / 295

src/utilities/core/StringHelpers.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 "Assert.hpp"
7 #include "StringHelpers.hpp"
8 #include "../math/FloatCompare.hpp"
9
10 #include <boost/algorithm/string/replace.hpp>
11
12 #include <cmath>
13 #include <iomanip>
14
15 namespace openstudio {
16
17 12 std::string toCamelCase(const std::string& s) {
18
2/2
✓ Branch 1 taken 12 times.
✓ Branch 4 taken 12 times.
12 std::string result = boost::regex_replace(s, boost::regex("[^a-zA-Z0-9]"), " ");
19
2/2
✓ Branch 1 taken 12 times.
✓ Branch 4 taken 12 times.
12 result = boost::regex_replace(result, boost::regex(" ([a-z])"), "\\u$1");
20
2/2
✓ Branch 1 taken 12 times.
✓ Branch 4 taken 12 times.
12 result = boost::regex_replace(result, boost::regex(" "), "");
21 12 return result;
22 ✗ }
23
24 4 std::string toUpperCamelCase(const std::string& s) {
25 4 std::string result = toCamelCase(s);
26
2/2
✓ Branch 1 taken 4 times.
✓ Branch 4 taken 4 times.
4 result = boost::regex_replace(result, boost::regex("^([a-z])"), "\\u$1");
27 4 return result;
28 ✗ }
29
30 4 std::string toLowerCamelCase(const std::string& s) {
31 4 std::string result = toCamelCase(s);
32
2/2
✓ Branch 1 taken 4 times.
✓ Branch 4 taken 4 times.
4 result = boost::regex_replace(result, boost::regex("^([A-Z])"), "\\l$1");
33 4 return result;
34 ✗ }
35
36 105 std::string toUnderscoreCase(const std::string& s) {
37 // DLM: previous implementation
38 //std::string result = toLowerCamelCase(s);
39 //result = boost::regex_replace(result,boost::regex("(.)([A-Z])"),"$1_\\l$2");
40
41 105 std::string result = boost::replace_all_copy(s, "OpenStudio", "Openstudio");
42
1/1
✓ Branch 1 taken 105 times.
105 boost::replace_all(result, "EnergyPlus", "Energyplus");
43
44 //http://stackoverflow.com/questions/1509915/converting-camel-case-to-underscore-case-in-ruby
45 // DLM: there is a to_underscore method in the BCL gem, this should be synchronized
46
2/2
✓ Branch 1 taken 105 times.
✓ Branch 4 taken 105 times.
105 result = boost::regex_replace(result, boost::regex("[^a-zA-Z0-9]"), " ");
47
2/2
✓ Branch 1 taken 105 times.
✓ Branch 4 taken 105 times.
105 result = boost::regex_replace(result, boost::regex("([\\-]+)"), "_");
48
2/2
✓ Branch 1 taken 105 times.
✓ Branch 4 taken 105 times.
105 result = boost::regex_replace(result, boost::regex("([\\s]+)"), "_");
49
2/2
✓ Branch 1 taken 105 times.
✓ Branch 4 taken 105 times.
105 result = boost::regex_replace(result, boost::regex("([A-Za-z])([0-9])"), "$1_$2");
50
2/2
✓ Branch 1 taken 105 times.
✓ Branch 4 taken 105 times.
105 result = boost::regex_replace(result, boost::regex("([0-9]+)([A-Za-z])"), "$1_$2");
51
2/2
✓ Branch 1 taken 105 times.
✓ Branch 4 taken 105 times.
105 result = boost::regex_replace(result, boost::regex("([A-Z]+)([A-Z][a-z])"), "$1_$2");
52
2/2
✓ Branch 1 taken 105 times.
✓ Branch 4 taken 105 times.
105 result = boost::regex_replace(result, boost::regex("([a-z])([A-Z])"), "$1_$2");
53
2/2
✓ Branch 1 taken 105 times.
✓ Branch 4 taken 105 times.
105 result = boost::regex_replace(result, boost::regex("([A-Z])"), "\\l$1");
54 105 return result;
55 ✗ }
56
57 // ETH@20110614 Implementation copied from IddFileFactoryData.cpp for use in Ruby script.
58 ✗ std::string convertIddName(const std::string& s) {
59 ✗ std::string result(s);
60 ✗ boost::trim(result);
61 ✗ result = boost::regex_replace(result, boost::regex("^100 ?%"), "All");
62 ✗ result = boost::regex_replace(result, boost::regex("[ \\-]+"), "\\u");
63 ✗ result = boost::regex_replace(result, boost::regex("[:/,\\(\\)%]+"), "_");
64 ✗ result = boost::regex_replace(result, boost::regex("[\\.\\?\\]\\[]"), "");
65 ✗ result = boost::regex_replace(result, boost::regex("="), "_EQUAL_");
66 ✗ result = boost::regex_replace(result, boost::regex("\\*\\*"), "_POW_");
67 ✗ result = boost::regex_replace(result, boost::regex("\\+"), "_PLUS_");
68 ✗ result = boost::regex_replace(result, boost::regex("\\*"), "_TIMES_");
69 ✗ return result;
70 ✗ }
71
72 110455 std::string iddObjectNameToIdfObjectName(const std::string& s) {
73 110455 std::string result(s);
74
1/1
✓ Branch 2 taken 110455 times.
110455 boost::trim(result);
75
2/2
✓ Branch 1 taken 110455 times.
✓ Branch 4 taken 110455 times.
110455 result = boost::regex_replace(result, boost::regex("OS:"), "");
76
2/2
✓ Branch 1 taken 110455 times.
✓ Branch 4 taken 110455 times.
110455 result = boost::regex_replace(result, boost::regex(":"), " ");
77
2/2
✓ Branch 1 taken 110455 times.
✓ Branch 4 taken 110455 times.
110455 result = boost::regex_replace(result, boost::regex("([a-z])([A-Z])"), "$1 $2");
78
1/1
✓ Branch 2 taken 110455 times.
110455 boost::trim(result);
79 110455 return result;
80 ✗ }
81
82 7 std::string toNeatString(double value, unsigned numFractionalDigits, bool applyCommas) {
83
84 7 std::string result;
85
86
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
7 if (std::isinf(value)) {
87 ✗ if (value > 0) {
88 ✗ result = "+Inf";
89 } else {
90 ✗ result = "-Inf";
91 }
92
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
7 } else if (std::isnan(value)) {
93 ✗ result = "NaN";
94
95 } else {
96
97
1/1
✓ Branch 1 taken 7 times.
7 std::stringstream ss;
98
2/2
✓ Branch 1 taken 7 times.
✓ Branch 6 taken 7 times.
7 ss << std::fixed << std::setprecision(numFractionalDigits) << value;
99
1/1
✓ Branch 1 taken 7 times.
7 result = ss.str();
100
101
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 if (applyCommas) {
102
1/1
✓ Branch 1 taken 7 times.
7 boost::smatch m;
103
2/2
✓ Branch 1 taken 7 times.
✓ Branch 4 taken 7 times.
7 bool ok = boost::regex_match(result, m, boost::regex("(-?[0-9]{1,3})([0-9]{3})*(\\.[0-9]+|$)"), boost::match_extra);
104
1/3
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
✗ Branch 3 not taken.
7 OS_ASSERT(ok);
105
2/2
✓ Branch 1 taken 7 times.
✓ Branch 4 taken 7 times.
14 ss.str("");
106
4/4
✓ Branch 1 taken 7 times.
✓ Branch 4 taken 7 times.
✓ Branch 7 taken 7 times.
✓ Branch 10 taken 7 times.
7 ss << std::string(m[1].first, m[1].second);
107
4/4
✓ Branch 1 taken 7 times.
✓ Branch 4 taken 16 times.
✓ Branch 7 taken 9 times.
✓ Branch 8 taken 7 times.
16 for (std::string::const_iterator start = m[1].second; start != m[3].first;) {
108 9 std::string::const_iterator end = start;
109 9 ++(++(++end));
110
3/3
✓ Branch 1 taken 9 times.
✓ Branch 4 taken 9 times.
✓ Branch 7 taken 9 times.
18 ss << "," << std::string(start, end);
111 9 start = end;
112 }
113
4/4
✓ Branch 1 taken 7 times.
✓ Branch 4 taken 7 times.
✓ Branch 7 taken 7 times.
✓ Branch 10 taken 7 times.
7 ss << std::string(m[3].first, m[3].second);
114
1/1
✓ Branch 1 taken 7 times.
7 result = ss.str();
115 7 }
116 7 }
117 7 return result;
118 ✗ }
119
120 2 std::string toNeatStringBySigFigs(double value, unsigned numSigFigs, bool applyCommas) {
121 2 return toNeatString(toNumSigFigs(value, numSigFigs), numFractionalDigits(value, numSigFigs), applyCommas);
122 }
123
124 6 unsigned numFractionalDigits(const std::string& str) {
125 6 unsigned result(0);
126
1/1
✓ Branch 1 taken 6 times.
6 boost::smatch m;
127
4/4
✓ Branch 1 taken 6 times.
✓ Branch 4 taken 6 times.
✓ Branch 7 taken 3 times.
✓ Branch 8 taken 3 times.
6 if (boost::regex_search(str, m, boost::regex("\\.([0-9]+)"))) {
128
3/3
✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
✓ Branch 7 taken 3 times.
6 result = std::string(m[1].first, m[1].second).size();
129 }
130 6 return result;
131 6 }
132
133 34 unsigned numFractionalDigits(double value, unsigned numSigFigs) {
134
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 34 times.
34 if (numSigFigs == 0u) {
135 ✗ LOG_FREE_AND_THROW("openstudio.core.StringHelpers", "Number of significant figures must be > 0.");
136 }
137
138
2/2
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 32 times.
34 if (equal<double>(value, 0.0)) {
139 2 return numSigFigs - 1u;
140 }
141
142 32 value = std::fabs(value);
143 32 int orderOfMagnitude = int(std::floor(std::log10(value))); // 1683 => 3
144 // 0.001683892 => -3
145 32 int figsBeforeDecimal = std::min(std::max(orderOfMagnitude + 1, 0), int(numSigFigs));
146
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
32 OS_ASSERT(figsBeforeDecimal >= 0);
147
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
32 OS_ASSERT(figsBeforeDecimal <= int(numSigFigs));
148 32 int numZerosAfterDecimal = std::max(-1 - orderOfMagnitude, 0);
149
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
32 OS_ASSERT(numZerosAfterDecimal >= 0);
150 32 return numSigFigs - figsBeforeDecimal + numZerosAfterDecimal;
151 }
152
153 10 std::pair<unsigned, unsigned> numFractionalDigits(const std::vector<double>& values, unsigned numSigFigs) {
154
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 if (numSigFigs == 0u) {
155 ✗ LOG_FREE_AND_THROW("openstudio.core.StringHelpers", "Number of significant figures must be > 0.");
156 }
157
158 10 std::pair<unsigned, unsigned> result(0u, 0u);
159
2/2
✓ Branch 1 taken 32 times.
✓ Branch 2 taken 10 times.
42 for (unsigned i = 0, n = values.size(); i < n; ++i) {
160 32 double value = values[i];
161
1/1
✓ Branch 1 taken 32 times.
32 unsigned numDigits = numFractionalDigits(value, numSigFigs);
162
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 22 times.
32 if (i == 0) {
163 10 result = std::pair<unsigned, unsigned>(numDigits, numDigits);
164 } else {
165
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 19 times.
22 if (numDigits < result.first) {
166 3 result.first = numDigits;
167
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 14 times.
19 } else if (numDigits > result.second) {
168 5 result.second = numDigits;
169 }
170 }
171 }
172
173 10 return result;
174 }
175
176 6 double toNumSigFigs(double value, unsigned numSigFigs) {
177
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (numSigFigs == 0u) {
178 ✗ LOG_FREE_AND_THROW("openstudio.core.StringHelpers", "Number of significant figures must be > 0.");
179 }
180
181
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 6 times.
6 if (equal<double>(value, 0.0)) {
182 ✗ return value;
183 }
184
185 6 double absValue = std::fabs(value);
186 6 bool negative = (value != absValue);
187
188 6 double orderOfMagnitude = std::floor(std::log10(absValue)); // 1683 => 3
189 // 0.001683892 => -3
190 // X.XXXXX add more sig-figs
191 6 double positioningPowerOfTen = -orderOfMagnitude + double(int(numSigFigs) - 1);
192 // 1683, 2 sig-figs => 1683 * 10**-2 => 16.83
193 // 0.001683892, 2 sig-figs => 0.001683892 * 10**4 => 16.83892
194
195 6 double temp = absValue * std::pow(10.0, positioningPowerOfTen);
196 6 temp = std::floor(temp + 0.5); // round doesn't exist in VS2008 at least
197 6 double result = temp * std::pow(10.0, -positioningPowerOfTen);
198
199
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
6 if (negative) {
200 3 return -result;
201 }
202 3 return result;
203 }
204
205 ✗ boost::regex superscript() {
206 //static boost::regex rgx("([\\d\\l\\u])(\\^(?:([\\d\\l\\u]+)|\\{(-?[\\d\\l\\u\\*\\+\\\\\\- ]+)\\}))");
207 //return rgx;
208 ✗ return boost::regex("([\\d\\l\\u])(\\^(?:([\\d\\l\\u]+)|\\{(-?[\\d\\l\\u\\*\\+\\\\\\- ]+)\\}))");
209 }
210
211 ✗ boost::regex subscript() {
212 //static boost::regex rgx("([\\d\\l\\u])(_(?:([\\d\\l\\u]+)|\\{(-?[\\d\\l\\u\\*\\+\\\\\\- ]+)\\}))");
213 //return rgx;
214 ✗ return boost::regex("([\\d\\l\\u])(_(?:([\\d\\l\\u]+)|\\{(-?[\\d\\l\\u\\*\\+\\\\\\- ]+)\\}))");
215 }
216
217 ✗ boost::regex underscore() {
218 //static boost::regex rgx("(\\\\_)");
219 //return rgx;
220 ✗ return boost::regex("(\\\\_)");
221 }
222
223 ✗ std::string formatText(const std::string& str, DocumentFormat fmt) {
224 ✗ std::string wStr = formatSuperAndSubscripts(str, fmt);
225 ✗ if (fmt == DocumentFormat::LaTeX) {
226 ✗ wStr = boost::regex_replace(wStr, boost::regex("%"), "\\\\$&");
227 ✗ wStr = boost::regex_replace(wStr, boost::regex("&"), "\\\\$&");
228 }
229 ✗ if (fmt == DocumentFormat::XHTML) {
230 ✗ wStr = boost::regex_replace(wStr, boost::regex("$"), "<br/>");
231 }
232 ✗ return wStr;
233 ✗ }
234
235 ✗ std::string formatSuperAndSubscripts(const std::string& str, DocumentFormat fmt) {
236 ✗ if (fmt == DocumentFormat::LaTeX) {
237 ✗ std::string wStr(str);
238 ✗ wStr = boost::regex_replace(wStr, superscript(), "$1\\\\ensuremath{$2}");
239 ✗ wStr = boost::regex_replace(wStr, subscript(), "$1\\\\ensuremath{$2}");
240 ✗ return wStr;
241 ✗ }
242 ✗ if (fmt == DocumentFormat::XHTML) {
243 ✗ std::string wStr(str);
244 ✗ wStr = boost::regex_replace(wStr, superscript(), "$1<sup>$3$4</sup>");
245 ✗ wStr = boost::regex_replace(wStr, subscript(), "$1<sub>$3$4</sub>");
246 ✗ wStr = boost::regex_replace(wStr, underscore(), "_");
247 ✗ return wStr;
248 ✗ }
249 ✗ return str;
250 }
251
252 ✗ std::string formatUnitString(const std::string& str, DocumentFormat fmt) {
253 ✗ std::string result = formatSuperAndSubscripts(str, fmt);
254 ✗ boost::regex asterix("\\*");
255 ✗ if (fmt == DocumentFormat::LaTeX) {
256 ✗ result = boost::regex_replace(result, asterix, "\\\\ensuremath{\\\\cdot}");
257 }
258 ✗ if (fmt == DocumentFormat::XHTML) {
259 ✗ result = boost::regex_replace(result, asterix, "&middot;");
260 }
261 ✗ return result;
262 ✗ }
263
264 ✗ std::string formatUnderscore(const std::string& str) {
265 ✗ std::string wStr(str);
266 ✗ wStr = boost::regex_replace(wStr, boost::regex("_"), "\\\\_");
267 ✗ return wStr;
268 ✗ }
269
270 561841 std::vector<std::string> splitString(const std::string& string, char delimiter) {
271 561841 std::vector<std::string> results;
272
2/2
✓ Branch 1 taken 561840 times.
✓ Branch 2 taken 1 time.
561841 if (!string.empty()) { // Only do work if there is work to do
273
1/1
✓ Branch 2 taken 561840 times.
561840 std::stringstream stream(string);
274 561840 std::string substring;
275
4/4
✓ Branch 1 taken 20206244 times.
✓ Branch 4 taken 20206244 times.
✓ Branch 6 taken 19644404 times.
✓ Branch 7 taken 561840 times.
20206244 while (std::getline(stream, substring, delimiter)) { // Loop and fill the results vector
276
1/1
✓ Branch 1 taken 19644404 times.
19644404 results.push_back(substring);
277 }
278
2/2
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 561838 times.
561840 if (*(string.end() - 1) == delimiter) { // Add an empty string if the last char is the delimiter
279
1/1
✓ Branch 2 taken 2 times.
2 results.push_back(std::string());
280 }
281 561840 }
282 561841 return results;
283 ✗ }
284
285 30 std::vector<std::string> splitEMSLineToTokens(const std::string& line, const std::string& delimiters) {
286
287 // Split line to get 'tokens' and look for ModelObject names
288 // Note: JM 2018-08-16: Really this parser should match the E+ one, so probably split on operators, handle parenthesis, etc
289 // The idea is to split on ' +-*/^=<>&|' which are the operators
290 // We also need to ignore the reserved keywords and functions
291
292 std::vector<std::string> reservedKeyWords = {
293 // Constant built-in variables
294 "NULL",
295 "FALSE",
296 "TRUE",
297 "OFF",
298 "ON",
299 "PI",
300
301 // Dynamic built-in variables
302 "YEAR",
303 "MONTH",
304 "DAYOFMONTH",
305 "DAYOFWEEK",
306 "DAYOFYEAR",
307 "HOUR",
308 "MINUTE",
309 "HOLIDAY",
310 "DAYLIGHTSAVINGS",
311 "CURRENTTIME",
312 "SUNISUP",
313 "ISRAINING",
314 "SYSTEMTIMESTEP",
315 "ZONETIMESTEP",
316 "CURRENTENVIRONMENT",
317 "ACTUALDATEANDTIME",
318 "ACTUALTIME",
319 "WARMUPFLAG",
320
321 // Statement Keywords
322 "RUN",
323 "RETURN",
324 "SET",
325 "IF",
326 "ELSEIF",
327 "ELSE",
328 "ENDIF",
329 "WHILE",
330 "ENDWHILE",
331
1/1
✓ Branch 1 taken 30 times.
60 };
332
333 30 std::vector<std::string> tokens;
334
2/2
✓ Branch 1 taken 30 times.
✓ Branch 4 taken 30 times.
30 boost::split(tokens, line, boost::is_any_of(delimiters));
335
336
2/2
✓ Branch 3 taken 174 times.
✓ Branch 4 taken 30 times.
204 for (auto it = tokens.begin(); it != tokens.end();) {
337 // We trim eventual parenthesis
338
1/1
✓ Branch 2 taken 174 times.
174 boost::replace_all(*it, "(", "");
339
1/1
✓ Branch 2 taken 174 times.
174 boost::replace_all(*it, ")", "");
340 // We trim leading and trailing whitespaces (shouldn't be necesssary with boost::split)
341
1/1
✓ Branch 3 taken 174 times.
174 boost::trim(*it);
342
343 // We Delete the token:
344
345 // If there's nothing left in the token,
346 174 if ((it->empty())
347 // or it starts with "@" (function)
348
3/3
✓ Branch 2 taken 100 times.
✓ Branch 4 taken 99 times.
✓ Branch 5 taken 1 time.
100 || (boost::starts_with(*it, "@"))
349 // or its one of the reserved keyword above
350
12/16
✓ Branch 1 taken 100 times.
✓ Branch 2 taken 74 times.
✓ Branch 7 taken 99 times.
✓ Branch 12 taken 99 times.
✓ Branch 15 taken 31 times.
✓ Branch 16 taken 68 times.
✓ Branch 17 taken 99 times.
✓ Branch 18 taken 75 times.
✓ Branch 20 taken 99 times.
✓ Branch 21 taken 75 times.
✓ Branch 23 taken 106 times.
✓ Branch 24 taken 68 times.
✗ Branch 25 not taken.
✗ Branch 26 not taken.
✗ Branch 28 not taken.
✗ Branch 29 not taken.
448 || (std::find(reservedKeyWords.begin(), reservedKeyWords.end(), boost::to_upper_copy<std::string>(*it)) != reservedKeyWords.end())) {
351
1/1
✓ Branch 2 taken 106 times.
106 it = tokens.erase(it);
352 } else {
353 68 ++it;
354 }
355 }
356 60 return tokens;
357 30 }
358
359 280 std::string replace(std::string input, const std::string& before, const std::string& after) {
360 280 boost::replace_all(input, before, after);
361 280 return input;
362 }
363
364 template <typename Value>
365 120 std::string number_formatter(const Value value, const int base) {
366
2/4
std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > openstudio::number_formatter<int>(int, int):
✓ Branch 1 taken 105 times.
std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > openstudio::number_formatter<long>(long, int):
✗ Branch 1 not taken.
std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > openstudio::number_formatter<unsigned int>(unsigned int, int):
✓ Branch 1 taken 15 times.
std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > openstudio::number_formatter<unsigned long>(unsigned long, int):
✗ Branch 1 not taken.
120 std::stringstream ss;
367
4/8
std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > openstudio::number_formatter<int>(int, int):
✓ Branch 1 taken 105 times.
✓ Branch 4 taken 105 times.
std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > openstudio::number_formatter<long>(long, int):
✗ Branch 1 not taken.
✗ Branch 4 not taken.
std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > openstudio::number_formatter<unsigned int>(unsigned int, int):
✓ Branch 1 taken 15 times.
✓ Branch 4 taken 15 times.
std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > openstudio::number_formatter<unsigned long>(unsigned long, int):
✗ Branch 1 not taken.
✗ Branch 4 not taken.
120 ss.imbue(std::locale::classic());
368 120 ss << std::setbase(base);
369
2/4
std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > openstudio::number_formatter<int>(int, int):
✓ Branch 1 taken 105 times.
std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > openstudio::number_formatter<long>(long, int):
✗ Branch 1 not taken.
std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > openstudio::number_formatter<unsigned int>(unsigned int, int):
✓ Branch 1 taken 15 times.
std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > openstudio::number_formatter<unsigned long>(unsigned long, int):
✗ Branch 1 not taken.
120 ss << value;
370
2/4
std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > openstudio::number_formatter<int>(int, int):
✓ Branch 1 taken 105 times.
std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > openstudio::number_formatter<long>(long, int):
✗ Branch 1 not taken.
std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > openstudio::number_formatter<unsigned int>(unsigned int, int):
✓ Branch 1 taken 15 times.
std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > openstudio::number_formatter<unsigned long>(unsigned long, int):
✗ Branch 1 not taken.
240 return ss.str();
371 120 }
372
373 template <typename Value>
374 34067 std::string number_formatter(const Value value, const FloatFormat format, const int precision) {
375 // set up several lambdas so we delay calculating anything that
376 // doesn't need to be calculated
377
378 71988 const auto make_stream = [&]() {
379 37921 std::stringstream ss;
380
2/2
✓ Branch 1 taken 37921 times.
✓ Branch 4 taken 37921 times.
37921 ss.imbue(std::locale::classic());
381
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 37919 times.
37921 if (format == FloatFormat::general_capital) {
382
1/1
✓ Branch 1 taken 2 times.
2 ss << std::uppercase;
383 }
384
385 37921 ss << std::setprecision(precision);
386 37921 return ss;
387 ✗ };
388
389 37921 const auto make_defaultfloat = [&]() {
390
1/1
✓ Branch 1 taken 3854 times.
3854 auto ss = make_stream();
391
1/1
✓ Branch 1 taken 3854 times.
3854 ss << value;
392
1/1
✓ Branch 1 taken 3854 times.
7708 return ss.str();
393 3854 };
394
395 64280 const auto make_fixed = [&]() {
396
1/1
✓ Branch 1 taken 30213 times.
30213 auto ss = make_stream();
397
1/1
✓ Branch 1 taken 30213 times.
30213 ss << std::fixed;
398
1/1
✓ Branch 1 taken 30213 times.
30213 ss << value;
399
1/1
✓ Branch 1 taken 30213 times.
60426 return ss.str();
400 30213 };
401
402 37921 const auto make_scientific = [&]() {
403
1/1
✓ Branch 1 taken 3854 times.
3854 auto ss = make_stream();
404
1/1
✓ Branch 1 taken 3854 times.
3854 ss << std::scientific;
405
1/1
✓ Branch 1 taken 3854 times.
3854 ss << value;
406
1/1
✓ Branch 1 taken 3854 times.
7708 return ss.str();
407 3854 };
408
409 37921 const auto make_most_compact = [&]() {
410
1/1
✓ Branch 1 taken 3854 times.
3854 const auto df = make_defaultfloat();
411
1/1
✓ Branch 1 taken 3854 times.
3854 const auto s = make_scientific();
412
413
1/2
✓ Branch 2 taken 3854 times.
✗ Branch 3 not taken.
3854 if (df.size() < s.size()) {
414
1/1
✓ Branch 1 taken 3854 times.
3854 return df;
415 } else {
416 ✗ return s;
417 }
418 3854 };
419
420
2/3
✓ Branch 0 taken 30213 times.
✓ Branch 1 taken 3854 times.
✗ Branch 2 not taken.
34067 switch (format) {
421 30213 case FloatFormat::fixed:
422
1/1
✓ Branch 1 taken 30213 times.
30213 return make_fixed();
423 3854 case FloatFormat::general:
424 case FloatFormat::general_capital:
425
1/1
✓ Branch 1 taken 3854 times.
3854 return make_most_compact();
426 ✗ default:
427 ✗ OS_ASSERT(false); // unhandled format
428 ✗ return make_defaultfloat();
429 }
430 }
431
432 namespace string_conversions {
433 15 std::string number(const std::uint32_t value, const int base) {
434 15 return number_formatter(value, base);
435 }
436 105 std::string number(const std::int32_t value, const int base) {
437 105 return number_formatter(value, base);
438 }
439 ✗ std::string number(const std::uint64_t value, const int base) {
440 ✗ return number_formatter(value, base);
441 }
442 ✗ std::string number(const std::int64_t value, const int base) {
443 ✗ return number_formatter(value, base);
444 }
445
446 34067 std::string number(const double value, const FloatFormat format, const int precision) {
447 34067 return number_formatter(value, format, precision);
448 }
449
450 } // namespace string_conversions
451
452 } // namespace openstudio
453