Loading...
Searching...
No Matches
messagepattern.h
Go to the documentation of this file.
// © 2016 and later: Unicode, Inc. and others.
// License & terms of use: http://www.unicode.org/copyright.html
/*
*******************************************************************************
* Copyright (C) 2011-2013, International Business Machines
* Corporation and others. All Rights Reserved.
*******************************************************************************
* file name: messagepattern.h
* encoding: UTF-8
* tab size: 8 (not used)
* indentation:4
*
* created on: 2011mar14
* created by: Markus W. Scherer
*/
#ifndef __MESSAGEPATTERN_H__
#define __MESSAGEPATTERN_H__
#include "unicode/utypes.h"
#if U_SHOW_CPLUSPLUS_API
#if !UCONFIG_NO_FORMATTING
#include "unicode/parseerr.h"
#include "unicode/unistr.h"
enum UMessagePatternApostropheMode {
UMSGPAT_APOS_DOUBLE_OPTIONAL,
UMSGPAT_APOS_DOUBLE_REQUIRED
};
typedef enum UMessagePatternApostropheMode UMessagePatternApostropheMode;
enum UMessagePatternPartType {
UMSGPAT_PART_TYPE_MSG_START,
UMSGPAT_PART_TYPE_MSG_LIMIT,
UMSGPAT_PART_TYPE_SKIP_SYNTAX,
UMSGPAT_PART_TYPE_INSERT_CHAR,
UMSGPAT_PART_TYPE_REPLACE_NUMBER,
UMSGPAT_PART_TYPE_ARG_START,
UMSGPAT_PART_TYPE_ARG_LIMIT,
UMSGPAT_PART_TYPE_ARG_NUMBER,
UMSGPAT_PART_TYPE_ARG_NAME,
UMSGPAT_PART_TYPE_ARG_TYPE,
UMSGPAT_PART_TYPE_ARG_STYLE,
UMSGPAT_PART_TYPE_ARG_SELECTOR,
UMSGPAT_PART_TYPE_ARG_INT,
UMSGPAT_PART_TYPE_ARG_DOUBLE
};
typedef enum UMessagePatternPartType UMessagePatternPartType;
enum UMessagePatternArgType {
UMSGPAT_ARG_TYPE_NONE,
UMSGPAT_ARG_TYPE_SIMPLE,
UMSGPAT_ARG_TYPE_CHOICE,
UMSGPAT_ARG_TYPE_PLURAL,
UMSGPAT_ARG_TYPE_SELECT,
UMSGPAT_ARG_TYPE_SELECTORDINAL
};
typedef enum UMessagePatternArgType UMessagePatternArgType;
#define UMSGPAT_ARG_TYPE_HAS_PLURAL_STYLE(argType) \
((argType)==UMSGPAT_ARG_TYPE_PLURAL || (argType)==UMSGPAT_ARG_TYPE_SELECTORDINAL)
enum {
UMSGPAT_ARG_NAME_NOT_NUMBER=-1,
UMSGPAT_ARG_NAME_NOT_VALID=-2
};
#define UMSGPAT_NO_NUMERIC_VALUE ((double)(-123456789))
U_NAMESPACE_BEGIN
class MessagePatternDoubleList;
class MessagePatternPartsList;
class U_COMMON_API MessagePattern : public UObject {
public:
MessagePattern(UErrorCode &errorCode);
MessagePattern(UMessagePatternApostropheMode mode, UErrorCode &errorCode);
MessagePattern(const UnicodeString &pattern, UParseError *parseError, UErrorCode &errorCode);
MessagePattern(const MessagePattern &other);
MessagePattern &operator=(const MessagePattern &other);
virtual ~MessagePattern();
MessagePattern &parse(const UnicodeString &pattern,
UParseError *parseError, UErrorCode &errorCode);
MessagePattern &parseChoiceStyle(const UnicodeString &pattern,
UParseError *parseError, UErrorCode &errorCode);
MessagePattern &parsePluralStyle(const UnicodeString &pattern,
UParseError *parseError, UErrorCode &errorCode);
MessagePattern &parseSelectStyle(const UnicodeString &pattern,
UParseError *parseError, UErrorCode &errorCode);
void clear();
void clearPatternAndSetApostropheMode(UMessagePatternApostropheMode mode) {
clear();
aposMode=mode;
}
bool operator==(const MessagePattern &other) const;
inline bool operator!=(const MessagePattern &other) const {
return !operator==(other);
}
int32_t hashCode() const;
UMessagePatternApostropheMode getApostropheMode() const {
return aposMode;
}
// Java has package-private jdkAposMode() here.
// In C++, this is declared in the MessageImpl class.
const UnicodeString &getPatternString() const {
return msg;
}
UBool hasNamedArguments() const {
return hasArgNames;
}
UBool hasNumberedArguments() const {
return hasArgNumbers;
}
static int32_t validateArgumentName(const UnicodeString &name);
UnicodeString autoQuoteApostropheDeep() const;
class Part;
int32_t countParts() const {
return partsLength;
}
const Part &getPart(int32_t i) const {
return parts[i];
}
UMessagePatternPartType getPartType(int32_t i) const {
return getPart(i).type;
}
int32_t getPatternIndex(int32_t partIndex) const {
return getPart(partIndex).index;
}
UnicodeString getSubstring(const Part &part) const {
return msg.tempSubString(part.index, part.length);
}
UBool partSubstringMatches(const Part &part, const UnicodeString &s) const {
return 0==msg.compare(part.index, part.length, s);
}
double getNumericValue(const Part &part) const;
double getPluralOffset(int32_t pluralStart) const;
int32_t getLimitPartIndex(int32_t start) const {
int32_t limit=getPart(start).limitPartIndex;
if(limit<start) {
return start;
}
return limit;
}
class Part : public UMemory {
public:
Part() {}
UMessagePatternPartType getType() const {
return type;
}
int32_t getIndex() const {
return index;
}
int32_t getLength() const {
return length;
}
int32_t getLimit() const {
return index+length;
}
int32_t getValue() const {
return value;
}
UMessagePatternArgType getArgType() const {
UMessagePatternPartType msgType=getType();
if(msgType ==UMSGPAT_PART_TYPE_ARG_START || msgType ==UMSGPAT_PART_TYPE_ARG_LIMIT) {
return static_cast<UMessagePatternArgType>(value);
} else {
return UMSGPAT_ARG_TYPE_NONE;
}
}
static UBool hasNumericValue(UMessagePatternPartType type) {
return type==UMSGPAT_PART_TYPE_ARG_INT || type==UMSGPAT_PART_TYPE_ARG_DOUBLE;
}
bool operator==(const Part &other) const;
inline bool operator!=(const Part &other) const {
return !operator==(other);
}
int32_t hashCode() const {
return ((type*37+index)*37+length)*37+value;
}
private:
friend class MessagePattern;
static const int32_t MAX_LENGTH=0xffff;
static const int32_t MAX_VALUE=0x7fff;
static const int32_t MAX_NESTED_LEVELS=0x03ff;
// Some fields are not final because they are modified during pattern parsing.
// After pattern parsing, the parts are effectively immutable.
UMessagePatternPartType type;
int32_t index;
uint16_t length;
int16_t value;
int32_t limitPartIndex;
};
private:
void preParse(const UnicodeString &pattern, UParseError *parseError, UErrorCode &errorCode);
void postParse();
int32_t parseMessage(int32_t index, int32_t msgStartLength,
int32_t nestingLevel, UMessagePatternArgType parentType,
UParseError *parseError, UErrorCode &errorCode);
int32_t parseArg(int32_t index, int32_t argStartLength, int32_t nestingLevel,
UParseError *parseError, UErrorCode &errorCode);
int32_t parseSimpleStyle(int32_t index, UParseError *parseError, UErrorCode &errorCode);
int32_t parseChoiceStyle(int32_t index, int32_t nestingLevel,
UParseError *parseError, UErrorCode &errorCode);
int32_t parsePluralOrSelectStyle(UMessagePatternArgType argType, int32_t index, int32_t nestingLevel,
UParseError *parseError, UErrorCode &errorCode);
static int32_t parseArgNumber(const UnicodeString &s, int32_t start, int32_t limit);
int32_t parseArgNumber(int32_t start, int32_t limit) {
return parseArgNumber(msg, start, limit);
}
void parseDouble(int32_t start, int32_t limit, UBool allowInfinity,
UParseError *parseError, UErrorCode &errorCode);
// Java has package-private appendReducedApostrophes() here.
// In C++, this is declared in the MessageImpl class.
int32_t skipWhiteSpace(int32_t index);
int32_t skipIdentifier(int32_t index);
int32_t skipDouble(int32_t index);
static UBool isArgTypeChar(UChar32 c);
UBool isChoice(int32_t index);
UBool isPlural(int32_t index);
UBool isSelect(int32_t index);
UBool isOrdinal(int32_t index);
UBool inMessageFormatPattern(int32_t nestingLevel);
UBool inTopLevelChoiceMessage(int32_t nestingLevel, UMessagePatternArgType parentType);
void addPart(UMessagePatternPartType type, int32_t index, int32_t length,
int32_t value, UErrorCode &errorCode);
void addLimitPart(int32_t start,
UMessagePatternPartType type, int32_t index, int32_t length,
int32_t value, UErrorCode &errorCode);
void addArgDoublePart(double numericValue, int32_t start, int32_t length, UErrorCode &errorCode);
void setParseError(UParseError *parseError, int32_t index);
UBool init(UErrorCode &errorCode);
UBool copyStorage(const MessagePattern &other, UErrorCode &errorCode);
UMessagePatternApostropheMode aposMode;
UnicodeString msg;
// ArrayList<Part> parts=new ArrayList<Part>();
MessagePatternPartsList *partsList;
Part *parts;
int32_t partsLength;
// ArrayList<Double> numericValues;
MessagePatternDoubleList *numericValuesList;
double *numericValues;
int32_t numericValuesLength;
UBool hasArgNames;
UBool hasArgNumbers;
UBool needsAutoQuoting;
};
U_NAMESPACE_END
#endif // !UCONFIG_NO_FORMATTING
#endif /* U_SHOW_CPLUSPLUS_API */
#endif // __MESSAGEPATTERN_H__
icu::MessagePattern::Part
A message pattern "part", representing a pattern parsing event.
Definition messagepattern.h:713
icu::MessagePattern::Part::operator==
bool operator==(const Part &other) const
icu::MessagePattern::Part::getArgType
UMessagePatternArgType getArgType() const
Returns the argument type if this part is of type ARG_START or ARG_LIMIT, otherwise UMSGPAT_ARG_TYPE_...
Definition messagepattern.h:775
icu::MessagePattern::Part::hashCode
int32_t hashCode() const
Definition messagepattern.h:815
icu::MessagePattern::Part::getIndex
int32_t getIndex() const
Returns the pattern string index associated with this Part.
Definition messagepattern.h:735
icu::MessagePattern::Part::getValue
int32_t getValue() const
Returns a value associated with this part.
Definition messagepattern.h:765
icu::MessagePattern::Part::operator!=
bool operator!=(const Part &other) const
Definition messagepattern.h:807
icu::MessagePattern::Part::getType
UMessagePatternPartType getType() const
Returns the type of this part.
Definition messagepattern.h:726
icu::MessagePattern::Part::getLength
int32_t getLength() const
Returns the length of the pattern substring associated with this Part.
Definition messagepattern.h:745
icu::MessagePattern::Part::getLimit
int32_t getLimit() const
Returns the pattern string limit (exclusive-end) index associated with this Part.
Definition messagepattern.h:755
icu::MessagePattern::Part::hasNumericValue
static UBool hasNumericValue(UMessagePatternPartType type)
Indicates whether the Part type has a numeric value.
Definition messagepattern.h:791
icu::MessagePattern::Part::Part
Part()
Default constructor, do not use.
Definition messagepattern.h:719
icu::MessagePattern::parseSelectStyle
MessagePattern & parseSelectStyle(const UnicodeString &pattern, UParseError *parseError, UErrorCode &errorCode)
Parses a SelectFormat pattern string.
icu::MessagePattern::clear
void clear()
Clears this MessagePattern.
icu::MessagePattern::getPatternString
const UnicodeString & getPatternString() const
Definition messagepattern.h:561
icu::MessagePattern::operator!=
bool operator!=(const MessagePattern &other) const
Definition messagepattern.h:536
icu::MessagePattern::getLimitPartIndex
int32_t getLimitPartIndex(int32_t start) const
Returns the index of the ARG|MSG_LIMIT part corresponding to the ARG|MSG_START at start.
Definition messagepattern.h:698
icu::MessagePattern::getSubstring
UnicodeString getSubstring(const Part &part) const
Returns the substring of the pattern string indicated by the Part.
Definition messagepattern.h:659
icu::MessagePattern::getNumericValue
double getNumericValue(const Part &part) const
Returns the numeric value associated with an ARG_INT or ARG_DOUBLE.
icu::MessagePattern::parseChoiceStyle
MessagePattern & parseChoiceStyle(const UnicodeString &pattern, UParseError *parseError, UErrorCode &errorCode)
Parses a ChoiceFormat pattern string.
icu::MessagePattern::countParts
int32_t countParts() const
Returns the number of "parts" created by parsing the pattern string.
Definition messagepattern.h:616
icu::MessagePattern::autoQuoteApostropheDeep
UnicodeString autoQuoteApostropheDeep() const
Returns a version of the parsed pattern string where each ASCII apostrophe is doubled (escaped) if it...
icu::MessagePattern::getPartType
UMessagePatternPartType getPartType(int32_t i) const
Returns the UMessagePatternPartType of the i-th pattern "part".
Definition messagepattern.h:637
icu::MessagePattern::clearPatternAndSetApostropheMode
void clearPatternAndSetApostropheMode(UMessagePatternApostropheMode mode)
Clears this MessagePattern and sets the UMessagePatternApostropheMode.
Definition messagepattern.h:519
icu::MessagePattern::hasNamedArguments
UBool hasNamedArguments() const
Does the parsed pattern have named arguments like {first_name}?
Definition messagepattern.h:570
icu::MessagePattern::MessagePattern
MessagePattern(UErrorCode &errorCode)
Constructs an empty MessagePattern with default UMessagePatternApostropheMode.
icu::MessagePattern::MessagePattern
MessagePattern(const MessagePattern &other)
Copy constructor.
icu::MessagePattern::validateArgumentName
static int32_t validateArgumentName(const UnicodeString &name)
Validates and parses an argument name or argument number string.
icu::MessagePattern::MessagePattern
MessagePattern(const UnicodeString &pattern, UParseError *parseError, UErrorCode &errorCode)
Constructs a MessagePattern with default UMessagePatternApostropheMode and parses the MessageFormat p...
icu::MessagePattern::hashCode
int32_t hashCode() const
icu::MessagePattern::getPluralOffset
double getPluralOffset(int32_t pluralStart) const
Returns the "offset:" value of a PluralFormat argument, or 0 if none is specified.
icu::MessagePattern::getPatternIndex
int32_t getPatternIndex(int32_t partIndex) const
Returns the pattern index of the specified pattern "part".
Definition messagepattern.h:648
icu::MessagePattern::parsePluralStyle
MessagePattern & parsePluralStyle(const UnicodeString &pattern, UParseError *parseError, UErrorCode &errorCode)
Parses a PluralFormat pattern string.
icu::MessagePattern::MessagePattern
MessagePattern(UMessagePatternApostropheMode mode, UErrorCode &errorCode)
Constructs an empty MessagePattern.
icu::MessagePattern::parse
MessagePattern & parse(const UnicodeString &pattern, UParseError *parseError, UErrorCode &errorCode)
Parses a MessageFormat pattern string.
icu::MessagePattern::operator==
bool operator==(const MessagePattern &other) const
icu::MessagePattern::hasNumberedArguments
UBool hasNumberedArguments() const
Does the parsed pattern have numbered arguments like {2}?
Definition messagepattern.h:579
icu::MessagePattern::operator=
MessagePattern & operator=(const MessagePattern &other)
Assignment operator.
icu::MessagePattern::getApostropheMode
UMessagePatternApostropheMode getApostropheMode() const
Definition messagepattern.h:550
icu::MessagePattern::~MessagePattern
virtual ~MessagePattern()
Destructor.
icu::MessagePattern::partSubstringMatches
UBool partSubstringMatches(const Part &part, const UnicodeString &s) const
Compares the part's substring with the input string s.
Definition messagepattern.h:670
icu::MessagePattern::getPart
const Part & getPart(int32_t i) const
Gets the i-th pattern "part".
Definition messagepattern.h:626
icu::UMemory
UMemory is the common ICU base class.
Definition uobject.h:115
icu::UObject
UObject is the common ICU "boilerplate" class.
Definition uobject.h:222
icu::UnicodeString
UnicodeString is a string class that stores Unicode characters directly and provides similar function...
Definition unistr.h:303
icu::UnicodeString::tempSubString
UnicodeString tempSubString(int32_t start=0, int32_t length=INT32_MAX) const
Create a temporary substring for the specified range.
UMessagePatternPartType
UMessagePatternPartType
MessagePattern::Part type constants.
Definition messagepattern.h:102
UMSGPAT_PART_TYPE_INSERT_CHAR
@ UMSGPAT_PART_TYPE_INSERT_CHAR
Indicates that a syntax character needs to be inserted for auto-quoting.
Definition messagepattern.h:136
UMSGPAT_PART_TYPE_ARG_TYPE
@ UMSGPAT_PART_TYPE_ARG_TYPE
The argument type.
Definition messagepattern.h:179
UMSGPAT_PART_TYPE_MSG_START
@ UMSGPAT_PART_TYPE_MSG_START
Start of a message pattern (main or nested).
Definition messagepattern.h:112
UMSGPAT_PART_TYPE_ARG_SELECTOR
@ UMSGPAT_PART_TYPE_ARG_SELECTOR
A selector substring in a "complex" argument style.
Definition messagepattern.h:191
UMSGPAT_PART_TYPE_ARG_LIMIT
@ UMSGPAT_PART_TYPE_ARG_LIMIT
End of an argument.
Definition messagepattern.h:162
UMSGPAT_PART_TYPE_ARG_NUMBER
@ UMSGPAT_PART_TYPE_ARG_NUMBER
The argument number, provided by the value.
Definition messagepattern.h:167
UMSGPAT_PART_TYPE_MSG_LIMIT
@ UMSGPAT_PART_TYPE_MSG_LIMIT
End of a message pattern (main or nested).
Definition messagepattern.h:121
UMSGPAT_PART_TYPE_ARG_DOUBLE
@ UMSGPAT_PART_TYPE_ARG_DOUBLE
A numeric value, for example the offset or an explicit selector value in a PluralFormat style.
Definition messagepattern.h:206
UMSGPAT_PART_TYPE_ARG_NAME
@ UMSGPAT_PART_TYPE_ARG_NAME
The argument name.
Definition messagepattern.h:173
UMSGPAT_PART_TYPE_ARG_START
@ UMSGPAT_PART_TYPE_ARG_START
Start of an argument.
Definition messagepattern.h:155
UMSGPAT_PART_TYPE_REPLACE_NUMBER
@ UMSGPAT_PART_TYPE_REPLACE_NUMBER
Indicates a syntactic (non-escaped) # symbol in a plural variant.
Definition messagepattern.h:144
UMSGPAT_PART_TYPE_ARG_STYLE
@ UMSGPAT_PART_TYPE_ARG_STYLE
The argument style text.
Definition messagepattern.h:185
UMSGPAT_PART_TYPE_ARG_INT
@ UMSGPAT_PART_TYPE_ARG_INT
An integer value, for example the offset or an explicit selector value in a PluralFormat style.
Definition messagepattern.h:198
UMSGPAT_PART_TYPE_SKIP_SYNTAX
@ UMSGPAT_PART_TYPE_SKIP_SYNTAX
Indicates a substring of the pattern string which is to be skipped when formatting.
Definition messagepattern.h:129
UMessagePatternArgType
UMessagePatternArgType
Argument type constants.
Definition messagepattern.h:221
UMSGPAT_ARG_TYPE_SELECT
@ UMSGPAT_ARG_TYPE_SELECT
The argument is a SelectFormat with one or more (ARG_SELECTOR, message) pairs.
Definition messagepattern.h:253
UMSGPAT_ARG_TYPE_NONE
@ UMSGPAT_ARG_TYPE_NONE
The argument has no specified type.
Definition messagepattern.h:226
UMSGPAT_ARG_TYPE_PLURAL
@ UMSGPAT_ARG_TYPE_PLURAL
The argument is a cardinal-number PluralFormat with an optional ARG_INT or ARG_DOUBLE offset (e....
Definition messagepattern.h:248
UMSGPAT_ARG_TYPE_SIMPLE
@ UMSGPAT_ARG_TYPE_SIMPLE
The argument has a "simple" type which is provided by the ARG_TYPE part.
Definition messagepattern.h:232
UMSGPAT_ARG_TYPE_SELECTORDINAL
@ UMSGPAT_ARG_TYPE_SELECTORDINAL
The argument is an ordinal-number PluralFormat with the same style parts sequence and semantics as UM...
Definition messagepattern.h:259
UMSGPAT_ARG_TYPE_CHOICE
@ UMSGPAT_ARG_TYPE_CHOICE
The argument is a ChoiceFormat with one or more ((ARG_INT | ARG_DOUBLE), ARG_SELECTOR,...
Definition messagepattern.h:238
UMSGPAT_ARG_NAME_NOT_VALID
@ UMSGPAT_ARG_NAME_NOT_VALID
Return value from MessagePattern.validateArgumentName() for when the string is invalid.
Definition messagepattern.h:290
UMSGPAT_ARG_NAME_NOT_NUMBER
@ UMSGPAT_ARG_NAME_NOT_NUMBER
Return value from MessagePattern.validateArgumentName() for when the string is a valid "pattern ident...
Definition messagepattern.h:281
UMessagePatternApostropheMode
UMessagePatternApostropheMode
Mode for when an apostrophe starts quoted literal text for MessageFormat output.
Definition messagepattern.h:70
UMSGPAT_APOS_DOUBLE_OPTIONAL
@ UMSGPAT_APOS_DOUBLE_OPTIONAL
A literal apostrophe is represented by either a single or a double apostrophe pattern character.
Definition messagepattern.h:82
UMSGPAT_APOS_DOUBLE_REQUIRED
@ UMSGPAT_APOS_DOUBLE_REQUIRED
A literal apostrophe must be represented by a double apostrophe pattern character.
Definition messagepattern.h:91
icu::operator==
U_COMMON_API UBool operator==(const StringPiece &x, const StringPiece &y)
Global operator == for StringPiece.
parseerr.h
C API: Parse Error Information.
UParseError
A UParseError struct is used to returned detailed information about parsing errors.
Definition parseerr.h:58
UChar32
int32_t UChar32
Define UChar32 as a type for single Unicode code points.
Definition umachine.h:449
UBool
int8_t UBool
The ICU boolean type, a signed-byte integer.
Definition umachine.h:269
unistr.h
C++ API: Unicode String.
utypes.h
Basic definitions for ICU, for both C and C++ APIs.
UErrorCode
UErrorCode
Standard ICU4C error code type, a substitute for exceptions.
Definition utypes.h:509
U_COMMON_API
#define U_COMMON_API
Set to export library symbols from inside the common library, and to import them from outside.
Definition utypes.h:315