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