// update this code, refer to:
// http://site.icu-project.org/design/formatting/measureformat/updating-measure-unit
//
-// Start generated code
-// TODO(ICU-21076): improve how this generated code is produced.
+// Start generated code for measunit.cpp
// Maps from Type ID to offset in gSubTypes.
static const int32_t gOffsets[] = {
404,
408,
423,
- 426,
- 432,
- 442,
- 446,
+ 424,
+ 430,
+ 440,
+ 444,
+ 448,
450,
- 452,
- 486
+ 484
};
-// TODO: FIX CODE GENERATION - leaving this here but commented-out to make it
-// clear that we no longer want this array. We needed it for only one thing: efficient checking of "currency".
-//
-// static const int32_t gIndexes[] = {
-// 0,
-// 2,
-// 7,
-// 17,
-// 25,
-// 29,
-// 29,
-// 40,
-// 56,
-// 60,
-// 69,
-// 71,
-// 75,
-// 83,
-// 105,
-// 109,
-// 124,
-// 127,
-// 133,
-// 143,
-// 147,
-// 151,
-// 153,
-// 187
-// };
static const int32_t kCurrencyOffset = 5;
// Must be sorted alphabetically.
"solar-mass",
"stone",
"ton",
- "", // TODO(ICU-21076): manual edit of what should have been generated by Java.
- "percent", // TODO(ICU-21076): regenerate, deal with duplication.
- "permille", // TODO(ICU-21076): regenerate, deal with duplication.
+ "",
"gigawatt",
"horsepower",
"kilowatt",
"teaspoon"
};
-// unitPerUnitToSingleUnit no longer in use! TODO: remove from code-generation code.
-
// Shortcuts to the base unit in order to make the default constructor fast
static const int32_t kBaseTypeIdx = 16;
static const int32_t kBaseSubTypeIdx = 0;
return MeasureUnit(22, 33);
}
-// End generated code
+// End generated code for measunit.cpp
static int32_t binarySearch(
const char * const * array, int32_t start, int32_t end, StringPiece key) {
}
bool MeasureUnit::findBySubType(StringPiece subType, MeasureUnit* output) {
+ // Sanity checking kCurrencyOffset and final entry in gOffsets
+ U_ASSERT(uprv_strcmp(gTypes[kCurrencyOffset], "currency") == 0);
+ U_ASSERT(gOffsets[UPRV_LENGTHOF(gOffsets) - 1] == UPRV_LENGTHOF(gSubTypes));
+
for (int32_t t = 0; t < UPRV_LENGTHOF(gOffsets) - 1; t++) {
- // Ensure kCurrencyOffset is set correctly
- U_ASSERT(uprv_strcmp(gTypes[kCurrencyOffset], "currency") == 0);
// Skip currency units
if (t == kCurrencyOffset) {
continue;
fTypeId = result;
result = binarySearch(gSubTypes, gOffsets[fTypeId], gOffsets[fTypeId + 1], timeId);
U_ASSERT(result != -1);
- fSubTypeId = result - gOffsets[fTypeId];
+ fSubTypeId = result - gOffsets[fTypeId];
}
void MeasureUnit::initCurrency(StringPiece isoCurrency) {
* Constant for unit of graphics: dot
* @draft ICU 68
* @provisional This API might change or be removed in a future release.
- */
+ */
public static final MeasureUnit DOT = MeasureUnit.internalGetInstance("graphics", "dot");
/**
* Constant for unit of length: earth-radius
* @draft ICU 68
* @provisional This API might change or be removed in a future release.
- */
+ */
public static final MeasureUnit EARTH_RADIUS = MeasureUnit.internalGetInstance("length", "earth-radius");
/**
* Constant for unit of light: candela
* @draft ICU 68
* @provisional This API might change or be removed in a future release.
- */
+ */
public static final MeasureUnit CANDELA = MeasureUnit.internalGetInstance("light", "candela");
/**
* Constant for unit of light: lumen
* @draft ICU 68
* @provisional This API might change or be removed in a future release.
- */
+ */
public static final MeasureUnit LUMEN = MeasureUnit.internalGetInstance("light", "lumen");
/**
* Constant for unit of mass: grain
* @draft ICU 68
* @provisional This API might change or be removed in a future release.
- */
+ */
public static final MeasureUnit GRAIN = MeasureUnit.internalGetInstance("mass", "grain");
/**
* Constant for unit of volume: dessert-spoon
* @draft ICU 68
* @provisional This API might change or be removed in a future release.
- */
+ */
public static final MeasureUnit DESSERT_SPOON = MeasureUnit.internalGetInstance("volume", "dessert-spoon");
/**
* Constant for unit of volume: dessert-spoon-imperial
* @draft ICU 68
* @provisional This API might change or be removed in a future release.
- */
+ */
public static final MeasureUnit DESSERT_SPOON_IMPERIAL = MeasureUnit.internalGetInstance("volume", "dessert-spoon-imperial");
/**
* Constant for unit of volume: dram
* @draft ICU 68
* @provisional This API might change or be removed in a future release.
- */
+ */
public static final MeasureUnit DRAM = MeasureUnit.internalGetInstance("volume", "dram");
/**
* Constant for unit of volume: drop
* @draft ICU 68
* @provisional This API might change or be removed in a future release.
- */
+ */
public static final MeasureUnit DROP = MeasureUnit.internalGetInstance("volume", "drop");
/**
* Constant for unit of volume: jigger
* @draft ICU 68
* @provisional This API might change or be removed in a future release.
- */
+ */
public static final MeasureUnit JIGGER = MeasureUnit.internalGetInstance("volume", "jigger");
/**
* Constant for unit of volume: pinch
* @draft ICU 68
* @provisional This API might change or be removed in a future release.
- */
+ */
public static final MeasureUnit PINCH = MeasureUnit.internalGetInstance("volume", "pinch");
/**
* Constant for unit of volume: quart-imperial
* @draft ICU 68
* @provisional This API might change or be removed in a future release.
- */
+ */
public static final MeasureUnit QUART_IMPERIAL = MeasureUnit.internalGetInstance("volume", "quart-imperial");
/**
*/
public static final MeasureUnit TEASPOON = MeasureUnit.internalGetInstance("volume", "teaspoon");
- // unitPerUnitToSingleUnit no longer in use! TODO: remove from code-generation code.
-
// End generated MeasureUnit constants
+
/* Private */
private Object writeReplace() throws ObjectStreamException {
// for MeasureFormat during the release process.
static void generateCXXHConstants(String thisVersion) {
Map<String, MeasureUnit> seen = new HashMap<>();
+ System.out.println("// Start generated createXXX methods");
System.out.println();
TreeMap<String, List<MeasureUnit>> allUnits = getAllUnits();
for (Map.Entry<String, List<MeasureUnit>> entry : allUnits.entrySet()) {
System.out.println(" /**");
System.out.println(" * Returns by value, unit of " + type + ": " + code + ".");
System.out.printf(" * Also see {@link #create%s()}.\n", name);
- // TODO: When the get* methods become stable in ICU 66, update their
- // @draft code to be more like that for the create* methods above.
String getterVersion = getVersion(javaName, thisVersion);
if (Integer.valueOf(getterVersion) < 64) {
getterVersion = "64";
}
- System.out.println(" * @draft ICU " + getterVersion);
+ if (isDraft(javaName)) {
+ System.out.println(" * @draft ICU " + getterVersion);
+ } else {
+ System.out.println(" * @stable ICU " + getterVersion);
+ }
System.out.println(" */");
System.out.printf(" static MeasureUnit get%s();\n", name);
if (isDraft(javaName)) {
System.out.println("");
}
}
+ System.out.println("// End generated createXXX methods");
}
private static void checkForDup(
// DO NOT DELETE THIS FUNCTION! It may appear as dead code, but we use this to generate code
// for MeasureFormat during the release process.
static void generateCXXConstants() {
+ System.out.println("// Start generated code for measunit.cpp");
System.out.println("");
TreeMap<String, List<MeasureUnit>> allUnits = getAllUnits();
- // Hack: for C++, add NoUnits here, but ignore them when printing the create methods.
- // ALso keep track of the base unit offset to make the C++ default constructor faster.
- allUnits.put("none", Arrays.asList(new MeasureUnit[]{NoUnit.BASE, NoUnit.PERCENT, NoUnit.PERMILLE}));
+ // Hack: for C++, add base unit here, but ignore them when printing the create methods.
+ // Also keep track of the base unit offset to make the C++ default constructor faster.
+ allUnits.put("none", Arrays.asList(new MeasureUnit[] {NoUnit.BASE}));
int baseTypeIdx = -1;
int baseSubTypeIdx = -1;
+ System.out.println("// Maps from Type ID to offset in gSubTypes.");
System.out.println("static const int32_t gOffsets[] = {");
int index = 0;
+ int typeCount = 0;
+ int currencyIndex = -1;
for (Map.Entry<String, List<MeasureUnit>> entry : allUnits.entrySet()) {
System.out.printf(" %d,\n", index);
+ if (entry.getKey() == "currency") {
+ currencyIndex = typeCount;
+ }
+ typeCount++;
index += entry.getValue().size();
}
+ assertTrue("currency present", currencyIndex >= 0);
System.out.printf(" %d\n", index);
System.out.println("};");
System.out.println();
- System.out.println("static const int32_t gIndexes[] = {");
- index = 0;
- for (Map.Entry<String, List<MeasureUnit>> entry : allUnits.entrySet()) {
- System.out.printf(" %d,\n", index);
- if (!entry.getKey().equals("currency")) {
- index += entry.getValue().size();
- }
- }
- System.out.printf(" %d\n", index);
- System.out.println("};");
+ System.out.println("static const int32_t kCurrencyOffset = " + currencyIndex + ";");
System.out.println();
System.out.println("// Must be sorted alphabetically.");
System.out.println("static const char * const gTypes[] = {");
if (!first) {
System.out.println(",");
}
- System.out.print(" \"" + unit.getSubtype() + "\"");
+ if (unit != null) {
+ System.out.print(" \"" + unit.getSubtype() + "\"");
+ } else {
+ assertEquals("unit only null for \"none\" type", "none", entry.getKey());
+ System.out.print(" \"\"");
+ }
first = false;
measureUnitToOffset.put(unit, offset);
measureUnitToTypeSubType.put(unit, Pair.of(typeIdx, subTypeIdx));
measureUnitToTypeSubType.get(entry.getKey()));
}
- System.out.println("// Must be sorted by first value and then second value.");
- System.out.println("static int32_t unitPerUnitToSingleUnit[][4] = {");
- first = true;
- for (Map.Entry<OrderedPair<Integer, Integer>, Pair<Integer, Integer>> entry
- : unitPerUnitOffsetsToTypeSubType.entrySet()) {
- if (!first) {
- System.out.println(",");
- }
- first = false;
- OrderedPair<Integer, Integer> unitPerUnitOffsets = entry.getKey();
- Pair<Integer, Integer> typeSubType = entry.getValue();
- System.out.printf(" {%d, %d, %d, %d}",
- unitPerUnitOffsets.first,
- unitPerUnitOffsets.second,
- typeSubType.first,
- typeSubType.second);
- }
- System.out.println();
- System.out.println("};");
- System.out.println();
-
// Print out the fast-path for the default constructor
System.out.println("// Shortcuts to the base unit in order to make the default constructor fast");
System.out.println("static const int32_t kBaseTypeIdx = " + baseTypeIdx + ";");
System.out.println();
}
}
+ System.out.println("// End generated code for measunit.cpp");
}
private static String toCamelCase(MeasureUnit unit) {
// DO NOT DELETE THIS FUNCTION! It may appear as dead code, but we use this to generate code
// for MeasureFormat during the release process.
static void generateConstants(String thisVersion) {
+ System.out.println(" // Start generated MeasureUnit constants");
System.out.println();
Map<String, MeasureUnit> seen = new HashMap<>();
TreeMap<String, List<MeasureUnit>> allUnits = getAllUnits();
} else {
System.out.println(" * @stable ICU " + getVersion(name, thisVersion));
}
- System.out.println(" */");
+ System.out.println(" */");
if ("duration".equals(type) && TIME_CODES.contains(code)) {
System.out.println(" public static final TimeUnit " + name + " = (TimeUnit) MeasureUnit.internalGetInstance(\"" +
type +
System.out.println();
}
}
- System.out.println(" private static HashMap<Pair<MeasureUnit, MeasureUnit>, MeasureUnit>unitPerUnitToSingleUnit =");
- System.out.println(" new HashMap<Pair<MeasureUnit, MeasureUnit>, MeasureUnit>();");
- System.out.println();
- System.out.println(" static {");
- for (Map.Entry<MeasureUnit, Pair<MeasureUnit, MeasureUnit>> unitPerUnitEntry
- : getUnitsToPerParts().entrySet()) {
- Pair<MeasureUnit, MeasureUnit> unitPerUnit = unitPerUnitEntry.getValue();
- System.out.println(" unitPerUnitToSingleUnit.put(Pair.<MeasureUnit, MeasureUnit>of(MeasureUnit." + toJAVAName(unitPerUnit.first) + ", MeasureUnit." + toJAVAName(unitPerUnit.second) + "), MeasureUnit." + toJAVAName(unitPerUnitEntry.getKey()) + ");");
- }
- System.out.println(" }");
+ System.out.println(" // End generated MeasureUnit constants");
}
private static String getVersion(String javaName, String thisVersion) {