intl/icu/source/i18n/smpdtfst.cpp

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /*
michael@0 2 *******************************************************************************
michael@0 3 * Copyright (C) 2009-2013, International Business Machines Corporation and *
michael@0 4 * others. All Rights Reserved. *
michael@0 5 *******************************************************************************
michael@0 6 *
michael@0 7 * This file contains the class SimpleDateFormatStaticSets
michael@0 8 *
michael@0 9 * SimpleDateFormatStaticSets holds the UnicodeSets that are needed for lenient
michael@0 10 * parsing of literal characters in date/time strings.
michael@0 11 ********************************************************************************
michael@0 12 */
michael@0 13
michael@0 14 #include "unicode/utypes.h"
michael@0 15
michael@0 16 #if !UCONFIG_NO_FORMATTING
michael@0 17
michael@0 18 #include "unicode/uniset.h"
michael@0 19 #include "unicode/udat.h"
michael@0 20 #include "cmemory.h"
michael@0 21 #include "uassert.h"
michael@0 22 #include "ucln_in.h"
michael@0 23 #include "umutex.h"
michael@0 24
michael@0 25
michael@0 26 #include "smpdtfst.h"
michael@0 27
michael@0 28 U_NAMESPACE_BEGIN
michael@0 29
michael@0 30 SimpleDateFormatStaticSets *gStaticSets = NULL;
michael@0 31 UInitOnce gSimpleDateFormatStaticSetsInitOnce = U_INITONCE_INITIALIZER;
michael@0 32
michael@0 33 SimpleDateFormatStaticSets::SimpleDateFormatStaticSets(UErrorCode &status)
michael@0 34 : fDateIgnorables(NULL),
michael@0 35 fTimeIgnorables(NULL),
michael@0 36 fOtherIgnorables(NULL)
michael@0 37 {
michael@0 38 fDateIgnorables = new UnicodeSet(UNICODE_STRING("[-,./[:whitespace:]]", 20), status);
michael@0 39 fTimeIgnorables = new UnicodeSet(UNICODE_STRING("[-.:[:whitespace:]]", 19), status);
michael@0 40 fOtherIgnorables = new UnicodeSet(UNICODE_STRING("[:whitespace:]", 14), status);
michael@0 41
michael@0 42 // Check for null pointers
michael@0 43 if (fDateIgnorables == NULL || fTimeIgnorables == NULL || fOtherIgnorables == NULL) {
michael@0 44 goto ExitConstrDeleteAll;
michael@0 45 }
michael@0 46
michael@0 47 // Freeze all the sets
michael@0 48 fDateIgnorables->freeze();
michael@0 49 fTimeIgnorables->freeze();
michael@0 50 fOtherIgnorables->freeze();
michael@0 51
michael@0 52 return; // If we reached this point, everything is fine so just exit
michael@0 53
michael@0 54 ExitConstrDeleteAll: // Remove all sets and return error
michael@0 55 delete fDateIgnorables; fDateIgnorables = NULL;
michael@0 56 delete fTimeIgnorables; fTimeIgnorables = NULL;
michael@0 57 delete fOtherIgnorables; fOtherIgnorables = NULL;
michael@0 58
michael@0 59 status = U_MEMORY_ALLOCATION_ERROR;
michael@0 60 }
michael@0 61
michael@0 62
michael@0 63 SimpleDateFormatStaticSets::~SimpleDateFormatStaticSets() {
michael@0 64 delete fDateIgnorables; fDateIgnorables = NULL;
michael@0 65 delete fTimeIgnorables; fTimeIgnorables = NULL;
michael@0 66 delete fOtherIgnorables; fOtherIgnorables = NULL;
michael@0 67 }
michael@0 68
michael@0 69
michael@0 70 //------------------------------------------------------------------------------
michael@0 71 //
michael@0 72 // smpdtfmt_cleanup Memory cleanup function, free/delete all
michael@0 73 // cached memory. Called by ICU's u_cleanup() function.
michael@0 74 //
michael@0 75 //------------------------------------------------------------------------------
michael@0 76 UBool
michael@0 77 SimpleDateFormatStaticSets::cleanup(void)
michael@0 78 {
michael@0 79 delete gStaticSets;
michael@0 80 gStaticSets = NULL;
michael@0 81 gSimpleDateFormatStaticSetsInitOnce.reset();
michael@0 82 return TRUE;
michael@0 83 }
michael@0 84
michael@0 85 U_CDECL_BEGIN
michael@0 86 static UBool U_CALLCONV
michael@0 87 smpdtfmt_cleanup(void)
michael@0 88 {
michael@0 89 return SimpleDateFormatStaticSets::cleanup();
michael@0 90 }
michael@0 91
michael@0 92 static void U_CALLCONV smpdtfmt_initSets(UErrorCode &status) {
michael@0 93 ucln_i18n_registerCleanup(UCLN_I18N_SMPDTFMT, smpdtfmt_cleanup);
michael@0 94 U_ASSERT(gStaticSets == NULL);
michael@0 95 gStaticSets = new SimpleDateFormatStaticSets(status);
michael@0 96 if (gStaticSets == NULL) {
michael@0 97 status = U_MEMORY_ALLOCATION_ERROR;
michael@0 98 return;
michael@0 99 }
michael@0 100 }
michael@0 101
michael@0 102 U_CDECL_END
michael@0 103
michael@0 104 UnicodeSet *SimpleDateFormatStaticSets::getIgnorables(UDateFormatField fieldIndex)
michael@0 105 {
michael@0 106 UErrorCode status = U_ZERO_ERROR;
michael@0 107 umtx_initOnce(gSimpleDateFormatStaticSetsInitOnce, &smpdtfmt_initSets, status);
michael@0 108 if (U_FAILURE(status)) {
michael@0 109 return NULL;
michael@0 110 }
michael@0 111
michael@0 112 switch (fieldIndex) {
michael@0 113 case UDAT_YEAR_FIELD:
michael@0 114 case UDAT_MONTH_FIELD:
michael@0 115 case UDAT_DATE_FIELD:
michael@0 116 case UDAT_STANDALONE_DAY_FIELD:
michael@0 117 case UDAT_STANDALONE_MONTH_FIELD:
michael@0 118 return gStaticSets->fDateIgnorables;
michael@0 119
michael@0 120 case UDAT_HOUR_OF_DAY1_FIELD:
michael@0 121 case UDAT_HOUR_OF_DAY0_FIELD:
michael@0 122 case UDAT_MINUTE_FIELD:
michael@0 123 case UDAT_SECOND_FIELD:
michael@0 124 case UDAT_HOUR1_FIELD:
michael@0 125 case UDAT_HOUR0_FIELD:
michael@0 126 return gStaticSets->fTimeIgnorables;
michael@0 127
michael@0 128 default:
michael@0 129 return gStaticSets->fOtherIgnorables;
michael@0 130 }
michael@0 131 }
michael@0 132
michael@0 133 U_NAMESPACE_END
michael@0 134
michael@0 135 #endif // #if !UCONFIG_NO_FORMATTING

mercurial