Path: blob/jdk8u272-b10-aarch32-20201026/jdk/src/share/native/common/unicode/simpleformatter.h
48773 views
// © 2016 and later: Unicode, Inc. and others.1// License & terms of use: http://www.unicode.org/copyright.html2/*3******************************************************************************4* Copyright (C) 2014-2016, International Business Machines5* Corporation and others. All Rights Reserved.6******************************************************************************7* simpleformatter.h8*/910#ifndef __SIMPLEFORMATTER_H__11#define __SIMPLEFORMATTER_H__1213/**14* \file15* \brief C++ API: Simple formatter, minimal subset of MessageFormat.16*/1718#include "unicode/utypes.h"19#include "unicode/unistr.h"2021U_NAMESPACE_BEGIN2223// Forward declaration:24namespace number {25namespace impl {26class SimpleModifier;27}28}2930/**31* Formats simple patterns like "{1} was born in {0}".32* Minimal subset of MessageFormat; fast, simple, minimal dependencies.33* Supports only numbered arguments with no type nor style parameters,34* and formats only string values.35* Quoting via ASCII apostrophe compatible with ICU MessageFormat default behavior.36*37* Factory methods set error codes for syntax errors38* and for too few or too many arguments/placeholders.39*40* SimpleFormatter objects are thread-safe except for assignment and applying new patterns.41*42* Example:43* <pre>44* UErrorCode errorCode = U_ZERO_ERROR;45* SimpleFormatter fmt("{1} '{born}' in {0}", errorCode);46* UnicodeString result;47*48* // Output: "paul {born} in england"49* fmt.format("england", "paul", result, errorCode);50* </pre>51*52* This class is not intended for public subclassing.53*54* @see MessageFormat55* @see UMessagePatternApostropheMode56* @stable ICU 5757*/58class U_COMMON_API SimpleFormatter U_FINAL : public UMemory {59public:60/**61* Default constructor.62* @stable ICU 5763*/64SimpleFormatter() : compiledPattern((char16_t)0) {}6566/**67* Constructs a formatter from the pattern string.68*69* @param pattern The pattern string.70* @param errorCode ICU error code in/out parameter.71* Must fulfill U_SUCCESS before the function call.72* Set to U_ILLEGAL_ARGUMENT_ERROR for bad argument syntax.73* @stable ICU 5774*/75SimpleFormatter(const UnicodeString& pattern, UErrorCode &errorCode) {76applyPattern(pattern, errorCode);77}7879/**80* Constructs a formatter from the pattern string.81* The number of arguments checked against the given limits is the82* highest argument number plus one, not the number of occurrences of arguments.83*84* @param pattern The pattern string.85* @param min The pattern must have at least this many arguments.86* @param max The pattern must have at most this many arguments.87* @param errorCode ICU error code in/out parameter.88* Must fulfill U_SUCCESS before the function call.89* Set to U_ILLEGAL_ARGUMENT_ERROR for bad argument syntax and90* too few or too many arguments.91* @stable ICU 5792*/93SimpleFormatter(const UnicodeString& pattern, int32_t min, int32_t max,94UErrorCode &errorCode) {95applyPatternMinMaxArguments(pattern, min, max, errorCode);96}9798/**99* Copy constructor.100* @stable ICU 57101*/102SimpleFormatter(const SimpleFormatter& other)103: compiledPattern(other.compiledPattern) {}104105/**106* Assignment operator.107* @stable ICU 57108*/109SimpleFormatter &operator=(const SimpleFormatter& other);110111/**112* Destructor.113* @stable ICU 57114*/115~SimpleFormatter();116117/**118* Changes this object according to the new pattern.119*120* @param pattern The pattern string.121* @param errorCode ICU error code in/out parameter.122* Must fulfill U_SUCCESS before the function call.123* Set to U_ILLEGAL_ARGUMENT_ERROR for bad argument syntax.124* @return TRUE if U_SUCCESS(errorCode).125* @stable ICU 57126*/127UBool applyPattern(const UnicodeString &pattern, UErrorCode &errorCode) {128return applyPatternMinMaxArguments(pattern, 0, INT32_MAX, errorCode);129}130131/**132* Changes this object according to the new pattern.133* The number of arguments checked against the given limits is the134* highest argument number plus one, not the number of occurrences of arguments.135*136* @param pattern The pattern string.137* @param min The pattern must have at least this many arguments.138* @param max The pattern must have at most this many arguments.139* @param errorCode ICU error code in/out parameter.140* Must fulfill U_SUCCESS before the function call.141* Set to U_ILLEGAL_ARGUMENT_ERROR for bad argument syntax and142* too few or too many arguments.143* @return TRUE if U_SUCCESS(errorCode).144* @stable ICU 57145*/146UBool applyPatternMinMaxArguments(const UnicodeString &pattern,147int32_t min, int32_t max, UErrorCode &errorCode);148149/**150* @return The max argument number + 1.151* @stable ICU 57152*/153int32_t getArgumentLimit() const {154return getArgumentLimit(compiledPattern.getBuffer(), compiledPattern.length());155}156157/**158* Formats the given value, appending to the appendTo builder.159* The argument value must not be the same object as appendTo.160* getArgumentLimit() must be at most 1.161*162* @param value0 Value for argument {0}.163* @param appendTo Gets the formatted pattern and value appended.164* @param errorCode ICU error code in/out parameter.165* Must fulfill U_SUCCESS before the function call.166* @return appendTo167* @stable ICU 57168*/169UnicodeString &format(170const UnicodeString &value0,171UnicodeString &appendTo, UErrorCode &errorCode) const;172173/**174* Formats the given values, appending to the appendTo builder.175* An argument value must not be the same object as appendTo.176* getArgumentLimit() must be at most 2.177*178* @param value0 Value for argument {0}.179* @param value1 Value for argument {1}.180* @param appendTo Gets the formatted pattern and values appended.181* @param errorCode ICU error code in/out parameter.182* Must fulfill U_SUCCESS before the function call.183* @return appendTo184* @stable ICU 57185*/186UnicodeString &format(187const UnicodeString &value0,188const UnicodeString &value1,189UnicodeString &appendTo, UErrorCode &errorCode) const;190191/**192* Formats the given values, appending to the appendTo builder.193* An argument value must not be the same object as appendTo.194* getArgumentLimit() must be at most 3.195*196* @param value0 Value for argument {0}.197* @param value1 Value for argument {1}.198* @param value2 Value for argument {2}.199* @param appendTo Gets the formatted pattern and values appended.200* @param errorCode ICU error code in/out parameter.201* Must fulfill U_SUCCESS before the function call.202* @return appendTo203* @stable ICU 57204*/205UnicodeString &format(206const UnicodeString &value0,207const UnicodeString &value1,208const UnicodeString &value2,209UnicodeString &appendTo, UErrorCode &errorCode) const;210211/**212* Formats the given values, appending to the appendTo string.213*214* @param values The argument values.215* An argument value must not be the same object as appendTo.216* Can be NULL if valuesLength==getArgumentLimit()==0.217* @param valuesLength The length of the values array.218* Must be at least getArgumentLimit().219* @param appendTo Gets the formatted pattern and values appended.220* @param offsets offsets[i] receives the offset of where221* values[i] replaced pattern argument {i}.222* Can be shorter or longer than values. Can be NULL if offsetsLength==0.223* If there is no {i} in the pattern, then offsets[i] is set to -1.224* @param offsetsLength The length of the offsets array.225* @param errorCode ICU error code in/out parameter.226* Must fulfill U_SUCCESS before the function call.227* @return appendTo228* @stable ICU 57229*/230UnicodeString &formatAndAppend(231const UnicodeString *const *values, int32_t valuesLength,232UnicodeString &appendTo,233int32_t *offsets, int32_t offsetsLength, UErrorCode &errorCode) const;234235/**236* Formats the given values, replacing the contents of the result string.237* May optimize by actually appending to the result if it is the same object238* as the value corresponding to the initial argument in the pattern.239*240* @param values The argument values.241* An argument value may be the same object as result.242* Can be NULL if valuesLength==getArgumentLimit()==0.243* @param valuesLength The length of the values array.244* Must be at least getArgumentLimit().245* @param result Gets its contents replaced by the formatted pattern and values.246* @param offsets offsets[i] receives the offset of where247* values[i] replaced pattern argument {i}.248* Can be shorter or longer than values. Can be NULL if offsetsLength==0.249* If there is no {i} in the pattern, then offsets[i] is set to -1.250* @param offsetsLength The length of the offsets array.251* @param errorCode ICU error code in/out parameter.252* Must fulfill U_SUCCESS before the function call.253* @return result254* @stable ICU 57255*/256UnicodeString &formatAndReplace(257const UnicodeString *const *values, int32_t valuesLength,258UnicodeString &result,259int32_t *offsets, int32_t offsetsLength, UErrorCode &errorCode) const;260261/**262* Returns the pattern text with none of the arguments.263* Like formatting with all-empty string values.264* @stable ICU 57265*/266UnicodeString getTextWithNoArguments() const {267return getTextWithNoArguments(268compiledPattern.getBuffer(),269compiledPattern.length(),270nullptr,2710);272}273274#ifndef U_HIDE_INTERNAL_API275/**276* Returns the pattern text with none of the arguments.277* Like formatting with all-empty string values.278*279* TODO(ICU-20406): Replace this with an Iterator interface.280*281* @param offsets offsets[i] receives the offset of where {i} was located282* before it was replaced by an empty string.283* For example, "a{0}b{1}" produces offset 1 for i=0 and 2 for i=1.284* Can be nullptr if offsetsLength==0.285* If there is no {i} in the pattern, then offsets[i] is set to -1.286* @param offsetsLength The length of the offsets array.287*288* @internal289*/290UnicodeString getTextWithNoArguments(int32_t *offsets, int32_t offsetsLength) const {291return getTextWithNoArguments(292compiledPattern.getBuffer(),293compiledPattern.length(),294offsets,295offsetsLength);296}297#endif // U_HIDE_INTERNAL_API298299private:300/**301* Binary representation of the compiled pattern.302* Index 0: One more than the highest argument number.303* Followed by zero or more arguments or literal-text segments.304*305* An argument is stored as its number, less than ARG_NUM_LIMIT.306* A literal-text segment is stored as its length (at least 1) offset by ARG_NUM_LIMIT,307* followed by that many chars.308*/309UnicodeString compiledPattern;310311static inline int32_t getArgumentLimit(const char16_t *compiledPattern,312int32_t compiledPatternLength) {313return compiledPatternLength == 0 ? 0 : compiledPattern[0];314}315316static UnicodeString getTextWithNoArguments(317const char16_t *compiledPattern,318int32_t compiledPatternLength,319int32_t *offsets,320int32_t offsetsLength);321322static UnicodeString &format(323const char16_t *compiledPattern, int32_t compiledPatternLength,324const UnicodeString *const *values,325UnicodeString &result, const UnicodeString *resultCopy, UBool forbidResultAsValue,326int32_t *offsets, int32_t offsetsLength,327UErrorCode &errorCode);328329// Give access to internals to SimpleModifier for number formatting330friend class number::impl::SimpleModifier;331};332333U_NAMESPACE_END334335#endif // __SIMPLEFORMATTER_H__336337338