1 // © 2018 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 #ifndef __FORMATTEDVALUE_H__
5 #define __FORMATTEDVALUE_H__
7 #include "unicode/utypes.h"
8 #if !UCONFIG_NO_FORMATTING
9 #ifndef U_HIDE_DRAFT_API
11 #include "unicode/appendable.h"
12 #include "unicode/fpositer.h"
13 #include "unicode/unistr.h"
14 #include "unicode/uformattedvalue.h"
16 #if U_SHOW_CPLUSPLUS_API
21 * \brief C++ API: Abstract operations for localized strings.
23 * This file contains declarations for classes that deal with formatted strings. A number
24 * of APIs throughout ICU use these classes for expressing their localized output.
29 * Represents a span of a string containing a given field.
31 * This class differs from FieldPosition in the following ways:
33 * 1. It has information on the field category.
34 * 2. It allows you to set constraints to use when iterating over field positions.
35 * 3. It is used for the newer FormattedValue APIs.
37 * This class is not intended for public subclassing.
41 class U_I18N_API ConstrainedFieldPosition
: public UMemory
{
45 * Initializes a ConstrainedFieldPosition.
47 * By default, the ConstrainedFieldPosition has no iteration constraints.
51 ConstrainedFieldPosition();
54 ~ConstrainedFieldPosition();
57 * Resets this ConstrainedFieldPosition to its initial state, as if it were newly created:
59 * - Removes any constraints that may have been set on the instance.
60 * - Resets the iteration position.
67 * Sets a constraint on the field category.
69 * When this instance of ConstrainedFieldPosition is passed to FormattedValue#nextPosition,
70 * positions are skipped unless they have the given category.
72 * Any previously set constraints are cleared.
74 * For example, to loop over only the number-related fields:
76 * ConstrainedFieldPosition cfpos;
77 * cfpos.constrainCategory(UFIELDCATEGORY_NUMBER_FORMAT);
78 * while (fmtval.nextPosition(cfpos, status)) {
79 * // handle the number-related field position
82 * Changing the constraint while in the middle of iterating over a FormattedValue
83 * does not generally have well-defined behavior.
85 * @param category The field category to fix when iterating.
88 void constrainCategory(int32_t category
);
91 * Sets a constraint on the category and field.
93 * When this instance of ConstrainedFieldPosition is passed to FormattedValue#nextPosition,
94 * positions are skipped unless they have the given category and field.
96 * Any previously set constraints are cleared.
98 * For example, to loop over all grouping separators:
100 * ConstrainedFieldPosition cfpos;
101 * cfpos.constrainField(UFIELDCATEGORY_NUMBER_FORMAT, UNUM_GROUPING_SEPARATOR_FIELD);
102 * while (fmtval.nextPosition(cfpos, status)) {
103 * // handle the grouping separator position
106 * Changing the constraint while in the middle of iterating over a FormattedValue
107 * does not generally have well-defined behavior.
109 * @param category The field category to fix when iterating.
110 * @param field The field to fix when iterating.
113 void constrainField(int32_t category
, int32_t field
);
116 * Gets the field category for the current position.
118 * The return value is well-defined only after
119 * FormattedValue#nextPosition returns TRUE.
121 * @return The field category saved in the instance.
124 inline int32_t getCategory() const {
129 * Gets the field for the current position.
131 * The return value is well-defined only after
132 * FormattedValue#nextPosition returns TRUE.
134 * @return The field saved in the instance.
137 inline int32_t getField() const {
142 * Gets the INCLUSIVE start index for the current position.
144 * The return value is well-defined only after FormattedValue#nextPosition returns TRUE.
146 * @return The start index saved in the instance.
149 inline int32_t getStart() const {
154 * Gets the EXCLUSIVE end index stored for the current position.
156 * The return value is well-defined only after FormattedValue#nextPosition returns TRUE.
158 * @return The end index saved in the instance.
161 inline int32_t getLimit() const {
165 ////////////////////////////////////////////////////////////////////
166 //// The following methods are for FormattedValue implementers; ////
167 //// most users can ignore them. ////
168 ////////////////////////////////////////////////////////////////////
171 * Gets an int64 that FormattedValue implementations may use for storage.
173 * The initial value is zero.
175 * Users of FormattedValue should not need to call this method.
177 * @return The current iteration context from {@link #setInt64IterationContext}.
180 inline int64_t getInt64IterationContext() const {
185 * Sets an int64 that FormattedValue implementations may use for storage.
187 * Intended to be used by FormattedValue implementations.
189 * @param context The new iteration context.
192 void setInt64IterationContext(int64_t context
);
195 * Determines whether a given field should be included given the
198 * Intended to be used by FormattedValue implementations.
200 * @param category The category to test.
201 * @param field The field to test.
204 UBool
matchesField(int32_t category
, int32_t field
) const;
207 * Sets new values for the primary public getters.
209 * Intended to be used by FormattedValue implementations.
211 * It is up to the implementation to ensure that the user-requested
212 * constraints are satisfied. This method does not check!
214 * @param category The new field category.
215 * @param field The new field.
216 * @param start The new inclusive start index.
217 * @param limit The new exclusive end index.
227 int64_t fContext
= 0LL;
231 int32_t fCategory
= UFIELD_CATEGORY_UNDEFINED
;
232 int8_t fConstraint
= 0;
237 * An abstract formatted value: a string with associated field attributes.
238 * Many formatters format to classes implementing FormattedValue.
242 class U_I18N_API FormattedValue
/* not : public UObject because this is an interface/mixin class */ {
245 virtual ~FormattedValue();
248 * Returns the formatted string as a self-contained UnicodeString.
250 * If you need the string within the current scope only, consider #toTempString.
252 * @param status Set if an error occurs.
253 * @return a UnicodeString containing the formatted string.
257 virtual UnicodeString
toString(UErrorCode
& status
) const = 0;
260 * Returns the formatted string as a read-only alias to memory owned by the FormattedValue.
262 * The return value is valid only as long as this FormattedValue is present and unchanged in
263 * memory. If you need the string outside the current scope, consider #toString.
265 * The buffer returned by calling UnicodeString#getBuffer() on the return value is
266 * guaranteed to be NUL-terminated.
268 * @param status Set if an error occurs.
269 * @return a temporary UnicodeString containing the formatted string.
273 virtual UnicodeString
toTempString(UErrorCode
& status
) const = 0;
276 * Appends the formatted string to an Appendable.
279 * The Appendable to which to append the string output.
280 * @param status Set if an error occurs.
281 * @return The same Appendable, for chaining.
286 virtual Appendable
& appendTo(Appendable
& appendable
, UErrorCode
& status
) const = 0;
289 * Iterates over field positions in the FormattedValue. This lets you determine the position
290 * of specific types of substrings, like a month or a decimal separator.
292 * To loop over all field positions:
294 * ConstrainedFieldPosition cfpos;
295 * while (fmtval.nextPosition(cfpos, status)) {
296 * // handle the field position; get information from cfpos
300 * The object used for iteration state. This can provide constraints to iterate over
301 * only one specific category or field;
302 * see ConstrainedFieldPosition#constrainCategory
303 * and ConstrainedFieldPosition#constrainField.
304 * @param status Set if an error occurs.
305 * @return TRUE if a new occurrence of the field was found;
306 * FALSE otherwise or if an error was set.
310 virtual UBool
nextPosition(ConstrainedFieldPosition
& cfpos
, UErrorCode
& status
) const = 0;
315 #endif // U_SHOW_CPLUSPLUS_API
317 #endif /* U_HIDE_DRAFT_API */
318 #endif /* #if !UCONFIG_NO_FORMATTING */
319 #endif // __FORMATTEDVALUE_H__