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

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

mercurial