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
10 #ifndef U_HIDE_DRAFT_API
12 #include "unicode/ufieldpositer.h"
16 * \brief C API: Abstract operations for localized strings.
18 * This file contains declarations for classes that deal with formatted strings. A number
19 * of APIs throughout ICU use these classes for expressing their localized output.
24 * All possible field categories in ICU. Every entry in this enum corresponds
25 * to another enum that exists in ICU.
27 * In the APIs that take a UFieldCategory, an int32_t type is used. Field
28 * categories having any of the top four bits turned on are reserved as
29 * private-use for external APIs implementing FormattedValue. This means that
30 * categories 2^28 and higher or below zero (with the highest bit turned on)
31 * are private-use and will not be used by ICU in the future.
35 typedef enum UFieldCategory
{
37 * For an undefined field category.
41 UFIELD_CATEGORY_UNDEFINED
= 0,
44 * For fields in UDateFormatField (udat.h), from ICU 3.0.
51 * For fields in UNumberFormatFields (unum.h), from ICU 49.
55 UFIELD_CATEGORY_NUMBER
,
58 * For fields in UListFormatterField (ulistformatter.h), from ICU 63.
65 * For fields in URelativeDateTimeFormatterField (ureldatefmt.h), from ICU 64.
69 UFIELD_CATEGORY_RELATIVE_DATETIME
,
72 * Reserved for possible future fields in UDateIntervalFormatField.
76 UFIELD_CATEGORY_DATE_INTERVAL
,
78 #ifndef U_HIDE_INTERNAL_API
80 UFIELD_CATEGORY_COUNT
,
81 #endif /* U_HIDE_INTERNAL_API */
84 * Category for spans in a list.
88 UFIELD_CATEGORY_LIST_SPAN
= 0x1000 + UFIELD_CATEGORY_LIST
,
91 * Category for spans in a date interval.
95 UFIELD_CATEGORY_DATE_INTERVAL_SPAN
= 0x1000 + UFIELD_CATEGORY_DATE_INTERVAL
,
100 struct UConstrainedFieldPosition
;
102 * Represents a span of a string containing a given field.
104 * This struct differs from UFieldPosition in the following ways:
106 * 1. It has information on the field category.
107 * 2. It allows you to set constraints to use when iterating over field positions.
108 * 3. It is used for the newer FormattedValue APIs.
112 typedef struct UConstrainedFieldPosition UConstrainedFieldPosition
;
116 * Creates a new UConstrainedFieldPosition.
118 * By default, the UConstrainedFieldPosition has no iteration constraints.
120 * @param ec Set if an error occurs.
121 * @return The new object, or NULL if an error occurs.
124 U_DRAFT UConstrainedFieldPosition
* U_EXPORT2
125 ucfpos_open(UErrorCode
* ec
);
129 * Resets a UConstrainedFieldPosition to its initial state, as if it were newly created.
131 * Removes any constraints that may have been set on the instance.
133 * @param ucfpos The instance of UConstrainedFieldPosition.
134 * @param ec Set if an error occurs.
137 U_DRAFT
void U_EXPORT2
139 UConstrainedFieldPosition
* ucfpos
,
144 * Destroys a UConstrainedFieldPosition and releases its memory.
146 * @param ucfpos The instance of UConstrainedFieldPosition.
149 U_DRAFT
void U_EXPORT2
150 ucfpos_close(UConstrainedFieldPosition
* ucfpos
);
154 * Sets a constraint on the field category.
156 * When this instance of UConstrainedFieldPosition is passed to ufmtval_nextPosition,
157 * positions are skipped unless they have the given category.
159 * Any previously set constraints are cleared.
161 * For example, to loop over only the number-related fields:
163 * UConstrainedFieldPosition* ucfpos = ucfpos_open(ec);
164 * ucfpos_constrainCategory(ucfpos, UFIELDCATEGORY_NUMBER_FORMAT, ec);
165 * while (ufmtval_nextPosition(ufmtval, ucfpos, ec)) {
166 * // handle the number-related field position
168 * ucfpos_close(ucfpos);
170 * Changing the constraint while in the middle of iterating over a FormattedValue
171 * does not generally have well-defined behavior.
173 * @param ucfpos The instance of UConstrainedFieldPosition.
174 * @param category The field category to fix when iterating.
175 * @param ec Set if an error occurs.
178 U_DRAFT
void U_EXPORT2
179 ucfpos_constrainCategory(
180 UConstrainedFieldPosition
* ucfpos
,
186 * Sets a constraint on the category and field.
188 * When this instance of UConstrainedFieldPosition is passed to ufmtval_nextPosition,
189 * positions are skipped unless they have the given category and field.
191 * Any previously set constraints are cleared.
193 * For example, to loop over all grouping separators:
195 * UConstrainedFieldPosition* ucfpos = ucfpos_open(ec);
196 * ucfpos_constrainField(ucfpos, UFIELDCATEGORY_NUMBER_FORMAT, UNUM_GROUPING_SEPARATOR_FIELD, ec);
197 * while (ufmtval_nextPosition(ufmtval, ucfpos, ec)) {
198 * // handle the grouping separator position
200 * ucfpos_close(ucfpos);
202 * Changing the constraint while in the middle of iterating over a FormattedValue
203 * does not generally have well-defined behavior.
205 * @param ucfpos The instance of UConstrainedFieldPosition.
206 * @param category The field category to fix when iterating.
207 * @param field The field to fix when iterating.
208 * @param ec Set if an error occurs.
211 U_DRAFT
void U_EXPORT2
212 ucfpos_constrainField(
213 UConstrainedFieldPosition
* ucfpos
,
220 * Gets the field category for the current position.
222 * If a category or field constraint was set, this function returns the constrained
223 * category. Otherwise, the return value is well-defined only after
224 * ufmtval_nextPosition returns TRUE.
226 * @param ucfpos The instance of UConstrainedFieldPosition.
227 * @param ec Set if an error occurs.
228 * @return The field category saved in the instance.
231 U_DRAFT
int32_t U_EXPORT2
233 const UConstrainedFieldPosition
* ucfpos
,
238 * Gets the field for the current position.
240 * If a field constraint was set, this function returns the constrained
241 * field. Otherwise, the return value is well-defined only after
242 * ufmtval_nextPosition returns TRUE.
244 * @param ucfpos The instance of UConstrainedFieldPosition.
245 * @param ec Set if an error occurs.
246 * @return The field saved in the instance.
249 U_DRAFT
int32_t U_EXPORT2
251 const UConstrainedFieldPosition
* ucfpos
,
256 * Gets the INCLUSIVE start and EXCLUSIVE end index stored for the current position.
258 * The output values are well-defined only after ufmtval_nextPosition returns TRUE.
260 * @param ucfpos The instance of UConstrainedFieldPosition.
261 * @param pStart Set to the start index saved in the instance. Ignored if nullptr.
262 * @param pLimit Set to the end index saved in the instance. Ignored if nullptr.
263 * @param ec Set if an error occurs.
266 U_DRAFT
void U_EXPORT2
268 const UConstrainedFieldPosition
* ucfpos
,
275 * Gets an int64 that FormattedValue implementations may use for storage.
277 * The initial value is zero.
279 * Users of FormattedValue should not need to call this method.
281 * @param ucfpos The instance of UConstrainedFieldPosition.
282 * @param ec Set if an error occurs.
283 * @return The current iteration context from ucfpos_setInt64IterationContext.
286 U_DRAFT
int64_t U_EXPORT2
287 ucfpos_getInt64IterationContext(
288 const UConstrainedFieldPosition
* ucfpos
,
293 * Sets an int64 that FormattedValue implementations may use for storage.
295 * Intended to be used by FormattedValue implementations.
297 * @param ucfpos The instance of UConstrainedFieldPosition.
298 * @param context The new iteration context.
299 * @param ec Set if an error occurs.
302 U_DRAFT
void U_EXPORT2
303 ucfpos_setInt64IterationContext(
304 UConstrainedFieldPosition
* ucfpos
,
310 * Determines whether a given field should be included given the
313 * Intended to be used by FormattedValue implementations.
315 * @param ucfpos The instance of UConstrainedFieldPosition.
316 * @param category The category to test.
317 * @param field The field to test.
318 * @param ec Set if an error occurs.
321 U_DRAFT UBool U_EXPORT2
323 const UConstrainedFieldPosition
* ucfpos
,
330 * Sets new values for the primary public getters.
332 * Intended to be used by FormattedValue implementations.
334 * It is up to the implementation to ensure that the user-requested
335 * constraints are satisfied. This method does not check!
337 * @param ucfpos The instance of UConstrainedFieldPosition.
338 * @param category The new field category.
339 * @param field The new field.
340 * @param start The new inclusive start index.
341 * @param limit The new exclusive end index.
342 * @param ec Set if an error occurs.
345 U_DRAFT
void U_EXPORT2
347 UConstrainedFieldPosition
* ucfpos
,
355 struct UFormattedValue
;
357 * An abstract formatted value: a string with associated field attributes.
358 * Many formatters format to types compatible with UFormattedValue.
362 typedef struct UFormattedValue UFormattedValue
;
366 * Returns a pointer to the formatted string. The pointer is owned by the UFormattedValue. The
367 * return value is valid only as long as the UFormattedValue is present and unchanged in memory.
369 * The return value is NUL-terminated but could contain internal NULs.
372 * The object containing the formatted string and attributes.
373 * @param pLength Output variable for the length of the string. Ignored if NULL.
374 * @param ec Set if an error occurs.
375 * @return A NUL-terminated char16 string owned by the UFormattedValue.
378 U_DRAFT
const UChar
* U_EXPORT2
380 const UFormattedValue
* ufmtval
,
386 * Iterates over field positions in the UFormattedValue. This lets you determine the position
387 * of specific types of substrings, like a month or a decimal separator.
389 * To loop over all field positions:
391 * UConstrainedFieldPosition* ucfpos = ucfpos_open(ec);
392 * while (ufmtval_nextPosition(ufmtval, ucfpos, ec)) {
393 * // handle the field position; get information from ucfpos
395 * ucfpos_close(ucfpos);
398 * The object containing the formatted string and attributes.
400 * The object used for iteration state; can provide constraints to iterate over only
401 * one specific category or field;
402 * see ucfpos_constrainCategory
403 * and ucfpos_constrainField.
404 * @param ec Set if an error occurs.
405 * @return TRUE if another position was found; FALSE otherwise.
408 U_DRAFT UBool U_EXPORT2
409 ufmtval_nextPosition(
410 const UFormattedValue
* ufmtval
,
411 UConstrainedFieldPosition
* ucfpos
,
415 #if U_SHOW_CPLUSPLUS_API
419 * \class LocalUConstrainedFieldPositionPointer
420 * "Smart pointer" class; closes a UConstrainedFieldPosition via ucfpos_close().
421 * For most methods see the LocalPointerBase base class.
425 * LocalUConstrainedFieldPositionPointer ucfpos(ucfpos_open(ec));
426 * // no need to explicitly call ucfpos_close()
430 U_DEFINE_LOCAL_OPEN_POINTER(LocalUConstrainedFieldPositionPointer
,
431 UConstrainedFieldPosition
,
435 #endif // U_SHOW_CPLUSPLUS_API
438 #endif /* U_HIDE_DRAFT_API */
439 #endif /* #if !UCONFIG_NO_FORMATTING */
440 #endif // __UFORMATTEDVALUE_H__