Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/native/common/unicode/simpleformatter.h
38827 views
/*1******************************************************************************2* Copyright (C) 2014-2016, International Business Machines3* Corporation and others. All Rights Reserved.4******************************************************************************5* simpleformatter.h6*/78#ifndef __SIMPLEFORMATTER_H__9#define __SIMPLEFORMATTER_H__1011/**12* \file13* \brief C++ API: Simple formatter, minimal subset of MessageFormat.14*/1516#include "unicode/utypes.h"17#include "unicode/unistr.h"1819#ifndef U_HIDE_DRAFT_API2021U_NAMESPACE_BEGIN2223/**24* Formats simple patterns like "{1} was born in {0}".25* Minimal subset of MessageFormat; fast, simple, minimal dependencies.26* Supports only numbered arguments with no type nor style parameters,27* and formats only string values.28* Quoting via ASCII apostrophe compatible with ICU MessageFormat default behavior.29*30* Factory methods set error codes for syntax errors31* and for too few or too many arguments/placeholders.32*33* SimpleFormatter objects are thread-safe except for assignment and applying new patterns.34*35* Example:36* <pre>37* UErrorCode errorCode = U_ZERO_ERROR;38* SimpleFormatter fmt("{1} '{born}' in {0}", errorCode);39* UnicodeString result;40*41* // Output: "paul {born} in england"42* fmt.format("england", "paul", result, errorCode);43* </pre>44*45* This class is not intended for public subclassing.46*47* @see MessageFormat48* @see UMessagePatternApostropheMode49* @draft ICU 5750*/51class U_COMMON_API SimpleFormatter U_FINAL : public UMemory {52public:53/**54* Default constructor.55* @draft ICU 5756*/57SimpleFormatter() : compiledPattern((UChar)0) {}5859/**60* Constructs a formatter from the pattern string.61*62* @param pattern The pattern string.63* @param errorCode ICU error code in/out parameter.64* Must fulfill U_SUCCESS before the function call.65* Set to U_ILLEGAL_ARGUMENT_ERROR for bad argument syntax.66* @draft ICU 5767*/68SimpleFormatter(const UnicodeString& pattern, UErrorCode &errorCode) {69applyPattern(pattern, errorCode);70}7172/**73* Constructs a formatter from the pattern string.74* The number of arguments checked against the given limits is the75* highest argument number plus one, not the number of occurrences of arguments.76*77* @param pattern The pattern string.78* @param min The pattern must have at least this many arguments.79* @param max The pattern must have at most this many arguments.80* @param errorCode ICU error code in/out parameter.81* Must fulfill U_SUCCESS before the function call.82* Set to U_ILLEGAL_ARGUMENT_ERROR for bad argument syntax and83* too few or too many arguments.84* @draft ICU 5785*/86SimpleFormatter(const UnicodeString& pattern, int32_t min, int32_t max,87UErrorCode &errorCode) {88applyPatternMinMaxArguments(pattern, min, max, errorCode);89}9091/**92* Copy constructor.93* @draft ICU 5794*/95SimpleFormatter(const SimpleFormatter& other)96: compiledPattern(other.compiledPattern) {}9798/**99* Assignment operator.100* @draft ICU 57101*/102SimpleFormatter &operator=(const SimpleFormatter& other);103104/**105* Destructor.106* @draft ICU 57107*/108~SimpleFormatter();109110/**111* Changes this object according to the new pattern.112*113* @param pattern The pattern string.114* @param errorCode ICU error code in/out parameter.115* Must fulfill U_SUCCESS before the function call.116* Set to U_ILLEGAL_ARGUMENT_ERROR for bad argument syntax.117* @return TRUE if U_SUCCESS(errorCode).118* @draft ICU 57119*/120UBool applyPattern(const UnicodeString &pattern, UErrorCode &errorCode) {121return applyPatternMinMaxArguments(pattern, 0, INT32_MAX, errorCode);122}123124/**125* Changes this object according to the new pattern.126* The number of arguments checked against the given limits is the127* highest argument number plus one, not the number of occurrences of arguments.128*129* @param pattern The pattern string.130* @param min The pattern must have at least this many arguments.131* @param max The pattern must have at most this many arguments.132* @param errorCode ICU error code in/out parameter.133* Must fulfill U_SUCCESS before the function call.134* Set to U_ILLEGAL_ARGUMENT_ERROR for bad argument syntax and135* too few or too many arguments.136* @return TRUE if U_SUCCESS(errorCode).137* @draft ICU 57138*/139UBool applyPatternMinMaxArguments(const UnicodeString &pattern,140int32_t min, int32_t max, UErrorCode &errorCode);141142/**143* @return The max argument number + 1.144* @draft ICU 57145*/146int32_t getArgumentLimit() const {147return getArgumentLimit(compiledPattern.getBuffer(), compiledPattern.length());148}149150/**151* Formats the given value, appending to the appendTo builder.152* The argument value must not be the same object as appendTo.153* getArgumentLimit() must be at most 1.154*155* @param value0 Value for argument {0}.156* @param appendTo Gets the formatted pattern and value appended.157* @param errorCode ICU error code in/out parameter.158* Must fulfill U_SUCCESS before the function call.159* @return appendTo160* @draft ICU 57161*/162UnicodeString &format(163const UnicodeString &value0,164UnicodeString &appendTo, UErrorCode &errorCode) const;165166/**167* Formats the given values, appending to the appendTo builder.168* An argument value must not be the same object as appendTo.169* getArgumentLimit() must be at most 2.170*171* @param value0 Value for argument {0}.172* @param value1 Value for argument {1}.173* @param appendTo Gets the formatted pattern and values appended.174* @param errorCode ICU error code in/out parameter.175* Must fulfill U_SUCCESS before the function call.176* @return appendTo177* @draft ICU 57178*/179UnicodeString &format(180const UnicodeString &value0,181const UnicodeString &value1,182UnicodeString &appendTo, UErrorCode &errorCode) const;183184/**185* Formats the given values, appending to the appendTo builder.186* An argument value must not be the same object as appendTo.187* getArgumentLimit() must be at most 3.188*189* @param value0 Value for argument {0}.190* @param value1 Value for argument {1}.191* @param value2 Value for argument {2}.192* @param appendTo Gets the formatted pattern and values appended.193* @param errorCode ICU error code in/out parameter.194* Must fulfill U_SUCCESS before the function call.195* @return appendTo196* @draft ICU 57197*/198UnicodeString &format(199const UnicodeString &value0,200const UnicodeString &value1,201const UnicodeString &value2,202UnicodeString &appendTo, UErrorCode &errorCode) const;203204/**205* Formats the given values, appending to the appendTo string.206*207* @param values The argument values.208* An argument value must not be the same object as appendTo.209* Can be NULL if valuesLength==getArgumentLimit()==0.210* @param valuesLength The length of the values array.211* Must be at least getArgumentLimit().212* @param appendTo Gets the formatted pattern and values appended.213* @param offsets offsets[i] receives the offset of where214* values[i] replaced pattern argument {i}.215* Can be shorter or longer than values. Can be NULL if offsetsLength==0.216* If there is no {i} in the pattern, then offsets[i] is set to -1.217* @param offsetsLength The length of the offsets array.218* @param errorCode ICU error code in/out parameter.219* Must fulfill U_SUCCESS before the function call.220* @return appendTo221* @draft ICU 57222*/223UnicodeString &formatAndAppend(224const UnicodeString *const *values, int32_t valuesLength,225UnicodeString &appendTo,226int32_t *offsets, int32_t offsetsLength, UErrorCode &errorCode) const;227228/**229* Formats the given values, replacing the contents of the result string.230* May optimize by actually appending to the result if it is the same object231* as the value corresponding to the initial argument in the pattern.232*233* @param values The argument values.234* An argument value may be the same object as result.235* Can be NULL if valuesLength==getArgumentLimit()==0.236* @param valuesLength The length of the values array.237* Must be at least getArgumentLimit().238* @param result Gets its contents replaced by the formatted pattern and values.239* @param offsets offsets[i] receives the offset of where240* values[i] replaced pattern argument {i}.241* Can be shorter or longer than values. Can be NULL if offsetsLength==0.242* If there is no {i} in the pattern, then offsets[i] is set to -1.243* @param offsetsLength The length of the offsets array.244* @param errorCode ICU error code in/out parameter.245* Must fulfill U_SUCCESS before the function call.246* @return result247* @draft ICU 57248*/249UnicodeString &formatAndReplace(250const UnicodeString *const *values, int32_t valuesLength,251UnicodeString &result,252int32_t *offsets, int32_t offsetsLength, UErrorCode &errorCode) const;253254/**255* Returns the pattern text with none of the arguments.256* Like formatting with all-empty string values.257* @draft ICU 57258*/259UnicodeString getTextWithNoArguments() const {260return getTextWithNoArguments(compiledPattern.getBuffer(), compiledPattern.length());261}262263private:264/**265* Binary representation of the compiled pattern.266* Index 0: One more than the highest argument number.267* Followed by zero or more arguments or literal-text segments.268*269* An argument is stored as its number, less than ARG_NUM_LIMIT.270* A literal-text segment is stored as its length (at least 1) offset by ARG_NUM_LIMIT,271* followed by that many chars.272*/273UnicodeString compiledPattern;274275static inline int32_t getArgumentLimit(const UChar *compiledPattern,276int32_t compiledPatternLength) {277return compiledPatternLength == 0 ? 0 : compiledPattern[0];278}279280static UnicodeString getTextWithNoArguments(const UChar *compiledPattern, int32_t compiledPatternLength);281282static UnicodeString &format(283const UChar *compiledPattern, int32_t compiledPatternLength,284const UnicodeString *const *values,285UnicodeString &result, const UnicodeString *resultCopy, UBool forbidResultAsValue,286int32_t *offsets, int32_t offsetsLength,287UErrorCode &errorCode);288};289290U_NAMESPACE_END291292#endif /* U_HIDE_DRAFT_API */293294#endif // __SIMPLEFORMATTER_H__295296297