Back to home page

EIC code displayed by LXR

 
 

    


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

0001 // © 2022 and later: Unicode, Inc. and others.
0002 // License & terms of use: http://www.unicode.org/copyright.html
0003 
0004 #ifndef __UFORMATTEDNUMBER_H__
0005 #define __UFORMATTEDNUMBER_H__
0006 
0007 #include "unicode/utypes.h"
0008 
0009 #if !UCONFIG_NO_FORMATTING
0010 
0011 #include "unicode/ufieldpositer.h"
0012 #include "unicode/uformattedvalue.h"
0013 #include "unicode/umisc.h"
0014 
0015 /**
0016  * \file
0017  * \brief C API: Formatted number result from various number formatting functions.
0018  *
0019  * Create a `UFormattedNumber` to hold the result of a number formatting operation. The same
0020  * `UFormattedNumber` can be reused multiple times.
0021  *
0022  * <pre>
0023  * LocalUFormattedNumberPointer uresult(unumf_openResult(status));
0024  *
0025  * // pass uresult.getAlias() to your number formatter
0026  *
0027  * int32_t length;
0028  * const UChar* s = ufmtval_getString(unumf_resultAsValue(uresult.getAlias(), status), &length, status));
0029  *
0030  * // The string result is in `s` with the given `length` (it is also NUL-terminated).
0031  * </pre>
0032  */
0033 
0034 
0035 struct UFormattedNumber;
0036 /**
0037  * C-compatible version of icu::number::FormattedNumber.
0038  *
0039  * NOTE: This is a C-compatible API; C++ users should build against numberformatter.h instead.
0040  *
0041  * @stable ICU 62
0042  */
0043 typedef struct UFormattedNumber UFormattedNumber;
0044 
0045 
0046 /**
0047  * Creates an object to hold the result of a UNumberFormatter
0048  * operation. The object can be used repeatedly; it is cleared whenever
0049  * passed to a format function.
0050  *
0051  * @param ec Set if an error occurs.
0052  * @stable ICU 62
0053  */
0054 U_CAPI UFormattedNumber* U_EXPORT2
0055 unumf_openResult(UErrorCode* ec);
0056 
0057 
0058 /**
0059  * Returns a representation of a UFormattedNumber as a UFormattedValue,
0060  * which can be subsequently passed to any API requiring that type.
0061  *
0062  * The returned object is owned by the UFormattedNumber and is valid
0063  * only as long as the UFormattedNumber is present and unchanged in memory.
0064  *
0065  * You can think of this method as a cast between types.
0066  *
0067  * @param uresult The object containing the formatted string.
0068  * @param ec Set if an error occurs.
0069  * @return A UFormattedValue owned by the input object.
0070  * @stable ICU 64
0071  */
0072 U_CAPI const UFormattedValue* U_EXPORT2
0073 unumf_resultAsValue(const UFormattedNumber* uresult, UErrorCode* ec);
0074 
0075 
0076 /**
0077  * Extracts the result number string out of a UFormattedNumber to a UChar buffer if possible.
0078  * If bufferCapacity is greater than the required length, a terminating NUL is written.
0079  * If bufferCapacity is less than the required length, an error code is set.
0080  *
0081  * Also see ufmtval_getString, which returns a NUL-terminated string:
0082  *
0083  *     int32_t len;
0084  *     const UChar* str = ufmtval_getString(unumf_resultAsValue(uresult, &ec), &len, &ec);
0085  *
0086  * NOTE: This is a C-compatible API; C++ users should build against numberformatter.h instead.
0087  *
0088  * @param uresult The object containing the formatted number.
0089  * @param buffer Where to save the string output.
0090  * @param bufferCapacity The number of UChars available in the buffer.
0091  * @param ec Set if an error occurs.
0092  * @return The required length.
0093  * @stable ICU 62
0094  */
0095 U_CAPI int32_t U_EXPORT2
0096 unumf_resultToString(const UFormattedNumber* uresult, UChar* buffer, int32_t bufferCapacity,
0097                      UErrorCode* ec);
0098 
0099 
0100 /**
0101  * Determines the start and end indices of the next occurrence of the given <em>field</em> in the
0102  * output string. This allows you to determine the locations of, for example, the integer part,
0103  * fraction part, or symbols.
0104  *
0105  * This is a simpler but less powerful alternative to {@link ufmtval_nextPosition}.
0106  *
0107  * If a field occurs just once, calling this method will find that occurrence and return it. If a
0108  * field occurs multiple times, this method may be called repeatedly with the following pattern:
0109  *
0110  * <pre>
0111  * UFieldPosition ufpos = {UNUM_GROUPING_SEPARATOR_FIELD, 0, 0};
0112  * while (unumf_resultNextFieldPosition(uresult, ufpos, &ec)) {
0113  *   // do something with ufpos.
0114  * }
0115  * </pre>
0116  *
0117  * This method is useful if you know which field to query. If you want all available field position
0118  * information, use unumf_resultGetAllFieldPositions().
0119  *
0120  * NOTE: All fields of the UFieldPosition must be initialized before calling this method.
0121  *
0122  * @param uresult The object containing the formatted number.
0123  * @param ufpos
0124  *            Input+output variable. On input, the "field" property determines which field to look up,
0125  *            and the "endIndex" property determines where to begin the search. On output, the
0126  *            "beginIndex" field is set to the beginning of the first occurrence of the field after the
0127  *            input "endIndex", and "endIndex" is set to the end of that occurrence of the field
0128  *            (exclusive index). If a field position is not found, the FieldPosition is not changed and
0129  *            the method returns false.
0130  * @param ec Set if an error occurs.
0131  * @stable ICU 62
0132  */
0133 U_CAPI UBool U_EXPORT2
0134 unumf_resultNextFieldPosition(const UFormattedNumber* uresult, UFieldPosition* ufpos, UErrorCode* ec);
0135 
0136 
0137 /**
0138  * Populates the given iterator with all fields in the formatted output string. This allows you to
0139  * determine the locations of the integer part, fraction part, and sign.
0140  *
0141  * This is an alternative to the more powerful {@link ufmtval_nextPosition} API.
0142  *
0143  * If you need information on only one field, use {@link ufmtval_nextPosition} or
0144  * {@link unumf_resultNextFieldPosition}.
0145  *
0146  * @param uresult The object containing the formatted number.
0147  * @param ufpositer
0148  *         A pointer to a UFieldPositionIterator created by {@link #ufieldpositer_open}. Iteration
0149  *         information already present in the UFieldPositionIterator is deleted, and the iterator is reset
0150  *         to apply to the fields in the formatted string created by this function call. The field values
0151  *         and indexes returned by {@link #ufieldpositer_next} represent fields denoted by
0152  *         the UNumberFormatFields enum. Fields are not returned in a guaranteed order. Fields cannot
0153  *         overlap, but they may nest. For example, 1234 could format as "1,234" which might consist of a
0154  *         grouping separator field for ',' and an integer field encompassing the entire string.
0155  * @param ec Set if an error occurs.
0156  * @stable ICU 62
0157  */
0158 U_CAPI void U_EXPORT2
0159 unumf_resultGetAllFieldPositions(const UFormattedNumber* uresult, UFieldPositionIterator* ufpositer,
0160                                  UErrorCode* ec);
0161 
0162 
0163 /**
0164  * Extracts the formatted number as a "numeric string" conforming to the
0165  * syntax defined in the Decimal Arithmetic Specification, available at
0166  * http://speleotrove.com/decimal
0167  *
0168  * This endpoint is useful for obtaining the exact number being printed
0169  * after scaling and rounding have been applied by the number formatter.
0170  *
0171  * @param uresult        The input object containing the formatted number.
0172  * @param  dest          the 8-bit char buffer into which the decimal number is placed
0173  * @param  destCapacity  The size, in chars, of the destination buffer.  May be zero
0174  *                       for precomputing the required size.
0175  * @param  ec            receives any error status.
0176  *                       If U_BUFFER_OVERFLOW_ERROR: Returns number of chars for
0177  *                       preflighting.
0178  * @return Number of chars in the data.  Does not include a trailing NUL.
0179  * @stable ICU 68
0180  */
0181 U_CAPI int32_t U_EXPORT2
0182 unumf_resultToDecimalNumber(
0183        const UFormattedNumber* uresult,
0184        char* dest,
0185        int32_t destCapacity,
0186        UErrorCode* ec);
0187 
0188 
0189 /**
0190  * Releases the UFormattedNumber created by unumf_openResult().
0191  *
0192  * @param uresult An object created by unumf_openResult().
0193  * @stable ICU 62
0194  */
0195 U_CAPI void U_EXPORT2
0196 unumf_closeResult(UFormattedNumber* uresult);
0197 
0198 
0199 #if U_SHOW_CPLUSPLUS_API
0200 U_NAMESPACE_BEGIN
0201 
0202 /**
0203  * \class LocalUFormattedNumberPointer
0204  * "Smart pointer" class; closes a UFormattedNumber via unumf_closeResult().
0205  * For most methods see the LocalPointerBase base class.
0206  *
0207  * Usage:
0208  * <pre>
0209  * LocalUFormattedNumberPointer uformatter(unumf_openResult(...));
0210  * // no need to explicitly call unumf_closeResult()
0211  * </pre>
0212  *
0213  * @see LocalPointerBase
0214  * @see LocalPointer
0215  * @stable ICU 62
0216  */
0217 U_DEFINE_LOCAL_OPEN_POINTER(LocalUFormattedNumberPointer, UFormattedNumber, unumf_closeResult);
0218 
0219 U_NAMESPACE_END
0220 #endif // U_SHOW_CPLUSPLUS_API
0221 
0222 
0223 #endif /* #if !UCONFIG_NO_FORMATTING */
0224 #endif //__UFORMATTEDNUMBER_H__