X-Git-Url: https://git.saurik.com/apple/icu.git/blobdiff_plain/57a6839dcb3bba09e8228b822b290604668416fe..3d1f044b704633e2e541231cd17ae9ecf9ad5c7a:/icuSources/i18n/measfmt.cpp diff --git a/icuSources/i18n/measfmt.cpp b/icuSources/i18n/measfmt.cpp index 6ce2c0c8..1e29bf13 100644 --- a/icuSources/i18n/measfmt.cpp +++ b/icuSources/i18n/measfmt.cpp @@ -1,6 +1,8 @@ +// © 2016 and later: Unicode, Inc. and others. +// License & terms of use: http://www.unicode.org/copyright.html /* ********************************************************************** -* Copyright (c) 2004-2014, International Business Machines +* Copyright (c) 2004-2016, International Business Machines * Corporation and others. All Rights Reserved. ********************************************************************** * Author: Alan Liu @@ -17,12 +19,15 @@ #include "unicode/numfmt.h" #include "currfmt.h" #include "unicode/localpointer.h" +#include "resource.h" +#include "unicode/simpleformatter.h" #include "quantityformatter.h" #include "unicode/plurrule.h" #include "unicode/decimfmt.h" -#include "lrucache.h" #include "uresimp.h" #include "unicode/ures.h" +#include "unicode/ustring.h" +#include "ureslocs.h" #include "cstring.h" #include "mutex.h" #include "ucln_in.h" @@ -30,31 +35,23 @@ #include "charstr.h" #include "unicode/putil.h" #include "unicode/smpdtfmt.h" +#include "uassert.h" +#include "unicode/numberformatter.h" +#include "number_longnames.h" +// Apple-specific +#include "unicode/uameasureformat.h" +#include "fphdlimp.h" #include "sharednumberformat.h" #include "sharedpluralrules.h" +#include "standardplural.h" +#include "unifiedcache.h" -#define LENGTHOF(array) (int32_t)(sizeof(array)/sizeof((array)[0])) -#define MEAS_UNIT_COUNT 52 -#define WIDTH_INDEX_COUNT (UMEASFMT_WIDTH_NARROW + 1) - -static icu::LRUCache *gCache = NULL; -static UMutex gCacheMutex = U_MUTEX_INITIALIZER; -static icu::UInitOnce gCacheInitOnce = U_INITONCE_INITIALIZER; - -U_CDECL_BEGIN -static UBool U_CALLCONV measfmt_cleanup() { - gCacheInitOnce.reset(); - if (gCache) { - delete gCache; - gCache = NULL; - } - return TRUE; -} -U_CDECL_END U_NAMESPACE_BEGIN +static constexpr int32_t WIDTH_INDEX_COUNT = UMEASFMT_WIDTH_NARROW + 1; + UOBJECT_DEFINE_RTTI_IMPLEMENTATION(MeasureFormat) // Used to format durations like 5:47 or 21:35:42. @@ -89,18 +86,55 @@ private: NumericDateFormatters &operator=(const NumericDateFormatters &other); }; -// Instances contain all MeasureFormat specific data for a particular locale. -// This data is cached. It is never copied, but is shared via shared pointers. +static UMeasureFormatWidth getRegularWidth(UMeasureFormatWidth width) { + if (width >= WIDTH_INDEX_COUNT) { + return UMEASFMT_WIDTH_NARROW; + } + return width; +} + +static UNumberUnitWidth getUnitWidth(UMeasureFormatWidth width) { + switch (width) { + case UMEASFMT_WIDTH_WIDE: + return UNUM_UNIT_WIDTH_FULL_NAME; + case UMEASFMT_WIDTH_NARROW: + case UMEASFMT_WIDTH_NUMERIC: + return UNUM_UNIT_WIDTH_NARROW; + case UMEASFMT_WIDTH_SHORT: + default: + return UNUM_UNIT_WIDTH_SHORT; + } +} + +/** + * Instances contain all MeasureFormat specific data for a particular locale. + * This data is cached. It is never copied, but is shared via shared pointers. + * + * Note: We might change the cache data to have an array[WIDTH_INDEX_COUNT] of + * complete sets of unit & per patterns, + * to correspond to the resource data and its aliases. + * + * TODO: Maybe store more sparsely in general, with pointers rather than potentially-empty objects. + */ class MeasureFormatCacheData : public SharedObject { public: - QuantityFormatter formatters[MEAS_UNIT_COUNT][WIDTH_INDEX_COUNT]; + + /** + * Redirection data from root-bundle, top-level sideways aliases. + * - UMEASFMT_WIDTH_COUNT: initial value, just fall back to root + * - UMEASFMT_WIDTH_WIDE/SHORT/NARROW: sideways alias for missing data + */ + UMeasureFormatWidth widthFallback[WIDTH_INDEX_COUNT]; + MeasureFormatCacheData(); + virtual ~MeasureFormatCacheData(); + void adoptCurrencyFormat(int32_t widthIndex, NumberFormat *nfToAdopt) { delete currencyFormats[widthIndex]; currencyFormats[widthIndex] = nfToAdopt; } - const NumberFormat *getCurrencyFormat(int32_t widthIndex) const { - return currencyFormats[widthIndex]; + const NumberFormat *getCurrencyFormat(UMeasureFormatWidth width) const { + return currencyFormats[getRegularWidth(width)]; } void adoptIntegerFormat(NumberFormat *nfToAdopt) { delete integerFormat; @@ -116,38 +150,33 @@ public: const NumericDateFormatters *getNumericDateFormatters() const { return numericDateFormatters; } - virtual ~MeasureFormatCacheData(); + private: - NumberFormat *currencyFormats[WIDTH_INDEX_COUNT]; - NumberFormat *integerFormat; - NumericDateFormatters *numericDateFormatters; + NumberFormat* currencyFormats[WIDTH_INDEX_COUNT]; + NumberFormat* integerFormat; + NumericDateFormatters* numericDateFormatters; + MeasureFormatCacheData(const MeasureFormatCacheData &other); MeasureFormatCacheData &operator=(const MeasureFormatCacheData &other); }; -MeasureFormatCacheData::MeasureFormatCacheData() { - for (int32_t i = 0; i < LENGTHOF(currencyFormats); ++i) { - currencyFormats[i] = NULL; +MeasureFormatCacheData::MeasureFormatCacheData() + : integerFormat(nullptr), numericDateFormatters(nullptr) { + for (int32_t i = 0; i < WIDTH_INDEX_COUNT; ++i) { + widthFallback[i] = UMEASFMT_WIDTH_COUNT; } - integerFormat = NULL; - numericDateFormatters = NULL; + memset(currencyFormats, 0, sizeof(currencyFormats)); } MeasureFormatCacheData::~MeasureFormatCacheData() { - for (int32_t i = 0; i < LENGTHOF(currencyFormats); ++i) { + for (int32_t i = 0; i < UPRV_LENGTHOF(currencyFormats); ++i) { delete currencyFormats[i]; } + // Note: the contents of 'dnams' are pointers into the resource bundle delete integerFormat; delete numericDateFormatters; } -static int32_t widthToIndex(UMeasureFormatWidth width) { - if (width >= WIDTH_INDEX_COUNT) { - return WIDTH_INDEX_COUNT - 1; - } - return width; -} - static UBool isCurrency(const MeasureUnit &unit) { return (uprv_strcmp(unit.getType(), "currency") == 0); } @@ -165,91 +194,195 @@ static UBool getString( return TRUE; } - -static UBool loadMeasureUnitData( - const UResourceBundle *resource, - MeasureFormatCacheData &cacheData, - UErrorCode &status) { - if (U_FAILURE(status)) { - return FALSE; - } - static const char *widthPath[] = {"units", "unitsShort", "unitsNarrow"}; - MeasureUnit *units = NULL; - int32_t unitCount = MeasureUnit::getAvailable(units, 0, status); - while (status == U_BUFFER_OVERFLOW_ERROR) { - status = U_ZERO_ERROR; - delete [] units; - units = new MeasureUnit[unitCount]; - if (units == NULL) { - status = U_MEMORY_ALLOCATION_ERROR; - return FALSE; - } - unitCount = MeasureUnit::getAvailable(units, unitCount, status); - } - for (int32_t currentWidth = 0; currentWidth < WIDTH_INDEX_COUNT; ++currentWidth) { - // Be sure status is clear since next resource bundle lookup may fail. - if (U_FAILURE(status)) { - delete [] units; - return FALSE; - } - LocalUResourceBundlePointer widthBundle( - ures_getByKeyWithFallback( - resource, widthPath[currentWidth], NULL, &status)); - // We may not have data for all widths in all locales. - if (status == U_MISSING_RESOURCE_ERROR) { - status = U_ZERO_ERROR; - continue; - } - for (int32_t currentUnit = 0; currentUnit < unitCount; ++currentUnit) { - // Be sure status is clear next lookup may fail. - if (U_FAILURE(status)) { - delete [] units; - return FALSE; - } - if (isCurrency(units[currentUnit])) { - continue; - } - CharString pathBuffer; - pathBuffer.append(units[currentUnit].getType(), status) - .append("/", status) - .append(units[currentUnit].getSubtype(), status); - LocalUResourceBundlePointer unitBundle( - ures_getByKeyWithFallback( - widthBundle.getAlias(), - pathBuffer.data(), - NULL, - &status)); - // We may not have data for all units in all widths - if (status == U_MISSING_RESOURCE_ERROR) { - status = U_ZERO_ERROR; - continue; - } - // We must have the unit bundle to proceed - if (U_FAILURE(status)) { - delete [] units; - return FALSE; - } - int32_t size = ures_getSize(unitBundle.getAlias()); - for (int32_t plIndex = 0; plIndex < size; ++plIndex) { - LocalUResourceBundlePointer pluralBundle( - ures_getByIndex( - unitBundle.getAlias(), plIndex, NULL, &status)); - if (U_FAILURE(status)) { - delete [] units; - return FALSE; - } - UnicodeString rawPattern; - getString(pluralBundle.getAlias(), rawPattern, status); - cacheData.formatters[units[currentUnit].getIndex()][currentWidth].add( - ures_getKey(pluralBundle.getAlias()), - rawPattern, - status); - } - } - } - delete [] units; - return U_SUCCESS(status); -} +static const UAMeasureUnit indexToUAMsasUnit[] = { + // UAMeasureUnit // UAMeasUnit vals # MeasUnit.getIndex() + // # --- acceleration (0) + UAMEASUNIT_ACCELERATION_G_FORCE, // (0 << 8) + 0, # 0 g-force + UAMEASUNIT_ACCELERATION_METER_PER_SECOND_SQUARED, // (0 << 8) + 1, # 1 meter-per-second-squared + // # --- angle (2) + UAMEASUNIT_ANGLE_ARC_MINUTE, // (1 << 8) + 1, # 2 arc-minute + UAMEASUNIT_ANGLE_ARC_SECOND, // (1 << 8) + 2, # 3 arc-second + UAMEASUNIT_ANGLE_DEGREE, // (1 << 8) + 0, # 4 degree + UAMEASUNIT_ANGLE_RADIAN, // (1 << 8) + 3, # 5 radian + UAMEASUNIT_ANGLE_REVOLUTION, // (1 << 8) + 4, # 6 revolution + // # --- area (7) + UAMEASUNIT_AREA_ACRE, // (2 << 8) + 4, # 7 acre + UAMEASUNIT_AREA_DUNAM, // (2 << 8) + 9, # 8 dunam + UAMEASUNIT_AREA_HECTARE, // (2 << 8) + 5, # 9 hectare + UAMEASUNIT_AREA_SQUARE_CENTIMETER, // (2 << 8) + 6, # 10 square-centimeter + UAMEASUNIT_AREA_SQUARE_FOOT, // (2 << 8) + 2, # 11 square-foot + UAMEASUNIT_AREA_SQUARE_INCH, // (2 << 8) + 7, # 12 square-inch + UAMEASUNIT_AREA_SQUARE_KILOMETER, // (2 << 8) + 1, # 13 square-kilometer + UAMEASUNIT_AREA_SQUARE_METER, // (2 << 8) + 0, # 14 square-meter + UAMEASUNIT_AREA_SQUARE_MILE, // (2 << 8) + 3, # 15 square-mile + UAMEASUNIT_AREA_SQUARE_YARD, // (2 << 8) + 8, # 16 square-yard + // # --- concentr (17) + UAMEASUNIT_CONCENTRATION_KARAT, // (18 << 8) + 0, # 17 karat + UAMEASUNIT_CONCENTRATION_MILLIGRAM_PER_DECILITER, // (18 << 8) + 1, # 18 milligram-per-deciliter + UAMEASUNIT_CONCENTRATION_MILLIMOLE_PER_LITER, // (18 << 8) + 2, # 19 millimole-per-liter + UAMEASUNIT_CONCENTRATION_MOLE, // (18 << 8) + 7, # 20 mole + UAMEASUNIT_CONCENTRATION_PART_PER_MILLION, // (18 << 8) + 3, # 21 part-per-million + UAMEASUNIT_CONCENTRATION_PERCENT, // (18 << 8) + 4, # 22 percent + UAMEASUNIT_CONCENTRATION_PERMILLE, // (18 << 8) + 5, # 23 permille + UAMEASUNIT_CONCENTRATION_PERMYRIAD, // (18 << 8) + 6, # 24 permyriad + // # --- consumption (25) + UAMEASUNIT_CONSUMPTION_LITER_PER_100_KILOMETERs, // (13 << 8) + 2, # 25 liter-per-100kilometers + UAMEASUNIT_CONSUMPTION_LITER_PER_KILOMETER, // (13 << 8) + 0, # 26 liter-per-kilometer + UAMEASUNIT_CONSUMPTION_MILE_PER_GALLON, // (13 << 8) + 1, # 27 mile-per-gallon + UAMEASUNIT_CONSUMPTION_MILE_PER_GALLON_IMPERIAL, // (13 << 8) + 3, # 28 mile-per-gallon-imperial + // # --- currency (29) + // # --- digital (29) + UAMEASUNIT_DIGITAL_BIT, // (14 << 8) + 0, # 29 bit + UAMEASUNIT_DIGITAL_BYTE, // (14 << 8) + 1, # 30 byte + UAMEASUNIT_DIGITAL_GIGABIT, // (14 << 8) + 2, # 31 gigabit + UAMEASUNIT_DIGITAL_GIGABYTE, // (14 << 8) + 3, # 32 gigabyte + UAMEASUNIT_DIGITAL_KILOBIT, // (14 << 8) + 4, # 33 kilobit + UAMEASUNIT_DIGITAL_KILOBYTE, // (14 << 8) + 5, # 34 kilobyte + UAMEASUNIT_DIGITAL_MEGABIT, // (14 << 8) + 6, # 35 megabit + UAMEASUNIT_DIGITAL_MEGABYTE, // (14 << 8) + 7, # 36 megabyte + UAMEASUNIT_DIGITAL_PETABYTE, // (14 << 8) + 10, # 37 petabyte + UAMEASUNIT_DIGITAL_TERABIT, // (14 << 8) + 8, # 38 terabit + UAMEASUNIT_DIGITAL_TERABYTE, // (14 << 8) + 9, # 39 terabyte + // # --- duration (40) + UAMEASUNIT_DURATION_CENTURY, // (4 << 8) + 10, # 40 century + UAMEASUNIT_DURATION_DAY, // (4 << 8) + 3, # 41 day + UAMEASUNIT_DURATION_DAY_PERSON, // (4 << 8) + 14, # 42 day-person + UAMEASUNIT_DURATION_HOUR, // (4 << 8) + 4, # 43 hour + UAMEASUNIT_DURATION_MICROSECOND, // (4 << 8) + 8, # 44 microsecond + UAMEASUNIT_DURATION_MILLISECOND, // (4 << 8) + 7, # 45 millisecond + UAMEASUNIT_DURATION_MINUTE, // (4 << 8) + 5, # 46 minute + UAMEASUNIT_DURATION_MONTH, // (4 << 8) + 1, # 47 month + UAMEASUNIT_DURATION_MONTH_PERSON, // (4 << 8) + 12, # 48 month-person + UAMEASUNIT_DURATION_NANOSECOND, // (4 << 8) + 9, # 49 nanosecond + UAMEASUNIT_DURATION_SECOND, // (4 << 8) + 6, # 50 second + UAMEASUNIT_DURATION_WEEK, // (4 << 8) + 2, # 51 week + UAMEASUNIT_DURATION_WEEK_PERSON, // (4 << 8) + 13, # 52 week-person + UAMEASUNIT_DURATION_YEAR, // (4 << 8) + 0, # 53 year + UAMEASUNIT_DURATION_YEAR_PERSON, // (4 << 8) + 11, # 54 year-person + // # --- electric (55) + UAMEASUNIT_ELECTRIC_AMPERE, // (15 << 8) + 0, # 55 ampere + UAMEASUNIT_ELECTRIC_MILLIAMPERE, // (15 << 8) + 1, # 56 milliampere + UAMEASUNIT_ELECTRIC_OHM, // (15 << 8) + 2, # 57 ohm + UAMEASUNIT_ELECTRIC_VOLT, // (15 << 8) + 3, # 58 volt + // # --- energy (59) + UAMEASUNIT_ENERGY_BRITISH_THERMAL_UNIT, // (12 << 8) + 7, # 59 british-thermal-unit + UAMEASUNIT_ENERGY_CALORIE, // (12 << 8) + 0, # 60 calorie + UAMEASUNIT_ENERGY_ELECTRONVOLT, // (12 << 8) + 6, # 61 electronvolt + UAMEASUNIT_ENERGY_FOODCALORIE, // (12 << 8) + 1, # 62 foodcalorie + UAMEASUNIT_ENERGY_JOULE, // (12 << 8) + 2, # 63 joule + UAMEASUNIT_ENERGY_KILOCALORIE, // (12 << 8) + 3, # 64 kilocalorie + UAMEASUNIT_ENERGY_KILOJOULE, // (12 << 8) + 4, # 65 kilojoule + UAMEASUNIT_ENERGY_KILOWATT_HOUR, // (12 << 8) + 5, # 66 kilowatt-hour + // # --- force (67) + UAMEASUNIT_FORCE_NEWTON, // (19 << 8) + 0, # 67 newton + UAMEASUNIT_FORCE_POUND_FORCE, // (19 << 8) + 1, # 68 pound-force + // # --- frequency (69) + UAMEASUNIT_FREQUENCY_GIGAHERTZ, // (16 << 8) + 3, # 69 gigahertz + UAMEASUNIT_FREQUENCY_HERTZ, // (16 << 8) + 0, # 70 hertz + UAMEASUNIT_FREQUENCY_KILOHERTZ, // (16 << 8) + 1, # 71 kilohertz + UAMEASUNIT_FREQUENCY_MEGAHERTZ, // (16 << 8) + 2, # 72 megahertz + // # --- length (73) + UAMEASUNIT_LENGTH_ASTRONOMICAL_UNIT, // (5 << 8) + 16, # 73 astronomical-unit + UAMEASUNIT_LENGTH_CENTIMETER, // (5 << 8) + 1, # 74 centimeter + UAMEASUNIT_LENGTH_DECIMETER, // (5 << 8) + 10, # 75 decimeter + UAMEASUNIT_LENGTH_FATHOM, // (5 << 8) + 14, # 76 fathom + UAMEASUNIT_LENGTH_FOOT, // (5 << 8) + 5, # 77 foot + UAMEASUNIT_LENGTH_FURLONG, // (5 << 8) + 15, # 78 furlong + UAMEASUNIT_LENGTH_INCH, // (5 << 8) + 6, # 79 inch + UAMEASUNIT_LENGTH_KILOMETER, // (5 << 8) + 2, # 80 kilometer + UAMEASUNIT_LENGTH_LIGHT_YEAR, // (5 << 8) + 9, # 81 light-year + UAMEASUNIT_LENGTH_METER, // (5 << 8) + 0, # 82 meter + UAMEASUNIT_LENGTH_MICROMETER, // (5 << 8) + 11, # 83 micrometer + UAMEASUNIT_LENGTH_MILE, // (5 << 8) + 7, # 84 mile + UAMEASUNIT_LENGTH_MILE_SCANDINAVIAN, // (5 << 8) + 18, # 85 mile-scandinavian + UAMEASUNIT_LENGTH_MILLIMETER, // (5 << 8) + 3, # 86 millimeter + UAMEASUNIT_LENGTH_NANOMETER, // (5 << 8) + 12, # 87 nanometer + UAMEASUNIT_LENGTH_NAUTICAL_MILE, // (5 << 8) + 13, # 88 nautical-mile + UAMEASUNIT_LENGTH_PARSEC, // (5 << 8) + 17, # 89 parsec + UAMEASUNIT_LENGTH_PICOMETER, // (5 << 8) + 4, # 90 picometer + UAMEASUNIT_LENGTH_POINT, // (5 << 8) + 19, # 91 point + UAMEASUNIT_LENGTH_SOLAR_RADIUS, // (5 << 8) + 20, # 92 solar-radius + UAMEASUNIT_LENGTH_YARD, // (5 << 8) + 8, # 93 yard + // # --- light (94) + UAMEASUNIT_LIGHT_LUX, // (17 << 8) + 0, # 94 lux + UAMEASUNIT_LIGHT_SOLAR_LUMINOSITY, // (17 << 8) + 1, # 95 solar-luminosity + // # --- mass (96) + UAMEASUNIT_MASS_CARAT, // (6 << 8) + 9, # 96 carat + UAMEASUNIT_MASS_DALTON, // (6 << 8) + 11, # 97 dalton + UAMEASUNIT_MASS_EARTH_MASS, // (6 << 8) + 12, # 98 earth-mass + UAMEASUNIT_MASS_GRAM, // (6 << 8) + 0, # 99 gram + UAMEASUNIT_MASS_KILOGRAM, // (6 << 8) + 1, # 100 kilogram + UAMEASUNIT_MASS_METRIC_TON, // (6 << 8) + 7, # 101 metric-ton + UAMEASUNIT_MASS_MICROGRAM, // (6 << 8) + 5, # 102 microgram + UAMEASUNIT_MASS_MILLIGRAM, // (6 << 8) + 6, # 103 milligram + UAMEASUNIT_MASS_OUNCE, // (6 << 8) + 2, # 104 ounce + UAMEASUNIT_MASS_OUNCE_TROY, // (6 << 8) + 10, # 105 ounce-troy + UAMEASUNIT_MASS_POUND, // (6 << 8) + 3, # 106 pound + UAMEASUNIT_MASS_SOLAR_MASS, // (6 << 8) + 13, # 107 solar-mass + UAMEASUNIT_MASS_STONE, // (6 << 8) + 4, # 108 stone + UAMEASUNIT_MASS_TON, // (6 << 8) + 8, # 109 ton + // # --- none (110) + UAMEASUNIT_CONCENTRATION_PERCENT, // BOGUS # 110 base + UAMEASUNIT_CONCENTRATION_PERCENT, // BOGUS # 111 percent + UAMEASUNIT_CONCENTRATION_PERMILLE, // BOGUS # 112 permille + // # --- power (113) + UAMEASUNIT_POWER_GIGAWATT, // (7 << 8) + 5, # 113 gigawatt + UAMEASUNIT_POWER_HORSEPOWER, // (7 << 8) + 2, # 114 horsepower + UAMEASUNIT_POWER_KILOWATT, // (7 << 8) + 1, # 115 kilowatt + UAMEASUNIT_POWER_MEGAWATT, // (7 << 8) + 4, # 116 megawatt + UAMEASUNIT_POWER_MILLIWATT, // (7 << 8) + 3, # 117 milliwatt + UAMEASUNIT_POWER_WATT, // (7 << 8) + 0, # 118 watt + // # --- pressure (119) + UAMEASUNIT_PRESSURE_ATMOSPHERE, // (8 << 8) + 5, # 119 atmosphere + UAMEASUNIT_PRESSURE_HECTOPASCAL, // (8 << 8) + 0, # 120 hectopascal + UAMEASUNIT_PRESSURE_INCH_HG, // (8 << 8) + 1, # 121 inch-hg + UAMEASUNIT_PRESSURE_KILOPASCAL, // (8 << 8) + 6, # 122 kilopascal + UAMEASUNIT_PRESSURE_MEGAPASCAL, // (8 << 8) + 7, # 123 megapascal + UAMEASUNIT_PRESSURE_MILLIBAR, // (8 << 8) + 2, # 124 millibar + UAMEASUNIT_PRESSURE_MILLIMETER_OF_MERCURY, // (8 << 8) + 3, # 125 millimeter-of-mercury + UAMEASUNIT_PRESSURE_POUND_PER_SQUARE_INCH, // (8 << 8) + 4, # 126 pound-per-square-inch + // # --- speed (127) + UAMEASUNIT_SPEED_KILOMETER_PER_HOUR, // (9 << 8) + 1, # 127 kilometer-per-hour + UAMEASUNIT_SPEED_KNOT, // (9 << 8) + 3, # 128 knot + UAMEASUNIT_SPEED_METER_PER_SECOND, // (9 << 8) + 0, # 129 meter-per-second + UAMEASUNIT_SPEED_MILE_PER_HOUR, // (9 << 8) + 2, # 130 mile-per-hour + // # --- temperature (131) + UAMEASUNIT_TEMPERATURE_CELSIUS, // (10 << 8) + 0, # 131 celsius + UAMEASUNIT_TEMPERATURE_FAHRENHEIT, // (10 << 8) + 1, # 132 fahrenheit + UAMEASUNIT_TEMPERATURE_GENERIC, // (10 << 8) + 3, # 133 generic + UAMEASUNIT_TEMPERATURE_KELVIN, // (10 << 8) + 2, # 134 kelvin + // # --- torque (135) + UAMEASUNIT_TORQUE_NEWTON_METER, // (20 << 8) + 0, # 135 newton-meter + UAMEASUNIT_TORQUE_POUND_FOOT, // (20 << 8) + 1, # 136 pound-foot + // # --- volume (137) + UAMEASUNIT_VOLUME_ACRE_FOOT, // (11 << 8) + 13, # 137 acre-foot + UAMEASUNIT_VOLUME_BARREL, // (11 << 8) + 26, # 138 barrel + UAMEASUNIT_VOLUME_BUSHEL, // (11 << 8) + 14, # 139 bushel + UAMEASUNIT_VOLUME_CENTILITER, // (11 << 8) + 4, # 140 centiliter + UAMEASUNIT_VOLUME_CUBIC_CENTIMETER, // (11 << 8) + 8, # 141 cubic-centimeter + UAMEASUNIT_VOLUME_CUBIC_FOOT, // (11 << 8) + 11, # 142 cubic-foot + UAMEASUNIT_VOLUME_CUBIC_INCH, // (11 << 8) + 10, # 143 cubic-inch + UAMEASUNIT_VOLUME_CUBIC_KILOMETER, // (11 << 8) + 1, # 144 cubic-kilometer + UAMEASUNIT_VOLUME_CUBIC_METER, // (11 << 8) + 9, # 145 cubic-meter + UAMEASUNIT_VOLUME_CUBIC_MILE, // (11 << 8) + 2, # 146 cubic-mile + UAMEASUNIT_VOLUME_CUBIC_YARD, // (11 << 8) + 12, # 147 cubic-yard + UAMEASUNIT_VOLUME_CUP, // (11 << 8) + 18, # 148 cup + UAMEASUNIT_VOLUME_CUP_METRIC, // (11 << 8) + 22, # 149 cup-metric + UAMEASUNIT_VOLUME_DECILITER, // (11 << 8) + 5, # 150 deciliter + UAMEASUNIT_VOLUME_FLUID_OUNCE, // (11 << 8) + 17, # 151 fluid-ounce + UAMEASUNIT_VOLUME_FLUID_OUNCE_IMPERIAL, // (11 << 8) + 25, # 152 fluid-ounce-imperial + UAMEASUNIT_VOLUME_GALLON, // (11 << 8) + 21, # 153 gallon + UAMEASUNIT_VOLUME_GALLON_IMPERIAL, // (11 << 8) + 24, # 154 gallon-imperial + UAMEASUNIT_VOLUME_HECTOLITER, // (11 << 8) + 6, # 155 hectoliter + UAMEASUNIT_VOLUME_LITER, // (11 << 8) + 0, # 156 liter + UAMEASUNIT_VOLUME_MEGALITER, // (11 << 8) + 7, # 157 megaliter + UAMEASUNIT_VOLUME_MILLILITER, // (11 << 8) + 3, # 158 milliliter + UAMEASUNIT_VOLUME_PINT, // (11 << 8) + 19, # 159 pint + UAMEASUNIT_VOLUME_PINT_METRIC, // (11 << 8) + 23, # 160 pint-metric + UAMEASUNIT_VOLUME_QUART, // (11 << 8) + 20, # 161 quart + UAMEASUNIT_VOLUME_TABLESPOON, // (11 << 8) + 16, # 162 tablespoon + UAMEASUNIT_VOLUME_TEASPOON, // (11 << 8) + 15, # 163 teaspoon +}; static UnicodeString loadNumericDateFormatterPattern( const UResourceBundle *resource, @@ -302,35 +435,32 @@ static NumericDateFormatters *loadNumericDateFormatters( return result; } -// Creates the MeasureFormatCacheData for a particular locale -static SharedObject *U_CALLCONV createData( - const char *localeId, UErrorCode &status) { - LocalUResourceBundlePointer topLevel(ures_open(NULL, localeId, &status)); +template<> U_I18N_API +const MeasureFormatCacheData *LocaleCacheKey::createObject( + const void * /*unused*/, UErrorCode &status) const { + const char *localeId = fLoc.getName(); + LocalUResourceBundlePointer unitsBundle(ures_open(U_ICUDATA_UNIT, localeId, &status)); static UNumberFormatStyle currencyStyles[] = { UNUM_CURRENCY_PLURAL, UNUM_CURRENCY_ISO, UNUM_CURRENCY}; + LocalPointer result(new MeasureFormatCacheData(), status); if (U_FAILURE(status)) { return NULL; } - LocalPointer result(new MeasureFormatCacheData()); - if (result.isNull()) { - status = U_MEMORY_ALLOCATION_ERROR; - return NULL; - } - if (!loadMeasureUnitData( - topLevel.getAlias(), - *result, - status)) { - return NULL; - } result->adoptNumericDateFormatters(loadNumericDateFormatters( - topLevel.getAlias(), status)); + unitsBundle.getAlias(), status)); if (U_FAILURE(status)) { return NULL; } for (int32_t i = 0; i < WIDTH_INDEX_COUNT; ++i) { + // NumberFormat::createInstance can erase warning codes from status, so pass it + // a separate status instance + UErrorCode localStatus = U_ZERO_ERROR; result->adoptCurrencyFormat(i, NumberFormat::createInstance( - localeId, currencyStyles[i], status)); + localeId, currencyStyles[i], localStatus)); + if (localStatus != U_ZERO_ERROR) { + status = localStatus; + } if (U_FAILURE(status)) { return NULL; } @@ -346,33 +476,10 @@ static SharedObject *U_CALLCONV createData( decfmt->setRoundingMode(DecimalFormat::kRoundDown); } result->adoptIntegerFormat(inf); + result->addRef(); return result.orphan(); } -static void U_CALLCONV cacheInit(UErrorCode &status) { - U_ASSERT(gCache == NULL); - U_ASSERT(MeasureUnit::getIndexCount() == MEAS_UNIT_COUNT); - ucln_i18n_registerCleanup(UCLN_I18N_MEASFMT, measfmt_cleanup); - gCache = new SimpleLRUCache(100, &createData, status); - if (U_FAILURE(status)) { - delete gCache; - gCache = NULL; - } -} - -static UBool getFromCache( - const char *locale, - const MeasureFormatCacheData *&ptr, - UErrorCode &status) { - umtx_initOnce(gCacheInitOnce, &cacheInit, status); - if (U_FAILURE(status)) { - return FALSE; - } - Mutex lock(&gCacheMutex); - gCache->get(locale, ptr, status); - return U_SUCCESS(status); -} - static UBool isTimeUnit(const MeasureUnit &mu, const char *tu) { return uprv_strcmp(mu.getType(), "duration") == 0 && uprv_strcmp(mu.getSubtype(), tu) == 0; @@ -444,9 +551,11 @@ MeasureFormat::MeasureFormat( : cache(NULL), numberFormat(NULL), pluralRules(NULL), - width(w), - listFormatter(NULL) { - initMeasureFormat(locale, w, NULL, status); + fWidth((w==UMEASFMT_WIDTH_SHORTER)? UMEASFMT_WIDTH_SHORT: w), + stripPatternSpaces(w==UMEASFMT_WIDTH_SHORTER), + listFormatter(NULL), + listFormatterStd(NULL) { + initMeasureFormat(locale, (w==UMEASFMT_WIDTH_SHORTER)? UMEASFMT_WIDTH_SHORT: w, NULL, status); } MeasureFormat::MeasureFormat( @@ -457,9 +566,11 @@ MeasureFormat::MeasureFormat( : cache(NULL), numberFormat(NULL), pluralRules(NULL), - width(w), - listFormatter(NULL) { - initMeasureFormat(locale, w, nfToAdopt, status); + fWidth((w==UMEASFMT_WIDTH_SHORTER)? UMEASFMT_WIDTH_SHORT: w), + stripPatternSpaces(w==UMEASFMT_WIDTH_SHORTER), + listFormatter(NULL), + listFormatterStd(NULL) { + initMeasureFormat(locale, (w==UMEASFMT_WIDTH_SHORTER)? UMEASFMT_WIDTH_SHORT: w, nfToAdopt, status); } MeasureFormat::MeasureFormat(const MeasureFormat &other) : @@ -467,12 +578,19 @@ MeasureFormat::MeasureFormat(const MeasureFormat &other) : cache(other.cache), numberFormat(other.numberFormat), pluralRules(other.pluralRules), - width(other.width), - listFormatter(NULL) { + fWidth(other.fWidth), + stripPatternSpaces(other.stripPatternSpaces), + listFormatter(NULL), + listFormatterStd(NULL) { cache->addRef(); numberFormat->addRef(); pluralRules->addRef(); - listFormatter = new ListFormatter(*other.listFormatter); + if (other.listFormatter != NULL) { + listFormatter = new ListFormatter(*other.listFormatter); + } + if (other.listFormatterStd != NULL) { + listFormatterStd = new ListFormatter(*other.listFormatterStd); + } } MeasureFormat &MeasureFormat::operator=(const MeasureFormat &other) { @@ -483,9 +601,20 @@ MeasureFormat &MeasureFormat::operator=(const MeasureFormat &other) { SharedObject::copyPtr(other.cache, cache); SharedObject::copyPtr(other.numberFormat, numberFormat); SharedObject::copyPtr(other.pluralRules, pluralRules); - width = other.width; + fWidth = other.fWidth; + stripPatternSpaces = other.stripPatternSpaces; delete listFormatter; - listFormatter = new ListFormatter(*other.listFormatter); + if (other.listFormatter != NULL) { + listFormatter = new ListFormatter(*other.listFormatter); + } else { + listFormatter = NULL; + } + delete listFormatterStd; + if (other.listFormatterStd != NULL) { + listFormatterStd = new ListFormatter(*other.listFormatterStd); + } else { + listFormatterStd = NULL; + } return *this; } @@ -493,8 +622,10 @@ MeasureFormat::MeasureFormat() : cache(NULL), numberFormat(NULL), pluralRules(NULL), - width(UMEASFMT_WIDTH_WIDE), - listFormatter(NULL) { + fWidth(UMEASFMT_WIDTH_SHORT), + stripPatternSpaces(FALSE), + listFormatter(NULL), + listFormatterStd(NULL) { } MeasureFormat::~MeasureFormat() { @@ -508,6 +639,7 @@ MeasureFormat::~MeasureFormat() { pluralRules->removeRef(); } delete listFormatter; + delete listFormatterStd; } UBool MeasureFormat::operator==(const Format &other) const { @@ -523,7 +655,7 @@ UBool MeasureFormat::operator==(const Format &other) const { // don't have to check it here. // differing widths aren't equivalent - if (width != rhs.width) { + if (fWidth != rhs.fWidth || stripPatternSpaces != rhs.stripPatternSpaces) { return FALSE; } // Width the same check locales. @@ -575,6 +707,33 @@ void MeasureFormat::parseObject( return; } +UnicodeString &MeasureFormat::formatMeasurePerUnit( + const Measure &measure, + const MeasureUnit &perUnit, + UnicodeString &appendTo, + FieldPosition &pos, + UErrorCode &status) const { + if (U_FAILURE(status)) { + return appendTo; + } + auto* df = dynamic_cast(&getNumberFormatInternal()); + if (df == nullptr) { + // Don't know how to handle other types of NumberFormat + status = U_UNSUPPORTED_ERROR; + return appendTo; + } + number::FormattedNumber result; + if (auto* lnf = df->toNumberFormatter(status)) { + result = lnf->unit(measure.getUnit()) + .perUnit(perUnit) + .unitWidth(getUnitWidth(fWidth)) + .formatDouble(measure.getNumber().getDouble(status), status); + } + DecimalFormat::fieldPositionHelper(result, pos, appendTo.length(), status); + appendTo.append(result.toTempString(status)); + return appendTo; +} + UnicodeString &MeasureFormat::formatMeasures( const Measure *measures, int32_t measureCount, @@ -590,11 +749,12 @@ UnicodeString &MeasureFormat::formatMeasures( if (measureCount == 1) { return formatMeasure(measures[0], **numberFormat, appendTo, pos, status); } - if (width == UMEASFMT_WIDTH_NUMERIC) { + if (fWidth == UMEASFMT_WIDTH_NUMERIC) { Formattable hms[3]; int32_t bitMap = toHMS(measures, measureCount, hms, status); if (bitMap > 0) { - return formatNumeric(hms, bitMap, appendTo, status); + FieldPositionIteratorHandler handler((FieldPositionIterator*)NULL, status); + return formatNumeric(hms, bitMap, appendTo, handler, status); } } if (pos.getField() != FieldPosition::DONT_CARE) { @@ -619,10 +779,92 @@ UnicodeString &MeasureFormat::formatMeasures( status); } listFormatter->format(results, measureCount, appendTo, status); - delete [] results; + delete [] results; + return appendTo; +} + +// Apple-specific version for now; +// uses FieldPositionIterator* instead of FieldPosition& +UnicodeString &MeasureFormat::formatMeasures( + const Measure *measures, + int32_t measureCount, + UnicodeString &appendTo, + FieldPositionIterator* posIter, + UErrorCode &status) const { + if (U_FAILURE(status)) { + return appendTo; + } + FieldPositionIteratorHandler handler(posIter, status); + if (measureCount == 0) { + return appendTo; + } + if (measureCount == 1) { + int32_t start = appendTo.length(); + int32_t field = indexToUAMsasUnit[measures[0].getUnit().getIndex()]; + FieldPosition pos(UAMEASFMT_NUMERIC_FIELD_FLAG); // special field value to request range of entire numeric part + formatMeasure(measures[0], **numberFormat, appendTo, pos, status); + handler.addAttribute(field, start, appendTo.length()); + handler.addAttribute(field | UAMEASFMT_NUMERIC_FIELD_FLAG, pos.getBeginIndex(), pos.getEndIndex()); + return appendTo; + } + if (fWidth == UMEASFMT_WIDTH_NUMERIC) { + Formattable hms[3]; + int32_t bitMap = toHMS(measures, measureCount, hms, status); + if (bitMap > 0) { + return formatNumeric(hms, bitMap, appendTo, handler, status); + } + } + UnicodeString *results = new UnicodeString[measureCount]; + if (results == NULL) { + status = U_MEMORY_ALLOCATION_ERROR; + return appendTo; + } + FieldPosition *numPositions = new FieldPosition[measureCount]; + if (results == NULL) { + delete [] results; + status = U_MEMORY_ALLOCATION_ERROR; + return appendTo; + } + + for (int32_t i = 0; i < measureCount; ++i) { + const NumberFormat *nf = cache->getIntegerFormat(); + if (i == measureCount - 1) { + nf = numberFormat->get(); + } + numPositions[i].setField(UAMEASFMT_NUMERIC_FIELD_FLAG); + formatMeasure( + measures[i], + *nf, + results[i], + numPositions[i], + status); + } + listFormatter->format(results, measureCount, appendTo, status); + for (int32_t i = 0; i < measureCount; ++i) { + int32_t begin = appendTo.indexOf(results[i]); + if (begin >= 0) { + int32_t field = indexToUAMsasUnit[measures[i].getUnit().getIndex()]; + handler.addAttribute(field, begin, begin + results[i].length()); + int32_t numPosBegin = numPositions[i].getBeginIndex(); + int32_t numPosEnd = numPositions[i].getEndIndex(); + if (numPosBegin >= 0 && numPosEnd > numPosBegin) { + handler.addAttribute(field | UAMEASFMT_NUMERIC_FIELD_FLAG, begin + numPosBegin, begin + numPosEnd); + } + } + } + delete [] results; + delete [] numPositions; return appendTo; } +UnicodeString MeasureFormat::getUnitDisplayName(const MeasureUnit& unit, UErrorCode& status) const { + return number::impl::LongNameHandler::getUnitDisplayName( + getLocale(status), + unit, + getUnitWidth(fWidth), + status); +} + void MeasureFormat::initMeasureFormat( const Locale &locale, UMeasureFormatWidth w, @@ -636,38 +878,46 @@ void MeasureFormat::initMeasureFormat( const char *name = locale.getName(); setLocaleIDs(name, name); - if (!getFromCache(name, cache, status)) { + UnifiedCache::getByLocale(locale, cache, status); + if (U_FAILURE(status)) { return; } - SharedObject::copyPtr( - PluralRules::createSharedInstance( - locale, UPLURAL_TYPE_CARDINAL, status), - pluralRules); + const SharedPluralRules *pr = PluralRules::createSharedInstance( + locale, UPLURAL_TYPE_CARDINAL, status); if (U_FAILURE(status)) { return; } - pluralRules->removeRef(); + SharedObject::copyPtr(pr, pluralRules); + pr->removeRef(); if (nf.isNull()) { - SharedObject::copyPtr( - NumberFormat::createSharedInstance( - locale, UNUM_DECIMAL, status), - numberFormat); + // TODO: Clean this up + const SharedNumberFormat *shared = NumberFormat::createSharedInstance( + locale, UNUM_DECIMAL, status); if (U_FAILURE(status)) { return; } - numberFormat->removeRef(); + SharedObject::copyPtr(shared, numberFormat); + shared->removeRef(); } else { adoptNumberFormat(nf.orphan(), status); if (U_FAILURE(status)) { return; } } - width = w; + fWidth = w; + if (stripPatternSpaces) { + w = UMEASFMT_WIDTH_NARROW; + } delete listFormatter; listFormatter = ListFormatter::createInstance( locale, - listStyles[widthToIndex(width)], + listStyles[getRegularWidth(w)], + status); + delete listFormatterStd; + listFormatterStd = ListFormatter::createInstance( + locale, + "standard", status); } @@ -690,14 +940,23 @@ UBool MeasureFormat::setMeasureFormatLocale(const Locale &locale, UErrorCode &st if (U_FAILURE(status) || locale == getLocale(status)) { return FALSE; } - initMeasureFormat(locale, width, NULL, status); + initMeasureFormat(locale, fWidth, NULL, status); return U_SUCCESS(status); } -const NumberFormat &MeasureFormat::getNumberFormat() const { +// Apple-specific for now +UMeasureFormatWidth MeasureFormat::getWidth() const { + return fWidth; +} + +const NumberFormat &MeasureFormat::getNumberFormatInternal() const { return **numberFormat; } +const NumberFormat &MeasureFormat::getCurrencyFormatInternal() const { + return *cache->getCurrencyFormat(UMEASFMT_WIDTH_NARROW); +} + const PluralRules &MeasureFormat::getPluralRules() const { return **pluralRules; } @@ -710,6 +969,49 @@ const char *MeasureFormat::getLocaleID(UErrorCode &status) const { return Format::getLocaleID(ULOC_VALID_LOCALE, status); } +// Apple=specific +// now just re-implement using standard getUnitDisplayName +// so we no longer use cache->getDisplayName +UnicodeString &MeasureFormat::getUnitName( + const MeasureUnit* unit, + UnicodeString &result ) const { + UErrorCode status = U_ZERO_ERROR; + result = getUnitDisplayName(*unit, status); // does not use or set status + return result; +} + +// Apple=specific +UnicodeString &MeasureFormat::getMultipleUnitNames( + const MeasureUnit** units, + int32_t unitCount, + UAMeasureNameListStyle listStyle, + UnicodeString &result ) const { + if (unitCount == 0) { + return result.remove(); + } + if (unitCount == 1) { + return getUnitName(units[0], result); + } + UnicodeString *results = new UnicodeString[unitCount]; + if (results != NULL) { + for (int32_t i = 0; i < unitCount; ++i) { + getUnitName(units[i], results[i]); + } + UErrorCode status = U_ZERO_ERROR; + if (listStyle == UAMEASNAME_LIST_STANDARD) { + listFormatterStd->format(results, unitCount, result, status); + } else { + listFormatter->format(results, unitCount, result, status); + } + delete [] results; + if (U_SUCCESS(status)) { + return result; + } + } + result.setToBogus(); + return result; +} + UnicodeString &MeasureFormat::formatMeasure( const Measure &measure, const NumberFormat &nf, @@ -724,24 +1026,104 @@ UnicodeString &MeasureFormat::formatMeasure( if (isCurrency(amtUnit)) { UChar isoCode[4]; u_charsToUChars(amtUnit.getSubtype(), isoCode, 4); - return cache->getCurrencyFormat(widthToIndex(width))->format( + return cache->getCurrencyFormat(fWidth)->format( new CurrencyAmount(amtNumber, isoCode, status), appendTo, pos, status); } - const QuantityFormatter *quantityFormatter = getQuantityFormatter( - amtUnit.getIndex(), widthToIndex(width), status); - if (U_FAILURE(status)) { - return appendTo; + int32_t cur = appendTo.length(); + UBool posForFullNumericPart = (pos.getField() == UAMEASFMT_NUMERIC_FIELD_FLAG); + if (posForFullNumericPart) { + pos.setField(FieldPosition::DONT_CARE); + } + + auto* df = dynamic_cast(&nf); + if (df == nullptr) { + // Handle other types of NumberFormat using the old code, Apple restore for + UnicodeString formattedNumber; + StandardPlural::Form pluralForm = QuantityFormatter::selectPlural( + amtNumber, nf, **pluralRules, formattedNumber, pos, status); + if (posForFullNumericPart) { + pos.setField(UAMEASFMT_NUMERIC_FIELD_FLAG); + pos.setBeginIndex(0); + pos.setEndIndex(formattedNumber.length()); + } + UnicodeString pattern = number::impl::LongNameHandler::getUnitPattern(getLocale(status), + amtUnit, getUnitWidth(fWidth), pluralForm, status); + // The above already handles fallback from other widths to short + if (pattern.isBogus() && pluralForm != StandardPlural::Form::OTHER) { + pattern = number::impl::LongNameHandler::getUnitPattern(getLocale(status), + amtUnit, getUnitWidth(fWidth), StandardPlural::Form::OTHER, status); + } + if (U_FAILURE(status)) { + return appendTo; + } + SimpleFormatter formatter(pattern, 0, 1, status); + QuantityFormatter::format(formatter, formattedNumber, appendTo, pos, status); + } else { + // This is the current code + number::FormattedNumber result; + if (auto* lnf = df->toNumberFormatter(status)) { + result = lnf->unit(amtUnit) + .unitWidth(getUnitWidth(fWidth)) + .formatDouble(amtNumber.getDouble(status), status); + } + DecimalFormat::fieldPositionHelper(result, pos, appendTo.length(), status); + if (posForFullNumericPart) { + pos.setField(UNUM_INTEGER_FIELD); + DecimalFormat::fieldPositionHelper(result, pos, appendTo.length(), status); + int32_t intBegin = pos.getBeginIndex(); + int32_t intEnd = pos.getEndIndex(); + pos.setField(UNUM_FRACTION_FIELD); + DecimalFormat::fieldPositionHelper(result, pos, appendTo.length(), status); + int32_t fracBegin = pos.getBeginIndex(); + int32_t fracEnd = pos.getEndIndex(); + if (intBegin >= 0 && intEnd > intBegin) { + // we have an integer part + pos.setBeginIndex(intBegin); + if (fracBegin >= intEnd && fracEnd > fracBegin) { + // we have a fraction part, include it too + pos.setEndIndex(fracEnd); + } else { + pos.setEndIndex(intEnd); + } + } else if (fracBegin >= 0 && fracEnd > fracBegin) { + // only a fract part, use it + pos.setBeginIndex(fracBegin); + pos.setEndIndex(fracEnd); + } else { + // no numeric part + pos.setBeginIndex(0); + pos.setEndIndex(0); + } + pos.setField(UAMEASFMT_NUMERIC_FIELD_FLAG); + } + appendTo.append(result.toTempString(status)); + } + + if (stripPatternSpaces) { + // Get the narrow pattern for OTHER. + // If there are spaces in that, then do not continue to strip spaces + // (i.e. even in the narrowest form this locale keeps spaces). + UnicodeString narrowPattern = number::impl::LongNameHandler::getUnitPattern(getLocale(status), + amtUnit, UNUM_UNIT_WIDTH_NARROW, StandardPlural::Form::OTHER, status); + if (U_SUCCESS(status)) { + if (narrowPattern.indexOf((UChar)0x0020) == -1 && narrowPattern.indexOf((UChar)0x00A0) == -1) { + int32_t end = appendTo.length(); + for (; cur < end; cur++) { + if (appendTo.charAt(cur) == 0x0020) { + appendTo.remove(cur, 1); + if (pos.getBeginIndex() > cur) { + pos.setBeginIndex(pos.getBeginIndex() - 1); + pos.setEndIndex(pos.getEndIndex() - 1); + } + } + } + } + } } - return quantityFormatter->format( - amtNumber, - nf, - **pluralRules, - appendTo, - pos, - status); + return appendTo; } // Formats hours-minutes-seconds as 5:37:23 or similar. @@ -749,6 +1131,7 @@ UnicodeString &MeasureFormat::formatNumeric( const Formattable *hms, // always length 3 int32_t bitMap, // 1=hourset, 2=minuteset, 4=secondset UnicodeString &appendTo, + FieldPositionHandler& handler, UErrorCode &status) const { if (U_FAILURE(status)) { return appendTo; @@ -766,6 +1149,7 @@ UnicodeString &MeasureFormat::formatNumeric( UDAT_SECOND_FIELD, hms[2], appendTo, + handler, status); break; case 6: // ms @@ -775,6 +1159,7 @@ UnicodeString &MeasureFormat::formatNumeric( UDAT_SECOND_FIELD, hms[2], appendTo, + handler, status); break; case 3: // hm @@ -784,6 +1169,7 @@ UnicodeString &MeasureFormat::formatNumeric( UDAT_MINUTE_FIELD, hms[1], appendTo, + handler, status); break; default: @@ -791,7 +1177,6 @@ UnicodeString &MeasureFormat::formatNumeric( return appendTo; break; } - return appendTo; } static void appendRange( @@ -816,6 +1201,7 @@ UnicodeString &MeasureFormat::formatNumeric( UDateFormatField smallestField, // seconds in 5:37:23.5 const Formattable &smallestAmount, // 23.5 for 5:37:23.5 UnicodeString &appendTo, + FieldPositionHandler& handler, UErrorCode &status) const { if (U_FAILURE(status)) { return appendTo; @@ -837,9 +1223,44 @@ UnicodeString &MeasureFormat::formatNumeric( } // Format time. draft becomes something like '5:30:45' - FieldPosition smallestFieldPosition(smallestField); + // #13606: DateFormat is not thread-safe, but MeasureFormat advertises itself as thread-safe. + FieldPositionIterator posIter; UnicodeString draft; - dateFmt.format(date, draft, smallestFieldPosition, status); + static UMutex *dateFmtMutex = STATIC_NEW(UMutex); + umtx_lock(dateFmtMutex); + dateFmt.format(date, draft, &posIter, status); + umtx_unlock(dateFmtMutex); + + int32_t start = appendTo.length(); + FieldPosition smallestFieldPosition(smallestField); + FieldPosition fp; + int32_t measField = -1; + while (posIter.next(fp)) { + int32_t dateField = fp.getField(); + switch (dateField) { + case UDAT_HOUR_OF_DAY1_FIELD: + case UDAT_HOUR_OF_DAY0_FIELD: + case UDAT_HOUR1_FIELD: + case UDAT_HOUR0_FIELD: + measField = UAMEASUNIT_DURATION_HOUR; break; + case UDAT_MINUTE_FIELD: + measField = UAMEASUNIT_DURATION_MINUTE; break; + case UDAT_SECOND_FIELD: + measField = UAMEASUNIT_DURATION_SECOND; break; + default: + measField = -1; break; + } + if (dateField != smallestField) { + if (measField >= 0) { + handler.addAttribute(measField, start + fp.getBeginIndex(), start + fp.getEndIndex()); + handler.addAttribute(measField | UAMEASFMT_NUMERIC_FIELD_FLAG, start + fp.getBeginIndex(), start + fp.getEndIndex()); + } + } else { + smallestFieldPosition.setBeginIndex(fp.getBeginIndex()); + smallestFieldPosition.setEndIndex(fp.getEndIndex()); + break; + } + } // If we find field for smallest amount replace it with the formatted // smallest amount from above taking care to replace the integer part @@ -867,34 +1288,14 @@ UnicodeString &MeasureFormat::formatNumeric( draft, smallestFieldPosition.getEndIndex(), appendTo); + handler.addAttribute(measField, start + smallestFieldPosition.getBeginIndex(), appendTo.length()); + handler.addAttribute(measField | UAMEASFMT_NUMERIC_FIELD_FLAG, start + smallestFieldPosition.getBeginIndex(), appendTo.length()); } else { appendTo.append(draft); } return appendTo; } -const QuantityFormatter *MeasureFormat::getQuantityFormatter( - int32_t index, - int32_t widthIndex, - UErrorCode &status) const { - if (U_FAILURE(status)) { - return NULL; - } - const QuantityFormatter *formatters = - cache->formatters[index]; - if (formatters[widthIndex].isValid()) { - return &formatters[widthIndex]; - } - if (formatters[UMEASFMT_WIDTH_SHORT].isValid()) { - return &formatters[UMEASFMT_WIDTH_SHORT]; - } - if (formatters[UMEASFMT_WIDTH_WIDE].isValid()) { - return &formatters[UMEASFMT_WIDTH_WIDE]; - } - status = U_MISSING_RESOURCE_ERROR; - return NULL; -} - UnicodeString &MeasureFormat::formatMeasuresSlowTrack( const Measure *measures, int32_t measureCount, @@ -906,7 +1307,7 @@ UnicodeString &MeasureFormat::formatMeasuresSlowTrack( } FieldPosition dontCare(FieldPosition::DONT_CARE); FieldPosition fpos(pos.getField()); - UnicodeString *results = new UnicodeString[measureCount]; + LocalArray results(new UnicodeString[measureCount], status); int32_t fieldPositionFoundIndex = -1; for (int32_t i = 0; i < measureCount; ++i) { const NumberFormat *nf = cache->getIntegerFormat(); @@ -916,7 +1317,6 @@ UnicodeString &MeasureFormat::formatMeasuresSlowTrack( if (fieldPositionFoundIndex == -1) { formatMeasure(measures[i], *nf, results[i], fpos, status); if (U_FAILURE(status)) { - delete [] results; return appendTo; } if (fpos.getBeginIndex() != 0 || fpos.getEndIndex() != 0) { @@ -928,40 +1328,35 @@ UnicodeString &MeasureFormat::formatMeasuresSlowTrack( } int32_t offset; listFormatter->format( - results, + results.getAlias(), measureCount, appendTo, fieldPositionFoundIndex, offset, status); if (U_FAILURE(status)) { - delete [] results; return appendTo; } + // Fix up FieldPosition indexes if our field is found. if (offset != -1) { pos.setBeginIndex(fpos.getBeginIndex() + offset); pos.setEndIndex(fpos.getEndIndex() + offset); } - delete [] results; return appendTo; } MeasureFormat* U_EXPORT2 MeasureFormat::createCurrencyFormat(const Locale& locale, UErrorCode& ec) { - CurrencyFormat* fmt = NULL; - if (U_SUCCESS(ec)) { - fmt = new CurrencyFormat(locale, ec); - if (U_FAILURE(ec)) { - delete fmt; - fmt = NULL; - } + if (U_FAILURE(ec)) { + return nullptr; } - return fmt; + LocalPointer fmt(new CurrencyFormat(locale, ec), ec); + return fmt.orphan(); } MeasureFormat* U_EXPORT2 MeasureFormat::createCurrencyFormat(UErrorCode& ec) { if (U_FAILURE(ec)) { - return NULL; + return nullptr; } return MeasureFormat::createCurrencyFormat(Locale::getDefault(), ec); }