| 571 | } |
| 572 | |
| 573 | Unit 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 | |
| 621 | bool operator!=(const Unit& lUnit, const Unit& rUnit) { |
| 622 | return !(lUnit == rUnit); |
no test coverage detected