]> git.saurik.com Git - apple/icu.git/blob - icuSources/i18n/erarules.cpp
ICU-62141.0.1.tar.gz
[apple/icu.git] / icuSources / i18n / erarules.cpp
1 // © 2018 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3
4 #include <utility>
5
6 #include "unicode/utypes.h"
7
8 #if !UCONFIG_NO_FORMATTING
9
10 #include <stdlib.h>
11 #include "unicode/ucal.h"
12 #include "unicode/ures.h"
13 #include "unicode/ustring.h"
14 #include "cmemory.h"
15 #include "cstring.h"
16 #include "erarules.h"
17 #include "gregoimp.h"
18 #include "uassert.h"
19
20 U_NAMESPACE_BEGIN
21
22 static const int32_t MAX_ENCODED_START_YEAR = 32767;
23 static const int32_t MIN_ENCODED_START_YEAR = -32768;
24 static const int32_t MIN_ENCODED_START = -2147483391; // encodeDate(MIN_ENCODED_START_YEAR, 1, 1, ...);
25
26 static const int32_t YEAR_MASK = 0xFFFF0000;
27 static const int32_t MONTH_MASK = 0x0000FF00;
28 static const int32_t DAY_MASK = 0x000000FF;
29
30 static const int32_t MAX_INT32 = 0x7FFFFFFF;
31 static const int32_t MIN_INT32 = 0xFFFFFFFF;
32
33 static const UChar VAL_FALSE[] = {0x66, 0x61, 0x6c, 0x73, 0x65}; // "false"
34 static const UChar VAL_FALSE_LEN = 5;
35
36 static UBool isSet(int startDate) {
37 return startDate != 0;
38 }
39
40 static UBool isValidRuleStartDate(int32_t year, int32_t month, int32_t day) {
41 return year >= MIN_ENCODED_START_YEAR && year <= MAX_ENCODED_START_YEAR
42 && month >= 1 && month <= 12 && day >=1 && day <= 31;
43 }
44
45 /**
46 * Encode year/month/date to a single integer.
47 * year is high 16 bits (-32768 to 32767), month is
48 * next 8 bits and day of month is last 8 bits.
49 *
50 * @param year year
51 * @param month month (1-base)
52 * @param day day of month
53 * @return an encoded date.
54 */
55 static int32_t encodeDate(int32_t year, int32_t month, int32_t day) {
56 return year << 16 | month << 8 | day;
57 }
58
59 static void decodeDate(int32_t encodedDate, int32_t (&fields)[3]) {
60 if (encodedDate == MIN_ENCODED_START) {
61 fields[0] = MIN_INT32;
62 fields[1] = 1;
63 fields[2] = 1;
64 } else {
65 fields[0] = (encodedDate & YEAR_MASK) >> 16;
66 fields[1] = (encodedDate & MONTH_MASK) >> 8;
67 fields[2] = encodedDate & DAY_MASK;
68 }
69 }
70
71 /**
72 * Compare an encoded date with another date specified by year/month/day.
73 * @param encoded An encoded date
74 * @param year Year of another date
75 * @param month Month of another date
76 * @param day Day of another date
77 * @return -1 when encoded date is earlier, 0 when two dates are same,
78 * and 1 when encoded date is later.
79 */
80 static int32_t compareEncodedDateWithYMD(int encoded, int year, int month, int day) {
81 if (year < MIN_ENCODED_START_YEAR) {
82 if (encoded == MIN_ENCODED_START) {
83 if (year > MIN_INT32 || month > 1 || day > 1) {
84 return -1;
85 }
86 return 0;
87 } else {
88 return 1;
89 }
90 } else if (year > MAX_ENCODED_START_YEAR) {
91 return -1;
92 } else {
93 int tmp = encodeDate(year, month, day);
94 if (encoded < tmp) {
95 return -1;
96 } else if (encoded == tmp) {
97 return 0;
98 } else {
99 return 1;
100 }
101 }
102 }
103
104 EraRules::EraRules(LocalMemory<int32_t>& eraStartDates, int32_t numEras)
105 : numEras(numEras) {
106 startDates = std::move(eraStartDates);
107 initCurrentEra();
108 }
109
110 EraRules::~EraRules() {
111 }
112
113 EraRules* EraRules::createInstance(const char *calType, UBool includeTentativeEra, UErrorCode& status) {
114 if(U_FAILURE(status)) {
115 return nullptr;
116 }
117 LocalUResourceBundlePointer rb(ures_openDirect(nullptr, "supplementalData", &status));
118 ures_getByKey(rb.getAlias(), "calendarData", rb.getAlias(), &status);
119 ures_getByKey(rb.getAlias(), calType, rb.getAlias(), &status);
120 ures_getByKey(rb.getAlias(), "eras", rb.getAlias(), &status);
121
122 if (U_FAILURE(status)) {
123 return nullptr;
124 }
125
126 int32_t numEras = ures_getSize(rb.getAlias());
127 int32_t firstTentativeIdx = MAX_INT32;
128
129 LocalMemory<int32_t> startDates(static_cast<int32_t *>(uprv_malloc(numEras * sizeof(int32_t))));
130 if (startDates.isNull()) {
131 status = U_MEMORY_ALLOCATION_ERROR;
132 return nullptr;
133 }
134 uprv_memset(startDates.getAlias(), 0 , numEras * sizeof(int32_t));
135
136 while (ures_hasNext(rb.getAlias())) {
137 LocalUResourceBundlePointer eraRuleRes(ures_getNextResource(rb.getAlias(), nullptr, &status));
138 if (U_FAILURE(status)) {
139 return nullptr;
140 }
141 const char *eraIdxStr = ures_getKey(eraRuleRes.getAlias());
142 char *endp;
143 int32_t eraIdx = (int32_t)strtol(eraIdxStr, &endp, 10);
144 if ((size_t)(endp - eraIdxStr) != uprv_strlen(eraIdxStr)) {
145 status = U_INVALID_FORMAT_ERROR;
146 return nullptr;
147 }
148 if (eraIdx < 0 || eraIdx >= numEras) {
149 status = U_INVALID_FORMAT_ERROR;
150 return nullptr;
151 }
152 if (isSet(startDates[eraIdx])) {
153 // start date of the index was already set
154 status = U_INVALID_FORMAT_ERROR;
155 return nullptr;
156 }
157
158 UBool hasName = TRUE;
159 UBool hasEnd = TRUE;
160 int32_t len;
161 while (ures_hasNext(eraRuleRes.getAlias())) {
162 LocalUResourceBundlePointer res(ures_getNextResource(eraRuleRes.getAlias(), nullptr, &status));
163 if (U_FAILURE(status)) {
164 return nullptr;
165 }
166 const char *key = ures_getKey(res.getAlias());
167 if (uprv_strcmp(key, "start") == 0) {
168 const int32_t *fields = ures_getIntVector(res.getAlias(), &len, &status);
169 if (U_FAILURE(status)) {
170 return nullptr;
171 }
172 if (len != 3 || !isValidRuleStartDate(fields[0], fields[1], fields[2])) {
173 status = U_INVALID_FORMAT_ERROR;
174 return nullptr;
175 }
176 startDates[eraIdx] = encodeDate(fields[0], fields[1], fields[2]);
177 } else if (uprv_strcmp(key, "named") == 0) {
178 const UChar *val = ures_getString(res.getAlias(), &len, &status);
179 if (u_strncmp(val, VAL_FALSE, VAL_FALSE_LEN) == 0) {
180 hasName = FALSE;
181 }
182 } else if (uprv_strcmp(key, "end") == 0) {
183 hasEnd = TRUE;
184 }
185 }
186
187 if (isSet(startDates[eraIdx])) {
188 if (hasEnd) {
189 // This implementation assumes either start or end is available, not both.
190 // For now, just ignore the end rule.
191 }
192 } else {
193 if (hasEnd) {
194 if (eraIdx != 0) {
195 // This implementation does not support end only rule for eras other than
196 // the first one.
197 status = U_INVALID_FORMAT_ERROR;
198 return nullptr;
199 }
200 U_ASSERT(eraIdx == 0);
201 startDates[eraIdx] = MIN_ENCODED_START;
202 } else {
203 status = U_INVALID_FORMAT_ERROR;
204 return nullptr;
205 }
206 }
207
208 if (hasName) {
209 if (eraIdx >= firstTentativeIdx) {
210 status = U_INVALID_FORMAT_ERROR;
211 return nullptr;
212 }
213 } else {
214 if (eraIdx < firstTentativeIdx) {
215 firstTentativeIdx = eraIdx;
216 }
217 }
218 }
219
220 EraRules *result;
221 if (firstTentativeIdx < MAX_INT32 && !includeTentativeEra) {
222 result = new EraRules(startDates, firstTentativeIdx);
223 } else {
224 result = new EraRules(startDates, numEras);
225 }
226
227 if (result == nullptr) {
228 status = U_MEMORY_ALLOCATION_ERROR;
229 }
230 return result;
231 }
232
233 void EraRules::getStartDate(int32_t eraIdx, int32_t (&fields)[3], UErrorCode& status) const {
234 if(U_FAILURE(status)) {
235 return;
236 }
237 if (eraIdx < 0 || eraIdx >= numEras) {
238 status = U_ILLEGAL_ARGUMENT_ERROR;
239 return;
240 }
241 decodeDate(startDates[eraIdx], fields);
242 }
243
244 int32_t EraRules::getStartYear(int32_t eraIdx, UErrorCode& status) const {
245 int year = MAX_INT32/2; // bogus value (/2 so don't overflow elsewhere <rdar://problem/49714633>)
246 if(U_FAILURE(status)) {
247 return year;
248 }
249 if (eraIdx < 0 || eraIdx >= numEras) {
250 status = U_ILLEGAL_ARGUMENT_ERROR;
251 return year;
252 }
253 int fields[3];
254 decodeDate(startDates[eraIdx], fields);
255 year = fields[0];
256
257 return year;
258 }
259
260 int32_t EraRules::getEraIndex(int32_t year, int32_t month, int32_t day, UErrorCode& status) const {
261 if(U_FAILURE(status)) {
262 return -1;
263 }
264
265 if (month < 1 || month > 12 || day < 1 || day > 31) {
266 status = U_ILLEGAL_ARGUMENT_ERROR;
267 return -1;
268 }
269 int32_t high = numEras; // last index + 1
270 int32_t low;
271
272 // Short circuit for recent years. Most modern computations will
273 // occur in the last few eras.
274 if (compareEncodedDateWithYMD(startDates[getCurrentEraIndex()], year, month, day) <= 0) {
275 low = getCurrentEraIndex();
276 } else {
277 low = 0;
278 }
279
280 // Do binary search
281 while (low < high - 1) {
282 int i = (low + high) / 2;
283 if (compareEncodedDateWithYMD(startDates[i], year, month, day) <= 0) {
284 low = i;
285 } else {
286 high = i;
287 }
288 }
289 return low;
290 }
291
292 void EraRules::initCurrentEra() {
293 UDate now = ucal_getNow();
294 int year, month0, dom, dow, doy, mid;
295 Grego::timeToFields(now, year, month0, dom, dow, doy, mid);
296 int currentEncodedDate = encodeDate(year, month0 + 1 /* changes to 1-base */, dom);
297 int eraIdx = numEras - 1;
298 while (eraIdx > 0) {
299 if (currentEncodedDate >= startDates[eraIdx]) {
300 break;
301 }
302 eraIdx--;
303 }
304 // Note: current era could be before the first era.
305 // In this case, this implementation returns the first era index (0).
306 currentEra = eraIdx;}
307
308 U_NAMESPACE_END
309 #endif /* #if !UCONFIG_NO_FORMATTING */
310
311