1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 **********************************************************************
5 * Copyright (c) 2004-2016, International Business Machines
6 * Corporation and others. All Rights Reserved.
7 **********************************************************************
9 * Created: April 26, 2004
11 **********************************************************************
13 #include "utypeinfo.h" // for 'typeid' to work
15 #include "unicode/measunit.h"
17 #if !UCONFIG_NO_FORMATTING
19 #include "unicode/uenum.h"
26 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(MeasureUnit
)
28 // All code between the "Start generated code" comment and
29 // the "End generated code" comment is auto generated code
30 // and must not be edited manually. For instructions on how to correctly
31 // update this code, refer to:
32 // http://site.icu-project.org/design/formatting/measureformat/updating-measure-unit
34 // Start generated code
37 static const int32_t gOffsets
[] = {
61 static const int32_t gIndexes
[] = {
85 // Must be sorted alphabetically.
86 static const char * const gTypes
[] = {
109 // Must be grouped by type and sorted alphabetically within each type.
110 static const char * const gSubTypes
[] = {
112 "meter-per-second-squared",
128 "milligram-per-deciliter",
129 "millimole-per-liter",
131 "liter-per-100kilometers",
132 "liter-per-kilometer",
134 "mile-per-gallon-imperial",
512 "millimeter-of-mercury",
513 "pound-per-square-inch",
514 "kilometer-per-hour",
549 // Must be sorted by first value and then second value.
550 static int32_t unitPerUnitToSingleUnit
[][4] = {
561 // Shortcuts to the base unit in order to make the default constructor fast
562 static const int32_t kBaseTypeIdx
= 14;
563 static const int32_t kBaseSubTypeIdx
= 0;
565 MeasureUnit
*MeasureUnit::createGForce(UErrorCode
&status
) {
566 return MeasureUnit::create(0, 0, status
);
569 MeasureUnit
*MeasureUnit::createMeterPerSecondSquared(UErrorCode
&status
) {
570 return MeasureUnit::create(0, 1, status
);
573 MeasureUnit
*MeasureUnit::createArcMinute(UErrorCode
&status
) {
574 return MeasureUnit::create(1, 0, status
);
577 MeasureUnit
*MeasureUnit::createArcSecond(UErrorCode
&status
) {
578 return MeasureUnit::create(1, 1, status
);
581 MeasureUnit
*MeasureUnit::createDegree(UErrorCode
&status
) {
582 return MeasureUnit::create(1, 2, status
);
585 MeasureUnit
*MeasureUnit::createRadian(UErrorCode
&status
) {
586 return MeasureUnit::create(1, 3, status
);
589 MeasureUnit
*MeasureUnit::createRevolutionAngle(UErrorCode
&status
) {
590 return MeasureUnit::create(1, 4, status
);
593 MeasureUnit
*MeasureUnit::createAcre(UErrorCode
&status
) {
594 return MeasureUnit::create(2, 0, status
);
597 MeasureUnit
*MeasureUnit::createHectare(UErrorCode
&status
) {
598 return MeasureUnit::create(2, 1, status
);
601 MeasureUnit
*MeasureUnit::createSquareCentimeter(UErrorCode
&status
) {
602 return MeasureUnit::create(2, 2, status
);
605 MeasureUnit
*MeasureUnit::createSquareFoot(UErrorCode
&status
) {
606 return MeasureUnit::create(2, 3, status
);
609 MeasureUnit
*MeasureUnit::createSquareInch(UErrorCode
&status
) {
610 return MeasureUnit::create(2, 4, status
);
613 MeasureUnit
*MeasureUnit::createSquareKilometer(UErrorCode
&status
) {
614 return MeasureUnit::create(2, 5, status
);
617 MeasureUnit
*MeasureUnit::createSquareMeter(UErrorCode
&status
) {
618 return MeasureUnit::create(2, 6, status
);
621 MeasureUnit
*MeasureUnit::createSquareMile(UErrorCode
&status
) {
622 return MeasureUnit::create(2, 7, status
);
625 MeasureUnit
*MeasureUnit::createSquareYard(UErrorCode
&status
) {
626 return MeasureUnit::create(2, 8, status
);
629 MeasureUnit
*MeasureUnit::createKarat(UErrorCode
&status
) {
630 return MeasureUnit::create(3, 0, status
);
633 MeasureUnit
*MeasureUnit::createMilligramPerDeciliter(UErrorCode
&status
) {
634 return MeasureUnit::create(3, 1, status
);
637 MeasureUnit
*MeasureUnit::createMillimolePerLiter(UErrorCode
&status
) {
638 return MeasureUnit::create(3, 2, status
);
641 MeasureUnit
*MeasureUnit::createPartPerMillion(UErrorCode
&status
) {
642 return MeasureUnit::create(3, 3, status
);
645 MeasureUnit
*MeasureUnit::createLiterPer100Kilometers(UErrorCode
&status
) {
646 return MeasureUnit::create(4, 0, status
);
649 MeasureUnit
*MeasureUnit::createLiterPerKilometer(UErrorCode
&status
) {
650 return MeasureUnit::create(4, 1, status
);
653 MeasureUnit
*MeasureUnit::createMilePerGallon(UErrorCode
&status
) {
654 return MeasureUnit::create(4, 2, status
);
657 MeasureUnit
*MeasureUnit::createMilePerGallonImperial(UErrorCode
&status
) {
658 return MeasureUnit::create(4, 3, status
);
661 MeasureUnit
*MeasureUnit::createBit(UErrorCode
&status
) {
662 return MeasureUnit::create(6, 0, status
);
665 MeasureUnit
*MeasureUnit::createByte(UErrorCode
&status
) {
666 return MeasureUnit::create(6, 1, status
);
669 MeasureUnit
*MeasureUnit::createGigabit(UErrorCode
&status
) {
670 return MeasureUnit::create(6, 2, status
);
673 MeasureUnit
*MeasureUnit::createGigabyte(UErrorCode
&status
) {
674 return MeasureUnit::create(6, 3, status
);
677 MeasureUnit
*MeasureUnit::createKilobit(UErrorCode
&status
) {
678 return MeasureUnit::create(6, 4, status
);
681 MeasureUnit
*MeasureUnit::createKilobyte(UErrorCode
&status
) {
682 return MeasureUnit::create(6, 5, status
);
685 MeasureUnit
*MeasureUnit::createMegabit(UErrorCode
&status
) {
686 return MeasureUnit::create(6, 6, status
);
689 MeasureUnit
*MeasureUnit::createMegabyte(UErrorCode
&status
) {
690 return MeasureUnit::create(6, 7, status
);
693 MeasureUnit
*MeasureUnit::createTerabit(UErrorCode
&status
) {
694 return MeasureUnit::create(6, 8, status
);
697 MeasureUnit
*MeasureUnit::createTerabyte(UErrorCode
&status
) {
698 return MeasureUnit::create(6, 9, status
);
701 MeasureUnit
*MeasureUnit::createCentury(UErrorCode
&status
) {
702 return MeasureUnit::create(7, 0, status
);
705 MeasureUnit
*MeasureUnit::createDay(UErrorCode
&status
) {
706 return MeasureUnit::create(7, 1, status
);
709 MeasureUnit
*MeasureUnit::createHour(UErrorCode
&status
) {
710 return MeasureUnit::create(7, 2, status
);
713 MeasureUnit
*MeasureUnit::createMicrosecond(UErrorCode
&status
) {
714 return MeasureUnit::create(7, 3, status
);
717 MeasureUnit
*MeasureUnit::createMillisecond(UErrorCode
&status
) {
718 return MeasureUnit::create(7, 4, status
);
721 MeasureUnit
*MeasureUnit::createMinute(UErrorCode
&status
) {
722 return MeasureUnit::create(7, 5, status
);
725 MeasureUnit
*MeasureUnit::createMonth(UErrorCode
&status
) {
726 return MeasureUnit::create(7, 6, status
);
729 MeasureUnit
*MeasureUnit::createNanosecond(UErrorCode
&status
) {
730 return MeasureUnit::create(7, 7, status
);
733 MeasureUnit
*MeasureUnit::createSecond(UErrorCode
&status
) {
734 return MeasureUnit::create(7, 8, status
);
737 MeasureUnit
*MeasureUnit::createWeek(UErrorCode
&status
) {
738 return MeasureUnit::create(7, 9, status
);
741 MeasureUnit
*MeasureUnit::createYear(UErrorCode
&status
) {
742 return MeasureUnit::create(7, 10, status
);
745 MeasureUnit
*MeasureUnit::createAmpere(UErrorCode
&status
) {
746 return MeasureUnit::create(8, 0, status
);
749 MeasureUnit
*MeasureUnit::createMilliampere(UErrorCode
&status
) {
750 return MeasureUnit::create(8, 1, status
);
753 MeasureUnit
*MeasureUnit::createOhm(UErrorCode
&status
) {
754 return MeasureUnit::create(8, 2, status
);
757 MeasureUnit
*MeasureUnit::createVolt(UErrorCode
&status
) {
758 return MeasureUnit::create(8, 3, status
);
761 MeasureUnit
*MeasureUnit::createCalorie(UErrorCode
&status
) {
762 return MeasureUnit::create(9, 0, status
);
765 MeasureUnit
*MeasureUnit::createFoodcalorie(UErrorCode
&status
) {
766 return MeasureUnit::create(9, 1, status
);
769 MeasureUnit
*MeasureUnit::createJoule(UErrorCode
&status
) {
770 return MeasureUnit::create(9, 2, status
);
773 MeasureUnit
*MeasureUnit::createKilocalorie(UErrorCode
&status
) {
774 return MeasureUnit::create(9, 3, status
);
777 MeasureUnit
*MeasureUnit::createKilojoule(UErrorCode
&status
) {
778 return MeasureUnit::create(9, 4, status
);
781 MeasureUnit
*MeasureUnit::createKilowattHour(UErrorCode
&status
) {
782 return MeasureUnit::create(9, 5, status
);
785 MeasureUnit
*MeasureUnit::createGigahertz(UErrorCode
&status
) {
786 return MeasureUnit::create(10, 0, status
);
789 MeasureUnit
*MeasureUnit::createHertz(UErrorCode
&status
) {
790 return MeasureUnit::create(10, 1, status
);
793 MeasureUnit
*MeasureUnit::createKilohertz(UErrorCode
&status
) {
794 return MeasureUnit::create(10, 2, status
);
797 MeasureUnit
*MeasureUnit::createMegahertz(UErrorCode
&status
) {
798 return MeasureUnit::create(10, 3, status
);
801 MeasureUnit
*MeasureUnit::createAstronomicalUnit(UErrorCode
&status
) {
802 return MeasureUnit::create(11, 0, status
);
805 MeasureUnit
*MeasureUnit::createCentimeter(UErrorCode
&status
) {
806 return MeasureUnit::create(11, 1, status
);
809 MeasureUnit
*MeasureUnit::createDecimeter(UErrorCode
&status
) {
810 return MeasureUnit::create(11, 2, status
);
813 MeasureUnit
*MeasureUnit::createFathom(UErrorCode
&status
) {
814 return MeasureUnit::create(11, 3, status
);
817 MeasureUnit
*MeasureUnit::createFoot(UErrorCode
&status
) {
818 return MeasureUnit::create(11, 4, status
);
821 MeasureUnit
*MeasureUnit::createFurlong(UErrorCode
&status
) {
822 return MeasureUnit::create(11, 5, status
);
825 MeasureUnit
*MeasureUnit::createInch(UErrorCode
&status
) {
826 return MeasureUnit::create(11, 6, status
);
829 MeasureUnit
*MeasureUnit::createKilometer(UErrorCode
&status
) {
830 return MeasureUnit::create(11, 7, status
);
833 MeasureUnit
*MeasureUnit::createLightYear(UErrorCode
&status
) {
834 return MeasureUnit::create(11, 8, status
);
837 MeasureUnit
*MeasureUnit::createMeter(UErrorCode
&status
) {
838 return MeasureUnit::create(11, 9, status
);
841 MeasureUnit
*MeasureUnit::createMicrometer(UErrorCode
&status
) {
842 return MeasureUnit::create(11, 10, status
);
845 MeasureUnit
*MeasureUnit::createMile(UErrorCode
&status
) {
846 return MeasureUnit::create(11, 11, status
);
849 MeasureUnit
*MeasureUnit::createMileScandinavian(UErrorCode
&status
) {
850 return MeasureUnit::create(11, 12, status
);
853 MeasureUnit
*MeasureUnit::createMillimeter(UErrorCode
&status
) {
854 return MeasureUnit::create(11, 13, status
);
857 MeasureUnit
*MeasureUnit::createNanometer(UErrorCode
&status
) {
858 return MeasureUnit::create(11, 14, status
);
861 MeasureUnit
*MeasureUnit::createNauticalMile(UErrorCode
&status
) {
862 return MeasureUnit::create(11, 15, status
);
865 MeasureUnit
*MeasureUnit::createParsec(UErrorCode
&status
) {
866 return MeasureUnit::create(11, 16, status
);
869 MeasureUnit
*MeasureUnit::createPicometer(UErrorCode
&status
) {
870 return MeasureUnit::create(11, 17, status
);
873 MeasureUnit
*MeasureUnit::createPoint(UErrorCode
&status
) {
874 return MeasureUnit::create(11, 18, status
);
877 MeasureUnit
*MeasureUnit::createYard(UErrorCode
&status
) {
878 return MeasureUnit::create(11, 19, status
);
881 MeasureUnit
*MeasureUnit::createLux(UErrorCode
&status
) {
882 return MeasureUnit::create(12, 0, status
);
885 MeasureUnit
*MeasureUnit::createCarat(UErrorCode
&status
) {
886 return MeasureUnit::create(13, 0, status
);
889 MeasureUnit
*MeasureUnit::createGram(UErrorCode
&status
) {
890 return MeasureUnit::create(13, 1, status
);
893 MeasureUnit
*MeasureUnit::createKilogram(UErrorCode
&status
) {
894 return MeasureUnit::create(13, 2, status
);
897 MeasureUnit
*MeasureUnit::createMetricTon(UErrorCode
&status
) {
898 return MeasureUnit::create(13, 3, status
);
901 MeasureUnit
*MeasureUnit::createMicrogram(UErrorCode
&status
) {
902 return MeasureUnit::create(13, 4, status
);
905 MeasureUnit
*MeasureUnit::createMilligram(UErrorCode
&status
) {
906 return MeasureUnit::create(13, 5, status
);
909 MeasureUnit
*MeasureUnit::createOunce(UErrorCode
&status
) {
910 return MeasureUnit::create(13, 6, status
);
913 MeasureUnit
*MeasureUnit::createOunceTroy(UErrorCode
&status
) {
914 return MeasureUnit::create(13, 7, status
);
917 MeasureUnit
*MeasureUnit::createPound(UErrorCode
&status
) {
918 return MeasureUnit::create(13, 8, status
);
921 MeasureUnit
*MeasureUnit::createStone(UErrorCode
&status
) {
922 return MeasureUnit::create(13, 9, status
);
925 MeasureUnit
*MeasureUnit::createTon(UErrorCode
&status
) {
926 return MeasureUnit::create(13, 10, status
);
929 MeasureUnit
*MeasureUnit::createGigawatt(UErrorCode
&status
) {
930 return MeasureUnit::create(15, 0, status
);
933 MeasureUnit
*MeasureUnit::createHorsepower(UErrorCode
&status
) {
934 return MeasureUnit::create(15, 1, status
);
937 MeasureUnit
*MeasureUnit::createKilowatt(UErrorCode
&status
) {
938 return MeasureUnit::create(15, 2, status
);
941 MeasureUnit
*MeasureUnit::createMegawatt(UErrorCode
&status
) {
942 return MeasureUnit::create(15, 3, status
);
945 MeasureUnit
*MeasureUnit::createMilliwatt(UErrorCode
&status
) {
946 return MeasureUnit::create(15, 4, status
);
949 MeasureUnit
*MeasureUnit::createWatt(UErrorCode
&status
) {
950 return MeasureUnit::create(15, 5, status
);
953 MeasureUnit
*MeasureUnit::createHectopascal(UErrorCode
&status
) {
954 return MeasureUnit::create(16, 0, status
);
957 MeasureUnit
*MeasureUnit::createInchHg(UErrorCode
&status
) {
958 return MeasureUnit::create(16, 1, status
);
961 MeasureUnit
*MeasureUnit::createMillibar(UErrorCode
&status
) {
962 return MeasureUnit::create(16, 2, status
);
965 MeasureUnit
*MeasureUnit::createMillimeterOfMercury(UErrorCode
&status
) {
966 return MeasureUnit::create(16, 3, status
);
969 MeasureUnit
*MeasureUnit::createPoundPerSquareInch(UErrorCode
&status
) {
970 return MeasureUnit::create(16, 4, status
);
973 MeasureUnit
*MeasureUnit::createKilometerPerHour(UErrorCode
&status
) {
974 return MeasureUnit::create(17, 0, status
);
977 MeasureUnit
*MeasureUnit::createKnot(UErrorCode
&status
) {
978 return MeasureUnit::create(17, 1, status
);
981 MeasureUnit
*MeasureUnit::createMeterPerSecond(UErrorCode
&status
) {
982 return MeasureUnit::create(17, 2, status
);
985 MeasureUnit
*MeasureUnit::createMilePerHour(UErrorCode
&status
) {
986 return MeasureUnit::create(17, 3, status
);
989 MeasureUnit
*MeasureUnit::createCelsius(UErrorCode
&status
) {
990 return MeasureUnit::create(18, 0, status
);
993 MeasureUnit
*MeasureUnit::createFahrenheit(UErrorCode
&status
) {
994 return MeasureUnit::create(18, 1, status
);
997 MeasureUnit
*MeasureUnit::createGenericTemperature(UErrorCode
&status
) {
998 return MeasureUnit::create(18, 2, status
);
1001 MeasureUnit
*MeasureUnit::createKelvin(UErrorCode
&status
) {
1002 return MeasureUnit::create(18, 3, status
);
1005 MeasureUnit
*MeasureUnit::createAcreFoot(UErrorCode
&status
) {
1006 return MeasureUnit::create(19, 0, status
);
1009 MeasureUnit
*MeasureUnit::createBushel(UErrorCode
&status
) {
1010 return MeasureUnit::create(19, 1, status
);
1013 MeasureUnit
*MeasureUnit::createCentiliter(UErrorCode
&status
) {
1014 return MeasureUnit::create(19, 2, status
);
1017 MeasureUnit
*MeasureUnit::createCubicCentimeter(UErrorCode
&status
) {
1018 return MeasureUnit::create(19, 3, status
);
1021 MeasureUnit
*MeasureUnit::createCubicFoot(UErrorCode
&status
) {
1022 return MeasureUnit::create(19, 4, status
);
1025 MeasureUnit
*MeasureUnit::createCubicInch(UErrorCode
&status
) {
1026 return MeasureUnit::create(19, 5, status
);
1029 MeasureUnit
*MeasureUnit::createCubicKilometer(UErrorCode
&status
) {
1030 return MeasureUnit::create(19, 6, status
);
1033 MeasureUnit
*MeasureUnit::createCubicMeter(UErrorCode
&status
) {
1034 return MeasureUnit::create(19, 7, status
);
1037 MeasureUnit
*MeasureUnit::createCubicMile(UErrorCode
&status
) {
1038 return MeasureUnit::create(19, 8, status
);
1041 MeasureUnit
*MeasureUnit::createCubicYard(UErrorCode
&status
) {
1042 return MeasureUnit::create(19, 9, status
);
1045 MeasureUnit
*MeasureUnit::createCup(UErrorCode
&status
) {
1046 return MeasureUnit::create(19, 10, status
);
1049 MeasureUnit
*MeasureUnit::createCupMetric(UErrorCode
&status
) {
1050 return MeasureUnit::create(19, 11, status
);
1053 MeasureUnit
*MeasureUnit::createDeciliter(UErrorCode
&status
) {
1054 return MeasureUnit::create(19, 12, status
);
1057 MeasureUnit
*MeasureUnit::createFluidOunce(UErrorCode
&status
) {
1058 return MeasureUnit::create(19, 13, status
);
1061 MeasureUnit
*MeasureUnit::createGallon(UErrorCode
&status
) {
1062 return MeasureUnit::create(19, 14, status
);
1065 MeasureUnit
*MeasureUnit::createGallonImperial(UErrorCode
&status
) {
1066 return MeasureUnit::create(19, 15, status
);
1069 MeasureUnit
*MeasureUnit::createHectoliter(UErrorCode
&status
) {
1070 return MeasureUnit::create(19, 16, status
);
1073 MeasureUnit
*MeasureUnit::createLiter(UErrorCode
&status
) {
1074 return MeasureUnit::create(19, 17, status
);
1077 MeasureUnit
*MeasureUnit::createMegaliter(UErrorCode
&status
) {
1078 return MeasureUnit::create(19, 18, status
);
1081 MeasureUnit
*MeasureUnit::createMilliliter(UErrorCode
&status
) {
1082 return MeasureUnit::create(19, 19, status
);
1085 MeasureUnit
*MeasureUnit::createPint(UErrorCode
&status
) {
1086 return MeasureUnit::create(19, 20, status
);
1089 MeasureUnit
*MeasureUnit::createPintMetric(UErrorCode
&status
) {
1090 return MeasureUnit::create(19, 21, status
);
1093 MeasureUnit
*MeasureUnit::createQuart(UErrorCode
&status
) {
1094 return MeasureUnit::create(19, 22, status
);
1097 MeasureUnit
*MeasureUnit::createTablespoon(UErrorCode
&status
) {
1098 return MeasureUnit::create(19, 23, status
);
1101 MeasureUnit
*MeasureUnit::createTeaspoon(UErrorCode
&status
) {
1102 return MeasureUnit::create(19, 24, status
);
1105 // End generated code
1107 static int32_t binarySearch(
1108 const char * const * array
, int32_t start
, int32_t end
, const char * key
) {
1109 while (start
< end
) {
1110 int32_t mid
= (start
+ end
) / 2;
1111 int32_t cmp
= uprv_strcmp(array
[mid
], key
);
1124 MeasureUnit::MeasureUnit() {
1126 fTypeId
= kBaseTypeIdx
;
1127 fSubTypeId
= kBaseSubTypeIdx
;
1130 MeasureUnit::MeasureUnit(const MeasureUnit
&other
)
1131 : fTypeId(other
.fTypeId
), fSubTypeId(other
.fSubTypeId
) {
1132 uprv_strcpy(fCurrency
, other
.fCurrency
);
1135 MeasureUnit
&MeasureUnit::operator=(const MeasureUnit
&other
) {
1136 if (this == &other
) {
1139 fTypeId
= other
.fTypeId
;
1140 fSubTypeId
= other
.fSubTypeId
;
1141 uprv_strcpy(fCurrency
, other
.fCurrency
);
1145 UObject
*MeasureUnit::clone() const {
1146 return new MeasureUnit(*this);
1149 MeasureUnit::~MeasureUnit() {
1152 const char *MeasureUnit::getType() const {
1153 return gTypes
[fTypeId
];
1156 const char *MeasureUnit::getSubtype() const {
1157 return fCurrency
[0] == 0 ? gSubTypes
[getOffset()] : fCurrency
;
1160 UBool
MeasureUnit::operator==(const UObject
& other
) const {
1161 if (this == &other
) { // Same object, equal
1164 if (typeid(*this) != typeid(other
)) { // Different types, not equal
1167 const MeasureUnit
&rhs
= static_cast<const MeasureUnit
&>(other
);
1169 fTypeId
== rhs
.fTypeId
1170 && fSubTypeId
== rhs
.fSubTypeId
1171 && uprv_strcmp(fCurrency
, rhs
.fCurrency
) == 0);
1174 int32_t MeasureUnit::getIndex() const {
1175 return gIndexes
[fTypeId
] + fSubTypeId
;
1178 int32_t MeasureUnit::getAvailable(
1180 int32_t destCapacity
,
1181 UErrorCode
&errorCode
) {
1182 if (U_FAILURE(errorCode
)) {
1185 if (destCapacity
< UPRV_LENGTHOF(gSubTypes
)) {
1186 errorCode
= U_BUFFER_OVERFLOW_ERROR
;
1187 return UPRV_LENGTHOF(gSubTypes
);
1190 for (int32_t typeIdx
= 0; typeIdx
< UPRV_LENGTHOF(gTypes
); ++typeIdx
) {
1191 int32_t len
= gOffsets
[typeIdx
+ 1] - gOffsets
[typeIdx
];
1192 for (int32_t subTypeIdx
= 0; subTypeIdx
< len
; ++subTypeIdx
) {
1193 dest
[idx
].setTo(typeIdx
, subTypeIdx
);
1197 U_ASSERT(idx
== UPRV_LENGTHOF(gSubTypes
));
1198 return UPRV_LENGTHOF(gSubTypes
);
1201 int32_t MeasureUnit::getAvailable(
1204 int32_t destCapacity
,
1205 UErrorCode
&errorCode
) {
1206 if (U_FAILURE(errorCode
)) {
1209 int32_t typeIdx
= binarySearch(gTypes
, 0, UPRV_LENGTHOF(gTypes
), type
);
1210 if (typeIdx
== -1) {
1213 int32_t len
= gOffsets
[typeIdx
+ 1] - gOffsets
[typeIdx
];
1214 if (destCapacity
< len
) {
1215 errorCode
= U_BUFFER_OVERFLOW_ERROR
;
1218 for (int subTypeIdx
= 0; subTypeIdx
< len
; ++subTypeIdx
) {
1219 dest
[subTypeIdx
].setTo(typeIdx
, subTypeIdx
);
1224 StringEnumeration
* MeasureUnit::getAvailableTypes(UErrorCode
&errorCode
) {
1225 UEnumeration
*uenum
= uenum_openCharStringsEnumeration(
1226 gTypes
, UPRV_LENGTHOF(gTypes
), &errorCode
);
1227 if (U_FAILURE(errorCode
)) {
1231 StringEnumeration
*result
= new UStringEnumeration(uenum
);
1232 if (result
== NULL
) {
1233 errorCode
= U_MEMORY_ALLOCATION_ERROR
;
1240 int32_t MeasureUnit::getIndexCount() {
1241 return gIndexes
[UPRV_LENGTHOF(gIndexes
) - 1];
1244 int32_t MeasureUnit::internalGetIndexForTypeAndSubtype(const char *type
, const char *subtype
) {
1245 int32_t t
= binarySearch(gTypes
, 0, UPRV_LENGTHOF(gTypes
), type
);
1249 int32_t st
= binarySearch(gSubTypes
, gOffsets
[t
], gOffsets
[t
+ 1], subtype
);
1253 return gIndexes
[t
] + st
- gOffsets
[t
];
1256 MeasureUnit
MeasureUnit::resolveUnitPerUnit(
1257 const MeasureUnit
&unit
, const MeasureUnit
&perUnit
, bool* isResolved
) {
1258 int32_t unitOffset
= unit
.getOffset();
1259 int32_t perUnitOffset
= perUnit
.getOffset();
1261 // binary search for (unitOffset, perUnitOffset)
1263 int32_t end
= UPRV_LENGTHOF(unitPerUnitToSingleUnit
);
1264 while (start
< end
) {
1265 int32_t mid
= (start
+ end
) / 2;
1266 int32_t *midRow
= unitPerUnitToSingleUnit
[mid
];
1267 if (unitOffset
< midRow
[0]) {
1269 } else if (unitOffset
> midRow
[0]) {
1271 } else if (perUnitOffset
< midRow
[1]) {
1273 } else if (perUnitOffset
> midRow
[1]) {
1276 // We found a resolution for our unit / per-unit combo
1279 return MeasureUnit(midRow
[2], midRow
[3]);
1283 *isResolved
= false;
1284 return MeasureUnit();
1287 MeasureUnit
*MeasureUnit::create(int typeId
, int subTypeId
, UErrorCode
&status
) {
1288 if (U_FAILURE(status
)) {
1291 MeasureUnit
*result
= new MeasureUnit(typeId
, subTypeId
);
1292 if (result
== NULL
) {
1293 status
= U_MEMORY_ALLOCATION_ERROR
;
1298 void MeasureUnit::initTime(const char *timeId
) {
1299 int32_t result
= binarySearch(gTypes
, 0, UPRV_LENGTHOF(gTypes
), "duration");
1300 U_ASSERT(result
!= -1);
1302 result
= binarySearch(gSubTypes
, gOffsets
[fTypeId
], gOffsets
[fTypeId
+ 1], timeId
);
1303 U_ASSERT(result
!= -1);
1304 fSubTypeId
= result
- gOffsets
[fTypeId
];
1307 void MeasureUnit::initCurrency(const char *isoCurrency
) {
1308 int32_t result
= binarySearch(gTypes
, 0, UPRV_LENGTHOF(gTypes
), "currency");
1309 U_ASSERT(result
!= -1);
1311 result
= binarySearch(
1312 gSubTypes
, gOffsets
[fTypeId
], gOffsets
[fTypeId
+ 1], isoCurrency
);
1314 fSubTypeId
= result
- gOffsets
[fTypeId
];
1316 uprv_strncpy(fCurrency
, isoCurrency
, UPRV_LENGTHOF(fCurrency
));
1321 void MeasureUnit::initNoUnit(const char *subtype
) {
1322 int32_t result
= binarySearch(gTypes
, 0, UPRV_LENGTHOF(gTypes
), "none");
1323 U_ASSERT(result
!= -1);
1325 result
= binarySearch(gSubTypes
, gOffsets
[fTypeId
], gOffsets
[fTypeId
+ 1], subtype
);
1326 U_ASSERT(result
!= -1);
1327 fSubTypeId
= result
- gOffsets
[fTypeId
];
1330 void MeasureUnit::setTo(int32_t typeId
, int32_t subTypeId
) {
1332 fSubTypeId
= subTypeId
;
1336 int32_t MeasureUnit::getOffset() const {
1337 return gOffsets
[fTypeId
] + fSubTypeId
;
1342 #endif /* !UNCONFIG_NO_FORMATTING */