dtptngen_impl.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. // © 2016 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  3. /*
  4. *******************************************************************************
  5. * Copyright (C) 2007-2016, International Business Machines Corporation and
  6. * others. All Rights Reserved.
  7. *******************************************************************************
  8. *
  9. * File DTPTNGEN.H
  10. *
  11. *******************************************************************************
  12. */
  13. #ifndef __DTPTNGEN_IMPL_H__
  14. #define __DTPTNGEN_IMPL_H__
  15. #include "unicode/udatpg.h"
  16. #include "unicode/strenum.h"
  17. #include "unicode/unistr.h"
  18. #include "uvector.h"
  19. // TODO(claireho): Split off Builder class.
  20. // TODO(claireho): If splitting off Builder class: As subclass or independent?
  21. #define MAX_PATTERN_ENTRIES 52
  22. #define MAX_CLDR_FIELD_LEN 60
  23. #define MAX_DT_TOKEN 50
  24. #define MAX_RESOURCE_FIELD 12
  25. #define MAX_AVAILABLE_FORMATS 12
  26. #define NONE 0
  27. #define EXTRA_FIELD 0x10000
  28. #define MISSING_FIELD 0x1000
  29. #define MAX_STRING_ENUMERATION 200
  30. #define SINGLE_QUOTE ((UChar)0x0027)
  31. #define FORWARDSLASH ((UChar)0x002F)
  32. #define BACKSLASH ((UChar)0x005C)
  33. #define SPACE ((UChar)0x0020)
  34. #define QUOTATION_MARK ((UChar)0x0022)
  35. #define ASTERISK ((UChar)0x002A)
  36. #define PLUSSITN ((UChar)0x002B)
  37. #define COMMA ((UChar)0x002C)
  38. #define HYPHEN ((UChar)0x002D)
  39. #define DOT ((UChar)0x002E)
  40. #define COLON ((UChar)0x003A)
  41. #define CAP_A ((UChar)0x0041)
  42. #define CAP_B ((UChar)0x0042)
  43. #define CAP_C ((UChar)0x0043)
  44. #define CAP_D ((UChar)0x0044)
  45. #define CAP_E ((UChar)0x0045)
  46. #define CAP_F ((UChar)0x0046)
  47. #define CAP_G ((UChar)0x0047)
  48. #define CAP_H ((UChar)0x0048)
  49. #define CAP_J ((UChar)0x004A)
  50. #define CAP_K ((UChar)0x004B)
  51. #define CAP_L ((UChar)0x004C)
  52. #define CAP_M ((UChar)0x004D)
  53. #define CAP_O ((UChar)0x004F)
  54. #define CAP_Q ((UChar)0x0051)
  55. #define CAP_S ((UChar)0x0053)
  56. #define CAP_T ((UChar)0x0054)
  57. #define CAP_U ((UChar)0x0055)
  58. #define CAP_V ((UChar)0x0056)
  59. #define CAP_W ((UChar)0x0057)
  60. #define CAP_X ((UChar)0x0058)
  61. #define CAP_Y ((UChar)0x0059)
  62. #define CAP_Z ((UChar)0x005A)
  63. #define LOWLINE ((UChar)0x005F)
  64. #define LOW_A ((UChar)0x0061)
  65. #define LOW_B ((UChar)0x0062)
  66. #define LOW_C ((UChar)0x0063)
  67. #define LOW_D ((UChar)0x0064)
  68. #define LOW_E ((UChar)0x0065)
  69. #define LOW_F ((UChar)0x0066)
  70. #define LOW_G ((UChar)0x0067)
  71. #define LOW_H ((UChar)0x0068)
  72. #define LOW_I ((UChar)0x0069)
  73. #define LOW_J ((UChar)0x006A)
  74. #define LOW_K ((UChar)0x006B)
  75. #define LOW_L ((UChar)0x006C)
  76. #define LOW_M ((UChar)0x006D)
  77. #define LOW_N ((UChar)0x006E)
  78. #define LOW_O ((UChar)0x006F)
  79. #define LOW_P ((UChar)0x0070)
  80. #define LOW_Q ((UChar)0x0071)
  81. #define LOW_R ((UChar)0x0072)
  82. #define LOW_S ((UChar)0x0073)
  83. #define LOW_T ((UChar)0x0074)
  84. #define LOW_U ((UChar)0x0075)
  85. #define LOW_V ((UChar)0x0076)
  86. #define LOW_W ((UChar)0x0077)
  87. #define LOW_X ((UChar)0x0078)
  88. #define LOW_Y ((UChar)0x0079)
  89. #define LOW_Z ((UChar)0x007A)
  90. #define DT_NARROW -0x101
  91. #define DT_SHORTER -0x102
  92. #define DT_SHORT -0x103
  93. #define DT_LONG -0x104
  94. #define DT_NUMERIC 0x100
  95. #define DT_DELTA 0x10
  96. U_NAMESPACE_BEGIN
  97. const int32_t UDATPG_FRACTIONAL_MASK = 1<<UDATPG_FRACTIONAL_SECOND_FIELD;
  98. const int32_t UDATPG_SECOND_AND_FRACTIONAL_MASK = (1<<UDATPG_SECOND_FIELD) | (1<<UDATPG_FRACTIONAL_SECOND_FIELD);
  99. typedef enum dtStrEnum {
  100. DT_BASESKELETON,
  101. DT_SKELETON,
  102. DT_PATTERN
  103. }dtStrEnum;
  104. typedef struct dtTypeElem {
  105. UChar patternChar;
  106. UDateTimePatternField field;
  107. int16_t type;
  108. int16_t minLen;
  109. int16_t weight;
  110. } dtTypeElem;
  111. // A compact storage mechanism for skeleton field strings. Several dozen of these will be created
  112. // for a typical DateTimePatternGenerator instance.
  113. class SkeletonFields : public UMemory {
  114. public:
  115. SkeletonFields();
  116. void clear();
  117. void copyFrom(const SkeletonFields& other);
  118. void clearField(int32_t field);
  119. UChar getFieldChar(int32_t field) const;
  120. int32_t getFieldLength(int32_t field) const;
  121. void populate(int32_t field, const UnicodeString& value);
  122. void populate(int32_t field, UChar repeatChar, int32_t repeatCount);
  123. UBool isFieldEmpty(int32_t field) const;
  124. UnicodeString& appendTo(UnicodeString& string) const;
  125. UnicodeString& appendFieldTo(int32_t field, UnicodeString& string) const;
  126. UChar getFirstChar() const;
  127. inline UBool operator==(const SkeletonFields& other) const;
  128. inline UBool operator!=(const SkeletonFields& other) const;
  129. private:
  130. int8_t chars[UDATPG_FIELD_COUNT];
  131. int8_t lengths[UDATPG_FIELD_COUNT];
  132. };
  133. inline UBool SkeletonFields::operator==(const SkeletonFields& other) const {
  134. return (uprv_memcmp(chars, other.chars, sizeof(chars)) == 0
  135. && uprv_memcmp(lengths, other.lengths, sizeof(lengths)) == 0);
  136. }
  137. inline UBool SkeletonFields::operator!=(const SkeletonFields& other) const {
  138. return (! operator==(other));
  139. }
  140. class PtnSkeleton : public UMemory {
  141. public:
  142. int32_t type[UDATPG_FIELD_COUNT];
  143. SkeletonFields original;
  144. SkeletonFields baseOriginal;
  145. UBool addedDefaultDayPeriod;
  146. PtnSkeleton();
  147. PtnSkeleton(const PtnSkeleton& other);
  148. void copyFrom(const PtnSkeleton& other);
  149. void clear();
  150. UBool equals(const PtnSkeleton& other) const;
  151. UnicodeString getSkeleton() const;
  152. UnicodeString getBaseSkeleton() const;
  153. UChar getFirstChar() const;
  154. // TODO: Why is this virtual, as well as the other destructors in this file? We don't want
  155. // vtables when we don't use class objects polymorphically.
  156. virtual ~PtnSkeleton();
  157. };
  158. class PtnElem : public UMemory {
  159. public:
  160. UnicodeString basePattern;
  161. LocalPointer<PtnSkeleton> skeleton;
  162. UnicodeString pattern;
  163. UBool skeletonWasSpecified; // if specified in availableFormats, not derived
  164. LocalPointer<PtnElem> next;
  165. PtnElem(const UnicodeString &basePattern, const UnicodeString &pattern);
  166. virtual ~PtnElem();
  167. };
  168. class FormatParser : public UMemory {
  169. public:
  170. UnicodeString items[MAX_DT_TOKEN];
  171. int32_t itemNumber;
  172. FormatParser();
  173. virtual ~FormatParser();
  174. void set(const UnicodeString& patternString);
  175. void getQuoteLiteral(UnicodeString& quote, int32_t *itemIndex);
  176. UBool isPatternSeparator(const UnicodeString& field) const;
  177. static UBool isQuoteLiteral(const UnicodeString& s);
  178. static int32_t getCanonicalIndex(const UnicodeString& s) { return getCanonicalIndex(s, TRUE); }
  179. static int32_t getCanonicalIndex(const UnicodeString& s, UBool strict);
  180. private:
  181. typedef enum TokenStatus {
  182. START,
  183. ADD_TOKEN,
  184. SYNTAX_ERROR,
  185. DONE
  186. } TokenStatus;
  187. TokenStatus status;
  188. virtual TokenStatus setTokens(const UnicodeString& pattern, int32_t startPos, int32_t *len);
  189. };
  190. class DistanceInfo : public UMemory {
  191. public:
  192. int32_t missingFieldMask;
  193. int32_t extraFieldMask;
  194. DistanceInfo() {}
  195. virtual ~DistanceInfo();
  196. void clear() { missingFieldMask = extraFieldMask = 0; }
  197. void setTo(const DistanceInfo& other);
  198. void addMissing(int32_t field) { missingFieldMask |= (1<<field); }
  199. void addExtra(int32_t field) { extraFieldMask |= (1<<field); }
  200. };
  201. class DateTimeMatcher: public UMemory {
  202. public:
  203. PtnSkeleton skeleton;
  204. void getBasePattern(UnicodeString& basePattern);
  205. UnicodeString getPattern();
  206. void set(const UnicodeString& pattern, FormatParser* fp);
  207. void set(const UnicodeString& pattern, FormatParser* fp, PtnSkeleton& skeleton);
  208. void copyFrom(const PtnSkeleton& skeleton);
  209. void copyFrom();
  210. PtnSkeleton* getSkeletonPtr();
  211. UBool equals(const DateTimeMatcher* other) const;
  212. int32_t getDistance(const DateTimeMatcher& other, int32_t includeMask, DistanceInfo& distanceInfo) const;
  213. DateTimeMatcher();
  214. DateTimeMatcher(const DateTimeMatcher& other);
  215. DateTimeMatcher& operator=(const DateTimeMatcher& other);
  216. virtual ~DateTimeMatcher();
  217. int32_t getFieldMask() const;
  218. };
  219. class PatternMap : public UMemory {
  220. public:
  221. PtnElem *boot[MAX_PATTERN_ENTRIES];
  222. PatternMap();
  223. virtual ~PatternMap();
  224. void add(const UnicodeString& basePattern, const PtnSkeleton& skeleton, const UnicodeString& value, UBool skeletonWasSpecified, UErrorCode& status);
  225. const UnicodeString* getPatternFromBasePattern(const UnicodeString& basePattern, UBool& skeletonWasSpecified) const;
  226. const UnicodeString* getPatternFromSkeleton(const PtnSkeleton& skeleton, const PtnSkeleton** specifiedSkeletonPtr = 0) const;
  227. void copyFrom(const PatternMap& other, UErrorCode& status);
  228. PtnElem* getHeader(UChar baseChar) const;
  229. UBool equals(const PatternMap& other) const;
  230. private:
  231. UBool isDupAllowed;
  232. PtnElem* getDuplicateElem(const UnicodeString& basePattern, const PtnSkeleton& skeleton, PtnElem *baseElem);
  233. }; // end PatternMap
  234. class PatternMapIterator : public UMemory {
  235. public:
  236. PatternMapIterator(UErrorCode &status);
  237. virtual ~PatternMapIterator();
  238. void set(PatternMap& patternMap);
  239. PtnSkeleton* getSkeleton() const;
  240. UBool hasNext() const;
  241. DateTimeMatcher& next();
  242. private:
  243. int32_t bootIndex;
  244. PtnElem *nodePtr;
  245. LocalPointer<DateTimeMatcher> matcher;
  246. PatternMap *patternMap;
  247. };
  248. class DTSkeletonEnumeration : public StringEnumeration {
  249. public:
  250. DTSkeletonEnumeration(PatternMap& patternMap, dtStrEnum type, UErrorCode& status);
  251. virtual ~DTSkeletonEnumeration();
  252. static UClassID U_EXPORT2 getStaticClassID(void);
  253. virtual UClassID getDynamicClassID(void) const;
  254. virtual const UnicodeString* snext(UErrorCode& status);
  255. virtual void reset(UErrorCode& status);
  256. virtual int32_t count(UErrorCode& status) const;
  257. private:
  258. int32_t pos;
  259. UBool isCanonicalItem(const UnicodeString& item);
  260. LocalPointer<UVector> fSkeletons;
  261. };
  262. class DTRedundantEnumeration : public StringEnumeration {
  263. public:
  264. DTRedundantEnumeration();
  265. virtual ~DTRedundantEnumeration();
  266. static UClassID U_EXPORT2 getStaticClassID(void);
  267. virtual UClassID getDynamicClassID(void) const;
  268. virtual const UnicodeString* snext(UErrorCode& status);
  269. virtual void reset(UErrorCode& status);
  270. virtual int32_t count(UErrorCode& status) const;
  271. void add(const UnicodeString &pattern, UErrorCode& status);
  272. private:
  273. int32_t pos;
  274. UBool isCanonicalItem(const UnicodeString& item) const;
  275. LocalPointer<UVector> fPatterns;
  276. };
  277. U_NAMESPACE_END
  278. #endif