1*0e209d39SAndroid Build Coastguard Worker // © 2022 and later: Unicode, Inc. and others. 2*0e209d39SAndroid Build Coastguard Worker // License & terms of use: http://www.unicode.org/copyright.html 3*0e209d39SAndroid Build Coastguard Worker 4*0e209d39SAndroid Build Coastguard Worker #ifndef __UFORMATTEDNUMBER_H__ 5*0e209d39SAndroid Build Coastguard Worker #define __UFORMATTEDNUMBER_H__ 6*0e209d39SAndroid Build Coastguard Worker 7*0e209d39SAndroid Build Coastguard Worker #include "unicode/utypes.h" 8*0e209d39SAndroid Build Coastguard Worker 9*0e209d39SAndroid Build Coastguard Worker #if !UCONFIG_NO_FORMATTING 10*0e209d39SAndroid Build Coastguard Worker 11*0e209d39SAndroid Build Coastguard Worker #include "unicode/ufieldpositer.h" 12*0e209d39SAndroid Build Coastguard Worker #include "unicode/uformattedvalue.h" 13*0e209d39SAndroid Build Coastguard Worker #include "unicode/umisc.h" 14*0e209d39SAndroid Build Coastguard Worker 15*0e209d39SAndroid Build Coastguard Worker /** 16*0e209d39SAndroid Build Coastguard Worker * \file 17*0e209d39SAndroid Build Coastguard Worker * \brief C API: Formatted number result from various number formatting functions. 18*0e209d39SAndroid Build Coastguard Worker * 19*0e209d39SAndroid Build Coastguard Worker * Create a `UFormattedNumber` to hold the result of a number formatting operation. The same 20*0e209d39SAndroid Build Coastguard Worker * `UFormattedNumber` can be reused multiple times. 21*0e209d39SAndroid Build Coastguard Worker * 22*0e209d39SAndroid Build Coastguard Worker * <pre> 23*0e209d39SAndroid Build Coastguard Worker * LocalUFormattedNumberPointer uresult(unumf_openResult(status)); 24*0e209d39SAndroid Build Coastguard Worker * 25*0e209d39SAndroid Build Coastguard Worker * // pass uresult.getAlias() to your number formatter 26*0e209d39SAndroid Build Coastguard Worker * 27*0e209d39SAndroid Build Coastguard Worker * int32_t length; 28*0e209d39SAndroid Build Coastguard Worker * const UChar* s = ufmtval_getString(unumf_resultAsValue(uresult.getAlias(), status), &length, status)); 29*0e209d39SAndroid Build Coastguard Worker * 30*0e209d39SAndroid Build Coastguard Worker * // The string result is in `s` with the given `length` (it is also NUL-terminated). 31*0e209d39SAndroid Build Coastguard Worker * </pre> 32*0e209d39SAndroid Build Coastguard Worker */ 33*0e209d39SAndroid Build Coastguard Worker 34*0e209d39SAndroid Build Coastguard Worker 35*0e209d39SAndroid Build Coastguard Worker struct UFormattedNumber; 36*0e209d39SAndroid Build Coastguard Worker /** 37*0e209d39SAndroid Build Coastguard Worker * C-compatible version of icu::number::FormattedNumber. 38*0e209d39SAndroid Build Coastguard Worker * 39*0e209d39SAndroid Build Coastguard Worker * NOTE: This is a C-compatible API; C++ users should build against numberformatter.h instead. 40*0e209d39SAndroid Build Coastguard Worker * 41*0e209d39SAndroid Build Coastguard Worker * @stable ICU 62 42*0e209d39SAndroid Build Coastguard Worker */ 43*0e209d39SAndroid Build Coastguard Worker typedef struct UFormattedNumber UFormattedNumber; 44*0e209d39SAndroid Build Coastguard Worker 45*0e209d39SAndroid Build Coastguard Worker 46*0e209d39SAndroid Build Coastguard Worker /** 47*0e209d39SAndroid Build Coastguard Worker * Creates an object to hold the result of a UNumberFormatter 48*0e209d39SAndroid Build Coastguard Worker * operation. The object can be used repeatedly; it is cleared whenever 49*0e209d39SAndroid Build Coastguard Worker * passed to a format function. 50*0e209d39SAndroid Build Coastguard Worker * 51*0e209d39SAndroid Build Coastguard Worker * @param ec Set if an error occurs. 52*0e209d39SAndroid Build Coastguard Worker * @stable ICU 62 53*0e209d39SAndroid Build Coastguard Worker */ 54*0e209d39SAndroid Build Coastguard Worker U_CAPI UFormattedNumber* U_EXPORT2 55*0e209d39SAndroid Build Coastguard Worker unumf_openResult(UErrorCode* ec); 56*0e209d39SAndroid Build Coastguard Worker 57*0e209d39SAndroid Build Coastguard Worker 58*0e209d39SAndroid Build Coastguard Worker /** 59*0e209d39SAndroid Build Coastguard Worker * Returns a representation of a UFormattedNumber as a UFormattedValue, 60*0e209d39SAndroid Build Coastguard Worker * which can be subsequently passed to any API requiring that type. 61*0e209d39SAndroid Build Coastguard Worker * 62*0e209d39SAndroid Build Coastguard Worker * The returned object is owned by the UFormattedNumber and is valid 63*0e209d39SAndroid Build Coastguard Worker * only as long as the UFormattedNumber is present and unchanged in memory. 64*0e209d39SAndroid Build Coastguard Worker * 65*0e209d39SAndroid Build Coastguard Worker * You can think of this method as a cast between types. 66*0e209d39SAndroid Build Coastguard Worker * 67*0e209d39SAndroid Build Coastguard Worker * @param uresult The object containing the formatted string. 68*0e209d39SAndroid Build Coastguard Worker * @param ec Set if an error occurs. 69*0e209d39SAndroid Build Coastguard Worker * @return A UFormattedValue owned by the input object. 70*0e209d39SAndroid Build Coastguard Worker * @stable ICU 64 71*0e209d39SAndroid Build Coastguard Worker */ 72*0e209d39SAndroid Build Coastguard Worker U_CAPI const UFormattedValue* U_EXPORT2 73*0e209d39SAndroid Build Coastguard Worker unumf_resultAsValue(const UFormattedNumber* uresult, UErrorCode* ec); 74*0e209d39SAndroid Build Coastguard Worker 75*0e209d39SAndroid Build Coastguard Worker 76*0e209d39SAndroid Build Coastguard Worker /** 77*0e209d39SAndroid Build Coastguard Worker * Extracts the result number string out of a UFormattedNumber to a UChar buffer if possible. 78*0e209d39SAndroid Build Coastguard Worker * If bufferCapacity is greater than the required length, a terminating NUL is written. 79*0e209d39SAndroid Build Coastguard Worker * If bufferCapacity is less than the required length, an error code is set. 80*0e209d39SAndroid Build Coastguard Worker * 81*0e209d39SAndroid Build Coastguard Worker * Also see ufmtval_getString, which returns a NUL-terminated string: 82*0e209d39SAndroid Build Coastguard Worker * 83*0e209d39SAndroid Build Coastguard Worker * int32_t len; 84*0e209d39SAndroid Build Coastguard Worker * const UChar* str = ufmtval_getString(unumf_resultAsValue(uresult, &ec), &len, &ec); 85*0e209d39SAndroid Build Coastguard Worker * 86*0e209d39SAndroid Build Coastguard Worker * NOTE: This is a C-compatible API; C++ users should build against numberformatter.h instead. 87*0e209d39SAndroid Build Coastguard Worker * 88*0e209d39SAndroid Build Coastguard Worker * @param uresult The object containing the formatted number. 89*0e209d39SAndroid Build Coastguard Worker * @param buffer Where to save the string output. 90*0e209d39SAndroid Build Coastguard Worker * @param bufferCapacity The number of UChars available in the buffer. 91*0e209d39SAndroid Build Coastguard Worker * @param ec Set if an error occurs. 92*0e209d39SAndroid Build Coastguard Worker * @return The required length. 93*0e209d39SAndroid Build Coastguard Worker * @stable ICU 62 94*0e209d39SAndroid Build Coastguard Worker */ 95*0e209d39SAndroid Build Coastguard Worker U_CAPI int32_t U_EXPORT2 96*0e209d39SAndroid Build Coastguard Worker unumf_resultToString(const UFormattedNumber* uresult, UChar* buffer, int32_t bufferCapacity, 97*0e209d39SAndroid Build Coastguard Worker UErrorCode* ec); 98*0e209d39SAndroid Build Coastguard Worker 99*0e209d39SAndroid Build Coastguard Worker 100*0e209d39SAndroid Build Coastguard Worker /** 101*0e209d39SAndroid Build Coastguard Worker * Determines the start and end indices of the next occurrence of the given <em>field</em> in the 102*0e209d39SAndroid Build Coastguard Worker * output string. This allows you to determine the locations of, for example, the integer part, 103*0e209d39SAndroid Build Coastguard Worker * fraction part, or symbols. 104*0e209d39SAndroid Build Coastguard Worker * 105*0e209d39SAndroid Build Coastguard Worker * This is a simpler but less powerful alternative to {@link ufmtval_nextPosition}. 106*0e209d39SAndroid Build Coastguard Worker * 107*0e209d39SAndroid Build Coastguard Worker * If a field occurs just once, calling this method will find that occurrence and return it. If a 108*0e209d39SAndroid Build Coastguard Worker * field occurs multiple times, this method may be called repeatedly with the following pattern: 109*0e209d39SAndroid Build Coastguard Worker * 110*0e209d39SAndroid Build Coastguard Worker * <pre> 111*0e209d39SAndroid Build Coastguard Worker * UFieldPosition ufpos = {UNUM_GROUPING_SEPARATOR_FIELD, 0, 0}; 112*0e209d39SAndroid Build Coastguard Worker * while (unumf_resultNextFieldPosition(uresult, ufpos, &ec)) { 113*0e209d39SAndroid Build Coastguard Worker * // do something with ufpos. 114*0e209d39SAndroid Build Coastguard Worker * } 115*0e209d39SAndroid Build Coastguard Worker * </pre> 116*0e209d39SAndroid Build Coastguard Worker * 117*0e209d39SAndroid Build Coastguard Worker * This method is useful if you know which field to query. If you want all available field position 118*0e209d39SAndroid Build Coastguard Worker * information, use unumf_resultGetAllFieldPositions(). 119*0e209d39SAndroid Build Coastguard Worker * 120*0e209d39SAndroid Build Coastguard Worker * NOTE: All fields of the UFieldPosition must be initialized before calling this method. 121*0e209d39SAndroid Build Coastguard Worker * 122*0e209d39SAndroid Build Coastguard Worker * @param uresult The object containing the formatted number. 123*0e209d39SAndroid Build Coastguard Worker * @param ufpos 124*0e209d39SAndroid Build Coastguard Worker * Input+output variable. On input, the "field" property determines which field to look up, 125*0e209d39SAndroid Build Coastguard Worker * and the "endIndex" property determines where to begin the search. On output, the 126*0e209d39SAndroid Build Coastguard Worker * "beginIndex" field is set to the beginning of the first occurrence of the field after the 127*0e209d39SAndroid Build Coastguard Worker * input "endIndex", and "endIndex" is set to the end of that occurrence of the field 128*0e209d39SAndroid Build Coastguard Worker * (exclusive index). If a field position is not found, the FieldPosition is not changed and 129*0e209d39SAndroid Build Coastguard Worker * the method returns false. 130*0e209d39SAndroid Build Coastguard Worker * @param ec Set if an error occurs. 131*0e209d39SAndroid Build Coastguard Worker * @stable ICU 62 132*0e209d39SAndroid Build Coastguard Worker */ 133*0e209d39SAndroid Build Coastguard Worker U_CAPI UBool U_EXPORT2 134*0e209d39SAndroid Build Coastguard Worker unumf_resultNextFieldPosition(const UFormattedNumber* uresult, UFieldPosition* ufpos, UErrorCode* ec); 135*0e209d39SAndroid Build Coastguard Worker 136*0e209d39SAndroid Build Coastguard Worker 137*0e209d39SAndroid Build Coastguard Worker /** 138*0e209d39SAndroid Build Coastguard Worker * Populates the given iterator with all fields in the formatted output string. This allows you to 139*0e209d39SAndroid Build Coastguard Worker * determine the locations of the integer part, fraction part, and sign. 140*0e209d39SAndroid Build Coastguard Worker * 141*0e209d39SAndroid Build Coastguard Worker * This is an alternative to the more powerful {@link ufmtval_nextPosition} API. 142*0e209d39SAndroid Build Coastguard Worker * 143*0e209d39SAndroid Build Coastguard Worker * If you need information on only one field, use {@link ufmtval_nextPosition} or 144*0e209d39SAndroid Build Coastguard Worker * {@link unumf_resultNextFieldPosition}. 145*0e209d39SAndroid Build Coastguard Worker * 146*0e209d39SAndroid Build Coastguard Worker * @param uresult The object containing the formatted number. 147*0e209d39SAndroid Build Coastguard Worker * @param ufpositer 148*0e209d39SAndroid Build Coastguard Worker * A pointer to a UFieldPositionIterator created by {@link #ufieldpositer_open}. Iteration 149*0e209d39SAndroid Build Coastguard Worker * information already present in the UFieldPositionIterator is deleted, and the iterator is reset 150*0e209d39SAndroid Build Coastguard Worker * to apply to the fields in the formatted string created by this function call. The field values 151*0e209d39SAndroid Build Coastguard Worker * and indexes returned by {@link #ufieldpositer_next} represent fields denoted by 152*0e209d39SAndroid Build Coastguard Worker * the UNumberFormatFields enum. Fields are not returned in a guaranteed order. Fields cannot 153*0e209d39SAndroid Build Coastguard Worker * overlap, but they may nest. For example, 1234 could format as "1,234" which might consist of a 154*0e209d39SAndroid Build Coastguard Worker * grouping separator field for ',' and an integer field encompassing the entire string. 155*0e209d39SAndroid Build Coastguard Worker * @param ec Set if an error occurs. 156*0e209d39SAndroid Build Coastguard Worker * @stable ICU 62 157*0e209d39SAndroid Build Coastguard Worker */ 158*0e209d39SAndroid Build Coastguard Worker U_CAPI void U_EXPORT2 159*0e209d39SAndroid Build Coastguard Worker unumf_resultGetAllFieldPositions(const UFormattedNumber* uresult, UFieldPositionIterator* ufpositer, 160*0e209d39SAndroid Build Coastguard Worker UErrorCode* ec); 161*0e209d39SAndroid Build Coastguard Worker 162*0e209d39SAndroid Build Coastguard Worker 163*0e209d39SAndroid Build Coastguard Worker /** 164*0e209d39SAndroid Build Coastguard Worker * Extracts the formatted number as a "numeric string" conforming to the 165*0e209d39SAndroid Build Coastguard Worker * syntax defined in the Decimal Arithmetic Specification, available at 166*0e209d39SAndroid Build Coastguard Worker * http://speleotrove.com/decimal 167*0e209d39SAndroid Build Coastguard Worker * 168*0e209d39SAndroid Build Coastguard Worker * This endpoint is useful for obtaining the exact number being printed 169*0e209d39SAndroid Build Coastguard Worker * after scaling and rounding have been applied by the number formatter. 170*0e209d39SAndroid Build Coastguard Worker * 171*0e209d39SAndroid Build Coastguard Worker * @param uresult The input object containing the formatted number. 172*0e209d39SAndroid Build Coastguard Worker * @param dest the 8-bit char buffer into which the decimal number is placed 173*0e209d39SAndroid Build Coastguard Worker * @param destCapacity The size, in chars, of the destination buffer. May be zero 174*0e209d39SAndroid Build Coastguard Worker * for precomputing the required size. 175*0e209d39SAndroid Build Coastguard Worker * @param ec receives any error status. 176*0e209d39SAndroid Build Coastguard Worker * If U_BUFFER_OVERFLOW_ERROR: Returns number of chars for 177*0e209d39SAndroid Build Coastguard Worker * preflighting. 178*0e209d39SAndroid Build Coastguard Worker * @return Number of chars in the data. Does not include a trailing NUL. 179*0e209d39SAndroid Build Coastguard Worker * @stable ICU 68 180*0e209d39SAndroid Build Coastguard Worker */ 181*0e209d39SAndroid Build Coastguard Worker U_CAPI int32_t U_EXPORT2 182*0e209d39SAndroid Build Coastguard Worker unumf_resultToDecimalNumber( 183*0e209d39SAndroid Build Coastguard Worker const UFormattedNumber* uresult, 184*0e209d39SAndroid Build Coastguard Worker char* dest, 185*0e209d39SAndroid Build Coastguard Worker int32_t destCapacity, 186*0e209d39SAndroid Build Coastguard Worker UErrorCode* ec); 187*0e209d39SAndroid Build Coastguard Worker 188*0e209d39SAndroid Build Coastguard Worker 189*0e209d39SAndroid Build Coastguard Worker /** 190*0e209d39SAndroid Build Coastguard Worker * Releases the UFormattedNumber created by unumf_openResult(). 191*0e209d39SAndroid Build Coastguard Worker * 192*0e209d39SAndroid Build Coastguard Worker * @param uresult An object created by unumf_openResult(). 193*0e209d39SAndroid Build Coastguard Worker * @stable ICU 62 194*0e209d39SAndroid Build Coastguard Worker */ 195*0e209d39SAndroid Build Coastguard Worker U_CAPI void U_EXPORT2 196*0e209d39SAndroid Build Coastguard Worker unumf_closeResult(UFormattedNumber* uresult); 197*0e209d39SAndroid Build Coastguard Worker 198*0e209d39SAndroid Build Coastguard Worker 199*0e209d39SAndroid Build Coastguard Worker #if U_SHOW_CPLUSPLUS_API 200*0e209d39SAndroid Build Coastguard Worker U_NAMESPACE_BEGIN 201*0e209d39SAndroid Build Coastguard Worker 202*0e209d39SAndroid Build Coastguard Worker /** 203*0e209d39SAndroid Build Coastguard Worker * \class LocalUFormattedNumberPointer 204*0e209d39SAndroid Build Coastguard Worker * "Smart pointer" class; closes a UFormattedNumber via unumf_closeResult(). 205*0e209d39SAndroid Build Coastguard Worker * For most methods see the LocalPointerBase base class. 206*0e209d39SAndroid Build Coastguard Worker * 207*0e209d39SAndroid Build Coastguard Worker * Usage: 208*0e209d39SAndroid Build Coastguard Worker * <pre> 209*0e209d39SAndroid Build Coastguard Worker * LocalUFormattedNumberPointer uformatter(unumf_openResult(...)); 210*0e209d39SAndroid Build Coastguard Worker * // no need to explicitly call unumf_closeResult() 211*0e209d39SAndroid Build Coastguard Worker * </pre> 212*0e209d39SAndroid Build Coastguard Worker * 213*0e209d39SAndroid Build Coastguard Worker * @see LocalPointerBase 214*0e209d39SAndroid Build Coastguard Worker * @see LocalPointer 215*0e209d39SAndroid Build Coastguard Worker * @stable ICU 62 216*0e209d39SAndroid Build Coastguard Worker */ 217*0e209d39SAndroid Build Coastguard Worker U_DEFINE_LOCAL_OPEN_POINTER(LocalUFormattedNumberPointer, UFormattedNumber, unumf_closeResult); 218*0e209d39SAndroid Build Coastguard Worker 219*0e209d39SAndroid Build Coastguard Worker U_NAMESPACE_END 220*0e209d39SAndroid Build Coastguard Worker #endif // U_SHOW_CPLUSPLUS_API 221*0e209d39SAndroid Build Coastguard Worker 222*0e209d39SAndroid Build Coastguard Worker 223*0e209d39SAndroid Build Coastguard Worker #endif /* #if !UCONFIG_NO_FORMATTING */ 224*0e209d39SAndroid Build Coastguard Worker #endif //__UFORMATTEDNUMBER_H__ 225