annotate modules/javafx.web/src/main/native/Source/ThirdParty/icu/source/common/unicode/simpleformatter.h @ 11038:20a8447c71c6

8207159: Update ICU to version 62.1 Reviewed-by: mbilla, kcr, ghb
author arajkumar
date Fri, 24 Aug 2018 15:06:40 +0530
parents
children
rev   line source
arajkumar@11038 1 // © 2016 and later: Unicode, Inc. and others.
arajkumar@11038 2 // License & terms of use: http://www.unicode.org/copyright.html
arajkumar@11038 3 /*
arajkumar@11038 4 ******************************************************************************
arajkumar@11038 5 * Copyright (C) 2014-2016, International Business Machines
arajkumar@11038 6 * Corporation and others. All Rights Reserved.
arajkumar@11038 7 ******************************************************************************
arajkumar@11038 8 * simpleformatter.h
arajkumar@11038 9 */
arajkumar@11038 10
arajkumar@11038 11 #ifndef __SIMPLEFORMATTER_H__
arajkumar@11038 12 #define __SIMPLEFORMATTER_H__
arajkumar@11038 13
arajkumar@11038 14 /**
arajkumar@11038 15 * \file
arajkumar@11038 16 * \brief C++ API: Simple formatter, minimal subset of MessageFormat.
arajkumar@11038 17 */
arajkumar@11038 18
arajkumar@11038 19 #include "unicode/utypes.h"
arajkumar@11038 20 #include "unicode/unistr.h"
arajkumar@11038 21
arajkumar@11038 22 U_NAMESPACE_BEGIN
arajkumar@11038 23
arajkumar@11038 24 // Forward declaration:
arajkumar@11038 25 namespace number {
arajkumar@11038 26 namespace impl {
arajkumar@11038 27 class SimpleModifier;
arajkumar@11038 28 }
arajkumar@11038 29 }
arajkumar@11038 30
arajkumar@11038 31 /**
arajkumar@11038 32 * Formats simple patterns like "{1} was born in {0}".
arajkumar@11038 33 * Minimal subset of MessageFormat; fast, simple, minimal dependencies.
arajkumar@11038 34 * Supports only numbered arguments with no type nor style parameters,
arajkumar@11038 35 * and formats only string values.
arajkumar@11038 36 * Quoting via ASCII apostrophe compatible with ICU MessageFormat default behavior.
arajkumar@11038 37 *
arajkumar@11038 38 * Factory methods set error codes for syntax errors
arajkumar@11038 39 * and for too few or too many arguments/placeholders.
arajkumar@11038 40 *
arajkumar@11038 41 * SimpleFormatter objects are thread-safe except for assignment and applying new patterns.
arajkumar@11038 42 *
arajkumar@11038 43 * Example:
arajkumar@11038 44 * <pre>
arajkumar@11038 45 * UErrorCode errorCode = U_ZERO_ERROR;
arajkumar@11038 46 * SimpleFormatter fmt("{1} '{born}' in {0}", errorCode);
arajkumar@11038 47 * UnicodeString result;
arajkumar@11038 48 *
arajkumar@11038 49 * // Output: "paul {born} in england"
arajkumar@11038 50 * fmt.format("england", "paul", result, errorCode);
arajkumar@11038 51 * </pre>
arajkumar@11038 52 *
arajkumar@11038 53 * This class is not intended for public subclassing.
arajkumar@11038 54 *
arajkumar@11038 55 * @see MessageFormat
arajkumar@11038 56 * @see UMessagePatternApostropheMode
arajkumar@11038 57 * @stable ICU 57
arajkumar@11038 58 */
arajkumar@11038 59 class U_COMMON_API SimpleFormatter U_FINAL : public UMemory {
arajkumar@11038 60 public:
arajkumar@11038 61 /**
arajkumar@11038 62 * Default constructor.
arajkumar@11038 63 * @stable ICU 57
arajkumar@11038 64 */
arajkumar@11038 65 SimpleFormatter() : compiledPattern((char16_t)0) {}
arajkumar@11038 66
arajkumar@11038 67 /**
arajkumar@11038 68 * Constructs a formatter from the pattern string.
arajkumar@11038 69 *
arajkumar@11038 70 * @param pattern The pattern string.
arajkumar@11038 71 * @param errorCode ICU error code in/out parameter.
arajkumar@11038 72 * Must fulfill U_SUCCESS before the function call.
arajkumar@11038 73 * Set to U_ILLEGAL_ARGUMENT_ERROR for bad argument syntax.
arajkumar@11038 74 * @stable ICU 57
arajkumar@11038 75 */
arajkumar@11038 76 SimpleFormatter(const UnicodeString& pattern, UErrorCode &errorCode) {
arajkumar@11038 77 applyPattern(pattern, errorCode);
arajkumar@11038 78 }
arajkumar@11038 79
arajkumar@11038 80 /**
arajkumar@11038 81 * Constructs a formatter from the pattern string.
arajkumar@11038 82 * The number of arguments checked against the given limits is the
arajkumar@11038 83 * highest argument number plus one, not the number of occurrences of arguments.
arajkumar@11038 84 *
arajkumar@11038 85 * @param pattern The pattern string.
arajkumar@11038 86 * @param min The pattern must have at least this many arguments.
arajkumar@11038 87 * @param max The pattern must have at most this many arguments.
arajkumar@11038 88 * @param errorCode ICU error code in/out parameter.
arajkumar@11038 89 * Must fulfill U_SUCCESS before the function call.
arajkumar@11038 90 * Set to U_ILLEGAL_ARGUMENT_ERROR for bad argument syntax and
arajkumar@11038 91 * too few or too many arguments.
arajkumar@11038 92 * @stable ICU 57
arajkumar@11038 93 */
arajkumar@11038 94 SimpleFormatter(const UnicodeString& pattern, int32_t min, int32_t max,
arajkumar@11038 95 UErrorCode &errorCode) {
arajkumar@11038 96 applyPatternMinMaxArguments(pattern, min, max, errorCode);
arajkumar@11038 97 }
arajkumar@11038 98
arajkumar@11038 99 /**
arajkumar@11038 100 * Copy constructor.
arajkumar@11038 101 * @stable ICU 57
arajkumar@11038 102 */
arajkumar@11038 103 SimpleFormatter(const SimpleFormatter& other)
arajkumar@11038 104 : compiledPattern(other.compiledPattern) {}
arajkumar@11038 105
arajkumar@11038 106 /**
arajkumar@11038 107 * Assignment operator.
arajkumar@11038 108 * @stable ICU 57
arajkumar@11038 109 */
arajkumar@11038 110 SimpleFormatter &operator=(const SimpleFormatter& other);
arajkumar@11038 111
arajkumar@11038 112 /**
arajkumar@11038 113 * Destructor.
arajkumar@11038 114 * @stable ICU 57
arajkumar@11038 115 */
arajkumar@11038 116 ~SimpleFormatter();
arajkumar@11038 117
arajkumar@11038 118 /**
arajkumar@11038 119 * Changes this object according to the new pattern.
arajkumar@11038 120 *
arajkumar@11038 121 * @param pattern The pattern string.
arajkumar@11038 122 * @param errorCode ICU error code in/out parameter.
arajkumar@11038 123 * Must fulfill U_SUCCESS before the function call.
arajkumar@11038 124 * Set to U_ILLEGAL_ARGUMENT_ERROR for bad argument syntax.
arajkumar@11038 125 * @return TRUE if U_SUCCESS(errorCode).
arajkumar@11038 126 * @stable ICU 57
arajkumar@11038 127 */
arajkumar@11038 128 UBool applyPattern(const UnicodeString &pattern, UErrorCode &errorCode) {
arajkumar@11038 129 return applyPatternMinMaxArguments(pattern, 0, INT32_MAX, errorCode);
arajkumar@11038 130 }
arajkumar@11038 131
arajkumar@11038 132 /**
arajkumar@11038 133 * Changes this object according to the new pattern.
arajkumar@11038 134 * The number of arguments checked against the given limits is the
arajkumar@11038 135 * highest argument number plus one, not the number of occurrences of arguments.
arajkumar@11038 136 *
arajkumar@11038 137 * @param pattern The pattern string.
arajkumar@11038 138 * @param min The pattern must have at least this many arguments.
arajkumar@11038 139 * @param max The pattern must have at most this many arguments.
arajkumar@11038 140 * @param errorCode ICU error code in/out parameter.
arajkumar@11038 141 * Must fulfill U_SUCCESS before the function call.
arajkumar@11038 142 * Set to U_ILLEGAL_ARGUMENT_ERROR for bad argument syntax and
arajkumar@11038 143 * too few or too many arguments.
arajkumar@11038 144 * @return TRUE if U_SUCCESS(errorCode).
arajkumar@11038 145 * @stable ICU 57
arajkumar@11038 146 */
arajkumar@11038 147 UBool applyPatternMinMaxArguments(const UnicodeString &pattern,
arajkumar@11038 148 int32_t min, int32_t max, UErrorCode &errorCode);
arajkumar@11038 149
arajkumar@11038 150 /**
arajkumar@11038 151 * @return The max argument number + 1.
arajkumar@11038 152 * @stable ICU 57
arajkumar@11038 153 */
arajkumar@11038 154 int32_t getArgumentLimit() const {
arajkumar@11038 155 return getArgumentLimit(compiledPattern.getBuffer(), compiledPattern.length());
arajkumar@11038 156 }
arajkumar@11038 157
arajkumar@11038 158 /**
arajkumar@11038 159 * Formats the given value, appending to the appendTo builder.
arajkumar@11038 160 * The argument value must not be the same object as appendTo.
arajkumar@11038 161 * getArgumentLimit() must be at most 1.
arajkumar@11038 162 *
arajkumar@11038 163 * @param value0 Value for argument {0}.
arajkumar@11038 164 * @param appendTo Gets the formatted pattern and value appended.
arajkumar@11038 165 * @param errorCode ICU error code in/out parameter.
arajkumar@11038 166 * Must fulfill U_SUCCESS before the function call.
arajkumar@11038 167 * @return appendTo
arajkumar@11038 168 * @stable ICU 57
arajkumar@11038 169 */
arajkumar@11038 170 UnicodeString &format(
arajkumar@11038 171 const UnicodeString &value0,
arajkumar@11038 172 UnicodeString &appendTo, UErrorCode &errorCode) const;
arajkumar@11038 173
arajkumar@11038 174 /**
arajkumar@11038 175 * Formats the given values, appending to the appendTo builder.
arajkumar@11038 176 * An argument value must not be the same object as appendTo.
arajkumar@11038 177 * getArgumentLimit() must be at most 2.
arajkumar@11038 178 *
arajkumar@11038 179 * @param value0 Value for argument {0}.
arajkumar@11038 180 * @param value1 Value for argument {1}.
arajkumar@11038 181 * @param appendTo Gets the formatted pattern and values appended.
arajkumar@11038 182 * @param errorCode ICU error code in/out parameter.
arajkumar@11038 183 * Must fulfill U_SUCCESS before the function call.
arajkumar@11038 184 * @return appendTo
arajkumar@11038 185 * @stable ICU 57
arajkumar@11038 186 */
arajkumar@11038 187 UnicodeString &format(
arajkumar@11038 188 const UnicodeString &value0,
arajkumar@11038 189 const UnicodeString &value1,
arajkumar@11038 190 UnicodeString &appendTo, UErrorCode &errorCode) const;
arajkumar@11038 191
arajkumar@11038 192 /**
arajkumar@11038 193 * Formats the given values, appending to the appendTo builder.
arajkumar@11038 194 * An argument value must not be the same object as appendTo.
arajkumar@11038 195 * getArgumentLimit() must be at most 3.
arajkumar@11038 196 *
arajkumar@11038 197 * @param value0 Value for argument {0}.
arajkumar@11038 198 * @param value1 Value for argument {1}.
arajkumar@11038 199 * @param value2 Value for argument {2}.
arajkumar@11038 200 * @param appendTo Gets the formatted pattern and values appended.
arajkumar@11038 201 * @param errorCode ICU error code in/out parameter.
arajkumar@11038 202 * Must fulfill U_SUCCESS before the function call.
arajkumar@11038 203 * @return appendTo
arajkumar@11038 204 * @stable ICU 57
arajkumar@11038 205 */
arajkumar@11038 206 UnicodeString &format(
arajkumar@11038 207 const UnicodeString &value0,
arajkumar@11038 208 const UnicodeString &value1,
arajkumar@11038 209 const UnicodeString &value2,
arajkumar@11038 210 UnicodeString &appendTo, UErrorCode &errorCode) const;
arajkumar@11038 211
arajkumar@11038 212 /**
arajkumar@11038 213 * Formats the given values, appending to the appendTo string.
arajkumar@11038 214 *
arajkumar@11038 215 * @param values The argument values.
arajkumar@11038 216 * An argument value must not be the same object as appendTo.
arajkumar@11038 217 * Can be NULL if valuesLength==getArgumentLimit()==0.
arajkumar@11038 218 * @param valuesLength The length of the values array.
arajkumar@11038 219 * Must be at least getArgumentLimit().
arajkumar@11038 220 * @param appendTo Gets the formatted pattern and values appended.
arajkumar@11038 221 * @param offsets offsets[i] receives the offset of where
arajkumar@11038 222 * values[i] replaced pattern argument {i}.
arajkumar@11038 223 * Can be shorter or longer than values. Can be NULL if offsetsLength==0.
arajkumar@11038 224 * If there is no {i} in the pattern, then offsets[i] is set to -1.
arajkumar@11038 225 * @param offsetsLength The length of the offsets array.
arajkumar@11038 226 * @param errorCode ICU error code in/out parameter.
arajkumar@11038 227 * Must fulfill U_SUCCESS before the function call.
arajkumar@11038 228 * @return appendTo
arajkumar@11038 229 * @stable ICU 57
arajkumar@11038 230 */
arajkumar@11038 231 UnicodeString &formatAndAppend(
arajkumar@11038 232 const UnicodeString *const *values, int32_t valuesLength,
arajkumar@11038 233 UnicodeString &appendTo,
arajkumar@11038 234 int32_t *offsets, int32_t offsetsLength, UErrorCode &errorCode) const;
arajkumar@11038 235
arajkumar@11038 236 /**
arajkumar@11038 237 * Formats the given values, replacing the contents of the result string.
arajkumar@11038 238 * May optimize by actually appending to the result if it is the same object
arajkumar@11038 239 * as the value corresponding to the initial argument in the pattern.
arajkumar@11038 240 *
arajkumar@11038 241 * @param values The argument values.
arajkumar@11038 242 * An argument value may be the same object as result.
arajkumar@11038 243 * Can be NULL if valuesLength==getArgumentLimit()==0.
arajkumar@11038 244 * @param valuesLength The length of the values array.
arajkumar@11038 245 * Must be at least getArgumentLimit().
arajkumar@11038 246 * @param result Gets its contents replaced by the formatted pattern and values.
arajkumar@11038 247 * @param offsets offsets[i] receives the offset of where
arajkumar@11038 248 * values[i] replaced pattern argument {i}.
arajkumar@11038 249 * Can be shorter or longer than values. Can be NULL if offsetsLength==0.
arajkumar@11038 250 * If there is no {i} in the pattern, then offsets[i] is set to -1.
arajkumar@11038 251 * @param offsetsLength The length of the offsets array.
arajkumar@11038 252 * @param errorCode ICU error code in/out parameter.
arajkumar@11038 253 * Must fulfill U_SUCCESS before the function call.
arajkumar@11038 254 * @return result
arajkumar@11038 255 * @stable ICU 57
arajkumar@11038 256 */
arajkumar@11038 257 UnicodeString &formatAndReplace(
arajkumar@11038 258 const UnicodeString *const *values, int32_t valuesLength,
arajkumar@11038 259 UnicodeString &result,
arajkumar@11038 260 int32_t *offsets, int32_t offsetsLength, UErrorCode &errorCode) const;
arajkumar@11038 261
arajkumar@11038 262 /**
arajkumar@11038 263 * Returns the pattern text with none of the arguments.
arajkumar@11038 264 * Like formatting with all-empty string values.
arajkumar@11038 265 * @stable ICU 57
arajkumar@11038 266 */
arajkumar@11038 267 UnicodeString getTextWithNoArguments() const {
arajkumar@11038 268 return getTextWithNoArguments(compiledPattern.getBuffer(), compiledPattern.length());
arajkumar@11038 269 }
arajkumar@11038 270
arajkumar@11038 271 private:
arajkumar@11038 272 /**
arajkumar@11038 273 * Binary representation of the compiled pattern.
arajkumar@11038 274 * Index 0: One more than the highest argument number.
arajkumar@11038 275 * Followed by zero or more arguments or literal-text segments.
arajkumar@11038 276 *
arajkumar@11038 277 * An argument is stored as its number, less than ARG_NUM_LIMIT.
arajkumar@11038 278 * A literal-text segment is stored as its length (at least 1) offset by ARG_NUM_LIMIT,
arajkumar@11038 279 * followed by that many chars.
arajkumar@11038 280 */
arajkumar@11038 281 UnicodeString compiledPattern;
arajkumar@11038 282
arajkumar@11038 283 static inline int32_t getArgumentLimit(const char16_t *compiledPattern,
arajkumar@11038 284 int32_t compiledPatternLength) {
arajkumar@11038 285 return compiledPatternLength == 0 ? 0 : compiledPattern[0];
arajkumar@11038 286 }
arajkumar@11038 287
arajkumar@11038 288 static UnicodeString getTextWithNoArguments(const char16_t *compiledPattern, int32_t compiledPatternLength);
arajkumar@11038 289
arajkumar@11038 290 static UnicodeString &format(
arajkumar@11038 291 const char16_t *compiledPattern, int32_t compiledPatternLength,
arajkumar@11038 292 const UnicodeString *const *values,
arajkumar@11038 293 UnicodeString &result, const UnicodeString *resultCopy, UBool forbidResultAsValue,
arajkumar@11038 294 int32_t *offsets, int32_t offsetsLength,
arajkumar@11038 295 UErrorCode &errorCode);
arajkumar@11038 296
arajkumar@11038 297 // Give access to internals to SimpleModifier for number formatting
arajkumar@11038 298 friend class number::impl::SimpleModifier;
arajkumar@11038 299 };
arajkumar@11038 300
arajkumar@11038 301 U_NAMESPACE_END
arajkumar@11038 302
arajkumar@11038 303 #endif // __SIMPLEFORMATTER_H__