clang -cc1 -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name datefmt.cpp -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -analyzer-config-compatibility-mode=true -mrelocation-model pic -pic-level 2 -fhalf-no-semantic-interposition -mframe-pointer=all -relaxed-aliasing -ffp-contract=off -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fdebug-compilation-dir=/root/firefox-clang/obj-x86_64-pc-linux-gnu/config/external/icu/i18n -fcoverage-compilation-dir=/root/firefox-clang/obj-x86_64-pc-linux-gnu/config/external/icu/i18n -resource-dir /usr/lib/llvm-21/lib/clang/21 -include /root/firefox-clang/config/gcc_hidden.h -include /root/firefox-clang/obj-x86_64-pc-linux-gnu/mozilla-config.h -I /root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/system_wrappers -U _FORTIFY_SOURCE -D _FORTIFY_SOURCE=2 -D _GLIBCXX_ASSERTIONS -D DEBUG=1 -D U_I18N_IMPLEMENTATION -D _LIBCPP_DISABLE_DEPRECATION_WARNINGS -D U_USING_ICU_NAMESPACE=0 -D U_NO_DEFAULT_INCLUDE_UTF_HEADERS=1 -D U_HIDE_OBSOLETE_UTF_OLD_H=1 -D UCONFIG_NO_LEGACY_CONVERSION -D UCONFIG_NO_TRANSLITERATION -D UCONFIG_NO_REGULAR_EXPRESSIONS -D UCONFIG_NO_BREAK_ITERATION -D UCONFIG_NO_IDNA -D UCONFIG_NO_MF2 -D U_CHARSET_IS_UTF8 -D UNISTR_FROM_CHAR_EXPLICIT=explicit -D UNISTR_FROM_STRING_EXPLICIT=explicit -D U_ENABLE_DYLOAD=0 -D U_DEBUG=1 -I /root/firefox-clang/config/external/icu/i18n -I /root/firefox-clang/obj-x86_64-pc-linux-gnu/config/external/icu/i18n -I /root/firefox-clang/intl/icu/source/common -I /root/firefox-clang/mfbt/double-conversion -I /root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include -I /root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/nspr -I /root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/nss -D MOZILLA_CLIENT -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/14/../../../../include/c++/14 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/14/../../../../include/x86_64-linux-gnu/c++/14 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/14/../../../../include/c++/14/backward -internal-isystem /usr/lib/llvm-21/lib/clang/21/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/14/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O2 -Wno-error=pessimizing-move -Wno-error=large-by-value-copy=128 -Wno-error=implicit-int-float-conversion -Wno-error=thread-safety-analysis -Wno-error=tautological-type-limit-compare -Wno-invalid-offsetof -Wno-range-loop-analysis -Wno-deprecated-anon-enum-enum-conversion -Wno-deprecated-enum-enum-conversion -Wno-deprecated-this-capture -Wno-inline-new-delete -Wno-error=deprecated-declarations -Wno-error=array-bounds -Wno-error=free-nonheap-object -Wno-error=atomic-alignment -Wno-error=deprecated-builtins -Wno-psabi -Wno-error=builtin-macro-redefined -Wno-vla-cxx-extension -Wno-unknown-warning-option -Wno-comma -Wno-implicit-const-int-float-conversion -Wno-macro-redefined -Wno-microsoft-include -Wno-tautological-unsigned-enum-zero-compare -Wno-unreachable-code-loop-increment -Wno-unreachable-code-return -fdeprecated-macro -ferror-limit 19 -fstrict-flex-arrays=1 -stack-protector 2 -fstack-clash-protection -ftrivial-auto-var-init=pattern -fgnuc-version=4.2.1 -fskip-odr-check-in-gmf -fno-sized-deallocation -fno-aligned-allocation -vectorize-loops -vectorize-slp -analyzer-checker optin.performance.Padding -analyzer-output=html -analyzer-config stable-report-filename=true -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /tmp/scan-build-2025-06-27-100320-3286336-1 -x c++ /root/firefox-clang/intl/icu/source/i18n/datefmt.cpp
1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | |
14 | |
15 | |
16 | |
17 | |
18 | |
19 | |
20 | |
21 | |
22 | #include "unicode/utypes.h" |
23 | |
24 | #if !UCONFIG_NO_FORMATTING |
25 | |
26 | #include "unicode/ures.h" |
27 | #include "unicode/datefmt.h" |
28 | #include "unicode/smpdtfmt.h" |
29 | #include "unicode/dtptngen.h" |
30 | #include "unicode/udisplaycontext.h" |
31 | #include "unicode/gregocal.h" |
32 | #include "reldtfmt.h" |
33 | #include "sharedobject.h" |
34 | #include "unifiedcache.h" |
35 | #include "uarrsort.h" |
36 | |
37 | #include "cstring.h" |
38 | #include "windtfmt.h" |
39 | |
40 | #if defined( U_DEBUG_CALSVC ) || defined (U_DEBUG_CAL) |
41 | #include <stdio.h> |
42 | #endif |
43 | #include <typeinfo> |
44 | |
45 | |
46 | |
47 | |
48 | |
49 | U_NAMESPACE_BEGIN |
50 | |
51 | class DateFmtBestPattern : public SharedObject { |
52 | public: |
53 | UnicodeString fPattern; |
54 | |
55 | DateFmtBestPattern(const UnicodeString &pattern) |
56 | : fPattern(pattern) { } |
57 | ~DateFmtBestPattern(); |
58 | }; |
59 | |
60 | DateFmtBestPattern::~DateFmtBestPattern() { |
61 | } |
62 | |
63 | template<> |
64 | const DateFmtBestPattern *LocaleCacheKey<DateFmtBestPattern>::createObject( |
65 | const void * , UErrorCode &status) const { |
66 | status = U_UNSUPPORTED_ERROR; |
67 | return nullptr; |
68 | } |
69 | |
70 | class DateFmtBestPatternKey : public LocaleCacheKey<DateFmtBestPattern> { |
71 | private: |
72 | UnicodeString fSkeleton; |
73 | protected: |
74 | virtual bool equals(const CacheKeyBase &other) const override { |
75 | if (!LocaleCacheKey<DateFmtBestPattern>::equals(other)) { |
76 | return false; |
77 | } |
78 | |
79 | return operator==(static_cast<const DateFmtBestPatternKey &>(other)); |
80 | } |
81 | public: |
82 | DateFmtBestPatternKey( |
83 | const Locale &loc, |
84 | const UnicodeString &skeleton, |
85 | UErrorCode &status) |
86 | : LocaleCacheKey<DateFmtBestPattern>(loc), |
87 | fSkeleton(DateTimePatternGenerator::staticGetSkeleton(skeleton, status)) { } |
88 | DateFmtBestPatternKey(const DateFmtBestPatternKey &other) : |
89 | LocaleCacheKey<DateFmtBestPattern>(other), |
90 | fSkeleton(other.fSkeleton) { } |
91 | virtual ~DateFmtBestPatternKey(); |
92 | virtual int32_t hashCode() const override { |
93 | return static_cast<int32_t>(37u * static_cast<uint32_t>(LocaleCacheKey<DateFmtBestPattern>::hashCode()) + static_cast<uint32_t>(fSkeleton.hashCode())); |
94 | } |
95 | inline bool operator==(const DateFmtBestPatternKey &other) const { |
96 | return fSkeleton == other.fSkeleton; |
97 | } |
98 | virtual CacheKeyBase *clone() const override { |
99 | return new DateFmtBestPatternKey(*this); |
100 | } |
101 | virtual const DateFmtBestPattern *createObject( |
102 | const void * , UErrorCode &status) const override { |
103 | LocalPointer<DateTimePatternGenerator> dtpg( |
104 | DateTimePatternGenerator::createInstance(fLoc, status)); |
105 | if (U_FAILURE(status)) { |
106 | return nullptr; |
107 | } |
108 | |
109 | LocalPointer<DateFmtBestPattern> pattern( |
110 | new DateFmtBestPattern( |
111 | dtpg->getBestPattern(fSkeleton, status)), |
112 | status); |
113 | if (U_FAILURE(status)) { |
114 | return nullptr; |
115 | } |
116 | DateFmtBestPattern *result = pattern.orphan(); |
117 | result->addRef(); |
118 | return result; |
119 | } |
120 | }; |
121 | |
122 | DateFmtBestPatternKey::~DateFmtBestPatternKey() { } |
123 | |
124 | |
125 | DateFormat::DateFormat() |
126 | : fCalendar(nullptr), |
127 | fNumberFormat(nullptr), |
128 | fCapitalizationContext(UDISPCTX_CAPITALIZATION_NONE) |
129 | { |
130 | } |
131 | |
132 | |
133 | |
134 | DateFormat::DateFormat(const DateFormat& other) |
135 | : Format(other), |
136 | fCalendar(nullptr), |
137 | fNumberFormat(nullptr), |
138 | fCapitalizationContext(UDISPCTX_CAPITALIZATION_NONE) |
139 | { |
140 | *this = other; |
141 | } |
142 | |
143 | |
144 | |
145 | DateFormat& DateFormat::operator=(const DateFormat& other) |
146 | { |
147 | if (this != &other) |
148 | { |
149 | delete fCalendar; |
150 | delete fNumberFormat; |
151 | if(other.fCalendar) { |
152 | fCalendar = other.fCalendar->clone(); |
153 | } else { |
154 | fCalendar = nullptr; |
155 | } |
156 | if(other.fNumberFormat) { |
157 | fNumberFormat = other.fNumberFormat->clone(); |
158 | } else { |
159 | fNumberFormat = nullptr; |
160 | } |
161 | fBoolFlags = other.fBoolFlags; |
162 | fCapitalizationContext = other.fCapitalizationContext; |
163 | } |
164 | return *this; |
165 | } |
166 | |
167 | |
168 | |
169 | DateFormat::~DateFormat() |
170 | { |
171 | delete fCalendar; |
172 | delete fNumberFormat; |
173 | } |
174 | |
175 | |
176 | |
177 | bool |
178 | DateFormat::operator==(const Format& other) const |
179 | { |
180 | if (this == &other) { |
181 | return true; |
182 | } |
183 | if (!(Format::operator==(other))) { |
184 | return false; |
185 | } |
186 | |
187 | DateFormat* fmt = (DateFormat*)&other; |
188 | return fCalendar&&(fCalendar->isEquivalentTo(*fmt->fCalendar)) && |
189 | (fNumberFormat && *fNumberFormat == *fmt->fNumberFormat) && |
190 | (fCapitalizationContext == fmt->fCapitalizationContext); |
191 | } |
192 | |
193 | |
194 | |
195 | UnicodeString& |
196 | DateFormat::format(const Formattable& obj, |
197 | UnicodeString& appendTo, |
198 | FieldPosition& fieldPosition, |
199 | UErrorCode& status) const |
200 | { |
201 | if (U_FAILURE(status)) return appendTo; |
202 | |
203 | |
204 | UDate date = 0; |
205 | switch (obj.getType()) |
206 | { |
207 | case Formattable::kDate: |
208 | date = obj.getDate(); |
209 | break; |
210 | case Formattable::kDouble: |
211 | date = static_cast<UDate>(obj.getDouble()); |
212 | break; |
213 | case Formattable::kLong: |
214 | date = static_cast<UDate>(obj.getLong()); |
215 | break; |
216 | default: |
217 | status = U_ILLEGAL_ARGUMENT_ERROR; |
218 | return appendTo; |
219 | } |
220 | |
221 | |
222 | |
223 | |
224 | |
225 | return format(date, appendTo, fieldPosition); |
226 | } |
227 | |
228 | |
229 | |
230 | UnicodeString& |
231 | DateFormat::format(const Formattable& obj, |
232 | UnicodeString& appendTo, |
233 | FieldPositionIterator* posIter, |
234 | UErrorCode& status) const |
235 | { |
236 | if (U_FAILURE(status)) return appendTo; |
237 | |
238 | |
239 | UDate date = 0; |
240 | switch (obj.getType()) |
241 | { |
242 | case Formattable::kDate: |
243 | date = obj.getDate(); |
244 | break; |
245 | case Formattable::kDouble: |
246 | date = static_cast<UDate>(obj.getDouble()); |
247 | break; |
248 | case Formattable::kLong: |
249 | date = static_cast<UDate>(obj.getLong()); |
250 | break; |
251 | default: |
252 | status = U_ILLEGAL_ARGUMENT_ERROR; |
253 | return appendTo; |
254 | } |
255 | |
256 | |
257 | |
258 | |
259 | |
260 | return format(date, appendTo, posIter, status); |
261 | } |
262 | |
263 | |
264 | |
265 | |
266 | UnicodeString& |
267 | DateFormat::format(Calendar& , |
268 | UnicodeString& appendTo, |
269 | FieldPositionIterator* , |
270 | UErrorCode& status) const { |
271 | if (U_SUCCESS(status)) { |
272 | status = U_UNSUPPORTED_ERROR; |
273 | } |
274 | return appendTo; |
275 | } |
276 | |
277 | |
278 | |
279 | UnicodeString& |
280 | DateFormat::format(UDate date, UnicodeString& appendTo, FieldPosition& fieldPosition) const { |
281 | if (fCalendar != nullptr) { |
282 | UErrorCode ec = U_ZERO_ERROR; |
283 | |
284 | if (typeid(*fCalendar) == typeid(GregorianCalendar)) { |
285 | GregorianCalendar cal(*static_cast<GregorianCalendar*>(fCalendar)); |
286 | cal.setTime(date, ec); |
287 | if (U_SUCCESS(ec)) { |
288 | format(cal, appendTo, fieldPosition); |
289 | } |
290 | } else { |
291 | |
292 | Calendar *calClone = fCalendar->clone(); |
293 | if (calClone != nullptr) { |
294 | calClone->setTime(date, ec); |
295 | if (U_SUCCESS(ec)) { |
296 | format(*calClone, appendTo, fieldPosition); |
297 | } |
298 | delete calClone; |
299 | } |
300 | } |
301 | } |
302 | return appendTo; |
303 | } |
304 | |
305 | |
306 | |
307 | UnicodeString& |
308 | DateFormat::format(UDate date, UnicodeString& appendTo, FieldPositionIterator* posIter, |
309 | UErrorCode& status) const { |
310 | if (fCalendar != nullptr) { |
311 | UErrorCode ec = U_ZERO_ERROR; |
312 | |
313 | if (typeid(*fCalendar) == typeid(GregorianCalendar)) { |
314 | GregorianCalendar cal(*static_cast<GregorianCalendar*>(fCalendar)); |
315 | cal.setTime(date, ec); |
316 | if (U_SUCCESS(ec)) { |
317 | format(cal, appendTo, posIter, status); |
318 | } |
319 | } else { |
320 | Calendar* calClone = fCalendar->clone(); |
321 | if (calClone != nullptr) { |
322 | calClone->setTime(date, status); |
323 | if (U_SUCCESS(status)) { |
324 | format(*calClone, appendTo, posIter, status); |
325 | } |
326 | delete calClone; |
327 | } |
328 | } |
329 | } |
330 | return appendTo; |
331 | } |
332 | |
333 | |
334 | |
335 | UnicodeString& |
336 | DateFormat::format(UDate date, UnicodeString& appendTo) const |
337 | { |
338 | |
339 | |
340 | FieldPosition fpos(FieldPosition::DONT_CARE); |
341 | return format(date, appendTo, fpos); |
342 | } |
343 | |
344 | |
345 | |
346 | UDate |
347 | DateFormat::parse(const UnicodeString& text, |
348 | ParsePosition& pos) const |
349 | { |
350 | UDate d = 0; |
351 | if (fCalendar != nullptr) { |
352 | Calendar* calClone = fCalendar->clone(); |
353 | if (calClone != nullptr) { |
354 | int32_t start = pos.getIndex(); |
355 | calClone->clear(); |
356 | parse(text, *calClone, pos); |
357 | if (pos.getIndex() != start) { |
358 | UErrorCode ec = U_ZERO_ERROR; |
359 | d = calClone->getTime(ec); |
360 | if (U_FAILURE(ec)) { |
361 | |
362 | |
363 | |
364 | pos.setIndex(start); |
365 | pos.setErrorIndex(start); |
366 | d = 0; |
367 | } |
368 | } |
369 | delete calClone; |
370 | } |
371 | } |
372 | return d; |
373 | } |
374 | |
375 | |
376 | |
377 | UDate |
378 | DateFormat::parse(const UnicodeString& text, |
379 | UErrorCode& status) const |
380 | { |
381 | if (U_FAILURE(status)) return 0; |
382 | |
383 | ParsePosition pos(0); |
384 | UDate result = parse(text, pos); |
385 | if (pos.getIndex() == 0) { |
386 | #if defined (U_DEBUG_CAL) |
387 | fprintf(stderr, "%s:%d - - failed to parse - err index %d\n" |
388 | , __FILE__, __LINE__, pos.getErrorIndex() ); |
389 | #endif |
390 | status = U_ILLEGAL_ARGUMENT_ERROR; |
391 | } |
392 | return result; |
393 | } |
394 | |
395 | |
396 | |
397 | void |
398 | DateFormat::parseObject(const UnicodeString& source, |
399 | Formattable& result, |
400 | ParsePosition& pos) const |
401 | { |
402 | result.setDate(parse(source, pos)); |
403 | } |
404 | |
405 | |
406 | |
407 | DateFormat* U_EXPORT2 |
408 | DateFormat::createTimeInstance(DateFormat::EStyle style, |
409 | const Locale& aLocale) |
410 | { |
411 | return createDateTimeInstance(kNone, style, aLocale); |
412 | } |
413 | |
414 | |
415 | |
416 | DateFormat* U_EXPORT2 |
417 | DateFormat::createDateInstance(DateFormat::EStyle style, |
418 | const Locale& aLocale) |
419 | { |
420 | return createDateTimeInstance(style, kNone, aLocale); |
421 | } |
422 | |
423 | |
424 | |
425 | DateFormat* U_EXPORT2 |
426 | DateFormat::createDateTimeInstance(EStyle dateStyle, |
427 | EStyle timeStyle, |
428 | const Locale& aLocale) |
429 | { |
430 | if(dateStyle != kNone) |
431 | { |
432 | dateStyle = static_cast<EStyle>(dateStyle + kDateOffset); |
433 | } |
434 | return create(timeStyle, dateStyle, aLocale); |
435 | } |
436 | |
437 | |
438 | |
439 | DateFormat* U_EXPORT2 |
440 | DateFormat::createInstance() |
441 | { |
442 | return createDateTimeInstance(kShort, kShort, Locale::getDefault()); |
443 | } |
444 | |
445 | |
446 | |
447 | UnicodeString U_EXPORT2 |
448 | DateFormat::getBestPattern( |
449 | const Locale &locale, |
450 | const UnicodeString &skeleton, |
451 | UErrorCode &status) { |
452 | UnifiedCache *cache = UnifiedCache::getInstance(status); |
453 | if (U_FAILURE(status)) { |
| |
454 | return {}; |
455 | } |
456 | DateFmtBestPatternKey key(locale, skeleton, status); |
457 | const DateFmtBestPattern *patternPtr = nullptr; |
| 5 | | 'patternPtr' initialized to a null pointer value | |
|
458 | cache->get(key, patternPtr, status); |
| 6 | | Calling 'UnifiedCache::get' | |
|
| 19 | | Returning from 'UnifiedCache::get' | |
|
459 | if (U_FAILURE(status)) { |
| |
460 | return {}; |
461 | } |
462 | UnicodeString result(patternPtr->fPattern); |
| 21 | | Forming reference to null pointer |
|
463 | patternPtr->removeRef(); |
464 | return result; |
465 | } |
466 | |
467 | DateFormat* U_EXPORT2 |
468 | DateFormat::createInstanceForSkeleton( |
469 | Calendar *calendarToAdopt, |
470 | const UnicodeString& skeleton, |
471 | const Locale &locale, |
472 | UErrorCode &status) { |
473 | LocalPointer<Calendar> calendar(calendarToAdopt); |
474 | if (U_FAILURE(status)) { |
475 | return nullptr; |
476 | } |
477 | if (calendar.isNull()) { |
478 | status = U_ILLEGAL_ARGUMENT_ERROR; |
479 | return nullptr; |
480 | } |
481 | Locale localeWithCalendar = locale; |
482 | localeWithCalendar.setKeywordValue("calendar", calendar->getType(), status); |
483 | if (U_FAILURE(status)) { |
484 | return nullptr; |
485 | } |
486 | DateFormat *result = createInstanceForSkeleton(skeleton, localeWithCalendar, status); |
487 | if (U_FAILURE(status)) { |
488 | return nullptr; |
489 | } |
490 | result->adoptCalendar(calendar.orphan()); |
491 | return result; |
492 | } |
493 | |
494 | DateFormat* U_EXPORT2 |
495 | DateFormat::createInstanceForSkeleton( |
496 | const UnicodeString& skeleton, |
497 | const Locale &locale, |
498 | UErrorCode &status) { |
499 | if (U_FAILURE(status)) { |
| |
500 | return nullptr; |
501 | } |
502 | LocalPointer<DateFormat> df( |
503 | new SimpleDateFormat( |
504 | getBestPattern(locale, skeleton, status), |
| 3 | | Calling 'DateFormat::getBestPattern' | |
|
505 | locale, status), |
506 | status); |
507 | return U_SUCCESS(status) ? df.orphan() : nullptr; |
508 | } |
509 | |
510 | DateFormat* U_EXPORT2 |
511 | DateFormat::createInstanceForSkeleton( |
512 | const UnicodeString& skeleton, |
513 | UErrorCode &status) { |
514 | return createInstanceForSkeleton( |
| 1 | Calling 'DateFormat::createInstanceForSkeleton' | |
|
515 | skeleton, Locale::getDefault(), status); |
516 | } |
517 | |
518 | |
519 | |
520 | DateFormat* U_EXPORT2 |
521 | DateFormat::create(EStyle timeStyle, EStyle dateStyle, const Locale& locale) |
522 | { |
523 | UErrorCode status = U_ZERO_ERROR; |
524 | #if U_PLATFORM_USES_ONLY_WIN32_API |
525 | char buffer[8]; |
526 | int32_t count = locale.getKeywordValue("compat", buffer, sizeof(buffer), status); |
527 | |
528 | |
529 | if (count > 0 && uprv_strcmp(buffer, "host") == 0) { |
530 | Win32DateFormat *f = new Win32DateFormat(timeStyle, dateStyle, locale, status); |
531 | |
532 | if (U_SUCCESS(status)) { |
533 | return f; |
534 | } |
535 | |
536 | delete f; |
537 | } |
538 | #endif |
539 | |
540 | |
541 | if(((dateStyle!=kNone)&&((dateStyle-kDateOffset) & UDAT_RELATIVE))) { |
542 | RelativeDateFormat* r = new RelativeDateFormat(static_cast<UDateFormatStyle>(timeStyle), static_cast<UDateFormatStyle>(dateStyle - kDateOffset), locale, status); |
543 | if(U_SUCCESS(status)) return r; |
544 | delete r; |
545 | status = U_ZERO_ERROR; |
546 | } |
547 | |
548 | |
549 | SimpleDateFormat *f = new SimpleDateFormat(timeStyle, dateStyle, locale, status); |
550 | if (U_SUCCESS(status)) return f; |
551 | delete f; |
552 | |
553 | |
554 | |
555 | status = U_ZERO_ERROR; |
556 | f = new SimpleDateFormat(locale, status); |
557 | if (U_SUCCESS(status)) return f; |
558 | delete f; |
559 | |
560 | |
561 | |
562 | |
563 | return nullptr; |
564 | } |
565 | |
566 | |
567 | |
568 | const Locale* U_EXPORT2 |
569 | DateFormat::getAvailableLocales(int32_t& count) |
570 | { |
571 | |
572 | |
573 | |
574 | return Locale::getAvailableLocales(count); |
575 | } |
576 | |
577 | |
578 | |
579 | void |
580 | DateFormat::adoptCalendar(Calendar* newCalendar) |
581 | { |
582 | delete fCalendar; |
583 | fCalendar = newCalendar; |
584 | } |
585 | |
586 | |
587 | void |
588 | DateFormat::setCalendar(const Calendar& newCalendar) |
589 | { |
590 | Calendar* newCalClone = newCalendar.clone(); |
591 | if (newCalClone != nullptr) { |
592 | adoptCalendar(newCalClone); |
593 | } |
594 | } |
595 | |
596 | |
597 | |
598 | const Calendar* |
599 | DateFormat::getCalendar() const |
600 | { |
601 | return fCalendar; |
602 | } |
603 | |
604 | |
605 | |
606 | void |
607 | DateFormat::adoptNumberFormat(NumberFormat* newNumberFormat) |
608 | { |
609 | delete fNumberFormat; |
610 | fNumberFormat = newNumberFormat; |
611 | newNumberFormat->setParseIntegerOnly(true); |
612 | newNumberFormat->setGroupingUsed(false); |
613 | } |
614 | |
615 | |
616 | void |
617 | DateFormat::setNumberFormat(const NumberFormat& newNumberFormat) |
618 | { |
619 | NumberFormat* newNumFmtClone = newNumberFormat.clone(); |
620 | if (newNumFmtClone != nullptr) { |
621 | adoptNumberFormat(newNumFmtClone); |
622 | } |
623 | } |
624 | |
625 | |
626 | |
627 | const NumberFormat* |
628 | DateFormat::getNumberFormat() const |
629 | { |
630 | return fNumberFormat; |
631 | } |
632 | |
633 | |
634 | |
635 | void |
636 | DateFormat::adoptTimeZone(TimeZone* zone) |
637 | { |
638 | if (fCalendar != nullptr) { |
639 | fCalendar->adoptTimeZone(zone); |
640 | } |
641 | } |
642 | |
643 | |
644 | void |
645 | DateFormat::setTimeZone(const TimeZone& zone) |
646 | { |
647 | if (fCalendar != nullptr) { |
648 | fCalendar->setTimeZone(zone); |
649 | } |
650 | } |
651 | |
652 | |
653 | |
654 | const TimeZone& |
655 | DateFormat::getTimeZone() const |
656 | { |
657 | if (fCalendar != nullptr) { |
658 | return fCalendar->getTimeZone(); |
659 | } |
660 | |
661 | |
662 | return *(TimeZone::createDefault()); |
663 | } |
664 | |
665 | |
666 | |
667 | void |
668 | DateFormat::setLenient(UBool lenient) |
669 | { |
670 | if (fCalendar != nullptr) { |
671 | fCalendar->setLenient(lenient); |
672 | } |
673 | UErrorCode status = U_ZERO_ERROR; |
674 | setBooleanAttribute(UDAT_PARSE_ALLOW_WHITESPACE, lenient, status); |
675 | setBooleanAttribute(UDAT_PARSE_ALLOW_NUMERIC, lenient, status); |
676 | } |
677 | |
678 | |
679 | |
680 | UBool |
681 | DateFormat::isLenient() const |
682 | { |
683 | UBool lenient = true; |
684 | if (fCalendar != nullptr) { |
685 | lenient = fCalendar->isLenient(); |
686 | } |
687 | UErrorCode status = U_ZERO_ERROR; |
688 | return lenient |
689 | && getBooleanAttribute(UDAT_PARSE_ALLOW_WHITESPACE, status) |
690 | && getBooleanAttribute(UDAT_PARSE_ALLOW_NUMERIC, status); |
691 | } |
692 | |
693 | void |
694 | DateFormat::setCalendarLenient(UBool lenient) |
695 | { |
696 | if (fCalendar != nullptr) { |
697 | fCalendar->setLenient(lenient); |
698 | } |
699 | } |
700 | |
701 | |
702 | |
703 | UBool |
704 | DateFormat::isCalendarLenient() const |
705 | { |
706 | if (fCalendar != nullptr) { |
707 | return fCalendar->isLenient(); |
708 | } |
709 | |
710 | return false; |
711 | } |
712 | |
713 | |
714 | |
715 | |
716 | |
717 | void DateFormat::setContext(UDisplayContext value, UErrorCode& status) |
718 | { |
719 | if (U_FAILURE(status)) |
720 | return; |
721 | if (static_cast<UDisplayContextType>(static_cast<uint32_t>(value) >> 8) == UDISPCTX_TYPE_CAPITALIZATION) { |
722 | fCapitalizationContext = value; |
723 | } else { |
724 | status = U_ILLEGAL_ARGUMENT_ERROR; |
725 | } |
726 | } |
727 | |
728 | |
729 | |
730 | |
731 | |
732 | UDisplayContext DateFormat::getContext(UDisplayContextType type, UErrorCode& status) const |
733 | { |
734 | if (U_FAILURE(status)) |
735 | return static_cast<UDisplayContext>(0); |
736 | if (type != UDISPCTX_TYPE_CAPITALIZATION) { |
737 | status = U_ILLEGAL_ARGUMENT_ERROR; |
738 | return static_cast<UDisplayContext>(0); |
739 | } |
740 | return fCapitalizationContext; |
741 | } |
742 | |
743 | |
744 | |
745 | |
746 | |
747 | DateFormat& |
748 | DateFormat::setBooleanAttribute(UDateFormatBooleanAttribute attr, |
749 | UBool newValue, |
750 | UErrorCode &status) { |
751 | if(!fBoolFlags.isValidValue(newValue)) { |
752 | status = U_ILLEGAL_ARGUMENT_ERROR; |
753 | } else { |
754 | fBoolFlags.set(attr, newValue); |
755 | } |
756 | |
757 | return *this; |
758 | } |
759 | |
760 | |
761 | |
762 | UBool |
763 | DateFormat::getBooleanAttribute(UDateFormatBooleanAttribute attr, UErrorCode &) const { |
764 | |
765 | return fBoolFlags.get(attr); |
766 | } |
767 | |
768 | U_NAMESPACE_END |
769 | |
770 | #endif /* #if !UCONFIG_NO_FORMATTING */ |
771 | |
772 | |
1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | #ifndef __UNIFIED_CACHE_H__ |
14 | #define __UNIFIED_CACHE_H__ |
15 | |
16 | #include "utypeinfo.h" // for 'typeid' to work |
17 | |
18 | #include "unicode/uobject.h" |
19 | #include "unicode/locid.h" |
20 | #include "sharedobject.h" |
21 | #include "unicode/unistr.h" |
22 | #include "cstring.h" |
23 | #include "ustr_imp.h" |
24 | |
25 | struct UHashtable; |
26 | struct UHashElement; |
27 | |
28 | U_NAMESPACE_BEGIN |
29 | |
30 | class UnifiedCache; |
31 | |
32 | |
33 | |
34 | |
35 | class U_COMMON_API CacheKeyBase : public UObject { |
36 | public: |
37 | CacheKeyBase() : fCreationStatus(U_ZERO_ERROR), fIsPrimary(false) {} |
38 | |
39 | |
40 | |
41 | |
42 | CacheKeyBase(const CacheKeyBase &other) |
43 | : UObject(other), fCreationStatus(other.fCreationStatus), fIsPrimary(false) { } |
44 | virtual ~CacheKeyBase(); |
45 | |
46 | |
47 | |
48 | |
49 | virtual int32_t hashCode() const = 0; |
50 | |
51 | |
52 | |
53 | |
54 | virtual CacheKeyBase *clone() const = 0; |
55 | |
56 | |
57 | |
58 | |
59 | |
60 | |
61 | |
62 | |
63 | |
64 | |
65 | |
66 | |
67 | |
68 | |
69 | virtual const SharedObject *createObject( |
70 | const void *creationContext, UErrorCode &status) const = 0; |
71 | |
72 | |
73 | |
74 | |
75 | |
76 | virtual char *writeDescription(char *buffer, int32_t bufSize) const = 0; |
77 | |
78 | friend inline bool operator==(const CacheKeyBase& lhs, |
79 | const CacheKeyBase& rhs) { |
80 | return lhs.equals(rhs); |
81 | } |
82 | |
83 | friend inline bool operator!=(const CacheKeyBase& lhs, |
84 | const CacheKeyBase& rhs) { |
85 | return !lhs.equals(rhs); |
86 | } |
87 | |
88 | protected: |
89 | virtual bool equals(const CacheKeyBase& other) const = 0; |
90 | |
91 | private: |
92 | mutable UErrorCode fCreationStatus; |
93 | mutable UBool fIsPrimary; |
94 | friend class UnifiedCache; |
95 | }; |
96 | |
97 | |
98 | |
99 | |
100 | |
101 | |
102 | |
103 | template<typename T> |
104 | class CacheKey : public CacheKeyBase { |
105 | public: |
106 | virtual ~CacheKey() { } |
107 | |
108 | |
109 | |
110 | virtual int32_t hashCode() const override { |
111 | const char *s = typeid(T).name(); |
112 | return ustr_hashCharsN(s, static_cast<int32_t>(uprv_strlen(s))); |
113 | } |
114 | |
115 | |
116 | |
117 | |
118 | virtual char *writeDescription(char *buffer, int32_t bufLen) const override { |
119 | const char *s = typeid(T).name(); |
120 | uprv_strncpy(buffer, s, bufLen); |
121 | buffer[bufLen - 1] = 0; |
122 | return buffer; |
123 | } |
124 | |
125 | protected: |
126 | |
127 | |
128 | |
129 | virtual bool equals(const CacheKeyBase &other) const override { |
130 | return this == &other || typeid(*this) == typeid(other); |
131 | } |
132 | }; |
133 | |
134 | |
135 | |
136 | |
137 | |
138 | template<typename T> |
139 | class LocaleCacheKey : public CacheKey<T> { |
140 | protected: |
141 | Locale fLoc; |
142 | virtual bool equals(const CacheKeyBase &other) const override { |
143 | if (!CacheKey<T>::equals(other)) { |
144 | return false; |
145 | } |
146 | |
147 | |
148 | return operator==(static_cast<const LocaleCacheKey<T> &>(other)); |
149 | } |
150 | public: |
151 | LocaleCacheKey(const Locale &loc) : fLoc(loc) {} |
152 | LocaleCacheKey(const LocaleCacheKey<T> &other) |
153 | : CacheKey<T>(other), fLoc(other.fLoc) { } |
154 | virtual ~LocaleCacheKey() { } |
155 | virtual int32_t hashCode() const override { |
156 | return (int32_t)(37u * (uint32_t)CacheKey<T>::hashCode() + (uint32_t)fLoc.hashCode()); |
157 | } |
158 | inline bool operator == (const LocaleCacheKey<T> &other) const { |
159 | return fLoc == other.fLoc; |
160 | } |
161 | virtual CacheKeyBase *clone() const override { |
162 | return new LocaleCacheKey<T>(*this); |
163 | } |
164 | virtual const T *createObject( |
165 | const void *creationContext, UErrorCode &status) const override; |
166 | |
167 | |
168 | |
169 | virtual char *writeDescription(char *buffer, int32_t bufLen) const override { |
170 | const char *s = fLoc.getName(); |
171 | uprv_strncpy(buffer, s, bufLen); |
172 | buffer[bufLen - 1] = 0; |
173 | return buffer; |
174 | } |
175 | |
176 | }; |
177 | |
178 | |
179 | |
180 | |
181 | |
182 | |
183 | class U_COMMON_API UnifiedCache : public UnifiedCacheBase { |
184 | public: |
185 | |
186 | |
187 | |
188 | |
189 | |
190 | UnifiedCache(UErrorCode &status); |
191 | |
192 | |
193 | |
194 | |
195 | static UnifiedCache *getInstance(UErrorCode &status); |
196 | |
197 | |
198 | |
199 | |
200 | |
201 | template<typename T> |
202 | void get( |
203 | const CacheKey<T>& key, |
204 | const T *&ptr, |
205 | UErrorCode &status) const { |
206 | get(key, nullptr, ptr, status); |
| 7 | | Calling 'UnifiedCache::get' | |
|
| 17 | | Returning from 'UnifiedCache::get' | |
|
207 | } |
| 18 | | Returning without writing to 'ptr' | |
|
208 | |
209 | |
210 | |
211 | |
212 | |
213 | |
214 | |
215 | |
216 | |
217 | |
218 | |
219 | |
220 | |
221 | |
222 | |
223 | template<typename T> |
224 | void get( |
225 | const CacheKey<T>& key, |
226 | const void *creationContext, |
227 | const T *&ptr, |
228 | UErrorCode &status) const { |
229 | if (U_FAILURE(status)) { |
| |
230 | return; |
231 | } |
232 | UErrorCode creationStatus = U_ZERO_ERROR; |
233 | const SharedObject *value = nullptr; |
234 | _get(key, value, creationContext, creationStatus); |
235 | const T *tvalue = (const T *) value; |
236 | if (U_SUCCESS(creationStatus)) { |
| |
237 | SharedObject::copyPtr(tvalue, ptr); |
| 10 | | Calling 'SharedObject::copyPtr' | |
|
| 14 | | Returning from 'SharedObject::copyPtr' | |
|
238 | } |
239 | SharedObject::clearPtr(tvalue); |
240 | |
241 | |
242 | if (status == U_ZERO_ERROR || U_FAILURE(creationStatus)) { |
| 15 | | Assuming 'status' is equal to U_ZERO_ERROR | |
|
243 | status = creationStatus; |
244 | } |
245 | } |
| 16 | | Returning without writing to 'ptr' | |
|
246 | |
247 | #ifdef UNIFIED_CACHE_DEBUG |
248 | |
249 | |
250 | |
251 | |
252 | void dumpContents() const; |
253 | #endif |
254 | |
255 | |
256 | |
257 | |
258 | |
259 | |
260 | |
261 | |
262 | |
263 | |
264 | |
265 | |
266 | |
267 | template<typename T> |
268 | static void getByLocale( |
269 | const Locale &loc, const T *&ptr, UErrorCode &status) { |
270 | const UnifiedCache *cache = getInstance(status); |
271 | if (U_FAILURE(status)) { |
272 | return; |
273 | } |
274 | cache->get(LocaleCacheKey<T>(loc), ptr, status); |
275 | } |
276 | |
277 | #ifdef UNIFIED_CACHE_DEBUG |
278 | |
279 | |
280 | |
281 | static void dump(); |
282 | #endif |
283 | |
284 | |
285 | |
286 | |
287 | int32_t keyCount() const; |
288 | |
289 | |
290 | |
291 | |
292 | |
293 | void flush() const; |
294 | |
295 | |
296 | |
297 | |
298 | |
299 | |
300 | |
301 | |
302 | |
303 | |
304 | |
305 | |
306 | |
307 | |
308 | |
309 | |
310 | |
311 | |
312 | |
313 | |
314 | |
315 | |
316 | |
317 | |
318 | |
319 | |
320 | |
321 | |
322 | |
323 | |
324 | |
325 | |
326 | |
327 | void setEvictionPolicy( |
328 | int32_t count, int32_t percentageOfInUseItems, UErrorCode &status); |
329 | |
330 | |
331 | |
332 | |
333 | |
334 | |
335 | |
336 | int64_t autoEvictedCount() const; |
337 | |
338 | |
339 | |
340 | |
341 | |
342 | int32_t unusedCount() const; |
343 | |
344 | virtual void handleUnreferencedObject() const override; |
345 | virtual ~UnifiedCache(); |
346 | |
347 | private: |
348 | UHashtable *fHashtable; |
349 | mutable int32_t fEvictPos; |
350 | mutable int32_t fNumValuesTotal; |
351 | mutable int32_t fNumValuesInUse; |
352 | int32_t fMaxUnused; |
353 | int32_t fMaxPercentageOfInUse; |
354 | mutable int64_t fAutoEvictedCount; |
355 | SharedObject *fNoValue; |
356 | |
357 | UnifiedCache(const UnifiedCache &other) = delete; |
358 | UnifiedCache &operator=(const UnifiedCache &other) = delete; |
359 | |
360 | |
361 | |
362 | |
363 | |
364 | |
365 | |
366 | |
367 | |
368 | |
369 | |
370 | |
371 | |
372 | |
373 | |
374 | |
375 | UBool _flush(UBool all) const; |
376 | |
377 | |
378 | |
379 | |
380 | |
381 | |
382 | |
383 | |
384 | |
385 | |
386 | |
387 | |
388 | void _get( |
389 | const CacheKeyBase &key, |
390 | const SharedObject *&value, |
391 | const void *creationContext, |
392 | UErrorCode &status) const; |
393 | |
394 | |
395 | |
396 | |
397 | |
398 | |
399 | |
400 | |
401 | |
402 | |
403 | |
404 | |
405 | UBool _poll( |
406 | const CacheKeyBase &key, |
407 | const SharedObject *&value, |
408 | UErrorCode &status) const; |
409 | |
410 | |
411 | |
412 | |
413 | |
414 | |
415 | |
416 | void _putNew( |
417 | const CacheKeyBase &key, |
418 | const SharedObject *value, |
419 | const UErrorCode creationStatus, |
420 | UErrorCode &status) const; |
421 | |
422 | |
423 | |
424 | |
425 | |
426 | |
427 | |
428 | |
429 | |
430 | |
431 | |
432 | |
433 | |
434 | |
435 | void _putIfAbsentAndGet( |
436 | const CacheKeyBase &key, |
437 | const SharedObject *&value, |
438 | UErrorCode &status) const; |
439 | |
440 | |
441 | |
442 | |
443 | |
444 | |
445 | const UHashElement *_nextElement() const; |
446 | |
447 | |
448 | |
449 | |
450 | |
451 | |
452 | |
453 | |
454 | |
455 | int32_t _computeCountOfItemsToEvict() const; |
456 | |
457 | |
458 | |
459 | |
460 | |
461 | |
462 | |
463 | void _runEvictionSlice() const; |
464 | |
465 | |
466 | |
467 | |
468 | |
469 | |
470 | |
471 | |
472 | |
473 | |
474 | |
475 | |
476 | void _registerPrimary(const CacheKeyBase *theKey, const SharedObject *value) const; |
477 | |
478 | |
479 | |
480 | |
481 | |
482 | |
483 | |
484 | |
485 | |
486 | void _put( |
487 | const UHashElement *element, |
488 | const SharedObject *value, |
489 | const UErrorCode status) const; |
490 | |
491 | |
492 | |
493 | |
494 | |
495 | |
496 | void removeSoftRef(const SharedObject *value) const; |
497 | |
498 | |
499 | |
500 | |
501 | |
502 | |
503 | |
504 | |
505 | |
506 | int32_t addHardRef(const SharedObject *value) const; |
507 | |
508 | |
509 | |
510 | |
511 | |
512 | |
513 | |
514 | |
515 | |
516 | int32_t removeHardRef(const SharedObject *value) const; |
517 | |
518 | |
519 | #ifdef UNIFIED_CACHE_DEBUG |
520 | void _dumpContents() const; |
521 | #endif |
522 | |
523 | |
524 | |
525 | |
526 | |
527 | |
528 | |
529 | |
530 | |
531 | |
532 | void _fetch(const UHashElement *element, const SharedObject *&value, |
533 | UErrorCode &status) const; |
534 | |
535 | |
536 | |
537 | |
538 | |
539 | UBool _inProgress(const UHashElement *element) const; |
540 | |
541 | |
542 | |
543 | |
544 | |
545 | UBool _inProgress(const SharedObject *theValue, UErrorCode creationStatus) const; |
546 | |
547 | |
548 | |
549 | |
550 | |
551 | UBool _isEvictable(const UHashElement *element) const; |
552 | }; |
553 | |
554 | U_NAMESPACE_END |
555 | |
556 | #endif |