ICU4C 79
Loading...
Searching...
No Matches
messagepattern.h
Go to the documentation of this file.
1// © 2016 and later: Unicode, Inc. and others.
2// License & terms of use: http://www.unicode.org/copyright.html
3/*
4*******************************************************************************
5* Copyright (C) 2011-2013, International Business Machines
6* Corporation and others. All Rights Reserved.
7*******************************************************************************
8* file name: messagepattern.h
9* encoding: UTF-8
10* tab size: 8 (not used)
11* indentation:4
12*
13* created on: 2011mar14
14* created by: Markus W. Scherer
15*/
16
17#ifndef __MESSAGEPATTERN_H__
18#define __MESSAGEPATTERN_H__
19
24
25#include "unicode/utypes.h"
26
27#if U_SHOW_CPLUSPLUS_API
28
29#if !UCONFIG_NO_FORMATTING
30
31#include "unicode/parseerr.h"
32#include "unicode/unistr.h"
33
93
97
208
212
261
265
272#define UMSGPAT_ARG_TYPE_HAS_PLURAL_STYLE(argType) \
273 ((argType)==UMSGPAT_ARG_TYPE_PLURAL || (argType)==UMSGPAT_ARG_TYPE_SELECTORDINAL)
274
275enum {
282
291};
292
299#define UMSGPAT_NO_NUMERIC_VALUE ((double)(-123456789))
300
301U_NAMESPACE_BEGIN
302
303class MessagePatternDoubleList;
304class MessagePatternPartsList;
305
363public:
373
384
403 MessagePattern(const UnicodeString &pattern, UParseError *parseError, UErrorCode &errorCode);
404
411
418 MessagePattern& operator=(const MessagePattern& other) U_LIFETIME_BOUND;
419
425
444 UParseError* parseError,
445 UErrorCode& errorCode) U_LIFETIME_BOUND;
446
465 UParseError* parseError,
466 UErrorCode& errorCode) U_LIFETIME_BOUND;
467
486 UParseError* parseError,
487 UErrorCode& errorCode) U_LIFETIME_BOUND;
488
507 UParseError* parseError,
508 UErrorCode& errorCode) U_LIFETIME_BOUND;
509
515 void clear();
516
524 clear();
525 aposMode=mode;
526 }
527
533 bool operator==(const MessagePattern &other) const;
534
540 inline bool operator!=(const MessagePattern &other) const {
541 return !operator==(other);
542 }
543
548 int32_t hashCode() const;
549
555 return aposMode;
556 }
557
558 // Java has package-private jdkAposMode() here.
559 // In C++, this is declared in the MessageImpl class.
560
565 const UnicodeString& getPatternString() const U_LIFETIME_BOUND {
566 return msg;
567 }
568
575 return hasArgNames;
576 }
577
584 return hasArgNumbers;
585 }
586
598 static int32_t validateArgumentName(const UnicodeString &name);
599
611
612 class Part;
613
620 int32_t countParts() const {
621 return partsLength;
622 }
623
630 const Part& getPart(int32_t i) const U_LIFETIME_BOUND {
631 return parts[i];
632 }
633
642 return getPart(i).type;
643 }
644
652 int32_t getPatternIndex(int32_t partIndex) const {
653 return getPart(partIndex).index;
654 }
655
663 UnicodeString getSubstring(const Part& part) const U_LIFETIME_BOUND {
664 return msg.tempSubString(part.index, part.length);
665 }
666
674 UBool partSubstringMatches(const Part &part, const UnicodeString &s) const {
675 return 0==msg.compare(part.index, part.length, s);
676 }
677
684 double getNumericValue(const Part &part) const;
685
692 double getPluralOffset(int32_t pluralStart) const;
693
702 int32_t getLimitPartIndex(int32_t start) const {
703 int32_t limit=getPart(start).limitPartIndex;
704 if(limit<start) {
705 return start;
706 }
707 return limit;
708 }
709
717 class Part : public UMemory {
718 public:
723 Part() {}
724
731 return type;
732 }
733
739 int32_t getIndex() const {
740 return index;
741 }
742
749 int32_t getLength() const {
750 return length;
751 }
752
759 int32_t getLimit() const {
760 return index+length;
761 }
762
769 int32_t getValue() const {
770 return value;
771 }
772
782 return static_cast<UMessagePatternArgType>(value);
783 } else {
785 }
786 }
787
798
804 bool operator==(const Part &other) const;
805
811 inline bool operator!=(const Part &other) const {
812 return !operator==(other);
813 }
814
819 int32_t hashCode() const {
820 return ((type*37+index)*37+length)*37+value;
821 }
822
823 private:
824 friend class MessagePattern;
825
826 static const int32_t MAX_LENGTH=0xffff;
827 static const int32_t MAX_VALUE=0x7fff;
828 static const int32_t MAX_NESTED_LEVELS=0x03ff;
829
830 // Some fields are not final because they are modified during pattern parsing.
831 // After pattern parsing, the parts are effectively immutable.
833 int32_t index;
834 uint16_t length;
835 int16_t value;
836 int32_t limitPartIndex;
837 };
838
839private:
840 void preParse(const UnicodeString &pattern, UParseError *parseError, UErrorCode &errorCode);
841
842 void postParse();
843
844 int32_t parseMessage(int32_t index, int32_t msgStartLength,
845 int32_t nestingLevel, UMessagePatternArgType parentType,
846 UParseError *parseError, UErrorCode &errorCode);
847
848 int32_t parseArg(int32_t index, int32_t argStartLength, int32_t nestingLevel,
849 UParseError *parseError, UErrorCode &errorCode);
850
851 int32_t parseSimpleStyle(int32_t index, UParseError *parseError, UErrorCode &errorCode);
852
853 int32_t parseChoiceStyle(int32_t index, int32_t nestingLevel,
854 UParseError *parseError, UErrorCode &errorCode);
855
856 int32_t parsePluralOrSelectStyle(UMessagePatternArgType argType, int32_t index, int32_t nestingLevel,
857 UParseError *parseError, UErrorCode &errorCode);
858
867 static int32_t parseArgNumber(const UnicodeString &s, int32_t start, int32_t limit);
868
869 int32_t parseArgNumber(int32_t start, int32_t limit) {
870 return parseArgNumber(msg, start, limit);
871 }
872
881 void parseDouble(int32_t start, int32_t limit, UBool allowInfinity,
882 UParseError *parseError, UErrorCode &errorCode);
883
884 // Java has package-private appendReducedApostrophes() here.
885 // In C++, this is declared in the MessageImpl class.
886
887 int32_t skipWhiteSpace(int32_t index);
888
889 int32_t skipIdentifier(int32_t index);
890
895 int32_t skipDouble(int32_t index);
896
897 static UBool isArgTypeChar(UChar32 c);
898
899 UBool isChoice(int32_t index);
900
901 UBool isPlural(int32_t index);
902
903 UBool isSelect(int32_t index);
904
905 UBool isOrdinal(int32_t index);
906
911 UBool inMessageFormatPattern(int32_t nestingLevel);
912
917 UBool inTopLevelChoiceMessage(int32_t nestingLevel, UMessagePatternArgType parentType);
918
919 void addPart(UMessagePatternPartType type, int32_t index, int32_t length,
920 int32_t value, UErrorCode &errorCode);
921
922 void addLimitPart(int32_t start,
923 UMessagePatternPartType type, int32_t index, int32_t length,
924 int32_t value, UErrorCode &errorCode);
925
926 void addArgDoublePart(double numericValue, int32_t start, int32_t length, UErrorCode &errorCode);
927
928 void setParseError(UParseError *parseError, int32_t index);
929
930 UBool init(UErrorCode &errorCode);
931 UBool copyStorage(const MessagePattern &other, UErrorCode &errorCode);
932
934 UnicodeString msg;
935 // ArrayList<Part> parts=new ArrayList<Part>();
936 MessagePatternPartsList *partsList;
937 Part *parts;
938 int32_t partsLength;
939 // ArrayList<Double> numericValues;
940 MessagePatternDoubleList *numericValuesList;
941 double *numericValues;
942 int32_t numericValuesLength;
943 UBool hasArgNames;
944 UBool hasArgNumbers;
945 UBool needsAutoQuoting;
946};
947
948U_NAMESPACE_END
949
950#endif // !UCONFIG_NO_FORMATTING
951
952#endif /* U_SHOW_CPLUSPLUS_API */
953
954#endif // __MESSAGEPATTERN_H__
A message pattern "part", representing a pattern parsing event.
bool operator==(const Part &other) const
UMessagePatternArgType getArgType() const
Returns the argument type if this part is of type ARG_START or ARG_LIMIT, otherwise UMSGPAT_ARG_TYPE_...
int32_t getIndex() const
Returns the pattern string index associated with this Part.
int32_t getValue() const
Returns a value associated with this part.
bool operator!=(const Part &other) const
UMessagePatternPartType getType() const
Returns the type of this part.
int32_t getLength() const
Returns the length of the pattern substring associated with this Part.
int32_t getLimit() const
Returns the pattern string limit (exclusive-end) index associated with this Part.
static UBool hasNumericValue(UMessagePatternPartType type)
Indicates whether the Part type has a numeric value.
Part()
Default constructor, do not use.
MessagePattern & parseSelectStyle(const UnicodeString &pattern, UParseError *parseError, UErrorCode &errorCode)
Parses a SelectFormat pattern string.
void clear()
Clears this MessagePattern.
const UnicodeString & getPatternString() const
bool operator!=(const MessagePattern &other) const
int32_t getLimitPartIndex(int32_t start) const
Returns the index of the ARG|MSG_LIMIT part corresponding to the ARG|MSG_START at start.
UnicodeString getSubstring(const Part &part) const
Returns the substring of the pattern string indicated by the Part.
double getNumericValue(const Part &part) const
Returns the numeric value associated with an ARG_INT or ARG_DOUBLE.
MessagePattern & parseChoiceStyle(const UnicodeString &pattern, UParseError *parseError, UErrorCode &errorCode)
Parses a ChoiceFormat pattern string.
int32_t countParts() const
Returns the number of "parts" created by parsing the pattern string.
UnicodeString autoQuoteApostropheDeep() const
Returns a version of the parsed pattern string where each ASCII apostrophe is doubled (escaped) if it...
UMessagePatternPartType getPartType(int32_t i) const
Returns the UMessagePatternPartType of the i-th pattern "part".
void clearPatternAndSetApostropheMode(UMessagePatternApostropheMode mode)
Clears this MessagePattern and sets the UMessagePatternApostropheMode.
UBool hasNamedArguments() const
Does the parsed pattern have named arguments like {first_name}?
MessagePattern(UErrorCode &errorCode)
Constructs an empty MessagePattern with default UMessagePatternApostropheMode.
MessagePattern(const MessagePattern &other)
Copy constructor.
static int32_t validateArgumentName(const UnicodeString &name)
Validates and parses an argument name or argument number string.
MessagePattern(const UnicodeString &pattern, UParseError *parseError, UErrorCode &errorCode)
Constructs a MessagePattern with default UMessagePatternApostropheMode and parses the MessageFormat p...
int32_t hashCode() const
double getPluralOffset(int32_t pluralStart) const
Returns the "offset:" value of a PluralFormat argument, or 0 if none is specified.
int32_t getPatternIndex(int32_t partIndex) const
Returns the pattern index of the specified pattern "part".
MessagePattern & parsePluralStyle(const UnicodeString &pattern, UParseError *parseError, UErrorCode &errorCode)
Parses a PluralFormat pattern string.
MessagePattern(UMessagePatternApostropheMode mode, UErrorCode &errorCode)
Constructs an empty MessagePattern.
MessagePattern & parse(const UnicodeString &pattern, UParseError *parseError, UErrorCode &errorCode)
Parses a MessageFormat pattern string.
bool operator==(const MessagePattern &other) const
UBool hasNumberedArguments() const
Does the parsed pattern have numbered arguments like {2}?
MessagePattern & operator=(const MessagePattern &other)
Assignment operator.
UMessagePatternApostropheMode getApostropheMode() const
virtual ~MessagePattern()
Destructor.
UBool partSubstringMatches(const Part &part, const UnicodeString &s) const
Compares the part's substring with the input string s.
const Part & getPart(int32_t i) const
Gets the i-th pattern "part".
UMemory is the common ICU base class.
Definition uobject.h:115
UObject is the common ICU "boilerplate" class.
Definition uobject.h:222
UnicodeString is a string class that stores Unicode characters directly and provides similar function...
Definition unistr.h:303
UMessagePatternPartType
MessagePattern::Part type constants.
@ UMSGPAT_PART_TYPE_INSERT_CHAR
Indicates that a syntax character needs to be inserted for auto-quoting.
@ UMSGPAT_PART_TYPE_ARG_TYPE
The argument type.
@ UMSGPAT_PART_TYPE_MSG_START
Start of a message pattern (main or nested).
@ UMSGPAT_PART_TYPE_ARG_SELECTOR
A selector substring in a "complex" argument style.
@ UMSGPAT_PART_TYPE_ARG_LIMIT
End of an argument.
@ UMSGPAT_PART_TYPE_ARG_NUMBER
The argument number, provided by the value.
@ UMSGPAT_PART_TYPE_MSG_LIMIT
End of a message pattern (main or nested).
@ UMSGPAT_PART_TYPE_ARG_DOUBLE
A numeric value, for example the offset or an explicit selector value in a PluralFormat style.
@ UMSGPAT_PART_TYPE_ARG_NAME
The argument name.
@ UMSGPAT_PART_TYPE_ARG_START
Start of an argument.
@ UMSGPAT_PART_TYPE_REPLACE_NUMBER
Indicates a syntactic (non-escaped) # symbol in a plural variant.
@ UMSGPAT_PART_TYPE_ARG_STYLE
The argument style text.
@ UMSGPAT_PART_TYPE_ARG_INT
An integer value, for example the offset or an explicit selector value in a PluralFormat style.
@ UMSGPAT_PART_TYPE_SKIP_SYNTAX
Indicates a substring of the pattern string which is to be skipped when formatting.
UMessagePatternArgType
Argument type constants.
@ UMSGPAT_ARG_TYPE_SELECT
The argument is a SelectFormat with one or more (ARG_SELECTOR, message) pairs.
@ UMSGPAT_ARG_TYPE_NONE
The argument has no specified type.
@ UMSGPAT_ARG_TYPE_PLURAL
The argument is a cardinal-number PluralFormat with an optional ARG_INT or ARG_DOUBLE offset (e....
@ UMSGPAT_ARG_TYPE_SIMPLE
The argument has a "simple" type which is provided by the ARG_TYPE part.
@ UMSGPAT_ARG_TYPE_SELECTORDINAL
The argument is an ordinal-number PluralFormat with the same style parts sequence and semantics as UM...
@ UMSGPAT_ARG_TYPE_CHOICE
The argument is a ChoiceFormat with one or more ((ARG_INT | ARG_DOUBLE), ARG_SELECTOR,...
@ UMSGPAT_ARG_NAME_NOT_VALID
Return value from MessagePattern.validateArgumentName() for when the string is invalid.
@ UMSGPAT_ARG_NAME_NOT_NUMBER
Return value from MessagePattern.validateArgumentName() for when the string is a valid "pattern ident...
UMessagePatternApostropheMode
Mode for when an apostrophe starts quoted literal text for MessageFormat output.
@ UMSGPAT_APOS_DOUBLE_OPTIONAL
A literal apostrophe is represented by either a single or a double apostrophe pattern character.
@ UMSGPAT_APOS_DOUBLE_REQUIRED
A literal apostrophe must be represented by a double apostrophe pattern character.
U_COMMON_API UBool operator==(const StringPiece &x, const StringPiece &y)
Global operator == for StringPiece.
C API: Parse Error Information.
A UParseError struct is used to returned detailed information about parsing errors.
Definition parseerr.h:58
int32_t UChar32
Define UChar32 as a type for single Unicode code points.
Definition umachine.h:449
int8_t UBool
The ICU boolean type, a signed-byte integer.
Definition umachine.h:269
C++ API: Unicode String.
Basic definitions for ICU, for both C and C++ APIs.
UErrorCode
Standard ICU4C error code type, a substitute for exceptions.
Definition utypes.h:509
#define U_COMMON_API
Set to export library symbols from inside the common library, and to import them from outside.
Definition utypes.h:315