ICU 49.1.1  49.1.1
messagepattern.h
Go to the documentation of this file.
1 /*
2 *******************************************************************************
3 * Copyright (C) 2011-2012, 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 
256 enum {
263 
272 };
273 
280 #define UMSGPAT_NO_NUMERIC_VALUE ((double)(-123456789))
281 
283 
284 class MessagePatternDoubleList;
285 class MessagePatternPartsList;
286 
344 public:
353  MessagePattern(UErrorCode &errorCode);
354 
365 
384  MessagePattern(const UnicodeString &pattern, UParseError *parseError, UErrorCode &errorCode);
385 
391  MessagePattern(const MessagePattern &other);
392 
399  MessagePattern &operator=(const MessagePattern &other);
400 
405  virtual ~MessagePattern();
406 
424  MessagePattern &parse(const UnicodeString &pattern,
425  UParseError *parseError, UErrorCode &errorCode);
426 
444  MessagePattern &parseChoiceStyle(const UnicodeString &pattern,
445  UParseError *parseError, UErrorCode &errorCode);
446 
464  MessagePattern &parsePluralStyle(const UnicodeString &pattern,
465  UParseError *parseError, UErrorCode &errorCode);
466 
484  MessagePattern &parseSelectStyle(const UnicodeString &pattern,
485  UParseError *parseError, UErrorCode &errorCode);
486 
492  void clear();
493 
501  clear();
502  aposMode=mode;
503  }
504 
510  UBool operator==(const MessagePattern &other) const;
511 
517  inline UBool operator!=(const MessagePattern &other) const {
518  return !operator==(other);
519  }
520 
525  int32_t hashCode() const;
526 
532  return aposMode;
533  }
534 
535  // Java has package-private jdkAposMode() here.
536  // In C++, this is declared in the MessageImpl class.
537 
543  return msg;
544  }
545 
552  return hasArgNames;
553  }
554 
561  return hasArgNumbers;
562  }
563 
575  static int32_t validateArgumentName(const UnicodeString &name);
576 
587  UnicodeString autoQuoteApostropheDeep() const;
588 
589  class Part;
590 
597  int32_t countParts() const {
598  return partsLength;
599  }
600 
607  const Part &getPart(int32_t i) const {
608  return parts[i];
609  }
610 
619  return getPart(i).type;
620  }
621 
629  int32_t getPatternIndex(int32_t partIndex) const {
630  return getPart(partIndex).index;
631  }
632 
640  UnicodeString getSubstring(const Part &part) const {
641  return msg.tempSubString(part.index, part.length);
642  }
643 
651  UBool partSubstringMatches(const Part &part, const UnicodeString &s) const {
652  return 0==msg.compare(part.index, part.length, s);
653  }
654 
661  double getNumericValue(const Part &part) const;
662 
669  double getPluralOffset(int32_t pluralStart) const;
670 
679  int32_t getLimitPartIndex(int32_t start) const {
680  int32_t limit=getPart(start).limitPartIndex;
681  if(limit<start) {
682  return start;
683  }
684  return limit;
685  }
686 
694  class Part : public UMemory {
695  public:
700  Part() {}
701 
708  return type;
709  }
710 
716  int32_t getIndex() const {
717  return index;
718  }
719 
726  int32_t getLength() const {
727  return length;
728  }
729 
736  int32_t getLimit() const {
737  return index+length;
738  }
739 
746  int32_t getValue() const {
747  return value;
748  }
749 
757  UMessagePatternPartType type=getType();
759  return (UMessagePatternArgType)value;
760  } else {
761  return UMSGPAT_ARG_TYPE_NONE;
762  }
763  }
764 
774  }
775 
781  UBool operator==(const Part &other) const;
782 
788  inline UBool operator!=(const Part &other) const {
789  return !operator==(other);
790  }
791 
796  int32_t hashCode() const {
797  return ((type*37+index)*37+length)*37+value;
798  }
799 
800  private:
801  friend class MessagePattern;
802 
803  static const int32_t MAX_LENGTH=0xffff;
804  static const int32_t MAX_VALUE=0x7fff;
805 
806  // Some fields are not final because they are modified during pattern parsing.
807  // After pattern parsing, the parts are effectively immutable.
809  int32_t index;
810  uint16_t length;
811  int16_t value;
812  int32_t limitPartIndex;
813  };
814 
815 private:
816  void preParse(const UnicodeString &pattern, UParseError *parseError, UErrorCode &errorCode);
817 
818  void postParse();
819 
820  int32_t parseMessage(int32_t index, int32_t msgStartLength,
821  int32_t nestingLevel, UMessagePatternArgType parentType,
822  UParseError *parseError, UErrorCode &errorCode);
823 
824  int32_t parseArg(int32_t index, int32_t argStartLength, int32_t nestingLevel,
825  UParseError *parseError, UErrorCode &errorCode);
826 
827  int32_t parseSimpleStyle(int32_t index, UParseError *parseError, UErrorCode &errorCode);
828 
829  int32_t parseChoiceStyle(int32_t index, int32_t nestingLevel,
830  UParseError *parseError, UErrorCode &errorCode);
831 
832  int32_t parsePluralOrSelectStyle(UMessagePatternArgType argType, int32_t index, int32_t nestingLevel,
833  UParseError *parseError, UErrorCode &errorCode);
834 
843  static int32_t parseArgNumber(const UnicodeString &s, int32_t start, int32_t limit);
844 
845  int32_t parseArgNumber(int32_t start, int32_t limit) {
846  return parseArgNumber(msg, start, limit);
847  }
848 
857  void parseDouble(int32_t start, int32_t limit, UBool allowInfinity,
858  UParseError *parseError, UErrorCode &errorCode);
859 
860  // Java has package-private appendReducedApostrophes() here.
861  // In C++, this is declared in the MessageImpl class.
862 
863  int32_t skipWhiteSpace(int32_t index);
864 
865  int32_t skipIdentifier(int32_t index);
866 
871  int32_t skipDouble(int32_t index);
872 
873  static UBool isArgTypeChar(UChar32 c);
874 
875  UBool isChoice(int32_t index);
876 
877  UBool isPlural(int32_t index);
878 
879  UBool isSelect(int32_t index);
880 
885  UBool inMessageFormatPattern(int32_t nestingLevel);
886 
891  UBool inTopLevelChoiceMessage(int32_t nestingLevel, UMessagePatternArgType parentType);
892 
893  void addPart(UMessagePatternPartType type, int32_t index, int32_t length,
894  int32_t value, UErrorCode &errorCode);
895 
896  void addLimitPart(int32_t start,
897  UMessagePatternPartType type, int32_t index, int32_t length,
898  int32_t value, UErrorCode &errorCode);
899 
900  void addArgDoublePart(double numericValue, int32_t start, int32_t length, UErrorCode &errorCode);
901 
902  void setParseError(UParseError *parseError, int32_t index);
903 
904  // No ICU "poor man's RTTI" for this class nor its subclasses.
905  virtual UClassID getDynamicClassID() const;
906 
907  UBool init(UErrorCode &errorCode);
908  UBool copyStorage(const MessagePattern &other, UErrorCode &errorCode);
909 
911  UnicodeString msg;
912  // ArrayList<Part> parts=new ArrayList<Part>();
913  MessagePatternPartsList *partsList;
914  Part *parts;
915  int32_t partsLength;
916  // ArrayList<Double> numericValues;
917  MessagePatternDoubleList *numericValuesList;
918  double *numericValues;
919  int32_t numericValuesLength;
920  UBool hasArgNames;
921  UBool hasArgNumbers;
922  UBool needsAutoQuoting;
923 };
924 
926 
927 #endif // !UCONFIG_NO_FORMATTING
928 
929 #endif // __MESSAGEPATTERN_H__