diff options
author | Moonchild <moonchild@palemoon.org> | 2021-03-31 11:54:20 +0000 |
---|---|---|
committer | Moonchild <moonchild@palemoon.org> | 2021-03-31 11:54:20 +0000 |
commit | 1271dbeaf373d8f32bb8c0bac1555d8019fd0895 (patch) | |
tree | e3b907e2b22734e5b8a0c4042e3fbdcbbb446212 /intl | |
parent | 2953a5b1a1f73cc6defe27e414069cd78befcaa0 (diff) | |
download | uxp-1271dbeaf373d8f32bb8c0bac1555d8019fd0895.tar.gz |
Issue #1756 - Initial wrapped implementation in C++
Diffstat (limited to 'intl')
-rw-r--r-- | intl/icu-patches/unum_formatDoubleForFields.diff | 134 | ||||
-rw-r--r-- | intl/icu/source/i18n/unicode/unum.h | 52 | ||||
-rw-r--r-- | intl/icu/source/i18n/unum.cpp | 28 | ||||
-rwxr-xr-x | intl/update-icu.sh | 1 |
4 files changed, 215 insertions, 0 deletions
diff --git a/intl/icu-patches/unum_formatDoubleForFields.diff b/intl/icu-patches/unum_formatDoubleForFields.diff new file mode 100644 index 0000000000..75a684d4aa --- /dev/null +++ b/intl/icu-patches/unum_formatDoubleForFields.diff @@ -0,0 +1,134 @@ +Add an ICU API for formatting a number into constituent parts (sign, integer, grouping separator, decimal, fraction, &c.) for use in implementing Intl.NumberFormat.prototype.formatToParts. + +https://ssl.icu-project.org/trac/ticket/12684 + +diff --git a/intl/icu/source/i18n/unicode/unum.h b/intl/icu/source/i18n/unicode/unum.h +--- a/intl/icu/source/i18n/unicode/unum.h ++++ b/intl/icu/source/i18n/unicode/unum.h +@@ -20,16 +20,17 @@ + + #include "unicode/localpointer.h" + #include "unicode/uloc.h" + #include "unicode/ucurr.h" + #include "unicode/umisc.h" + #include "unicode/parseerr.h" + #include "unicode/uformattable.h" + #include "unicode/udisplaycontext.h" ++#include "unicode/ufieldpositer.h" + + /** + * \file + * \brief C API: NumberFormat + * + * <h2> Number Format C API </h2> + * + * Number Format C API Provides functions for +@@ -647,16 +648,67 @@ U_STABLE int32_t U_EXPORT2 + unum_formatUFormattable(const UNumberFormat* fmt, + const UFormattable *number, + UChar *result, + int32_t resultLength, + UFieldPosition *pos, + UErrorCode *status); + + /** ++* Format a double using a UNumberFormat according to the UNumberFormat's locale, ++* and initialize a UFieldPositionIterator that enumerates the subcomponents of ++* the resulting string. ++* ++* @param format ++* The formatter to use. ++* @param number ++* The number to format. ++* @param result ++* A pointer to a buffer to receive the NULL-terminated formatted ++* number. If the formatted number fits into dest but cannot be ++* NULL-terminated (length == resultLength) then the error code is set ++* to U_STRING_NOT_TERMINATED_WARNING. If the formatted number doesn't ++* fit into result then the error code is set to ++* U_BUFFER_OVERFLOW_ERROR. ++* @param resultLength ++* The maximum size of result. ++* @param fpositer ++* A pointer to a UFieldPositionIterator created by {@link #ufieldpositer_open} ++* (may be NULL if field position information is not needed, but in this ++* case it's preferable to use {@link #unum_formatDouble}). Iteration ++* information already present in the UFieldPositionIterator is deleted, ++* and the iterator is reset to apply to the fields in the formatted ++* string created by this function call. The field values and indexes ++* returned by {@link #ufieldpositer_next} represent fields denoted by ++* the UNumberFormatFields enum. Fields are not returned in a guaranteed ++* order. Fields cannot overlap, but they may nest. For example, 1234 ++* could format as "1,234" which might consist of a grouping separator ++* field for ',' and an integer field encompassing the entire string. ++* @param status ++* A pointer to an UErrorCode to receive any errors ++* @return ++* The total buffer size needed; if greater than resultLength, the ++* output was truncated. ++* @see unum_formatDouble ++* @see unum_parse ++* @see unum_parseDouble ++* @see UFieldPositionIterator ++* @see UNumberFormatFields ++* @draft ICU 59 ++*/ ++U_DRAFT int32_t U_EXPORT2 ++unum_formatDoubleForFields(const UNumberFormat* format, ++ double number, ++ UChar* result, ++ int32_t resultLength, ++ UFieldPositionIterator* fpositer, ++ UErrorCode* status); ++#define ICU_UNUM_HAS_FORMATDOUBLEFORFIELDS ++ ++/** + * Parse a string into an integer using a UNumberFormat. + * The string will be parsed according to the UNumberFormat's locale. + * Note: parsing is not supported for styles UNUM_DECIMAL_COMPACT_SHORT + * and UNUM_DECIMAL_COMPACT_LONG. + * @param fmt The formatter to use. + * @param text The text to parse. + * @param textLength The length of text, or -1 if null-terminated. + * @param parsePos If not NULL, on input a pointer to an integer specifying the offset at which +diff --git a/intl/icu/source/i18n/unum.cpp b/intl/icu/source/i18n/unum.cpp +--- a/intl/icu/source/i18n/unum.cpp ++++ b/intl/icu/source/i18n/unum.cpp +@@ -870,9 +870,37 @@ unum_formatUFormattable(const UNumberFor + if(pos != 0) { + pos->beginIndex = fp.getBeginIndex(); + pos->endIndex = fp.getEndIndex(); + } + + return res.extract(result, resultLength, *status); + } + ++U_CAPI int32_t U_EXPORT2 ++unum_formatDoubleForFields(const UNumberFormat* format, ++ double number, ++ UChar* result, ++ int32_t resultLength, ++ UFieldPositionIterator* fpositer, ++ UErrorCode* status) ++{ ++ if (U_FAILURE(*status)) ++ return -1; ++ ++ if (result == NULL ? resultLength != 0 : resultLength < 0) { ++ *status = U_ILLEGAL_ARGUMENT_ERROR; ++ return -1; ++ } ++ ++ UnicodeString res; ++ if (result != NULL) { ++ // NULL destination for pure preflighting: empty dummy string ++ // otherwise, alias the destination buffer ++ res.setTo(result, 0, resultLength); ++ } ++ ++ ((const NumberFormat*)format)->format(number, res, (FieldPositionIterator*)fpositer, *status); ++ ++ return res.extract(result, resultLength, *status); ++} ++ + #endif /* #if !UCONFIG_NO_FORMATTING */ diff --git a/intl/icu/source/i18n/unicode/unum.h b/intl/icu/source/i18n/unicode/unum.h index 34d54427f0..acc6abf8c5 100644 --- a/intl/icu/source/i18n/unicode/unum.h +++ b/intl/icu/source/i18n/unicode/unum.h @@ -25,6 +25,7 @@ #include "unicode/parseerr.h" #include "unicode/uformattable.h" #include "unicode/udisplaycontext.h" +#include "unicode/ufieldpositer.h" /** * \file @@ -652,6 +653,57 @@ unum_formatUFormattable(const UNumberFormat* fmt, UErrorCode *status); /** +* Format a double using a UNumberFormat according to the UNumberFormat's locale, +* and initialize a UFieldPositionIterator that enumerates the subcomponents of +* the resulting string. +* +* @param format +* The formatter to use. +* @param number +* The number to format. +* @param result +* A pointer to a buffer to receive the NULL-terminated formatted +* number. If the formatted number fits into dest but cannot be +* NULL-terminated (length == resultLength) then the error code is set +* to U_STRING_NOT_TERMINATED_WARNING. If the formatted number doesn't +* fit into result then the error code is set to +* U_BUFFER_OVERFLOW_ERROR. +* @param resultLength +* The maximum size of result. +* @param fpositer +* A pointer to a UFieldPositionIterator created by {@link #ufieldpositer_open} +* (may be NULL if field position information is not needed, but in this +* case it's preferable to use {@link #unum_formatDouble}). Iteration +* information already present in the UFieldPositionIterator is deleted, +* and the iterator is reset to apply to the fields in the formatted +* string created by this function call. The field values and indexes +* returned by {@link #ufieldpositer_next} represent fields denoted by +* the UNumberFormatFields enum. Fields are not returned in a guaranteed +* order. Fields cannot overlap, but they may nest. For example, 1234 +* could format as "1,234" which might consist of a grouping separator +* field for ',' and an integer field encompassing the entire string. +* @param status +* A pointer to an UErrorCode to receive any errors +* @return +* The total buffer size needed; if greater than resultLength, the +* output was truncated. +* @see unum_formatDouble +* @see unum_parse +* @see unum_parseDouble +* @see UFieldPositionIterator +* @see UNumberFormatFields +* @draft ICU 59 +*/ +U_DRAFT int32_t U_EXPORT2 +unum_formatDoubleForFields(const UNumberFormat* format, + double number, + UChar* result, + int32_t resultLength, + UFieldPositionIterator* fpositer, + UErrorCode* status); +#define ICU_UNUM_HAS_FORMATDOUBLEFORFIELDS + +/** * Parse a string into an integer using a UNumberFormat. * The string will be parsed according to the UNumberFormat's locale. * Note: parsing is not supported for styles UNUM_DECIMAL_COMPACT_SHORT diff --git a/intl/icu/source/i18n/unum.cpp b/intl/icu/source/i18n/unum.cpp index 01170b4c1d..8029810eaf 100644 --- a/intl/icu/source/i18n/unum.cpp +++ b/intl/icu/source/i18n/unum.cpp @@ -875,4 +875,32 @@ unum_formatUFormattable(const UNumberFormat* fmt, return res.extract(result, resultLength, *status); } +U_CAPI int32_t U_EXPORT2 +unum_formatDoubleForFields(const UNumberFormat* format, + double number, + UChar* result, + int32_t resultLength, + UFieldPositionIterator* fpositer, + UErrorCode* status) +{ + if (U_FAILURE(*status)) + return -1; + + if (result == NULL ? resultLength != 0 : resultLength < 0) { + *status = U_ILLEGAL_ARGUMENT_ERROR; + return -1; + } + + UnicodeString res; + if (result != NULL) { + // NULL destination for pure preflighting: empty dummy string + // otherwise, alias the destination buffer + res.setTo(result, 0, resultLength); + } + + ((const NumberFormat*)format)->format(number, res, (FieldPositionIterator*)fpositer, *status); + + return res.extract(result, resultLength, *status); +} + #endif /* #if !UCONFIG_NO_FORMATTING */ diff --git a/intl/update-icu.sh b/intl/update-icu.sh index 4983b66707..0662c57c9e 100755 --- a/intl/update-icu.sh +++ b/intl/update-icu.sh @@ -72,6 +72,7 @@ for patch in \ bug-1198952-workaround-make-3.82-bug.diff \ bug-1228227-bug-1263325-libc++-gcc_hidden.diff \ ucol_getKeywordValuesForLocale-ulist_resetList.diff \ + unum_formatDoubleForFields.diff \ ; do echo "Applying local patch $patch" patch -d ${icu_dir}/../../ -p1 --no-backup-if-mismatch < ${icu_dir}/../icu-patches/$patch |