ICU 66.0.1  66.0.1
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 
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 
92 };
97 
207 };
212 
260 };
265 
272 #define UMSGPAT_ARG_TYPE_HAS_PLURAL_STYLE(argType) \
273  ((argType)==UMSGPAT_ARG_TYPE_PLURAL || (argType)==UMSGPAT_ARG_TYPE_SELECTORDINAL)
274 
275 enum {
282 
291 };
292 
299 #define UMSGPAT_NO_NUMERIC_VALUE ((double)(-123456789))
300 
301 U_NAMESPACE_BEGIN
302 
303 class MessagePatternDoubleList;
304 class MessagePatternPartsList;
305 
363 public:
372  MessagePattern(UErrorCode &errorCode);
373 
384 
403  MessagePattern(const UnicodeString &pattern, UParseError *parseError, UErrorCode &errorCode);
404 
410  MessagePattern(const MessagePattern &other);
411 
418  MessagePattern &operator=(const MessagePattern &other);
419 
424  virtual ~MessagePattern();
425 
443  MessagePattern &parse(const UnicodeString &pattern,
444  UParseError *parseError, UErrorCode &errorCode);
445 
463  MessagePattern &parseChoiceStyle(const UnicodeString &pattern,
464  UParseError *parseError, UErrorCode &errorCode);
465 
483  MessagePattern &parsePluralStyle(const UnicodeString &pattern,
484  UParseError *parseError, UErrorCode &errorCode);
485 
503  MessagePattern &parseSelectStyle(const UnicodeString &pattern,
504  UParseError *parseError, UErrorCode &errorCode);
505 
511  void clear();
512 
520  clear();
521  aposMode=mode;
522  }
523 
529  UBool operator==(const MessagePattern &other) const;
530 
536  inline UBool operator!=(const MessagePattern &other) const {
537  return !operator==(other);
538  }
539 
544  int32_t hashCode() const;
545 
551  return aposMode;
552  }
553 
554  // Java has package-private jdkAposMode() here.
555  // In C++, this is declared in the MessageImpl class.
556 
562  return msg;
563  }
564 
571  return hasArgNames;
572  }
573 
580  return hasArgNumbers;
581  }
582 
594  static int32_t validateArgumentName(const UnicodeString &name);
595 
606  UnicodeString autoQuoteApostropheDeep() const;
607 
608  class Part;
609 
616  int32_t countParts() const {
617  return partsLength;
618  }
619 
626  const Part &getPart(int32_t i) const {
627  return parts[i];
628  }
629 
638  return getPart(i).type;
639  }
640 
648  int32_t getPatternIndex(int32_t partIndex) const {
649  return getPart(partIndex).index;
650  }
651 
659  UnicodeString getSubstring(const Part &part) const {
660  return msg.tempSubString(part.index, part.length);
661  }
662 
670  UBool partSubstringMatches(const Part &part, const UnicodeString &s) const {
671  return 0==msg.compare(part.index, part.length, s);
672  }
673 
680  double getNumericValue(const Part &part) const;
681 
688  double getPluralOffset(int32_t pluralStart) const;
689 
698  int32_t getLimitPartIndex(int32_t start) const {
699  int32_t limit=getPart(start).limitPartIndex;
700  if(limit<start) {
701  return start;
702  }
703  return limit;
704  }
705 
713  class Part : public UMemory {
714  public:
719  Part() {}
720 
727  return type;
728  }
729 
735  int32_t getIndex() const {
736  return index;
737  }
738 
745  int32_t getLength() const {
746  return length;
747  }
748 
755  int32_t getLimit() const {
756  return index+length;
757  }
758 
765  int32_t getValue() const {
766  return value;
767  }
768 
776  UMessagePatternPartType msgType=getType();
777  if(msgType ==UMSGPAT_PART_TYPE_ARG_START || msgType ==UMSGPAT_PART_TYPE_ARG_LIMIT) {
778  return (UMessagePatternArgType)value;
779  } else {
780  return UMSGPAT_ARG_TYPE_NONE;
781  }
782  }
783 
793  }
794 
800  UBool operator==(const Part &other) const;
801 
807  inline UBool operator!=(const Part &other) const {
808  return !operator==(other);
809  }
810 
815  int32_t hashCode() const {
816  return ((type*37+index)*37+length)*37+value;
817  }
818 
819  private:
820  friend class MessagePattern;
821 
822  static const int32_t MAX_LENGTH=0xffff;
823  static const int32_t MAX_VALUE=0x7fff;
824 
825  // Some fields are not final because they are modified during pattern parsing.
826  // After pattern parsing, the parts are effectively immutable.
828  int32_t index;
829  uint16_t length;
830  int16_t value;
831  int32_t limitPartIndex;
832  };
833 
834 private:
835  void preParse(const UnicodeString &pattern, UParseError *parseError, UErrorCode &errorCode);
836 
837  void postParse();
838 
839  int32_t parseMessage(int32_t index, int32_t msgStartLength,
840  int32_t nestingLevel, UMessagePatternArgType parentType,
841  UParseError *parseError, UErrorCode &errorCode);
842 
843  int32_t parseArg(int32_t index, int32_t argStartLength, int32_t nestingLevel,
844  UParseError *parseError, UErrorCode &errorCode);
845 
846  int32_t parseSimpleStyle(int32_t index, UParseError *parseError, UErrorCode &errorCode);
847 
848  int32_t parseChoiceStyle(int32_t index, int32_t nestingLevel,
849  UParseError *parseError, UErrorCode &errorCode);
850 
851  int32_t parsePluralOrSelectStyle(UMessagePatternArgType argType, int32_t index, int32_t nestingLevel,
852  UParseError *parseError, UErrorCode &errorCode);
853 
862  static int32_t parseArgNumber(const UnicodeString &s, int32_t start, int32_t limit);
863 
864  int32_t parseArgNumber(int32_t start, int32_t limit) {
865  return parseArgNumber(msg, start, limit);
866  }
867 
876  void parseDouble(int32_t start, int32_t limit, UBool allowInfinity,
877  UParseError *parseError, UErrorCode &errorCode);
878 
879  // Java has package-private appendReducedApostrophes() here.
880  // In C++, this is declared in the MessageImpl class.
881 
882  int32_t skipWhiteSpace(int32_t index);
883 
884  int32_t skipIdentifier(int32_t index);
885 
890  int32_t skipDouble(int32_t index);
891 
892  static UBool isArgTypeChar(UChar32 c);
893 
894  UBool isChoice(int32_t index);
895 
896  UBool isPlural(int32_t index);
897 
898  UBool isSelect(int32_t index);
899 
900  UBool isOrdinal(int32_t index);
901 
906  UBool inMessageFormatPattern(int32_t nestingLevel);
907 
912  UBool inTopLevelChoiceMessage(int32_t nestingLevel, UMessagePatternArgType parentType);
913 
914  void addPart(UMessagePatternPartType type, int32_t index, int32_t length,
915  int32_t value, UErrorCode &errorCode);
916 
917  void addLimitPart(int32_t start,
918  UMessagePatternPartType type, int32_t index, int32_t length,
919  int32_t value, UErrorCode &errorCode);
920 
921  void addArgDoublePart(double numericValue, int32_t start, int32_t length, UErrorCode &errorCode);
922 
923  void setParseError(UParseError *parseError, int32_t index);
924 
925  UBool init(UErrorCode &errorCode);
926  UBool copyStorage(const MessagePattern &other, UErrorCode &errorCode);
927 
929  UnicodeString msg;
930  // ArrayList<Part> parts=new ArrayList<Part>();
931  MessagePatternPartsList *partsList;
932  Part *parts;
933  int32_t partsLength;
934  // ArrayList<Double> numericValues;
935  MessagePatternDoubleList *numericValuesList;
936  double *numericValues;
937  int32_t numericValuesLength;
938  UBool hasArgNames;
939  UBool hasArgNumbers;
940  UBool needsAutoQuoting;
941 };
942 
943 U_NAMESPACE_END
944 
945 #endif // !UCONFIG_NO_FORMATTING
946 
947 #endif /* U_SHOW_CPLUSPLUS_API */
948 
949 #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...
const UnicodeString & getPatternString() const
A numeric value, for example the offset or an explicit selector value in a PluralFormat style...
A literal apostrophe is represented by either a single or a double apostrophe pattern character...
The argument name.
const Part & getPart(int32_t i) const
Gets the i-th pattern "part".
UnicodeString tempSubString(int32_t start=0, int32_t length=INT32_MAX) const
Create a temporary substring for the specified range.
C++ API: Unicode String.
U_EXPORT UBool operator==(const StringPiece &x, const StringPiece &y)
Global operator == for StringPiece.
The argument has no specified type.
The argument style text.
static UBool hasNumericValue(UMessagePatternPartType type)
Indicates whether the Part type has a numeric value.
UMessagePatternPartType
MessagePattern::Part type constants.
The argument number, provided by the value.
int32_t getValue() const
Returns a value associated with this part.
The argument is an ordinal-number PluralFormat with the same style parts sequence and semantics as UM...
void clearPatternAndSetApostropheMode(UMessagePatternApostropheMode mode)
Clears this MessagePattern and sets the UMessagePatternApostropheMode.
Parses and represents ICU MessageFormat patterns.
The argument is a ChoiceFormat with one or more ((ARG_INT | ARG_DOUBLE), ARG_SELECTOR, message) tuples.
Return value from MessagePattern.validateArgumentName() for when the string is a valid "pattern ident...
int32_t getPatternIndex(int32_t partIndex) const
Returns the pattern index of the specified pattern "part".
A literal apostrophe must be represented by a double apostrophe pattern character.
UMessagePatternArgType getArgType() const
Returns the argument type if this part is of type ARG_START or ARG_LIMIT, otherwise UMSGPAT_ARG_TYPE_...
UMessagePatternApostropheMode getApostropheMode() const
int32_t UChar32
Define UChar32 as a type for single Unicode code points.
Definition: umachine.h:425
UBool partSubstringMatches(const Part &part, const UnicodeString &s) const
Compares the part&#39;s substring with the input string s.
Indicates that a syntax character needs to be inserted for auto-quoting.
int32_t countParts() const
Returns the number of "parts" created by parsing the pattern string.
int32_t getLimit() const
Returns the pattern string limit (exclusive-end) index associated with this Part. ...
Return value from MessagePattern.validateArgumentName() for when the string is invalid.
UBool operator!=(const Part &other) const
UBool operator!=(const MessagePattern &other) const
Start of a message pattern (main or nested).
End of an argument.
UBool hasNumberedArguments() const
Does the parsed pattern have numbered arguments like {2}?
UMessagePatternPartType getType() const
Returns the type of this part.
C API: Parse Error Information.
End of a message pattern (main or nested).
A selector substring in a "complex" argument style.
Indicates a substring of the pattern string which is to be skipped when formatting.
UErrorCode
Standard ICU4C error code type, a substitute for exceptions.
Definition: utypes.h:415
Part()
Default constructor, do not use.
A message pattern "part", representing a pattern parsing event.
Indicates a syntactic (non-escaped) # symbol in a plural variant.
UMessagePatternPartType getPartType(int32_t i) const
Returns the UMessagePatternPartType of the i-th pattern "part".
The argument is a cardinal-number PluralFormat with an optional ARG_INT or ARG_DOUBLE offset (e...
int32_t getIndex() const
Returns the pattern string index associated with this Part.
The argument type.
UMessagePatternArgType
Argument type constants.
A UParseError struct is used to returned detailed information about parsing errors.
Definition: parseerr.h:58
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:300
int32_t getLimitPartIndex(int32_t start) const
Returns the index of the ARG|MSG_LIMIT part corresponding to the ARG|MSG_START at start...
UBool hasNamedArguments() const
Does the parsed pattern have named arguments like {first_name}?
UnicodeString is a string class that stores Unicode characters directly and provides similar function...
Definition: unistr.h:294
int32_t getLength() const
Returns the length of the pattern substring associated with this Part.
The argument has a "simple" 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.
UObject is the common ICU "boilerplate" class.
Definition: uobject.h:223
UMemory is the common ICU base class.
Definition: uobject.h:115
UMessagePatternApostropheMode
Mode for when an apostrophe starts quoted literal text for MessageFormat output.
int8_t UBool
The ICU boolean type.
Definition: umachine.h:261