1 // © 2018 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 #ifndef __UFORMATTEDVALUE_H__
5 #define __UFORMATTEDVALUE_H__
7 #include "unicode/utypes.h"
9 #if !UCONFIG_NO_FORMATTING
11 #include "unicode/ufieldpositer.h"
13 #ifndef U_HIDE_DRAFT_API
17 * \brief C API: Abstract operations for localized strings.
19 * This file contains declarations for classes that deal with formatted strings. A number
20 * of APIs throughout ICU use these classes for expressing their localized output.
25 * All possible field categories in ICU. Every entry in this enum corresponds
26 * to another enum that exists in ICU.
28 * In the APIs that take a UFieldCategory, an int32_t type is used. Field
29 * categories having any of the top four bits turned on are reserved as
30 * private-use for external APIs implementing FormattedValue. This means that
31 * categories 2^28 and higher or below zero (with the highest bit turned on)
32 * are private-use and will not be used by ICU in the future.
36 typedef enum UFieldCategory
{
38 * For an undefined field category.
42 UFIELD_CATEGORY_UNDEFINED
= 0,
45 * For fields in UDateFormatField (udat.h), from ICU 3.0.
52 * For fields in UNumberFormatFields (unum.h), from ICU 49.
56 UFIELD_CATEGORY_NUMBER
,
59 * For fields in UListFormatterField (ulistformatter.h), from ICU 63.
66 * For fields in URelativeDateTimeFormatterField (ureldatefmt.h), from ICU 64.
70 UFIELD_CATEGORY_RELATIVE_DATETIME
,
73 * Reserved for possible future fields in UDateIntervalFormatField.
77 UFIELD_CATEGORY_DATE_INTERVAL
,
79 #ifndef U_HIDE_INTERNAL_API
81 UFIELD_CATEGORY_COUNT
,
82 #endif /* U_HIDE_INTERNAL_API */
85 * Category for spans in a list.
89 UFIELD_CATEGORY_LIST_SPAN
= 0x1000 + UFIELD_CATEGORY_LIST
,
92 * Category for spans in a date interval.
96 UFIELD_CATEGORY_DATE_INTERVAL_SPAN
= 0x1000 + UFIELD_CATEGORY_DATE_INTERVAL
,
101 struct UConstrainedFieldPosition
;
103 * Represents a span of a string containing a given field.
105 * This struct differs from UFieldPosition in the following ways:
107 * 1. It has information on the field category.
108 * 2. It allows you to set constraints to use when iterating over field positions.
109 * 3. It is used for the newer FormattedValue APIs.
113 typedef struct UConstrainedFieldPosition UConstrainedFieldPosition
;
117 * Creates a new UConstrainedFieldPosition.
119 * By default, the UConstrainedFieldPosition has no iteration constraints.
121 * @param ec Set if an error occurs.
122 * @return The new object, or NULL if an error occurs.
125 U_DRAFT UConstrainedFieldPosition
* U_EXPORT2
126 ucfpos_open(UErrorCode
* ec
);
130 * Resets a UConstrainedFieldPosition to its initial state, as if it were newly created.
132 * Removes any constraints that may have been set on the instance.
134 * @param ucfpos The instance of UConstrainedFieldPosition.
135 * @param ec Set if an error occurs.
138 U_DRAFT
void U_EXPORT2
140 UConstrainedFieldPosition
* ucfpos
,
145 * Destroys a UConstrainedFieldPosition and releases its memory.
147 * @param ucfpos The instance of UConstrainedFieldPosition.
150 U_DRAFT
void U_EXPORT2
151 ucfpos_close(UConstrainedFieldPosition
* ucfpos
);
155 * Sets a constraint on the field category.
157 * When this instance of UConstrainedFieldPosition is passed to ufmtval_nextPosition,
158 * positions are skipped unless they have the given category.
160 * Any previously set constraints are cleared.
162 * For example, to loop over only the number-related fields:
164 * UConstrainedFieldPosition* ucfpos = ucfpos_open(ec);
165 * ucfpos_constrainCategory(ucfpos, UFIELDCATEGORY_NUMBER_FORMAT, ec);
166 * while (ufmtval_nextPosition(ufmtval, ucfpos, ec)) {
167 * // handle the number-related field position
169 * ucfpos_close(ucfpos);
171 * Changing the constraint while in the middle of iterating over a FormattedValue
172 * does not generally have well-defined behavior.
174 * @param ucfpos The instance of UConstrainedFieldPosition.
175 * @param category The field category to fix when iterating.
176 * @param ec Set if an error occurs.
179 U_DRAFT
void U_EXPORT2
180 ucfpos_constrainCategory(
181 UConstrainedFieldPosition
* ucfpos
,
187 * Sets a constraint on the category and field.
189 * When this instance of UConstrainedFieldPosition is passed to ufmtval_nextPosition,
190 * positions are skipped unless they have the given category and field.
192 * Any previously set constraints are cleared.
194 * For example, to loop over all grouping separators:
196 * UConstrainedFieldPosition* ucfpos = ucfpos_open(ec);
197 * ucfpos_constrainField(ucfpos, UFIELDCATEGORY_NUMBER_FORMAT, UNUM_GROUPING_SEPARATOR_FIELD, ec);
198 * while (ufmtval_nextPosition(ufmtval, ucfpos, ec)) {
199 * // handle the grouping separator position
201 * ucfpos_close(ucfpos);
203 * Changing the constraint while in the middle of iterating over a FormattedValue
204 * does not generally have well-defined behavior.
206 * @param ucfpos The instance of UConstrainedFieldPosition.
207 * @param category The field category to fix when iterating.
208 * @param field The field to fix when iterating.
209 * @param ec Set if an error occurs.
212 U_DRAFT
void U_EXPORT2
213 ucfpos_constrainField(
214 UConstrainedFieldPosition
* ucfpos
,
221 * Gets the field category for the current position.
223 * If a category or field constraint was set, this function returns the constrained
224 * category. Otherwise, the return value is well-defined only after
225 * ufmtval_nextPosition returns TRUE.
227 * @param ucfpos The instance of UConstrainedFieldPosition.
228 * @param ec Set if an error occurs.
229 * @return The field category saved in the instance.
232 U_DRAFT
int32_t U_EXPORT2
234 const UConstrainedFieldPosition
* ucfpos
,
239 * Gets the field for the current position.
241 * If a field constraint was set, this function returns the constrained
242 * field. Otherwise, the return value is well-defined only after
243 * ufmtval_nextPosition returns TRUE.
245 * @param ucfpos The instance of UConstrainedFieldPosition.
246 * @param ec Set if an error occurs.
247 * @return The field saved in the instance.
250 U_DRAFT
int32_t U_EXPORT2
252 const UConstrainedFieldPosition
* ucfpos
,
257 * Gets the INCLUSIVE start and EXCLUSIVE end index stored for the current position.
259 * The output values are well-defined only after ufmtval_nextPosition returns TRUE.
261 * @param ucfpos The instance of UConstrainedFieldPosition.
262 * @param pStart Set to the start index saved in the instance. Ignored if nullptr.
263 * @param pLimit Set to the end index saved in the instance. Ignored if nullptr.
264 * @param ec Set if an error occurs.
267 U_DRAFT
void U_EXPORT2
269 const UConstrainedFieldPosition
* ucfpos
,
276 * Gets an int64 that FormattedValue implementations may use for storage.
278 * The initial value is zero.
280 * Users of FormattedValue should not need to call this method.
282 * @param ucfpos The instance of UConstrainedFieldPosition.
283 * @param ec Set if an error occurs.
284 * @return The current iteration context from ucfpos_setInt64IterationContext.
287 U_DRAFT
int64_t U_EXPORT2
288 ucfpos_getInt64IterationContext(
289 const UConstrainedFieldPosition
* ucfpos
,
294 * Sets an int64 that FormattedValue implementations may use for storage.
296 * Intended to be used by FormattedValue implementations.
298 * @param ucfpos The instance of UConstrainedFieldPosition.
299 * @param context The new iteration context.
300 * @param ec Set if an error occurs.
303 U_DRAFT
void U_EXPORT2
304 ucfpos_setInt64IterationContext(
305 UConstrainedFieldPosition
* ucfpos
,
311 * Determines whether a given field should be included given the
314 * Intended to be used by FormattedValue implementations.
316 * @param ucfpos The instance of UConstrainedFieldPosition.
317 * @param category The category to test.
318 * @param field The field to test.
319 * @param ec Set if an error occurs.
322 U_DRAFT UBool U_EXPORT2
324 const UConstrainedFieldPosition
* ucfpos
,
331 * Sets new values for the primary public getters.
333 * Intended to be used by FormattedValue implementations.
335 * It is up to the implementation to ensure that the user-requested
336 * constraints are satisfied. This method does not check!
338 * @param ucfpos The instance of UConstrainedFieldPosition.
339 * @param category The new field category.
340 * @param field The new field.
341 * @param start The new inclusive start index.
342 * @param limit The new exclusive end index.
343 * @param ec Set if an error occurs.
346 U_DRAFT
void U_EXPORT2
348 UConstrainedFieldPosition
* ucfpos
,
356 struct UFormattedValue
;
358 * An abstract formatted value: a string with associated field attributes.
359 * Many formatters format to types compatible with UFormattedValue.
363 typedef struct UFormattedValue UFormattedValue
;
367 * Returns a pointer to the formatted string. The pointer is owned by the UFormattedValue. The
368 * return value is valid only as long as the UFormattedValue is present and unchanged in memory.
370 * The return value is NUL-terminated but could contain internal NULs.
373 * The object containing the formatted string and attributes.
374 * @param pLength Output variable for the length of the string. Ignored if NULL.
375 * @param ec Set if an error occurs.
376 * @return A NUL-terminated char16 string owned by the UFormattedValue.
379 U_DRAFT
const UChar
* U_EXPORT2
381 const UFormattedValue
* ufmtval
,
387 * Iterates over field positions in the UFormattedValue. This lets you determine the position
388 * of specific types of substrings, like a month or a decimal separator.
390 * To loop over all field positions:
392 * UConstrainedFieldPosition* ucfpos = ucfpos_open(ec);
393 * while (ufmtval_nextPosition(ufmtval, ucfpos, ec)) {
394 * // handle the field position; get information from ucfpos
396 * ucfpos_close(ucfpos);
399 * The object containing the formatted string and attributes.
401 * The object used for iteration state; can provide constraints to iterate over only
402 * one specific category or field;
403 * see ucfpos_constrainCategory
404 * and ucfpos_constrainField.
405 * @param ec Set if an error occurs.
406 * @return TRUE if another position was found; FALSE otherwise.
409 U_DRAFT UBool U_EXPORT2
410 ufmtval_nextPosition(
411 const UFormattedValue
* ufmtval
,
412 UConstrainedFieldPosition
* ucfpos
,
416 #if U_SHOW_CPLUSPLUS_API
420 * \class LocalUConstrainedFieldPositionPointer
421 * "Smart pointer" class; closes a UConstrainedFieldPosition via ucfpos_close().
422 * For most methods see the LocalPointerBase base class.
426 * LocalUConstrainedFieldPositionPointer ucfpos(ucfpos_open(ec));
427 * // no need to explicitly call ucfpos_close()
431 U_DEFINE_LOCAL_OPEN_POINTER(LocalUConstrainedFieldPositionPointer
,
432 UConstrainedFieldPosition
,
436 #endif // U_SHOW_CPLUSPLUS_API
439 #endif /* U_HIDE_DRAFT_API */
440 #endif /* #if !UCONFIG_NO_FORMATTING */
441 #endif // __UFORMATTEDVALUE_H__