ICU 4.8.1.1  4.8.1.1
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
messagepattern.h
Go to the documentation of this file.
1 /*
2 *******************************************************************************
3 * Copyright (C) 2011, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 *******************************************************************************
6 * file name: messagepattern.h
7 * encoding: US-ASCII
8 * tab size: 8 (not used)
9 * indentation:4
10 *
11 * created on: 2011mar14
12 * created by: Markus W. Scherer
13 */
14 
15 #ifndef __MESSAGEPATTERN_H__
16 #define __MESSAGEPATTERN_H__
17 
23 #include "unicode/utypes.h"
24 
25 #if !UCONFIG_NO_FORMATTING
26 
27 #include "unicode/parseerr.h"
28 #include "unicode/unistr.h"
29 
88 };
93 
203 };
208 
250 };
255 
259 enum {
266 
275 };
276 
283 #define UMSGPAT_NO_NUMERIC_VALUE ((double)(-123456789))
284 
286 
287 class MessagePatternDoubleList;
288 class MessagePatternPartsList;
289 
347 public:
356  MessagePattern(UErrorCode &errorCode);
357 
368 
387  MessagePattern(const UnicodeString &pattern, UParseError *parseError, UErrorCode &errorCode);
388 
394  MessagePattern(const MessagePattern &other);
395 
402  MessagePattern &operator=(const MessagePattern &other);
403 
408  virtual ~MessagePattern();
409 
427  MessagePattern &parse(const UnicodeString &pattern,
428  UParseError *parseError, UErrorCode &errorCode);
429 
447  MessagePattern &parseChoiceStyle(const UnicodeString &pattern,
448  UParseError *parseError, UErrorCode &errorCode);
449 
467  MessagePattern &parsePluralStyle(const UnicodeString &pattern,
468  UParseError *parseError, UErrorCode &errorCode);
469 
487  MessagePattern &parseSelectStyle(const UnicodeString &pattern,
488  UParseError *parseError, UErrorCode &errorCode);
489 
495  void clear();
496 
504  clear();
505  aposMode=mode;
506  }
507 
513  UBool operator==(const MessagePattern &other) const;
514 
520  inline UBool operator!=(const MessagePattern &other) const {
521  return !operator==(other);
522  }
523 
528  int32_t hashCode() const;
529 
535  return aposMode;
536  }
537 
538  // Java has package-private jdkAposMode() here.
539  // In C++, this is declared in the MessageImpl class.
540 
546  return msg;
547  }
548 
555  return hasArgNames;
556  }
557 
564  return hasArgNumbers;
565  }
566 
578  static int32_t validateArgumentName(const UnicodeString &name);
579 
590  UnicodeString autoQuoteApostropheDeep() const;
591 
592  class Part;
593 
600  int32_t countParts() const {
601  return partsLength;
602  }
603 
610  const Part &getPart(int32_t i) const {
611  return parts[i];
612  }
613 
622  return getPart(i).type;
623  }
624 
632  int32_t getPatternIndex(int32_t partIndex) const {
633  return getPart(partIndex).index;
634  }
635 
643  UnicodeString getSubstring(const Part &part) const {
644  return msg.tempSubString(part.index, part.length);
645  }
646 
654  UBool partSubstringMatches(const Part &part, const UnicodeString &s) const {
655  return 0==msg.compare(part.index, part.length, s);
656  }
657 
664  double getNumericValue(const Part &part) const;
665 
672  double getPluralOffset(int32_t pluralStart) const;
673 
682  int32_t getLimitPartIndex(int32_t start) const {
683  int32_t limit=getPart(start).limitPartIndex;
684  if(limit<start) {
685  return start;
686  }
687  return limit;
688  }
689 
697  class Part : public UMemory {
698  public:
703  Part() {}
704 
711  return type;
712  }
713 
719  int32_t getIndex() const {
720  return index;
721  }
722 
729  int32_t getLength() const {
730  return length;
731  }
732 
739  int32_t getLimit() const {
740  return index+length;
741  }
742 
749  int32_t getValue() const {
750  return value;
751  }
752 
760  UMessagePatternPartType type=getType();
762  return (UMessagePatternArgType)value;
763  } else {
764  return UMSGPAT_ARG_TYPE_NONE;
765  }
766  }
767 
777  }
778 
784  UBool operator==(const Part &other) const;
785 
791  inline UBool operator!=(const Part &other) const {
792  return !operator==(other);
793  }
794 
799  int32_t hashCode() const {
800  return ((type*37+index)*37+length)*37+value;
801  }
802 
803  private:
804  friend class MessagePattern;
805 
806  static const int32_t MAX_LENGTH=0xffff;
807  static const int32_t MAX_VALUE=0x7fff;
808 
809  // Some fields are not final because they are modified during pattern parsing.
810  // After pattern parsing, the parts are effectively immutable.
812  int32_t index;
813  uint16_t length;
814  int16_t value;
815  int32_t limitPartIndex;
816  };
817 
818 private:
819  void preParse(const UnicodeString &pattern, UParseError *parseError, UErrorCode &errorCode);
820 
821  void postParse();
822 
823  int32_t parseMessage(int32_t index, int32_t msgStartLength,
824  int32_t nestingLevel, UMessagePatternArgType parentType,
825  UParseError *parseError, UErrorCode &errorCode);
826 
827  int32_t parseArg(int32_t index, int32_t argStartLength, int32_t nestingLevel,
828  UParseError *parseError, UErrorCode &errorCode);
829 
830  int32_t parseSimpleStyle(int32_t index, UParseError *parseError, UErrorCode &errorCode);
831 
832  int32_t parseChoiceStyle(int32_t index, int32_t nestingLevel,
833  UParseError *parseError, UErrorCode &errorCode);
834 
835  int32_t parsePluralOrSelectStyle(UMessagePatternArgType argType, int32_t index, int32_t nestingLevel,
836  UParseError *parseError, UErrorCode &errorCode);
837 
846  static int32_t parseArgNumber(const UnicodeString &s, int32_t start, int32_t limit);
847 
848  int32_t parseArgNumber(int32_t start, int32_t limit) {
849  return parseArgNumber(msg, start, limit);
850  }
851 
860  void parseDouble(int32_t start, int32_t limit, UBool allowInfinity,
861  UParseError *parseError, UErrorCode &errorCode);
862 
863  // Java has package-private appendReducedApostrophes() here.
864  // In C++, this is declared in the MessageImpl class.
865 
866  int32_t skipWhiteSpace(int32_t index);
867 
868  int32_t skipIdentifier(int32_t index);
869 
874  int32_t skipDouble(int32_t index);
875 
876  static UBool isArgTypeChar(UChar32 c);
877 
878  UBool isChoice(int32_t index);
879 
880  UBool isPlural(int32_t index);
881 
882  UBool isSelect(int32_t index);
883 
888  UBool inMessageFormatPattern(int32_t nestingLevel);
889 
894  UBool inTopLevelChoiceMessage(int32_t nestingLevel, UMessagePatternArgType parentType);
895 
896  void addPart(UMessagePatternPartType type, int32_t index, int32_t length,
897  int32_t value, UErrorCode &errorCode);
898 
899  void addLimitPart(int32_t start,
900  UMessagePatternPartType type, int32_t index, int32_t length,
901  int32_t value, UErrorCode &errorCode);
902 
903  void addArgDoublePart(double numericValue, int32_t start, int32_t length, UErrorCode &errorCode);
904 
905  void setParseError(UParseError *parseError, int32_t index);
906 
907  // No ICU "poor man's RTTI" for this class nor its subclasses.
908  virtual UClassID getDynamicClassID() const;
909 
910  UBool init(UErrorCode &errorCode);
911  UBool copyStorage(const MessagePattern &other, UErrorCode &errorCode);
912 
914  UnicodeString msg;
915  // ArrayList<Part> parts=new ArrayList<Part>();
916  MessagePatternPartsList *partsList;
917  Part *parts;
918  int32_t partsLength;
919  // ArrayList<Double> numericValues;
920  MessagePatternDoubleList *numericValuesList;
921  double *numericValues;
922  int32_t numericValuesLength;
923  UBool hasArgNames;
924  UBool hasArgNumbers;
925  UBool needsAutoQuoting;
926 };
927 
929 
930 #endif // !UCONFIG_NO_FORMATTING
931 
932 #endif // __MESSAGEPATTERN_H__
The argument is a SelectFormat with one or more (ARG_SELECTOR, message) pairs.
An integer value, for example the offset or an explicit selector value in a PluralFormat style...
MessagePattern & parseChoiceStyle(const UnicodeString &pattern, UParseError *parseError, UErrorCode &errorCode)
Parses a ChoiceFormat pattern string.
A numeric value, for example the offset or an explicit selector value in a PluralFormat style...
int32_t getLength() const
Returns the length of the pattern substring associated with this Part.
A literal apostrophe is represented by either a single or a double apostrophe pattern character...
A message pattern &quot;part&quot;, representing a pattern parsing event.
The argument name.
int32_t getIndex() const
Returns the pattern string index associated with this Part.
UBool partSubstringMatches(const Part &part, const UnicodeString &s) const
Compares the part&#39;s substring with the input string s.
C++ API: Unicode String.
UBool operator!=(const Part &other) const
The argument has no specified type.
static UBool hasNumericValue(UMessagePatternPartType type)
Indicates whether the Part type has a numeric value.
The argument style text.
UBool hasNumberedArguments() const
Does the parsed pattern have numbered arguments like {2}?
UMessagePatternPartType
MessagePattern::Part type constants.
U_EXPORT UBool operator==(const StringPiece &x, const StringPiece &y)
Global operator == for StringPiece.
The argument number, provided by the value.
UMessagePatternApostropheMode getApostropheMode() const
UnicodeString tempSubString(int32_t start=0, int32_t length=INT32_MAX) const
Create a temporary substring for the specified range.
The argument is a ChoiceFormat with one or more ((ARG_INT | ARG_DOUBLE), ARG_SELECTOR, message) tuples.
UMessagePatternArgType getArgType() const
Returns the argument type if this part is of type ARG_START or ARG_LIMIT, otherwise UMSGPAT_ARG_TYPE_...
UMessagePatternPartType getType() const
Returns the type of this part.
#define U_NAMESPACE_BEGIN
This is used to begin a declaration of a public ICU C++ API.
Definition: uversion.h:131
Return value from MessagePattern.validateArgumentName() for when the string is a valid &quot;pattern ident...
A literal apostrophe must be represented by a double apostrophe pattern character.
UMessagePatternPartType getPartType(int32_t i) const
Returns the UMessagePatternPartType of the i-th pattern &quot;part&quot;.
int32_t UChar32
Define UChar32 as a type for single Unicode code points.
Definition: umachine.h:345
UBool operator!=(const MessagePattern &other) const
Indicates that a syntax character needs to be inserted for auto-quoting.
int32_t getLimit() const
Returns the pattern string limit (exclusive-end) index associated with this Part. ...
int32_t getPatternIndex(int32_t partIndex) const
Returns the pattern index of the specified pattern &quot;part&quot;.
UMemory is the common ICU base class.
Definition: uobject.h:101
Return value from MessagePattern.validateArgumentName() for when the string is invalid.
UnicodeString is a string class that stores Unicode characters directly and provides similar function...
Definition: unistr.h:188
Start of a message pattern (main or nested).
End of an argument.
Parses and represents ICU MessageFormat patterns.
#define U_NAMESPACE_END
This is used to end a declaration of a public ICU C++ API.
Definition: uversion.h:132
UObject is the common ICU &quot;boilerplate&quot; class.
Definition: uobject.h:215
C API: Parse Error Information.
End of a message pattern (main or nested).
int32_t getValue() const
Returns a value associated with this part.
A selector substring in a &quot;complex&quot; argument style.
Indicates a substring of the pattern string which is to be skipped when formatting.
const Part & getPart(int32_t i) const
Gets the i-th pattern &quot;part&quot;.
void * UClassID
UClassID is used to identify classes without using RTTI, since RTTI is not yet supported by all C++ c...
Definition: utypes.h:385
UErrorCode
Error code to replace exception handling, so that the code is compatible with all C++ compilers...
Definition: utypes.h:639
Indicates a syntactic (non-escaped) # symbol in a plural variant.
The argument is a PluralFormat with an optional ARG_INT or ARG_DOUBLE offset (e.g., offset:1) and one or more (ARG_SELECTOR [explicit-value] message) tuples.
The argument type.
Part()
Default constructor, do not use.
UBool hasNamedArguments() const
Does the parsed pattern have named arguments like {first_name}?
UMessagePatternArgType
Argument type constants.
A UParseError struct is used to returned detailed information about parsing errors.
Definition: parseerr.h:56
const UnicodeString & getPatternString() const
Basic definitions for ICU, for both C and C++ APIs.
#define U_COMMON_API
Set to export library symbols from inside the common library, and to import them from outside...
Definition: utypes.h:520
int32_t getLimitPartIndex(int32_t start) const
Returns the index of the ARG|MSG_LIMIT part corresponding to the ARG|MSG_START at start...
int32_t countParts() const
Returns the number of &quot;parts&quot; created by parsing the pattern string.
The argument has a &quot;simple&quot; type which is provided by the ARG_TYPE part.
Start of an argument.
UnicodeString getSubstring(const Part &part) const
Returns the substring of the pattern string indicated by the Part.
int32_t hashCode() const
virtual UClassID getDynamicClassID() const =0
ICU4C &quot;poor man&#39;s RTTI&quot;, returns a UClassID for the actual ICU class.
void clearPatternAndSetApostropheMode(UMessagePatternApostropheMode mode)
Clears this MessagePattern and sets the UMessagePatternApostropheMode.
UMessagePatternApostropheMode
Mode for when an apostrophe starts quoted literal text for MessageFormat output.
int8_t UBool
The ICU boolean type.
Definition: umachine.h:228