]> git.saurik.com Git - apple/icu.git/blob - icuSources/i18n/visibledigits.h
ICU-62141.0.1.tar.gz
[apple/icu.git] / icuSources / i18n / visibledigits.h
1 // © 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
13 #ifndef __VISIBLEDIGITS_H__
14 #define __VISIBLEDIGITS_H__
15
16 #include "unicode/utypes.h"
17
18 #if !UCONFIG_NO_FORMATTING
19
20 #include "unicode/uobject.h"
21
22 #include "charstr.h"
23 #include "digitinterval.h"
24
25 U_NAMESPACE_BEGIN
26
27 class DigitList;
28
29 /**
30 * VisibleDigits represents the digits visible for formatting.
31 * Once initialized using a FixedPrecision instance, VisibleDigits instances
32 * remain unchanged until they are initialized again. A VisibleDigits with
33 * a numeric value equal to 3.0 could be "3", "3.0", "3.00" or even "003.0"
34 * depending on settings of the FixedPrecision instance used to initialize it.
35 */
36 class U_I18N_API VisibleDigits : public UMemory {
37 public:
38 VisibleDigits() : fExponent(0), fFlags(0), fAbsIntValue(0), fAbsIntValueSet(FALSE), fAbsDoubleValue(0.0), fAbsDoubleValueSet(FALSE), fFormatFullPrecision(TRUE) { }
39
40 UBool isNegative() const;
41 UBool isNaN() const;
42 UBool isInfinite() const;
43 UBool isNaNOrInfinity() const;
44 UBool formatFullPrecision() const; // Apple
45
46 /**
47 * Gets the digit at particular exponent, if number is 987.6, then
48 * getDigit(2) == 9 and gitDigit(0) == 7 and gitDigit(-1) == 6.
49 * If isNaN() or isInfinity() return TRUE, then the result of this
50 * function is undefined.
51 */
52 int32_t getDigitByExponent(int32_t digitPos) const;
53
54 /**
55 * Returns the digit interval which indicates the leftmost and rightmost
56 * position of this instance.
57 * If isNaN() or isInfinity() return TRUE, then the result of this
58 * function is undefined.
59 */
60 const DigitInterval &getInterval() const { return fInterval; }
61
62 /**
63 * Gets the parameters needed to create a FixedDecimal.
64 */
65 void getFixedDecimal(double &source, int64_t &intValue, int64_t &f, int64_t &t, int32_t &v, UBool &hasIntValue) const;
66
67
68 private:
69 /**
70 * The digits, least significant first. Both the least and most
71 * significant digit in this list are non-zero; however, digits in the
72 * middle may be zero. This field contains values between (char) 0, and
73 * (char) 9 inclusive.
74 */
75 CharString fDigits;
76
77 /**
78 * The range of displayable digits. This field is needed to account for
79 * any leading and trailing zeros which are not stored in fDigits.
80 */
81 DigitInterval fInterval;
82
83 /**
84 * The exponent value of the least significant digit in fDigits. For
85 * example, fExponent = 2 and fDigits = {7, 8, 5} represents 58700.
86 */
87 int32_t fExponent;
88
89 /**
90 * Contains flags such as NaN, Inf, and negative.
91 */
92 int32_t fFlags;
93
94 /**
95 * Contains the absolute value of the digits left of the decimal place
96 * if fAbsIntValueSet is TRUE
97 */
98 int64_t fAbsIntValue;
99
100 /**
101 * Indicates whether or not fAbsIntValue is set.
102 */
103 UBool fAbsIntValueSet;
104
105 /**
106 * Contains the absolute value of the value this instance represents
107 * if fAbsDoubleValueSet is TRUE
108 */
109 double fAbsDoubleValue;
110
111 /**
112 * Indicates whether or not fAbsDoubleValue is set.
113 */
114 UBool fAbsDoubleValueSet;
115
116 // Flag to cap double conversion precision at DBL_DIG digits (Apple specific)
117 UBool fFormatFullPrecision;
118 void setFormatFullPrecision(UBool formatFullPrecision);
119
120 void setNegative();
121 void setNaN();
122 void setInfinite();
123 void clear();
124 double computeAbsDoubleValue() const;
125 UBool isOverMaxDigits() const;
126
127 VisibleDigits(const VisibleDigits &);
128 VisibleDigits &operator=(const VisibleDigits &);
129
130 friend class FixedPrecision;
131 friend class VisibleDigitsWithExponent;
132 };
133
134 /**
135 * A VisibleDigits with a possible exponent.
136 */
137 class U_I18N_API VisibleDigitsWithExponent : public UMemory {
138 public:
139 VisibleDigitsWithExponent() : fHasExponent(FALSE) { }
140 const VisibleDigits &getMantissa() const { return fMantissa; }
141 const VisibleDigits *getExponent() const {
142 return fHasExponent ? &fExponent : NULL;
143 }
144 void clear() {
145 fMantissa.clear();
146 fExponent.clear();
147 fHasExponent = FALSE;
148 }
149 UBool isNegative() const { return fMantissa.isNegative(); }
150 UBool isNaN() const { return fMantissa.isNaN(); }
151 UBool isInfinite() const { return fMantissa.isInfinite(); }
152 void setFormatFullPrecision(UBool formatFullPrecision) { fMantissa.setFormatFullPrecision(formatFullPrecision); }
153 private:
154 VisibleDigitsWithExponent(const VisibleDigitsWithExponent &);
155 VisibleDigitsWithExponent &operator=(
156 const VisibleDigitsWithExponent &);
157 VisibleDigits fMantissa;
158 VisibleDigits fExponent;
159 UBool fHasExponent;
160
161 friend class ScientificPrecision;
162 friend class FixedPrecision;
163 };
164
165
166 U_NAMESPACE_END
167 #endif /* #if !UCONFIG_NO_FORMATTING */
168 #endif // __VISIBLEDIGITS_H__