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