1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 ******************************************************************************
5 * Copyright (C) 2014-2016, International Business Machines
6 * Corporation and others. All Rights Reserved.
7 ******************************************************************************
11 #ifndef __SIMPLEFORMATTER_H__
12 #define __SIMPLEFORMATTER_H__
16 * \brief C++ API: Simple formatter, minimal subset of MessageFormat.
19 #include "unicode/utypes.h"
21 #if U_SHOW_CPLUSPLUS_API
23 #include "unicode/unistr.h"
27 // Forward declaration:
35 * Formats simple patterns like "{1} was born in {0}".
36 * Minimal subset of MessageFormat; fast, simple, minimal dependencies.
37 * Supports only numbered arguments with no type nor style parameters,
38 * and formats only string values.
39 * Quoting via ASCII apostrophe compatible with ICU MessageFormat default behavior.
41 * Factory methods set error codes for syntax errors
42 * and for too few or too many arguments/placeholders.
44 * SimpleFormatter objects are thread-safe except for assignment and applying new patterns.
48 * UErrorCode errorCode = U_ZERO_ERROR;
49 * SimpleFormatter fmt("{1} '{born}' in {0}", errorCode);
50 * UnicodeString result;
52 * // Output: "paul {born} in england"
53 * fmt.format("england", "paul", result, errorCode);
56 * This class is not intended for public subclassing.
59 * @see UMessagePatternApostropheMode
62 class U_COMMON_API SimpleFormatter U_FINAL
: public UMemory
{
65 * Default constructor.
68 SimpleFormatter() : compiledPattern((char16_t)0) {}
71 * Constructs a formatter from the pattern string.
73 * @param pattern The pattern string.
74 * @param errorCode ICU error code in/out parameter.
75 * Must fulfill U_SUCCESS before the function call.
76 * Set to U_ILLEGAL_ARGUMENT_ERROR for bad argument syntax.
79 SimpleFormatter(const UnicodeString
& pattern
, UErrorCode
&errorCode
) {
80 applyPattern(pattern
, errorCode
);
84 * Constructs a formatter from the pattern string.
85 * The number of arguments checked against the given limits is the
86 * highest argument number plus one, not the number of occurrences of arguments.
88 * @param pattern The pattern string.
89 * @param min The pattern must have at least this many arguments.
90 * @param max The pattern must have at most this many arguments.
91 * @param errorCode ICU error code in/out parameter.
92 * Must fulfill U_SUCCESS before the function call.
93 * Set to U_ILLEGAL_ARGUMENT_ERROR for bad argument syntax and
94 * too few or too many arguments.
97 SimpleFormatter(const UnicodeString
& pattern
, int32_t min
, int32_t max
,
98 UErrorCode
&errorCode
) {
99 applyPatternMinMaxArguments(pattern
, min
, max
, FALSE
, errorCode
);
102 #ifndef U_HIDE_INTERNAL_API
104 * Constructs a formatter from the pattern string.
105 * The number of arguments checked against the given limits is the
106 * highest argument number plus one, not the number of occurrences of arguments.
108 * @param pattern The pattern string.
109 * @param min The pattern must have at least this many arguments.
110 * @param max The pattern must have at most this many arguments.
111 * @param removeSingleQuotes If TRUE, single quotes are treated as syntax characters even
112 * when they don't appear adjacent to other syntax characters
113 * (The old default UMSGPAT_APOS_DOUBLE_REQUIRED behavior.)
114 * If FALSE, single quotes not adjacent to syntax characters are treated
115 * as literal text (the current default).
116 * @param errorCode ICU error code in/out parameter.
117 * Must fulfill U_SUCCESS before the function call.
118 * Set to U_ILLEGAL_ARGUMENT_ERROR for bad argument syntax and
119 * too few or too many arguments.
120 * @internal Apple only
122 SimpleFormatter(const UnicodeString
& pattern
, int32_t min
, int32_t max
,
123 UBool removeSingleQuotes
, UErrorCode
&errorCode
) {
124 applyPatternMinMaxArguments(pattern
, min
, max
, removeSingleQuotes
, errorCode
);
126 #endif /* U_HIDE_INTERNAL_API */
132 SimpleFormatter(const SimpleFormatter
& other
)
133 : compiledPattern(other
.compiledPattern
) {}
136 * Assignment operator.
139 SimpleFormatter
&operator=(const SimpleFormatter
& other
);
148 * Changes this object according to the new pattern.
150 * @param pattern The pattern string.
151 * @param errorCode ICU error code in/out parameter.
152 * Must fulfill U_SUCCESS before the function call.
153 * Set to U_ILLEGAL_ARGUMENT_ERROR for bad argument syntax.
154 * @return TRUE if U_SUCCESS(errorCode).
157 UBool
applyPattern(const UnicodeString
&pattern
, UErrorCode
&errorCode
) {
158 return applyPatternMinMaxArguments(pattern
, 0, INT32_MAX
, FALSE
, errorCode
);
162 * Changes this object according to the new pattern.
163 * The number of arguments checked against the given limits is the
164 * highest argument number plus one, not the number of occurrences of arguments.
166 * @param pattern The pattern string.
167 * @param min The pattern must have at least this many arguments.
168 * @param max The pattern must have at most this many arguments.
169 * @param errorCode ICU error code in/out parameter.
170 * Must fulfill U_SUCCESS before the function call.
171 * Set to U_ILLEGAL_ARGUMENT_ERROR for bad argument syntax and
172 * too few or too many arguments.
173 * @return TRUE if U_SUCCESS(errorCode).
176 UBool
applyPatternMinMaxArguments(const UnicodeString
&pattern
,
177 int32_t min
, int32_t max
, UErrorCode
&errorCode
) {
178 return applyPatternMinMaxArguments(pattern
, min
, max
, FALSE
, errorCode
);
181 #ifndef U_HIDE_INTERNAL_API
183 * Changes this object according to the new pattern.
184 * The number of arguments checked against the given limits is the
185 * highest argument number plus one, not the number of occurrences of arguments.
187 * @param pattern The pattern string.
188 * @param min The pattern must have at least this many arguments.
189 * @param max The pattern must have at most this many arguments.
190 * @param removeSingleQuotes If TRUE, single quotes are treated as syntax characters even
191 * when they don't appear adjacent to other syntax characters
192 * (The old default UMSGPAT_APOS_DOUBLE_REQUIRED behavior.)
193 * If FALSE, single quotes not adjacent to syntax characters are treated
194 * as literal text (the current default).
195 * @param errorCode ICU error code in/out parameter.
196 * Must fulfill U_SUCCESS before the function call.
197 * Set to U_ILLEGAL_ARGUMENT_ERROR for bad argument syntax and
198 * too few or too many arguments.
199 * @return TRUE if U_SUCCESS(errorCode).
200 * @internal Apple only
202 UBool
applyPatternMinMaxArguments(const UnicodeString
&pattern
,
203 int32_t min
, int32_t max
, UBool removeSingleQuotes
,
204 UErrorCode
&errorCode
);
205 #endif /* U_HIDE_INTERNAL_API */
208 * @return The max argument number + 1.
211 int32_t getArgumentLimit() const {
212 return getArgumentLimit(compiledPattern
.getBuffer(), compiledPattern
.length());
216 * Formats the given value, appending to the appendTo builder.
217 * The argument value must not be the same object as appendTo.
218 * getArgumentLimit() must be at most 1.
220 * @param value0 Value for argument {0}.
221 * @param appendTo Gets the formatted pattern and value appended.
222 * @param errorCode ICU error code in/out parameter.
223 * Must fulfill U_SUCCESS before the function call.
227 UnicodeString
&format(
228 const UnicodeString
&value0
,
229 UnicodeString
&appendTo
, UErrorCode
&errorCode
) const;
232 * Formats the given values, appending to the appendTo builder.
233 * An argument value must not be the same object as appendTo.
234 * getArgumentLimit() must be at most 2.
236 * @param value0 Value for argument {0}.
237 * @param value1 Value for argument {1}.
238 * @param appendTo Gets the formatted pattern and values appended.
239 * @param errorCode ICU error code in/out parameter.
240 * Must fulfill U_SUCCESS before the function call.
244 UnicodeString
&format(
245 const UnicodeString
&value0
,
246 const UnicodeString
&value1
,
247 UnicodeString
&appendTo
, UErrorCode
&errorCode
) const;
250 * Formats the given values, appending to the appendTo builder.
251 * An argument value must not be the same object as appendTo.
252 * getArgumentLimit() must be at most 3.
254 * @param value0 Value for argument {0}.
255 * @param value1 Value for argument {1}.
256 * @param value2 Value for argument {2}.
257 * @param appendTo Gets the formatted pattern and values appended.
258 * @param errorCode ICU error code in/out parameter.
259 * Must fulfill U_SUCCESS before the function call.
263 UnicodeString
&format(
264 const UnicodeString
&value0
,
265 const UnicodeString
&value1
,
266 const UnicodeString
&value2
,
267 UnicodeString
&appendTo
, UErrorCode
&errorCode
) const;
270 * Formats the given values, appending to the appendTo string.
272 * @param values The argument values.
273 * An argument value must not be the same object as appendTo.
274 * Can be NULL if valuesLength==getArgumentLimit()==0.
275 * @param valuesLength The length of the values array.
276 * Must be at least getArgumentLimit().
277 * @param appendTo Gets the formatted pattern and values appended.
278 * @param offsets offsets[i] receives the offset of where
279 * values[i] replaced pattern argument {i}.
280 * Can be shorter or longer than values. Can be NULL if offsetsLength==0.
281 * If there is no {i} in the pattern, then offsets[i] is set to -1.
282 * @param offsetsLength The length of the offsets array.
283 * @param errorCode ICU error code in/out parameter.
284 * Must fulfill U_SUCCESS before the function call.
288 UnicodeString
&formatAndAppend(
289 const UnicodeString
*const *values
, int32_t valuesLength
,
290 UnicodeString
&appendTo
,
291 int32_t *offsets
, int32_t offsetsLength
, UErrorCode
&errorCode
) const;
294 * Formats the given values, replacing the contents of the result string.
295 * May optimize by actually appending to the result if it is the same object
296 * as the value corresponding to the initial argument in the pattern.
298 * @param values The argument values.
299 * An argument value may be the same object as result.
300 * Can be NULL if valuesLength==getArgumentLimit()==0.
301 * @param valuesLength The length of the values array.
302 * Must be at least getArgumentLimit().
303 * @param result Gets its contents replaced by the formatted pattern and values.
304 * @param offsets offsets[i] receives the offset of where
305 * values[i] replaced pattern argument {i}.
306 * Can be shorter or longer than values. Can be NULL if offsetsLength==0.
307 * If there is no {i} in the pattern, then offsets[i] is set to -1.
308 * @param offsetsLength The length of the offsets array.
309 * @param errorCode ICU error code in/out parameter.
310 * Must fulfill U_SUCCESS before the function call.
314 UnicodeString
&formatAndReplace(
315 const UnicodeString
*const *values
, int32_t valuesLength
,
316 UnicodeString
&result
,
317 int32_t *offsets
, int32_t offsetsLength
, UErrorCode
&errorCode
) const;
320 * Returns the pattern text with none of the arguments.
321 * Like formatting with all-empty string values.
324 UnicodeString
getTextWithNoArguments() const {
325 return getTextWithNoArguments(
326 compiledPattern
.getBuffer(),
327 compiledPattern
.length(),
332 #ifndef U_HIDE_INTERNAL_API
334 * Returns the pattern text with none of the arguments.
335 * Like formatting with all-empty string values.
337 * TODO(ICU-20406): Replace this with an Iterator interface.
339 * @param offsets offsets[i] receives the offset of where {i} was located
340 * before it was replaced by an empty string.
341 * For example, "a{0}b{1}" produces offset 1 for i=0 and 2 for i=1.
342 * Can be nullptr if offsetsLength==0.
343 * If there is no {i} in the pattern, then offsets[i] is set to -1.
344 * @param offsetsLength The length of the offsets array.
348 UnicodeString
getTextWithNoArguments(int32_t *offsets
, int32_t offsetsLength
) const {
349 return getTextWithNoArguments(
350 compiledPattern
.getBuffer(),
351 compiledPattern
.length(),
355 #endif // U_HIDE_INTERNAL_API
359 * Binary representation of the compiled pattern.
360 * Index 0: One more than the highest argument number.
361 * Followed by zero or more arguments or literal-text segments.
363 * An argument is stored as its number, less than ARG_NUM_LIMIT.
364 * A literal-text segment is stored as its length (at least 1) offset by ARG_NUM_LIMIT,
365 * followed by that many chars.
367 UnicodeString compiledPattern
;
369 static inline int32_t getArgumentLimit(const char16_t *compiledPattern
,
370 int32_t compiledPatternLength
) {
371 return compiledPatternLength
== 0 ? 0 : compiledPattern
[0];
374 static UnicodeString
getTextWithNoArguments(
375 const char16_t *compiledPattern
,
376 int32_t compiledPatternLength
,
378 int32_t offsetsLength
);
380 static UnicodeString
&format(
381 const char16_t *compiledPattern
, int32_t compiledPatternLength
,
382 const UnicodeString
*const *values
,
383 UnicodeString
&result
, const UnicodeString
*resultCopy
, UBool forbidResultAsValue
,
384 int32_t *offsets
, int32_t offsetsLength
,
385 UErrorCode
&errorCode
);
387 // Give access to internals to SimpleModifier for number formatting
388 friend class number::impl::SimpleModifier
;
393 #endif /* U_SHOW_CPLUSPLUS_API */
395 #endif // __SIMPLEFORMATTER_H__