1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819
|
// © 2018 and later: Unicode, Inc. and others.
// License & terms of use: http://www.unicode.org/copyright.html
#include <_foundation_unicode/utypes.h>
#if !UCONFIG_NO_FORMATTING
// Allow implicit conversion from char16_t* to UnicodeString for this file:
// Helpful in toString methods and elsewhere.
#define UNISTR_FROM_STRING_EXPLICIT
#include "number_decnum.h"
#include "number_roundingutils.h"
#include "number_skeletons.h"
#include "umutex.h"
#include "ucln_in.h"
#include "patternprops.h"
#include <_foundation_unicode/ucharstriebuilder.h>
#include "number_utils.h"
#include "number_decimalquantity.h"
#include <_foundation_unicode/numberformatter.h>
#include "uinvchar.h"
#include "charstr.h"
#include "string_segment.h"
#include <_foundation_unicode/errorcode.h>
#include "util.h"
#include "measunit_impl.h"
using namespace icu;
using namespace icu::number;
using namespace icu::number::impl;
using namespace icu::number::impl::skeleton;
namespace {
icu::UInitOnce gNumberSkeletonsInitOnce {};
char16_t* kSerializedStemTrie = nullptr;
UBool U_CALLCONV cleanupNumberSkeletons() {
uprv_free(kSerializedStemTrie);
kSerializedStemTrie = nullptr;
gNumberSkeletonsInitOnce.reset();
return true;
}
void U_CALLCONV initNumberSkeletons(UErrorCode& status) {
ucln_i18n_registerCleanup(UCLN_I18N_NUMBER_SKELETONS, cleanupNumberSkeletons);
UCharsTrieBuilder b(status);
if (U_FAILURE(status)) { return; }
// Section 1:
b.add(u"compact-short", STEM_COMPACT_SHORT, status);
b.add(u"compact-long", STEM_COMPACT_LONG, status);
b.add(u"scientific", STEM_SCIENTIFIC, status);
b.add(u"engineering", STEM_ENGINEERING, status);
b.add(u"notation-simple", STEM_NOTATION_SIMPLE, status);
b.add(u"base-unit", STEM_BASE_UNIT, status);
b.add(u"percent", STEM_PERCENT, status);
b.add(u"permille", STEM_PERMILLE, status);
b.add(u"precision-integer", STEM_PRECISION_INTEGER, status);
b.add(u"precision-unlimited", STEM_PRECISION_UNLIMITED, status);
b.add(u"precision-currency-standard", STEM_PRECISION_CURRENCY_STANDARD, status);
b.add(u"precision-currency-cash", STEM_PRECISION_CURRENCY_CASH, status);
b.add(u"rounding-mode-ceiling", STEM_ROUNDING_MODE_CEILING, status);
b.add(u"rounding-mode-floor", STEM_ROUNDING_MODE_FLOOR, status);
b.add(u"rounding-mode-down", STEM_ROUNDING_MODE_DOWN, status);
b.add(u"rounding-mode-up", STEM_ROUNDING_MODE_UP, status);
b.add(u"rounding-mode-half-even", STEM_ROUNDING_MODE_HALF_EVEN, status);
b.add(u"rounding-mode-half-odd", STEM_ROUNDING_MODE_HALF_ODD, status);
b.add(u"rounding-mode-half-ceiling", STEM_ROUNDING_MODE_HALF_CEILING, status);
b.add(u"rounding-mode-half-floor", STEM_ROUNDING_MODE_HALF_FLOOR, status);
b.add(u"rounding-mode-half-down", STEM_ROUNDING_MODE_HALF_DOWN, status);
b.add(u"rounding-mode-half-up", STEM_ROUNDING_MODE_HALF_UP, status);
b.add(u"rounding-mode-unnecessary", STEM_ROUNDING_MODE_UNNECESSARY, status);
b.add(u"integer-width-trunc", STEM_INTEGER_WIDTH_TRUNC, status);
b.add(u"group-off", STEM_GROUP_OFF, status);
b.add(u"group-min2", STEM_GROUP_MIN2, status);
b.add(u"group-auto", STEM_GROUP_AUTO, status);
b.add(u"group-on-aligned", STEM_GROUP_ON_ALIGNED, status);
b.add(u"group-thousands", STEM_GROUP_THOUSANDS, status);
b.add(u"latin", STEM_LATIN, status);
b.add(u"unit-width-narrow", STEM_UNIT_WIDTH_NARROW, status);
b.add(u"unit-width-short", STEM_UNIT_WIDTH_SHORT, status);
b.add(u"unit-width-full-name", STEM_UNIT_WIDTH_FULL_NAME, status);
b.add(u"unit-width-iso-code", STEM_UNIT_WIDTH_ISO_CODE, status);
b.add(u"unit-width-formal", STEM_UNIT_WIDTH_FORMAL, status);
b.add(u"unit-width-variant", STEM_UNIT_WIDTH_VARIANT, status);
b.add(u"unit-width-hidden", STEM_UNIT_WIDTH_HIDDEN, status);
b.add(u"sign-auto", STEM_SIGN_AUTO, status);
b.add(u"sign-always", STEM_SIGN_ALWAYS, status);
b.add(u"sign-never", STEM_SIGN_NEVER, status);
b.add(u"sign-accounting", STEM_SIGN_ACCOUNTING, status);
b.add(u"sign-accounting-always", STEM_SIGN_ACCOUNTING_ALWAYS, status);
b.add(u"sign-except-zero", STEM_SIGN_EXCEPT_ZERO, status);
b.add(u"sign-accounting-except-zero", STEM_SIGN_ACCOUNTING_EXCEPT_ZERO, status);
b.add(u"sign-negative", STEM_SIGN_NEGATIVE, status);
b.add(u"sign-accounting-negative", STEM_SIGN_ACCOUNTING_NEGATIVE, status);
b.add(u"decimal-auto", STEM_DECIMAL_AUTO, status);
b.add(u"decimal-always", STEM_DECIMAL_ALWAYS, status);
if (U_FAILURE(status)) { return; }
// Section 2:
b.add(u"precision-increment", STEM_PRECISION_INCREMENT, status);
b.add(u"measure-unit", STEM_MEASURE_UNIT, status);
b.add(u"per-measure-unit", STEM_PER_MEASURE_UNIT, status);
b.add(u"unit", STEM_UNIT, status);
b.add(u"usage", STEM_UNIT_USAGE, status);
b.add(u"currency", STEM_CURRENCY, status);
b.add(u"integer-width", STEM_INTEGER_WIDTH, status);
b.add(u"numbering-system", STEM_NUMBERING_SYSTEM, status);
b.add(u"scale", STEM_SCALE, status);
if (U_FAILURE(status)) { return; }
// Section 3 (concise tokens):
b.add(u"K", STEM_COMPACT_SHORT, status);
b.add(u"KK", STEM_COMPACT_LONG, status);
b.add(u"%", STEM_PERCENT, status);
b.add(u"%x100", STEM_PERCENT_100, status);
b.add(u",_", STEM_GROUP_OFF, status);
b.add(u",?", STEM_GROUP_MIN2, status);
b.add(u",!", STEM_GROUP_ON_ALIGNED, status);
b.add(u"+!", STEM_SIGN_ALWAYS, status);
b.add(u"+_", STEM_SIGN_NEVER, status);
b.add(u"()", STEM_SIGN_ACCOUNTING, status);
b.add(u"()!", STEM_SIGN_ACCOUNTING_ALWAYS, status);
b.add(u"+?", STEM_SIGN_EXCEPT_ZERO, status);
b.add(u"()?", STEM_SIGN_ACCOUNTING_EXCEPT_ZERO, status);
b.add(u"+-", STEM_SIGN_NEGATIVE, status);
b.add(u"()-", STEM_SIGN_ACCOUNTING_NEGATIVE, status);
if (U_FAILURE(status)) { return; }
// Build the CharsTrie
// TODO: Use SLOW or FAST here?
UnicodeString result;
b.buildUnicodeString(USTRINGTRIE_BUILD_FAST, result, status);
if (U_FAILURE(status)) { return; }
// Copy the result into the global constant pointer
size_t numBytes = result.length() * sizeof(char16_t);
kSerializedStemTrie = static_cast<char16_t*>(uprv_malloc(numBytes));
uprv_memcpy(kSerializedStemTrie, result.getBuffer(), numBytes);
}
inline void appendMultiple(UnicodeString& sb, UChar32 cp, int32_t count) {
for (int i = 0; i < count; i++) {
sb.append(cp);
}
}
#define CHECK_NULL(seen, field, status) (void)(seen); /* for auto-format line wrapping */ \
UPRV_BLOCK_MACRO_BEGIN { \
if ((seen).field) { \
(status) = U_NUMBER_SKELETON_SYNTAX_ERROR; \
return STATE_NULL; \
} \
(seen).field = true; \
} UPRV_BLOCK_MACRO_END
} // anonymous namespace
Notation stem_to_object::notation(skeleton::StemEnum stem) {
switch (stem) {
case STEM_COMPACT_SHORT:
return Notation::compactShort();
case STEM_COMPACT_LONG:
return Notation::compactLong();
case STEM_SCIENTIFIC:
return Notation::scientific();
case STEM_ENGINEERING:
return Notation::engineering();
case STEM_NOTATION_SIMPLE:
return Notation::simple();
default:
UPRV_UNREACHABLE_EXIT;
}
}
MeasureUnit stem_to_object::unit(skeleton::StemEnum stem) {
switch (stem) {
case STEM_BASE_UNIT:
return MeasureUnit();
case STEM_PERCENT:
return MeasureUnit::getPercent();
case STEM_PERMILLE:
return MeasureUnit::getPermille();
default:
UPRV_UNREACHABLE_EXIT;
}
}
Precision stem_to_object::precision(skeleton::StemEnum stem) {
switch (stem) {
case STEM_PRECISION_INTEGER:
return Precision::integer();
case STEM_PRECISION_UNLIMITED:
return Precision::unlimited();
case STEM_PRECISION_CURRENCY_STANDARD:
return Precision::currency(UCURR_USAGE_STANDARD);
case STEM_PRECISION_CURRENCY_CASH:
return Precision::currency(UCURR_USAGE_CASH);
default:
UPRV_UNREACHABLE_EXIT;
}
}
UNumberFormatRoundingMode stem_to_object::roundingMode(skeleton::StemEnum stem) {
switch (stem) {
case STEM_ROUNDING_MODE_CEILING:
return UNUM_ROUND_CEILING;
case STEM_ROUNDING_MODE_FLOOR:
return UNUM_ROUND_FLOOR;
case STEM_ROUNDING_MODE_DOWN:
return UNUM_ROUND_DOWN;
case STEM_ROUNDING_MODE_UP:
return UNUM_ROUND_UP;
case STEM_ROUNDING_MODE_HALF_EVEN:
return UNUM_ROUND_HALFEVEN;
case STEM_ROUNDING_MODE_HALF_ODD:
return UNUM_ROUND_HALF_ODD;
case STEM_ROUNDING_MODE_HALF_CEILING:
return UNUM_ROUND_HALF_CEILING;
case STEM_ROUNDING_MODE_HALF_FLOOR:
return UNUM_ROUND_HALF_FLOOR;
case STEM_ROUNDING_MODE_HALF_DOWN:
return UNUM_ROUND_HALFDOWN;
case STEM_ROUNDING_MODE_HALF_UP:
return UNUM_ROUND_HALFUP;
case STEM_ROUNDING_MODE_UNNECESSARY:
return UNUM_ROUND_UNNECESSARY;
default:
UPRV_UNREACHABLE_EXIT;
}
}
UNumberGroupingStrategy stem_to_object::groupingStrategy(skeleton::StemEnum stem) {
switch (stem) {
case STEM_GROUP_OFF:
return UNUM_GROUPING_OFF;
case STEM_GROUP_MIN2:
return UNUM_GROUPING_MIN2;
case STEM_GROUP_AUTO:
return UNUM_GROUPING_AUTO;
case STEM_GROUP_ON_ALIGNED:
return UNUM_GROUPING_ON_ALIGNED;
case STEM_GROUP_THOUSANDS:
return UNUM_GROUPING_THOUSANDS;
default:
return UNUM_GROUPING_COUNT; // for objects, throw; for enums, return COUNT
}
}
UNumberUnitWidth stem_to_object::unitWidth(skeleton::StemEnum stem) {
switch (stem) {
case STEM_UNIT_WIDTH_NARROW:
return UNUM_UNIT_WIDTH_NARROW;
case STEM_UNIT_WIDTH_SHORT:
return UNUM_UNIT_WIDTH_SHORT;
case STEM_UNIT_WIDTH_FULL_NAME:
return UNUM_UNIT_WIDTH_FULL_NAME;
case STEM_UNIT_WIDTH_ISO_CODE:
return UNUM_UNIT_WIDTH_ISO_CODE;
case STEM_UNIT_WIDTH_FORMAL:
return UNUM_UNIT_WIDTH_FORMAL;
case STEM_UNIT_WIDTH_VARIANT:
return UNUM_UNIT_WIDTH_VARIANT;
case STEM_UNIT_WIDTH_HIDDEN:
return UNUM_UNIT_WIDTH_HIDDEN;
default:
return UNUM_UNIT_WIDTH_COUNT; // for objects, throw; for enums, return COUNT
}
}
UNumberSignDisplay stem_to_object::signDisplay(skeleton::StemEnum stem) {
switch (stem) {
case STEM_SIGN_AUTO:
return UNUM_SIGN_AUTO;
case STEM_SIGN_ALWAYS:
return UNUM_SIGN_ALWAYS;
case STEM_SIGN_NEVER:
return UNUM_SIGN_NEVER;
case STEM_SIGN_ACCOUNTING:
return UNUM_SIGN_ACCOUNTING;
case STEM_SIGN_ACCOUNTING_ALWAYS:
return UNUM_SIGN_ACCOUNTING_ALWAYS;
case STEM_SIGN_EXCEPT_ZERO:
return UNUM_SIGN_EXCEPT_ZERO;
case STEM_SIGN_ACCOUNTING_EXCEPT_ZERO:
return UNUM_SIGN_ACCOUNTING_EXCEPT_ZERO;
case STEM_SIGN_NEGATIVE:
return UNUM_SIGN_NEGATIVE;
case STEM_SIGN_ACCOUNTING_NEGATIVE:
return UNUM_SIGN_ACCOUNTING_NEGATIVE;
default:
return UNUM_SIGN_COUNT; // for objects, throw; for enums, return COUNT
}
}
UNumberDecimalSeparatorDisplay stem_to_object::decimalSeparatorDisplay(skeleton::StemEnum stem) {
switch (stem) {
case STEM_DECIMAL_AUTO:
return UNUM_DECIMAL_SEPARATOR_AUTO;
case STEM_DECIMAL_ALWAYS:
return UNUM_DECIMAL_SEPARATOR_ALWAYS;
default:
return UNUM_DECIMAL_SEPARATOR_COUNT; // for objects, throw; for enums, return COUNT
}
}
void enum_to_stem_string::roundingMode(UNumberFormatRoundingMode value, UnicodeString& sb) {
switch (value) {
case UNUM_ROUND_CEILING:
sb.append(u"rounding-mode-ceiling", -1);
break;
case UNUM_ROUND_FLOOR:
sb.append(u"rounding-mode-floor", -1);
break;
case UNUM_ROUND_DOWN:
sb.append(u"rounding-mode-down", -1);
break;
case UNUM_ROUND_UP:
sb.append(u"rounding-mode-up", -1);
break;
case UNUM_ROUND_HALFEVEN:
sb.append(u"rounding-mode-half-even", -1);
break;
case UNUM_ROUND_HALF_ODD:
sb.append(u"rounding-mode-half-odd", -1);
break;
case UNUM_ROUND_HALF_CEILING:
sb.append(u"rounding-mode-half-ceiling", -1);
break;
case UNUM_ROUND_HALF_FLOOR:
sb.append(u"rounding-mode-half-floor", -1);
break;
case UNUM_ROUND_HALFDOWN:
sb.append(u"rounding-mode-half-down", -1);
break;
case UNUM_ROUND_HALFUP:
sb.append(u"rounding-mode-half-up", -1);
break;
case UNUM_ROUND_UNNECESSARY:
sb.append(u"rounding-mode-unnecessary", -1);
break;
default:
UPRV_UNREACHABLE_EXIT;
}
}
void enum_to_stem_string::groupingStrategy(UNumberGroupingStrategy value, UnicodeString& sb) {
switch (value) {
case UNUM_GROUPING_OFF:
sb.append(u"group-off", -1);
break;
case UNUM_GROUPING_MIN2:
sb.append(u"group-min2", -1);
break;
case UNUM_GROUPING_AUTO:
sb.append(u"group-auto", -1);
break;
case UNUM_GROUPING_ON_ALIGNED:
sb.append(u"group-on-aligned", -1);
break;
case UNUM_GROUPING_THOUSANDS:
sb.append(u"group-thousands", -1);
break;
default:
UPRV_UNREACHABLE_EXIT;
}
}
void enum_to_stem_string::unitWidth(UNumberUnitWidth value, UnicodeString& sb) {
switch (value) {
case UNUM_UNIT_WIDTH_NARROW:
sb.append(u"unit-width-narrow", -1);
break;
case UNUM_UNIT_WIDTH_SHORT:
sb.append(u"unit-width-short", -1);
break;
case UNUM_UNIT_WIDTH_FULL_NAME:
sb.append(u"unit-width-full-name", -1);
break;
case UNUM_UNIT_WIDTH_ISO_CODE:
sb.append(u"unit-width-iso-code", -1);
break;
case UNUM_UNIT_WIDTH_FORMAL:
sb.append(u"unit-width-formal", -1);
break;
case UNUM_UNIT_WIDTH_VARIANT:
sb.append(u"unit-width-variant", -1);
break;
case UNUM_UNIT_WIDTH_HIDDEN:
sb.append(u"unit-width-hidden", -1);
break;
default:
UPRV_UNREACHABLE_EXIT;
}
}
void enum_to_stem_string::signDisplay(UNumberSignDisplay value, UnicodeString& sb) {
switch (value) {
case UNUM_SIGN_AUTO:
sb.append(u"sign-auto", -1);
break;
case UNUM_SIGN_ALWAYS:
sb.append(u"sign-always", -1);
break;
case UNUM_SIGN_NEVER:
sb.append(u"sign-never", -1);
break;
case UNUM_SIGN_ACCOUNTING:
sb.append(u"sign-accounting", -1);
break;
case UNUM_SIGN_ACCOUNTING_ALWAYS:
sb.append(u"sign-accounting-always", -1);
break;
case UNUM_SIGN_EXCEPT_ZERO:
sb.append(u"sign-except-zero", -1);
break;
case UNUM_SIGN_ACCOUNTING_EXCEPT_ZERO:
sb.append(u"sign-accounting-except-zero", -1);
break;
case UNUM_SIGN_NEGATIVE:
sb.append(u"sign-negative", -1);
break;
case UNUM_SIGN_ACCOUNTING_NEGATIVE:
sb.append(u"sign-accounting-negative", -1);
break;
default:
UPRV_UNREACHABLE_EXIT;
}
}
void
enum_to_stem_string::decimalSeparatorDisplay(UNumberDecimalSeparatorDisplay value, UnicodeString& sb) {
switch (value) {
case UNUM_DECIMAL_SEPARATOR_AUTO:
sb.append(u"decimal-auto", -1);
break;
case UNUM_DECIMAL_SEPARATOR_ALWAYS:
sb.append(u"decimal-always", -1);
break;
default:
UPRV_UNREACHABLE_EXIT;
}
}
UnlocalizedNumberFormatter skeleton::create(
const UnicodeString& skeletonString, UParseError* perror, UErrorCode& status) {
// Initialize perror
if (perror != nullptr) {
perror->line = 0;
perror->offset = -1;
perror->preContext[0] = 0;
perror->postContext[0] = 0;
}
umtx_initOnce(gNumberSkeletonsInitOnce, &initNumberSkeletons, status);
if (U_FAILURE(status)) {
return {};
}
int32_t errOffset;
MacroProps macros = parseSkeleton(skeletonString, errOffset, status);
if (U_SUCCESS(status)) {
return NumberFormatter::with().macros(macros);
}
if (perror == nullptr) {
return {};
}
// Populate the UParseError with the error location
perror->offset = errOffset;
int32_t contextStart = uprv_max(0, errOffset - U_PARSE_CONTEXT_LEN + 1);
int32_t contextEnd = uprv_min(skeletonString.length(), errOffset + U_PARSE_CONTEXT_LEN - 1);
skeletonString.extract(contextStart, errOffset - contextStart, perror->preContext, 0);
perror->preContext[errOffset - contextStart] = 0;
skeletonString.extract(errOffset, contextEnd - errOffset, perror->postContext, 0);
perror->postContext[contextEnd - errOffset] = 0;
return {};
}
UnicodeString skeleton::generate(const MacroProps& macros, UErrorCode& status) {
umtx_initOnce(gNumberSkeletonsInitOnce, &initNumberSkeletons, status);
UnicodeString sb;
GeneratorHelpers::generateSkeleton(macros, sb, status);
return sb;
}
MacroProps skeleton::parseSkeleton(
const UnicodeString& skeletonString, int32_t& errOffset, UErrorCode& status) {
U_ASSERT(U_SUCCESS(status));
U_ASSERT(kSerializedStemTrie != nullptr);
// Add a trailing whitespace to the end of the skeleton string to make code cleaner.
UnicodeString tempSkeletonString(skeletonString);
tempSkeletonString.append(u' ');
SeenMacroProps seen;
MacroProps macros;
StringSegment segment(tempSkeletonString, false);
UCharsTrie stemTrie(kSerializedStemTrie);
ParseState stem = STATE_NULL;
int32_t offset = 0;
// Primary skeleton parse loop:
while (offset < segment.length()) {
UChar32 cp = segment.codePointAt(offset);
bool isTokenSeparator = PatternProps::isWhiteSpace(cp);
bool isOptionSeparator = (cp == u'/');
if (!isTokenSeparator && !isOptionSeparator) {
// Non-separator token; consume it.
offset += U16_LENGTH(cp);
if (stem == STATE_NULL) {
// We are currently consuming a stem.
// Go to the next state in the stem trie.
stemTrie.nextForCodePoint(cp);
}
continue;
}
// We are looking at a token or option separator.
// If the segment is nonempty, parse it and reset the segment.
// Otherwise, make sure it is a valid repeating separator.
if (offset != 0) {
segment.setLength(offset);
if (stem == STATE_NULL) {
// The first separator after the start of a token. Parse it as a stem.
stem = parseStem(segment, stemTrie, seen, macros, status);
stemTrie.reset();
} else {
// A separator after the first separator of a token. Parse it as an option.
stem = parseOption(stem, segment, macros, status);
}
segment.resetLength();
if (U_FAILURE(status)) {
errOffset = segment.getOffset();
return macros;
}
// Consume the segment:
segment.adjustOffset(offset);
offset = 0;
} else if (stem != STATE_NULL) {
// A separator ('/' or whitespace) following an option separator ('/')
// segment.setLength(U16_LENGTH(cp)); // for error message
// throw new SkeletonSyntaxException("Unexpected separator character", segment);
status = U_NUMBER_SKELETON_SYNTAX_ERROR;
errOffset = segment.getOffset();
return macros;
} else {
// Two spaces in a row; this is OK.
}
// Does the current stem forbid options?
if (isOptionSeparator && stem == STATE_NULL) {
// segment.setLength(U16_LENGTH(cp)); // for error message
// throw new SkeletonSyntaxException("Unexpected option separator", segment);
status = U_NUMBER_SKELETON_SYNTAX_ERROR;
errOffset = segment.getOffset();
return macros;
}
// Does the current stem require an option?
if (isTokenSeparator && stem != STATE_NULL) {
switch (stem) {
case STATE_INCREMENT_PRECISION:
case STATE_MEASURE_UNIT:
case STATE_PER_MEASURE_UNIT:
case STATE_IDENTIFIER_UNIT:
case STATE_UNIT_USAGE:
case STATE_CURRENCY_UNIT:
case STATE_INTEGER_WIDTH:
case STATE_NUMBERING_SYSTEM:
case STATE_SCALE:
// segment.setLength(U16_LENGTH(cp)); // for error message
// throw new SkeletonSyntaxException("Stem requires an option", segment);
status = U_NUMBER_SKELETON_SYNTAX_ERROR;
errOffset = segment.getOffset();
return macros;
default:
break;
}
stem = STATE_NULL;
}
// Consume the separator:
segment.adjustOffset(U16_LENGTH(cp));
}
U_ASSERT(stem == STATE_NULL);
return macros;
}
ParseState
skeleton::parseStem(const StringSegment& segment, const UCharsTrie& stemTrie, SeenMacroProps& seen,
MacroProps& macros, UErrorCode& status) {
U_ASSERT(U_SUCCESS(status));
// First check for "blueprint" stems, which start with a "signal char"
switch (segment.charAt(0)) {
case u'.':
CHECK_NULL(seen, precision, status);
blueprint_helpers::parseFractionStem(segment, macros, status);
return STATE_FRACTION_PRECISION;
case u'@':
CHECK_NULL(seen, precision, status);
blueprint_helpers::parseDigitsStem(segment, macros, status);
return STATE_PRECISION;
case u'E':
CHECK_NULL(seen, notation, status);
blueprint_helpers::parseScientificStem(segment, macros, status);
return STATE_NULL;
case u'0':
CHECK_NULL(seen, integerWidth, status);
blueprint_helpers::parseIntegerStem(segment, macros, status);
return STATE_NULL;
default:
break;
}
// Now look at the stemsTrie, which is already be pointing at our stem.
UStringTrieResult stemResult = stemTrie.current();
if (stemResult != USTRINGTRIE_INTERMEDIATE_VALUE && stemResult != USTRINGTRIE_FINAL_VALUE) {
// throw new SkeletonSyntaxException("Unknown stem", segment);
status = U_NUMBER_SKELETON_SYNTAX_ERROR;
return STATE_NULL;
}
auto stem = static_cast<StemEnum>(stemTrie.getValue());
switch (stem) {
// Stems with meaning on their own, not requiring an option:
case STEM_COMPACT_SHORT:
case STEM_COMPACT_LONG:
case STEM_SCIENTIFIC:
case STEM_ENGINEERING:
case STEM_NOTATION_SIMPLE:
CHECK_NULL(seen, notation, status);
macros.notation = stem_to_object::notation(stem);
switch (stem) {
case STEM_SCIENTIFIC:
case STEM_ENGINEERING:
return STATE_SCIENTIFIC; // allows for scientific options
default:
return STATE_NULL;
}
case STEM_BASE_UNIT:
case STEM_PERCENT:
case STEM_PERMILLE:
CHECK_NULL(seen, unit, status);
macros.unit = stem_to_object::unit(stem);
return STATE_NULL;
case STEM_PERCENT_100:
CHECK_NULL(seen, scale, status);
CHECK_NULL(seen, unit, status);
macros.scale = Scale::powerOfTen(2);
macros.unit = NoUnit::percent();
return STATE_NULL;
case STEM_PRECISION_INTEGER:
case STEM_PRECISION_UNLIMITED:
case STEM_PRECISION_CURRENCY_STANDARD:
case STEM_PRECISION_CURRENCY_CASH:
CHECK_NULL(seen, precision, status);
macros.precision = stem_to_object::precision(stem);
switch (stem) {
case STEM_PRECISION_INTEGER:
return STATE_FRACTION_PRECISION; // allows for "precision-integer/@##"
default:
return STATE_PRECISION;
}
case STEM_ROUNDING_MODE_CEILING:
case STEM_ROUNDING_MODE_FLOOR:
case STEM_ROUNDING_MODE_DOWN:
case STEM_ROUNDING_MODE_UP:
case STEM_ROUNDING_MODE_HALF_EVEN:
case STEM_ROUNDING_MODE_HALF_ODD:
case STEM_ROUNDING_MODE_HALF_CEILING:
case STEM_ROUNDING_MODE_HALF_FLOOR:
case STEM_ROUNDING_MODE_HALF_DOWN:
case STEM_ROUNDING_MODE_HALF_UP:
case STEM_ROUNDING_MODE_UNNECESSARY:
CHECK_NULL(seen, roundingMode, status);
macros.roundingMode = stem_to_object::roundingMode(stem);
return STATE_NULL;
case STEM_INTEGER_WIDTH_TRUNC:
CHECK_NULL(seen, integerWidth, status);
macros.integerWidth = IntegerWidth::zeroFillTo(0).truncateAt(0);
return STATE_NULL;
case STEM_GROUP_OFF:
case STEM_GROUP_MIN2:
case STEM_GROUP_AUTO:
case STEM_GROUP_ON_ALIGNED:
case STEM_GROUP_THOUSANDS:
CHECK_NULL(seen, grouper, status);
macros.grouper = Grouper::forStrategy(stem_to_object::groupingStrategy(stem));
return STATE_NULL;
case STEM_LATIN:
CHECK_NULL(seen, symbols, status);
macros.symbols.setTo(NumberingSystem::createInstanceByName("latn", status));
return STATE_NULL;
case STEM_UNIT_WIDTH_NARROW:
case STEM_UNIT_WIDTH_SHORT:
case STEM_UNIT_WIDTH_FULL_NAME:
case STEM_UNIT_WIDTH_ISO_CODE:
case STEM_UNIT_WIDTH_FORMAL:
case STEM_UNIT_WIDTH_VARIANT:
case STEM_UNIT_WIDTH_HIDDEN:
CHECK_NULL(seen, unitWidth, status);
macros.unitWidth = stem_to_object::unitWidth(stem);
return STATE_NULL;
case STEM_SIGN_AUTO:
case STEM_SIGN_ALWAYS:
case STEM_SIGN_NEVER:
case STEM_SIGN_ACCOUNTING:
case STEM_SIGN_ACCOUNTING_ALWAYS:
case STEM_SIGN_EXCEPT_ZERO:
case STEM_SIGN_ACCOUNTING_EXCEPT_ZERO:
case STEM_SIGN_NEGATIVE:
case STEM_SIGN_ACCOUNTING_NEGATIVE:
CHECK_NULL(seen, sign, status);
macros.sign = stem_to_object::signDisplay(stem);
return STATE_NULL;
case STEM_DECIMAL_AUTO:
case STEM_DECIMAL_ALWAYS:
CHECK_NULL(seen, decimal, status);
macros.decimal = stem_to_object::decimalSeparatorDisplay(stem);
return STATE_NULL;
// Stems requiring an option:
case STEM_PRECISION_INCREMENT:
CHECK_NULL(seen, precision, status);
return STATE_INCREMENT_PRECISION;
case STEM_MEASURE_UNIT:
CHECK_NULL(seen, unit, status);
return STATE_MEASURE_UNIT;
case STEM_PER_MEASURE_UNIT:
CHECK_NULL(seen, perUnit, status);
return STATE_PER_MEASURE_UNIT;
case STEM_UNIT:
CHECK_NULL(seen, unit, status);
CHECK_NULL(seen, perUnit, status);
return STATE_IDENTIFIER_UNIT;
case STEM_UNIT_USAGE:
CHECK_NULL(seen, usage, status);
return STATE_UNIT_USAGE;
case STEM_CURRENCY:
CHECK_NULL(seen, unit, status);
CHECK_NULL(seen, perUnit, status);
return STATE_CURRENCY_UNIT;
case STEM_INTEGER_WIDTH:
CHECK_NULL(seen, integerWidth, status);
return STATE_INTEGER_WIDTH;
case STEM_NUMBERING_SYSTEM:
CHECK_NULL(seen, symbols, status);
return STATE_NUMBERING_SYSTEM;
case STEM_SCALE:
CHECK_NULL(seen, scale, status);
return STATE_SCALE;
default:
UPRV_UNREACHABLE_EXIT;
}
}
ParseState skeleton::parseOption(ParseState stem, const StringSegment& segment, MacroProps& macros,
UErrorCode& status) {
U_ASSERT(U_SUCCESS(status));
///// Required options: /////
switch (stem) {
case STATE_CURRENCY_UNIT:
blueprint_helpers::parseCurrencyOption(segment, macros, status);
return STATE_NULL;
case STATE_MEASURE_UNIT:
blueprint_helpers::parseMeasureUnitOption(segment, macros, status);
return STATE_NULL;
case STATE_PER_MEASURE_UNIT:
blueprint_helpers::parseMeasurePerUnitOption(segment, macros, status);
return STATE_NULL;
case STATE_IDENTIFIER_UNIT:
blueprint_helpers::parseIdentifierUnitOption(segment, macros, status);
return STATE_NULL;
case STATE_UNIT_USAGE:
blueprint_helpers::parseUnitUsageOption(segment, macros, status);
return STATE_NULL;
case STATE_INCREMENT_PRECISION:
blueprint_helpers::parseIncrementOption(segment, macros, status);
return STATE_PRECISION;
case STATE_INTEGER_WIDTH:
blueprint_helpers::parseIntegerWidthOption(segment, macros, status);
return STATE_NULL;
case STATE_NUMBERING_SYSTEM:
blueprint_helpers::parseNumberingSystemOption(segment, macros, status);
return STATE_NULL;
case STATE_SCALE:
blueprint_helpers::parseScaleOption(segment, macros, status);
return STATE_NULL;
default:
break;
}
///// Non-required options: /////
// Scientific options
switch (stem) {
case STATE_SCIENTIFIC:
if (blueprint_helpers::parseExponentWidthOption(segment, macros, status)) {
return STATE_SCIENTIFIC;
}
if (U_FAILURE(status)) {
return {};
}
if (blueprint_helpers::parseExponentSignOption(segment, macros, status)) {
return STATE_SCIENTIFIC;
}
if (U_FAILURE(status)) {
return {};
}
break;
default:
break;
}
// Frac-sig option
switch (stem) {
case STATE_FRACTION_PRECISION:
if (blueprint_helpers::parseFracSigOption(segment, macros, status)) {
return STATE_PRECISION;
}
if (U_FAILURE(status)) {
return {};
}
// If the fracSig option was not found, try normal precision options.
stem = STATE_PRECISION;
break;
default:
break;
}
// Trailing zeros option
switch (stem) {
case STATE_PRECISION:
if (blueprint_helpers::parseTrailingZeroOption(segment, macros, status)) {
return STATE_NULL;
}
if (U_FAILURE(status)) {
return {};
}
break;
default:
break;
}
// Unknown option
// throw new SkeletonSyntaxException("Invalid option", segment);
status = U_NUMBER_SKELETON_SYNTAX_ERROR;
return STATE_NULL;
}
void GeneratorHelpers::generateSkeleton(const MacroProps& macros, UnicodeString& sb, UErrorCode& status) {
if (U_FAILURE(status)) { return; }
// Supported options
if (GeneratorHelpers::notation(macros, sb, status)) {
sb.append(u' ');
}
if (U_FAILURE(status)) { return; }
if (GeneratorHelpers::unit(macros, sb, status)) {
sb.append(u' ');
}
if (U_FAILURE(status)) { return; }
if (GeneratorHelpers::usage(macros, sb, status)) {
sb.append(u' ');
}
if (U_FAILURE(status)) { return; }
if (GeneratorHelpers::precision(macros, sb, status)) {
sb.append(u' ');
}
if (U_FAILURE(status)) { return; }
if (GeneratorHelpers::roundingMode(macros, sb, status)) {
sb.append(u' ');
}
if (U_FAILURE(status)) { return; }
if (GeneratorHelpers::grouping(macros, sb, status)) {
sb.append(u' ');
}
if (U_FAILURE(status)) { return; }
if (GeneratorHelpers::integerWidth(macros, sb, status)) {
sb.append(u' ');
}
if (U_FAILURE(status)) { return; }
if (GeneratorHelpers::symbols(macros, sb, status)) {
sb.append(u' ');
}
if (U_FAILURE(status)) { return; }
if (GeneratorHelpers::unitWidth(macros, sb, status)) {
sb.append(u' ');
}
if (U_FAILURE(status)) { return; }
if (GeneratorHelpers::sign(macros, sb, status)) {
sb.append(u' ');
}
if (U_FAILURE(status)) { return; }
if (GeneratorHelpers::decimal(macros, sb, status)) {
sb.append(u' ');
}
if (U_FAILURE(status)) { return; }
if (GeneratorHelpers::scale(macros, sb, status)) {
sb.append(u' ');
}
if (U_FAILURE(status)) { return; }
// Unsupported options
if (!macros.padder.isBogus()) {
status = U_UNSUPPORTED_ERROR;
return;
}
if (macros.unitDisplayCase.isSet()) {
status = U_UNSUPPORTED_ERROR;
return;
}
if (macros.affixProvider != nullptr) {
status = U_UNSUPPORTED_ERROR;
return;
}
if (macros.rules != nullptr) {
status = U_UNSUPPORTED_ERROR;
return;
}
// Remove the trailing space
if (sb.length() > 0) {
sb.truncate(sb.length() - 1);
}
}
bool blueprint_helpers::parseExponentWidthOption(const StringSegment& segment, MacroProps& macros,
UErrorCode&) {
if (!isWildcardChar(segment.charAt(0))) {
return false;
}
int32_t offset = 1;
int32_t minExp = 0;
for (; offset < segment.length(); offset++) {
if (segment.charAt(offset) == u'e') {
minExp++;
} else {
break;
}
}
if (offset < segment.length()) {
return false;
}
// Use the public APIs to enforce bounds checking
macros.notation = static_cast<ScientificNotation&>(macros.notation).withMinExponentDigits(minExp);
return true;
}
void
blueprint_helpers::generateExponentWidthOption(int32_t minExponentDigits, UnicodeString& sb, UErrorCode&) {
sb.append(kWildcardChar);
appendMultiple(sb, u'e', minExponentDigits);
}
bool
blueprint_helpers::parseExponentSignOption(const StringSegment& segment, MacroProps& macros, UErrorCode&) {
// Get the sign display type out of the CharsTrie data structure.
UCharsTrie tempStemTrie(kSerializedStemTrie);
UStringTrieResult result = tempStemTrie.next(
segment.toTempUnicodeString().getBuffer(),
segment.length());
if (result != USTRINGTRIE_INTERMEDIATE_VALUE && result != USTRINGTRIE_FINAL_VALUE) {
return false;
}
auto sign = stem_to_object::signDisplay(static_cast<StemEnum>(tempStemTrie.getValue()));
if (sign == UNUM_SIGN_COUNT) {
return false;
}
macros.notation = static_cast<ScientificNotation&>(macros.notation).withExponentSignDisplay(sign);
return true;
}
void blueprint_helpers::parseCurrencyOption(const StringSegment& segment, MacroProps& macros,
UErrorCode& status) {
// Unlike ICU4J, have to check length manually because ICU4C CurrencyUnit does not check it for us
if (segment.length() != 3) {
status = U_NUMBER_SKELETON_SYNTAX_ERROR;
return;
}
const char16_t* currencyCode = segment.toTempUnicodeString().getBuffer();
UErrorCode localStatus = U_ZERO_ERROR;
CurrencyUnit currency(currencyCode, localStatus);
if (U_FAILURE(localStatus)) {
// Not 3 ascii chars
// throw new SkeletonSyntaxException("Invalid currency", segment);
status = U_NUMBER_SKELETON_SYNTAX_ERROR;
return;
}
// Slicing is OK
macros.unit = currency; // NOLINT
}
void
blueprint_helpers::generateCurrencyOption(const CurrencyUnit& currency, UnicodeString& sb, UErrorCode&) {
sb.append(currency.getISOCurrency(), -1);
}
void blueprint_helpers::parseMeasureUnitOption(const StringSegment& segment, MacroProps& macros,
UErrorCode& status) {
U_ASSERT(U_SUCCESS(status));
const UnicodeString stemString = segment.toTempUnicodeString();
// NOTE: The category (type) of the unit is guaranteed to be a valid subtag (alphanumeric)
// http://unicode.org/reports/tr35/#Validity_Data
int firstHyphen = 0;
while (firstHyphen < stemString.length() && stemString.charAt(firstHyphen) != '-') {
firstHyphen++;
}
if (firstHyphen == stemString.length()) {
// throw new SkeletonSyntaxException("Invalid measure unit option", segment);
status = U_NUMBER_SKELETON_SYNTAX_ERROR;
return;
}
// Need to do char <-> char16_t conversion...
CharString type;
SKELETON_UCHAR_TO_CHAR(type, stemString, 0, firstHyphen, status);
CharString subType;
SKELETON_UCHAR_TO_CHAR(subType, stemString, firstHyphen + 1, stemString.length(), status);
// Note: the largest type as of this writing (Aug 2020) is "volume", which has 33 units.
static constexpr int32_t CAPACITY = 40;
MeasureUnit units[CAPACITY];
UErrorCode localStatus = U_ZERO_ERROR;
int32_t numUnits = MeasureUnit::getAvailable(type.data(), units, CAPACITY, localStatus);
if (U_FAILURE(localStatus)) {
// More than 30 units in this type?
status = U_INTERNAL_PROGRAM_ERROR;
return;
}
for (int32_t i = 0; i < numUnits; i++) {
auto& unit = units[i];
if (uprv_strcmp(subType.data(), unit.getSubtype()) == 0) {
macros.unit = unit;
return;
}
}
// throw new SkeletonSyntaxException("Unknown measure unit", segment);
status = U_NUMBER_SKELETON_SYNTAX_ERROR;
}
void blueprint_helpers::parseMeasurePerUnitOption(const StringSegment& segment, MacroProps& macros,
UErrorCode& status) {
// A little bit of a hack: save the current unit (numerator), call the main measure unit
// parsing code, put back the numerator unit, and put the new unit into per-unit.
MeasureUnit numerator = macros.unit;
parseMeasureUnitOption(segment, macros, status);
if (U_FAILURE(status)) { return; }
macros.perUnit = macros.unit;
macros.unit = numerator;
}
void blueprint_helpers::parseIdentifierUnitOption(const StringSegment& segment, MacroProps& macros,
UErrorCode& status) {
// Need to do char <-> char16_t conversion...
U_ASSERT(U_SUCCESS(status));
CharString buffer;
SKELETON_UCHAR_TO_CHAR(buffer, segment.toTempUnicodeString(), 0, segment.length(), status);
ErrorCode internalStatus;
macros.unit = MeasureUnit::forIdentifier(buffer.toStringPiece(), internalStatus);
if (internalStatus.isFailure()) {
// throw new SkeletonSyntaxException("Invalid core unit identifier", segment, e);
status = U_NUMBER_SKELETON_SYNTAX_ERROR;
return;
}
}
void blueprint_helpers::parseUnitUsageOption(const StringSegment &segment, MacroProps ¯os,
UErrorCode &status) {
// Need to do char <-> char16_t conversion...
U_ASSERT(U_SUCCESS(status));
CharString buffer;
SKELETON_UCHAR_TO_CHAR(buffer, segment.toTempUnicodeString(), 0, segment.length(), status);
macros.usage.set(buffer.toStringPiece());
// We do not do any validation of the usage string: it depends on the
// unitPreferenceData in the units resources.
}
void blueprint_helpers::parseFractionStem(const StringSegment& segment, MacroProps& macros,
UErrorCode& status) {
U_ASSERT(segment.charAt(0) == u'.');
int32_t offset = 1;
int32_t minFrac = 0;
int32_t maxFrac;
for (; offset < segment.length(); offset++) {
if (segment.charAt(offset) == u'0') {
minFrac++;
} else {
break;
}
}
if (offset < segment.length()) {
if (isWildcardChar(segment.charAt(offset))) {
maxFrac = -1;
offset++;
} else {
maxFrac = minFrac;
for (; offset < segment.length(); offset++) {
if (segment.charAt(offset) == u'#') {
maxFrac++;
} else {
break;
}
}
}
} else {
maxFrac = minFrac;
}
if (offset < segment.length()) {
// throw new SkeletonSyntaxException("Invalid fraction stem", segment);
status = U_NUMBER_SKELETON_SYNTAX_ERROR;
return;
}
// Use the public APIs to enforce bounds checking
if (maxFrac == -1) {
if (minFrac == 0) {
macros.precision = Precision::unlimited();
} else {
macros.precision = Precision::minFraction(minFrac);
}
} else {
macros.precision = Precision::minMaxFraction(minFrac, maxFrac);
}
}
void
blueprint_helpers::generateFractionStem(int32_t minFrac, int32_t maxFrac, UnicodeString& sb, UErrorCode&) {
if (minFrac == 0 && maxFrac == 0) {
sb.append(u"precision-integer", -1);
return;
}
sb.append(u'.');
appendMultiple(sb, u'0', minFrac);
if (maxFrac == -1) {
sb.append(kWildcardChar);
} else {
appendMultiple(sb, u'#', maxFrac - minFrac);
}
}
void
blueprint_helpers::parseDigitsStem(const StringSegment& segment, MacroProps& macros, UErrorCode& status) {
U_ASSERT(segment.charAt(0) == u'@');
int32_t offset = 0;
int32_t minSig = 0;
int32_t maxSig;
for (; offset < segment.length(); offset++) {
if (segment.charAt(offset) == u'@') {
minSig++;
} else {
break;
}
}
if (offset < segment.length()) {
if (isWildcardChar(segment.charAt(offset))) {
maxSig = -1;
offset++;
} else {
maxSig = minSig;
for (; offset < segment.length(); offset++) {
if (segment.charAt(offset) == u'#') {
maxSig++;
} else {
break;
}
}
}
} else {
maxSig = minSig;
}
if (offset < segment.length()) {
// throw new SkeletonSyntaxException("Invalid significant digits stem", segment);
status = U_NUMBER_SKELETON_SYNTAX_ERROR;
return;
}
// Use the public APIs to enforce bounds checking
if (maxSig == -1) {
macros.precision = Precision::minSignificantDigits(minSig);
} else {
macros.precision = Precision::minMaxSignificantDigits(minSig, maxSig);
}
}
void
blueprint_helpers::generateDigitsStem(int32_t minSig, int32_t maxSig, UnicodeString& sb, UErrorCode&) {
appendMultiple(sb, u'@', minSig);
if (maxSig == -1) {
sb.append(kWildcardChar);
} else {
appendMultiple(sb, u'#', maxSig - minSig);
}
}
void blueprint_helpers::parseScientificStem(const StringSegment& segment, MacroProps& macros, UErrorCode& status) {
U_ASSERT(segment.charAt(0) == u'E');
{
int32_t offset = 1;
if (segment.length() == offset) {
goto fail;
}
bool isEngineering = false;
if (segment.charAt(offset) == u'E') {
isEngineering = true;
offset++;
if (segment.length() == offset) {
goto fail;
}
}
UNumberSignDisplay signDisplay = UNUM_SIGN_AUTO;
if (segment.charAt(offset) == u'+') {
offset++;
if (segment.length() == offset) {
goto fail;
}
if (segment.charAt(offset) == u'!') {
signDisplay = UNUM_SIGN_ALWAYS;
} else if (segment.charAt(offset) == u'?') {
signDisplay = UNUM_SIGN_EXCEPT_ZERO;
} else {
// NOTE: Other sign displays are not included because they aren't useful in this context
goto fail;
}
offset++;
if (segment.length() == offset) {
goto fail;
}
}
int32_t minDigits = 0;
for (; offset < segment.length(); offset++) {
if (segment.charAt(offset) != u'0') {
goto fail;
}
minDigits++;
}
macros.notation = (isEngineering ? Notation::engineering() : Notation::scientific())
.withExponentSignDisplay(signDisplay)
.withMinExponentDigits(minDigits);
return;
}
fail: void();
// throw new SkeletonSyntaxException("Invalid scientific stem", segment);
status = U_NUMBER_SKELETON_SYNTAX_ERROR;
return;
}
void blueprint_helpers::parseIntegerStem(const StringSegment& segment, MacroProps& macros, UErrorCode& status) {
U_ASSERT(segment.charAt(0) == u'0');
int32_t offset = 1;
for (; offset < segment.length(); offset++) {
if (segment.charAt(offset) != u'0') {
offset--;
break;
}
}
if (offset < segment.length()) {
// throw new SkeletonSyntaxException("Invalid integer stem", segment);
status = U_NUMBER_SKELETON_SYNTAX_ERROR;
return;
}
macros.integerWidth = IntegerWidth::zeroFillTo(offset);
return;
}
bool blueprint_helpers::parseFracSigOption(const StringSegment& segment, MacroProps& macros,
UErrorCode& status) {
if (segment.charAt(0) != u'@') {
return false;
}
int offset = 0;
int minSig = 0;
int maxSig;
for (; offset < segment.length(); offset++) {
if (segment.charAt(offset) == u'@') {
minSig++;
} else {
break;
}
}
if (offset < segment.length()) {
if (isWildcardChar(segment.charAt(offset))) {
// @+, @@+, @@@+
maxSig = -1;
offset++;
} else {
// @#, @##, @###
// @@#, @@##, @@@#
maxSig = minSig;
for (; offset < segment.length(); offset++) {
if (segment.charAt(offset) == u'#') {
maxSig++;
} else {
break;
}
}
}
} else {
// @, @@, @@@
maxSig = minSig;
}
auto& oldPrecision = static_cast<const FractionPrecision&>(macros.precision);
if (offset < segment.length()) {
UNumberRoundingPriority priority;
if (maxSig == -1) {
// The wildcard character is not allowed with the priority annotation
status = U_NUMBER_SKELETON_SYNTAX_ERROR;
return false;
}
if (segment.codePointAt(offset) == u'r') {
priority = UNUM_ROUNDING_PRIORITY_RELAXED;
offset++;
} else if (segment.codePointAt(offset) == u's') {
priority = UNUM_ROUNDING_PRIORITY_STRICT;
offset++;
} else {
// Invalid digits option for fraction rounder
status = U_NUMBER_SKELETON_SYNTAX_ERROR;
return false;
}
if (offset < segment.length()) {
// Invalid digits option for fraction rounder
status = U_NUMBER_SKELETON_SYNTAX_ERROR;
return false;
}
macros.precision = oldPrecision.withSignificantDigits(minSig, maxSig, priority);
} else if (maxSig == -1) {
// withMinDigits
macros.precision = oldPrecision.withMinDigits(minSig);
} else if (minSig == 1) {
// withMaxDigits
macros.precision = oldPrecision.withMaxDigits(maxSig);
} else {
// Digits options with both min and max sig require the priority option
status = U_NUMBER_SKELETON_SYNTAX_ERROR;
return false;
}
return true;
}
bool blueprint_helpers::parseTrailingZeroOption(const StringSegment& segment, MacroProps& macros, UErrorCode&) {
if (segment == u"w") {
macros.precision = macros.precision.trailingZeroDisplay(UNUM_TRAILING_ZERO_HIDE_IF_WHOLE);
return true;
}
return false;
}
void blueprint_helpers::parseIncrementOption(const StringSegment &segment, MacroProps ¯os,
UErrorCode &status) {
number::impl::parseIncrementOption(segment, macros.precision, status);
}
void blueprint_helpers::generateIncrementOption(
uint32_t increment,
digits_t incrementMagnitude,
int32_t minFrac,
UnicodeString& sb,
UErrorCode&) {
// Utilize DecimalQuantity/double_conversion to format this for us.
DecimalQuantity dq;
dq.setToLong(increment);
dq.adjustMagnitude(incrementMagnitude);
dq.setMinFraction(minFrac);
sb.append(dq.toPlainString());
}
void blueprint_helpers::parseIntegerWidthOption(const StringSegment& segment, MacroProps& macros,
UErrorCode& status) {
int32_t offset = 0;
int32_t minInt = 0;
int32_t maxInt;
if (isWildcardChar(segment.charAt(0))) {
maxInt = -1;
offset++;
} else {
maxInt = 0;
}
for (; offset < segment.length(); offset++) {
if (maxInt != -1 && segment.charAt(offset) == u'#') {
maxInt++;
} else {
break;
}
}
if (offset < segment.length()) {
for (; offset < segment.length(); offset++) {
if (segment.charAt(offset) == u'0') {
minInt++;
} else {
break;
}
}
}
if (maxInt != -1) {
maxInt += minInt;
}
if (offset < segment.length()) {
// throw new SkeletonSyntaxException("Invalid integer width stem", segment);
status = U_NUMBER_SKELETON_SYNTAX_ERROR;
return;
}
// Use the public APIs to enforce bounds checking
if (maxInt == -1) {
macros.integerWidth = IntegerWidth::zeroFillTo(minInt);
} else {
macros.integerWidth = IntegerWidth::zeroFillTo(minInt).truncateAt(maxInt);
}
}
void blueprint_helpers::generateIntegerWidthOption(int32_t minInt, int32_t maxInt, UnicodeString& sb,
UErrorCode&) {
if (maxInt == -1) {
sb.append(kWildcardChar);
} else {
appendMultiple(sb, u'#', maxInt - minInt);
}
appendMultiple(sb, u'0', minInt);
}
void blueprint_helpers::parseNumberingSystemOption(const StringSegment& segment, MacroProps& macros,
UErrorCode& status) {
// Need to do char <-> char16_t conversion...
U_ASSERT(U_SUCCESS(status));
CharString buffer;
SKELETON_UCHAR_TO_CHAR(buffer, segment.toTempUnicodeString(), 0, segment.length(), status);
NumberingSystem* ns = NumberingSystem::createInstanceByName(buffer.data(), status);
if (ns == nullptr || U_FAILURE(status)) {
// This is a skeleton syntax error; don't bubble up the low-level NumberingSystem error
// throw new SkeletonSyntaxException("Unknown numbering system", segment);
status = U_NUMBER_SKELETON_SYNTAX_ERROR;
return;
}
macros.symbols.setTo(ns);
}
void blueprint_helpers::generateNumberingSystemOption(const NumberingSystem& ns, UnicodeString& sb,
UErrorCode&) {
// Need to do char <-> char16_t conversion...
sb.append(UnicodeString(ns.getName(), -1, US_INV));
}
void blueprint_helpers::parseScaleOption(const StringSegment& segment, MacroProps& macros,
UErrorCode& status) {
// Need to do char <-> char16_t conversion...
U_ASSERT(U_SUCCESS(status));
CharString buffer;
SKELETON_UCHAR_TO_CHAR(buffer, segment.toTempUnicodeString(), 0, segment.length(), status);
LocalPointer<DecNum> decnum(new DecNum(), status);
if (U_FAILURE(status)) { return; }
decnum->setTo({buffer.data(), buffer.length()}, status);
if (U_FAILURE(status) || decnum->isSpecial()) {
// This is a skeleton syntax error; don't let the low-level decnum error bubble up
status = U_NUMBER_SKELETON_SYNTAX_ERROR;
return;
}
// NOTE: The constructor will optimize the decnum for us if possible.
macros.scale = {0, decnum.orphan()};
}
void blueprint_helpers::generateScaleOption(int32_t magnitude, const DecNum* arbitrary, UnicodeString& sb,
UErrorCode& status) {
// Utilize DecimalQuantity/double_conversion to format this for us.
DecimalQuantity dq;
if (arbitrary != nullptr) {
dq.setToDecNum(*arbitrary, status);
if (U_FAILURE(status)) { return; }
} else {
dq.setToInt(1);
}
dq.adjustMagnitude(magnitude);
dq.roundToInfinity();
sb.append(dq.toPlainString());
}
bool GeneratorHelpers::notation(const MacroProps& macros, UnicodeString& sb, UErrorCode& status) {
if (macros.notation.fType == Notation::NTN_COMPACT) {
UNumberCompactStyle style = macros.notation.fUnion.compactStyle;
if (style == UNumberCompactStyle::UNUM_LONG) {
sb.append(u"compact-long", -1);
return true;
} else if (style == UNumberCompactStyle::UNUM_SHORT) {
sb.append(u"compact-short", -1);
return true;
} else {
// Compact notation generated from custom data (not supported in skeleton)
// The other compact notations are literals
status = U_UNSUPPORTED_ERROR;
return false;
}
} else if (macros.notation.fType == Notation::NTN_SCIENTIFIC) {
const Notation::ScientificSettings& impl = macros.notation.fUnion.scientific;
if (impl.fEngineeringInterval == 3) {
sb.append(u"engineering", -1);
} else {
sb.append(u"scientific", -1);
}
if (impl.fMinExponentDigits > 1) {
sb.append(u'/');
blueprint_helpers::generateExponentWidthOption(impl.fMinExponentDigits, sb, status);
if (U_FAILURE(status)) {
return false;
}
}
if (impl.fExponentSignDisplay != UNUM_SIGN_AUTO) {
sb.append(u'/');
enum_to_stem_string::signDisplay(impl.fExponentSignDisplay, sb);
}
return true;
} else {
// Default value is not shown in normalized form
return false;
}
}
bool GeneratorHelpers::unit(const MacroProps& macros, UnicodeString& sb, UErrorCode& status) {
MeasureUnit unit = macros.unit;
if (!utils::unitIsBaseUnit(macros.perUnit)) {
if (utils::unitIsCurrency(macros.unit) || utils::unitIsCurrency(macros.perUnit)) {
status = U_UNSUPPORTED_ERROR;
return false;
}
unit = unit.product(macros.perUnit.reciprocal(status), status);
}
if (utils::unitIsCurrency(unit)) {
sb.append(u"currency/", -1);
CurrencyUnit currency(unit, status);
if (U_FAILURE(status)) {
return false;
}
blueprint_helpers::generateCurrencyOption(currency, sb, status);
return true;
} else if (utils::unitIsBaseUnit(unit)) {
// Default value is not shown in normalized form
return false;
} else if (utils::unitIsPercent(unit)) {
sb.append(u"percent", -1);
return true;
} else if (utils::unitIsPermille(unit)) {
sb.append(u"permille", -1);
return true;
} else {
sb.append(u"unit/", -1);
sb.append(unit.getIdentifier());
return true;
}
}
bool GeneratorHelpers::usage(const MacroProps& macros, UnicodeString& sb, UErrorCode& /* status */) {
if (macros.usage.isSet()) {
sb.append(u"usage/", -1);
sb.append(UnicodeString(macros.usage.fValue, -1, US_INV));
return true;
}
return false;
}
bool GeneratorHelpers::precision(const MacroProps& macros, UnicodeString& sb, UErrorCode& status) {
if (macros.precision.fType == Precision::RND_NONE) {
sb.append(u"precision-unlimited", -1);
} else if (macros.precision.fType == Precision::RND_FRACTION) {
const Precision::FractionSignificantSettings& impl = macros.precision.fUnion.fracSig;
blueprint_helpers::generateFractionStem(impl.fMinFrac, impl.fMaxFrac, sb, status);
} else if (macros.precision.fType == Precision::RND_SIGNIFICANT) {
const Precision::FractionSignificantSettings& impl = macros.precision.fUnion.fracSig;
blueprint_helpers::generateDigitsStem(impl.fMinSig, impl.fMaxSig, sb, status);
#if APPLE_ICU_CHANGES
// rdar://52538227 8e26bee05d.. New Precision type combining roundingIncr and sig digits; use when both are set
} else if (macros.precision.fType == Precision::RND_INCREMENT_SIGNIFICANT) { // Apple rdar://52538227
const Precision::IncrementSignificantSettings& impl = macros.precision.fUnion.incrSig;
blueprint_helpers::generateDigitsStem(impl.fMinSig, impl.fMaxSig, sb, status);
#endif // APPLE_ICU_CHANGES
} else if (macros.precision.fType == Precision::RND_FRACTION_SIGNIFICANT) {
const Precision::FractionSignificantSettings& impl = macros.precision.fUnion.fracSig;
blueprint_helpers::generateFractionStem(impl.fMinFrac, impl.fMaxFrac, sb, status);
sb.append(u'/');
if (impl.fRetain) {
if (impl.fPriority == UNUM_ROUNDING_PRIORITY_RELAXED) {
// withMinDigits
blueprint_helpers::generateDigitsStem(impl.fMaxSig, -1, sb, status);
} else {
// withMaxDigits
blueprint_helpers::generateDigitsStem(1, impl.fMaxSig, sb, status);
}
} else {
blueprint_helpers::generateDigitsStem(impl.fMinSig, impl.fMaxSig, sb, status);
if (impl.fPriority == UNUM_ROUNDING_PRIORITY_RELAXED) {
sb.append(u'r');
} else {
sb.append(u's');
}
}
} else if (macros.precision.fType == Precision::RND_INCREMENT
|| macros.precision.fType == Precision::RND_INCREMENT_ONE
|| macros.precision.fType == Precision::RND_INCREMENT_FIVE) {
const Precision::IncrementSettings& impl = macros.precision.fUnion.increment;
sb.append(u"precision-increment/", -1);
blueprint_helpers::generateIncrementOption(
impl.fIncrement,
impl.fIncrementMagnitude,
impl.fMinFrac,
sb,
status);
} else if (macros.precision.fType == Precision::RND_CURRENCY) {
UCurrencyUsage usage = macros.precision.fUnion.currencyUsage;
if (usage == UCURR_USAGE_STANDARD) {
sb.append(u"precision-currency-standard", -1);
} else {
sb.append(u"precision-currency-cash", -1);
}
} else {
// Bogus or Error
return false;
}
if (macros.precision.fTrailingZeroDisplay == UNUM_TRAILING_ZERO_HIDE_IF_WHOLE) {
sb.append(u"/w", -1);
}
// NOTE: Always return true for rounding because the default value depends on other options.
return true;
}
bool GeneratorHelpers::roundingMode(const MacroProps& macros, UnicodeString& sb, UErrorCode&) {
if (macros.roundingMode == kDefaultMode) {
return false; // Default
}
enum_to_stem_string::roundingMode(macros.roundingMode, sb);
return true;
}
bool GeneratorHelpers::grouping(const MacroProps& macros, UnicodeString& sb, UErrorCode& status) {
if (macros.grouper.isBogus()) {
return false; // No value
} else if (macros.grouper.fStrategy == UNUM_GROUPING_COUNT) {
status = U_UNSUPPORTED_ERROR;
return false;
} else if (macros.grouper.fStrategy == UNUM_GROUPING_AUTO) {
return false; // Default value
} else {
enum_to_stem_string::groupingStrategy(macros.grouper.fStrategy, sb);
return true;
}
}
bool GeneratorHelpers::integerWidth(const MacroProps& macros, UnicodeString& sb, UErrorCode& status) {
if (macros.integerWidth.fHasError || macros.integerWidth.isBogus() ||
macros.integerWidth == IntegerWidth::standard()) {
// Error or Default
return false;
}
const auto& minMaxInt = macros.integerWidth.fUnion.minMaxInt;
if (minMaxInt.fMinInt == 0 && minMaxInt.fMaxInt == 0) {
sb.append(u"integer-width-trunc", -1);
return true;
}
sb.append(u"integer-width/", -1);
blueprint_helpers::generateIntegerWidthOption(
minMaxInt.fMinInt,
minMaxInt.fMaxInt,
sb,
status);
return true;
}
bool GeneratorHelpers::symbols(const MacroProps& macros, UnicodeString& sb, UErrorCode& status) {
if (macros.symbols.isNumberingSystem()) {
const NumberingSystem& ns = *macros.symbols.getNumberingSystem();
if (uprv_strcmp(ns.getName(), "latn") == 0) {
sb.append(u"latin", -1);
} else {
sb.append(u"numbering-system/", -1);
blueprint_helpers::generateNumberingSystemOption(ns, sb, status);
}
return true;
} else if (macros.symbols.isDecimalFormatSymbols()) {
status = U_UNSUPPORTED_ERROR;
return false;
} else {
// No custom symbols
return false;
}
}
bool GeneratorHelpers::unitWidth(const MacroProps& macros, UnicodeString& sb, UErrorCode&) {
if (macros.unitWidth == UNUM_UNIT_WIDTH_SHORT || macros.unitWidth == UNUM_UNIT_WIDTH_COUNT) {
return false; // Default or Bogus
}
enum_to_stem_string::unitWidth(macros.unitWidth, sb);
return true;
}
bool GeneratorHelpers::sign(const MacroProps& macros, UnicodeString& sb, UErrorCode&) {
if (macros.sign == UNUM_SIGN_AUTO || macros.sign == UNUM_SIGN_COUNT) {
return false; // Default or Bogus
}
enum_to_stem_string::signDisplay(macros.sign, sb);
return true;
}
bool GeneratorHelpers::decimal(const MacroProps& macros, UnicodeString& sb, UErrorCode&) {
if (macros.decimal == UNUM_DECIMAL_SEPARATOR_AUTO || macros.decimal == UNUM_DECIMAL_SEPARATOR_COUNT) {
return false; // Default or Bogus
}
enum_to_stem_string::decimalSeparatorDisplay(macros.decimal, sb);
return true;
}
bool GeneratorHelpers::scale(const MacroProps& macros, UnicodeString& sb, UErrorCode& status) {
if (!macros.scale.isValid()) {
return false; // Default or Bogus
}
sb.append(u"scale/", -1);
blueprint_helpers::generateScaleOption(
macros.scale.fMagnitude,
macros.scale.fArbitrary,
sb,
status);
return true;
}
// Definitions of public API methods (put here for dependency disentanglement)
#if (U_PF_WINDOWS <= U_PLATFORM && U_PLATFORM <= U_PF_CYGWIN) && defined(_MSC_VER)
// Ignore MSVC warning 4661. This is generated for NumberFormatterSettings<>::toSkeleton() as this method
// is defined elsewhere (in number_skeletons.cpp). The compiler is warning that the explicit template instantiation
// inside this single translation unit (CPP file) is incomplete, and thus it isn't sure if the template class is
// fully defined. However, since each translation unit explicitly instantiates all the necessary template classes,
// they will all be passed to the linker, and the linker will still find and export all the class members.
#pragma warning(push)
#pragma warning(disable: 4661)
#endif
template<typename Derived>
UnicodeString NumberFormatterSettings<Derived>::toSkeleton(UErrorCode& status) const {
if (U_FAILURE(status)) {
return ICU_Utility::makeBogusString();
}
if (fMacros.copyErrorTo(status)) {
return ICU_Utility::makeBogusString();
}
return skeleton::generate(fMacros, status);
}
// Declare all classes that implement NumberFormatterSettings
// See https://stackoverflow.com/a/495056/1407170
template
class icu::number::NumberFormatterSettings<icu::number::UnlocalizedNumberFormatter>;
template
class icu::number::NumberFormatterSettings<icu::number::LocalizedNumberFormatter>;
UnlocalizedNumberFormatter
NumberFormatter::forSkeleton(const UnicodeString& skeleton, UErrorCode& status) {
return skeleton::create(skeleton, nullptr, status);
}
UnlocalizedNumberFormatter
NumberFormatter::forSkeleton(const UnicodeString& skeleton, UParseError& perror, UErrorCode& status) {
return skeleton::create(skeleton, &perror, status);
}
#if (U_PF_WINDOWS <= U_PLATFORM && U_PLATFORM <= U_PF_CYGWIN) && defined(_MSC_VER)
// Warning 4661.
#pragma warning(pop)
#endif
#endif /* #if !UCONFIG_NO_FORMATTING */
|