1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 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 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042
|
// © 2016 and later: Unicode, Inc. and others.
// License & terms of use: http://www.unicode.org/copyright.html
/*
*******************************************************************************
* Copyright (C) 1997-2016, International Business Machines Corporation and
* others. All Rights Reserved.
*******************************************************************************
*
* File FMTABLE.CPP
*
* Modification History:
*
* Date Name Description
* 03/25/97 clhuang Initial Implementation.
********************************************************************************
*/
#include <_foundation_unicode/utypes.h>
#if !UCONFIG_NO_FORMATTING
#include <cstdlib>
#include <math.h>
#include <_foundation_unicode/fmtable.h>
#include <_foundation_unicode/ustring.h>
#include <_foundation_unicode/measure.h>
#include <_foundation_unicode/curramt.h>
#include <_foundation_unicode/uformattable.h>
#include "charstr.h"
#include "cmemory.h"
#include "cstring.h"
#include "fmtableimp.h"
#include "number_decimalquantity.h"
// *****************************************************************************
// class Formattable
// *****************************************************************************
U_NAMESPACE_BEGIN
UOBJECT_DEFINE_RTTI_IMPLEMENTATION(Formattable)
using number::impl::DecimalQuantity;
//-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.
// NOTE: As of 3.0, there are limitations to the UObject API. It does
// not (yet) support cloning, operator=, nor operator==. To
// work around this, I implement some simple inlines here. Later
// these can be modified or removed. [alan]
// NOTE: These inlines assume that all fObjects are in fact instances
// of the Measure class, which is true as of 3.0. [alan]
// Return true if *a == *b.
static inline UBool objectEquals(const UObject* a, const UObject* b) {
// LATER: return *a == *b;
return *((const Measure*) a) == *b;
}
// Return a clone of *a.
static inline UObject* objectClone(const UObject* a) {
// LATER: return a->clone();
return ((const Measure*) a)->clone();
}
// Return true if *a is an instance of Measure.
static inline UBool instanceOfMeasure(const UObject* a) {
return dynamic_cast<const Measure*>(a) != nullptr;
}
/**
* Creates a new Formattable array and copies the values from the specified
* original.
* @param array the original array
* @param count the original array count
* @return the new Formattable array.
*/
static Formattable* createArrayCopy(const Formattable* array, int32_t count) {
Formattable *result = new Formattable[count];
if (result != nullptr) {
for (int32_t i=0; i<count; ++i)
result[i] = array[i]; // Don't memcpy!
}
return result;
}
//-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.
/**
* Set 'ec' to 'err' only if 'ec' is not already set to a failing UErrorCode.
*/
static void setError(UErrorCode& ec, UErrorCode err) {
if (U_SUCCESS(ec)) {
ec = err;
}
}
//
// Common initialization code, shared by constructors.
// Put everything into a known state.
//
void Formattable::init() {
fValue.fInt64 = 0;
fType = kLong;
fDecimalStr = nullptr;
fDecimalQuantity = nullptr;
fBogus.setToBogus();
}
// -------------------------------------
// default constructor.
// Creates a formattable object with a long value 0.
Formattable::Formattable() {
init();
}
// -------------------------------------
// Creates a formattable object with a Date instance.
Formattable::Formattable(UDate date, ISDATE /*isDate*/)
{
init();
fType = kDate;
fValue.fDate = date;
}
// -------------------------------------
// Creates a formattable object with a double value.
Formattable::Formattable(double value)
{
init();
fType = kDouble;
fValue.fDouble = value;
}
// -------------------------------------
// Creates a formattable object with an int32_t value.
Formattable::Formattable(int32_t value)
{
init();
fValue.fInt64 = value;
}
// -------------------------------------
// Creates a formattable object with an int64_t value.
Formattable::Formattable(int64_t value)
{
init();
fType = kInt64;
fValue.fInt64 = value;
}
// -------------------------------------
// Creates a formattable object with a decimal number value from a string.
Formattable::Formattable(StringPiece number, UErrorCode &status) {
init();
setDecimalNumber(number, status);
}
// -------------------------------------
// Creates a formattable object with a UnicodeString instance.
Formattable::Formattable(const UnicodeString& stringToCopy)
{
init();
fType = kString;
fValue.fString = new UnicodeString(stringToCopy);
}
// -------------------------------------
// Creates a formattable object with a UnicodeString* value.
// (adopting semantics)
Formattable::Formattable(UnicodeString* stringToAdopt)
{
init();
fType = kString;
fValue.fString = stringToAdopt;
}
Formattable::Formattable(UObject* objectToAdopt)
{
init();
fType = kObject;
fValue.fObject = objectToAdopt;
}
// -------------------------------------
Formattable::Formattable(const Formattable* arrayToCopy, int32_t count)
: UObject(), fType(kArray)
{
init();
fType = kArray;
fValue.fArrayAndCount.fArray = createArrayCopy(arrayToCopy, count);
fValue.fArrayAndCount.fCount = count;
}
// -------------------------------------
// copy constructor
Formattable::Formattable(const Formattable &source)
: UObject(*this)
{
init();
*this = source;
}
// -------------------------------------
// assignment operator
Formattable&
Formattable::operator=(const Formattable& source)
{
if (this != &source)
{
// Disposes the current formattable value/setting.
dispose();
// Sets the correct data type for this value.
fType = source.fType;
switch (fType)
{
case kArray:
// Sets each element in the array one by one and records the array count.
fValue.fArrayAndCount.fCount = source.fValue.fArrayAndCount.fCount;
fValue.fArrayAndCount.fArray = createArrayCopy(source.fValue.fArrayAndCount.fArray,
source.fValue.fArrayAndCount.fCount);
break;
case kString:
// Sets the string value.
fValue.fString = new UnicodeString(*source.fValue.fString);
break;
case kDouble:
// Sets the double value.
fValue.fDouble = source.fValue.fDouble;
break;
case kLong:
case kInt64:
// Sets the long value.
fValue.fInt64 = source.fValue.fInt64;
break;
case kDate:
// Sets the Date value.
fValue.fDate = source.fValue.fDate;
break;
case kObject:
fValue.fObject = objectClone(source.fValue.fObject);
break;
}
UErrorCode status = U_ZERO_ERROR;
if (source.fDecimalQuantity != nullptr) {
fDecimalQuantity = new DecimalQuantity(*source.fDecimalQuantity);
}
if (source.fDecimalStr != nullptr) {
fDecimalStr = new CharString(*source.fDecimalStr, status);
if (U_FAILURE(status)) {
delete fDecimalStr;
fDecimalStr = nullptr;
}
}
}
return *this;
}
// -------------------------------------
bool
Formattable::operator==(const Formattable& that) const
{
int32_t i;
if (this == &that) return true;
// Returns false if the data types are different.
if (fType != that.fType) return false;
// Compares the actual data values.
bool equal = true;
switch (fType) {
case kDate:
equal = (fValue.fDate == that.fValue.fDate);
break;
case kDouble:
equal = (fValue.fDouble == that.fValue.fDouble);
break;
case kLong:
case kInt64:
equal = (fValue.fInt64 == that.fValue.fInt64);
break;
case kString:
equal = (*(fValue.fString) == *(that.fValue.fString));
break;
case kArray:
if (fValue.fArrayAndCount.fCount != that.fValue.fArrayAndCount.fCount) {
equal = false;
break;
}
// Checks each element for equality.
for (i=0; i<fValue.fArrayAndCount.fCount; ++i) {
if (fValue.fArrayAndCount.fArray[i] != that.fValue.fArrayAndCount.fArray[i]) {
equal = false;
break;
}
}
break;
case kObject:
if (fValue.fObject == nullptr || that.fValue.fObject == nullptr) {
equal = false;
} else {
equal = objectEquals(fValue.fObject, that.fValue.fObject);
}
break;
}
// TODO: compare digit lists if numeric.
return equal;
}
// -------------------------------------
Formattable::~Formattable()
{
dispose();
}
// -------------------------------------
void Formattable::dispose()
{
// Deletes the data value if necessary.
switch (fType) {
case kString:
delete fValue.fString;
break;
case kArray:
delete[] fValue.fArrayAndCount.fArray;
break;
case kObject:
delete fValue.fObject;
break;
default:
break;
}
fType = kLong;
fValue.fInt64 = 0;
delete fDecimalStr;
fDecimalStr = nullptr;
delete fDecimalQuantity;
fDecimalQuantity = nullptr;
}
Formattable *
Formattable::clone() const {
return new Formattable(*this);
}
// -------------------------------------
// Gets the data type of this Formattable object.
Formattable::Type
Formattable::getType() const
{
return fType;
}
UBool
Formattable::isNumeric() const {
switch (fType) {
case kDouble:
case kLong:
case kInt64:
return true;
default:
return false;
}
}
// -------------------------------------
int32_t
//Formattable::getLong(UErrorCode* status) const
Formattable::getLong(UErrorCode& status) const
{
if (U_FAILURE(status)) {
return 0;
}
switch (fType) {
case Formattable::kLong:
return (int32_t)fValue.fInt64;
case Formattable::kInt64:
if (fValue.fInt64 > INT32_MAX) {
status = U_INVALID_FORMAT_ERROR;
return INT32_MAX;
} else if (fValue.fInt64 < INT32_MIN) {
status = U_INVALID_FORMAT_ERROR;
return INT32_MIN;
} else {
return (int32_t)fValue.fInt64;
}
case Formattable::kDouble:
if (fValue.fDouble > INT32_MAX) {
status = U_INVALID_FORMAT_ERROR;
return INT32_MAX;
} else if (fValue.fDouble < INT32_MIN) {
status = U_INVALID_FORMAT_ERROR;
return INT32_MIN;
} else {
return (int32_t)fValue.fDouble; // loses fraction
}
case Formattable::kObject:
if (fValue.fObject == nullptr) {
status = U_MEMORY_ALLOCATION_ERROR;
return 0;
}
// TODO Later replace this with instanceof call
if (instanceOfMeasure(fValue.fObject)) {
return ((const Measure*) fValue.fObject)->
getNumber().getLong(status);
}
U_FALLTHROUGH;
default:
status = U_INVALID_FORMAT_ERROR;
return 0;
}
}
// -------------------------------------
// Maximum int that can be represented exactly in a double. (53 bits)
// Larger ints may be rounded to a near-by value as not all are representable.
// TODO: move this constant elsewhere, possibly configure it for different
// floating point formats, if any non-standard ones are still in use.
static const int64_t U_DOUBLE_MAX_EXACT_INT = 9007199254740992LL;
int64_t
Formattable::getInt64(UErrorCode& status) const
{
if (U_FAILURE(status)) {
return 0;
}
switch (fType) {
case Formattable::kLong:
case Formattable::kInt64:
return fValue.fInt64;
case Formattable::kDouble:
if (fValue.fDouble > (double)U_INT64_MAX) {
status = U_INVALID_FORMAT_ERROR;
return U_INT64_MAX;
} else if (fValue.fDouble < (double)U_INT64_MIN) {
status = U_INVALID_FORMAT_ERROR;
return U_INT64_MIN;
} else if (fabs(fValue.fDouble) > U_DOUBLE_MAX_EXACT_INT && fDecimalQuantity != nullptr) {
if (fDecimalQuantity->fitsInLong(true)) {
return fDecimalQuantity->toLong();
} else {
// Unexpected
status = U_INVALID_FORMAT_ERROR;
return fDecimalQuantity->isNegative() ? U_INT64_MIN : U_INT64_MAX;
}
} else {
return (int64_t)fValue.fDouble;
}
case Formattable::kObject:
if (fValue.fObject == nullptr) {
status = U_MEMORY_ALLOCATION_ERROR;
return 0;
}
if (instanceOfMeasure(fValue.fObject)) {
return ((const Measure*) fValue.fObject)->
getNumber().getInt64(status);
}
U_FALLTHROUGH;
default:
status = U_INVALID_FORMAT_ERROR;
return 0;
}
}
// -------------------------------------
double
Formattable::getDouble(UErrorCode& status) const
{
if (U_FAILURE(status)) {
return 0;
}
switch (fType) {
case Formattable::kLong:
case Formattable::kInt64: // loses precision
return (double)fValue.fInt64;
case Formattable::kDouble:
return fValue.fDouble;
case Formattable::kObject:
if (fValue.fObject == nullptr) {
status = U_MEMORY_ALLOCATION_ERROR;
return 0;
}
// TODO Later replace this with instanceof call
if (instanceOfMeasure(fValue.fObject)) {
return ((const Measure*) fValue.fObject)->
getNumber().getDouble(status);
}
U_FALLTHROUGH;
default:
status = U_INVALID_FORMAT_ERROR;
return 0;
}
}
const UObject*
Formattable::getObject() const {
return (fType == kObject) ? fValue.fObject : nullptr;
}
// -------------------------------------
// Sets the value to a double value d.
void
Formattable::setDouble(double d)
{
dispose();
fType = kDouble;
fValue.fDouble = d;
}
// -------------------------------------
// Sets the value to a long value l.
void
Formattable::setLong(int32_t l)
{
dispose();
fType = kLong;
fValue.fInt64 = l;
}
// -------------------------------------
// Sets the value to an int64 value ll.
void
Formattable::setInt64(int64_t ll)
{
dispose();
fType = kInt64;
fValue.fInt64 = ll;
}
// -------------------------------------
// Sets the value to a Date instance d.
void
Formattable::setDate(UDate d)
{
dispose();
fType = kDate;
fValue.fDate = d;
}
// -------------------------------------
// Sets the value to a string value stringToCopy.
void
Formattable::setString(const UnicodeString& stringToCopy)
{
dispose();
fType = kString;
fValue.fString = new UnicodeString(stringToCopy);
}
// -------------------------------------
// Sets the value to an array of Formattable objects.
void
Formattable::setArray(const Formattable* array, int32_t count)
{
dispose();
fType = kArray;
fValue.fArrayAndCount.fArray = createArrayCopy(array, count);
fValue.fArrayAndCount.fCount = count;
}
// -------------------------------------
// Adopts the stringToAdopt value.
void
Formattable::adoptString(UnicodeString* stringToAdopt)
{
dispose();
fType = kString;
fValue.fString = stringToAdopt;
}
// -------------------------------------
// Adopts the array value and its count.
void
Formattable::adoptArray(Formattable* array, int32_t count)
{
dispose();
fType = kArray;
fValue.fArrayAndCount.fArray = array;
fValue.fArrayAndCount.fCount = count;
}
void
Formattable::adoptObject(UObject* objectToAdopt) {
dispose();
fType = kObject;
fValue.fObject = objectToAdopt;
}
// -------------------------------------
UnicodeString&
Formattable::getString(UnicodeString& result, UErrorCode& status) const
{
if (fType != kString) {
setError(status, U_INVALID_FORMAT_ERROR);
result.setToBogus();
} else {
if (fValue.fString == nullptr) {
setError(status, U_MEMORY_ALLOCATION_ERROR);
} else {
result = *fValue.fString;
}
}
return result;
}
// -------------------------------------
const UnicodeString&
Formattable::getString(UErrorCode& status) const
{
if (fType != kString) {
setError(status, U_INVALID_FORMAT_ERROR);
return *getBogus();
}
if (fValue.fString == nullptr) {
setError(status, U_MEMORY_ALLOCATION_ERROR);
return *getBogus();
}
return *fValue.fString;
}
// -------------------------------------
UnicodeString&
Formattable::getString(UErrorCode& status)
{
if (fType != kString) {
setError(status, U_INVALID_FORMAT_ERROR);
return *getBogus();
}
if (fValue.fString == nullptr) {
setError(status, U_MEMORY_ALLOCATION_ERROR);
return *getBogus();
}
return *fValue.fString;
}
// -------------------------------------
const Formattable*
Formattable::getArray(int32_t& count, UErrorCode& status) const
{
if (fType != kArray) {
setError(status, U_INVALID_FORMAT_ERROR);
count = 0;
return nullptr;
}
count = fValue.fArrayAndCount.fCount;
return fValue.fArrayAndCount.fArray;
}
// -------------------------------------
// Gets the bogus string, ensures mondo bogosity.
UnicodeString*
Formattable::getBogus() const
{
return (UnicodeString*)&fBogus; /* cast away const :-( */
}
// --------------------------------------
StringPiece Formattable::getDecimalNumber(UErrorCode &status) {
if (U_FAILURE(status)) {
return "";
}
if (fDecimalStr != nullptr) {
return fDecimalStr->toStringPiece();
}
CharString *decimalStr = internalGetCharString(status);
if(decimalStr == nullptr) {
return ""; // getDecimalNumber returns "" for error cases
} else {
return decimalStr->toStringPiece();
}
}
CharString *Formattable::internalGetCharString(UErrorCode &status) {
if(fDecimalStr == nullptr) {
if (fDecimalQuantity == nullptr) {
// No decimal number for the formattable yet. Which means the value was
// set directly by the user as an int, int64 or double. If the value came
// from parsing, or from the user setting a decimal number, fDecimalNum
// would already be set.
//
LocalPointer<DecimalQuantity> dq(new DecimalQuantity(), status);
if (U_FAILURE(status)) { return nullptr; }
populateDecimalQuantity(*dq, status);
if (U_FAILURE(status)) { return nullptr; }
fDecimalQuantity = dq.orphan();
}
fDecimalStr = new CharString();
if (fDecimalStr == nullptr) {
status = U_MEMORY_ALLOCATION_ERROR;
return nullptr;
}
// Older ICUs called uprv_decNumberToString here, which is not exactly the same as
// DecimalQuantity::toScientificString(). The biggest difference is that uprv_decNumberToString does
// not print scientific notation for magnitudes greater than -5 and smaller than some amount (+5?).
if (fDecimalQuantity->isInfinite()) {
fDecimalStr->append("Infinity", status);
} else if (fDecimalQuantity->isNaN()) {
fDecimalStr->append("NaN", status);
} else if (fDecimalQuantity->isZeroish()) {
fDecimalStr->append("0", -1, status);
} else if (fType==kLong || fType==kInt64 || // use toPlainString for integer types
(fDecimalQuantity->getMagnitude() != INT32_MIN && std::abs(fDecimalQuantity->getMagnitude()) < 5)) {
fDecimalStr->appendInvariantChars(fDecimalQuantity->toPlainString(), status);
} else {
fDecimalStr->appendInvariantChars(fDecimalQuantity->toScientificString(), status);
}
}
return fDecimalStr;
}
void
Formattable::populateDecimalQuantity(number::impl::DecimalQuantity& output, UErrorCode& status) const {
if (fDecimalQuantity != nullptr) {
output = *fDecimalQuantity;
return;
}
switch (fType) {
case kDouble:
output.setToDouble(this->getDouble());
output.roundToInfinity();
break;
case kLong:
output.setToInt(this->getLong());
break;
case kInt64:
output.setToLong(this->getInt64());
break;
default:
// The formattable's value is not a numeric type.
status = U_INVALID_STATE_ERROR;
}
}
// ---------------------------------------
void
Formattable::adoptDecimalQuantity(DecimalQuantity *dq) {
if (fDecimalQuantity != nullptr) {
delete fDecimalQuantity;
}
fDecimalQuantity = dq;
if (dq == nullptr) { // allow adoptDigitList(nullptr) to clear
return;
}
// Set the value into the Union of simple type values.
// Cannot use the set() functions because they would delete the fDecimalNum value.
if (fDecimalQuantity->fitsInLong()) {
fValue.fInt64 = fDecimalQuantity->toLong();
if (fValue.fInt64 <= INT32_MAX && fValue.fInt64 >= INT32_MIN) {
fType = kLong;
} else {
fType = kInt64;
}
} else {
fType = kDouble;
fValue.fDouble = fDecimalQuantity->toDouble();
}
}
// ---------------------------------------
void
Formattable::setDecimalNumber(StringPiece numberString, UErrorCode &status) {
if (U_FAILURE(status)) {
return;
}
dispose();
auto* dq = new DecimalQuantity();
dq->setToDecNumber(numberString, status);
adoptDecimalQuantity(dq);
// Note that we do not hang on to the caller's input string.
// If we are asked for the string, we will regenerate one from fDecimalQuantity.
}
#if 0
//----------------------------------------------------
// console I/O
//----------------------------------------------------
#ifdef _DEBUG
#include <iostream>
using namespace std;
#include <_foundation_unicode/datefmt.h>
#include "unistrm.h"
class FormattableStreamer /* not : public UObject because all methods are static */ {
public:
static void streamOut(ostream& stream, const Formattable& obj);
private:
FormattableStreamer() {} // private - forbid instantiation
};
// This is for debugging purposes only. This will send a displayable
// form of the Formattable object to the output stream.
void
FormattableStreamer::streamOut(ostream& stream, const Formattable& obj)
{
static DateFormat *defDateFormat = 0;
UnicodeString buffer;
switch(obj.getType()) {
case Formattable::kDate :
// Creates a DateFormat instance for formatting the
// Date instance.
if (defDateFormat == 0) {
defDateFormat = DateFormat::createInstance();
}
defDateFormat->format(obj.getDate(), buffer);
stream << buffer;
break;
case Formattable::kDouble :
// Output the double as is.
stream << obj.getDouble() << 'D';
break;
case Formattable::kLong :
// Output the double as is.
stream << obj.getLong() << 'L';
break;
case Formattable::kString:
// Output the double as is. Please see UnicodeString console
// I/O routine for more details.
stream << '"' << obj.getString(buffer) << '"';
break;
case Formattable::kArray:
int32_t i, count;
const Formattable* array;
array = obj.getArray(count);
stream << '[';
// Recursively calling the console I/O routine for each element in the array.
for (i=0; i<count; ++i) {
FormattableStreamer::streamOut(stream, array[i]);
stream << ( (i==(count-1)) ? "" : ", " );
}
stream << ']';
break;
default:
// Not a recognizable Formattable object.
stream << "INVALID_Formattable";
}
stream.flush();
}
#endif
#endif
U_NAMESPACE_END
/* ---- UFormattable implementation ---- */
U_NAMESPACE_USE
U_CAPI UFormattable* U_EXPORT2
ufmt_open(UErrorCode *status) {
if( U_FAILURE(*status) ) {
return nullptr;
}
UFormattable *fmt = (new Formattable())->toUFormattable();
if( fmt == nullptr ) {
*status = U_MEMORY_ALLOCATION_ERROR;
}
return fmt;
}
U_CAPI void U_EXPORT2
ufmt_close(UFormattable *fmt) {
Formattable *obj = Formattable::fromUFormattable(fmt);
delete obj;
}
U_CAPI UFormattableType U_EXPORT2
ufmt_getType(const UFormattable *fmt, UErrorCode *status) {
if(U_FAILURE(*status)) {
return (UFormattableType)UFMT_COUNT;
}
const Formattable *obj = Formattable::fromUFormattable(fmt);
return (UFormattableType)obj->getType();
}
U_CAPI UBool U_EXPORT2
ufmt_isNumeric(const UFormattable *fmt) {
const Formattable *obj = Formattable::fromUFormattable(fmt);
return obj->isNumeric();
}
U_CAPI UDate U_EXPORT2
ufmt_getDate(const UFormattable *fmt, UErrorCode *status) {
const Formattable *obj = Formattable::fromUFormattable(fmt);
return obj->getDate(*status);
}
U_CAPI double U_EXPORT2
ufmt_getDouble(UFormattable *fmt, UErrorCode *status) {
Formattable *obj = Formattable::fromUFormattable(fmt);
return obj->getDouble(*status);
}
U_CAPI int32_t U_EXPORT2
ufmt_getLong(UFormattable *fmt, UErrorCode *status) {
Formattable *obj = Formattable::fromUFormattable(fmt);
return obj->getLong(*status);
}
U_CAPI const void *U_EXPORT2
ufmt_getObject(const UFormattable *fmt, UErrorCode *status) {
const Formattable *obj = Formattable::fromUFormattable(fmt);
const void *ret = obj->getObject();
if( ret==nullptr &&
(obj->getType() != Formattable::kObject) &&
U_SUCCESS( *status )) {
*status = U_INVALID_FORMAT_ERROR;
}
return ret;
}
U_CAPI const char16_t* U_EXPORT2
ufmt_getUChars(UFormattable *fmt, int32_t *len, UErrorCode *status) {
Formattable *obj = Formattable::fromUFormattable(fmt);
// avoid bogosity by checking the type first.
if( obj->getType() != Formattable::kString ) {
if( U_SUCCESS(*status) ){
*status = U_INVALID_FORMAT_ERROR;
}
return nullptr;
}
// This should return a valid string
UnicodeString &str = obj->getString(*status);
if( U_SUCCESS(*status) && len != nullptr ) {
*len = str.length();
}
return str.getTerminatedBuffer();
}
U_CAPI int32_t U_EXPORT2
ufmt_getArrayLength(const UFormattable* fmt, UErrorCode *status) {
const Formattable *obj = Formattable::fromUFormattable(fmt);
int32_t count;
(void)obj->getArray(count, *status);
return count;
}
U_CAPI UFormattable * U_EXPORT2
ufmt_getArrayItemByIndex(UFormattable* fmt, int32_t n, UErrorCode *status) {
Formattable *obj = Formattable::fromUFormattable(fmt);
int32_t count;
(void)obj->getArray(count, *status);
if(U_FAILURE(*status)) {
return nullptr;
} else if(n<0 || n>=count) {
setError(*status, U_INDEX_OUTOFBOUNDS_ERROR);
return nullptr;
} else {
return (*obj)[n].toUFormattable(); // returns non-const Formattable
}
}
U_CAPI const char * U_EXPORT2
ufmt_getDecNumChars(UFormattable *fmt, int32_t *len, UErrorCode *status) {
if(U_FAILURE(*status)) {
return "";
}
Formattable *obj = Formattable::fromUFormattable(fmt);
CharString *charString = obj->internalGetCharString(*status);
if(U_FAILURE(*status)) {
return "";
}
if(charString == nullptr) {
*status = U_MEMORY_ALLOCATION_ERROR;
return "";
} else {
if(len!=nullptr) {
*len = charString->length();
}
return charString->data();
}
}
U_CAPI int64_t U_EXPORT2
ufmt_getInt64(UFormattable *fmt, UErrorCode *status) {
Formattable *obj = Formattable::fromUFormattable(fmt);
return obj->getInt64(*status);
}
#endif /* #if !UCONFIG_NO_FORMATTING */
//eof
|