MCPcopy Create free account
hub / github.com/NatLabRockies/OpenStudio / IddUnitString

Method IddUnitString

src/utilities/units/IddUnitString.cpp:14–114  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

12namespace openstudio {
13
14IddUnitString::IddUnitString(const std::string& s) : m_original(s), m_converted(s) {
15 // remove differentiation between kg-H2O and kg-Air (will be kg/kg).
16 // better to add kg-H2O and kg-air as separate SI base units?
17 m_converted = boost::regex_replace(m_converted, boost::regex("-H2O"), "");
18 m_converted = boost::regex_replace(m_converted, boost::regex("-[aA]ir"), "");
19 m_converted = boost::regex_replace(m_converted, boost::regex("Water"), "");
20 m_converted = boost::regex_replace(m_converted, boost::regex("DryAir"), "");
21
22 // basic replacements
23 m_converted = boost::regex_replace(m_converted, boost::regex("-"), "*");
24
25 // If H2O, convert to H_{2}O
26 m_converted = boost::regex_replace(m_converted, boost::regex("H2O"), "H_{2}O");
27
28 // Now, replace numbers by exponents, excluding the stuff that's preceding by curly brace `{` or a litteral `^`
29 // I use a negative lookbehind for that condition. Standard regex flavor: (?<![{\^])([2-9]+)
30 // The entire regex can be tested in regular flavor using this:
31 // "(?<![{\^])([2-9]+)"
32 // ft3 => ft^3
33 // ft^3 => no match
34 // inH_{2}O => no match
35 m_converted = boost::regex_replace(m_converted, boost::regex("(?<![\\^\\{])([2-9]+)"), "^\\1");
36
37 m_converted = boost::regex_replace(m_converted, boost::regex("deltaC"), "K");
38 m_converted = boost::regex_replace(m_converted, boost::regex("minutes"), "min");
39 m_converted = boost::regex_replace(m_converted, boost::regex("dimensionless"), "");
40 m_converted = boost::regex_replace(m_converted, boost::regex("Person"), "person");
41 m_converted = boost::regex_replace(m_converted, boost::regex("Rotations Per Minute"), "rpm");
42 m_converted = boost::regex_replace(m_converted, boost::regex("rev/min"), "rpm");
43 m_converted = boost::regex_replace(m_converted, boost::regex("ohms"), "ohm");
44 m_converted = boost::regex_replace(m_converted, boost::regex("VA"), "V*A");
45 m_converted = boost::regex_replace(m_converted, boost::regex("deltaJ"), "J");
46 m_converted = boost::regex_replace(m_converted, boost::regex("rev"), "cycle");
47 m_converted = boost::regex_replace(m_converted, boost::regex("Ah"), "A*h");
48
49 // relative temperatures
50 if (!boost::regex_match(m_converted, boost::regex("C|F"))) {
51 m_converted = boost::regex_replace(m_converted, boost::regex("C"), "K");
52 m_converted = boost::regex_replace(m_converted, boost::regex("F"), "R");
53 }
54
55 // Multiple /'s
56 boost::smatch matches;
57 if (boost::regex_match(m_converted, matches, boost::regex("(.*)/\\((.*)/(.*)\\)"))) {
58 std::string part1(matches[1].first, matches[1].second);
59 std::string part2(matches[2].first, matches[2].second);
60 std::string part3(matches[3].first, matches[3].second);
61 m_converted = part1 + "*" + part3 + "/" + part2;
62 }
63
64 if (boost::regex_match(m_converted, matches, boost::regex("\\((.*)/(.*)\\)/(.*)"))) {
65 std::string part1(matches[1].first, matches[1].second);
66 std::string part2(matches[2].first, matches[2].second);
67 std::string part3(matches[3].first, matches[3].second);
68 m_converted = part1 + "/" + part2 + "*" + part3;
69 }
70
71 if ((!m_converted.empty()) && boost::regex_search(m_converted, boost::regex("g"))) {

Callers

nothing calls this directly

Calls 6

parseUnitStringFunction · 0.85
standardStringMethod · 0.80
emptyMethod · 0.45
baseUnitExponentMethod · 0.45
setBaseUnitExponentMethod · 0.45
setScaleMethod · 0.45

Tested by

no test coverage detected