]> git.saurik.com Git - apple/icu.git/blame - icuSources/i18n/smpdtfst.cpp
ICU-64260.0.1.tar.gz
[apple/icu.git] / icuSources / i18n / smpdtfst.cpp
CommitLineData
f3c0d7a5
A
1// © 2016 and later: Unicode, Inc. and others.
2// License & terms of use: http://www.unicode.org/copyright.html
4388f060
A
3/*
4*******************************************************************************
57a6839d 5* Copyright (C) 2009-2013, International Business Machines Corporation and *
4388f060
A
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*/
46f4442e
A
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"
57a6839d 23#include "uassert.h"
46f4442e
A
24#include "ucln_in.h"
25#include "umutex.h"
26
27
28#include "smpdtfst.h"
29
30U_NAMESPACE_BEGIN
31
57a6839d
A
32SimpleDateFormatStaticSets *gStaticSets = NULL;
33UInitOnce gSimpleDateFormatStaticSetsInitOnce = U_INITONCE_INITIALIZER;
46f4442e 34
57a6839d 35SimpleDateFormatStaticSets::SimpleDateFormatStaticSets(UErrorCode &status)
46f4442e
A
36: fDateIgnorables(NULL),
37 fTimeIgnorables(NULL),
38 fOtherIgnorables(NULL)
39{
57a6839d
A
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);
4388f060 43
46f4442e
A
44 // Check for null pointers
45 if (fDateIgnorables == NULL || fTimeIgnorables == NULL || fOtherIgnorables == NULL) {
46 goto ExitConstrDeleteAll;
47 }
4388f060 48
46f4442e
A
49 // Freeze all the sets
50 fDateIgnorables->freeze();
51 fTimeIgnorables->freeze();
52 fOtherIgnorables->freeze();
4388f060 53
46f4442e 54 return; // If we reached this point, everything is fine so just exit
4388f060 55
46f4442e
A
56ExitConstrDeleteAll: // Remove all sets and return error
57 delete fDateIgnorables; fDateIgnorables = NULL;
58 delete fTimeIgnorables; fTimeIgnorables = NULL;
59 delete fOtherIgnorables; fOtherIgnorables = NULL;
4388f060 60
57a6839d 61 status = U_MEMORY_ALLOCATION_ERROR;
46f4442e
A
62}
63
64
65SimpleDateFormatStaticSets::~SimpleDateFormatStaticSets() {
66 delete fDateIgnorables; fDateIgnorables = NULL;
67 delete fTimeIgnorables; fTimeIgnorables = NULL;
68 delete fOtherIgnorables; fOtherIgnorables = NULL;
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//------------------------------------------------------------------------------
78UBool
79SimpleDateFormatStaticSets::cleanup(void)
80{
57a6839d
A
81 delete gStaticSets;
82 gStaticSets = NULL;
83 gSimpleDateFormatStaticSetsInitOnce.reset();
46f4442e
A
84 return TRUE;
85}
86
87U_CDECL_BEGIN
88static UBool U_CALLCONV
89smpdtfmt_cleanup(void)
90{
91 return SimpleDateFormatStaticSets::cleanup();
92}
46f4442e 93
57a6839d
A
94static void U_CALLCONV smpdtfmt_initSets(UErrorCode &status) {
95 ucln_i18n_registerCleanup(UCLN_I18N_SMPDTFMT, smpdtfmt_cleanup);
96 U_ASSERT(gStaticSets == NULL);
97 gStaticSets = new SimpleDateFormatStaticSets(status);
98 if (gStaticSets == NULL) {
99 status = U_MEMORY_ALLOCATION_ERROR;
100 return;
46f4442e
A
101 }
102}
103
57a6839d
A
104U_CDECL_END
105
46f4442e
A
106UnicodeSet *SimpleDateFormatStaticSets::getIgnorables(UDateFormatField fieldIndex)
107{
57a6839d
A
108 UErrorCode status = U_ZERO_ERROR;
109 umtx_initOnce(gSimpleDateFormatStaticSetsInitOnce, &smpdtfmt_initSets, status);
110 if (U_FAILURE(status)) {
111 return NULL;
112 }
46f4442e
A
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
46f4442e
A
135U_NAMESPACE_END
136
137#endif // #if !UCONFIG_NO_FORMATTING