scientificnumberformatter.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. // © 2016 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  3. /*
  4. **********************************************************************
  5. * Copyright (c) 2014, International Business Machines
  6. * Corporation and others. All Rights Reserved.
  7. **********************************************************************
  8. */
  9. #include "unicode/utypes.h"
  10. #if !UCONFIG_NO_FORMATTING
  11. #include "unicode/scientificnumberformatter.h"
  12. #include "unicode/dcfmtsym.h"
  13. #include "unicode/fpositer.h"
  14. #include "unicode/utf16.h"
  15. #include "unicode/uniset.h"
  16. #include "unicode/decimfmt.h"
  17. #include "static_unicode_sets.h"
  18. U_NAMESPACE_BEGIN
  19. static const char16_t kSuperscriptDigits[] = {
  20. 0x2070,
  21. 0xB9,
  22. 0xB2,
  23. 0xB3,
  24. 0x2074,
  25. 0x2075,
  26. 0x2076,
  27. 0x2077,
  28. 0x2078,
  29. 0x2079};
  30. static const char16_t kSuperscriptPlusSign = 0x207A;
  31. static const char16_t kSuperscriptMinusSign = 0x207B;
  32. static UBool copyAsSuperscript(
  33. const UnicodeString &s,
  34. int32_t beginIndex,
  35. int32_t endIndex,
  36. UnicodeString &result,
  37. UErrorCode &status) {
  38. if (U_FAILURE(status)) {
  39. return false;
  40. }
  41. for (int32_t i = beginIndex; i < endIndex;) {
  42. UChar32 c = s.char32At(i);
  43. int32_t digit = u_charDigitValue(c);
  44. if (digit < 0) {
  45. status = U_INVALID_CHAR_FOUND;
  46. return false;
  47. }
  48. result.append(kSuperscriptDigits[digit]);
  49. i += U16_LENGTH(c);
  50. }
  51. return true;
  52. }
  53. ScientificNumberFormatter *ScientificNumberFormatter::createSuperscriptInstance(
  54. DecimalFormat *fmtToAdopt, UErrorCode &status) {
  55. return createInstance(fmtToAdopt, new SuperscriptStyle(), status);
  56. }
  57. ScientificNumberFormatter *ScientificNumberFormatter::createSuperscriptInstance(
  58. const Locale &locale, UErrorCode &status) {
  59. return createInstance(
  60. static_cast<DecimalFormat *>(
  61. DecimalFormat::createScientificInstance(locale, status)),
  62. new SuperscriptStyle(),
  63. status);
  64. }
  65. ScientificNumberFormatter *ScientificNumberFormatter::createMarkupInstance(
  66. DecimalFormat *fmtToAdopt,
  67. const UnicodeString &beginMarkup,
  68. const UnicodeString &endMarkup,
  69. UErrorCode &status) {
  70. return createInstance(
  71. fmtToAdopt,
  72. new MarkupStyle(beginMarkup, endMarkup),
  73. status);
  74. }
  75. ScientificNumberFormatter *ScientificNumberFormatter::createMarkupInstance(
  76. const Locale &locale,
  77. const UnicodeString &beginMarkup,
  78. const UnicodeString &endMarkup,
  79. UErrorCode &status) {
  80. return createInstance(
  81. static_cast<DecimalFormat *>(
  82. DecimalFormat::createScientificInstance(locale, status)),
  83. new MarkupStyle(beginMarkup, endMarkup),
  84. status);
  85. }
  86. ScientificNumberFormatter *ScientificNumberFormatter::createInstance(
  87. DecimalFormat *fmtToAdopt,
  88. Style *styleToAdopt,
  89. UErrorCode &status) {
  90. LocalPointer<DecimalFormat> fmt(fmtToAdopt);
  91. LocalPointer<Style> style(styleToAdopt);
  92. if (U_FAILURE(status)) {
  93. return nullptr;
  94. }
  95. ScientificNumberFormatter *result =
  96. new ScientificNumberFormatter(
  97. fmt.getAlias(),
  98. style.getAlias(),
  99. status);
  100. if (result == nullptr) {
  101. status = U_MEMORY_ALLOCATION_ERROR;
  102. return nullptr;
  103. }
  104. fmt.orphan();
  105. style.orphan();
  106. if (U_FAILURE(status)) {
  107. delete result;
  108. return nullptr;
  109. }
  110. return result;
  111. }
  112. ScientificNumberFormatter::SuperscriptStyle *ScientificNumberFormatter::SuperscriptStyle::clone() const {
  113. return new ScientificNumberFormatter::SuperscriptStyle(*this);
  114. }
  115. UnicodeString &ScientificNumberFormatter::SuperscriptStyle::format(
  116. const UnicodeString &original,
  117. FieldPositionIterator &fpi,
  118. const UnicodeString &preExponent,
  119. UnicodeString &appendTo,
  120. UErrorCode &status) const {
  121. if (U_FAILURE(status)) {
  122. return appendTo;
  123. }
  124. FieldPosition fp;
  125. int32_t copyFromOffset = 0;
  126. while (fpi.next(fp)) {
  127. switch (fp.getField()) {
  128. case UNUM_EXPONENT_SYMBOL_FIELD:
  129. appendTo.append(
  130. original,
  131. copyFromOffset,
  132. fp.getBeginIndex() - copyFromOffset);
  133. copyFromOffset = fp.getEndIndex();
  134. appendTo.append(preExponent);
  135. break;
  136. case UNUM_EXPONENT_SIGN_FIELD:
  137. {
  138. using namespace icu::numparse::impl;
  139. int32_t beginIndex = fp.getBeginIndex();
  140. int32_t endIndex = fp.getEndIndex();
  141. UChar32 aChar = original.char32At(beginIndex);
  142. if (unisets::get(unisets::MINUS_SIGN)->contains(aChar)) {
  143. appendTo.append(
  144. original,
  145. copyFromOffset,
  146. beginIndex - copyFromOffset);
  147. appendTo.append(kSuperscriptMinusSign);
  148. } else if (unisets::get(unisets::PLUS_SIGN)->contains(aChar)) {
  149. appendTo.append(
  150. original,
  151. copyFromOffset,
  152. beginIndex - copyFromOffset);
  153. appendTo.append(kSuperscriptPlusSign);
  154. } else {
  155. status = U_INVALID_CHAR_FOUND;
  156. return appendTo;
  157. }
  158. copyFromOffset = endIndex;
  159. }
  160. break;
  161. case UNUM_EXPONENT_FIELD:
  162. appendTo.append(
  163. original,
  164. copyFromOffset,
  165. fp.getBeginIndex() - copyFromOffset);
  166. if (!copyAsSuperscript(
  167. original,
  168. fp.getBeginIndex(),
  169. fp.getEndIndex(),
  170. appendTo,
  171. status)) {
  172. return appendTo;
  173. }
  174. copyFromOffset = fp.getEndIndex();
  175. break;
  176. default:
  177. break;
  178. }
  179. }
  180. appendTo.append(
  181. original, copyFromOffset, original.length() - copyFromOffset);
  182. return appendTo;
  183. }
  184. ScientificNumberFormatter::MarkupStyle *ScientificNumberFormatter::MarkupStyle::clone() const {
  185. return new ScientificNumberFormatter::MarkupStyle(*this);
  186. }
  187. UnicodeString &ScientificNumberFormatter::MarkupStyle::format(
  188. const UnicodeString &original,
  189. FieldPositionIterator &fpi,
  190. const UnicodeString &preExponent,
  191. UnicodeString &appendTo,
  192. UErrorCode &status) const {
  193. if (U_FAILURE(status)) {
  194. return appendTo;
  195. }
  196. FieldPosition fp;
  197. int32_t copyFromOffset = 0;
  198. while (fpi.next(fp)) {
  199. switch (fp.getField()) {
  200. case UNUM_EXPONENT_SYMBOL_FIELD:
  201. appendTo.append(
  202. original,
  203. copyFromOffset,
  204. fp.getBeginIndex() - copyFromOffset);
  205. copyFromOffset = fp.getEndIndex();
  206. appendTo.append(preExponent);
  207. appendTo.append(fBeginMarkup);
  208. break;
  209. case UNUM_EXPONENT_FIELD:
  210. appendTo.append(
  211. original,
  212. copyFromOffset,
  213. fp.getEndIndex() - copyFromOffset);
  214. copyFromOffset = fp.getEndIndex();
  215. appendTo.append(fEndMarkup);
  216. break;
  217. default:
  218. break;
  219. }
  220. }
  221. appendTo.append(
  222. original, copyFromOffset, original.length() - copyFromOffset);
  223. return appendTo;
  224. }
  225. ScientificNumberFormatter::ScientificNumberFormatter(
  226. DecimalFormat *fmtToAdopt, Style *styleToAdopt, UErrorCode &status)
  227. : fPreExponent(),
  228. fDecimalFormat(fmtToAdopt),
  229. fStyle(styleToAdopt) {
  230. if (U_FAILURE(status)) {
  231. return;
  232. }
  233. if (fDecimalFormat == nullptr || fStyle == nullptr) {
  234. status = U_ILLEGAL_ARGUMENT_ERROR;
  235. return;
  236. }
  237. const DecimalFormatSymbols *sym = fDecimalFormat->getDecimalFormatSymbols();
  238. if (sym == nullptr) {
  239. status = U_ILLEGAL_ARGUMENT_ERROR;
  240. return;
  241. }
  242. getPreExponent(*sym, fPreExponent);
  243. }
  244. ScientificNumberFormatter::ScientificNumberFormatter(
  245. const ScientificNumberFormatter &other)
  246. : UObject(other),
  247. fPreExponent(other.fPreExponent),
  248. fDecimalFormat(nullptr),
  249. fStyle(nullptr) {
  250. fDecimalFormat = static_cast<DecimalFormat *>(
  251. other.fDecimalFormat->clone());
  252. fStyle = other.fStyle->clone();
  253. }
  254. ScientificNumberFormatter::~ScientificNumberFormatter() {
  255. delete fDecimalFormat;
  256. delete fStyle;
  257. }
  258. UnicodeString &ScientificNumberFormatter::format(
  259. const Formattable &number,
  260. UnicodeString &appendTo,
  261. UErrorCode &status) const {
  262. if (U_FAILURE(status)) {
  263. return appendTo;
  264. }
  265. UnicodeString original;
  266. FieldPositionIterator fpi;
  267. fDecimalFormat->format(number, original, &fpi, status);
  268. return fStyle->format(
  269. original,
  270. fpi,
  271. fPreExponent,
  272. appendTo,
  273. status);
  274. }
  275. void ScientificNumberFormatter::getPreExponent(
  276. const DecimalFormatSymbols &dfs, UnicodeString &preExponent) {
  277. preExponent.append(dfs.getConstSymbol(
  278. DecimalFormatSymbols::kExponentMultiplicationSymbol));
  279. preExponent.append(dfs.getConstSymbol(DecimalFormatSymbols::kOneDigitSymbol));
  280. preExponent.append(dfs.getConstSymbol(DecimalFormatSymbols::kZeroDigitSymbol));
  281. }
  282. U_NAMESPACE_END
  283. #endif /* !UCONFIG_NO_FORMATTING */