number_patternmodifier.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. // © 2017 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  3. #include "unicode/utypes.h"
  4. #if !UCONFIG_NO_FORMATTING
  5. #include "cstring.h"
  6. #include "number_patternmodifier.h"
  7. #include "unicode/dcfmtsym.h"
  8. #include "unicode/ucurr.h"
  9. #include "unicode/unistr.h"
  10. #include "number_microprops.h"
  11. using namespace icu;
  12. using namespace icu::number;
  13. using namespace icu::number::impl;
  14. AffixPatternProvider::~AffixPatternProvider() = default;
  15. MutablePatternModifier::MutablePatternModifier(bool isStrong)
  16. : fStrong(isStrong) {}
  17. void MutablePatternModifier::setPatternInfo(const AffixPatternProvider* patternInfo, Field field) {
  18. fPatternInfo = patternInfo;
  19. fField = field;
  20. }
  21. void MutablePatternModifier::setPatternAttributes(
  22. UNumberSignDisplay signDisplay,
  23. bool perMille,
  24. bool approximately) {
  25. fSignDisplay = signDisplay;
  26. fPerMilleReplacesPercent = perMille;
  27. fApproximately = approximately;
  28. }
  29. void MutablePatternModifier::setSymbols(const DecimalFormatSymbols* symbols,
  30. const CurrencyUnit& currency,
  31. const UNumberUnitWidth unitWidth,
  32. const PluralRules* rules,
  33. UErrorCode& status) {
  34. U_ASSERT((rules != nullptr) == needsPlurals());
  35. fSymbols = symbols;
  36. fCurrencySymbols = {currency, symbols->getLocale(), *symbols, status};
  37. fUnitWidth = unitWidth;
  38. fRules = rules;
  39. }
  40. void MutablePatternModifier::setNumberProperties(Signum signum, StandardPlural::Form plural) {
  41. fSignum = signum;
  42. fPlural = plural;
  43. }
  44. bool MutablePatternModifier::needsPlurals() const {
  45. UErrorCode statusLocal = U_ZERO_ERROR;
  46. return fPatternInfo->containsSymbolType(AffixPatternType::TYPE_CURRENCY_TRIPLE, statusLocal);
  47. // Silently ignore any error codes.
  48. }
  49. AdoptingSignumModifierStore MutablePatternModifier::createImmutableForPlural(StandardPlural::Form plural, UErrorCode& status) {
  50. AdoptingSignumModifierStore pm;
  51. setNumberProperties(SIGNUM_POS, plural);
  52. pm.adoptModifier(SIGNUM_POS, createConstantModifier(status));
  53. setNumberProperties(SIGNUM_NEG_ZERO, plural);
  54. pm.adoptModifier(SIGNUM_NEG_ZERO, createConstantModifier(status));
  55. setNumberProperties(SIGNUM_POS_ZERO, plural);
  56. pm.adoptModifier(SIGNUM_POS_ZERO, createConstantModifier(status));
  57. setNumberProperties(SIGNUM_NEG, plural);
  58. pm.adoptModifier(SIGNUM_NEG, createConstantModifier(status));
  59. return pm;
  60. }
  61. ImmutablePatternModifier* MutablePatternModifier::createImmutable(UErrorCode& status) {
  62. // TODO: Move StandardPlural VALUES to standardplural.h
  63. static const StandardPlural::Form STANDARD_PLURAL_VALUES[] = {
  64. StandardPlural::Form::ZERO,
  65. StandardPlural::Form::ONE,
  66. StandardPlural::Form::TWO,
  67. StandardPlural::Form::FEW,
  68. StandardPlural::Form::MANY,
  69. StandardPlural::Form::OTHER};
  70. auto pm = new AdoptingModifierStore();
  71. if (pm == nullptr) {
  72. status = U_MEMORY_ALLOCATION_ERROR;
  73. return nullptr;
  74. }
  75. if (needsPlurals()) {
  76. // Slower path when we require the plural keyword.
  77. for (StandardPlural::Form plural : STANDARD_PLURAL_VALUES) {
  78. pm->adoptSignumModifierStore(plural, createImmutableForPlural(plural, status));
  79. }
  80. if (U_FAILURE(status)) {
  81. delete pm;
  82. return nullptr;
  83. }
  84. return new ImmutablePatternModifier(pm, fRules); // adopts pm
  85. } else {
  86. // Faster path when plural keyword is not needed.
  87. pm->adoptSignumModifierStoreNoPlural(createImmutableForPlural(StandardPlural::Form::COUNT, status));
  88. if (U_FAILURE(status)) {
  89. delete pm;
  90. return nullptr;
  91. }
  92. return new ImmutablePatternModifier(pm, nullptr); // adopts pm
  93. }
  94. }
  95. ConstantMultiFieldModifier* MutablePatternModifier::createConstantModifier(UErrorCode& status) {
  96. FormattedStringBuilder a;
  97. FormattedStringBuilder b;
  98. insertPrefix(a, 0, status);
  99. insertSuffix(b, 0, status);
  100. if (fPatternInfo->hasCurrencySign()) {
  101. return new CurrencySpacingEnabledModifier(
  102. a, b, !fPatternInfo->hasBody(), fStrong, *fSymbols, status);
  103. } else {
  104. return new ConstantMultiFieldModifier(a, b, !fPatternInfo->hasBody(), fStrong);
  105. }
  106. }
  107. ImmutablePatternModifier::ImmutablePatternModifier(AdoptingModifierStore* pm, const PluralRules* rules)
  108. : pm(pm), rules(rules), parent(nullptr) {}
  109. void ImmutablePatternModifier::processQuantity(DecimalQuantity& quantity, MicroProps& micros,
  110. UErrorCode& status) const {
  111. parent->processQuantity(quantity, micros, status);
  112. micros.rounder.apply(quantity, status);
  113. if (micros.modMiddle != nullptr) {
  114. return;
  115. }
  116. applyToMicros(micros, quantity, status);
  117. }
  118. void ImmutablePatternModifier::applyToMicros(
  119. MicroProps& micros, const DecimalQuantity& quantity, UErrorCode& status) const {
  120. if (rules == nullptr) {
  121. micros.modMiddle = pm->getModifierWithoutPlural(quantity.signum());
  122. } else {
  123. StandardPlural::Form pluralForm = utils::getPluralSafe(micros.rounder, rules, quantity, status);
  124. micros.modMiddle = pm->getModifier(quantity.signum(), pluralForm);
  125. }
  126. }
  127. const Modifier* ImmutablePatternModifier::getModifier(Signum signum, StandardPlural::Form plural) const {
  128. if (rules == nullptr) {
  129. return pm->getModifierWithoutPlural(signum);
  130. } else {
  131. return pm->getModifier(signum, plural);
  132. }
  133. }
  134. void ImmutablePatternModifier::addToChain(const MicroPropsGenerator* parent) {
  135. this->parent = parent;
  136. }
  137. /** Used by the unsafe code path. */
  138. MicroPropsGenerator& MutablePatternModifier::addToChain(const MicroPropsGenerator* parent) {
  139. fParent = parent;
  140. return *this;
  141. }
  142. void MutablePatternModifier::processQuantity(DecimalQuantity& fq, MicroProps& micros,
  143. UErrorCode& status) const {
  144. fParent->processQuantity(fq, micros, status);
  145. micros.rounder.apply(fq, status);
  146. if (micros.modMiddle != nullptr) {
  147. return;
  148. }
  149. // The unsafe code path performs self-mutation, so we need a const_cast.
  150. // This method needs to be const because it overrides a const method in the parent class.
  151. auto nonConstThis = const_cast<MutablePatternModifier*>(this);
  152. if (needsPlurals()) {
  153. StandardPlural::Form pluralForm = utils::getPluralSafe(micros.rounder, fRules, fq, status);
  154. nonConstThis->setNumberProperties(fq.signum(), pluralForm);
  155. } else {
  156. nonConstThis->setNumberProperties(fq.signum(), StandardPlural::Form::COUNT);
  157. }
  158. micros.modMiddle = this;
  159. }
  160. int32_t MutablePatternModifier::apply(FormattedStringBuilder& output, int32_t leftIndex, int32_t rightIndex,
  161. UErrorCode& status) const {
  162. // The unsafe code path performs self-mutation, so we need a const_cast.
  163. // This method needs to be const because it overrides a const method in the parent class.
  164. auto nonConstThis = const_cast<MutablePatternModifier*>(this);
  165. int32_t prefixLen = nonConstThis->insertPrefix(output, leftIndex, status);
  166. int32_t suffixLen = nonConstThis->insertSuffix(output, rightIndex + prefixLen, status);
  167. // If the pattern had no decimal stem body (like #,##0.00), overwrite the value.
  168. int32_t overwriteLen = 0;
  169. if (!fPatternInfo->hasBody()) {
  170. overwriteLen = output.splice(
  171. leftIndex + prefixLen,
  172. rightIndex + prefixLen,
  173. UnicodeString(),
  174. 0,
  175. 0,
  176. kUndefinedField,
  177. status);
  178. }
  179. CurrencySpacingEnabledModifier::applyCurrencySpacing(
  180. output,
  181. leftIndex,
  182. prefixLen,
  183. rightIndex + overwriteLen + prefixLen,
  184. suffixLen,
  185. *fSymbols,
  186. status);
  187. return prefixLen + overwriteLen + suffixLen;
  188. }
  189. int32_t MutablePatternModifier::getPrefixLength() const {
  190. // The unsafe code path performs self-mutation, so we need a const_cast.
  191. // This method needs to be const because it overrides a const method in the parent class.
  192. auto nonConstThis = const_cast<MutablePatternModifier*>(this);
  193. // Enter and exit CharSequence Mode to get the length.
  194. UErrorCode status = U_ZERO_ERROR; // status fails only with an iilegal argument exception
  195. nonConstThis->prepareAffix(true);
  196. int result = AffixUtils::unescapedCodePointCount(currentAffix, *this, status); // prefix length
  197. return result;
  198. }
  199. int32_t MutablePatternModifier::getCodePointCount() const {
  200. // The unsafe code path performs self-mutation, so we need a const_cast.
  201. // This method needs to be const because it overrides a const method in the parent class.
  202. auto nonConstThis = const_cast<MutablePatternModifier*>(this);
  203. // Render the affixes to get the length
  204. UErrorCode status = U_ZERO_ERROR; // status fails only with an iilegal argument exception
  205. nonConstThis->prepareAffix(true);
  206. int result = AffixUtils::unescapedCodePointCount(currentAffix, *this, status); // prefix length
  207. nonConstThis->prepareAffix(false);
  208. result += AffixUtils::unescapedCodePointCount(currentAffix, *this, status); // suffix length
  209. return result;
  210. }
  211. bool MutablePatternModifier::isStrong() const {
  212. return fStrong;
  213. }
  214. bool MutablePatternModifier::containsField(Field field) const {
  215. (void)field;
  216. // This method is not currently used.
  217. UPRV_UNREACHABLE_EXIT;
  218. }
  219. void MutablePatternModifier::getParameters(Parameters& output) const {
  220. (void)output;
  221. // This method is not currently used.
  222. UPRV_UNREACHABLE_EXIT;
  223. }
  224. bool MutablePatternModifier::semanticallyEquivalent(const Modifier& other) const {
  225. (void)other;
  226. // This method is not currently used.
  227. UPRV_UNREACHABLE_EXIT;
  228. }
  229. int32_t MutablePatternModifier::insertPrefix(FormattedStringBuilder& sb, int position, UErrorCode& status) {
  230. prepareAffix(true);
  231. int32_t length = AffixUtils::unescape(currentAffix, sb, position, *this, fField, status);
  232. return length;
  233. }
  234. int32_t MutablePatternModifier::insertSuffix(FormattedStringBuilder& sb, int position, UErrorCode& status) {
  235. prepareAffix(false);
  236. int32_t length = AffixUtils::unescape(currentAffix, sb, position, *this, fField, status);
  237. return length;
  238. }
  239. /** This method contains the heart of the logic for rendering LDML affix strings. */
  240. void MutablePatternModifier::prepareAffix(bool isPrefix) {
  241. PatternStringUtils::patternInfoToStringBuilder(
  242. *fPatternInfo,
  243. isPrefix,
  244. PatternStringUtils::resolveSignDisplay(fSignDisplay, fSignum),
  245. fApproximately,
  246. fPlural,
  247. fPerMilleReplacesPercent,
  248. false, // dropCurrencySymbols
  249. currentAffix);
  250. }
  251. UnicodeString MutablePatternModifier::getSymbol(AffixPatternType type) const {
  252. UErrorCode localStatus = U_ZERO_ERROR;
  253. switch (type) {
  254. case AffixPatternType::TYPE_MINUS_SIGN:
  255. return fSymbols->getSymbol(DecimalFormatSymbols::ENumberFormatSymbol::kMinusSignSymbol);
  256. case AffixPatternType::TYPE_PLUS_SIGN:
  257. return fSymbols->getSymbol(DecimalFormatSymbols::ENumberFormatSymbol::kPlusSignSymbol);
  258. case AffixPatternType::TYPE_APPROXIMATELY_SIGN:
  259. return fSymbols->getSymbol(DecimalFormatSymbols::ENumberFormatSymbol::kApproximatelySignSymbol);
  260. case AffixPatternType::TYPE_PERCENT:
  261. return fSymbols->getSymbol(DecimalFormatSymbols::ENumberFormatSymbol::kPercentSymbol);
  262. case AffixPatternType::TYPE_PERMILLE:
  263. return fSymbols->getSymbol(DecimalFormatSymbols::ENumberFormatSymbol::kPerMillSymbol);
  264. case AffixPatternType::TYPE_CURRENCY_SINGLE:
  265. return getCurrencySymbolForUnitWidth(localStatus);
  266. case AffixPatternType::TYPE_CURRENCY_DOUBLE:
  267. return fCurrencySymbols.getIntlCurrencySymbol(localStatus);
  268. case AffixPatternType::TYPE_CURRENCY_TRIPLE:
  269. // NOTE: This is the code path only for patterns containing "¤¤¤".
  270. // Plural currencies set via the API are formatted in LongNameHandler.
  271. // This code path is used by DecimalFormat via CurrencyPluralInfo.
  272. U_ASSERT(fPlural != StandardPlural::Form::COUNT);
  273. return fCurrencySymbols.getPluralName(fPlural, localStatus);
  274. case AffixPatternType::TYPE_CURRENCY_QUAD:
  275. return UnicodeString(u"\uFFFD");
  276. case AffixPatternType::TYPE_CURRENCY_QUINT:
  277. return UnicodeString(u"\uFFFD");
  278. default:
  279. UPRV_UNREACHABLE_EXIT;
  280. }
  281. }
  282. UnicodeString MutablePatternModifier::getCurrencySymbolForUnitWidth(UErrorCode& status) const {
  283. switch (fUnitWidth) {
  284. case UNumberUnitWidth::UNUM_UNIT_WIDTH_NARROW:
  285. return fCurrencySymbols.getNarrowCurrencySymbol(status);
  286. case UNumberUnitWidth::UNUM_UNIT_WIDTH_SHORT:
  287. return fCurrencySymbols.getCurrencySymbol(status);
  288. case UNumberUnitWidth::UNUM_UNIT_WIDTH_ISO_CODE:
  289. return fCurrencySymbols.getIntlCurrencySymbol(status);
  290. case UNumberUnitWidth::UNUM_UNIT_WIDTH_FORMAL:
  291. return fCurrencySymbols.getFormalCurrencySymbol(status);
  292. case UNumberUnitWidth::UNUM_UNIT_WIDTH_VARIANT:
  293. return fCurrencySymbols.getVariantCurrencySymbol(status);
  294. case UNumberUnitWidth::UNUM_UNIT_WIDTH_HIDDEN:
  295. return UnicodeString();
  296. default:
  297. return fCurrencySymbols.getCurrencySymbol(status);
  298. }
  299. }
  300. UnicodeString MutablePatternModifier::toUnicodeString() const {
  301. // Never called by AffixUtils
  302. UPRV_UNREACHABLE_EXIT;
  303. }
  304. #endif /* #if !UCONFIG_NO_FORMATTING */