Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/unicode/uformattedvalue.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 // © 2018 and later: Unicode, Inc. and others.
0002 // License & terms of use: http://www.unicode.org/copyright.html
0003 
0004 #ifndef __UFORMATTEDVALUE_H__
0005 #define __UFORMATTEDVALUE_H__
0006 
0007 #include "unicode/utypes.h"
0008 
0009 #if !UCONFIG_NO_FORMATTING
0010 
0011 #include "unicode/ufieldpositer.h"
0012 
0013 /**
0014  * \file
0015  * \brief C API: Abstract operations for localized strings.
0016  * 
0017  * This file contains declarations for classes that deal with formatted strings. A number
0018  * of APIs throughout ICU use these classes for expressing their localized output.
0019  */
0020 
0021 
0022 /**
0023  * All possible field categories in ICU. Every entry in this enum corresponds
0024  * to another enum that exists in ICU.
0025  * 
0026  * In the APIs that take a UFieldCategory, an int32_t type is used. Field
0027  * categories having any of the top four bits turned on are reserved as
0028  * private-use for external APIs implementing FormattedValue. This means that
0029  * categories 2^28 and higher or below zero (with the highest bit turned on)
0030  * are private-use and will not be used by ICU in the future.
0031  *
0032  * @stable ICU 64
0033  */
0034 typedef enum UFieldCategory {
0035     /**
0036      * For an undefined field category.
0037      * 
0038      * @stable ICU 64
0039      */
0040     UFIELD_CATEGORY_UNDEFINED = 0,
0041 
0042     /**
0043      * For fields in UDateFormatField (udat.h), from ICU 3.0.
0044      *
0045      * @stable ICU 64
0046      */
0047     UFIELD_CATEGORY_DATE,
0048 
0049     /**
0050      * For fields in UNumberFormatFields (unum.h), from ICU 49.
0051      *
0052      * @stable ICU 64
0053      */
0054     UFIELD_CATEGORY_NUMBER,
0055 
0056     /**
0057      * For fields in UListFormatterField (ulistformatter.h), from ICU 63.
0058      *
0059      * @stable ICU 64
0060      */
0061     UFIELD_CATEGORY_LIST,
0062 
0063     /**
0064      * For fields in URelativeDateTimeFormatterField (ureldatefmt.h), from ICU 64.
0065      *
0066      * @stable ICU 64
0067      */
0068     UFIELD_CATEGORY_RELATIVE_DATETIME,
0069 
0070     /**
0071      * Reserved for possible future fields in UDateIntervalFormatField.
0072      *
0073      * @internal
0074      */
0075     UFIELD_CATEGORY_DATE_INTERVAL,
0076 
0077 #ifndef U_HIDE_INTERNAL_API
0078     /** @internal */
0079     UFIELD_CATEGORY_COUNT,
0080 #endif  /* U_HIDE_INTERNAL_API */
0081 
0082     /**
0083      * Category for spans in a list.
0084      *
0085      * @stable ICU 64
0086      */
0087     UFIELD_CATEGORY_LIST_SPAN = 0x1000 + UFIELD_CATEGORY_LIST,
0088 
0089     /**
0090      * Category for spans in a date interval.
0091      *
0092      * @stable ICU 64
0093      */
0094     UFIELD_CATEGORY_DATE_INTERVAL_SPAN = 0x1000 + UFIELD_CATEGORY_DATE_INTERVAL,
0095 
0096     /**
0097      * Category for spans in a number range.
0098      *
0099      * @stable ICU 69
0100      */
0101     UFIELD_CATEGORY_NUMBER_RANGE_SPAN = 0x1000 + UFIELD_CATEGORY_NUMBER,
0102 
0103 } UFieldCategory;
0104 
0105 
0106 struct UConstrainedFieldPosition;
0107 /**
0108  * Represents a span of a string containing a given field.
0109  *
0110  * This struct differs from UFieldPosition in the following ways:
0111  *
0112  *   1. It has information on the field category.
0113  *   2. It allows you to set constraints to use when iterating over field positions.
0114  *   3. It is used for the newer FormattedValue APIs.
0115  *
0116  * @stable ICU 64
0117  */
0118 typedef struct UConstrainedFieldPosition UConstrainedFieldPosition;
0119 
0120 
0121 /**
0122  * Creates a new UConstrainedFieldPosition.
0123  *
0124  * By default, the UConstrainedFieldPosition has no iteration constraints.
0125  *
0126  * @param ec Set if an error occurs.
0127  * @return The new object, or NULL if an error occurs.
0128  * @stable ICU 64
0129  */
0130 U_CAPI UConstrainedFieldPosition* U_EXPORT2
0131 ucfpos_open(UErrorCode* ec);
0132 
0133 
0134 /**
0135  * Resets a UConstrainedFieldPosition to its initial state, as if it were newly created.
0136  *
0137  * Removes any constraints that may have been set on the instance.
0138  *
0139  * @param ucfpos The instance of UConstrainedFieldPosition.
0140  * @param ec Set if an error occurs.
0141  * @stable ICU 64
0142  */
0143 U_CAPI void U_EXPORT2
0144 ucfpos_reset(
0145     UConstrainedFieldPosition* ucfpos,
0146     UErrorCode* ec);
0147 
0148 
0149 /**
0150  * Destroys a UConstrainedFieldPosition and releases its memory.
0151  *
0152  * @param ucfpos The instance of UConstrainedFieldPosition.
0153  * @stable ICU 64
0154  */
0155 U_CAPI void U_EXPORT2
0156 ucfpos_close(UConstrainedFieldPosition* ucfpos);
0157 
0158 
0159 /**
0160  * Sets a constraint on the field category.
0161  * 
0162  * When this instance of UConstrainedFieldPosition is passed to ufmtval_nextPosition,
0163  * positions are skipped unless they have the given category.
0164  *
0165  * Any previously set constraints are cleared.
0166  *
0167  * For example, to loop over only the number-related fields:
0168  *
0169  *     UConstrainedFieldPosition* ucfpos = ucfpos_open(ec);
0170  *     ucfpos_constrainCategory(ucfpos, UFIELDCATEGORY_NUMBER_FORMAT, ec);
0171  *     while (ufmtval_nextPosition(ufmtval, ucfpos, ec)) {
0172  *         // handle the number-related field position
0173  *     }
0174  *     ucfpos_close(ucfpos);
0175  *
0176  * Changing the constraint while in the middle of iterating over a FormattedValue
0177  * does not generally have well-defined behavior.
0178  *
0179  * @param ucfpos The instance of UConstrainedFieldPosition.
0180  * @param category The field category to fix when iterating.
0181  * @param ec Set if an error occurs.
0182  * @stable ICU 64
0183  */
0184 U_CAPI void U_EXPORT2
0185 ucfpos_constrainCategory(
0186     UConstrainedFieldPosition* ucfpos,
0187     int32_t category,
0188     UErrorCode* ec);
0189 
0190 
0191 /**
0192  * Sets a constraint on the category and field.
0193  * 
0194  * When this instance of UConstrainedFieldPosition is passed to ufmtval_nextPosition,
0195  * positions are skipped unless they have the given category and field.
0196  *
0197  * Any previously set constraints are cleared.
0198  *
0199  * For example, to loop over all grouping separators:
0200  *
0201  *     UConstrainedFieldPosition* ucfpos = ucfpos_open(ec);
0202  *     ucfpos_constrainField(ucfpos, UFIELDCATEGORY_NUMBER_FORMAT, UNUM_GROUPING_SEPARATOR_FIELD, ec);
0203  *     while (ufmtval_nextPosition(ufmtval, ucfpos, ec)) {
0204  *         // handle the grouping separator position
0205  *     }
0206  *     ucfpos_close(ucfpos);
0207  *
0208  * Changing the constraint while in the middle of iterating over a FormattedValue
0209  * does not generally have well-defined behavior.
0210  *
0211  * @param ucfpos The instance of UConstrainedFieldPosition.
0212  * @param category The field category to fix when iterating.
0213  * @param field The field to fix when iterating.
0214  * @param ec Set if an error occurs.
0215  * @stable ICU 64
0216  */
0217 U_CAPI void U_EXPORT2
0218 ucfpos_constrainField(
0219     UConstrainedFieldPosition* ucfpos,
0220     int32_t category,
0221     int32_t field,
0222     UErrorCode* ec);
0223 
0224 
0225 /**
0226  * Gets the field category for the current position.
0227  *
0228  * If a category or field constraint was set, this function returns the constrained
0229  * category. Otherwise, the return value is well-defined only after
0230  * ufmtval_nextPosition returns true.
0231  *
0232  * @param ucfpos The instance of UConstrainedFieldPosition.
0233  * @param ec Set if an error occurs.
0234  * @return The field category saved in the instance.
0235  * @stable ICU 64
0236  */
0237 U_CAPI int32_t U_EXPORT2
0238 ucfpos_getCategory(
0239     const UConstrainedFieldPosition* ucfpos,
0240     UErrorCode* ec);
0241 
0242 
0243 /**
0244  * Gets the field for the current position.
0245  *
0246  * If a field constraint was set, this function returns the constrained
0247  * field. Otherwise, the return value is well-defined only after
0248  * ufmtval_nextPosition returns true.
0249  *
0250  * @param ucfpos The instance of UConstrainedFieldPosition.
0251  * @param ec Set if an error occurs.
0252  * @return The field saved in the instance.
0253  * @stable ICU 64
0254  */
0255 U_CAPI int32_t U_EXPORT2
0256 ucfpos_getField(
0257     const UConstrainedFieldPosition* ucfpos,
0258     UErrorCode* ec);
0259 
0260 
0261 /**
0262  * Gets the INCLUSIVE start and EXCLUSIVE end index stored for the current position.
0263  *
0264  * The output values are well-defined only after ufmtval_nextPosition returns true.
0265  *
0266  * @param ucfpos The instance of UConstrainedFieldPosition.
0267  * @param pStart Set to the start index saved in the instance. Ignored if nullptr.
0268  * @param pLimit Set to the end index saved in the instance. Ignored if nullptr.
0269  * @param ec Set if an error occurs.
0270  * @stable ICU 64
0271  */
0272 U_CAPI void U_EXPORT2
0273 ucfpos_getIndexes(
0274     const UConstrainedFieldPosition* ucfpos,
0275     int32_t* pStart,
0276     int32_t* pLimit,
0277     UErrorCode* ec);
0278 
0279 
0280 /**
0281  * Gets an int64 that FormattedValue implementations may use for storage.
0282  *
0283  * The initial value is zero.
0284  *
0285  * Users of FormattedValue should not need to call this method.
0286  *
0287  * @param ucfpos The instance of UConstrainedFieldPosition.
0288  * @param ec Set if an error occurs.
0289  * @return The current iteration context from ucfpos_setInt64IterationContext.
0290  * @stable ICU 64
0291  */
0292 U_CAPI int64_t U_EXPORT2
0293 ucfpos_getInt64IterationContext(
0294     const UConstrainedFieldPosition* ucfpos,
0295     UErrorCode* ec);
0296 
0297 
0298 /**
0299  * Sets an int64 that FormattedValue implementations may use for storage.
0300  *
0301  * Intended to be used by FormattedValue implementations.
0302  *
0303  * @param ucfpos The instance of UConstrainedFieldPosition.
0304  * @param context The new iteration context.
0305  * @param ec Set if an error occurs.
0306  * @stable ICU 64
0307  */
0308 U_CAPI void U_EXPORT2
0309 ucfpos_setInt64IterationContext(
0310     UConstrainedFieldPosition* ucfpos,
0311     int64_t context,
0312     UErrorCode* ec);
0313 
0314 
0315 /**
0316  * Determines whether a given field should be included given the
0317  * constraints.
0318  *
0319  * Intended to be used by FormattedValue implementations.
0320  *
0321  * @param ucfpos The instance of UConstrainedFieldPosition.
0322  * @param category The category to test.
0323  * @param field The field to test.
0324  * @param ec Set if an error occurs.
0325  * @stable ICU 64
0326  */
0327 U_CAPI UBool U_EXPORT2
0328 ucfpos_matchesField(
0329     const UConstrainedFieldPosition* ucfpos,
0330     int32_t category,
0331     int32_t field,
0332     UErrorCode* ec);
0333 
0334 
0335 /**
0336  * Sets new values for the primary public getters.
0337  *
0338  * Intended to be used by FormattedValue implementations.
0339  *
0340  * It is up to the implementation to ensure that the user-requested
0341  * constraints are satisfied. This method does not check!
0342  *
0343  * @param ucfpos The instance of UConstrainedFieldPosition.
0344  * @param category The new field category.
0345  * @param field The new field.
0346  * @param start The new inclusive start index.
0347  * @param limit The new exclusive end index.
0348  * @param ec Set if an error occurs.
0349  * @stable ICU 64
0350  */
0351 U_CAPI void U_EXPORT2
0352 ucfpos_setState(
0353     UConstrainedFieldPosition* ucfpos,
0354     int32_t category,
0355     int32_t field,
0356     int32_t start,
0357     int32_t limit,
0358     UErrorCode* ec);
0359 
0360 
0361 struct UFormattedValue;
0362 /**
0363  * An abstract formatted value: a string with associated field attributes.
0364  * Many formatters format to types compatible with UFormattedValue.
0365  *
0366  * @stable ICU 64
0367  */
0368 typedef struct UFormattedValue UFormattedValue;
0369 
0370 
0371 /**
0372  * Returns a pointer to the formatted string. The pointer is owned by the UFormattedValue. The
0373  * return value is valid only as long as the UFormattedValue is present and unchanged in memory.
0374  *
0375  * The return value is NUL-terminated but could contain internal NULs.
0376  *
0377  * @param ufmtval
0378  *         The object containing the formatted string and attributes.
0379  * @param pLength Output variable for the length of the string. Ignored if NULL.
0380  * @param ec Set if an error occurs.
0381  * @return A NUL-terminated char16 string owned by the UFormattedValue.
0382  * @stable ICU 64
0383  */
0384 U_CAPI const UChar* U_EXPORT2
0385 ufmtval_getString(
0386     const UFormattedValue* ufmtval,
0387     int32_t* pLength,
0388     UErrorCode* ec);
0389 
0390 
0391 /**
0392  * Iterates over field positions in the UFormattedValue. This lets you determine the position
0393  * of specific types of substrings, like a month or a decimal separator.
0394  *
0395  * To loop over all field positions:
0396  *
0397  *     UConstrainedFieldPosition* ucfpos = ucfpos_open(ec);
0398  *     while (ufmtval_nextPosition(ufmtval, ucfpos, ec)) {
0399  *         // handle the field position; get information from ucfpos
0400  *     }
0401  *     ucfpos_close(ucfpos);
0402  *
0403  * @param ufmtval
0404  *         The object containing the formatted string and attributes.
0405  * @param ucfpos
0406  *         The object used for iteration state; can provide constraints to iterate over only
0407  *         one specific category or field;
0408  *         see ucfpos_constrainCategory
0409  *         and ucfpos_constrainField.
0410  * @param ec Set if an error occurs.
0411  * @return true if another position was found; false otherwise.
0412  * @stable ICU 64
0413  */
0414 U_CAPI UBool U_EXPORT2
0415 ufmtval_nextPosition(
0416     const UFormattedValue* ufmtval,
0417     UConstrainedFieldPosition* ucfpos,
0418     UErrorCode* ec);
0419 
0420 
0421 #if U_SHOW_CPLUSPLUS_API
0422 U_NAMESPACE_BEGIN
0423 
0424 /**
0425  * \class LocalUConstrainedFieldPositionPointer
0426  * "Smart pointer" class; closes a UConstrainedFieldPosition via ucfpos_close().
0427  * For most methods see the LocalPointerBase base class.
0428  *
0429  * Usage:
0430  *
0431  *     LocalUConstrainedFieldPositionPointer ucfpos(ucfpos_open(ec));
0432  *     // no need to explicitly call ucfpos_close()
0433  *
0434  * @stable ICU 64
0435  */
0436 U_DEFINE_LOCAL_OPEN_POINTER(LocalUConstrainedFieldPositionPointer,
0437     UConstrainedFieldPosition,
0438     ucfpos_close);
0439 
0440 U_NAMESPACE_END
0441 #endif // U_SHOW_CPLUSPLUS_API
0442 
0443 
0444 #endif /* #if !UCONFIG_NO_FORMATTING */
0445 #endif // __UFORMATTEDVALUE_H__