Write a string into output, including a terminating '\0' (for unit testing). Return strlen(output) (for SkWStream::write) The resulting string will be in the form /[-]?([0-9]*.)?[0-9]+/ and sscanf(output, "%f", &x) will return the original value iff the value is finite. This function accepts all possible input values.
Motivation: "PDF does not support [numbers] in exponential format
(such as 6.02e23)." Otherwise, this function would rely on a sprintf-type function from the standard library.
73 {
74
75
76
77
79
80
81
83
84
85
86
87
88
89
90
91
92
93 char* output_ptr = &
output[0];
95
96
97
98
99
100
101 if (value == INFINITY) {
103 }
104 if (value == -INFINITY) {
106 }
107 if (!std::isfinite(value) ||
value == 0.0f) {
108
109
110 *output_ptr++ = '0';
111 *output_ptr = '\0';
112 return static_cast<unsigned>(output_ptr -
output);
113 }
114 if (value < 0.0) {
115 *output_ptr++ = '-';
117 }
119
120 int binaryExponent;
121 (void)std::frexp(value, &binaryExponent);
122 static const double kLog2 = 0.3010299956639812;
123 int decimalExponent = static_cast<int>(std::floor(kLog2 * binaryExponent));
124 int decimalShift = decimalExponent - 8;
125 double power =
pow10(-decimalShift);
126 SkASSERT(value * power <= (
double)INT_MAX);
127 int d =
static_cast<int>(
value * power + 0.5);
128
131
132 decimalShift = decimalExponent - 7;
133
134
135 d =
static_cast<int>(
value * (power * 0.1) + 0.5);
137 }
138 while (
d % 10 == 0) {
140 ++decimalShift;
141 }
143
145 int bufferIndex = 0;
146 do {
147 buffer[bufferIndex++] =
d % 10;
151 if (decimalShift >= 0) {
152 do {
153 --bufferIndex;
154 *output_ptr++ =
'0' +
buffer[bufferIndex];
155 } while (bufferIndex);
156 for (int i = 0; i < decimalShift; ++i) {
157 *output_ptr++ = '0';
158 }
159 } else {
160 int placesBeforeDecimal = bufferIndex + decimalShift;
161 if (placesBeforeDecimal > 0) {
162 while (placesBeforeDecimal-- > 0) {
163 --bufferIndex;
164 *output_ptr++ =
'0' +
buffer[bufferIndex];
165 }
166 *output_ptr++ = '.';
167 } else {
168 *output_ptr++ = '.';
169 int placesAfterDecimal = -placesBeforeDecimal;
170 while (placesAfterDecimal-- > 0) {
171 *output_ptr++ = '0';
172 }
173 }
174 while (bufferIndex > 0) {
175 --bufferIndex;
176 *output_ptr++ =
'0' +
buffer[bufferIndex];
177 if (output_ptr ==
end) {
178 break;
179
180
181 }
182 }
183 }
185 *output_ptr = '\0';
186 return static_cast<unsigned>(output_ptr -
output);
187}
static double pow10(int e)
constexpr unsigned kMaximumSkFloatToDecimalLength
VULKAN_HPP_DEFAULT_DISPATCH_LOADER_DYNAMIC_STORAGE auto & d
static const uint8_t buffer[]