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"
9 #if U_SHOW_CPLUSPLUS_API
11 #if !UCONFIG_NO_FORMATTING
13 #include "unicode/appendable.h"
14 #include "unicode/fpositer.h"
15 #include "unicode/unistr.h"
16 #include "unicode/uformattedvalue.h"
22 * \brief C++ API: Abstract operations for localized strings.
24 * This file contains declarations for classes that deal with formatted strings. A number
25 * of APIs throughout ICU use these classes for expressing their localized output.
29 // The following cannot have #ifndef U_HIDE_DRAFT_API because
30 // class FormattedValue depends on it, and FormattedValue cannot be
31 // hidden becauseclass FormattedNumber (stable ICU 60) depends on it.
32 #ifndef U_FORCE_HIDE_DRAFT_API
34 * Represents a span of a string containing a given field.
36 * This class differs from FieldPosition in the following ways:
38 * 1. It has information on the field category.
39 * 2. It allows you to set constraints to use when iterating over field positions.
40 * 3. It is used for the newer FormattedValue APIs.
42 * This class is not intended for public subclassing.
46 class U_I18N_API ConstrainedFieldPosition
: public UMemory
{
50 * Initializes a ConstrainedFieldPosition.
52 * By default, the ConstrainedFieldPosition has no iteration constraints.
56 ConstrainedFieldPosition();
59 ~ConstrainedFieldPosition();
61 #ifndef U_HIDE_DRAFT_API
63 * Resets this ConstrainedFieldPosition to its initial state, as if it were newly created:
65 * - Removes any constraints that may have been set on the instance.
66 * - Resets the iteration position.
73 * Sets a constraint on the field category.
75 * When this instance of ConstrainedFieldPosition is passed to FormattedValue#nextPosition,
76 * positions are skipped unless they have the given category.
78 * Any previously set constraints are cleared.
80 * For example, to loop over only the number-related fields:
82 * ConstrainedFieldPosition cfpos;
83 * cfpos.constrainCategory(UFIELDCATEGORY_NUMBER_FORMAT);
84 * while (fmtval.nextPosition(cfpos, status)) {
85 * // handle the number-related field position
88 * Changing the constraint while in the middle of iterating over a FormattedValue
89 * does not generally have well-defined behavior.
91 * @param category The field category to fix when iterating.
94 void constrainCategory(int32_t category
);
97 * Sets a constraint on the category and field.
99 * When this instance of ConstrainedFieldPosition is passed to FormattedValue#nextPosition,
100 * positions are skipped unless they have the given category and field.
102 * Any previously set constraints are cleared.
104 * For example, to loop over all grouping separators:
106 * ConstrainedFieldPosition cfpos;
107 * cfpos.constrainField(UFIELDCATEGORY_NUMBER_FORMAT, UNUM_GROUPING_SEPARATOR_FIELD);
108 * while (fmtval.nextPosition(cfpos, status)) {
109 * // handle the grouping separator position
112 * Changing the constraint while in the middle of iterating over a FormattedValue
113 * does not generally have well-defined behavior.
115 * @param category The field category to fix when iterating.
116 * @param field The field to fix when iterating.
119 void constrainField(int32_t category
, int32_t field
);
122 * Gets the field category for the current position.
124 * The return value is well-defined only after
125 * FormattedValue#nextPosition returns TRUE.
127 * @return The field category saved in the instance.
130 inline int32_t getCategory() const {
135 * Gets the field for the current position.
137 * The return value is well-defined only after
138 * FormattedValue#nextPosition returns TRUE.
140 * @return The field saved in the instance.
143 inline int32_t getField() const {
148 * Gets the INCLUSIVE start index for the current position.
150 * The return value is well-defined only after FormattedValue#nextPosition returns TRUE.
152 * @return The start index saved in the instance.
155 inline int32_t getStart() const {
160 * Gets the EXCLUSIVE end index stored for the current position.
162 * The return value is well-defined only after FormattedValue#nextPosition returns TRUE.
164 * @return The end index saved in the instance.
167 inline int32_t getLimit() const {
171 ////////////////////////////////////////////////////////////////////
172 //// The following methods are for FormattedValue implementers; ////
173 //// most users can ignore them. ////
174 ////////////////////////////////////////////////////////////////////
177 * Gets an int64 that FormattedValue implementations may use for storage.
179 * The initial value is zero.
181 * Users of FormattedValue should not need to call this method.
183 * @return The current iteration context from {@link #setInt64IterationContext}.
186 inline int64_t getInt64IterationContext() const {
191 * Sets an int64 that FormattedValue implementations may use for storage.
193 * Intended to be used by FormattedValue implementations.
195 * @param context The new iteration context.
198 void setInt64IterationContext(int64_t context
);
201 * Determines whether a given field should be included given the
204 * Intended to be used by FormattedValue implementations.
206 * @param category The category to test.
207 * @param field The field to test.
210 UBool
matchesField(int32_t category
, int32_t field
) const;
213 * Sets new values for the primary public getters.
215 * Intended to be used by FormattedValue implementations.
217 * It is up to the implementation to ensure that the user-requested
218 * constraints are satisfied. This method does not check!
220 * @param category The new field category.
221 * @param field The new field.
222 * @param start The new inclusive start index.
223 * @param limit The new exclusive end index.
231 #endif /* U_HIDE_DRAFT_API */
234 int64_t fContext
= 0LL;
238 #ifndef U_HIDE_DRAFT_API
239 int32_t fCategory
= UFIELD_CATEGORY_UNDEFINED
;
240 #else /* U_HIDE_DRAFT_API */
241 int32_t fCategory
= 0;
242 #endif /* U_HIDE_DRAFT_API */
243 int8_t fConstraint
= 0;
246 // The following cannot have #ifndef U_HIDE_DRAFT_API because
247 // class FormattedNumber (stable ICU 60) depends on it.
249 * An abstract formatted value: a string with associated field attributes.
250 * Many formatters format to classes implementing FormattedValue.
254 class U_I18N_API FormattedValue
/* not : public UObject because this is an interface/mixin class */ {
257 virtual ~FormattedValue();
260 * Returns the formatted string as a self-contained UnicodeString.
262 * If you need the string within the current scope only, consider #toTempString.
264 * @param status Set if an error occurs.
265 * @return a UnicodeString containing the formatted string.
269 virtual UnicodeString
toString(UErrorCode
& status
) const = 0;
272 * Returns the formatted string as a read-only alias to memory owned by the FormattedValue.
274 * The return value is valid only as long as this FormattedValue is present and unchanged in
275 * memory. If you need the string outside the current scope, consider #toString.
277 * The buffer returned by calling UnicodeString#getBuffer() on the return value is
278 * guaranteed to be NUL-terminated.
280 * @param status Set if an error occurs.
281 * @return a temporary UnicodeString containing the formatted string.
285 virtual UnicodeString
toTempString(UErrorCode
& status
) const = 0;
288 * Appends the formatted string to an Appendable.
291 * The Appendable to which to append the string output.
292 * @param status Set if an error occurs.
293 * @return The same Appendable, for chaining.
298 virtual Appendable
& appendTo(Appendable
& appendable
, UErrorCode
& status
) const = 0;
301 * Iterates over field positions in the FormattedValue. This lets you determine the position
302 * of specific types of substrings, like a month or a decimal separator.
304 * To loop over all field positions:
306 * ConstrainedFieldPosition cfpos;
307 * while (fmtval.nextPosition(cfpos, status)) {
308 * // handle the field position; get information from cfpos
312 * The object used for iteration state. This can provide constraints to iterate over
313 * only one specific category or field;
314 * see ConstrainedFieldPosition#constrainCategory
315 * and ConstrainedFieldPosition#constrainField.
316 * @param status Set if an error occurs.
317 * @return TRUE if a new occurrence of the field was found;
318 * FALSE otherwise or if an error was set.
322 virtual UBool
nextPosition(ConstrainedFieldPosition
& cfpos
, UErrorCode
& status
) const = 0;
324 #endif // U_FORCE_HIDE_DRAFT_API
328 #endif /* #if !UCONFIG_NO_FORMATTING */
330 #endif /* U_SHOW_CPLUSPLUS_API */
332 #endif // __FORMATTEDVALUE_H__