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__