tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

smpdtfst.cpp (4223B)


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