currpinf.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. // Copyright (C) 2016 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  3. /*
  4. *******************************************************************************
  5. * Copyright (C) 2009-2014, International Business Machines Corporation and
  6. * others. All Rights Reserved.
  7. *******************************************************************************
  8. */
  9. #include "unicode/currpinf.h"
  10. #if !UCONFIG_NO_FORMATTING
  11. //#define CURRENCY_PLURAL_INFO_DEBUG 1
  12. #ifdef CURRENCY_PLURAL_INFO_DEBUG
  13. #include <iostream>
  14. #endif
  15. #include "unicode/locid.h"
  16. #include "unicode/plurrule.h"
  17. #include "unicode/ures.h"
  18. #include "unicode/numsys.h"
  19. #include "cstring.h"
  20. #include "hash.h"
  21. #include "uresimp.h"
  22. #include "ureslocs.h"
  23. U_NAMESPACE_BEGIN
  24. static const UChar gNumberPatternSeparator = 0x3B; // ;
  25. U_CDECL_BEGIN
  26. /**
  27. * @internal ICU 4.2
  28. */
  29. static UBool U_CALLCONV ValueComparator(UHashTok val1, UHashTok val2);
  30. UBool
  31. U_CALLCONV ValueComparator(UHashTok val1, UHashTok val2) {
  32. const UnicodeString* affix_1 = (UnicodeString*)val1.pointer;
  33. const UnicodeString* affix_2 = (UnicodeString*)val2.pointer;
  34. return *affix_1 == *affix_2;
  35. }
  36. U_CDECL_END
  37. UOBJECT_DEFINE_RTTI_IMPLEMENTATION(CurrencyPluralInfo)
  38. static const UChar gDefaultCurrencyPluralPattern[] = {'0', '.', '#', '#', ' ', 0xA4, 0xA4, 0xA4, 0};
  39. static const UChar gTripleCurrencySign[] = {0xA4, 0xA4, 0xA4, 0};
  40. static const UChar gPluralCountOther[] = {0x6F, 0x74, 0x68, 0x65, 0x72, 0};
  41. static const UChar gPart0[] = {0x7B, 0x30, 0x7D, 0};
  42. static const UChar gPart1[] = {0x7B, 0x31, 0x7D, 0};
  43. static const char gNumberElementsTag[]="NumberElements";
  44. static const char gLatnTag[]="latn";
  45. static const char gPatternsTag[]="patterns";
  46. static const char gDecimalFormatTag[]="decimalFormat";
  47. static const char gCurrUnitPtnTag[]="CurrencyUnitPatterns";
  48. CurrencyPluralInfo::CurrencyPluralInfo(UErrorCode& status)
  49. : fPluralCountToCurrencyUnitPattern(NULL),
  50. fPluralRules(NULL),
  51. fLocale(NULL) {
  52. initialize(Locale::getDefault(), status);
  53. }
  54. CurrencyPluralInfo::CurrencyPluralInfo(const Locale& locale, UErrorCode& status)
  55. : fPluralCountToCurrencyUnitPattern(NULL),
  56. fPluralRules(NULL),
  57. fLocale(NULL) {
  58. initialize(locale, status);
  59. }
  60. CurrencyPluralInfo::CurrencyPluralInfo(const CurrencyPluralInfo& info)
  61. : UObject(info),
  62. fPluralCountToCurrencyUnitPattern(NULL),
  63. fPluralRules(NULL),
  64. fLocale(NULL) {
  65. *this = info;
  66. }
  67. CurrencyPluralInfo&
  68. CurrencyPluralInfo::operator=(const CurrencyPluralInfo& info) {
  69. if (this == &info) {
  70. return *this;
  71. }
  72. deleteHash(fPluralCountToCurrencyUnitPattern);
  73. UErrorCode status = U_ZERO_ERROR;
  74. fPluralCountToCurrencyUnitPattern = initHash(status);
  75. copyHash(info.fPluralCountToCurrencyUnitPattern,
  76. fPluralCountToCurrencyUnitPattern, status);
  77. if ( U_FAILURE(status) ) {
  78. return *this;
  79. }
  80. delete fPluralRules;
  81. delete fLocale;
  82. if (info.fPluralRules) {
  83. fPluralRules = info.fPluralRules->clone();
  84. } else {
  85. fPluralRules = NULL;
  86. }
  87. if (info.fLocale) {
  88. fLocale = info.fLocale->clone();
  89. } else {
  90. fLocale = NULL;
  91. }
  92. return *this;
  93. }
  94. CurrencyPluralInfo::~CurrencyPluralInfo() {
  95. deleteHash(fPluralCountToCurrencyUnitPattern);
  96. fPluralCountToCurrencyUnitPattern = NULL;
  97. delete fPluralRules;
  98. delete fLocale;
  99. fPluralRules = NULL;
  100. fLocale = NULL;
  101. }
  102. UBool
  103. CurrencyPluralInfo::operator==(const CurrencyPluralInfo& info) const {
  104. #ifdef CURRENCY_PLURAL_INFO_DEBUG
  105. if (*fPluralRules == *info.fPluralRules) {
  106. std::cout << "same plural rules\n";
  107. }
  108. if (*fLocale == *info.fLocale) {
  109. std::cout << "same locale\n";
  110. }
  111. if (fPluralCountToCurrencyUnitPattern->equals(*info.fPluralCountToCurrencyUnitPattern)) {
  112. std::cout << "same pattern\n";
  113. }
  114. #endif
  115. return *fPluralRules == *info.fPluralRules &&
  116. *fLocale == *info.fLocale &&
  117. fPluralCountToCurrencyUnitPattern->equals(*info.fPluralCountToCurrencyUnitPattern);
  118. }
  119. CurrencyPluralInfo*
  120. CurrencyPluralInfo::clone() const {
  121. return new CurrencyPluralInfo(*this);
  122. }
  123. const PluralRules*
  124. CurrencyPluralInfo::getPluralRules() const {
  125. return fPluralRules;
  126. }
  127. UnicodeString&
  128. CurrencyPluralInfo::getCurrencyPluralPattern(const UnicodeString& pluralCount,
  129. UnicodeString& result) const {
  130. const UnicodeString* currencyPluralPattern =
  131. (UnicodeString*)fPluralCountToCurrencyUnitPattern->get(pluralCount);
  132. if (currencyPluralPattern == NULL) {
  133. // fall back to "other"
  134. if (pluralCount.compare(gPluralCountOther, 5)) {
  135. currencyPluralPattern =
  136. (UnicodeString*)fPluralCountToCurrencyUnitPattern->get(UnicodeString(TRUE, gPluralCountOther, 5));
  137. }
  138. if (currencyPluralPattern == NULL) {
  139. // no currencyUnitPatterns defined,
  140. // fallback to predefined defult.
  141. // This should never happen when ICU resource files are
  142. // available, since currencyUnitPattern of "other" is always
  143. // defined in root.
  144. result = UnicodeString(gDefaultCurrencyPluralPattern);
  145. return result;
  146. }
  147. }
  148. result = *currencyPluralPattern;
  149. return result;
  150. }
  151. const Locale&
  152. CurrencyPluralInfo::getLocale() const {
  153. return *fLocale;
  154. }
  155. void
  156. CurrencyPluralInfo::setPluralRules(const UnicodeString& ruleDescription,
  157. UErrorCode& status) {
  158. if (U_SUCCESS(status)) {
  159. if (fPluralRules) {
  160. delete fPluralRules;
  161. }
  162. fPluralRules = PluralRules::createRules(ruleDescription, status);
  163. }
  164. }
  165. void
  166. CurrencyPluralInfo::setCurrencyPluralPattern(const UnicodeString& pluralCount,
  167. const UnicodeString& pattern,
  168. UErrorCode& status) {
  169. if (U_SUCCESS(status)) {
  170. fPluralCountToCurrencyUnitPattern->put(pluralCount, new UnicodeString(pattern), status);
  171. }
  172. }
  173. void
  174. CurrencyPluralInfo::setLocale(const Locale& loc, UErrorCode& status) {
  175. initialize(loc, status);
  176. }
  177. void
  178. CurrencyPluralInfo::initialize(const Locale& loc, UErrorCode& status) {
  179. if (U_FAILURE(status)) {
  180. return;
  181. }
  182. delete fLocale;
  183. fLocale = loc.clone();
  184. if (fPluralRules) {
  185. delete fPluralRules;
  186. }
  187. fPluralRules = PluralRules::forLocale(loc, status);
  188. setupCurrencyPluralPattern(loc, status);
  189. }
  190. void
  191. CurrencyPluralInfo::setupCurrencyPluralPattern(const Locale& loc, UErrorCode& status) {
  192. if (U_FAILURE(status)) {
  193. return;
  194. }
  195. if (fPluralCountToCurrencyUnitPattern) {
  196. deleteHash(fPluralCountToCurrencyUnitPattern);
  197. }
  198. fPluralCountToCurrencyUnitPattern = initHash(status);
  199. if (U_FAILURE(status)) {
  200. return;
  201. }
  202. NumberingSystem *ns = NumberingSystem::createInstance(loc,status);
  203. UErrorCode ec = U_ZERO_ERROR;
  204. UResourceBundle *rb = ures_open(NULL, loc.getName(), &ec);
  205. UResourceBundle *numElements = ures_getByKeyWithFallback(rb, gNumberElementsTag, NULL, &ec);
  206. rb = ures_getByKeyWithFallback(numElements, ns->getName(), rb, &ec);
  207. rb = ures_getByKeyWithFallback(rb, gPatternsTag, rb, &ec);
  208. int32_t ptnLen;
  209. const UChar* numberStylePattern = ures_getStringByKeyWithFallback(rb, gDecimalFormatTag, &ptnLen, &ec);
  210. // Fall back to "latn" if num sys specific pattern isn't there.
  211. if ( ec == U_MISSING_RESOURCE_ERROR && uprv_strcmp(ns->getName(),gLatnTag)) {
  212. ec = U_ZERO_ERROR;
  213. rb = ures_getByKeyWithFallback(numElements, gLatnTag, rb, &ec);
  214. rb = ures_getByKeyWithFallback(rb, gPatternsTag, rb, &ec);
  215. numberStylePattern = ures_getStringByKeyWithFallback(rb, gDecimalFormatTag, &ptnLen, &ec);
  216. }
  217. int32_t numberStylePatternLen = ptnLen;
  218. const UChar* negNumberStylePattern = NULL;
  219. int32_t negNumberStylePatternLen = 0;
  220. // TODO: Java
  221. // parse to check whether there is ";" separator in the numberStylePattern
  222. UBool hasSeparator = false;
  223. if (U_SUCCESS(ec)) {
  224. for (int32_t styleCharIndex = 0; styleCharIndex < ptnLen; ++styleCharIndex) {
  225. if (numberStylePattern[styleCharIndex] == gNumberPatternSeparator) {
  226. hasSeparator = true;
  227. // split the number style pattern into positive and negative
  228. negNumberStylePattern = numberStylePattern + styleCharIndex + 1;
  229. negNumberStylePatternLen = ptnLen - styleCharIndex - 1;
  230. numberStylePatternLen = styleCharIndex;
  231. }
  232. }
  233. }
  234. ures_close(numElements);
  235. ures_close(rb);
  236. delete ns;
  237. if (U_FAILURE(ec)) {
  238. return;
  239. }
  240. UResourceBundle *currRb = ures_open(U_ICUDATA_CURR, loc.getName(), &ec);
  241. UResourceBundle *currencyRes = ures_getByKeyWithFallback(currRb, gCurrUnitPtnTag, NULL, &ec);
  242. #ifdef CURRENCY_PLURAL_INFO_DEBUG
  243. std::cout << "in set up\n";
  244. #endif
  245. StringEnumeration* keywords = fPluralRules->getKeywords(ec);
  246. if (U_SUCCESS(ec)) {
  247. const char* pluralCount;
  248. while ((pluralCount = keywords->next(NULL, ec)) != NULL) {
  249. if ( U_SUCCESS(ec) ) {
  250. int32_t ptnLen;
  251. UErrorCode err = U_ZERO_ERROR;
  252. const UChar* patternChars = ures_getStringByKeyWithFallback(
  253. currencyRes, pluralCount, &ptnLen, &err);
  254. if (U_SUCCESS(err) && ptnLen > 0) {
  255. UnicodeString* pattern = new UnicodeString(patternChars, ptnLen);
  256. #ifdef CURRENCY_PLURAL_INFO_DEBUG
  257. char result_1[1000];
  258. pattern->extract(0, pattern->length(), result_1, "UTF-8");
  259. std::cout << "pluralCount: " << pluralCount << "; pattern: " << result_1 << "\n";
  260. #endif
  261. pattern->findAndReplace(UnicodeString(TRUE, gPart0, 3),
  262. UnicodeString(numberStylePattern, numberStylePatternLen));
  263. pattern->findAndReplace(UnicodeString(TRUE, gPart1, 3), UnicodeString(TRUE, gTripleCurrencySign, 3));
  264. if (hasSeparator) {
  265. UnicodeString negPattern(patternChars, ptnLen);
  266. negPattern.findAndReplace(UnicodeString(TRUE, gPart0, 3),
  267. UnicodeString(negNumberStylePattern, negNumberStylePatternLen));
  268. negPattern.findAndReplace(UnicodeString(TRUE, gPart1, 3), UnicodeString(TRUE, gTripleCurrencySign, 3));
  269. pattern->append(gNumberPatternSeparator);
  270. pattern->append(negPattern);
  271. }
  272. #ifdef CURRENCY_PLURAL_INFO_DEBUG
  273. pattern->extract(0, pattern->length(), result_1, "UTF-8");
  274. std::cout << "pluralCount: " << pluralCount << "; pattern: " << result_1 << "\n";
  275. #endif
  276. fPluralCountToCurrencyUnitPattern->put(UnicodeString(pluralCount, -1, US_INV), pattern, status);
  277. }
  278. }
  279. }
  280. }
  281. delete keywords;
  282. ures_close(currencyRes);
  283. ures_close(currRb);
  284. }
  285. void
  286. CurrencyPluralInfo::deleteHash(Hashtable* hTable)
  287. {
  288. if ( hTable == NULL ) {
  289. return;
  290. }
  291. int32_t pos = UHASH_FIRST;
  292. const UHashElement* element = NULL;
  293. while ( (element = hTable->nextElement(pos)) != NULL ) {
  294. const UHashTok valueTok = element->value;
  295. const UnicodeString* value = (UnicodeString*)valueTok.pointer;
  296. delete value;
  297. }
  298. delete hTable;
  299. hTable = NULL;
  300. }
  301. Hashtable*
  302. CurrencyPluralInfo::initHash(UErrorCode& status) {
  303. if ( U_FAILURE(status) ) {
  304. return NULL;
  305. }
  306. Hashtable* hTable;
  307. if ( (hTable = new Hashtable(TRUE, status)) == NULL ) {
  308. status = U_MEMORY_ALLOCATION_ERROR;
  309. return NULL;
  310. }
  311. if ( U_FAILURE(status) ) {
  312. delete hTable;
  313. return NULL;
  314. }
  315. hTable->setValueComparator(ValueComparator);
  316. return hTable;
  317. }
  318. void
  319. CurrencyPluralInfo::copyHash(const Hashtable* source,
  320. Hashtable* target,
  321. UErrorCode& status) {
  322. if ( U_FAILURE(status) ) {
  323. return;
  324. }
  325. int32_t pos = UHASH_FIRST;
  326. const UHashElement* element = NULL;
  327. if ( source ) {
  328. while ( (element = source->nextElement(pos)) != NULL ) {
  329. const UHashTok keyTok = element->key;
  330. const UnicodeString* key = (UnicodeString*)keyTok.pointer;
  331. const UHashTok valueTok = element->value;
  332. const UnicodeString* value = (UnicodeString*)valueTok.pointer;
  333. UnicodeString* copy = new UnicodeString(*value);
  334. target->put(UnicodeString(*key), copy, status);
  335. if ( U_FAILURE(status) ) {
  336. return;
  337. }
  338. }
  339. }
  340. }
  341. U_NAMESPACE_END
  342. #endif