visibledigits.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. // Copyright (C) 2016 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  3. /*
  4. ******************************************************************************* * Copyright (C) 2015, International Business Machines
  5. * Corporation and others. All Rights Reserved.
  6. *******************************************************************************
  7. * visibledigits.h
  8. *
  9. * created on: 2015jun20
  10. * created by: Travis Keep
  11. */
  12. #ifndef __VISIBLEDIGITS_H__
  13. #define __VISIBLEDIGITS_H__
  14. #include "unicode/utypes.h"
  15. #if !UCONFIG_NO_FORMATTING
  16. #include "unicode/uobject.h"
  17. #include "charstr.h"
  18. #include "digitinterval.h"
  19. U_NAMESPACE_BEGIN
  20. class DigitList;
  21. /**
  22. * VisibleDigits represents the digits visible for formatting.
  23. * Once initialized using a FixedPrecision instance, VisibleDigits instances
  24. * remain unchanged until they are initialized again. A VisibleDigits with
  25. * a numeric value equal to 3.0 could be "3", "3.0", "3.00" or even "003.0"
  26. * depending on settings of the FixedPrecision instance used to initialize it.
  27. */
  28. class U_I18N_API VisibleDigits : public UMemory {
  29. public:
  30. VisibleDigits() : fExponent(0), fFlags(0), fAbsIntValue(0), fAbsIntValueSet(FALSE), fAbsDoubleValue(0.0), fAbsDoubleValueSet(FALSE) { }
  31. UBool isNegative() const;
  32. UBool isNaN() const;
  33. UBool isInfinite() const;
  34. UBool isNaNOrInfinity() const;
  35. /**
  36. * Gets the digit at particular exponent, if number is 987.6, then
  37. * getDigit(2) == 9 and gitDigit(0) == 7 and gitDigit(-1) == 6.
  38. * If isNaN() or isInfinity() return TRUE, then the result of this
  39. * function is undefined.
  40. */
  41. int32_t getDigitByExponent(int32_t digitPos) const;
  42. /**
  43. * Returns the digit interval which indicates the leftmost and rightmost
  44. * position of this instance.
  45. * If isNaN() or isInfinity() return TRUE, then the result of this
  46. * function is undefined.
  47. */
  48. const DigitInterval &getInterval() const { return fInterval; }
  49. /**
  50. * Gets the parameters needed to create a FixedDecimal.
  51. */
  52. void getFixedDecimal(double &source, int64_t &intValue, int64_t &f, int64_t &t, int32_t &v, UBool &hasIntValue) const;
  53. private:
  54. /**
  55. * The digits, least significant first. Both the least and most
  56. * significant digit in this list are non-zero; however, digits in the
  57. * middle may be zero. This field contains values between (char) 0, and
  58. * (char) 9 inclusive.
  59. */
  60. CharString fDigits;
  61. /**
  62. * The range of displayable digits. This field is needed to account for
  63. * any leading and trailing zeros which are not stored in fDigits.
  64. */
  65. DigitInterval fInterval;
  66. /**
  67. * The exponent value of the least significant digit in fDigits. For
  68. * example, fExponent = 2 and fDigits = {7, 8, 5} represents 58700.
  69. */
  70. int32_t fExponent;
  71. /**
  72. * Contains flags such as NaN, Inf, and negative.
  73. */
  74. int32_t fFlags;
  75. /**
  76. * Contains the absolute value of the digits left of the decimal place
  77. * if fAbsIntValueSet is TRUE
  78. */
  79. int64_t fAbsIntValue;
  80. /**
  81. * Indicates whether or not fAbsIntValue is set.
  82. */
  83. UBool fAbsIntValueSet;
  84. /**
  85. * Contains the absolute value of the value this instance represents
  86. * if fAbsDoubleValueSet is TRUE
  87. */
  88. double fAbsDoubleValue;
  89. /**
  90. * Indicates whether or not fAbsDoubleValue is set.
  91. */
  92. UBool fAbsDoubleValueSet;
  93. void setNegative();
  94. void setNaN();
  95. void setInfinite();
  96. void clear();
  97. double computeAbsDoubleValue() const;
  98. UBool isOverMaxDigits() const;
  99. VisibleDigits(const VisibleDigits &);
  100. VisibleDigits &operator=(const VisibleDigits &);
  101. friend class FixedPrecision;
  102. friend class VisibleDigitsWithExponent;
  103. };
  104. /**
  105. * A VisibleDigits with a possible exponent.
  106. */
  107. class U_I18N_API VisibleDigitsWithExponent : public UMemory {
  108. public:
  109. VisibleDigitsWithExponent() : fHasExponent(FALSE) { }
  110. const VisibleDigits &getMantissa() const { return fMantissa; }
  111. const VisibleDigits *getExponent() const {
  112. return fHasExponent ? &fExponent : NULL;
  113. }
  114. void clear() {
  115. fMantissa.clear();
  116. fExponent.clear();
  117. fHasExponent = FALSE;
  118. }
  119. UBool isNegative() const { return fMantissa.isNegative(); }
  120. UBool isNaN() const { return fMantissa.isNaN(); }
  121. UBool isInfinite() const { return fMantissa.isInfinite(); }
  122. private:
  123. VisibleDigitsWithExponent(const VisibleDigitsWithExponent &);
  124. VisibleDigitsWithExponent &operator=(
  125. const VisibleDigitsWithExponent &);
  126. VisibleDigits fMantissa;
  127. VisibleDigits fExponent;
  128. UBool fHasExponent;
  129. friend class ScientificPrecision;
  130. friend class FixedPrecision;
  131. };
  132. U_NAMESPACE_END
  133. #endif /* #if !UCONFIG_NO_FORMATTING */
  134. #endif // __VISIBLEDIGITS_H__