Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/unicode/unumberrangeformatter.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 // © 2020 and later: Unicode, Inc. and others.
0002 // License & terms of use: http://www.unicode.org/copyright.html
0003 
0004 #ifndef __UNUMBERRANGEFORMATTER_H__
0005 #define __UNUMBERRANGEFORMATTER_H__
0006 
0007 #include "unicode/utypes.h"
0008 
0009 #if !UCONFIG_NO_FORMATTING
0010 
0011 #include "unicode/parseerr.h"
0012 #include "unicode/ufieldpositer.h"
0013 #include "unicode/umisc.h"
0014 #include "unicode/uformattedvalue.h"
0015 #include "unicode/uformattable.h"
0016 
0017 
0018 /**
0019  * \file
0020  * \brief C API: Localized number range formatting
0021  *
0022  * This is the C-compatible version of the NumberRangeFormatter API. C++ users
0023  * should include unicode/numberrangeformatter.h and use the proper C++ APIs.
0024  *
0025  * First create a UNumberRangeFormatter, which is immutable, and then format to
0026  * a UFormattedNumberRange.
0027  *
0028  * Example code:
0029  * <pre>
0030  * // Setup:
0031  * UErrorCode ec = U_ZERO_ERROR;
0032  * UNumberRangeFormatter* uformatter = unumrf_openForSkeletonCollapseIdentityFallbackAndLocaleWithError(
0033  *     u"currency/USD precision-integer",
0034  *     -1,
0035  *     UNUM_RANGE_COLLAPSE_AUTO,
0036  *     UNUM_IDENTITY_FALLBACK_APPROXIMATELY,
0037  *     "en-US",
0038  *     NULL,
0039  *     &ec);
0040  * UFormattedNumberRange* uresult = unumrf_openResult(&ec);
0041  * if (U_FAILURE(ec)) { return; }
0042  *
0043  * // Format a double range:
0044  * unumrf_formatDoubleRange(uformatter, 3.0, 5.0, uresult, &ec);
0045  * if (U_FAILURE(ec)) { return; }
0046  *
0047  * // Get the result string:
0048  * int32_t len;
0049  * const UChar* str = ufmtval_getString(unumrf_resultAsValue(uresult, &ec), &len, &ec);
0050  * if (U_FAILURE(ec)) { return; }
0051  * // str should equal "$3 – $5"
0052  *
0053  * // Cleanup:
0054  * unumf_close(uformatter);
0055  * unumf_closeResult(uresult);
0056  * </pre>
0057  *
0058  * If you are a C++ user linking against the C libraries, you can use the LocalPointer versions of these
0059  * APIs. The following example uses LocalPointer with the decimal number and field position APIs:
0060  *
0061  * <pre>
0062  * // Setup:
0063  * LocalUNumberRangeFormatterPointer uformatter(
0064  *     unumrf_openForSkeletonCollapseIdentityFallbackAndLocaleWithError(...));
0065  * LocalUFormattedNumberRangePointer uresult(unumrf_openResult(&ec));
0066  * if (U_FAILURE(ec)) { return; }
0067  *
0068  * // Format a double number range:
0069  * unumrf_formatDoubleRange(uformatter.getAlias(), 3.0, 5.0, uresult.getAlias(), &ec);
0070  * if (U_FAILURE(ec)) { return; }
0071  *
0072  * // No need to do any cleanup since we are using LocalPointer.
0073  * </pre>
0074  *
0075  * You can also get field positions. For more information, see uformattedvalue.h.
0076  */
0077 
0078 /**
0079  * Defines how to merge fields that are identical across the range sign.
0080  *
0081  * @stable ICU 63
0082  */
0083 typedef enum UNumberRangeCollapse {
0084     /**
0085      * Use locale data and heuristics to determine how much of the string to collapse. Could end up collapsing none,
0086      * some, or all repeated pieces in a locale-sensitive way.
0087      *
0088      * The heuristics used for this option are subject to change over time.
0089      *
0090      * @stable ICU 63
0091      */
0092     UNUM_RANGE_COLLAPSE_AUTO,
0093 
0094     /**
0095      * Do not collapse any part of the number. Example: "3.2 thousand kilograms – 5.3 thousand kilograms"
0096      *
0097      * @stable ICU 63
0098      */
0099     UNUM_RANGE_COLLAPSE_NONE,
0100 
0101     /**
0102      * Collapse the unit part of the number, but not the notation, if present. Example: "3.2 thousand – 5.3 thousand
0103      * kilograms"
0104      *
0105      * @stable ICU 63
0106      */
0107     UNUM_RANGE_COLLAPSE_UNIT,
0108 
0109     /**
0110      * Collapse any field that is equal across the range sign. May introduce ambiguity on the magnitude of the
0111      * number. Example: "3.2 – 5.3 thousand kilograms"
0112      *
0113      * @stable ICU 63
0114      */
0115     UNUM_RANGE_COLLAPSE_ALL
0116 } UNumberRangeCollapse;
0117 
0118 /**
0119  * Defines the behavior when the two numbers in the range are identical after rounding. To programmatically detect
0120  * when the identity fallback is used, compare the lower and upper BigDecimals via FormattedNumber.
0121  *
0122  * @stable ICU 63
0123  * @see NumberRangeFormatter
0124  */
0125 typedef enum UNumberRangeIdentityFallback {
0126     /**
0127      * Show the number as a single value rather than a range. Example: "$5"
0128      *
0129      * @stable ICU 63
0130      */
0131     UNUM_IDENTITY_FALLBACK_SINGLE_VALUE,
0132 
0133     /**
0134      * Show the number using a locale-sensitive approximation pattern. If the numbers were the same before rounding,
0135      * show the single value. Example: "~$5" or "$5"
0136      *
0137      * @stable ICU 63
0138      */
0139     UNUM_IDENTITY_FALLBACK_APPROXIMATELY_OR_SINGLE_VALUE,
0140 
0141     /**
0142      * Show the number using a locale-sensitive approximation pattern. Use the range pattern always, even if the
0143      * inputs are the same. Example: "~$5"
0144      *
0145      * @stable ICU 63
0146      */
0147     UNUM_IDENTITY_FALLBACK_APPROXIMATELY,
0148 
0149     /**
0150      * Show the number as the range of two equal values. Use the range pattern always, even if the inputs are the
0151      * same. Example (with RangeCollapse.NONE): "$5 – $5"
0152      *
0153      * @stable ICU 63
0154      */
0155     UNUM_IDENTITY_FALLBACK_RANGE
0156 } UNumberRangeIdentityFallback;
0157 
0158 /**
0159  * Used in the result class FormattedNumberRange to indicate to the user whether the numbers formatted in the range
0160  * were equal or not, and whether or not the identity fallback was applied.
0161  *
0162  * @stable ICU 63
0163  * @see NumberRangeFormatter
0164  */
0165 typedef enum UNumberRangeIdentityResult {
0166     /**
0167      * Used to indicate that the two numbers in the range were equal, even before any rounding rules were applied.
0168      *
0169      * @stable ICU 63
0170      * @see NumberRangeFormatter
0171      */
0172     UNUM_IDENTITY_RESULT_EQUAL_BEFORE_ROUNDING,
0173 
0174     /**
0175      * Used to indicate that the two numbers in the range were equal, but only after rounding rules were applied.
0176      *
0177      * @stable ICU 63
0178      * @see NumberRangeFormatter
0179      */
0180     UNUM_IDENTITY_RESULT_EQUAL_AFTER_ROUNDING,
0181 
0182     /**
0183      * Used to indicate that the two numbers in the range were not equal, even after rounding rules were applied.
0184      *
0185      * @stable ICU 63
0186      * @see NumberRangeFormatter
0187      */
0188     UNUM_IDENTITY_RESULT_NOT_EQUAL,
0189 
0190 #ifndef U_HIDE_INTERNAL_API
0191     /**
0192      * The number of entries in this enum.
0193      * @internal
0194      */
0195     UNUM_IDENTITY_RESULT_COUNT
0196 #endif  /* U_HIDE_INTERNAL_API */
0197 
0198 } UNumberRangeIdentityResult;
0199 
0200 
0201 struct UNumberRangeFormatter;
0202 /**
0203  * C-compatible version of icu::number::LocalizedNumberRangeFormatter.
0204  *
0205  * NOTE: This is a C-compatible API; C++ users should build against numberrangeformatter.h instead.
0206  *
0207  * @stable ICU 68
0208  */
0209 typedef struct UNumberRangeFormatter UNumberRangeFormatter;
0210 
0211 
0212 struct UFormattedNumberRange;
0213 /**
0214  * C-compatible version of icu::number::FormattedNumberRange.
0215  *
0216  * NOTE: This is a C-compatible API; C++ users should build against numberrangeformatter.h instead.
0217  *
0218  * @stable ICU 68
0219  */
0220 typedef struct UFormattedNumberRange UFormattedNumberRange;
0221 
0222 
0223 /**
0224  * Creates a new UNumberFormatter for the given skeleton string, collapse option, identity fallback
0225  * option, and locale. This is currently the only method for creating a new UNumberRangeFormatter.
0226  *
0227  * Objects of type UNumberRangeFormatter returned by this method are threadsafe.
0228  *
0229  * For more details on skeleton strings, see the documentation in numberrangeformatter.h. For more
0230  * details on the usage of this API, see the documentation at the top of unumberrangeformatter.h.
0231  *
0232  * NOTE: This is a C-compatible API; C++ users should build against numberrangeformatter.h instead.
0233  *
0234  * @param skeleton The skeleton string, like u"percent precision-integer"
0235  * @param skeletonLen The number of UChars in the skeleton string, or -1 if it is NUL-terminated.
0236  * @param collapse Option for how to merge affixes (if unsure, use UNUM_RANGE_COLLAPSE_AUTO)
0237  * @param identityFallback Option for resolving when both sides of the range are equal.
0238  * @param locale The NUL-terminated locale ID.
0239  * @param perror A parse error struct populated if an error occurs when parsing. Can be NULL.
0240  *               If no error occurs, perror->offset will be set to -1.
0241  * @param ec Set if an error occurs.
0242  * @stable ICU 68
0243  */
0244 U_CAPI UNumberRangeFormatter* U_EXPORT2
0245 unumrf_openForSkeletonWithCollapseAndIdentityFallback(
0246     const UChar* skeleton,
0247     int32_t skeletonLen,
0248     UNumberRangeCollapse collapse,
0249     UNumberRangeIdentityFallback identityFallback,
0250     const char* locale,
0251     UParseError* perror,
0252     UErrorCode* ec);
0253 
0254 
0255 /**
0256  * Creates an object to hold the result of a UNumberRangeFormatter
0257  * operation. The object can be used repeatedly; it is cleared whenever
0258  * passed to a format function.
0259  *
0260  * @param ec Set if an error occurs.
0261  * @stable ICU 68
0262  */
0263 U_CAPI UFormattedNumberRange* U_EXPORT2
0264 unumrf_openResult(UErrorCode* ec);
0265 
0266 
0267 /**
0268  * Uses a UNumberRangeFormatter to format a range of doubles.
0269  *
0270  * The UNumberRangeFormatter can be shared between threads. Each thread should have its own local
0271  * UFormattedNumberRange, however, for storing the result of the formatting operation.
0272  *
0273  * NOTE: This is a C-compatible API; C++ users should build against numberrangeformatter.h instead.
0274  *
0275  * @param uformatter A formatter object; see unumberrangeformatter.h.
0276  * @param first The first (usually smaller) number in the range.
0277  * @param second The second (usually larger) number in the range.
0278  * @param uresult The object that will be mutated to store the result; see unumrf_openResult.
0279  * @param ec Set if an error occurs.
0280  * @stable ICU 68
0281  */
0282 U_CAPI void U_EXPORT2
0283 unumrf_formatDoubleRange(
0284     const UNumberRangeFormatter* uformatter,
0285     double first,
0286     double second,
0287     UFormattedNumberRange* uresult,
0288     UErrorCode* ec);
0289 
0290 
0291 /**
0292  * Uses a UNumberRangeFormatter to format a range of decimal numbers.
0293  *
0294  * With a decimal number string, you can specify an input with arbitrary precision.
0295  *
0296  * The UNumberRangeFormatter can be shared between threads. Each thread should have its own local
0297  * UFormattedNumberRange, however, for storing the result of the formatting operation.
0298  *
0299  * NOTE: This is a C-compatible API; C++ users should build against numberrangeformatter.h instead.
0300  *
0301  * @param uformatter A formatter object; see unumberrangeformatter.h.
0302  * @param first The first (usually smaller) number in the range.
0303  * @param firstLen The length of the first decimal number string.
0304  * @param second The second (usually larger) number in the range.
0305  * @param secondLen The length of the second decimal number string.
0306  * @param uresult The object that will be mutated to store the result; see unumrf_openResult.
0307  * @param ec Set if an error occurs.
0308  * @stable ICU 68
0309  */
0310 U_CAPI void U_EXPORT2
0311 unumrf_formatDecimalRange(
0312     const UNumberRangeFormatter* uformatter,
0313     const char* first,
0314     int32_t firstLen,
0315     const char* second,
0316     int32_t secondLen,
0317     UFormattedNumberRange* uresult,
0318     UErrorCode* ec);
0319 
0320 
0321 /**
0322  * Returns a representation of a UFormattedNumberRange as a UFormattedValue,
0323  * which can be subsequently passed to any API requiring that type.
0324  *
0325  * The returned object is owned by the UFormattedNumberRange and is valid
0326  * only as long as the UFormattedNumber is present and unchanged in memory.
0327  *
0328  * You can think of this method as a cast between types.
0329  *
0330  * @param uresult The object containing the formatted number range.
0331  * @param ec Set if an error occurs.
0332  * @return A UFormattedValue owned by the input object.
0333  * @stable ICU 68
0334  */
0335 U_CAPI const UFormattedValue* U_EXPORT2
0336 unumrf_resultAsValue(const UFormattedNumberRange* uresult, UErrorCode* ec);
0337 
0338 
0339 /**
0340  * Extracts the identity result from a UFormattedNumberRange.
0341  *
0342  * NOTE: This is a C-compatible API; C++ users should build against numberformatter.h instead.
0343  *
0344  * @param uresult The object containing the formatted number range.
0345  * @param ec Set if an error occurs.
0346  * @return The identity result; see UNumberRangeIdentityResult.
0347  * @stable ICU 68
0348  */
0349 U_CAPI UNumberRangeIdentityResult U_EXPORT2
0350 unumrf_resultGetIdentityResult(
0351     const UFormattedNumberRange* uresult,
0352     UErrorCode* ec);
0353 
0354 
0355 /**
0356  * Extracts the first formatted number as a decimal number. This endpoint
0357  * is useful for obtaining the exact number being printed after scaling
0358  * and rounding have been applied by the number range formatting pipeline.
0359  * 
0360  * The syntax of the unformatted number is a "numeric string"
0361  * as defined in the Decimal Arithmetic Specification, available at
0362  * http://speleotrove.com/decimal
0363  *
0364  * @param  uresult       The input object containing the formatted number range.
0365  * @param  dest          the 8-bit char buffer into which the decimal number is placed
0366  * @param  destCapacity  The size, in chars, of the destination buffer.  May be zero
0367  *                       for precomputing the required size.
0368  * @param  ec            receives any error status.
0369  *                       If U_BUFFER_OVERFLOW_ERROR: Returns number of chars for
0370  *                       preflighting.
0371  * @return Number of chars in the data.  Does not include a trailing NUL.
0372  * @stable ICU 68
0373  */
0374 U_CAPI int32_t U_EXPORT2
0375 unumrf_resultGetFirstDecimalNumber(
0376     const UFormattedNumberRange* uresult,
0377     char* dest,
0378     int32_t destCapacity,
0379     UErrorCode* ec);
0380 
0381 
0382 /**
0383  * Extracts the second formatted number as a decimal number. This endpoint
0384  * is useful for obtaining the exact number being printed after scaling
0385  * and rounding have been applied by the number range formatting pipeline.
0386  * 
0387  * The syntax of the unformatted number is a "numeric string"
0388  * as defined in the Decimal Arithmetic Specification, available at
0389  * http://speleotrove.com/decimal
0390  *
0391  * @param  uresult       The input object containing the formatted number range.
0392  * @param  dest          the 8-bit char buffer into which the decimal number is placed
0393  * @param  destCapacity  The size, in chars, of the destination buffer.  May be zero
0394  *                       for precomputing the required size.
0395  * @param  ec            receives any error status.
0396  *                       If U_BUFFER_OVERFLOW_ERROR: Returns number of chars for
0397  *                       preflighting.
0398  * @return Number of chars in the data.  Does not include a trailing NUL.
0399  * @stable ICU 68
0400  */
0401 U_CAPI int32_t U_EXPORT2
0402 unumrf_resultGetSecondDecimalNumber(
0403     const UFormattedNumberRange* uresult,
0404     char* dest,
0405     int32_t destCapacity,
0406     UErrorCode* ec);
0407 
0408 
0409 /**
0410  * Releases the UNumberFormatter created by unumf_openForSkeletonAndLocale().
0411  *
0412  * @param uformatter An object created by unumf_openForSkeletonAndLocale().
0413  * @stable ICU 68
0414  */
0415 U_CAPI void U_EXPORT2
0416 unumrf_close(UNumberRangeFormatter* uformatter);
0417 
0418 
0419 /**
0420  * Releases the UFormattedNumber created by unumf_openResult().
0421  *
0422  * @param uresult An object created by unumf_openResult().
0423  * @stable ICU 68
0424  */
0425 U_CAPI void U_EXPORT2
0426 unumrf_closeResult(UFormattedNumberRange* uresult);
0427 
0428 
0429 #if U_SHOW_CPLUSPLUS_API
0430 U_NAMESPACE_BEGIN
0431 
0432 /**
0433  * \class LocalUNumberRangeFormatterPointer
0434  * "Smart pointer" class; closes a UNumberFormatter via unumf_close().
0435  * For most methods see the LocalPointerBase base class.
0436  *
0437  * Usage:
0438  * <pre>
0439  * LocalUNumberRangeFormatterPointer uformatter(
0440  *     unumrf_openForSkeletonCollapseIdentityFallbackAndLocaleWithError(...));
0441  * // no need to explicitly call unumrf_close()
0442  * </pre>
0443  *
0444  * @see LocalPointerBase
0445  * @see LocalPointer
0446  * @stable ICU 68
0447  */
0448 U_DEFINE_LOCAL_OPEN_POINTER(LocalUNumberRangeFormatterPointer, UNumberRangeFormatter, unumrf_close);
0449 
0450 /**
0451  * \class LocalUFormattedNumberPointer
0452  * "Smart pointer" class; closes a UFormattedNumber via unumf_closeResult().
0453  * For most methods see the LocalPointerBase base class.
0454  *
0455  * Usage:
0456  * <pre>
0457  * LocalUFormattedNumberRangePointer uresult(unumrf_openResult(...));
0458  * // no need to explicitly call unumrf_closeResult()
0459  * </pre>
0460  *
0461  * @see LocalPointerBase
0462  * @see LocalPointer
0463  * @stable ICU 68
0464  */
0465 U_DEFINE_LOCAL_OPEN_POINTER(LocalUFormattedNumberRangePointer, UFormattedNumberRange, unumrf_closeResult);
0466 
0467 U_NAMESPACE_END
0468 #endif // U_SHOW_CPLUSPLUS_API
0469 
0470 #endif /* #if !UCONFIG_NO_FORMATTING */
0471 #endif //__UNUMBERRANGEFORMATTER_H__