uformattednumber.h 8.1 KB

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