}
// Load factor for a single source
-Factor* loadSingleFactor(StringPiece source, const ConversionRates &ratesInfo, UErrorCode &status) {
+Factor loadSingleFactor(StringPiece source, const ConversionRates &ratesInfo, UErrorCode &status) {
const auto conversionUnit = ratesInfo.extractConversionInfo(source, status);
- if (U_FAILURE(status)) return nullptr;
+ if (U_FAILURE(status)) return Factor();
if(conversionUnit == nullptr) {
status = U_INTERNAL_PROGRAM_ERROR;
- return nullptr;
+ return Factor();
}
Factor result = extractFactorConversions(conversionUnit->factor.toStringPiece(), status);
result.offset = strHasDivideSignToDouble(conversionUnit->offset.toStringPiece());
- return &result;
+ return result;
}
// Load Factor of a compound source unit.
for (int32_t i = 0, n = compoundSourceUnit.units.length(); i < n; i++) {
auto singleUnit = *compoundSourceUnit.units[i]; // a TempSingleUnit
- Factor *singleFactor = loadSingleFactor(singleUnit.identifier, ratesInfo, status);
+ Factor singleFactor = loadSingleFactor(singleUnit.identifier, ratesInfo, status);
if(U_FAILURE(status)) return result;
- if (singleFactor == nullptr) {
- status = U_INTERNAL_PROGRAM_ERROR;
- return result;
- }
// Apply SiPrefix before the power, because the power may be will flip the factor.
- singleFactor->applySiPrefix(singleUnit.siPrefix);
+ singleFactor.applySiPrefix(singleUnit.siPrefix);
// Apply the power of the `dimensionality`
- singleFactor->power(singleUnit.dimensionality);
+ singleFactor.power(singleUnit.dimensionality);
- result.multiplyBy(*singleFactor);
+ result.multiplyBy(singleFactor);
}
return result;
namespace {
+void trimSpaces(CharString& factor, UErrorCode& status){
+ CharString trimmed;
+ for (int i = 0 ; i < factor.length(); i++) {
+ if (factor[i] == ' ') continue;
+
+ trimmed.append(factor[i], status);
+ }
+
+ factor.clear();
+ factor.append(trimmed, status);
+}
+
/**
* A ResourceSink that collects conversion rate information.
*
cr->sourceUnit.append(srcUnit, status);
cr->baseUnit.appendInvariantChars(baseUnit, status);
cr->factor.appendInvariantChars(factor, status);
+ trimSpaces(cr->factor, status);
if (!offset.isBogus()) cr->offset.appendInvariantChars(offset, status);
}
}