2007-08-03 18:43:04 +00:00
|
|
|
/*
|
|
|
|
*******************************************************************************
|
2012-01-17 08:06:29 +00:00
|
|
|
* Copyright (C) 2007-2012, International Business Machines Corporation and
|
2007-08-03 18:43:04 +00:00
|
|
|
* others. All Rights Reserved.
|
|
|
|
*******************************************************************************
|
|
|
|
*
|
|
|
|
* File DTPTNGEN.CPP
|
|
|
|
*
|
|
|
|
*******************************************************************************
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "unicode/utypes.h"
|
|
|
|
#if !UCONFIG_NO_FORMATTING
|
|
|
|
|
|
|
|
#include "unicode/datefmt.h"
|
|
|
|
#include "unicode/decimfmt.h"
|
|
|
|
#include "unicode/dtfmtsym.h"
|
|
|
|
#include "unicode/dtptngen.h"
|
|
|
|
#include "unicode/msgfmt.h"
|
|
|
|
#include "unicode/smpdtfmt.h"
|
|
|
|
#include "unicode/udat.h"
|
|
|
|
#include "unicode/udatpg.h"
|
|
|
|
#include "unicode/uniset.h"
|
2008-11-19 04:19:02 +00:00
|
|
|
#include "unicode/uloc.h"
|
2007-08-03 18:43:04 +00:00
|
|
|
#include "unicode/ures.h"
|
2009-03-20 00:08:24 +00:00
|
|
|
#include "unicode/ustring.h"
|
2007-08-03 18:43:04 +00:00
|
|
|
#include "unicode/rep.h"
|
|
|
|
#include "cpputils.h"
|
|
|
|
#include "ucln_in.h"
|
|
|
|
#include "mutex.h"
|
|
|
|
#include "cmemory.h"
|
|
|
|
#include "cstring.h"
|
|
|
|
#include "locbased.h"
|
|
|
|
#include "gregoimp.h"
|
|
|
|
#include "hash.h"
|
|
|
|
#include "uresimp.h"
|
|
|
|
#include "dtptngen_impl.h"
|
|
|
|
|
|
|
|
#define LENGTHOF(array) (int32_t)(sizeof(array)/sizeof((array)[0]))
|
|
|
|
|
2008-06-28 07:02:26 +00:00
|
|
|
#if U_CHARSET_FAMILY==U_EBCDIC_FAMILY
|
|
|
|
/**
|
2009-04-22 23:10:07 +00:00
|
|
|
* If we are on EBCDIC, use an iterator which will
|
2008-06-28 07:02:26 +00:00
|
|
|
* traverse the bundles in ASCII order.
|
|
|
|
*/
|
|
|
|
#define U_USE_ASCII_BUNDLE_ITERATOR
|
|
|
|
#define U_SORT_ASCII_BUNDLE_ITERATOR
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(U_USE_ASCII_BUNDLE_ITERATOR)
|
|
|
|
|
|
|
|
#include "unicode/ustring.h"
|
|
|
|
#include "uarrsort.h"
|
|
|
|
|
|
|
|
struct UResAEntry {
|
|
|
|
UChar *key;
|
|
|
|
UResourceBundle *item;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct UResourceBundleAIterator {
|
|
|
|
UResourceBundle *bund;
|
|
|
|
UResAEntry *entries;
|
|
|
|
int32_t num;
|
|
|
|
int32_t cursor;
|
|
|
|
};
|
|
|
|
|
2008-10-01 19:23:06 +00:00
|
|
|
/* Must be C linkage to pass function pointer to the sort function */
|
|
|
|
|
2011-07-23 00:10:30 +00:00
|
|
|
U_CDECL_BEGIN
|
|
|
|
|
2009-07-18 02:35:27 +00:00
|
|
|
static int32_t U_CALLCONV
|
2008-06-28 07:02:26 +00:00
|
|
|
ures_a_codepointSort(const void *context, const void *left, const void *right) {
|
|
|
|
//CompareContext *cmp=(CompareContext *)context;
|
|
|
|
return u_strcmp(((const UResAEntry *)left)->key,
|
|
|
|
((const UResAEntry *)right)->key);
|
|
|
|
}
|
|
|
|
|
2011-07-23 00:10:30 +00:00
|
|
|
U_CDECL_END
|
2008-06-28 07:02:26 +00:00
|
|
|
|
|
|
|
static void ures_a_open(UResourceBundleAIterator *aiter, UResourceBundle *bund, UErrorCode *status) {
|
|
|
|
if(U_FAILURE(*status)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
aiter->bund = bund;
|
|
|
|
aiter->num = ures_getSize(aiter->bund);
|
|
|
|
aiter->cursor = 0;
|
|
|
|
#if !defined(U_SORT_ASCII_BUNDLE_ITERATOR)
|
|
|
|
aiter->entries = NULL;
|
|
|
|
#else
|
|
|
|
aiter->entries = (UResAEntry*)uprv_malloc(sizeof(UResAEntry)*aiter->num);
|
|
|
|
for(int i=0;i<aiter->num;i++) {
|
|
|
|
aiter->entries[i].item = ures_getByIndex(aiter->bund, i, NULL, status);
|
|
|
|
const char *akey = ures_getKey(aiter->entries[i].item);
|
|
|
|
int32_t len = uprv_strlen(akey)+1;
|
|
|
|
aiter->entries[i].key = (UChar*)uprv_malloc(len*sizeof(UChar));
|
|
|
|
u_charsToUChars(akey, aiter->entries[i].key, len);
|
|
|
|
}
|
|
|
|
uprv_sortArray(aiter->entries, aiter->num, sizeof(UResAEntry), ures_a_codepointSort, NULL, TRUE, status);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ures_a_close(UResourceBundleAIterator *aiter) {
|
|
|
|
#if defined(U_SORT_ASCII_BUNDLE_ITERATOR)
|
|
|
|
for(int i=0;i<aiter->num;i++) {
|
|
|
|
uprv_free(aiter->entries[i].key);
|
|
|
|
ures_close(aiter->entries[i].item);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static const UChar *ures_a_getNextString(UResourceBundleAIterator *aiter, int32_t *len, const char **key, UErrorCode *err) {
|
|
|
|
#if !defined(U_SORT_ASCII_BUNDLE_ITERATOR)
|
|
|
|
return ures_getNextString(aiter->bund, len, key, err);
|
|
|
|
#else
|
|
|
|
if(U_FAILURE(*err)) return NULL;
|
|
|
|
UResourceBundle *item = aiter->entries[aiter->cursor].item;
|
|
|
|
const UChar* ret = ures_getString(item, len, err);
|
|
|
|
*key = ures_getKey(item);
|
|
|
|
aiter->cursor++;
|
|
|
|
return ret;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2007-08-03 18:43:04 +00:00
|
|
|
U_NAMESPACE_BEGIN
|
|
|
|
|
2008-06-28 07:02:26 +00:00
|
|
|
|
2007-08-03 18:43:04 +00:00
|
|
|
// *****************************************************************************
|
|
|
|
// class DateTimePatternGenerator
|
|
|
|
// *****************************************************************************
|
|
|
|
static const UChar Canonical_Items[] = {
|
2010-10-20 22:06:10 +00:00
|
|
|
// GyQMwWEdDFHmsSv
|
|
|
|
CAP_G, LOW_Y, CAP_Q, CAP_M, LOW_W, CAP_W, CAP_E, LOW_D, CAP_D, CAP_F,
|
2007-08-03 18:43:04 +00:00
|
|
|
CAP_H, LOW_M, LOW_S, CAP_S, LOW_V, 0
|
|
|
|
};
|
|
|
|
|
|
|
|
static const dtTypeElem dtTypes[] = {
|
|
|
|
// patternChar, field, type, minLen, weight
|
|
|
|
{CAP_G, UDATPG_ERA_FIELD, DT_SHORT, 1, 3,},
|
2007-08-04 02:44:11 +00:00
|
|
|
{CAP_G, UDATPG_ERA_FIELD, DT_LONG, 4, 0},
|
2007-08-03 18:43:04 +00:00
|
|
|
{LOW_Y, UDATPG_YEAR_FIELD, DT_NUMERIC, 1, 20},
|
|
|
|
{CAP_Y, UDATPG_YEAR_FIELD, DT_NUMERIC + DT_DELTA, 1, 20},
|
|
|
|
{LOW_U, UDATPG_YEAR_FIELD, DT_NUMERIC + 2*DT_DELTA, 1, 20},
|
2012-01-17 08:06:29 +00:00
|
|
|
{CAP_U, UDATPG_YEAR_FIELD, DT_SHORT, 1, 3},
|
|
|
|
{CAP_U, UDATPG_YEAR_FIELD, DT_LONG, 4, 0},
|
|
|
|
{CAP_U, UDATPG_YEAR_FIELD, DT_NARROW, 5, 0},
|
2007-08-03 18:43:04 +00:00
|
|
|
{CAP_Q, UDATPG_QUARTER_FIELD, DT_NUMERIC, 1, 2},
|
2007-08-04 02:44:11 +00:00
|
|
|
{CAP_Q, UDATPG_QUARTER_FIELD, DT_SHORT, 3, 0},
|
|
|
|
{CAP_Q, UDATPG_QUARTER_FIELD, DT_LONG, 4, 0},
|
2009-04-22 16:28:25 +00:00
|
|
|
{LOW_Q, UDATPG_QUARTER_FIELD, DT_NUMERIC + DT_DELTA, 1, 2},
|
|
|
|
{LOW_Q, UDATPG_QUARTER_FIELD, DT_SHORT + DT_DELTA, 3, 0},
|
|
|
|
{LOW_Q, UDATPG_QUARTER_FIELD, DT_LONG + DT_DELTA, 4, 0},
|
2007-08-03 18:43:04 +00:00
|
|
|
{CAP_M, UDATPG_MONTH_FIELD, DT_NUMERIC, 1, 2},
|
2007-08-04 02:44:11 +00:00
|
|
|
{CAP_M, UDATPG_MONTH_FIELD, DT_SHORT, 3, 0},
|
|
|
|
{CAP_M, UDATPG_MONTH_FIELD, DT_LONG, 4, 0},
|
|
|
|
{CAP_M, UDATPG_MONTH_FIELD, DT_NARROW, 5, 0},
|
2007-08-03 18:43:04 +00:00
|
|
|
{CAP_L, UDATPG_MONTH_FIELD, DT_NUMERIC + DT_DELTA, 1, 2},
|
2007-08-04 02:44:11 +00:00
|
|
|
{CAP_L, UDATPG_MONTH_FIELD, DT_SHORT - DT_DELTA, 3, 0},
|
|
|
|
{CAP_L, UDATPG_MONTH_FIELD, DT_LONG - DT_DELTA, 4, 0},
|
|
|
|
{CAP_L, UDATPG_MONTH_FIELD, DT_NARROW - DT_DELTA, 5, 0},
|
2011-04-05 05:41:43 +00:00
|
|
|
{LOW_L, UDATPG_MONTH_FIELD, DT_NUMERIC + DT_DELTA, 1, 1},
|
2007-08-03 18:43:04 +00:00
|
|
|
{LOW_W, UDATPG_WEEK_OF_YEAR_FIELD, DT_NUMERIC, 1, 2},
|
2007-08-04 02:44:11 +00:00
|
|
|
{CAP_W, UDATPG_WEEK_OF_MONTH_FIELD, DT_NUMERIC + DT_DELTA, 1, 0},
|
2007-08-03 18:43:04 +00:00
|
|
|
{CAP_E, UDATPG_WEEKDAY_FIELD, DT_SHORT, 1, 3},
|
2007-08-04 02:44:11 +00:00
|
|
|
{CAP_E, UDATPG_WEEKDAY_FIELD, DT_LONG, 4, 0},
|
|
|
|
{CAP_E, UDATPG_WEEKDAY_FIELD, DT_NARROW, 5, 0},
|
2007-08-03 18:43:04 +00:00
|
|
|
{LOW_C, UDATPG_WEEKDAY_FIELD, DT_NUMERIC + 2*DT_DELTA, 1, 2},
|
2007-08-04 02:44:11 +00:00
|
|
|
{LOW_C, UDATPG_WEEKDAY_FIELD, DT_SHORT - 2*DT_DELTA, 3, 0},
|
|
|
|
{LOW_C, UDATPG_WEEKDAY_FIELD, DT_LONG - 2*DT_DELTA, 4, 0},
|
|
|
|
{LOW_C, UDATPG_WEEKDAY_FIELD, DT_NARROW - 2*DT_DELTA, 5, 0},
|
2010-10-20 22:06:10 +00:00
|
|
|
{LOW_E, UDATPG_WEEKDAY_FIELD, DT_NUMERIC + DT_DELTA, 1, 2}, // LOW_E is currently not used in CLDR data, should not be canonical
|
|
|
|
{LOW_E, UDATPG_WEEKDAY_FIELD, DT_SHORT - DT_DELTA, 3, 0},
|
|
|
|
{LOW_E, UDATPG_WEEKDAY_FIELD, DT_LONG - DT_DELTA, 4, 0},
|
|
|
|
{LOW_E, UDATPG_WEEKDAY_FIELD, DT_NARROW - DT_DELTA, 5, 0},
|
2007-08-03 18:43:04 +00:00
|
|
|
{LOW_D, UDATPG_DAY_FIELD, DT_NUMERIC, 1, 2},
|
|
|
|
{CAP_D, UDATPG_DAY_OF_YEAR_FIELD, DT_NUMERIC + DT_DELTA, 1, 3},
|
2007-08-04 02:44:11 +00:00
|
|
|
{CAP_F, UDATPG_DAY_OF_WEEK_IN_MONTH_FIELD, DT_NUMERIC + 2*DT_DELTA, 1, 0},
|
2008-11-18 18:27:40 +00:00
|
|
|
{LOW_G, UDATPG_DAY_FIELD, DT_NUMERIC + 3*DT_DELTA, 1, 20}, // really internal use, so we don't care
|
2007-08-04 02:44:11 +00:00
|
|
|
{LOW_A, UDATPG_DAYPERIOD_FIELD, DT_SHORT, 1, 0},
|
2007-08-03 18:43:04 +00:00
|
|
|
{CAP_H, UDATPG_HOUR_FIELD, DT_NUMERIC + 10*DT_DELTA, 1, 2}, // 24 hour
|
|
|
|
{LOW_K, UDATPG_HOUR_FIELD, DT_NUMERIC + 11*DT_DELTA, 1, 2},
|
|
|
|
{LOW_H, UDATPG_HOUR_FIELD, DT_NUMERIC, 1, 2}, // 12 hour
|
|
|
|
{LOW_K, UDATPG_HOUR_FIELD, DT_NUMERIC + DT_DELTA, 1, 2},
|
|
|
|
{LOW_M, UDATPG_MINUTE_FIELD, DT_NUMERIC, 1, 2},
|
|
|
|
{LOW_S, UDATPG_SECOND_FIELD, DT_NUMERIC, 1, 2},
|
|
|
|
{CAP_S, UDATPG_FRACTIONAL_SECOND_FIELD, DT_NUMERIC + DT_DELTA, 1, 1000},
|
|
|
|
{CAP_A, UDATPG_SECOND_FIELD, DT_NUMERIC + 2*DT_DELTA, 1, 1000},
|
2007-08-04 02:44:11 +00:00
|
|
|
{LOW_V, UDATPG_ZONE_FIELD, DT_SHORT - 2*DT_DELTA, 1, 0},
|
|
|
|
{LOW_V, UDATPG_ZONE_FIELD, DT_LONG - 2*DT_DELTA, 4, 0},
|
2007-08-03 18:43:04 +00:00
|
|
|
{LOW_Z, UDATPG_ZONE_FIELD, DT_SHORT, 1, 3},
|
2007-08-04 02:44:11 +00:00
|
|
|
{LOW_Z, UDATPG_ZONE_FIELD, DT_LONG, 4, 0},
|
2007-08-03 18:43:04 +00:00
|
|
|
{CAP_Z, UDATPG_ZONE_FIELD, DT_SHORT - DT_DELTA, 1, 3},
|
2007-08-04 02:44:11 +00:00
|
|
|
{CAP_Z, UDATPG_ZONE_FIELD, DT_LONG - DT_DELTA, 4, 0},
|
2009-04-22 16:28:25 +00:00
|
|
|
{CAP_V, UDATPG_ZONE_FIELD, DT_SHORT - DT_DELTA, 1, 3},
|
|
|
|
{CAP_V, UDATPG_ZONE_FIELD, DT_LONG - DT_DELTA, 4, 0},
|
2009-04-22 23:10:07 +00:00
|
|
|
{0, UDATPG_FIELD_COUNT, 0, 0, 0} , // last row of dtTypes[]
|
2007-08-03 18:43:04 +00:00
|
|
|
};
|
|
|
|
|
2008-01-03 10:07:47 +00:00
|
|
|
static const char* const CLDR_FIELD_APPEND[] = {
|
2007-08-03 18:43:04 +00:00
|
|
|
"Era", "Year", "Quarter", "Month", "Week", "*", "Day-Of-Week", "Day", "*", "*", "*",
|
|
|
|
"Hour", "Minute", "Second", "*", "Timezone"
|
|
|
|
};
|
|
|
|
|
2008-01-03 10:07:47 +00:00
|
|
|
static const char* const CLDR_FIELD_NAME[] = {
|
2007-08-03 18:43:04 +00:00
|
|
|
"era", "year", "quarter", "month", "week", "*", "weekday", "day", "*", "*", "dayperiod",
|
|
|
|
"hour", "minute", "second", "*", "zone"
|
2009-04-22 23:10:07 +00:00
|
|
|
};
|
2007-08-03 18:43:04 +00:00
|
|
|
|
2008-01-03 10:07:47 +00:00
|
|
|
static const char* const Resource_Fields[] = {
|
2007-08-03 18:43:04 +00:00
|
|
|
"day", "dayperiod", "era", "hour", "minute", "month", "second", "week",
|
2007-08-26 20:10:45 +00:00
|
|
|
"weekday", "year", "zone", "quarter" };
|
|
|
|
|
2007-08-07 03:14:19 +00:00
|
|
|
// For appendItems
|
|
|
|
static const UChar UDATPG_ItemFormat[]= {0x7B, 0x30, 0x7D, 0x20, 0x251C, 0x7B, 0x32, 0x7D, 0x3A,
|
|
|
|
0x20, 0x7B, 0x31, 0x7D, 0x2524, 0}; // {0} \u251C{2}: {1}\u2524
|
2007-08-03 18:43:04 +00:00
|
|
|
|
2007-08-23 05:48:44 +00:00
|
|
|
static const UChar repeatedPatterns[6]={CAP_G, CAP_E, LOW_Z, LOW_V, CAP_Q, 0}; // "GEzvQ"
|
|
|
|
|
2007-08-11 20:03:10 +00:00
|
|
|
static const char DT_DateTimePatternsTag[]="DateTimePatterns";
|
|
|
|
static const char DT_DateTimeCalendarTag[]="calendar";
|
|
|
|
static const char DT_DateTimeGregorianTag[]="gregorian";
|
|
|
|
static const char DT_DateTimeAppendItemsTag[]="appendItems";
|
|
|
|
static const char DT_DateTimeFieldsTag[]="fields";
|
|
|
|
static const char DT_DateTimeAvailableFormatsTag[]="availableFormats";
|
2007-12-19 22:39:09 +00:00
|
|
|
//static const UnicodeString repeatedPattern=UnicodeString(repeatedPatterns);
|
2007-08-11 20:03:10 +00:00
|
|
|
|
2007-08-03 18:43:04 +00:00
|
|
|
UOBJECT_DEFINE_RTTI_IMPLEMENTATION(DateTimePatternGenerator)
|
|
|
|
UOBJECT_DEFINE_RTTI_IMPLEMENTATION(DTSkeletonEnumeration)
|
|
|
|
UOBJECT_DEFINE_RTTI_IMPLEMENTATION(DTRedundantEnumeration)
|
|
|
|
|
|
|
|
DateTimePatternGenerator* U_EXPORT2
|
|
|
|
DateTimePatternGenerator::createInstance(UErrorCode& status) {
|
|
|
|
return createInstance(Locale::getDefault(), status);
|
|
|
|
}
|
|
|
|
|
|
|
|
DateTimePatternGenerator* U_EXPORT2
|
|
|
|
DateTimePatternGenerator::createInstance(const Locale& locale, UErrorCode& status) {
|
2008-02-17 18:26:39 +00:00
|
|
|
DateTimePatternGenerator *result = new DateTimePatternGenerator(locale, status);
|
|
|
|
if (result == NULL) {
|
|
|
|
status = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
}
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
delete result;
|
|
|
|
result = NULL;
|
|
|
|
}
|
|
|
|
return result;
|
2007-08-03 18:43:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DateTimePatternGenerator* U_EXPORT2
|
|
|
|
DateTimePatternGenerator::createEmptyInstance(UErrorCode& status) {
|
2008-02-17 18:26:39 +00:00
|
|
|
DateTimePatternGenerator *result = new DateTimePatternGenerator(status);
|
|
|
|
if (result == NULL) {
|
|
|
|
status = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
}
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
delete result;
|
|
|
|
result = NULL;
|
|
|
|
}
|
|
|
|
return result;
|
2007-08-03 18:43:04 +00:00
|
|
|
}
|
|
|
|
|
2009-04-22 23:10:07 +00:00
|
|
|
DateTimePatternGenerator::DateTimePatternGenerator(UErrorCode &status) :
|
2008-02-17 18:26:39 +00:00
|
|
|
skipMatcher(NULL),
|
|
|
|
fAvailableFormatKeyHash(NULL)
|
2007-08-29 06:49:58 +00:00
|
|
|
{
|
2007-08-03 18:43:04 +00:00
|
|
|
fp = new FormatParser();
|
|
|
|
dtMatcher = new DateTimeMatcher();
|
|
|
|
distanceInfo = new DistanceInfo();
|
|
|
|
patternMap = new PatternMap();
|
2007-08-29 06:49:58 +00:00
|
|
|
if (fp == NULL || dtMatcher == NULL || distanceInfo == NULL || patternMap == NULL) {
|
|
|
|
status = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
}
|
2007-08-03 18:43:04 +00:00
|
|
|
}
|
|
|
|
|
2008-02-17 18:26:39 +00:00
|
|
|
DateTimePatternGenerator::DateTimePatternGenerator(const Locale& locale, UErrorCode &status) :
|
|
|
|
skipMatcher(NULL),
|
|
|
|
fAvailableFormatKeyHash(NULL)
|
2007-08-29 06:49:58 +00:00
|
|
|
{
|
2007-08-03 18:43:04 +00:00
|
|
|
fp = new FormatParser();
|
|
|
|
dtMatcher = new DateTimeMatcher();
|
|
|
|
distanceInfo = new DistanceInfo();
|
|
|
|
patternMap = new PatternMap();
|
2007-08-29 06:49:58 +00:00
|
|
|
if (fp == NULL || dtMatcher == NULL || distanceInfo == NULL || patternMap == NULL) {
|
|
|
|
status = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
}
|
|
|
|
else {
|
2008-02-17 18:26:39 +00:00
|
|
|
initData(locale, status);
|
2007-08-29 06:49:58 +00:00
|
|
|
}
|
2007-08-03 18:43:04 +00:00
|
|
|
}
|
|
|
|
|
2009-04-22 23:10:07 +00:00
|
|
|
DateTimePatternGenerator::DateTimePatternGenerator(const DateTimePatternGenerator& other) :
|
2008-02-17 18:26:39 +00:00
|
|
|
UObject(),
|
|
|
|
skipMatcher(NULL),
|
|
|
|
fAvailableFormatKeyHash(NULL)
|
|
|
|
{
|
2007-08-03 18:43:04 +00:00
|
|
|
fp = new FormatParser();
|
2009-04-22 23:10:07 +00:00
|
|
|
dtMatcher = new DateTimeMatcher();
|
2007-08-03 18:43:04 +00:00
|
|
|
distanceInfo = new DistanceInfo();
|
|
|
|
patternMap = new PatternMap();
|
|
|
|
*this=other;
|
|
|
|
}
|
|
|
|
|
|
|
|
DateTimePatternGenerator&
|
|
|
|
DateTimePatternGenerator::operator=(const DateTimePatternGenerator& other) {
|
|
|
|
pLocale = other.pLocale;
|
2009-03-20 00:08:24 +00:00
|
|
|
fDefaultHourFormatChar = other.fDefaultHourFormatChar;
|
2007-08-03 18:43:04 +00:00
|
|
|
*fp = *(other.fp);
|
|
|
|
dtMatcher->copyFrom(other.dtMatcher->skeleton);
|
|
|
|
*distanceInfo = *(other.distanceInfo);
|
|
|
|
dateTimeFormat = other.dateTimeFormat;
|
|
|
|
decimal = other.decimal;
|
|
|
|
// NUL-terminate for the C API.
|
|
|
|
dateTimeFormat.getTerminatedBuffer();
|
|
|
|
decimal.getTerminatedBuffer();
|
2007-08-29 05:44:57 +00:00
|
|
|
delete skipMatcher;
|
|
|
|
if ( other.skipMatcher == NULL ) {
|
|
|
|
skipMatcher = NULL;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
skipMatcher = new DateTimeMatcher(*other.skipMatcher);
|
|
|
|
}
|
2007-08-03 18:43:04 +00:00
|
|
|
for (int32_t i=0; i< UDATPG_FIELD_COUNT; ++i ) {
|
|
|
|
appendItemFormats[i] = other.appendItemFormats[i];
|
|
|
|
appendItemNames[i] = other.appendItemNames[i];
|
|
|
|
// NUL-terminate for the C API.
|
|
|
|
appendItemFormats[i].getTerminatedBuffer();
|
|
|
|
appendItemNames[i].getTerminatedBuffer();
|
|
|
|
}
|
2008-02-17 18:26:39 +00:00
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
patternMap->copyFrom(*other.patternMap, status);
|
|
|
|
copyHashtable(other.fAvailableFormatKeyHash, status);
|
2007-08-03 18:43:04 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
UBool
|
|
|
|
DateTimePatternGenerator::operator==(const DateTimePatternGenerator& other) const {
|
|
|
|
if (this == &other) {
|
|
|
|
return TRUE;
|
|
|
|
}
|
2007-08-07 03:14:19 +00:00
|
|
|
if ((pLocale==other.pLocale) && (patternMap->equals(*other.patternMap)) &&
|
2008-11-18 18:27:40 +00:00
|
|
|
(dateTimeFormat==other.dateTimeFormat) && (decimal==other.decimal)) {
|
2007-08-07 03:14:19 +00:00
|
|
|
for ( int32_t i=0 ; i<UDATPG_FIELD_COUNT; ++i ) {
|
|
|
|
if ((appendItemFormats[i] != other.appendItemFormats[i]) ||
|
|
|
|
(appendItemNames[i] != other.appendItemNames[i]) ) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return FALSE;
|
|
|
|
}
|
2007-08-03 18:43:04 +00:00
|
|
|
}
|
|
|
|
|
2007-08-08 01:07:23 +00:00
|
|
|
UBool
|
|
|
|
DateTimePatternGenerator::operator!=(const DateTimePatternGenerator& other) const {
|
|
|
|
return !operator==(other);
|
|
|
|
}
|
|
|
|
|
2007-08-03 18:43:04 +00:00
|
|
|
DateTimePatternGenerator::~DateTimePatternGenerator() {
|
|
|
|
if (fAvailableFormatKeyHash!=NULL) {
|
|
|
|
delete fAvailableFormatKeyHash;
|
|
|
|
}
|
2009-04-22 23:10:07 +00:00
|
|
|
|
|
|
|
if (fp != NULL) delete fp;
|
2007-08-03 18:43:04 +00:00
|
|
|
if (dtMatcher != NULL) delete dtMatcher;
|
|
|
|
if (distanceInfo != NULL) delete distanceInfo;
|
|
|
|
if (patternMap != NULL) delete patternMap;
|
2007-08-29 05:44:57 +00:00
|
|
|
if (skipMatcher != NULL) delete skipMatcher;
|
2007-08-03 18:43:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2008-02-17 18:26:39 +00:00
|
|
|
DateTimePatternGenerator::initData(const Locale& locale, UErrorCode &status) {
|
2009-03-20 00:08:24 +00:00
|
|
|
//const char *baseLangName = locale.getBaseName(); // unused
|
2009-04-22 23:10:07 +00:00
|
|
|
|
2007-08-03 18:43:04 +00:00
|
|
|
skipMatcher = NULL;
|
|
|
|
fAvailableFormatKeyHash=NULL;
|
|
|
|
addCanonicalItems();
|
2008-02-17 18:26:39 +00:00
|
|
|
addICUPatterns(locale, status);
|
|
|
|
if (U_FAILURE(status)) {
|
2007-08-07 03:14:19 +00:00
|
|
|
return;
|
|
|
|
}
|
2009-06-09 21:28:13 +00:00
|
|
|
addCLDRData(locale, status);
|
2008-02-17 18:26:39 +00:00
|
|
|
setDateTimeFromCalendar(locale, status);
|
|
|
|
setDecimalSymbols(locale, status);
|
2007-08-03 18:43:04 +00:00
|
|
|
} // DateTimePatternGenerator::initData
|
|
|
|
|
|
|
|
UnicodeString
|
2008-02-17 18:26:39 +00:00
|
|
|
DateTimePatternGenerator::getSkeleton(const UnicodeString& pattern, UErrorCode&
|
|
|
|
/*status*/) {
|
2007-08-03 18:43:04 +00:00
|
|
|
dtMatcher->set(pattern, fp);
|
2007-08-07 03:14:19 +00:00
|
|
|
return dtMatcher->getSkeletonPtr()->getSkeleton();
|
2007-08-03 18:43:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
UnicodeString
|
2008-02-17 18:26:39 +00:00
|
|
|
DateTimePatternGenerator::getBaseSkeleton(const UnicodeString& pattern, UErrorCode& /*status*/) {
|
2007-08-03 18:43:04 +00:00
|
|
|
dtMatcher->set(pattern, fp);
|
2007-08-07 03:14:19 +00:00
|
|
|
return dtMatcher->getSkeletonPtr()->getBaseSkeleton();
|
2007-08-03 18:43:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2007-08-07 03:14:19 +00:00
|
|
|
DateTimePatternGenerator::addICUPatterns(const Locale& locale, UErrorCode& status) {
|
2007-08-04 04:13:16 +00:00
|
|
|
UnicodeString dfPattern;
|
2007-08-03 18:43:04 +00:00
|
|
|
UnicodeString conflictingString;
|
2008-02-14 02:43:33 +00:00
|
|
|
DateFormat* df;
|
|
|
|
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-08-03 18:43:04 +00:00
|
|
|
// Load with ICU patterns
|
2007-08-04 14:19:29 +00:00
|
|
|
for (int32_t i=DateFormat::kFull; i<=DateFormat::kShort; i++) {
|
2008-02-14 02:43:33 +00:00
|
|
|
DateFormat::EStyle style = (DateFormat::EStyle)i;
|
|
|
|
df = DateFormat::createDateInstance(style, locale);
|
2010-05-19 17:29:33 +00:00
|
|
|
SimpleDateFormat* sdf;
|
|
|
|
if (df != NULL && (sdf = dynamic_cast<SimpleDateFormat*>(df)) != NULL) {
|
2012-02-06 19:57:08 +00:00
|
|
|
addPattern(sdf->toPattern(dfPattern), FALSE, conflictingString, status);
|
2008-02-14 02:43:33 +00:00
|
|
|
}
|
|
|
|
// TODO Maybe we should return an error when the date format isn't simple.
|
|
|
|
delete df;
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return;
|
2007-08-03 18:43:04 +00:00
|
|
|
}
|
|
|
|
|
2008-02-14 02:43:33 +00:00
|
|
|
df = DateFormat::createTimeInstance(style, locale);
|
2010-05-19 17:29:33 +00:00
|
|
|
if (df != NULL && (sdf = dynamic_cast<SimpleDateFormat*>(df)) != NULL) {
|
2012-02-06 19:57:08 +00:00
|
|
|
addPattern(sdf->toPattern(dfPattern), FALSE, conflictingString, status);
|
2007-08-03 18:43:04 +00:00
|
|
|
// HACK for hh:ss
|
|
|
|
if ( i==DateFormat::kMedium ) {
|
2007-08-04 04:13:16 +00:00
|
|
|
hackPattern = dfPattern;
|
2007-08-03 18:43:04 +00:00
|
|
|
}
|
|
|
|
}
|
2008-02-14 02:43:33 +00:00
|
|
|
// TODO Maybe we should return an error when the date format isn't simple.
|
|
|
|
delete df;
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return;
|
|
|
|
}
|
2007-08-03 18:43:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2007-08-23 05:48:44 +00:00
|
|
|
DateTimePatternGenerator::hackTimes(const UnicodeString& hackPattern, UErrorCode& status) {
|
2007-08-03 18:43:04 +00:00
|
|
|
UnicodeString conflictingString;
|
2009-04-22 23:10:07 +00:00
|
|
|
|
2007-08-03 18:43:04 +00:00
|
|
|
fp->set(hackPattern);
|
|
|
|
UnicodeString mmss;
|
|
|
|
UBool gotMm=FALSE;
|
|
|
|
for (int32_t i=0; i<fp->itemNumber; ++i) {
|
|
|
|
UnicodeString field = fp->items[i];
|
|
|
|
if ( fp->isQuoteLiteral(field) ) {
|
|
|
|
if ( gotMm ) {
|
|
|
|
UnicodeString quoteLiteral;
|
|
|
|
fp->getQuoteLiteral(quoteLiteral, &i);
|
|
|
|
mmss += quoteLiteral;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (fp->isPatternSeparator(field) && gotMm) {
|
|
|
|
mmss+=field;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
UChar ch=field.charAt(0);
|
|
|
|
if (ch==LOW_M) {
|
|
|
|
gotMm=TRUE;
|
|
|
|
mmss+=field;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (ch==LOW_S) {
|
|
|
|
if (!gotMm) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
mmss+= field;
|
2012-02-06 19:57:08 +00:00
|
|
|
addPattern(mmss, FALSE, conflictingString, status);
|
2007-08-03 18:43:04 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (gotMm || ch==LOW_Z || ch==CAP_Z || ch==LOW_V || ch==CAP_V) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-19 04:19:02 +00:00
|
|
|
#define ULOC_LOCALE_IDENTIFIER_CAPACITY (ULOC_FULLNAME_CAPACITY + 1 + ULOC_KEYWORD_AND_VALUES_CAPACITY)
|
|
|
|
|
2009-03-20 00:08:24 +00:00
|
|
|
static const UChar hourFormatChars[] = { CAP_H, LOW_H, CAP_K, LOW_K, 0 }; // HhKk, the hour format characters
|
|
|
|
|
2007-08-03 18:43:04 +00:00
|
|
|
void
|
2009-06-09 21:28:13 +00:00
|
|
|
DateTimePatternGenerator::addCLDRData(const Locale& locale, UErrorCode& err) {
|
2008-12-19 20:10:03 +00:00
|
|
|
UResourceBundle *rb, *calTypeBundle, *calBundle;
|
2007-08-03 18:43:04 +00:00
|
|
|
UResourceBundle *patBundle, *fieldBundle, *fBundle;
|
|
|
|
UnicodeString rbPattern, value, field;
|
|
|
|
UnicodeString conflictingPattern;
|
|
|
|
const char *key=NULL;
|
2007-09-06 18:00:08 +00:00
|
|
|
int32_t i;
|
2007-08-03 18:43:04 +00:00
|
|
|
|
2007-08-07 03:14:19 +00:00
|
|
|
UnicodeString defaultItemFormat(TRUE, UDATPG_ItemFormat, LENGTHOF(UDATPG_ItemFormat)-1); // Read-only alias.
|
2007-08-03 18:43:04 +00:00
|
|
|
|
2009-06-09 21:28:13 +00:00
|
|
|
err = U_ZERO_ERROR;
|
|
|
|
|
2009-03-20 00:08:24 +00:00
|
|
|
fDefaultHourFormatChar = 0;
|
2007-09-06 18:00:08 +00:00
|
|
|
for (i=0; i<UDATPG_FIELD_COUNT; ++i ) {
|
2007-08-03 18:43:04 +00:00
|
|
|
appendItemNames[i]=CAP_F;
|
|
|
|
if (i<10) {
|
2007-08-06 23:09:29 +00:00
|
|
|
appendItemNames[i]+=(UChar)(i+0x30);
|
2007-08-03 18:43:04 +00:00
|
|
|
}
|
|
|
|
else {
|
2007-08-06 21:25:31 +00:00
|
|
|
appendItemNames[i]+=(UChar)0x31;
|
2007-08-06 23:09:29 +00:00
|
|
|
appendItemNames[i]+=(UChar)(i-10 + 0x30);
|
2007-08-03 18:43:04 +00:00
|
|
|
}
|
|
|
|
// NUL-terminate for the C API.
|
|
|
|
appendItemNames[i].getTerminatedBuffer();
|
|
|
|
}
|
|
|
|
|
|
|
|
rb = ures_open(NULL, locale.getName(), &err);
|
2009-06-09 21:28:13 +00:00
|
|
|
if (rb == NULL || U_FAILURE(err)) {
|
|
|
|
return;
|
|
|
|
}
|
2009-04-22 23:10:07 +00:00
|
|
|
const char *curLocaleName=ures_getLocaleByType(rb, ULOC_ACTUAL_LOCALE, &err);
|
2008-11-19 04:19:02 +00:00
|
|
|
const char * calendarTypeToUse = DT_DateTimeGregorianTag; // initial default
|
|
|
|
char calendarType[ULOC_KEYWORDS_CAPACITY]; // to be filled in with the type to use, if all goes well
|
|
|
|
if ( U_SUCCESS(err) ) {
|
|
|
|
char localeWithCalendarKey[ULOC_LOCALE_IDENTIFIER_CAPACITY];
|
|
|
|
// obtain a locale that always has the calendar key value that should be used
|
|
|
|
(void)ures_getFunctionalEquivalent(localeWithCalendarKey, ULOC_LOCALE_IDENTIFIER_CAPACITY, NULL,
|
|
|
|
"calendar", "calendar", locale.getName(), NULL, FALSE, &err);
|
|
|
|
localeWithCalendarKey[ULOC_LOCALE_IDENTIFIER_CAPACITY-1] = 0; // ensure null termination
|
|
|
|
// now get the calendar key value from that locale
|
|
|
|
int32_t calendarTypeLen = uloc_getKeywordValue(localeWithCalendarKey, "calendar", calendarType, ULOC_KEYWORDS_CAPACITY, &err);
|
|
|
|
if (U_SUCCESS(err) && calendarTypeLen < ULOC_KEYWORDS_CAPACITY) {
|
|
|
|
calendarTypeToUse = calendarType;
|
|
|
|
}
|
|
|
|
err = U_ZERO_ERROR;
|
|
|
|
}
|
2009-03-20 00:08:24 +00:00
|
|
|
calBundle = ures_getByKeyWithFallback(rb, DT_DateTimeCalendarTag, NULL, &err);
|
|
|
|
calTypeBundle = ures_getByKeyWithFallback(calBundle, calendarTypeToUse, NULL, &err);
|
2007-08-03 18:43:04 +00:00
|
|
|
|
|
|
|
key=NULL;
|
|
|
|
int32_t dtCount=0;
|
2008-12-19 20:10:03 +00:00
|
|
|
patBundle = ures_getByKeyWithFallback(calTypeBundle, DT_DateTimePatternsTag, NULL, &err);
|
2007-08-03 18:43:04 +00:00
|
|
|
while (U_SUCCESS(err)) {
|
|
|
|
rbPattern = ures_getNextUnicodeString(patBundle, &key, &err);
|
|
|
|
dtCount++;
|
|
|
|
if (rbPattern.length()==0 ) {
|
|
|
|
break; // no more pattern
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (dtCount==9) {
|
|
|
|
setDateTimeFormat(rbPattern);
|
2009-03-20 00:08:24 +00:00
|
|
|
} else if (dtCount==4) { // short time format
|
|
|
|
// set fDefaultHourFormatChar to the hour format character from this pattern
|
|
|
|
int32_t tfIdx, tfLen = rbPattern.length();
|
2009-04-23 05:58:36 +00:00
|
|
|
UBool ignoreChars = FALSE;
|
2009-03-20 00:08:24 +00:00
|
|
|
for (tfIdx = 0; tfIdx < tfLen; tfIdx++) {
|
|
|
|
UChar tfChar = rbPattern.charAt(tfIdx);
|
2009-04-23 05:58:36 +00:00
|
|
|
if ( tfChar == SINGLE_QUOTE ) {
|
|
|
|
ignoreChars = !ignoreChars; // toggle (handle quoted literals & '' for single quote)
|
|
|
|
} else if ( !ignoreChars && u_strchr(hourFormatChars, tfChar) != NULL ) {
|
2009-03-20 00:08:24 +00:00
|
|
|
fDefaultHourFormatChar = tfChar;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2007-08-03 18:43:04 +00:00
|
|
|
}
|
|
|
|
}
|
2009-06-09 21:28:13 +00:00
|
|
|
}
|
2007-08-03 18:43:04 +00:00
|
|
|
ures_close(patBundle);
|
2009-04-22 23:10:07 +00:00
|
|
|
|
2007-08-03 18:43:04 +00:00
|
|
|
err = U_ZERO_ERROR;
|
2008-12-19 20:10:03 +00:00
|
|
|
patBundle = ures_getByKeyWithFallback(calTypeBundle, DT_DateTimeAppendItemsTag, NULL, &err);
|
2007-08-03 18:43:04 +00:00
|
|
|
key=NULL;
|
|
|
|
UnicodeString itemKey;
|
|
|
|
while (U_SUCCESS(err)) {
|
|
|
|
rbPattern = ures_getNextUnicodeString(patBundle, &key, &err);
|
|
|
|
if (rbPattern.length()==0 ) {
|
|
|
|
break; // no more pattern
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
setAppendItemFormat(getAppendFormatNumber(key), rbPattern);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ures_close(patBundle);
|
2009-04-22 23:10:07 +00:00
|
|
|
|
2007-08-03 18:43:04 +00:00
|
|
|
key=NULL;
|
|
|
|
err = U_ZERO_ERROR;
|
2008-12-19 20:10:03 +00:00
|
|
|
fBundle = ures_getByKeyWithFallback(calTypeBundle, DT_DateTimeFieldsTag, NULL, &err);
|
2007-09-06 18:00:08 +00:00
|
|
|
for (i=0; i<MAX_RESOURCE_FIELD; ++i) {
|
2007-08-03 18:43:04 +00:00
|
|
|
err = U_ZERO_ERROR;
|
|
|
|
patBundle = ures_getByKeyWithFallback(fBundle, Resource_Fields[i], NULL, &err);
|
|
|
|
fieldBundle = ures_getByKeyWithFallback(patBundle, "dn", NULL, &err);
|
|
|
|
rbPattern = ures_getNextUnicodeString(fieldBundle, &key, &err);
|
2009-06-09 21:28:13 +00:00
|
|
|
ures_close(fieldBundle);
|
2007-08-03 18:43:04 +00:00
|
|
|
ures_close(patBundle);
|
|
|
|
if (rbPattern.length()==0 ) {
|
2009-04-22 23:10:07 +00:00
|
|
|
continue;
|
2007-08-03 18:43:04 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
setAppendItemName(getAppendNameNumber(Resource_Fields[i]), rbPattern);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ures_close(fBundle);
|
|
|
|
|
|
|
|
// add available formats
|
2012-02-23 10:06:47 +00:00
|
|
|
UBool firstTimeThrough = TRUE;
|
2007-08-03 18:43:04 +00:00
|
|
|
err = U_ZERO_ERROR;
|
|
|
|
initHashtable(err);
|
2012-02-23 10:06:47 +00:00
|
|
|
while (TRUE) {
|
|
|
|
// At the start of the loop:
|
|
|
|
// - rb is the open resource bundle for the current locale being processed,
|
|
|
|
// whose actual name is in curLocaleName.
|
|
|
|
// - if U_SUCCESS(err), then calBundle and calTypeBundle are open;
|
|
|
|
// process contents of calTypeBundle, then close calBundle and calTypeBundle.
|
2007-08-03 18:43:04 +00:00
|
|
|
if (U_SUCCESS(err)) {
|
2012-02-23 10:06:47 +00:00
|
|
|
// process contents of calTypeBundle
|
|
|
|
patBundle = ures_getByKeyWithFallback(calTypeBundle, DT_DateTimeAvailableFormatsTag, NULL, &err);
|
|
|
|
if (U_SUCCESS(err)) {
|
|
|
|
int32_t numberKeys = ures_getSize(patBundle);
|
|
|
|
int32_t len;
|
|
|
|
const UChar *retPattern;
|
|
|
|
key=NULL;
|
2008-06-28 07:02:26 +00:00
|
|
|
#if defined(U_USE_ASCII_BUNDLE_ITERATOR)
|
2012-02-23 10:06:47 +00:00
|
|
|
UResourceBundleAIterator aiter;
|
|
|
|
ures_a_open(&aiter, patBundle, &err);
|
2008-06-28 07:02:26 +00:00
|
|
|
#endif
|
2012-02-23 10:06:47 +00:00
|
|
|
for(i=0; i<numberKeys; ++i) {
|
2008-06-28 07:02:26 +00:00
|
|
|
#if defined(U_USE_ASCII_BUNDLE_ITERATOR)
|
2012-02-23 10:06:47 +00:00
|
|
|
retPattern=ures_a_getNextString(&aiter, &len, &key, &err);
|
2008-06-28 07:02:26 +00:00
|
|
|
#else
|
2012-02-23 10:06:47 +00:00
|
|
|
retPattern=ures_getNextString(patBundle, &len, &key, &err);
|
2009-04-22 23:10:07 +00:00
|
|
|
#endif
|
2012-02-23 10:06:47 +00:00
|
|
|
UnicodeString format=UnicodeString(retPattern);
|
|
|
|
UnicodeString retKey=UnicodeString(key, -1, US_INV);
|
|
|
|
if ( firstTimeThrough || !isAvailableFormatSet(retKey) ) {
|
|
|
|
setAvailableFormat(retKey, err);
|
|
|
|
// Add pattern with its associated skeleton. Override any duplicate derived from std patterns,
|
|
|
|
// but not a previous availableFormats entry:
|
|
|
|
addPatternWithSkeleton(format, &retKey, TRUE, conflictingPattern, err);
|
|
|
|
}
|
2007-08-03 18:43:04 +00:00
|
|
|
}
|
2008-06-28 07:02:26 +00:00
|
|
|
#if defined(U_USE_ASCII_BUNDLE_ITERATOR)
|
2012-02-23 10:06:47 +00:00
|
|
|
ures_a_close(&aiter);
|
2008-06-28 07:02:26 +00:00
|
|
|
#endif
|
2012-02-23 10:06:47 +00:00
|
|
|
ures_close(patBundle);
|
|
|
|
}
|
|
|
|
firstTimeThrough = FALSE;
|
|
|
|
// close calBundle and calTypeBundle
|
|
|
|
ures_close(calTypeBundle);
|
|
|
|
ures_close(calBundle);
|
2007-08-03 18:43:04 +00:00
|
|
|
}
|
2012-02-23 10:06:47 +00:00
|
|
|
if (uprv_strcmp(curLocaleName,"root")==0 || uprv_strlen(curLocaleName)==0) {
|
|
|
|
// we just finished handling root, nothing more to check
|
|
|
|
ures_close(rb);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// Find the name of the appropriate parent locale (from %%Parent if present, else
|
|
|
|
// uloc_getParent on the actual locale name)
|
|
|
|
// (It would be nice to have a ures function that did this...)
|
|
|
|
err = U_ZERO_ERROR;
|
2012-03-01 23:50:07 +00:00
|
|
|
char parentLocale[ULOC_FULLNAME_CAPACITY];
|
2012-02-23 10:06:47 +00:00
|
|
|
int32_t locNameLen;
|
|
|
|
const UChar * parentUName = ures_getStringByKey(rb, "%%Parent", &locNameLen, &err);
|
2012-03-01 23:50:07 +00:00
|
|
|
if (U_SUCCESS(err) && err != U_USING_FALLBACK_WARNING && locNameLen < ULOC_FULLNAME_CAPACITY) {
|
|
|
|
u_UCharsToChars(parentUName, parentLocale, locNameLen + 1);
|
2012-02-23 10:06:47 +00:00
|
|
|
} else {
|
|
|
|
err = U_ZERO_ERROR;
|
2012-03-01 23:50:07 +00:00
|
|
|
uloc_getParent(curLocaleName, parentLocale, ULOC_FULLNAME_CAPACITY, &err);
|
|
|
|
if (U_FAILURE(err) || err == U_STRING_NOT_TERMINATED_WARNING) {
|
2012-02-23 10:06:47 +00:00
|
|
|
// just fallback to root, since we are not already there
|
|
|
|
parentLocale[0] = 0;
|
|
|
|
err = U_ZERO_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Close current locale bundle
|
2007-08-03 18:43:04 +00:00
|
|
|
ures_close(rb);
|
2012-02-23 10:06:47 +00:00
|
|
|
// And open its parent, which becomes the new current locale being processed
|
|
|
|
rb = ures_open(NULL, parentLocale, &err);
|
|
|
|
if ( U_FAILURE(err) ) {
|
|
|
|
err = U_ZERO_ERROR;
|
2007-08-03 18:43:04 +00:00
|
|
|
break;
|
|
|
|
}
|
2012-02-23 10:06:47 +00:00
|
|
|
// Get the name of the parent / new current locale
|
|
|
|
curLocaleName=ures_getLocaleByType(rb, ULOC_ACTUAL_LOCALE, &err);
|
|
|
|
if ( U_FAILURE(err) ) {
|
|
|
|
curLocaleName = parentLocale;
|
|
|
|
err = U_ZERO_ERROR;
|
|
|
|
}
|
|
|
|
// Open calBundle and calTypeBundle
|
|
|
|
calBundle = ures_getByKeyWithFallback(rb, DT_DateTimeCalendarTag, NULL, &err);
|
|
|
|
if (U_SUCCESS(err)) {
|
|
|
|
calTypeBundle = ures_getByKeyWithFallback(calBundle, calendarTypeToUse, NULL, &err);
|
|
|
|
if ( U_FAILURE(err) ) {
|
|
|
|
ures_close(calBundle);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Go to the top of the loop to process contents of calTypeBundle
|
|
|
|
}
|
2007-08-03 18:43:04 +00:00
|
|
|
|
|
|
|
if (hackPattern.length()>0) {
|
|
|
|
hackTimes(hackPattern, err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DateTimePatternGenerator::initHashtable(UErrorCode& err) {
|
|
|
|
if (fAvailableFormatKeyHash!=NULL) {
|
|
|
|
return;
|
|
|
|
}
|
2007-08-04 14:19:29 +00:00
|
|
|
if ((fAvailableFormatKeyHash = new Hashtable(FALSE, err))==NULL) {
|
|
|
|
err=U_MEMORY_ALLOCATION_ERROR;
|
2007-08-03 18:43:04 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
DateTimePatternGenerator::setAppendItemFormat(UDateTimePatternField field, const UnicodeString& value) {
|
|
|
|
appendItemFormats[field] = value;
|
|
|
|
// NUL-terminate for the C API.
|
|
|
|
appendItemFormats[field].getTerminatedBuffer();
|
|
|
|
}
|
|
|
|
|
|
|
|
const UnicodeString&
|
|
|
|
DateTimePatternGenerator::getAppendItemFormat(UDateTimePatternField field) const {
|
|
|
|
return appendItemFormats[field];
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DateTimePatternGenerator::setAppendItemName(UDateTimePatternField field, const UnicodeString& value) {
|
|
|
|
appendItemNames[field] = value;
|
|
|
|
// NUL-terminate for the C API.
|
|
|
|
appendItemNames[field].getTerminatedBuffer();
|
|
|
|
}
|
|
|
|
|
|
|
|
const UnicodeString&
|
|
|
|
DateTimePatternGenerator:: getAppendItemName(UDateTimePatternField field) const {
|
|
|
|
return appendItemNames[field];
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DateTimePatternGenerator::getAppendName(UDateTimePatternField field, UnicodeString& value) {
|
|
|
|
value = SINGLE_QUOTE;
|
|
|
|
value += appendItemNames[field];
|
|
|
|
value += SINGLE_QUOTE;
|
|
|
|
}
|
|
|
|
|
|
|
|
UnicodeString
|
|
|
|
DateTimePatternGenerator::getBestPattern(const UnicodeString& patternForm, UErrorCode& status) {
|
2009-12-10 09:13:49 +00:00
|
|
|
return getBestPattern(patternForm, UDATPG_MATCH_NO_OPTIONS, status);
|
|
|
|
}
|
|
|
|
|
|
|
|
UnicodeString
|
|
|
|
DateTimePatternGenerator::getBestPattern(const UnicodeString& patternForm, UDateTimePatternMatchOptions options, UErrorCode& status) {
|
2007-08-04 02:44:11 +00:00
|
|
|
const UnicodeString *bestPattern=NULL;
|
2007-08-03 18:43:04 +00:00
|
|
|
UnicodeString dtFormat;
|
|
|
|
UnicodeString resultPattern;
|
|
|
|
|
|
|
|
int32_t dateMask=(1<<UDATPG_DAYPERIOD_FIELD) - 1;
|
|
|
|
int32_t timeMask=(1<<UDATPG_FIELD_COUNT) - 1 - dateMask;
|
2009-04-22 23:10:07 +00:00
|
|
|
|
2009-03-20 00:08:24 +00:00
|
|
|
UnicodeString patternFormCopy = UnicodeString(patternForm);
|
|
|
|
patternFormCopy.findAndReplace(UnicodeString(LOW_J), UnicodeString(fDefaultHourFormatChar));
|
2009-04-22 23:10:07 +00:00
|
|
|
|
2007-08-04 04:13:16 +00:00
|
|
|
resultPattern.remove();
|
2009-03-20 00:08:24 +00:00
|
|
|
dtMatcher->set(patternFormCopy, fp);
|
2008-11-18 18:27:40 +00:00
|
|
|
const PtnSkeleton* specifiedSkeleton=NULL;
|
|
|
|
bestPattern=getBestRaw(*dtMatcher, -1, distanceInfo, &specifiedSkeleton);
|
2007-08-03 18:43:04 +00:00
|
|
|
if ( distanceInfo->missingFieldMask==0 && distanceInfo->extraFieldMask==0 ) {
|
2009-12-10 09:13:49 +00:00
|
|
|
resultPattern = adjustFieldTypes(*bestPattern, specifiedSkeleton, FALSE, options);
|
2007-08-03 18:43:04 +00:00
|
|
|
|
|
|
|
return resultPattern;
|
|
|
|
}
|
|
|
|
int32_t neededFields = dtMatcher->getFieldMask();
|
2009-12-10 09:13:49 +00:00
|
|
|
UnicodeString datePattern=getBestAppending(neededFields & dateMask, options);
|
|
|
|
UnicodeString timePattern=getBestAppending(neededFields & timeMask, options);
|
2007-08-03 18:43:04 +00:00
|
|
|
if (datePattern.length()==0) {
|
|
|
|
if (timePattern.length()==0) {
|
2007-08-04 04:13:16 +00:00
|
|
|
resultPattern.remove();
|
2007-08-03 18:43:04 +00:00
|
|
|
}
|
|
|
|
else {
|
2007-08-07 03:14:19 +00:00
|
|
|
return timePattern;
|
2007-08-03 18:43:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (timePattern.length()==0) {
|
2007-08-07 03:14:19 +00:00
|
|
|
return datePattern;
|
2007-08-03 18:43:04 +00:00
|
|
|
}
|
2007-08-04 04:13:16 +00:00
|
|
|
resultPattern.remove();
|
2007-08-26 20:10:45 +00:00
|
|
|
status = U_ZERO_ERROR;
|
2007-08-03 18:43:04 +00:00
|
|
|
dtFormat=getDateTimeFormat();
|
2007-12-22 05:30:33 +00:00
|
|
|
Formattable dateTimeObject[] = { timePattern, datePattern };
|
2007-08-04 14:19:29 +00:00
|
|
|
resultPattern = MessageFormat::format(dtFormat, dateTimeObject, 2, resultPattern, status );
|
2007-08-03 18:43:04 +00:00
|
|
|
return resultPattern;
|
|
|
|
}
|
|
|
|
|
|
|
|
UnicodeString
|
2009-04-22 23:10:07 +00:00
|
|
|
DateTimePatternGenerator::replaceFieldTypes(const UnicodeString& pattern,
|
|
|
|
const UnicodeString& skeleton,
|
2009-12-10 09:13:49 +00:00
|
|
|
UErrorCode& status) {
|
|
|
|
return replaceFieldTypes(pattern, skeleton, UDATPG_MATCH_NO_OPTIONS, status);
|
|
|
|
}
|
|
|
|
|
|
|
|
UnicodeString
|
|
|
|
DateTimePatternGenerator::replaceFieldTypes(const UnicodeString& pattern,
|
|
|
|
const UnicodeString& skeleton,
|
|
|
|
UDateTimePatternMatchOptions options,
|
2008-02-17 18:26:39 +00:00
|
|
|
UErrorCode& /*status*/) {
|
2007-08-03 18:43:04 +00:00
|
|
|
dtMatcher->set(skeleton, fp);
|
2009-12-10 09:13:49 +00:00
|
|
|
UnicodeString result = adjustFieldTypes(pattern, NULL, FALSE, options);
|
2007-08-03 18:43:04 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2007-08-31 05:20:56 +00:00
|
|
|
DateTimePatternGenerator::setDecimal(const UnicodeString& newDecimal) {
|
|
|
|
this->decimal = newDecimal;
|
2007-08-03 18:43:04 +00:00
|
|
|
// NUL-terminate for the C API.
|
|
|
|
this->decimal.getTerminatedBuffer();
|
|
|
|
}
|
|
|
|
|
|
|
|
const UnicodeString&
|
|
|
|
DateTimePatternGenerator::getDecimal() const {
|
|
|
|
return decimal;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DateTimePatternGenerator::addCanonicalItems() {
|
|
|
|
UnicodeString conflictingPattern;
|
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
|
|
|
|
for (int32_t i=0; i<UDATPG_FIELD_COUNT; i++) {
|
2012-02-06 19:57:08 +00:00
|
|
|
addPattern(UnicodeString(Canonical_Items[i]), FALSE, conflictingPattern, status);
|
2007-08-03 18:43:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DateTimePatternGenerator::setDateTimeFormat(const UnicodeString& dtFormat) {
|
|
|
|
dateTimeFormat = dtFormat;
|
|
|
|
// NUL-terminate for the C API.
|
|
|
|
dateTimeFormat.getTerminatedBuffer();
|
|
|
|
}
|
|
|
|
|
|
|
|
const UnicodeString&
|
|
|
|
DateTimePatternGenerator::getDateTimeFormat() const {
|
|
|
|
return dateTimeFormat;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2007-08-11 20:03:10 +00:00
|
|
|
DateTimePatternGenerator::setDateTimeFromCalendar(const Locale& locale, UErrorCode& status) {
|
|
|
|
const UChar *resStr;
|
|
|
|
int32_t resStrLen = 0;
|
2009-04-22 23:10:07 +00:00
|
|
|
|
2007-08-11 20:03:10 +00:00
|
|
|
Calendar* fCalendar = Calendar::createInstance(locale, status);
|
|
|
|
CalendarData calData(locale, fCalendar?fCalendar->getType():NULL, status);
|
|
|
|
UResourceBundle *dateTimePatterns = calData.getByKey(DT_DateTimePatternsTag, status);
|
|
|
|
if (U_FAILURE(status)) return;
|
|
|
|
|
|
|
|
if (ures_getSize(dateTimePatterns) <= DateFormat::kDateTime)
|
|
|
|
{
|
|
|
|
status = U_INVALID_FORMAT_ERROR;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
resStr = ures_getStringByIndex(dateTimePatterns, (int32_t)DateFormat::kDateTime, &resStrLen, &status);
|
|
|
|
setDateTimeFormat(UnicodeString(TRUE, resStr, resStrLen));
|
2009-04-22 23:10:07 +00:00
|
|
|
|
2007-08-11 20:03:10 +00:00
|
|
|
delete fCalendar;
|
2007-08-03 18:43:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DateTimePatternGenerator::setDecimalSymbols(const Locale& locale, UErrorCode& status) {
|
|
|
|
DecimalFormatSymbols dfs = DecimalFormatSymbols(locale, status);
|
|
|
|
if(U_SUCCESS(status)) {
|
|
|
|
decimal = dfs.getSymbol(DecimalFormatSymbols::kDecimalSeparatorSymbol);
|
|
|
|
// NUL-terminate for the C API.
|
|
|
|
decimal.getTerminatedBuffer();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
UDateTimePatternConflict
|
|
|
|
DateTimePatternGenerator::addPattern(
|
|
|
|
const UnicodeString& pattern,
|
|
|
|
UBool override,
|
|
|
|
UnicodeString &conflictingPattern,
|
2007-08-04 04:13:16 +00:00
|
|
|
UErrorCode& status)
|
|
|
|
{
|
2008-11-18 18:27:40 +00:00
|
|
|
return addPatternWithSkeleton(pattern, NULL, override, conflictingPattern, status);
|
|
|
|
}
|
|
|
|
|
|
|
|
// For DateTimePatternGenerator::addPatternWithSkeleton -
|
|
|
|
// If skeletonToUse is specified, then an availableFormats entry is being added. In this case:
|
|
|
|
// 1. We pass that skeleton to matcher.set instead of having it derive a skeleton from the pattern.
|
|
|
|
// 2. If the new entry's skeleton or basePattern does match an existing entry but that entry also had a skeleton specified
|
|
|
|
// (i.e. it was also from availableFormats), then the new entry does not override it regardless of the value of the override
|
|
|
|
// parameter. This prevents later availableFormats entries from a parent locale overriding earlier ones from the actual
|
|
|
|
// specified locale. However, availableFormats entries *should* override entries with matching skeleton whose skeleton was
|
|
|
|
// derived (i.e. entries derived from the standard date/time patters for the specified locale).
|
|
|
|
// 3. When adding the pattern (patternMap->add), we set a new boolean to indicate that the added entry had a
|
|
|
|
// specified skeleton (which sets a new field in the PtnElem in the PatternMap).
|
|
|
|
UDateTimePatternConflict
|
|
|
|
DateTimePatternGenerator::addPatternWithSkeleton(
|
|
|
|
const UnicodeString& pattern,
|
|
|
|
const UnicodeString* skeletonToUse,
|
|
|
|
UBool override,
|
|
|
|
UnicodeString& conflictingPattern,
|
|
|
|
UErrorCode& status)
|
|
|
|
{
|
2007-08-03 18:43:04 +00:00
|
|
|
|
|
|
|
UnicodeString basePattern;
|
|
|
|
PtnSkeleton skeleton;
|
|
|
|
UDateTimePatternConflict conflictingStatus = UDATPG_NO_CONFLICT;
|
|
|
|
|
|
|
|
DateTimeMatcher matcher;
|
2008-11-18 18:27:40 +00:00
|
|
|
if ( skeletonToUse == NULL ) {
|
|
|
|
matcher.set(pattern, fp, skeleton);
|
|
|
|
matcher.getBasePattern(basePattern);
|
|
|
|
} else {
|
2011-12-05 04:49:40 +00:00
|
|
|
matcher.set(*skeletonToUse, fp, skeleton); // no longer trims skeleton fields to max len 3, per #7930
|
2008-11-18 18:27:40 +00:00
|
|
|
matcher.getBasePattern(basePattern); // or perhaps instead: basePattern = *skeletonToUse;
|
|
|
|
}
|
|
|
|
UBool entryHadSpecifiedSkeleton;
|
|
|
|
const UnicodeString *duplicatePattern = patternMap->getPatternFromBasePattern(basePattern, entryHadSpecifiedSkeleton);
|
2007-08-03 18:43:04 +00:00
|
|
|
if (duplicatePattern != NULL ) {
|
|
|
|
conflictingStatus = UDATPG_BASE_CONFLICT;
|
|
|
|
conflictingPattern = *duplicatePattern;
|
2008-11-18 18:27:40 +00:00
|
|
|
if (!override || (skeletonToUse != NULL && entryHadSpecifiedSkeleton)) {
|
2007-08-03 18:43:04 +00:00
|
|
|
return conflictingStatus;
|
|
|
|
}
|
|
|
|
}
|
2008-11-18 18:27:40 +00:00
|
|
|
const PtnSkeleton* entrySpecifiedSkeleton = NULL;
|
|
|
|
duplicatePattern = patternMap->getPatternFromSkeleton(skeleton, &entrySpecifiedSkeleton);
|
2007-08-03 18:43:04 +00:00
|
|
|
if (duplicatePattern != NULL ) {
|
|
|
|
conflictingStatus = UDATPG_CONFLICT;
|
|
|
|
conflictingPattern = *duplicatePattern;
|
2008-11-18 18:27:40 +00:00
|
|
|
if (!override || (skeletonToUse != NULL && entrySpecifiedSkeleton != NULL)) {
|
2007-08-03 18:43:04 +00:00
|
|
|
return conflictingStatus;
|
|
|
|
}
|
|
|
|
}
|
2008-11-18 18:27:40 +00:00
|
|
|
patternMap->add(basePattern, skeleton, pattern, skeletonToUse != NULL, status);
|
2007-08-03 18:43:04 +00:00
|
|
|
if(U_FAILURE(status)) {
|
|
|
|
return conflictingStatus;
|
|
|
|
}
|
2009-04-22 23:10:07 +00:00
|
|
|
|
2007-08-03 18:43:04 +00:00
|
|
|
return UDATPG_NO_CONFLICT;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
UDateTimePatternField
|
2007-08-23 05:48:44 +00:00
|
|
|
DateTimePatternGenerator::getAppendFormatNumber(const char* field) const {
|
2007-08-03 18:43:04 +00:00
|
|
|
for (int32_t i=0; i<UDATPG_FIELD_COUNT; ++i ) {
|
|
|
|
if (uprv_strcmp(CLDR_FIELD_APPEND[i], field)==0) {
|
|
|
|
return (UDateTimePatternField)i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return UDATPG_FIELD_COUNT;
|
|
|
|
}
|
|
|
|
|
|
|
|
UDateTimePatternField
|
2007-08-23 05:48:44 +00:00
|
|
|
DateTimePatternGenerator::getAppendNameNumber(const char* field) const {
|
2007-08-03 18:43:04 +00:00
|
|
|
for (int32_t i=0; i<UDATPG_FIELD_COUNT; ++i ) {
|
|
|
|
if (uprv_strcmp(CLDR_FIELD_NAME[i],field)==0) {
|
|
|
|
return (UDateTimePatternField)i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return UDATPG_FIELD_COUNT;
|
|
|
|
}
|
|
|
|
|
2007-08-04 02:44:11 +00:00
|
|
|
const UnicodeString*
|
2007-08-03 18:43:04 +00:00
|
|
|
DateTimePatternGenerator::getBestRaw(DateTimeMatcher& source,
|
|
|
|
int32_t includeMask,
|
2008-11-18 18:27:40 +00:00
|
|
|
DistanceInfo* missingFields,
|
|
|
|
const PtnSkeleton** specifiedSkeletonPtr) {
|
2007-08-03 18:43:04 +00:00
|
|
|
int32_t bestDistance = 0x7fffffff;
|
|
|
|
DistanceInfo tempInfo;
|
2007-08-04 02:44:11 +00:00
|
|
|
const UnicodeString *bestPattern=NULL;
|
2008-11-18 18:27:40 +00:00
|
|
|
const PtnSkeleton* specifiedSkeleton=NULL;
|
2007-08-03 18:43:04 +00:00
|
|
|
|
|
|
|
PatternMapIterator it;
|
|
|
|
for (it.set(*patternMap); it.hasNext(); ) {
|
|
|
|
DateTimeMatcher trial = it.next();
|
|
|
|
if (trial.equals(skipMatcher)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
int32_t distance=source.getDistance(trial, includeMask, tempInfo);
|
|
|
|
if (distance<bestDistance) {
|
|
|
|
bestDistance=distance;
|
2008-11-18 18:27:40 +00:00
|
|
|
bestPattern=patternMap->getPatternFromSkeleton(*trial.getSkeletonPtr(), &specifiedSkeleton);
|
2007-08-03 18:43:04 +00:00
|
|
|
missingFields->setTo(tempInfo);
|
|
|
|
if (distance==0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-18 18:27:40 +00:00
|
|
|
// If the best raw match had a specified skeleton and that skeleton was requested by the caller,
|
|
|
|
// then return it too. This generally happens when the caller needs to pass that skeleton
|
|
|
|
// through to adjustFieldTypes so the latter can do a better job.
|
|
|
|
if (bestPattern && specifiedSkeletonPtr) {
|
|
|
|
*specifiedSkeletonPtr = specifiedSkeleton;
|
|
|
|
}
|
2007-08-03 18:43:04 +00:00
|
|
|
return bestPattern;
|
|
|
|
}
|
|
|
|
|
|
|
|
UnicodeString
|
|
|
|
DateTimePatternGenerator::adjustFieldTypes(const UnicodeString& pattern,
|
2008-11-18 18:27:40 +00:00
|
|
|
const PtnSkeleton* specifiedSkeleton,
|
2009-12-10 09:13:49 +00:00
|
|
|
UBool fixFractionalSeconds,
|
|
|
|
UDateTimePatternMatchOptions options) {
|
2007-08-04 04:13:16 +00:00
|
|
|
UnicodeString newPattern;
|
2007-08-03 18:43:04 +00:00
|
|
|
fp->set(pattern);
|
|
|
|
for (int32_t i=0; i < fp->itemNumber; i++) {
|
|
|
|
UnicodeString field = fp->items[i];
|
|
|
|
if ( fp->isQuoteLiteral(field) ) {
|
|
|
|
|
2007-08-04 04:13:16 +00:00
|
|
|
UnicodeString quoteLiteral;
|
2007-08-03 18:43:04 +00:00
|
|
|
fp->getQuoteLiteral(quoteLiteral, &i);
|
|
|
|
newPattern += quoteLiteral;
|
|
|
|
}
|
|
|
|
else {
|
2007-08-07 03:14:19 +00:00
|
|
|
if (fp->isPatternSeparator(field)) {
|
|
|
|
newPattern+=field;
|
|
|
|
continue;
|
|
|
|
}
|
2007-08-03 18:43:04 +00:00
|
|
|
int32_t canonicalIndex = fp->getCanonicalIndex(field);
|
|
|
|
if (canonicalIndex < 0) {
|
2007-08-07 03:14:19 +00:00
|
|
|
newPattern+=field;
|
|
|
|
continue; // don't adjust
|
2007-08-03 18:43:04 +00:00
|
|
|
}
|
|
|
|
const dtTypeElem *row = &dtTypes[canonicalIndex];
|
|
|
|
int32_t typeValue = row->field;
|
|
|
|
if (fixFractionalSeconds && typeValue == UDATPG_SECOND_FIELD) {
|
|
|
|
UnicodeString newField=dtMatcher->skeleton.original[UDATPG_FRACTIONAL_SECOND_FIELD];
|
|
|
|
field = field + decimal + newField;
|
2011-02-10 04:46:32 +00:00
|
|
|
} else if (dtMatcher->skeleton.type[typeValue]!=0) {
|
2008-11-18 18:27:40 +00:00
|
|
|
// Here:
|
|
|
|
// - "reqField" is the field from the originally requested skeleton, with length
|
|
|
|
// "reqFieldLen".
|
|
|
|
// - "field" is the field from the found pattern.
|
|
|
|
//
|
|
|
|
// The adjusted field should consist of characters from the originally requested
|
2010-10-20 22:06:10 +00:00
|
|
|
// skeleton, except in the case of UDATPG_HOUR_FIELD or UDATPG_MONTH_FIELD or
|
2012-02-17 08:42:10 +00:00
|
|
|
// UDATPG_WEEKDAY_FIELD or UDATPG_YEAR_FIELD, in which case it should consist
|
|
|
|
// of characters from the found pattern.
|
2008-11-18 18:27:40 +00:00
|
|
|
//
|
|
|
|
// The length of the adjusted field (adjFieldLen) should match that in the originally
|
2009-12-10 09:13:49 +00:00
|
|
|
// requested skeleton, except that in the following cases the length of the adjusted field
|
|
|
|
// should match that in the found pattern (i.e. the length of this pattern field should
|
|
|
|
// not be adjusted):
|
2010-02-22 00:58:52 +00:00
|
|
|
// 1. typeValue is UDATPG_HOUR_FIELD/MINUTE/SECOND and the corresponding bit in options is
|
|
|
|
// not set (ticket #7180). Note, we may want to implement a similar change for other
|
|
|
|
// numeric fields (MM, dd, etc.) so the default behavior is to get locale preference for
|
|
|
|
// field length, but options bits can be used to override this.
|
2009-12-10 09:13:49 +00:00
|
|
|
// 2. There is a specified skeleton for the found pattern and one of the following is true:
|
|
|
|
// a) The length of the field in the skeleton (skelFieldLen) is equal to reqFieldLen.
|
|
|
|
// b) The pattern field is numeric and the skeleton field is not, or vice versa.
|
2008-11-18 18:27:40 +00:00
|
|
|
|
|
|
|
UnicodeString reqField = dtMatcher->skeleton.original[typeValue];
|
|
|
|
int32_t reqFieldLen = reqField.length();
|
2010-10-20 22:06:10 +00:00
|
|
|
if (reqField.charAt(0) == CAP_E && reqFieldLen < 3)
|
2011-02-10 04:46:32 +00:00
|
|
|
reqFieldLen = 3; // 1-3 for E are equivalent to 3 for c,e
|
2008-11-18 18:27:40 +00:00
|
|
|
int32_t adjFieldLen = reqFieldLen;
|
2010-02-22 00:58:52 +00:00
|
|
|
if ( (typeValue==UDATPG_HOUR_FIELD && (options & UDATPG_MATCH_HOUR_FIELD_LENGTH)==0) ||
|
|
|
|
(typeValue==UDATPG_MINUTE_FIELD && (options & UDATPG_MATCH_MINUTE_FIELD_LENGTH)==0) ||
|
|
|
|
(typeValue==UDATPG_SECOND_FIELD && (options & UDATPG_MATCH_SECOND_FIELD_LENGTH)==0) ) {
|
2009-12-10 09:13:49 +00:00
|
|
|
adjFieldLen = field.length();
|
|
|
|
} else if (specifiedSkeleton) {
|
2008-11-18 18:27:40 +00:00
|
|
|
UnicodeString skelField = specifiedSkeleton->original[typeValue];
|
|
|
|
int32_t skelFieldLen = skelField.length();
|
|
|
|
UBool patFieldIsNumeric = (row->type > 0);
|
|
|
|
UBool skelFieldIsNumeric = (specifiedSkeleton->type[typeValue] > 0);
|
|
|
|
if (skelFieldLen == reqFieldLen || (patFieldIsNumeric && !skelFieldIsNumeric) || (skelFieldIsNumeric && !patFieldIsNumeric)) {
|
|
|
|
// don't adjust the field length in the found pattern
|
|
|
|
adjFieldLen = field.length();
|
2007-08-03 18:43:04 +00:00
|
|
|
}
|
|
|
|
}
|
2012-02-17 08:42:10 +00:00
|
|
|
UChar c = (typeValue!= UDATPG_HOUR_FIELD && typeValue!= UDATPG_MONTH_FIELD &&
|
|
|
|
typeValue!= UDATPG_WEEKDAY_FIELD && typeValue!= UDATPG_YEAR_FIELD)?
|
2010-10-20 22:06:10 +00:00
|
|
|
reqField.charAt(0): field.charAt(0);
|
2008-11-18 18:27:40 +00:00
|
|
|
field.remove();
|
|
|
|
for (int32_t i=adjFieldLen; i>0; --i) {
|
|
|
|
field+=c;
|
|
|
|
}
|
2007-08-03 18:43:04 +00:00
|
|
|
}
|
2011-02-10 04:46:32 +00:00
|
|
|
newPattern+=field;
|
2007-08-03 18:43:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return newPattern;
|
|
|
|
}
|
|
|
|
|
|
|
|
UnicodeString
|
2009-12-10 09:13:49 +00:00
|
|
|
DateTimePatternGenerator::getBestAppending(int32_t missingFields, UDateTimePatternMatchOptions options) {
|
2011-02-10 04:46:32 +00:00
|
|
|
UnicodeString resultPattern, tempPattern;
|
2007-08-03 18:43:04 +00:00
|
|
|
UErrorCode err=U_ZERO_ERROR;
|
|
|
|
int32_t lastMissingFieldMask=0;
|
|
|
|
if (missingFields!=0) {
|
2007-08-04 04:13:16 +00:00
|
|
|
resultPattern=UnicodeString();
|
2008-11-18 18:27:40 +00:00
|
|
|
const PtnSkeleton* specifiedSkeleton=NULL;
|
|
|
|
tempPattern = *getBestRaw(*dtMatcher, missingFields, distanceInfo, &specifiedSkeleton);
|
2009-12-10 09:13:49 +00:00
|
|
|
resultPattern = adjustFieldTypes(tempPattern, specifiedSkeleton, FALSE, options);
|
2007-08-03 18:43:04 +00:00
|
|
|
if ( distanceInfo->missingFieldMask==0 ) {
|
|
|
|
return resultPattern;
|
|
|
|
}
|
|
|
|
while (distanceInfo->missingFieldMask!=0) { // precondition: EVERY single field must work!
|
|
|
|
if ( lastMissingFieldMask == distanceInfo->missingFieldMask ) {
|
|
|
|
break; // cannot find the proper missing field
|
|
|
|
}
|
|
|
|
if (((distanceInfo->missingFieldMask & UDATPG_SECOND_AND_FRACTIONAL_MASK)==UDATPG_FRACTIONAL_MASK) &&
|
|
|
|
((missingFields & UDATPG_SECOND_AND_FRACTIONAL_MASK) == UDATPG_SECOND_AND_FRACTIONAL_MASK)) {
|
2011-02-10 04:46:32 +00:00
|
|
|
resultPattern = adjustFieldTypes(resultPattern, specifiedSkeleton, TRUE, options);
|
2007-08-03 18:43:04 +00:00
|
|
|
distanceInfo->missingFieldMask &= ~UDATPG_FRACTIONAL_MASK;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
int32_t startingMask = distanceInfo->missingFieldMask;
|
2008-11-18 18:27:40 +00:00
|
|
|
tempPattern = *getBestRaw(*dtMatcher, distanceInfo->missingFieldMask, distanceInfo, &specifiedSkeleton);
|
2009-12-10 09:13:49 +00:00
|
|
|
tempPattern = adjustFieldTypes(tempPattern, specifiedSkeleton, FALSE, options);
|
2007-08-03 18:43:04 +00:00
|
|
|
int32_t foundMask=startingMask& ~distanceInfo->missingFieldMask;
|
|
|
|
int32_t topField=getTopBitNumber(foundMask);
|
|
|
|
UnicodeString appendName;
|
|
|
|
getAppendName((UDateTimePatternField)topField, appendName);
|
|
|
|
const Formattable formatPattern[] = {
|
|
|
|
resultPattern,
|
|
|
|
tempPattern,
|
|
|
|
appendName
|
|
|
|
};
|
2008-02-08 07:10:28 +00:00
|
|
|
UnicodeString emptyStr;
|
2011-02-10 04:46:32 +00:00
|
|
|
resultPattern = MessageFormat::format(appendItemFormats[topField], formatPattern, 3, emptyStr, err);
|
2007-08-03 18:43:04 +00:00
|
|
|
lastMissingFieldMask = distanceInfo->missingFieldMask;
|
|
|
|
}
|
|
|
|
}
|
2011-02-10 04:46:32 +00:00
|
|
|
return resultPattern;
|
2007-08-03 18:43:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int32_t
|
|
|
|
DateTimePatternGenerator::getTopBitNumber(int32_t foundMask) {
|
|
|
|
if ( foundMask==0 ) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
int32_t i=0;
|
|
|
|
while (foundMask!=0) {
|
|
|
|
foundMask >>=1;
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
if (i-1 >UDATPG_ZONE_FIELD) {
|
|
|
|
return UDATPG_ZONE_FIELD;
|
|
|
|
}
|
2009-04-22 23:10:07 +00:00
|
|
|
else
|
2007-08-03 18:43:04 +00:00
|
|
|
return i-1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2007-08-04 04:13:16 +00:00
|
|
|
DateTimePatternGenerator::setAvailableFormat(const UnicodeString &key, UErrorCode& err)
|
|
|
|
{
|
2007-08-03 18:43:04 +00:00
|
|
|
fAvailableFormatKeyHash->puti(key, 1, err);
|
|
|
|
}
|
|
|
|
|
|
|
|
UBool
|
2007-08-23 05:48:44 +00:00
|
|
|
DateTimePatternGenerator::isAvailableFormatSet(const UnicodeString &key) const {
|
2007-08-04 14:19:29 +00:00
|
|
|
return (UBool)(fAvailableFormatKeyHash->geti(key) == 1);
|
2007-08-03 18:43:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2008-02-17 18:26:39 +00:00
|
|
|
DateTimePatternGenerator::copyHashtable(Hashtable *other, UErrorCode &status) {
|
2007-08-03 18:43:04 +00:00
|
|
|
|
|
|
|
if (other == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
2008-02-13 09:35:50 +00:00
|
|
|
if (fAvailableFormatKeyHash != NULL) {
|
|
|
|
delete fAvailableFormatKeyHash;
|
|
|
|
fAvailableFormatKeyHash = NULL;
|
|
|
|
}
|
2008-02-17 18:26:39 +00:00
|
|
|
initHashtable(status);
|
|
|
|
if(U_FAILURE(status)){
|
2007-08-03 18:43:04 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
int32_t pos = -1;
|
|
|
|
const UHashElement* elem = NULL;
|
|
|
|
// walk through the hash table and create a deep clone
|
|
|
|
while((elem = other->nextElement(pos))!= NULL){
|
|
|
|
const UHashTok otherKeyTok = elem->key;
|
|
|
|
UnicodeString* otherKey = (UnicodeString*)otherKeyTok.pointer;
|
2008-02-17 18:26:39 +00:00
|
|
|
fAvailableFormatKeyHash->puti(*otherKey, 1, status);
|
|
|
|
if(U_FAILURE(status)){
|
2007-08-03 18:43:04 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
StringEnumeration*
|
|
|
|
DateTimePatternGenerator::getSkeletons(UErrorCode& status) const {
|
|
|
|
StringEnumeration* skeletonEnumerator = new DTSkeletonEnumeration(*patternMap, DT_SKELETON, status);
|
|
|
|
return skeletonEnumerator;
|
|
|
|
}
|
|
|
|
|
|
|
|
const UnicodeString&
|
|
|
|
DateTimePatternGenerator::getPatternForSkeleton(const UnicodeString& skeleton) const {
|
|
|
|
PtnElem *curElem;
|
2009-04-22 23:10:07 +00:00
|
|
|
|
2007-08-03 18:43:04 +00:00
|
|
|
if (skeleton.length() ==0) {
|
|
|
|
return emptyString;
|
2009-04-22 23:10:07 +00:00
|
|
|
}
|
2007-08-07 03:14:19 +00:00
|
|
|
curElem = patternMap->getHeader(skeleton.charAt(0));
|
2007-08-03 18:43:04 +00:00
|
|
|
while ( curElem != NULL ) {
|
|
|
|
if ( curElem->skeleton->getSkeleton()==skeleton ) {
|
2007-08-04 02:44:11 +00:00
|
|
|
return curElem->pattern;
|
2007-08-03 18:43:04 +00:00
|
|
|
}
|
|
|
|
curElem=curElem->next;
|
|
|
|
}
|
|
|
|
return emptyString;
|
|
|
|
}
|
|
|
|
|
|
|
|
StringEnumeration*
|
|
|
|
DateTimePatternGenerator::getBaseSkeletons(UErrorCode& status) const {
|
|
|
|
StringEnumeration* baseSkeletonEnumerator = new DTSkeletonEnumeration(*patternMap, DT_BASESKELETON, status);
|
|
|
|
return baseSkeletonEnumerator;
|
|
|
|
}
|
|
|
|
|
|
|
|
StringEnumeration*
|
|
|
|
DateTimePatternGenerator::getRedundants(UErrorCode& status) {
|
|
|
|
StringEnumeration* output = new DTRedundantEnumeration();
|
2007-08-04 02:44:11 +00:00
|
|
|
const UnicodeString *pattern;
|
2007-08-03 18:43:04 +00:00
|
|
|
PatternMapIterator it;
|
|
|
|
for (it.set(*patternMap); it.hasNext(); ) {
|
|
|
|
DateTimeMatcher current = it.next();
|
|
|
|
pattern = patternMap->getPatternFromSkeleton(*(it.getSkeleton()));
|
|
|
|
if ( isCanonicalItem(*pattern) ) {
|
|
|
|
continue;
|
|
|
|
}
|
2007-08-29 05:44:57 +00:00
|
|
|
if ( skipMatcher == NULL ) {
|
|
|
|
skipMatcher = new DateTimeMatcher(current);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
*skipMatcher = current;
|
|
|
|
}
|
2007-08-03 18:43:04 +00:00
|
|
|
UnicodeString trial = getBestPattern(current.getPattern(), status);
|
2009-04-22 23:10:07 +00:00
|
|
|
if (trial == *pattern) {
|
2007-08-03 18:43:04 +00:00
|
|
|
((DTRedundantEnumeration *)output)->add(*pattern, status);
|
|
|
|
}
|
|
|
|
if (current.equals(skipMatcher)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
|
|
|
UBool
|
2007-08-23 05:48:44 +00:00
|
|
|
DateTimePatternGenerator::isCanonicalItem(const UnicodeString& item) const {
|
2007-08-03 18:43:04 +00:00
|
|
|
if ( item.length() != 1 ) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
for (int32_t i=0; i<UDATPG_FIELD_COUNT; ++i) {
|
|
|
|
if (item.charAt(0)==Canonical_Items[i]) {
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DateTimePatternGenerator*
|
|
|
|
DateTimePatternGenerator::clone() const {
|
|
|
|
return new DateTimePatternGenerator(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
PatternMap::PatternMap() {
|
|
|
|
for (int32_t i=0; i < MAX_PATTERN_ENTRIES; ++i ) {
|
|
|
|
boot[i]=NULL;
|
|
|
|
}
|
|
|
|
isDupAllowed = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
PatternMap::copyFrom(const PatternMap& other, UErrorCode& status) {
|
|
|
|
this->isDupAllowed = other.isDupAllowed;
|
|
|
|
for (int32_t bootIndex=0; bootIndex<MAX_PATTERN_ENTRIES; ++bootIndex ) {
|
|
|
|
PtnElem *curElem, *otherElem, *prevElem=NULL;
|
|
|
|
otherElem = other.boot[bootIndex];
|
|
|
|
while (otherElem!=NULL) {
|
2007-08-07 23:16:36 +00:00
|
|
|
if ((curElem = new PtnElem(otherElem->basePattern, otherElem->pattern))==NULL) {
|
2007-08-03 18:43:04 +00:00
|
|
|
// out of memory
|
|
|
|
status = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
return;
|
|
|
|
}
|
2007-08-07 23:16:36 +00:00
|
|
|
if ( this->boot[bootIndex]== NULL ) {
|
|
|
|
this->boot[bootIndex] = curElem;
|
|
|
|
}
|
2007-08-03 18:43:04 +00:00
|
|
|
if ((curElem->skeleton=new PtnSkeleton(*(otherElem->skeleton))) == NULL ) {
|
|
|
|
// out of memory
|
|
|
|
status = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (prevElem!=NULL) {
|
|
|
|
prevElem->next=curElem;
|
|
|
|
}
|
|
|
|
curElem->next=NULL;
|
|
|
|
prevElem = curElem;
|
|
|
|
otherElem = otherElem->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-04-22 23:10:07 +00:00
|
|
|
PtnElem*
|
2007-08-07 03:14:19 +00:00
|
|
|
PatternMap::getHeader(UChar baseChar) {
|
|
|
|
PtnElem* curElem;
|
2009-04-22 23:10:07 +00:00
|
|
|
|
2007-08-07 03:14:19 +00:00
|
|
|
if ( (baseChar >= CAP_A) && (baseChar <= CAP_Z) ) {
|
|
|
|
curElem = boot[baseChar-CAP_A];
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if ( (baseChar >=LOW_A) && (baseChar <= LOW_Z) ) {
|
|
|
|
curElem = boot[26+baseChar-LOW_A];
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
2007-08-07 06:13:19 +00:00
|
|
|
return curElem;
|
2007-08-07 03:14:19 +00:00
|
|
|
}
|
2009-04-22 23:10:07 +00:00
|
|
|
|
2007-08-03 18:43:04 +00:00
|
|
|
PatternMap::~PatternMap() {
|
|
|
|
for (int32_t i=0; i < MAX_PATTERN_ENTRIES; ++i ) {
|
|
|
|
if (boot[i]!=NULL ) {
|
|
|
|
delete boot[i];
|
|
|
|
boot[i]=NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // PatternMap destructor
|
|
|
|
|
|
|
|
void
|
|
|
|
PatternMap::add(const UnicodeString& basePattern,
|
|
|
|
const PtnSkeleton& skeleton,
|
|
|
|
const UnicodeString& value,// mapped pattern value
|
2008-11-18 18:27:40 +00:00
|
|
|
UBool skeletonWasSpecified,
|
2007-08-03 18:43:04 +00:00
|
|
|
UErrorCode &status) {
|
|
|
|
UChar baseChar = basePattern.charAt(0);
|
|
|
|
PtnElem *curElem, *baseElem;
|
|
|
|
status = U_ZERO_ERROR;
|
|
|
|
|
|
|
|
// the baseChar must be A-Z or a-z
|
|
|
|
if ((baseChar >= CAP_A) && (baseChar <= CAP_Z)) {
|
|
|
|
baseElem = boot[baseChar-CAP_A];
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if ((baseChar >=LOW_A) && (baseChar <= LOW_Z)) {
|
|
|
|
baseElem = boot[26+baseChar-LOW_A];
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
status = U_ILLEGAL_CHARACTER;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (baseElem == NULL) {
|
2007-08-04 02:44:11 +00:00
|
|
|
if ((curElem = new PtnElem(basePattern, value)) == NULL ) {
|
2007-08-03 18:43:04 +00:00
|
|
|
// out of memory
|
|
|
|
status = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (baseChar >= LOW_A) {
|
2007-08-23 05:48:44 +00:00
|
|
|
boot[26 + (baseChar-LOW_A)] = curElem;
|
2007-08-03 18:43:04 +00:00
|
|
|
}
|
|
|
|
else {
|
2007-08-23 05:48:44 +00:00
|
|
|
boot[baseChar-CAP_A] = curElem;
|
2007-08-03 18:43:04 +00:00
|
|
|
}
|
2007-08-23 05:48:44 +00:00
|
|
|
curElem->skeleton = new PtnSkeleton(skeleton);
|
2008-11-18 18:27:40 +00:00
|
|
|
curElem->skeletonWasSpecified = skeletonWasSpecified;
|
2007-08-03 18:43:04 +00:00
|
|
|
}
|
|
|
|
if ( baseElem != NULL ) {
|
|
|
|
curElem = getDuplicateElem(basePattern, skeleton, baseElem);
|
|
|
|
|
|
|
|
if (curElem == NULL) {
|
|
|
|
// add new element to the list.
|
|
|
|
curElem = baseElem;
|
2007-08-23 05:48:44 +00:00
|
|
|
while( curElem -> next != NULL )
|
2007-08-03 18:43:04 +00:00
|
|
|
{
|
2007-08-23 05:48:44 +00:00
|
|
|
curElem = curElem->next;
|
|
|
|
}
|
|
|
|
if ((curElem->next = new PtnElem(basePattern, value)) == NULL ) {
|
|
|
|
// out of memory
|
|
|
|
status = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
return;
|
2007-08-03 18:43:04 +00:00
|
|
|
}
|
2007-08-23 05:48:44 +00:00
|
|
|
curElem=curElem->next;
|
2007-08-07 03:14:19 +00:00
|
|
|
curElem->skeleton = new PtnSkeleton(skeleton);
|
2008-11-18 18:27:40 +00:00
|
|
|
curElem->skeletonWasSpecified = skeletonWasSpecified;
|
2007-08-03 18:43:04 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Pattern exists in the list already.
|
|
|
|
if ( !isDupAllowed ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Overwrite the value.
|
2007-08-04 02:44:11 +00:00
|
|
|
curElem->pattern = value;
|
2007-08-03 18:43:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} // PatternMap::add
|
|
|
|
|
|
|
|
// Find the pattern from the given basePattern string.
|
2007-08-04 02:44:11 +00:00
|
|
|
const UnicodeString *
|
2008-11-18 18:27:40 +00:00
|
|
|
PatternMap::getPatternFromBasePattern(UnicodeString& basePattern, UBool& skeletonWasSpecified) { // key to search for
|
2007-08-04 02:44:11 +00:00
|
|
|
PtnElem *curElem;
|
2007-08-03 18:43:04 +00:00
|
|
|
|
2007-08-07 03:14:19 +00:00
|
|
|
if ((curElem=getHeader(basePattern.charAt(0)))==NULL) {
|
2007-08-04 14:19:29 +00:00
|
|
|
return NULL; // no match
|
2007-08-03 18:43:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
2007-08-04 14:19:29 +00:00
|
|
|
if ( basePattern.compare(curElem->basePattern)==0 ) {
|
2008-11-18 18:27:40 +00:00
|
|
|
skeletonWasSpecified = curElem->skeletonWasSpecified;
|
2007-08-04 14:19:29 +00:00
|
|
|
return &(curElem->pattern);
|
|
|
|
}
|
|
|
|
curElem=curElem->next;
|
2007-08-03 18:43:04 +00:00
|
|
|
}while (curElem != NULL);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
} // PatternMap::getFromBasePattern
|
|
|
|
|
|
|
|
|
|
|
|
// Find the pattern from the given skeleton.
|
2008-11-18 18:27:40 +00:00
|
|
|
// At least when this is called from getBestRaw & addPattern (in which case specifiedSkeletonPtr is non-NULL),
|
|
|
|
// the comparison should be based on skeleton.original (which is unique and tied to the distance measurement in bestRaw)
|
|
|
|
// and not skeleton.baseOriginal (which is not unique); otherwise we may pick a different skeleton than the one with the
|
|
|
|
// optimum distance value in getBestRaw. When this is called from public getRedundants (specifiedSkeletonPtr is NULL),
|
|
|
|
// for now it will continue to compare based on baseOriginal so as not to change the behavior unnecessarily.
|
2007-08-04 02:44:11 +00:00
|
|
|
const UnicodeString *
|
2008-11-18 18:27:40 +00:00
|
|
|
PatternMap::getPatternFromSkeleton(PtnSkeleton& skeleton, const PtnSkeleton** specifiedSkeletonPtr) { // key to search for
|
2007-08-04 02:44:11 +00:00
|
|
|
PtnElem *curElem;
|
2007-08-03 18:43:04 +00:00
|
|
|
|
2008-11-18 18:27:40 +00:00
|
|
|
if (specifiedSkeletonPtr) {
|
|
|
|
*specifiedSkeletonPtr = NULL;
|
|
|
|
}
|
|
|
|
|
2007-08-03 18:43:04 +00:00
|
|
|
// find boot entry
|
|
|
|
UChar baseChar='\0';
|
|
|
|
for (int32_t i=0; i<UDATPG_FIELD_COUNT; ++i) {
|
|
|
|
if (skeleton.baseOriginal[i].length() !=0 ) {
|
|
|
|
baseChar = skeleton.baseOriginal[i].charAt(0);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2009-04-22 23:10:07 +00:00
|
|
|
|
2007-08-07 03:14:19 +00:00
|
|
|
if ((curElem=getHeader(baseChar))==NULL) {
|
2007-08-04 14:19:29 +00:00
|
|
|
return NULL; // no match
|
2007-08-03 18:43:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
|
|
|
int32_t i=0;
|
2008-11-18 18:27:40 +00:00
|
|
|
if (specifiedSkeletonPtr != NULL) { // called from DateTimePatternGenerator::getBestRaw or addPattern, use original
|
|
|
|
for (i=0; i<UDATPG_FIELD_COUNT; ++i) {
|
|
|
|
if (curElem->skeleton->original[i].compare(skeleton.original[i]) != 0 )
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else { // called from DateTimePatternGenerator::getRedundants, use baseOriginal
|
|
|
|
for (i=0; i<UDATPG_FIELD_COUNT; ++i) {
|
|
|
|
if (curElem->skeleton->baseOriginal[i].compare(skeleton.baseOriginal[i]) != 0 )
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2007-08-03 18:43:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (i == UDATPG_FIELD_COUNT) {
|
2008-11-18 18:27:40 +00:00
|
|
|
if (specifiedSkeletonPtr && curElem->skeletonWasSpecified) {
|
|
|
|
*specifiedSkeletonPtr = curElem->skeleton;
|
|
|
|
}
|
2007-08-04 02:44:11 +00:00
|
|
|
return &(curElem->pattern);
|
2007-08-03 18:43:04 +00:00
|
|
|
}
|
|
|
|
curElem=curElem->next;
|
|
|
|
}while (curElem != NULL);
|
|
|
|
|
|
|
|
return NULL;
|
2009-04-22 23:10:07 +00:00
|
|
|
}
|
2007-08-03 18:43:04 +00:00
|
|
|
|
|
|
|
UBool
|
|
|
|
PatternMap::equals(const PatternMap& other) {
|
|
|
|
if ( this==&other ) {
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
for (int32_t bootIndex=0; bootIndex<MAX_PATTERN_ENTRIES; ++bootIndex ) {
|
|
|
|
if ( boot[bootIndex]==other.boot[bootIndex] ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ( (boot[bootIndex]==NULL)||(other.boot[bootIndex]==NULL) ) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
PtnElem *otherElem = other.boot[bootIndex];
|
|
|
|
PtnElem *myElem = boot[bootIndex];
|
|
|
|
while ((otherElem!=NULL) || (myElem!=NULL)) {
|
|
|
|
if ( myElem == otherElem ) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if ((otherElem==NULL) || (myElem==NULL)) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
2007-08-04 02:44:11 +00:00
|
|
|
if ( (myElem->basePattern != otherElem->basePattern) ||
|
|
|
|
(myElem->pattern != otherElem->pattern) ) {
|
2007-08-03 18:43:04 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
if ((myElem->skeleton!=otherElem->skeleton)&&
|
|
|
|
!myElem->skeleton->equals(*(otherElem->skeleton))) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
myElem = myElem->next;
|
|
|
|
otherElem=otherElem->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// find any key existing in the mapping table already.
|
|
|
|
// return TRUE if there is an existing key, otherwise return FALSE.
|
|
|
|
PtnElem*
|
|
|
|
PatternMap::getDuplicateElem(
|
|
|
|
const UnicodeString &basePattern,
|
|
|
|
const PtnSkeleton &skeleton,
|
|
|
|
PtnElem *baseElem) {
|
|
|
|
PtnElem *curElem;
|
|
|
|
|
|
|
|
if ( baseElem == (PtnElem *)NULL ) {
|
|
|
|
return (PtnElem*)NULL;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
curElem = baseElem;
|
|
|
|
}
|
|
|
|
do {
|
2007-08-04 02:44:11 +00:00
|
|
|
if ( basePattern.compare(curElem->basePattern)==0 ) {
|
2007-08-03 18:43:04 +00:00
|
|
|
UBool isEqual=TRUE;
|
|
|
|
for (int32_t i=0; i<UDATPG_FIELD_COUNT; ++i) {
|
|
|
|
if (curElem->skeleton->type[i] != skeleton.type[i] ) {
|
|
|
|
isEqual=FALSE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (isEqual) {
|
|
|
|
return curElem;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
curElem = curElem->next;
|
|
|
|
} while( curElem != (PtnElem *)NULL );
|
|
|
|
|
|
|
|
// end of the list
|
|
|
|
return (PtnElem*)NULL;
|
|
|
|
|
|
|
|
} // PatternMap::getDuplicateElem
|
|
|
|
|
|
|
|
DateTimeMatcher::DateTimeMatcher(void) {
|
|
|
|
}
|
|
|
|
|
2011-07-26 05:32:25 +00:00
|
|
|
DateTimeMatcher::~DateTimeMatcher() {}
|
|
|
|
|
2007-08-29 05:44:57 +00:00
|
|
|
DateTimeMatcher::DateTimeMatcher(const DateTimeMatcher& other) {
|
|
|
|
copyFrom(other.skeleton);
|
|
|
|
}
|
|
|
|
|
2007-08-03 18:43:04 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
DateTimeMatcher::set(const UnicodeString& pattern, FormatParser* fp) {
|
2007-08-31 05:20:56 +00:00
|
|
|
PtnSkeleton localSkeleton;
|
|
|
|
return set(pattern, fp, localSkeleton);
|
2007-08-03 18:43:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2007-08-31 05:20:56 +00:00
|
|
|
DateTimeMatcher::set(const UnicodeString& pattern, FormatParser* fp, PtnSkeleton& skeletonResult) {
|
2007-09-06 18:00:08 +00:00
|
|
|
int32_t i;
|
|
|
|
for (i=0; i<UDATPG_FIELD_COUNT; ++i) {
|
2007-08-31 18:19:41 +00:00
|
|
|
skeletonResult.type[i]=NONE;
|
2007-08-03 18:43:04 +00:00
|
|
|
}
|
|
|
|
fp->set(pattern);
|
2007-09-06 18:00:08 +00:00
|
|
|
for (i=0; i < fp->itemNumber; i++) {
|
2007-08-03 18:43:04 +00:00
|
|
|
UnicodeString field = fp->items[i];
|
|
|
|
if ( field.charAt(0) == LOW_A ) {
|
|
|
|
continue; // skip 'a'
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( fp->isQuoteLiteral(field) ) {
|
2007-08-04 04:13:16 +00:00
|
|
|
UnicodeString quoteLiteral;
|
2007-08-03 18:43:04 +00:00
|
|
|
fp->getQuoteLiteral(quoteLiteral, &i);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
int32_t canonicalIndex = fp->getCanonicalIndex(field);
|
|
|
|
if (canonicalIndex < 0 ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
const dtTypeElem *row = &dtTypes[canonicalIndex];
|
|
|
|
int32_t typeValue = row->field;
|
2007-08-31 05:20:56 +00:00
|
|
|
skeletonResult.original[typeValue]=field;
|
2007-08-03 18:43:04 +00:00
|
|
|
UChar repeatChar = row->patternChar;
|
2011-12-05 04:49:40 +00:00
|
|
|
int32_t repeatCount = row->minLen; // #7930 removes cap at 3
|
2007-08-03 18:43:04 +00:00
|
|
|
while (repeatCount-- > 0) {
|
2007-08-31 05:20:56 +00:00
|
|
|
skeletonResult.baseOriginal[typeValue] += repeatChar;
|
2007-08-03 18:43:04 +00:00
|
|
|
}
|
|
|
|
int16_t subTypeValue = row->type;
|
|
|
|
if ( row->type > 0) {
|
|
|
|
subTypeValue += field.length();
|
|
|
|
}
|
2008-08-07 17:06:38 +00:00
|
|
|
skeletonResult.type[typeValue] = subTypeValue;
|
2007-08-03 18:43:04 +00:00
|
|
|
}
|
2007-08-31 05:20:56 +00:00
|
|
|
copyFrom(skeletonResult);
|
2007-08-03 18:43:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DateTimeMatcher::getBasePattern(UnicodeString &result ) {
|
|
|
|
result.remove(); // Reset the result first.
|
|
|
|
for (int32_t i=0; i<UDATPG_FIELD_COUNT; ++i ) {
|
|
|
|
if (skeleton.baseOriginal[i].length()!=0) {
|
|
|
|
result += skeleton.baseOriginal[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
UnicodeString
|
|
|
|
DateTimeMatcher::getPattern() {
|
|
|
|
UnicodeString result;
|
2009-04-22 23:10:07 +00:00
|
|
|
|
2007-08-03 18:43:04 +00:00
|
|
|
for (int32_t i=0; i<UDATPG_FIELD_COUNT; ++i ) {
|
|
|
|
if (skeleton.original[i].length()!=0) {
|
|
|
|
result += skeleton.original[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t
|
2007-08-23 05:48:44 +00:00
|
|
|
DateTimeMatcher::getDistance(const DateTimeMatcher& other, int32_t includeMask, DistanceInfo& distanceInfo) {
|
2007-08-03 18:43:04 +00:00
|
|
|
int32_t result=0;
|
|
|
|
distanceInfo.clear();
|
|
|
|
for (int32_t i=0; i<UDATPG_FIELD_COUNT; ++i ) {
|
|
|
|
int32_t myType = (includeMask&(1<<i))==0 ? 0 : skeleton.type[i];
|
|
|
|
int32_t otherType = other.skeleton.type[i];
|
|
|
|
if (myType==otherType) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (myType==0) {// and other is not
|
|
|
|
result += EXTRA_FIELD;
|
|
|
|
distanceInfo.addExtra(i);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (otherType==0) {
|
|
|
|
result += MISSING_FIELD;
|
|
|
|
distanceInfo.addMissing(i);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
result += abs(myType - otherType);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2007-08-31 05:20:56 +00:00
|
|
|
DateTimeMatcher::copyFrom(const PtnSkeleton& newSkeleton) {
|
2007-08-03 18:43:04 +00:00
|
|
|
for (int32_t i=0; i<UDATPG_FIELD_COUNT; ++i) {
|
2007-08-31 05:20:56 +00:00
|
|
|
this->skeleton.type[i]=newSkeleton.type[i];
|
|
|
|
this->skeleton.original[i]=newSkeleton.original[i];
|
|
|
|
this->skeleton.baseOriginal[i]=newSkeleton.baseOriginal[i];
|
2007-08-03 18:43:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DateTimeMatcher::copyFrom() {
|
|
|
|
// same as clear
|
|
|
|
for (int32_t i=0; i<UDATPG_FIELD_COUNT; ++i) {
|
|
|
|
this->skeleton.type[i]=0;
|
2007-08-04 04:13:16 +00:00
|
|
|
this->skeleton.original[i].remove();
|
|
|
|
this->skeleton.baseOriginal[i].remove();
|
2007-08-03 18:43:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
UBool
|
2007-08-23 05:48:44 +00:00
|
|
|
DateTimeMatcher::equals(const DateTimeMatcher* other) const {
|
2007-08-03 18:43:04 +00:00
|
|
|
if (other==NULL) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
for (int32_t i=0; i<UDATPG_FIELD_COUNT; ++i) {
|
|
|
|
if (this->skeleton.original[i]!=other->skeleton.original[i] ) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t
|
|
|
|
DateTimeMatcher::getFieldMask() {
|
|
|
|
int32_t result=0;
|
|
|
|
|
|
|
|
for (int32_t i=0; i<UDATPG_FIELD_COUNT; ++i) {
|
|
|
|
if (skeleton.type[i]!=0) {
|
|
|
|
result |= (1<<i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
PtnSkeleton*
|
|
|
|
DateTimeMatcher::getSkeletonPtr() {
|
|
|
|
return &skeleton;
|
|
|
|
}
|
|
|
|
|
|
|
|
FormatParser::FormatParser () {
|
|
|
|
status = START;
|
|
|
|
itemNumber=0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
FormatParser::~FormatParser () {
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Find the next token with the starting position and length
|
|
|
|
// Note: the startPos may
|
|
|
|
FormatParser::TokenStatus
|
|
|
|
FormatParser::setTokens(const UnicodeString& pattern, int32_t startPos, int32_t *len) {
|
|
|
|
int32_t curLoc = startPos;
|
|
|
|
if ( curLoc >= pattern.length()) {
|
|
|
|
return DONE;
|
|
|
|
}
|
|
|
|
// check the current char is between A-Z or a-z
|
|
|
|
do {
|
|
|
|
UChar c=pattern.charAt(curLoc);
|
|
|
|
if ( (c>=CAP_A && c<=CAP_Z) || (c>=LOW_A && c<=LOW_Z) ) {
|
|
|
|
curLoc++;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
startPos = curLoc;
|
|
|
|
*len=1;
|
|
|
|
return ADD_TOKEN;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( pattern.charAt(curLoc)!= pattern.charAt(startPos) ) {
|
|
|
|
break; // not the same token
|
|
|
|
}
|
|
|
|
} while(curLoc <= pattern.length());
|
|
|
|
*len = curLoc-startPos;
|
|
|
|
return ADD_TOKEN;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
FormatParser::set(const UnicodeString& pattern) {
|
|
|
|
int32_t startPos=0;
|
|
|
|
TokenStatus result=START;
|
|
|
|
int32_t len=0;
|
|
|
|
itemNumber =0;
|
|
|
|
|
|
|
|
do {
|
|
|
|
result = setTokens( pattern, startPos, &len );
|
|
|
|
if ( result == ADD_TOKEN )
|
|
|
|
{
|
|
|
|
items[itemNumber++] = UnicodeString(pattern, startPos, len );
|
|
|
|
startPos += len;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while (result==ADD_TOKEN && itemNumber < MAX_DT_TOKEN);
|
|
|
|
}
|
|
|
|
|
2007-08-06 21:25:31 +00:00
|
|
|
int32_t
|
2009-04-22 16:28:25 +00:00
|
|
|
FormatParser::getCanonicalIndex(const UnicodeString& s, UBool strict) {
|
2007-08-03 18:43:04 +00:00
|
|
|
int32_t len = s.length();
|
2009-04-22 16:28:25 +00:00
|
|
|
if (len == 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
2007-08-03 18:43:04 +00:00
|
|
|
UChar ch = s.charAt(0);
|
|
|
|
|
2009-04-22 16:28:25 +00:00
|
|
|
// Verify that all are the same character.
|
|
|
|
for (int32_t l = 1; l < len; l++) {
|
|
|
|
if (ch != s.charAt(l)) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
int32_t i = 0;
|
|
|
|
int32_t bestRow = -1;
|
|
|
|
while (dtTypes[i].patternChar != '\0') {
|
|
|
|
if ( dtTypes[i].patternChar != ch ) {
|
2007-08-03 18:43:04 +00:00
|
|
|
++i;
|
|
|
|
continue;
|
|
|
|
}
|
2009-04-22 16:28:25 +00:00
|
|
|
bestRow = i;
|
|
|
|
if (dtTypes[i].patternChar != dtTypes[i+1].patternChar) {
|
2007-08-03 18:43:04 +00:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
if (dtTypes[i+1].minLen <= len) {
|
|
|
|
++i;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
return i;
|
|
|
|
}
|
2009-04-22 16:28:25 +00:00
|
|
|
return strict ? -1 : bestRow;
|
2007-08-03 18:43:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
UBool
|
2007-08-23 05:48:44 +00:00
|
|
|
FormatParser::isQuoteLiteral(const UnicodeString& s) const {
|
2007-08-07 03:14:19 +00:00
|
|
|
return (UBool)(s.charAt(0)==SINGLE_QUOTE);
|
2007-08-03 18:43:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// This function aussumes the current itemIndex points to the quote literal.
|
|
|
|
// Please call isQuoteLiteral prior to this function.
|
|
|
|
void
|
|
|
|
FormatParser::getQuoteLiteral(UnicodeString& quote, int32_t *itemIndex) {
|
|
|
|
int32_t i=*itemIndex;
|
2009-04-22 23:10:07 +00:00
|
|
|
|
2007-08-23 05:48:44 +00:00
|
|
|
quote.remove();
|
2007-08-03 18:43:04 +00:00
|
|
|
if (items[i].charAt(0)==SINGLE_QUOTE) {
|
|
|
|
quote += items[i];
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
while ( i < itemNumber ) {
|
|
|
|
if ( items[i].charAt(0)==SINGLE_QUOTE ) {
|
2007-08-22 21:57:11 +00:00
|
|
|
if ( (i+1<itemNumber) && (items[i+1].charAt(0)==SINGLE_QUOTE)) {
|
2007-08-03 18:43:04 +00:00
|
|
|
// two single quotes e.g. 'o''clock'
|
2007-08-23 05:48:44 +00:00
|
|
|
quote += items[i++];
|
|
|
|
quote += items[i++];
|
2007-08-03 18:43:04 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else {
|
2007-08-07 03:14:19 +00:00
|
|
|
quote += items[i];
|
2007-08-03 18:43:04 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
quote += items[i];
|
|
|
|
}
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
*itemIndex=i;
|
|
|
|
}
|
|
|
|
|
|
|
|
UBool
|
|
|
|
FormatParser::isPatternSeparator(UnicodeString& field) {
|
|
|
|
for (int32_t i=0; i<field.length(); ++i ) {
|
|
|
|
UChar c= field.charAt(i);
|
|
|
|
if ( (c==SINGLE_QUOTE) || (c==BACKSLASH) || (c==SPACE) || (c==COLON) ||
|
2007-08-07 03:14:19 +00:00
|
|
|
(c==QUOTATION_MARK) || (c==COMMA) || (c==HYPHEN) ||(items[i].charAt(0)==DOT) ) {
|
2007-08-03 18:43:04 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2011-07-26 05:32:25 +00:00
|
|
|
DistanceInfo::~DistanceInfo() {}
|
|
|
|
|
2007-08-03 18:43:04 +00:00
|
|
|
void
|
|
|
|
DistanceInfo::setTo(DistanceInfo &other) {
|
|
|
|
missingFieldMask = other.missingFieldMask;
|
|
|
|
extraFieldMask= other.extraFieldMask;
|
|
|
|
}
|
|
|
|
|
|
|
|
PatternMapIterator::PatternMapIterator() {
|
|
|
|
bootIndex = 0;
|
|
|
|
nodePtr = NULL;
|
|
|
|
patternMap=NULL;
|
|
|
|
matcher= new DateTimeMatcher();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PatternMapIterator::~PatternMapIterator() {
|
2007-08-04 02:44:11 +00:00
|
|
|
delete matcher;
|
2007-08-03 18:43:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2007-08-31 05:20:56 +00:00
|
|
|
PatternMapIterator::set(PatternMap& newPatternMap) {
|
|
|
|
this->patternMap=&newPatternMap;
|
2007-08-03 18:43:04 +00:00
|
|
|
}
|
|
|
|
|
2009-04-22 23:10:07 +00:00
|
|
|
PtnSkeleton*
|
2007-08-03 18:43:04 +00:00
|
|
|
PatternMapIterator::getSkeleton() {
|
|
|
|
if ( nodePtr == NULL ) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return nodePtr->skeleton;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
UBool
|
|
|
|
PatternMapIterator::hasNext() {
|
|
|
|
int32_t headIndex=bootIndex;
|
|
|
|
PtnElem *curPtr=nodePtr;
|
|
|
|
|
|
|
|
if (patternMap==NULL) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
while ( headIndex < MAX_PATTERN_ENTRIES ) {
|
|
|
|
if ( curPtr != NULL ) {
|
|
|
|
if ( curPtr->next != NULL ) {
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
headIndex++;
|
|
|
|
curPtr=NULL;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if ( patternMap->boot[headIndex] != NULL ) {
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
headIndex++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
DateTimeMatcher&
|
|
|
|
PatternMapIterator::next() {
|
|
|
|
while ( bootIndex < MAX_PATTERN_ENTRIES ) {
|
|
|
|
if ( nodePtr != NULL ) {
|
|
|
|
if ( nodePtr->next != NULL ) {
|
|
|
|
nodePtr = nodePtr->next;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
bootIndex++;
|
|
|
|
nodePtr=NULL;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if ( patternMap->boot[bootIndex] != NULL ) {
|
|
|
|
nodePtr = patternMap->boot[bootIndex];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
bootIndex++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (nodePtr!=NULL) {
|
|
|
|
matcher->copyFrom(*nodePtr->skeleton);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
matcher->copyFrom();
|
|
|
|
}
|
|
|
|
return *matcher;
|
|
|
|
}
|
|
|
|
|
2007-08-04 02:53:37 +00:00
|
|
|
PtnSkeleton::PtnSkeleton() {
|
2007-08-03 18:43:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-08-04 03:14:19 +00:00
|
|
|
PtnSkeleton::PtnSkeleton(const PtnSkeleton& other) {
|
2007-08-03 18:43:04 +00:00
|
|
|
for (int32_t i=0; i<UDATPG_FIELD_COUNT; ++i) {
|
|
|
|
this->type[i]=other.type[i];
|
|
|
|
this->original[i]=other.original[i];
|
|
|
|
this->baseOriginal[i]=other.baseOriginal[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
UBool
|
2007-08-04 03:14:19 +00:00
|
|
|
PtnSkeleton::equals(const PtnSkeleton& other) {
|
2007-08-03 18:43:04 +00:00
|
|
|
for (int32_t i=0; i<UDATPG_FIELD_COUNT; ++i) {
|
|
|
|
if ( (type[i]!= other.type[i]) ||
|
|
|
|
(original[i]!=other.original[i]) ||
|
|
|
|
(baseOriginal[i]!=other.baseOriginal[i]) ) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
UnicodeString
|
|
|
|
PtnSkeleton::getSkeleton() {
|
|
|
|
UnicodeString result;
|
|
|
|
|
|
|
|
for(int32_t i=0; i< UDATPG_FIELD_COUNT; ++i) {
|
|
|
|
if (original[i].length()!=0) {
|
|
|
|
result += original[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2007-08-07 03:14:19 +00:00
|
|
|
UnicodeString
|
|
|
|
PtnSkeleton::getBaseSkeleton() {
|
|
|
|
UnicodeString result;
|
|
|
|
|
|
|
|
for(int32_t i=0; i< UDATPG_FIELD_COUNT; ++i) {
|
|
|
|
if (baseOriginal[i].length()!=0) {
|
|
|
|
result += baseOriginal[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2007-08-03 18:43:04 +00:00
|
|
|
PtnSkeleton::~PtnSkeleton() {
|
|
|
|
}
|
|
|
|
|
2009-04-22 23:10:07 +00:00
|
|
|
PtnElem::PtnElem(const UnicodeString &basePat, const UnicodeString &pat) :
|
2007-08-04 02:44:11 +00:00
|
|
|
basePattern(basePat),
|
2007-08-23 05:48:44 +00:00
|
|
|
skeleton(NULL),
|
2007-08-29 06:13:07 +00:00
|
|
|
pattern(pat),
|
2007-08-23 05:48:44 +00:00
|
|
|
next(NULL)
|
2007-08-04 02:44:11 +00:00
|
|
|
{
|
2007-08-03 18:43:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
PtnElem::~PtnElem() {
|
|
|
|
|
|
|
|
if (next!=NULL) {
|
|
|
|
delete next;
|
|
|
|
}
|
|
|
|
delete skeleton;
|
|
|
|
}
|
|
|
|
|
|
|
|
DTSkeletonEnumeration::DTSkeletonEnumeration(PatternMap &patternMap, dtStrEnum type, UErrorCode& status) {
|
|
|
|
PtnElem *curElem;
|
|
|
|
PtnSkeleton *curSkeleton;
|
|
|
|
UnicodeString s;
|
|
|
|
int32_t bootIndex;
|
|
|
|
|
|
|
|
pos=0;
|
|
|
|
fSkeletons = new UVector(status);
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
delete fSkeletons;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (bootIndex=0; bootIndex<MAX_PATTERN_ENTRIES; ++bootIndex ) {
|
|
|
|
curElem = patternMap.boot[bootIndex];
|
|
|
|
while (curElem!=NULL) {
|
|
|
|
switch(type) {
|
|
|
|
case DT_BASESKELETON:
|
2007-08-04 02:44:11 +00:00
|
|
|
s=curElem->basePattern;
|
2007-08-03 18:43:04 +00:00
|
|
|
break;
|
|
|
|
case DT_PATTERN:
|
2007-08-04 02:44:11 +00:00
|
|
|
s=curElem->pattern;
|
2007-08-03 18:43:04 +00:00
|
|
|
break;
|
|
|
|
case DT_SKELETON:
|
|
|
|
curSkeleton=curElem->skeleton;
|
|
|
|
s=curSkeleton->getSkeleton();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if ( !isCanonicalItem(s) ) {
|
|
|
|
fSkeletons->addElement(new UnicodeString(s), status);
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
delete fSkeletons;
|
|
|
|
fSkeletons = NULL;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
curElem = curElem->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ((bootIndex==MAX_PATTERN_ENTRIES) && (curElem!=NULL) ) {
|
|
|
|
status = U_BUFFER_OVERFLOW_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const UnicodeString*
|
|
|
|
DTSkeletonEnumeration::snext(UErrorCode& status) {
|
|
|
|
if (U_SUCCESS(status) && pos < fSkeletons->size()) {
|
|
|
|
return (const UnicodeString*)fSkeletons->elementAt(pos++);
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2007-08-29 06:49:58 +00:00
|
|
|
DTSkeletonEnumeration::reset(UErrorCode& /*status*/) {
|
2007-08-03 18:43:04 +00:00
|
|
|
pos=0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t
|
2007-08-29 06:49:58 +00:00
|
|
|
DTSkeletonEnumeration::count(UErrorCode& /*status*/) const {
|
|
|
|
return (fSkeletons==NULL) ? 0 : fSkeletons->size();
|
|
|
|
}
|
|
|
|
|
2007-08-03 18:43:04 +00:00
|
|
|
UBool
|
|
|
|
DTSkeletonEnumeration::isCanonicalItem(const UnicodeString& item) {
|
|
|
|
if ( item.length() != 1 ) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
for (int32_t i=0; i<UDATPG_FIELD_COUNT; ++i) {
|
|
|
|
if (item.charAt(0)==Canonical_Items[i]) {
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
DTSkeletonEnumeration::~DTSkeletonEnumeration() {
|
|
|
|
UnicodeString *s;
|
|
|
|
for (int32_t i=0; i<fSkeletons->size(); ++i) {
|
|
|
|
if ((s=(UnicodeString *)fSkeletons->elementAt(i))!=NULL) {
|
|
|
|
delete s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
delete fSkeletons;
|
|
|
|
}
|
|
|
|
|
|
|
|
DTRedundantEnumeration::DTRedundantEnumeration() {
|
|
|
|
pos=0;
|
|
|
|
fPatterns = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DTRedundantEnumeration::add(const UnicodeString& pattern, UErrorCode& status) {
|
2007-08-29 05:44:57 +00:00
|
|
|
if (U_FAILURE(status)) return;
|
|
|
|
if (fPatterns == NULL) {
|
2007-08-03 18:43:04 +00:00
|
|
|
fPatterns = new UVector(status);
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
delete fPatterns;
|
|
|
|
fPatterns = NULL;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fPatterns->addElement(new UnicodeString(pattern), status);
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
delete fPatterns;
|
|
|
|
fPatterns = NULL;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const UnicodeString*
|
|
|
|
DTRedundantEnumeration::snext(UErrorCode& status) {
|
|
|
|
if (U_SUCCESS(status) && pos < fPatterns->size()) {
|
|
|
|
return (const UnicodeString*)fPatterns->elementAt(pos++);
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2007-08-29 06:49:58 +00:00
|
|
|
DTRedundantEnumeration::reset(UErrorCode& /*status*/) {
|
2007-08-03 18:43:04 +00:00
|
|
|
pos=0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t
|
2007-08-29 06:49:58 +00:00
|
|
|
DTRedundantEnumeration::count(UErrorCode& /*status*/) const {
|
2007-08-03 18:43:04 +00:00
|
|
|
return (fPatterns==NULL) ? 0 : fPatterns->size();
|
|
|
|
}
|
|
|
|
|
|
|
|
UBool
|
|
|
|
DTRedundantEnumeration::isCanonicalItem(const UnicodeString& item) {
|
|
|
|
if ( item.length() != 1 ) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
for (int32_t i=0; i<UDATPG_FIELD_COUNT; ++i) {
|
|
|
|
if (item.charAt(0)==Canonical_Items[i]) {
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
DTRedundantEnumeration::~DTRedundantEnumeration() {
|
|
|
|
UnicodeString *s;
|
|
|
|
for (int32_t i=0; i<fPatterns->size(); ++i) {
|
|
|
|
if ((s=(UnicodeString *)fPatterns->elementAt(i))!=NULL) {
|
|
|
|
delete s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
delete fPatterns;
|
|
|
|
}
|
|
|
|
|
|
|
|
U_NAMESPACE_END
|
|
|
|
|
|
|
|
|
|
|
|
#endif /* #if !UCONFIG_NO_FORMATTING */
|
|
|
|
|
|
|
|
//eof
|