| 1 |
// © 2018 and later: Unicode, Inc. and others. |
| 2 |
// License & terms of use: http://www.unicode.org/copyright.html |
| 3 |
|
| 4 |
#ifndef __UFORMATTEDVALUE_H__ |
| 5 |
#define __UFORMATTEDVALUE_H__ |
| 6 |
|
| 7 |
#include "unicode/utypes.h" |
| 8 |
|
| 9 |
#if !UCONFIG_NO_FORMATTING |
| 10 |
|
| 11 |
#include "unicode/ufieldpositer.h" |
| 12 |
|
| 13 |
/** |
| 14 |
* \file |
| 15 |
* \brief C API: Abstract operations for localized strings. |
| 16 |
* |
| 17 |
* This file contains declarations for classes that deal with formatted strings. A number |
| 18 |
* of APIs throughout ICU use these classes for expressing their localized output. |
| 19 |
*/ |
| 20 |
|
| 21 |
|
| 22 |
/** |
| 23 |
* All possible field categories in ICU. Every entry in this enum corresponds |
| 24 |
* to another enum that exists in ICU. |
| 25 |
* |
| 26 |
* In the APIs that take a UFieldCategory, an int32_t type is used. Field |
| 27 |
* categories having any of the top four bits turned on are reserved as |
| 28 |
* private-use for external APIs implementing FormattedValue. This means that |
| 29 |
* categories 2^28 and higher or below zero (with the highest bit turned on) |
| 30 |
* are private-use and will not be used by ICU in the future. |
| 31 |
* |
| 32 |
* @stable ICU 64 |
| 33 |
*/ |
| 34 |
typedef enum UFieldCategory { |
| 35 |
/** |
| 36 |
* For an undefined field category. |
| 37 |
* |
| 38 |
* @stable ICU 64 |
| 39 |
*/ |
| 40 |
UFIELD_CATEGORY_UNDEFINED = 0, |
| 41 |
|
| 42 |
/** |
| 43 |
* For fields in UDateFormatField (udat.h), from ICU 3.0. |
| 44 |
* |
| 45 |
* @stable ICU 64 |
| 46 |
*/ |
| 47 |
UFIELD_CATEGORY_DATE, |
| 48 |
|
| 49 |
/** |
| 50 |
* For fields in UNumberFormatFields (unum.h), from ICU 49. |
| 51 |
* |
| 52 |
* @stable ICU 64 |
| 53 |
*/ |
| 54 |
UFIELD_CATEGORY_NUMBER, |
| 55 |
|
| 56 |
/** |
| 57 |
* For fields in UListFormatterField (ulistformatter.h), from ICU 63. |
| 58 |
* |
| 59 |
* @stable ICU 64 |
| 60 |
*/ |
| 61 |
UFIELD_CATEGORY_LIST, |
| 62 |
|
| 63 |
/** |
| 64 |
* For fields in URelativeDateTimeFormatterField (ureldatefmt.h), from ICU 64. |
| 65 |
* |
| 66 |
* @stable ICU 64 |
| 67 |
*/ |
| 68 |
UFIELD_CATEGORY_RELATIVE_DATETIME, |
| 69 |
|
| 70 |
/** |
| 71 |
* Reserved for possible future fields in UDateIntervalFormatField. |
| 72 |
* |
| 73 |
* @internal |
| 74 |
*/ |
| 75 |
UFIELD_CATEGORY_DATE_INTERVAL, |
| 76 |
|
| 77 |
#ifndef U_HIDE_INTERNAL_API |
| 78 |
/** @internal */ |
| 79 |
UFIELD_CATEGORY_COUNT, |
| 80 |
#endif /* U_HIDE_INTERNAL_API */ |
| 81 |
|
| 82 |
/** |
| 83 |
* Category for spans in a list. |
| 84 |
* |
| 85 |
* @stable ICU 64 |
| 86 |
*/ |
| 87 |
UFIELD_CATEGORY_LIST_SPAN = 0x1000 + UFIELD_CATEGORY_LIST, |
| 88 |
|
| 89 |
/** |
| 90 |
* Category for spans in a date interval. |
| 91 |
* |
| 92 |
* @stable ICU 64 |
| 93 |
*/ |
| 94 |
UFIELD_CATEGORY_DATE_INTERVAL_SPAN = 0x1000 + UFIELD_CATEGORY_DATE_INTERVAL, |
| 95 |
|
| 96 |
/** |
| 97 |
* Category for spans in a number range. |
| 98 |
* |
| 99 |
* @stable ICU 69 |
| 100 |
*/ |
| 101 |
UFIELD_CATEGORY_NUMBER_RANGE_SPAN = 0x1000 + UFIELD_CATEGORY_NUMBER, |
| 102 |
|
| 103 |
} UFieldCategory; |
| 104 |
|
| 105 |
|
| 106 |
struct UConstrainedFieldPosition; |
| 107 |
/** |
| 108 |
* Represents a span of a string containing a given field. |
| 109 |
* |
| 110 |
* This struct differs from UFieldPosition in the following ways: |
| 111 |
* |
| 112 |
* 1. It has information on the field category. |
| 113 |
* 2. It allows you to set constraints to use when iterating over field positions. |
| 114 |
* 3. It is used for the newer FormattedValue APIs. |
| 115 |
* |
| 116 |
* @stable ICU 64 |
| 117 |
*/ |
| 118 |
typedef struct UConstrainedFieldPosition UConstrainedFieldPosition; |
| 119 |
|
| 120 |
|
| 121 |
/** |
| 122 |
* Creates a new UConstrainedFieldPosition. |
| 123 |
* |
| 124 |
* By default, the UConstrainedFieldPosition has no iteration constraints. |
| 125 |
* |
| 126 |
* @param ec Set if an error occurs. |
| 127 |
* @return The new object, or NULL if an error occurs. |
| 128 |
* @stable ICU 64 |
| 129 |
*/ |
| 130 |
U_CAPI UConstrainedFieldPosition* U_EXPORT2 |
| 131 |
ucfpos_open(UErrorCode* ec); |
| 132 |
|
| 133 |
|
| 134 |
/** |
| 135 |
* Resets a UConstrainedFieldPosition to its initial state, as if it were newly created. |
| 136 |
* |
| 137 |
* Removes any constraints that may have been set on the instance. |
| 138 |
* |
| 139 |
* @param ucfpos The instance of UConstrainedFieldPosition. |
| 140 |
* @param ec Set if an error occurs. |
| 141 |
* @stable ICU 64 |
| 142 |
*/ |
| 143 |
U_CAPI void U_EXPORT2 |
| 144 |
ucfpos_reset( |
| 145 |
UConstrainedFieldPosition* ucfpos, |
| 146 |
UErrorCode* ec); |
| 147 |
|
| 148 |
|
| 149 |
/** |
| 150 |
* Destroys a UConstrainedFieldPosition and releases its memory. |
| 151 |
* |
| 152 |
* @param ucfpos The instance of UConstrainedFieldPosition. |
| 153 |
* @stable ICU 64 |
| 154 |
*/ |
| 155 |
U_CAPI void U_EXPORT2 |
| 156 |
ucfpos_close(UConstrainedFieldPosition* ucfpos); |
| 157 |
|
| 158 |
|
| 159 |
/** |
| 160 |
* Sets a constraint on the field category. |
| 161 |
* |
| 162 |
* When this instance of UConstrainedFieldPosition is passed to ufmtval_nextPosition, |
| 163 |
* positions are skipped unless they have the given category. |
| 164 |
* |
| 165 |
* Any previously set constraints are cleared. |
| 166 |
* |
| 167 |
* For example, to loop over only the number-related fields: |
| 168 |
* |
| 169 |
* UConstrainedFieldPosition* ucfpos = ucfpos_open(ec); |
| 170 |
* ucfpos_constrainCategory(ucfpos, UFIELDCATEGORY_NUMBER_FORMAT, ec); |
| 171 |
* while (ufmtval_nextPosition(ufmtval, ucfpos, ec)) { |
| 172 |
* // handle the number-related field position |
| 173 |
* } |
| 174 |
* ucfpos_close(ucfpos); |
| 175 |
* |
| 176 |
* Changing the constraint while in the middle of iterating over a FormattedValue |
| 177 |
* does not generally have well-defined behavior. |
| 178 |
* |
| 179 |
* @param ucfpos The instance of UConstrainedFieldPosition. |
| 180 |
* @param category The field category to fix when iterating. |
| 181 |
* @param ec Set if an error occurs. |
| 182 |
* @stable ICU 64 |
| 183 |
*/ |
| 184 |
U_CAPI void U_EXPORT2 |
| 185 |
ucfpos_constrainCategory( |
| 186 |
UConstrainedFieldPosition* ucfpos, |
| 187 |
int32_t category, |
| 188 |
UErrorCode* ec); |
| 189 |
|
| 190 |
|
| 191 |
/** |
| 192 |
* Sets a constraint on the category and field. |
| 193 |
* |
| 194 |
* When this instance of UConstrainedFieldPosition is passed to ufmtval_nextPosition, |
| 195 |
* positions are skipped unless they have the given category and field. |
| 196 |
* |
| 197 |
* Any previously set constraints are cleared. |
| 198 |
* |
| 199 |
* For example, to loop over all grouping separators: |
| 200 |
* |
| 201 |
* UConstrainedFieldPosition* ucfpos = ucfpos_open(ec); |
| 202 |
* ucfpos_constrainField(ucfpos, UFIELDCATEGORY_NUMBER_FORMAT, UNUM_GROUPING_SEPARATOR_FIELD, ec); |
| 203 |
* while (ufmtval_nextPosition(ufmtval, ucfpos, ec)) { |
| 204 |
* // handle the grouping separator position |
| 205 |
* } |
| 206 |
* ucfpos_close(ucfpos); |
| 207 |
* |
| 208 |
* Changing the constraint while in the middle of iterating over a FormattedValue |
| 209 |
* does not generally have well-defined behavior. |
| 210 |
* |
| 211 |
* @param ucfpos The instance of UConstrainedFieldPosition. |
| 212 |
* @param category The field category to fix when iterating. |
| 213 |
* @param field The field to fix when iterating. |
| 214 |
* @param ec Set if an error occurs. |
| 215 |
* @stable ICU 64 |
| 216 |
*/ |
| 217 |
U_CAPI void U_EXPORT2 |
| 218 |
ucfpos_constrainField( |
| 219 |
UConstrainedFieldPosition* ucfpos, |
| 220 |
int32_t category, |
| 221 |
int32_t field, |
| 222 |
UErrorCode* ec); |
| 223 |
|
| 224 |
|
| 225 |
/** |
| 226 |
* Gets the field category for the current position. |
| 227 |
* |
| 228 |
* If a category or field constraint was set, this function returns the constrained |
| 229 |
* category. Otherwise, the return value is well-defined only after |
| 230 |
* ufmtval_nextPosition returns true. |
| 231 |
* |
| 232 |
* @param ucfpos The instance of UConstrainedFieldPosition. |
| 233 |
* @param ec Set if an error occurs. |
| 234 |
* @return The field category saved in the instance. |
| 235 |
* @stable ICU 64 |
| 236 |
*/ |
| 237 |
U_CAPI int32_t U_EXPORT2 |
| 238 |
ucfpos_getCategory( |
| 239 |
const UConstrainedFieldPosition* ucfpos, |
| 240 |
UErrorCode* ec); |
| 241 |
|
| 242 |
|
| 243 |
/** |
| 244 |
* Gets the field for the current position. |
| 245 |
* |
| 246 |
* If a field constraint was set, this function returns the constrained |
| 247 |
* field. Otherwise, the return value is well-defined only after |
| 248 |
* ufmtval_nextPosition returns true. |
| 249 |
* |
| 250 |
* @param ucfpos The instance of UConstrainedFieldPosition. |
| 251 |
* @param ec Set if an error occurs. |
| 252 |
* @return The field saved in the instance. |
| 253 |
* @stable ICU 64 |
| 254 |
*/ |
| 255 |
U_CAPI int32_t U_EXPORT2 |
| 256 |
ucfpos_getField( |
| 257 |
const UConstrainedFieldPosition* ucfpos, |
| 258 |
UErrorCode* ec); |
| 259 |
|
| 260 |
|
| 261 |
/** |
| 262 |
* Gets the INCLUSIVE start and EXCLUSIVE end index stored for the current position. |
| 263 |
* |
| 264 |
* The output values are well-defined only after ufmtval_nextPosition returns true. |
| 265 |
* |
| 266 |
* @param ucfpos The instance of UConstrainedFieldPosition. |
| 267 |
* @param pStart Set to the start index saved in the instance. Ignored if nullptr. |
| 268 |
* @param pLimit Set to the end index saved in the instance. Ignored if nullptr. |
| 269 |
* @param ec Set if an error occurs. |
| 270 |
* @stable ICU 64 |
| 271 |
*/ |
| 272 |
U_CAPI void U_EXPORT2 |
| 273 |
ucfpos_getIndexes( |
| 274 |
const UConstrainedFieldPosition* ucfpos, |
| 275 |
int32_t* pStart, |
| 276 |
int32_t* pLimit, |
| 277 |
UErrorCode* ec); |
| 278 |
|
| 279 |
|
| 280 |
/** |
| 281 |
* Gets an int64 that FormattedValue implementations may use for storage. |
| 282 |
* |
| 283 |
* The initial value is zero. |
| 284 |
* |
| 285 |
* Users of FormattedValue should not need to call this method. |
| 286 |
* |
| 287 |
* @param ucfpos The instance of UConstrainedFieldPosition. |
| 288 |
* @param ec Set if an error occurs. |
| 289 |
* @return The current iteration context from ucfpos_setInt64IterationContext. |
| 290 |
* @stable ICU 64 |
| 291 |
*/ |
| 292 |
U_CAPI int64_t U_EXPORT2 |
| 293 |
ucfpos_getInt64IterationContext( |
| 294 |
const UConstrainedFieldPosition* ucfpos, |
| 295 |
UErrorCode* ec); |
| 296 |
|
| 297 |
|
| 298 |
/** |
| 299 |
* Sets an int64 that FormattedValue implementations may use for storage. |
| 300 |
* |
| 301 |
* Intended to be used by FormattedValue implementations. |
| 302 |
* |
| 303 |
* @param ucfpos The instance of UConstrainedFieldPosition. |
| 304 |
* @param context The new iteration context. |
| 305 |
* @param ec Set if an error occurs. |
| 306 |
* @stable ICU 64 |
| 307 |
*/ |
| 308 |
U_CAPI void U_EXPORT2 |
| 309 |
ucfpos_setInt64IterationContext( |
| 310 |
UConstrainedFieldPosition* ucfpos, |
| 311 |
int64_t context, |
| 312 |
UErrorCode* ec); |
| 313 |
|
| 314 |
|
| 315 |
/** |
| 316 |
* Determines whether a given field should be included given the |
| 317 |
* constraints. |
| 318 |
* |
| 319 |
* Intended to be used by FormattedValue implementations. |
| 320 |
* |
| 321 |
* @param ucfpos The instance of UConstrainedFieldPosition. |
| 322 |
* @param category The category to test. |
| 323 |
* @param field The field to test. |
| 324 |
* @param ec Set if an error occurs. |
| 325 |
* @stable ICU 64 |
| 326 |
*/ |
| 327 |
U_CAPI UBool U_EXPORT2 |
| 328 |
ucfpos_matchesField( |
| 329 |
const UConstrainedFieldPosition* ucfpos, |
| 330 |
int32_t category, |
| 331 |
int32_t field, |
| 332 |
UErrorCode* ec); |
| 333 |
|
| 334 |
|
| 335 |
/** |
| 336 |
* Sets new values for the primary public getters. |
| 337 |
* |
| 338 |
* Intended to be used by FormattedValue implementations. |
| 339 |
* |
| 340 |
* It is up to the implementation to ensure that the user-requested |
| 341 |
* constraints are satisfied. This method does not check! |
| 342 |
* |
| 343 |
* @param ucfpos The instance of UConstrainedFieldPosition. |
| 344 |
* @param category The new field category. |
| 345 |
* @param field The new field. |
| 346 |
* @param start The new inclusive start index. |
| 347 |
* @param limit The new exclusive end index. |
| 348 |
* @param ec Set if an error occurs. |
| 349 |
* @stable ICU 64 |
| 350 |
*/ |
| 351 |
U_CAPI void U_EXPORT2 |
| 352 |
ucfpos_setState( |
| 353 |
UConstrainedFieldPosition* ucfpos, |
| 354 |
int32_t category, |
| 355 |
int32_t field, |
| 356 |
int32_t start, |
| 357 |
int32_t limit, |
| 358 |
UErrorCode* ec); |
| 359 |
|
| 360 |
|
| 361 |
struct UFormattedValue; |
| 362 |
/** |
| 363 |
* An abstract formatted value: a string with associated field attributes. |
| 364 |
* Many formatters format to types compatible with UFormattedValue. |
| 365 |
* |
| 366 |
* @stable ICU 64 |
| 367 |
*/ |
| 368 |
typedef struct UFormattedValue UFormattedValue; |
| 369 |
|
| 370 |
|
| 371 |
/** |
| 372 |
* Returns a pointer to the formatted string. The pointer is owned by the UFormattedValue. The |
| 373 |
* return value is valid only as long as the UFormattedValue is present and unchanged in memory. |
| 374 |
* |
| 375 |
* The return value is NUL-terminated but could contain internal NULs. |
| 376 |
* |
| 377 |
* @param ufmtval |
| 378 |
* The object containing the formatted string and attributes. |
| 379 |
* @param pLength Output variable for the length of the string. Ignored if NULL. |
| 380 |
* @param ec Set if an error occurs. |
| 381 |
* @return A NUL-terminated char16 string owned by the UFormattedValue. |
| 382 |
* @stable ICU 64 |
| 383 |
*/ |
| 384 |
U_CAPI const UChar* U_EXPORT2 |
| 385 |
ufmtval_getString( |
| 386 |
const UFormattedValue* ufmtval, |
| 387 |
int32_t* pLength, |
| 388 |
UErrorCode* ec); |
| 389 |
|
| 390 |
|
| 391 |
/** |
| 392 |
* Iterates over field positions in the UFormattedValue. This lets you determine the position |
| 393 |
* of specific types of substrings, like a month or a decimal separator. |
| 394 |
* |
| 395 |
* To loop over all field positions: |
| 396 |
* |
| 397 |
* UConstrainedFieldPosition* ucfpos = ucfpos_open(ec); |
| 398 |
* while (ufmtval_nextPosition(ufmtval, ucfpos, ec)) { |
| 399 |
* // handle the field position; get information from ucfpos |
| 400 |
* } |
| 401 |
* ucfpos_close(ucfpos); |
| 402 |
* |
| 403 |
* @param ufmtval |
| 404 |
* The object containing the formatted string and attributes. |
| 405 |
* @param ucfpos |
| 406 |
* The object used for iteration state; can provide constraints to iterate over only |
| 407 |
* one specific category or field; |
| 408 |
* see ucfpos_constrainCategory |
| 409 |
* and ucfpos_constrainField. |
| 410 |
* @param ec Set if an error occurs. |
| 411 |
* @return true if another position was found; false otherwise. |
| 412 |
* @stable ICU 64 |
| 413 |
*/ |
| 414 |
U_CAPI UBool U_EXPORT2 |
| 415 |
ufmtval_nextPosition( |
| 416 |
const UFormattedValue* ufmtval, |
| 417 |
UConstrainedFieldPosition* ucfpos, |
| 418 |
UErrorCode* ec); |
| 419 |
|
| 420 |
|
| 421 |
#if U_SHOW_CPLUSPLUS_API |
| 422 |
U_NAMESPACE_BEGIN |
| 423 |
|
| 424 |
/** |
| 425 |
* \class LocalUConstrainedFieldPositionPointer |
| 426 |
* "Smart pointer" class; closes a UConstrainedFieldPosition via ucfpos_close(). |
| 427 |
* For most methods see the LocalPointerBase base class. |
| 428 |
* |
| 429 |
* Usage: |
| 430 |
* |
| 431 |
* LocalUConstrainedFieldPositionPointer ucfpos(ucfpos_open(ec)); |
| 432 |
* // no need to explicitly call ucfpos_close() |
| 433 |
* |
| 434 |
* @stable ICU 64 |
| 435 |
*/ |
| 436 |
U_DEFINE_LOCAL_OPEN_POINTER(LocalUConstrainedFieldPositionPointer, |
| 437 |
UConstrainedFieldPosition, |
| 438 |
ucfpos_close); |
| 439 |
|
| 440 |
U_NAMESPACE_END |
| 441 |
#endif // U_SHOW_CPLUSPLUS_API |
| 442 |
|
| 443 |
|
| 444 |
#endif /* #if !UCONFIG_NO_FORMATTING */ |
| 445 |
#endif // __UFORMATTEDVALUE_H__ |