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

Function parseUnitString

src/utilities/units/Unit.cpp:573–619  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

571}
572
573Unit parseUnitString(const std::string& unitString) {
574
575 if (!isUnit(unitString)) {
576 LOG_FREE_AND_THROW("openstudio.Unit", unitString << " is not a UnitString, per openstudio::isUnit.");
577 }
578
579 std::string compoundUnitString(unitString);
580 int scaleExponent = 0;
581 if (isScaledUnit(unitString)) {
582 std::pair<std::string, std::string> scaledUnitParseResult = decomposeScaledUnitString(unitString);
583 ScaleConstant scale = ScaleFactory::instance().createScale(scaledUnitParseResult.first);
584 if (scale().value != 0.0) {
585 scaleExponent = scale().exponent;
586 }
587 compoundUnitString = scaledUnitParseResult.second;
588 }
589
590 Unit result(scaleExponent);
591
592 std::pair<std::vector<std::string>, std::vector<std::string>> compoundUnitParseResult = decomposeCompoundUnitString(compoundUnitString);
593
594 std::vector<std::string>::const_iterator i;
595 auto theEnd = compoundUnitParseResult.first.end();
596
597 // loop over numerator
598 for (i = compoundUnitParseResult.first.begin(); i != theEnd; ++i) {
599 std::pair<std::string, int> atom = decomposeAtomicUnitString(*i);
600 if (result.isBaseUnit(atom.first)) {
601 result.setBaseUnitExponent(atom.first, result.baseUnitExponent(atom.first) + atom.second);
602 } else {
603 result.setBaseUnitExponent(atom.first, atom.second);
604 }
605 }
606
607 // loop over denominator
608 theEnd = compoundUnitParseResult.second.end();
609 for (i = compoundUnitParseResult.second.begin(); i != theEnd; ++i) {
610 std::pair<std::string, int> atom = decomposeAtomicUnitString(*i);
611 if (result.isBaseUnit(atom.first)) {
612 result.setBaseUnitExponent(atom.first, result.baseUnitExponent(atom.first) - atom.second);
613 } else {
614 result.setBaseUnitExponent(atom.first, -atom.second);
615 }
616 }
617
618 return result;
619}
620
621bool operator!=(const Unit& lUnit, const Unit& rUnit) {
622 return !(lUnit == rUnit);

Callers 10

getSummaryDataMethod · 0.85
getUnitsMethod · 0.85
m_convertToSIMethod · 0.85
m_convertFromSIMethod · 0.85
registerUnitMethod · 0.85
prettyStringMethod · 0.85
operator*=Method · 0.85
powMethod · 0.85
IddUnitStringMethod · 0.85

Calls 11

isUnitFunction · 0.85
isScaledUnitFunction · 0.85
createScaleMethod · 0.80
beginMethod · 0.80
isBaseUnitMethod · 0.80
endMethod · 0.45
setBaseUnitExponentMethod · 0.45
baseUnitExponentMethod · 0.45

Tested by

no test coverage detected