clang -cc1 -cc1 -triple x86_64-unknown-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 -mrelocation-model pic -pic-level 2 -pic-is-pie -mframe-pointer=all -fmath-errno -ffp-contract=on -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fcoverage-compilation-dir=/home/maurizio/node-v18.6.0/out -resource-dir /usr/local/lib/clang/16.0.0 -D V8_DEPRECATION_WARNINGS -D V8_IMMINENT_DEPRECATION_WARNINGS -D _GLIBCXX_USE_CXX11_ABI=1 -D NODE_OPENSSL_CONF_NAME=nodejs_conf -D NODE_OPENSSL_HAS_QUIC -D __STDC_FORMAT_MACROS -D OPENSSL_NO_PINSHARED -D OPENSSL_THREADS -D U_COMMON_IMPLEMENTATION=1 -D U_I18N_IMPLEMENTATION=1 -D U_IO_IMPLEMENTATION=1 -D U_TOOLUTIL_IMPLEMENTATION=1 -D U_ATTRIBUTE_DEPRECATED= -D _CRT_SECURE_NO_DEPRECATE= -D U_STATIC_IMPLEMENTATION=1 -D UCONFIG_NO_SERVICE=1 -D U_ENABLE_DYLOAD=0 -D U_HAVE_STD_STRING=1 -D UCONFIG_NO_BREAK_ITERATION=0 -I ../deps/icu-small/source/common -I ../deps/icu-small/source/i18n -I ../deps/icu-small/source/tools/toolutil -internal-isystem /usr/lib/gcc/x86_64-redhat-linux/8/../../../../include/c++/8 -internal-isystem /usr/lib/gcc/x86_64-redhat-linux/8/../../../../include/c++/8/x86_64-redhat-linux -internal-isystem /usr/lib/gcc/x86_64-redhat-linux/8/../../../../include/c++/8/backward -internal-isystem /usr/local/lib/clang/16.0.0/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-redhat-linux/8/../../../../x86_64-redhat-linux/include -internal-externc-isystem /include -internal-externc-isystem /usr/include -O3 -Wno-unused-parameter -Wno-deprecated-declarations -Wno-strict-aliasing -std=gnu++17 -fdeprecated-macro -fdebug-compilation-dir=/home/maurizio/node-v18.6.0/out -ferror-limit 19 -fgnuc-version=4.2.1 -vectorize-loops -vectorize-slp -analyzer-output=html -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /tmp/scan-build-2022-08-22-142216-507842-1 -x c++ ../deps/icu-small/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 "reldtfmt.h" |
| 32 | #include "sharedobject.h" |
| 33 | #include "unifiedcache.h" |
| 34 | #include "uarrsort.h" |
| 35 | |
| 36 | #include "cstring.h" |
| 37 | #include "windtfmt.h" |
| 38 | |
| 39 | #if defined( U_DEBUG_CALSVC ) || defined (U_DEBUG_CAL) |
| 40 | #include <stdio.h> |
| 41 | #endif |
| 42 | |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | U_NAMESPACE_BEGIN |
| 48 | |
| 49 | class U_I18N_API DateFmtBestPattern : public SharedObject { |
| 50 | public: |
| 51 | UnicodeString fPattern; |
| 52 | |
| 53 | DateFmtBestPattern(const UnicodeString &pattern) |
| 54 | : fPattern(pattern) { } |
| 55 | ~DateFmtBestPattern(); |
| 56 | }; |
| 57 | |
| 58 | DateFmtBestPattern::~DateFmtBestPattern() { |
| 59 | } |
| 60 | |
| 61 | template<> U_I18N_API |
| 62 | const DateFmtBestPattern *LocaleCacheKey<DateFmtBestPattern>::createObject( |
| 63 | const void * , UErrorCode &status) const { |
| 64 | status = U_UNSUPPORTED_ERROR; |
| 65 | return NULL; |
| 66 | } |
| 67 | |
| 68 | class U_I18N_API DateFmtBestPatternKey : public LocaleCacheKey<DateFmtBestPattern> { |
| 69 | private: |
| 70 | UnicodeString fSkeleton; |
| 71 | protected: |
| 72 | virtual bool equals(const CacheKeyBase &other) const override { |
| 73 | if (!LocaleCacheKey<DateFmtBestPattern>::equals(other)) { |
| 74 | return false; |
| 75 | } |
| 76 | |
| 77 | return operator==(static_cast<const DateFmtBestPatternKey &>(other)); |
| 78 | } |
| 79 | public: |
| 80 | DateFmtBestPatternKey( |
| 81 | const Locale &loc, |
| 82 | const UnicodeString &skeleton, |
| 83 | UErrorCode &status) |
| 84 | : LocaleCacheKey<DateFmtBestPattern>(loc), |
| 85 | fSkeleton(DateTimePatternGenerator::staticGetSkeleton(skeleton, status)) { } |
| 86 | DateFmtBestPatternKey(const DateFmtBestPatternKey &other) : |
| 87 | LocaleCacheKey<DateFmtBestPattern>(other), |
| 88 | fSkeleton(other.fSkeleton) { } |
| 89 | virtual ~DateFmtBestPatternKey(); |
| 90 | virtual int32_t hashCode() const override { |
| 91 | return (int32_t)(37u * (uint32_t)LocaleCacheKey<DateFmtBestPattern>::hashCode() + (uint32_t)fSkeleton.hashCode()); |
| 92 | } |
| 93 | inline bool operator==(const DateFmtBestPatternKey &other) const { |
| 94 | return fSkeleton == other.fSkeleton; |
| 95 | } |
| 96 | virtual CacheKeyBase *clone() const override { |
| 97 | return new DateFmtBestPatternKey(*this); |
| 98 | } |
| 99 | virtual const DateFmtBestPattern *createObject( |
| 100 | const void * , UErrorCode &status) const override { |
| 101 | LocalPointer<DateTimePatternGenerator> dtpg( |
| 102 | DateTimePatternGenerator::createInstance(fLoc, status)); |
| 103 | if (U_FAILURE(status)) { |
| 104 | return NULL; |
| 105 | } |
| 106 | |
| 107 | LocalPointer<DateFmtBestPattern> pattern( |
| 108 | new DateFmtBestPattern( |
| 109 | dtpg->getBestPattern(fSkeleton, status)), |
| 110 | status); |
| 111 | if (U_FAILURE(status)) { |
| 112 | return NULL; |
| 113 | } |
| 114 | DateFmtBestPattern *result = pattern.orphan(); |
| 115 | result->addRef(); |
| 116 | return result; |
| 117 | } |
| 118 | }; |
| 119 | |
| 120 | DateFmtBestPatternKey::~DateFmtBestPatternKey() { } |
| 121 | |
| 122 | |
| 123 | DateFormat::DateFormat() |
| 124 | : fCalendar(0), |
| 125 | fNumberFormat(0), |
| 126 | fCapitalizationContext(UDISPCTX_CAPITALIZATION_NONE) |
| 127 | { |
| 128 | } |
| 129 | |
| 130 | |
| 131 | |
| 132 | DateFormat::DateFormat(const DateFormat& other) |
| 133 | : Format(other), |
| 134 | fCalendar(0), |
| 135 | fNumberFormat(0), |
| 136 | fCapitalizationContext(UDISPCTX_CAPITALIZATION_NONE) |
| 137 | { |
| 138 | *this = other; |
| 139 | } |
| 140 | |
| 141 | |
| 142 | |
| 143 | DateFormat& DateFormat::operator=(const DateFormat& other) |
| 144 | { |
| 145 | if (this != &other) |
| 146 | { |
| 147 | delete fCalendar; |
| 148 | delete fNumberFormat; |
| 149 | if(other.fCalendar) { |
| 150 | fCalendar = other.fCalendar->clone(); |
| 151 | } else { |
| 152 | fCalendar = NULL; |
| 153 | } |
| 154 | if(other.fNumberFormat) { |
| 155 | fNumberFormat = other.fNumberFormat->clone(); |
| 156 | } else { |
| 157 | fNumberFormat = NULL; |
| 158 | } |
| 159 | fBoolFlags = other.fBoolFlags; |
| 160 | fCapitalizationContext = other.fCapitalizationContext; |
| 161 | } |
| 162 | return *this; |
| 163 | } |
| 164 | |
| 165 | |
| 166 | |
| 167 | DateFormat::~DateFormat() |
| 168 | { |
| 169 | delete fCalendar; |
| 170 | delete fNumberFormat; |
| 171 | } |
| 172 | |
| 173 | |
| 174 | |
| 175 | bool |
| 176 | DateFormat::operator==(const Format& other) const |
| 177 | { |
| 178 | |
| 179 | |
| 180 | |
| 181 | |
| 182 | |
| 183 | DateFormat* fmt = (DateFormat*)&other; |
| 184 | |
| 185 | return (this == fmt) || |
| 186 | (Format::operator==(other) && |
| 187 | fCalendar&&(fCalendar->isEquivalentTo(*fmt->fCalendar)) && |
| 188 | (fNumberFormat && *fNumberFormat == *fmt->fNumberFormat) && |
| 189 | (fCapitalizationContext == fmt->fCapitalizationContext) ); |
| 190 | } |
| 191 | |
| 192 | |
| 193 | |
| 194 | UnicodeString& |
| 195 | DateFormat::format(const Formattable& obj, |
| 196 | UnicodeString& appendTo, |
| 197 | FieldPosition& fieldPosition, |
| 198 | UErrorCode& status) const |
| 199 | { |
| 200 | if (U_FAILURE(status)) return appendTo; |
| 201 | |
| 202 | |
| 203 | UDate date = 0; |
| 204 | switch (obj.getType()) |
| 205 | { |
| 206 | case Formattable::kDate: |
| 207 | date = obj.getDate(); |
| 208 | break; |
| 209 | case Formattable::kDouble: |
| 210 | date = (UDate)obj.getDouble(); |
| 211 | break; |
| 212 | case Formattable::kLong: |
| 213 | date = (UDate)obj.getLong(); |
| 214 | break; |
| 215 | default: |
| 216 | status = U_ILLEGAL_ARGUMENT_ERROR; |
| 217 | return appendTo; |
| 218 | } |
| 219 | |
| 220 | |
| 221 | |
| 222 | |
| 223 | |
| 224 | return format(date, appendTo, fieldPosition); |
| 225 | } |
| 226 | |
| 227 | |
| 228 | |
| 229 | UnicodeString& |
| 230 | DateFormat::format(const Formattable& obj, |
| 231 | UnicodeString& appendTo, |
| 232 | FieldPositionIterator* posIter, |
| 233 | UErrorCode& status) const |
| 234 | { |
| 235 | if (U_FAILURE(status)) return appendTo; |
| 236 | |
| 237 | |
| 238 | UDate date = 0; |
| 239 | switch (obj.getType()) |
| 240 | { |
| 241 | case Formattable::kDate: |
| 242 | date = obj.getDate(); |
| 243 | break; |
| 244 | case Formattable::kDouble: |
| 245 | date = (UDate)obj.getDouble(); |
| 246 | break; |
| 247 | case Formattable::kLong: |
| 248 | date = (UDate)obj.getLong(); |
| 249 | break; |
| 250 | default: |
| 251 | status = U_ILLEGAL_ARGUMENT_ERROR; |
| 252 | return appendTo; |
| 253 | } |
| 254 | |
| 255 | |
| 256 | |
| 257 | |
| 258 | |
| 259 | return format(date, appendTo, posIter, status); |
| 260 | } |
| 261 | |
| 262 | |
| 263 | |
| 264 | |
| 265 | UnicodeString& |
| 266 | DateFormat::format(Calendar& , |
| 267 | UnicodeString& appendTo, |
| 268 | FieldPositionIterator* , |
| 269 | UErrorCode& status) const { |
| 270 | if (U_SUCCESS(status)) { |
| 271 | status = U_UNSUPPORTED_ERROR; |
| 272 | } |
| 273 | return appendTo; |
| 274 | } |
| 275 | |
| 276 | |
| 277 | |
| 278 | UnicodeString& |
| 279 | DateFormat::format(UDate date, UnicodeString& appendTo, FieldPosition& fieldPosition) const { |
| 280 | if (fCalendar != NULL) { |
| 281 | |
| 282 | Calendar* calClone = fCalendar->clone(); |
| 283 | if (calClone != NULL) { |
| 284 | UErrorCode ec = U_ZERO_ERROR; |
| 285 | calClone->setTime(date, ec); |
| 286 | if (U_SUCCESS(ec)) { |
| 287 | format(*calClone, appendTo, fieldPosition); |
| 288 | } |
| 289 | delete calClone; |
| 290 | } |
| 291 | } |
| 292 | return appendTo; |
| 293 | } |
| 294 | |
| 295 | |
| 296 | |
| 297 | UnicodeString& |
| 298 | DateFormat::format(UDate date, UnicodeString& appendTo, FieldPositionIterator* posIter, |
| 299 | UErrorCode& status) const { |
| 300 | if (fCalendar != NULL) { |
| 301 | Calendar* calClone = fCalendar->clone(); |
| 302 | if (calClone != NULL) { |
| 303 | calClone->setTime(date, status); |
| 304 | if (U_SUCCESS(status)) { |
| 305 | format(*calClone, appendTo, posIter, status); |
| 306 | } |
| 307 | delete calClone; |
| 308 | } |
| 309 | } |
| 310 | return appendTo; |
| 311 | } |
| 312 | |
| 313 | |
| 314 | |
| 315 | UnicodeString& |
| 316 | DateFormat::format(UDate date, UnicodeString& appendTo) const |
| 317 | { |
| 318 | |
| 319 | |
| 320 | FieldPosition fpos(FieldPosition::DONT_CARE); |
| 321 | return format(date, appendTo, fpos); |
| 322 | } |
| 323 | |
| 324 | |
| 325 | |
| 326 | UDate |
| 327 | DateFormat::parse(const UnicodeString& text, |
| 328 | ParsePosition& pos) const |
| 329 | { |
| 330 | UDate d = 0; |
| 331 | if (fCalendar != NULL) { |
| 332 | Calendar* calClone = fCalendar->clone(); |
| 333 | if (calClone != NULL) { |
| 334 | int32_t start = pos.getIndex(); |
| 335 | calClone->clear(); |
| 336 | parse(text, *calClone, pos); |
| 337 | if (pos.getIndex() != start) { |
| 338 | UErrorCode ec = U_ZERO_ERROR; |
| 339 | d = calClone->getTime(ec); |
| 340 | if (U_FAILURE(ec)) { |
| 341 | |
| 342 | |
| 343 | |
| 344 | pos.setIndex(start); |
| 345 | pos.setErrorIndex(start); |
| 346 | d = 0; |
| 347 | } |
| 348 | } |
| 349 | delete calClone; |
| 350 | } |
| 351 | } |
| 352 | return d; |
| 353 | } |
| 354 | |
| 355 | |
| 356 | |
| 357 | UDate |
| 358 | DateFormat::parse(const UnicodeString& text, |
| 359 | UErrorCode& status) const |
| 360 | { |
| 361 | if (U_FAILURE(status)) return 0; |
| 362 | |
| 363 | ParsePosition pos(0); |
| 364 | UDate result = parse(text, pos); |
| 365 | if (pos.getIndex() == 0) { |
| 366 | #if defined (U_DEBUG_CAL) |
| 367 | fprintf(stderr, "%s:%d - - failed to parse - err index %d\n" |
| 368 | , __FILE__, __LINE__, pos.getErrorIndex() ); |
| 369 | #endif |
| 370 | status = U_ILLEGAL_ARGUMENT_ERROR; |
| 371 | } |
| 372 | return result; |
| 373 | } |
| 374 | |
| 375 | |
| 376 | |
| 377 | void |
| 378 | DateFormat::parseObject(const UnicodeString& source, |
| 379 | Formattable& result, |
| 380 | ParsePosition& pos) const |
| 381 | { |
| 382 | result.setDate(parse(source, pos)); |
| 383 | } |
| 384 | |
| 385 | |
| 386 | |
| 387 | DateFormat* U_EXPORT2 |
| 388 | DateFormat::createTimeInstance(DateFormat::EStyle style, |
| 389 | const Locale& aLocale) |
| 390 | { |
| 391 | return createDateTimeInstance(kNone, style, aLocale); |
| 392 | } |
| 393 | |
| 394 | |
| 395 | |
| 396 | DateFormat* U_EXPORT2 |
| 397 | DateFormat::createDateInstance(DateFormat::EStyle style, |
| 398 | const Locale& aLocale) |
| 399 | { |
| 400 | return createDateTimeInstance(style, kNone, aLocale); |
| 401 | } |
| 402 | |
| 403 | |
| 404 | |
| 405 | DateFormat* U_EXPORT2 |
| 406 | DateFormat::createDateTimeInstance(EStyle dateStyle, |
| 407 | EStyle timeStyle, |
| 408 | const Locale& aLocale) |
| 409 | { |
| 410 | if(dateStyle != kNone) |
| 411 | { |
| 412 | dateStyle = (EStyle) (dateStyle + kDateOffset); |
| 413 | } |
| 414 | return create(timeStyle, dateStyle, aLocale); |
| 415 | } |
| 416 | |
| 417 | |
| 418 | |
| 419 | DateFormat* U_EXPORT2 |
| 420 | DateFormat::createInstance() |
| 421 | { |
| 422 | return createDateTimeInstance(kShort, kShort, Locale::getDefault()); |
| 423 | } |
| 424 | |
| 425 | |
| 426 | |
| 427 | UnicodeString U_EXPORT2 |
| 428 | DateFormat::getBestPattern( |
| 429 | const Locale &locale, |
| 430 | const UnicodeString &skeleton, |
| 431 | UErrorCode &status) { |
| 432 | UnifiedCache *cache = UnifiedCache::getInstance(status); |
| 433 | if (U_FAILURE(status)) { |
| |
| 434 | return UnicodeString(); |
| 435 | } |
| 436 | DateFmtBestPatternKey key(locale, skeleton, status); |
| 437 | const DateFmtBestPattern *patternPtr = NULL; |
| 5 | | 'patternPtr' initialized to a null pointer value | |
|
| 438 | cache->get(key, patternPtr, status); |
| 6 | | Calling 'UnifiedCache::get' | |
|
| 19 | | Returning from 'UnifiedCache::get' | |
|
| 439 | if (U_FAILURE(status)) { |
| |
| 440 | return UnicodeString(); |
| 441 | } |
| 442 | UnicodeString result(patternPtr->fPattern); |
| 21 | | Forming reference to null pointer |
|
| 443 | patternPtr->removeRef(); |
| 444 | return result; |
| 445 | } |
| 446 | |
| 447 | DateFormat* U_EXPORT2 |
| 448 | DateFormat::createInstanceForSkeleton( |
| 449 | Calendar *calendarToAdopt, |
| 450 | const UnicodeString& skeleton, |
| 451 | const Locale &locale, |
| 452 | UErrorCode &status) { |
| 453 | LocalPointer<Calendar> calendar(calendarToAdopt); |
| 454 | if (U_FAILURE(status)) { |
| 455 | return NULL; |
| 456 | } |
| 457 | if (calendar.isNull()) { |
| 458 | status = U_ILLEGAL_ARGUMENT_ERROR; |
| 459 | return NULL; |
| 460 | } |
| 461 | Locale localeWithCalendar = locale; |
| 462 | localeWithCalendar.setKeywordValue("calendar", calendar->getType(), status); |
| 463 | if (U_FAILURE(status)) { |
| 464 | return NULL; |
| 465 | } |
| 466 | DateFormat *result = createInstanceForSkeleton(skeleton, localeWithCalendar, status); |
| 467 | if (U_FAILURE(status)) { |
| 468 | return NULL; |
| 469 | } |
| 470 | result->adoptCalendar(calendar.orphan()); |
| 471 | return result; |
| 472 | } |
| 473 | |
| 474 | DateFormat* U_EXPORT2 |
| 475 | DateFormat::createInstanceForSkeleton( |
| 476 | const UnicodeString& skeleton, |
| 477 | const Locale &locale, |
| 478 | UErrorCode &status) { |
| 479 | if (U_FAILURE(status)) { |
| |
| 480 | return NULL; |
| 481 | } |
| 482 | LocalPointer<DateFormat> df( |
| 483 | new SimpleDateFormat( |
| 484 | getBestPattern(locale, skeleton, status), |
| 3 | | Calling 'DateFormat::getBestPattern' | |
|
| 485 | locale, status), |
| 486 | status); |
| 487 | return U_SUCCESS(status) ? df.orphan() : NULL; |
| 488 | } |
| 489 | |
| 490 | DateFormat* U_EXPORT2 |
| 491 | DateFormat::createInstanceForSkeleton( |
| 492 | const UnicodeString& skeleton, |
| 493 | UErrorCode &status) { |
| 494 | return createInstanceForSkeleton( |
| 1 | Calling 'DateFormat::createInstanceForSkeleton' | |
|
| 495 | skeleton, Locale::getDefault(), status); |
| 496 | } |
| 497 | |
| 498 | |
| 499 | |
| 500 | DateFormat* U_EXPORT2 |
| 501 | DateFormat::create(EStyle timeStyle, EStyle dateStyle, const Locale& locale) |
| 502 | { |
| 503 | UErrorCode status = U_ZERO_ERROR; |
| 504 | #if U_PLATFORM_USES_ONLY_WIN32_API |
| 505 | char buffer[8]; |
| 506 | int32_t count = locale.getKeywordValue("compat", buffer, sizeof(buffer), status); |
| 507 | |
| 508 | |
| 509 | if (count > 0 && uprv_strcmp(buffer, "host") == 0) { |
| 510 | Win32DateFormat *f = new Win32DateFormat(timeStyle, dateStyle, locale, status); |
| 511 | |
| 512 | if (U_SUCCESS(status)) { |
| 513 | return f; |
| 514 | } |
| 515 | |
| 516 | delete f; |
| 517 | } |
| 518 | #endif |
| 519 | |
| 520 | |
| 521 | if(((dateStyle!=kNone)&&((dateStyle-kDateOffset) & UDAT_RELATIVE))) { |
| 522 | RelativeDateFormat *r = new RelativeDateFormat((UDateFormatStyle)timeStyle, (UDateFormatStyle)(dateStyle-kDateOffset), locale, status); |
| 523 | if(U_SUCCESS(status)) return r; |
| 524 | delete r; |
| 525 | status = U_ZERO_ERROR; |
| 526 | } |
| 527 | |
| 528 | |
| 529 | SimpleDateFormat *f = new SimpleDateFormat(timeStyle, dateStyle, locale, status); |
| 530 | if (U_SUCCESS(status)) return f; |
| 531 | delete f; |
| 532 | |
| 533 | |
| 534 | |
| 535 | status = U_ZERO_ERROR; |
| 536 | f = new SimpleDateFormat(locale, status); |
| 537 | if (U_SUCCESS(status)) return f; |
| 538 | delete f; |
| 539 | |
| 540 | |
| 541 | |
| 542 | |
| 543 | return 0; |
| 544 | } |
| 545 | |
| 546 | |
| 547 | |
| 548 | const Locale* U_EXPORT2 |
| 549 | DateFormat::getAvailableLocales(int32_t& count) |
| 550 | { |
| 551 | |
| 552 | |
| 553 | |
| 554 | return Locale::getAvailableLocales(count); |
| 555 | } |
| 556 | |
| 557 | |
| 558 | |
| 559 | void |
| 560 | DateFormat::adoptCalendar(Calendar* newCalendar) |
| 561 | { |
| 562 | delete fCalendar; |
| 563 | fCalendar = newCalendar; |
| 564 | } |
| 565 | |
| 566 | |
| 567 | void |
| 568 | DateFormat::setCalendar(const Calendar& newCalendar) |
| 569 | { |
| 570 | Calendar* newCalClone = newCalendar.clone(); |
| 571 | if (newCalClone != NULL) { |
| 572 | adoptCalendar(newCalClone); |
| 573 | } |
| 574 | } |
| 575 | |
| 576 | |
| 577 | |
| 578 | const Calendar* |
| 579 | DateFormat::getCalendar() const |
| 580 | { |
| 581 | return fCalendar; |
| 582 | } |
| 583 | |
| 584 | |
| 585 | |
| 586 | void |
| 587 | DateFormat::adoptNumberFormat(NumberFormat* newNumberFormat) |
| 588 | { |
| 589 | delete fNumberFormat; |
| 590 | fNumberFormat = newNumberFormat; |
| 591 | newNumberFormat->setParseIntegerOnly(TRUE); |
| 592 | newNumberFormat->setGroupingUsed(FALSE); |
| 593 | } |
| 594 | |
| 595 | |
| 596 | void |
| 597 | DateFormat::setNumberFormat(const NumberFormat& newNumberFormat) |
| 598 | { |
| 599 | NumberFormat* newNumFmtClone = newNumberFormat.clone(); |
| 600 | if (newNumFmtClone != NULL) { |
| 601 | adoptNumberFormat(newNumFmtClone); |
| 602 | } |
| 603 | } |
| 604 | |
| 605 | |
| 606 | |
| 607 | const NumberFormat* |
| 608 | DateFormat::getNumberFormat() const |
| 609 | { |
| 610 | return fNumberFormat; |
| 611 | } |
| 612 | |
| 613 | |
| 614 | |
| 615 | void |
| 616 | DateFormat::adoptTimeZone(TimeZone* zone) |
| 617 | { |
| 618 | if (fCalendar != NULL) { |
| 619 | fCalendar->adoptTimeZone(zone); |
| 620 | } |
| 621 | } |
| 622 | |
| 623 | |
| 624 | void |
| 625 | DateFormat::setTimeZone(const TimeZone& zone) |
| 626 | { |
| 627 | if (fCalendar != NULL) { |
| 628 | fCalendar->setTimeZone(zone); |
| 629 | } |
| 630 | } |
| 631 | |
| 632 | |
| 633 | |
| 634 | const TimeZone& |
| 635 | DateFormat::getTimeZone() const |
| 636 | { |
| 637 | if (fCalendar != NULL) { |
| 638 | return fCalendar->getTimeZone(); |
| 639 | } |
| 640 | |
| 641 | |
| 642 | return *(TimeZone::createDefault()); |
| 643 | } |
| 644 | |
| 645 | |
| 646 | |
| 647 | void |
| 648 | DateFormat::setLenient(UBool lenient) |
| 649 | { |
| 650 | if (fCalendar != NULL) { |
| 651 | fCalendar->setLenient(lenient); |
| 652 | } |
| 653 | UErrorCode status = U_ZERO_ERROR; |
| 654 | setBooleanAttribute(UDAT_PARSE_ALLOW_WHITESPACE, lenient, status); |
| 655 | setBooleanAttribute(UDAT_PARSE_ALLOW_NUMERIC, lenient, status); |
| 656 | } |
| 657 | |
| 658 | |
| 659 | |
| 660 | UBool |
| 661 | DateFormat::isLenient() const |
| 662 | { |
| 663 | UBool lenient = TRUE; |
| 664 | if (fCalendar != NULL) { |
| 665 | lenient = fCalendar->isLenient(); |
| 666 | } |
| 667 | UErrorCode status = U_ZERO_ERROR; |
| 668 | return lenient |
| 669 | && getBooleanAttribute(UDAT_PARSE_ALLOW_WHITESPACE, status) |
| 670 | && getBooleanAttribute(UDAT_PARSE_ALLOW_NUMERIC, status); |
| 671 | } |
| 672 | |
| 673 | void |
| 674 | DateFormat::setCalendarLenient(UBool lenient) |
| 675 | { |
| 676 | if (fCalendar != NULL) { |
| 677 | fCalendar->setLenient(lenient); |
| 678 | } |
| 679 | } |
| 680 | |
| 681 | |
| 682 | |
| 683 | UBool |
| 684 | DateFormat::isCalendarLenient() const |
| 685 | { |
| 686 | if (fCalendar != NULL) { |
| 687 | return fCalendar->isLenient(); |
| 688 | } |
| 689 | |
| 690 | return FALSE; |
| 691 | } |
| 692 | |
| 693 | |
| 694 | |
| 695 | |
| 696 | |
| 697 | void DateFormat::setContext(UDisplayContext value, UErrorCode& status) |
| 698 | { |
| 699 | if (U_FAILURE(status)) |
| 700 | return; |
| 701 | if ( (UDisplayContextType)((uint32_t)value >> 8) == UDISPCTX_TYPE_CAPITALIZATION ) { |
| 702 | fCapitalizationContext = value; |
| 703 | } else { |
| 704 | status = U_ILLEGAL_ARGUMENT_ERROR; |
| 705 | } |
| 706 | } |
| 707 | |
| 708 | |
| 709 | |
| 710 | |
| 711 | |
| 712 | UDisplayContext DateFormat::getContext(UDisplayContextType type, UErrorCode& status) const |
| 713 | { |
| 714 | if (U_FAILURE(status)) |
| 715 | return (UDisplayContext)0; |
| 716 | if (type != UDISPCTX_TYPE_CAPITALIZATION) { |
| 717 | status = U_ILLEGAL_ARGUMENT_ERROR; |
| 718 | return (UDisplayContext)0; |
| 719 | } |
| 720 | return fCapitalizationContext; |
| 721 | } |
| 722 | |
| 723 | |
| 724 | |
| 725 | |
| 726 | |
| 727 | DateFormat& |
| 728 | DateFormat::setBooleanAttribute(UDateFormatBooleanAttribute attr, |
| 729 | UBool newValue, |
| 730 | UErrorCode &status) { |
| 731 | if(!fBoolFlags.isValidValue(newValue)) { |
| 732 | status = U_ILLEGAL_ARGUMENT_ERROR; |
| 733 | } else { |
| 734 | fBoolFlags.set(attr, newValue); |
| 735 | } |
| 736 | |
| 737 | return *this; |
| 738 | } |
| 739 | |
| 740 | |
| 741 | |
| 742 | UBool |
| 743 | DateFormat::getBooleanAttribute(UDateFormatBooleanAttribute attr, UErrorCode &) const { |
| 744 | |
| 745 | return static_cast<UBool>(fBoolFlags.get(attr)); |
| 746 | } |
| 747 | |
| 748 | U_NAMESPACE_END |
| 749 | |
| 750 | #endif /* #if !UCONFIG_NO_FORMATTING */ |
| 751 | |
| 752 | |
| 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, NULL, 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 = NULL; |
| 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); |
| 358 | UnifiedCache &operator=(const UnifiedCache &other); |
| 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 |