| 129 | } |
| 130 | |
| 131 | std::string Unit_Impl::standardString(bool withScale) const { |
| 132 | if (withScale && (scale().abbr.empty())) { |
| 133 | withScale = false; |
| 134 | } |
| 135 | |
| 136 | std::stringstream result; |
| 137 | bool parentheses(false); |
| 138 | |
| 139 | // determine number of non-zero, positive, and negative baseUnits |
| 140 | int nnz(0); |
| 141 | int npos(0); |
| 142 | std::vector<UnitElement>::const_iterator unitsIter; |
| 143 | std::vector<UnitElement>::const_iterator firstPosIter; |
| 144 | std::vector<UnitElement>::const_iterator firstNegIter; |
| 145 | auto unitsEnd = m_units.end(); |
| 146 | |
| 147 | firstPosIter = unitsEnd; |
| 148 | firstNegIter = unitsEnd; |
| 149 | |
| 150 | for (unitsIter = m_units.begin(); unitsIter != unitsEnd; ++unitsIter) { |
| 151 | if (unitsIter->second > 0) { |
| 152 | ++npos; |
| 153 | ++nnz; |
| 154 | if (firstPosIter == unitsEnd) { |
| 155 | firstPosIter = unitsIter; |
| 156 | } |
| 157 | } |
| 158 | if (unitsIter->second < 0) { |
| 159 | ++nnz; |
| 160 | if (firstNegIter == unitsEnd) { |
| 161 | firstNegIter = unitsIter; |
| 162 | } |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | if (withScale) { |
| 167 | if (nnz == 0) { |
| 168 | parentheses = true; |
| 169 | result << scale().abbr << "("; |
| 170 | } else { |
| 171 | ScaleConstant printScale = m_scale; |
| 172 | if (npos > 0) { |
| 173 | if (firstPosIter->second > 1) { |
| 174 | parentheses = true; |
| 175 | // see if can pull scale into exponent |
| 176 | if (scale().exponent % firstPosIter->second == 0) { |
| 177 | ScaleConstant candidateScale = ScaleFactory::instance().createScale(scale().exponent / firstPosIter->second); |
| 178 | if (candidateScale().value != 0.0) { |
| 179 | printScale = candidateScale; |
| 180 | parentheses = false; |
| 181 | } |
| 182 | } |
| 183 | } |
| 184 | result << printScale().abbr; |
| 185 | if (parentheses) { |
| 186 | result << "("; |
| 187 | } |
| 188 | } else { |