ICU 51.2  51.2
messagepattern.h
Go to the documentation of this file.
1 /*
2 *******************************************************************************
3 * Copyright (C) 2011-2013, 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 #ifndef U_HIDE_DRAFT_API
251 
257 #endif /* U_HIDE_DRAFT_API */
258 };
263 
264 #ifndef U_HIDE_DRAFT_API
265 
270 #define UMSGPAT_ARG_TYPE_HAS_PLURAL_STYLE(argType) \
271  ((argType)==UMSGPAT_ARG_TYPE_PLURAL || (argType)==UMSGPAT_ARG_TYPE_SELECTORDINAL)
272 #endif /* U_HIDE_DRAFT_API */
273 
274 enum {
281 
290 };
291 
298 #define UMSGPAT_NO_NUMERIC_VALUE ((double)(-123456789))
299 
301 
302 class MessagePatternDoubleList;
303 class MessagePatternPartsList;
304 
362 public:
371  MessagePattern(UErrorCode &errorCode);
372 
383 
402  MessagePattern(const UnicodeString &pattern, UParseError *parseError, UErrorCode &errorCode);
403 
409  MessagePattern(const MessagePattern &other);
410 
417  MessagePattern &operator=(const MessagePattern &other);
418 
423  virtual ~MessagePattern();
424 
442  MessagePattern &parse(const UnicodeString &pattern,
443  UParseError *parseError, UErrorCode &errorCode);
444 
462  MessagePattern &parseChoiceStyle(const UnicodeString &pattern,
463  UParseError *parseError, UErrorCode &errorCode);
464 
482  MessagePattern &parsePluralStyle(const UnicodeString &pattern,
483  UParseError *parseError, UErrorCode &errorCode);
484 
502  MessagePattern &parseSelectStyle(const UnicodeString &pattern,
503  UParseError *parseError, UErrorCode &errorCode);
504 
510  void clear();
511 
519  clear();
520  aposMode=mode;
521  }
522 
528  UBool operator==(const MessagePattern &other) const;
529 
535  inline UBool operator!=(const MessagePattern &other) const {
536  return !operator==(other);
537  }
538 
543  int32_t hashCode() const;
544 
550  return aposMode;
551  }
552 
553  // Java has package-private jdkAposMode() here.
554  // In C++, this is declared in the MessageImpl class.
555 
561  return msg;
562  }
563 
570  return hasArgNames;
571  }
572 
579  return hasArgNumbers;
580  }
581 
593  static int32_t validateArgumentName(const UnicodeString &name);
594 
605  UnicodeString autoQuoteApostropheDeep() const;
606 
607  class Part;
608 
615  int32_t countParts() const {
616  return partsLength;
617  }
618 
625  const Part &getPart(int32_t i) const {
626  return parts[i];
627  }
628 
637  return getPart(i).type;
638  }
639 
647  int32_t getPatternIndex(int32_t partIndex) const {
648  return getPart(partIndex).index;
649  }
650 
658  UnicodeString getSubstring(const Part &part) const {
659  return msg.tempSubString(part.index, part.length);
660  }
661 
669  UBool partSubstringMatches(const Part &part, const UnicodeString &s) const {
670  return 0==msg.compare(part.index, part.length, s);
671  }
672 
679  double getNumericValue(const Part &part) const;
680 
687  double getPluralOffset(int32_t pluralStart) const;
688 
697  int32_t getLimitPartIndex(int32_t start) const {
698  int32_t limit=getPart(start).limitPartIndex;
699  if(limit<start) {
700  return start;
701  }
702  return limit;
703  }
704 
712  class Part : public UMemory {
713  public:
718  Part() {}
719 
726  return type;
727  }
728 
734  int32_t getIndex() const {
735  return index;
736  }
737 
744  int32_t getLength() const {
745  return length;
746  }
747 
754  int32_t getLimit() const {
755  return index+length;
756  }
757 
764  int32_t getValue() const {
765  return value;
766  }
767 
775  UMessagePatternPartType type=getType();
777  return (UMessagePatternArgType)value;
778  } else {
779  return UMSGPAT_ARG_TYPE_NONE;
780  }
781  }
782 
792  }
793 
799  UBool operator==(const Part &other) const;
800 
806  inline UBool operator!=(const Part &other) const {
807  return !operator==(other);
808  }
809 
814  int32_t hashCode() const {
815  return ((type*37+index)*37+length)*37+value;
816  }
817 
818  private:
819  friend class MessagePattern;
820 
821  static const int32_t MAX_LENGTH=0xffff;
822  static const int32_t MAX_VALUE=0x7fff;
823 
824  // Some fields are not final because they are modified during pattern parsing.
825  // After pattern parsing, the parts are effectively immutable.
827  int32_t index;
828  uint16_t length;
829  int16_t value;
830  int32_t limitPartIndex;
831  };
832 
833 private:
834  void preParse(const UnicodeString &pattern, UParseError *parseError, UErrorCode &errorCode);
835 
836  void postParse();
837 
838  int32_t parseMessage(int32_t index, int32_t msgStartLength,
839  int32_t nestingLevel, UMessagePatternArgType parentType,
840  UParseError *parseError, UErrorCode &errorCode);
841 
842  int32_t parseArg(int32_t index, int32_t argStartLength, int32_t nestingLevel,
843  UParseError *parseError, UErrorCode &errorCode);
844 
845  int32_t parseSimpleStyle(int32_t index, UParseError *parseError, UErrorCode &errorCode);
846 
847  int32_t parseChoiceStyle(int32_t index, int32_t nestingLevel,
848  UParseError *parseError, UErrorCode &errorCode);
849 
850  int32_t parsePluralOrSelectStyle(UMessagePatternArgType argType, int32_t index, int32_t nestingLevel,
851  UParseError *parseError, UErrorCode &errorCode);
852 
861  static int32_t parseArgNumber(const UnicodeString &s, int32_t start, int32_t limit);
862 
863  int32_t parseArgNumber(int32_t start, int32_t limit) {
864  return parseArgNumber(msg, start, limit);
865  }
866 
875  void parseDouble(int32_t start, int32_t limit, UBool allowInfinity,
876  UParseError *parseError, UErrorCode &errorCode);
877 
878  // Java has package-private appendReducedApostrophes() here.
879  // In C++, this is declared in the MessageImpl class.
880 
881  int32_t skipWhiteSpace(int32_t index);
882 
883  int32_t skipIdentifier(int32_t index);
884 
889  int32_t skipDouble(int32_t index);
890 
891  static UBool isArgTypeChar(UChar32 c);
892 
893  UBool isChoice(int32_t index);
894 
895  UBool isPlural(int32_t index);
896 
897  UBool isSelect(int32_t index);
898 
899  UBool isOrdinal(int32_t index);
900 
905  UBool inMessageFormatPattern(int32_t nestingLevel);
906 
911  UBool inTopLevelChoiceMessage(int32_t nestingLevel, UMessagePatternArgType parentType);
912 
913  void addPart(UMessagePatternPartType type, int32_t index, int32_t length,
914  int32_t value, UErrorCode &errorCode);
915 
916  void addLimitPart(int32_t start,
917  UMessagePatternPartType type, int32_t index, int32_t length,
918  int32_t value, UErrorCode &errorCode);
919 
920  void addArgDoublePart(double numericValue, int32_t start, int32_t length, UErrorCode &errorCode);
921 
922  void setParseError(UParseError *parseError, int32_t index);
923 
924  UBool init(UErrorCode &errorCode);
925  UBool copyStorage(const MessagePattern &other, UErrorCode &errorCode);
926 
928  UnicodeString msg;
929  // ArrayList<Part> parts=new ArrayList<Part>();
930  MessagePatternPartsList *partsList;
931  Part *parts;
932  int32_t partsLength;
933  // ArrayList<Double> numericValues;
934  MessagePatternDoubleList *numericValuesList;
935  double *numericValues;
936  int32_t numericValuesLength;
937  UBool hasArgNames;
938  UBool hasArgNumbers;
939  UBool needsAutoQuoting;
940 };
941 
943 
944 #endif // !UCONFIG_NO_FORMATTING
945 
946 #endif // __MESSAGEPATTERN_H__