1
// Copyright 2011 the V8 project authors. All rights reserved.
2
// Redistribution and use in source and binary forms, with or without
3
// modification, are permitted provided that the following conditions are
6
// * Redistributions of source code must retain the above copyright
7
// notice, this list of conditions and the following disclaimer.
8
// * Redistributions in binary form must reproduce the above
9
// copyright notice, this list of conditions and the following
10
// disclaimer in the documentation and/or other materials provided
11
// with the distribution.
12
// * Neither the name of Google Inc. nor the names of its
13
// contributors may be used to endorse or promote products derived
14
// from this software without specific prior written permission.
16
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32
#include "conversions-inl.h"
41
double StringToDouble(UnicodeCache* unicode_cache,
42
const char* str, int flags, double empty_string_val) {
43
const char* end = str + StrLength(str);
44
return InternalStringToDouble(unicode_cache, str, end, flags,
49
double StringToDouble(UnicodeCache* unicode_cache,
50
Vector<const char> str,
52
double empty_string_val) {
53
const char* end = str.start() + str.length();
54
return InternalStringToDouble(unicode_cache, str.start(), end, flags,
58
double StringToDouble(UnicodeCache* unicode_cache,
59
Vector<const uc16> str,
61
double empty_string_val) {
62
const uc16* end = str.start() + str.length();
63
return InternalStringToDouble(unicode_cache, str.start(), end, flags,
68
const char* DoubleToCString(double v, Vector<char> buffer) {
69
switch (fpclassify(v)) {
70
case FP_NAN: return "NaN";
71
case FP_INFINITE: return (v < 0.0 ? "-Infinity" : "Infinity");
72
case FP_ZERO: return "0";
74
SimpleStringBuilder builder(buffer.start(), buffer.length());
77
const int kV8DtoaBufferCapacity = kBase10MaximalLength + 1;
78
char decimal_rep[kV8DtoaBufferCapacity];
81
DoubleToAscii(v, DTOA_SHORTEST, 0,
82
Vector<char>(decimal_rep, kV8DtoaBufferCapacity),
83
&sign, &length, &decimal_point);
85
if (sign) builder.AddCharacter('-');
87
if (length <= decimal_point && decimal_point <= 21) {
88
// ECMA-262 section 9.8.1 step 6.
89
builder.AddString(decimal_rep);
90
builder.AddPadding('0', decimal_point - length);
92
} else if (0 < decimal_point && decimal_point <= 21) {
93
// ECMA-262 section 9.8.1 step 7.
94
builder.AddSubstring(decimal_rep, decimal_point);
95
builder.AddCharacter('.');
96
builder.AddString(decimal_rep + decimal_point);
98
} else if (decimal_point <= 0 && decimal_point > -6) {
99
// ECMA-262 section 9.8.1 step 8.
100
builder.AddString("0.");
101
builder.AddPadding('0', -decimal_point);
102
builder.AddString(decimal_rep);
105
// ECMA-262 section 9.8.1 step 9 and 10 combined.
106
builder.AddCharacter(decimal_rep[0]);
108
builder.AddCharacter('.');
109
builder.AddString(decimal_rep + 1);
111
builder.AddCharacter('e');
112
builder.AddCharacter((decimal_point >= 0) ? '+' : '-');
113
int exponent = decimal_point - 1;
114
if (exponent < 0) exponent = -exponent;
115
builder.AddDecimalInteger(exponent);
117
return builder.Finalize();
123
const char* IntToCString(int n, Vector<char> buffer) {
124
bool negative = false;
126
// We must not negate the most negative int.
127
if (n == kMinInt) return DoubleToCString(n, buffer);
131
// Build the string backwards from the least significant digit.
132
int i = buffer.length();
135
buffer[--i] = '0' + (n % 10);
138
if (negative) buffer[--i] = '-';
139
return buffer.start() + i;
143
char* DoubleToFixedCString(double value, int f) {
144
const int kMaxDigitsBeforePoint = 21;
145
const double kFirstNonFixed = 1e21;
146
const int kMaxDigitsAfterPoint = 20;
148
ASSERT(f <= kMaxDigitsAfterPoint);
150
bool negative = false;
151
double abs_value = value;
157
// If abs_value has more than kMaxDigitsBeforePoint digits before the point
158
// use the non-fixed conversion routine.
159
if (abs_value >= kFirstNonFixed) {
161
Vector<char> buffer(arr, ARRAY_SIZE(arr));
162
return StrDup(DoubleToCString(value, buffer));
165
// Find a sufficiently precise decimal representation of n.
168
// Add space for the '\0' byte.
169
const int kDecimalRepCapacity =
170
kMaxDigitsBeforePoint + kMaxDigitsAfterPoint + 1;
171
char decimal_rep[kDecimalRepCapacity];
172
int decimal_rep_length;
173
DoubleToAscii(value, DTOA_FIXED, f,
174
Vector<char>(decimal_rep, kDecimalRepCapacity),
175
&sign, &decimal_rep_length, &decimal_point);
177
// Create a representation that is padded with zeros if needed.
178
int zero_prefix_length = 0;
179
int zero_postfix_length = 0;
181
if (decimal_point <= 0) {
182
zero_prefix_length = -decimal_point + 1;
186
if (zero_prefix_length + decimal_rep_length < decimal_point + f) {
187
zero_postfix_length = decimal_point + f - decimal_rep_length -
191
unsigned rep_length =
192
zero_prefix_length + decimal_rep_length + zero_postfix_length;
193
SimpleStringBuilder rep_builder(rep_length + 1);
194
rep_builder.AddPadding('0', zero_prefix_length);
195
rep_builder.AddString(decimal_rep);
196
rep_builder.AddPadding('0', zero_postfix_length);
197
char* rep = rep_builder.Finalize();
199
// Create the result string by appending a minus and putting in a
200
// decimal point if needed.
201
unsigned result_size = decimal_point + f + 2;
202
SimpleStringBuilder builder(result_size + 1);
203
if (negative) builder.AddCharacter('-');
204
builder.AddSubstring(rep, decimal_point);
206
builder.AddCharacter('.');
207
builder.AddSubstring(rep + decimal_point, f);
210
return builder.Finalize();
214
static char* CreateExponentialRepresentation(char* decimal_rep,
217
int significant_digits) {
218
bool negative_exponent = false;
220
negative_exponent = true;
221
exponent = -exponent;
224
// Leave room in the result for appending a minus, for a period, the
225
// letter 'e', a minus or a plus depending on the exponent, and a
226
// three digit exponent.
227
unsigned result_size = significant_digits + 7;
228
SimpleStringBuilder builder(result_size + 1);
230
if (negative) builder.AddCharacter('-');
231
builder.AddCharacter(decimal_rep[0]);
232
if (significant_digits != 1) {
233
builder.AddCharacter('.');
234
builder.AddString(decimal_rep + 1);
235
int rep_length = StrLength(decimal_rep);
236
builder.AddPadding('0', significant_digits - rep_length);
239
builder.AddCharacter('e');
240
builder.AddCharacter(negative_exponent ? '-' : '+');
241
builder.AddDecimalInteger(exponent);
242
return builder.Finalize();
247
char* DoubleToExponentialCString(double value, int f) {
248
const int kMaxDigitsAfterPoint = 20;
249
// f might be -1 to signal that f was undefined in JavaScript.
250
ASSERT(f >= -1 && f <= kMaxDigitsAfterPoint);
252
bool negative = false;
258
// Find a sufficiently precise decimal representation of n.
261
// f corresponds to the digits after the point. There is always one digit
262
// before the point. The number of requested_digits equals hence f + 1.
263
// And we have to add one character for the null-terminator.
264
const int kV8DtoaBufferCapacity = kMaxDigitsAfterPoint + 1 + 1;
265
// Make sure that the buffer is big enough, even if we fall back to the
266
// shortest representation (which happens when f equals -1).
267
ASSERT(kBase10MaximalLength <= kMaxDigitsAfterPoint + 1);
268
char decimal_rep[kV8DtoaBufferCapacity];
269
int decimal_rep_length;
272
DoubleToAscii(value, DTOA_SHORTEST, 0,
273
Vector<char>(decimal_rep, kV8DtoaBufferCapacity),
274
&sign, &decimal_rep_length, &decimal_point);
275
f = decimal_rep_length - 1;
277
DoubleToAscii(value, DTOA_PRECISION, f + 1,
278
Vector<char>(decimal_rep, kV8DtoaBufferCapacity),
279
&sign, &decimal_rep_length, &decimal_point);
281
ASSERT(decimal_rep_length > 0);
282
ASSERT(decimal_rep_length <= f + 1);
284
int exponent = decimal_point - 1;
286
CreateExponentialRepresentation(decimal_rep, exponent, negative, f+1);
292
char* DoubleToPrecisionCString(double value, int p) {
293
const int kMinimalDigits = 1;
294
const int kMaximalDigits = 21;
295
ASSERT(p >= kMinimalDigits && p <= kMaximalDigits);
298
bool negative = false;
304
// Find a sufficiently precise decimal representation of n.
307
// Add one for the terminating null character.
308
const int kV8DtoaBufferCapacity = kMaximalDigits + 1;
309
char decimal_rep[kV8DtoaBufferCapacity];
310
int decimal_rep_length;
312
DoubleToAscii(value, DTOA_PRECISION, p,
313
Vector<char>(decimal_rep, kV8DtoaBufferCapacity),
314
&sign, &decimal_rep_length, &decimal_point);
315
ASSERT(decimal_rep_length <= p);
317
int exponent = decimal_point - 1;
321
if (exponent < -6 || exponent >= p) {
323
CreateExponentialRepresentation(decimal_rep, exponent, negative, p);
325
// Use fixed notation.
327
// Leave room in the result for appending a minus, a period and in
328
// the case where decimal_point is not positive for a zero in
329
// front of the period.
330
unsigned result_size = (decimal_point <= 0)
331
? -decimal_point + p + 3
333
SimpleStringBuilder builder(result_size + 1);
334
if (negative) builder.AddCharacter('-');
335
if (decimal_point <= 0) {
336
builder.AddString("0.");
337
builder.AddPadding('0', -decimal_point);
338
builder.AddString(decimal_rep);
339
builder.AddPadding('0', p - decimal_rep_length);
341
const int m = Min(decimal_rep_length, decimal_point);
342
builder.AddSubstring(decimal_rep, m);
343
builder.AddPadding('0', decimal_point - decimal_rep_length);
344
if (decimal_point < p) {
345
builder.AddCharacter('.');
346
const int extra = negative ? 2 : 1;
347
if (decimal_rep_length > decimal_point) {
348
const int len = StrLength(decimal_rep + decimal_point);
349
const int n = Min(len, p - (builder.position() - extra));
350
builder.AddSubstring(decimal_rep + decimal_point, n);
352
builder.AddPadding('0', extra + (p - builder.position()));
355
result = builder.Finalize();
362
char* DoubleToRadixCString(double value, int radix) {
363
ASSERT(radix >= 2 && radix <= 36);
365
// Character array used for conversion.
366
static const char chars[] = "0123456789abcdefghijklmnopqrstuvwxyz";
368
// Buffer for the integer part of the result. 1024 chars is enough
369
// for max integer value in radix 2. We need room for a sign too.
370
static const int kBufferSize = 1100;
371
char integer_buffer[kBufferSize];
372
integer_buffer[kBufferSize - 1] = '\0';
374
// Buffer for the decimal part of the result. We only generate up
375
// to kBufferSize - 1 chars for the decimal part.
376
char decimal_buffer[kBufferSize];
377
decimal_buffer[kBufferSize - 1] = '\0';
379
// Make sure the value is positive.
380
bool is_negative = value < 0.0;
381
if (is_negative) value = -value;
383
// Get the integer part and the decimal part.
384
double integer_part = floor(value);
385
double decimal_part = value - integer_part;
387
// Convert the integer part starting from the back. Always generate
388
// at least one digit.
389
int integer_pos = kBufferSize - 2;
391
integer_buffer[integer_pos--] =
392
chars[static_cast<int>(fmod(integer_part, radix))];
393
integer_part /= radix;
394
} while (integer_part >= 1.0);
396
ASSERT(integer_pos > 0);
397
// Add sign if needed.
398
if (is_negative) integer_buffer[integer_pos--] = '-';
400
// Convert the decimal part. Repeatedly multiply by the radix to
401
// generate the next char. Never generate more than kBufferSize - 1
404
// TODO(1093998): We will often generate a full decimal_buffer of
405
// chars because hitting zero will often not happen. The right
406
// solution would be to continue until the string representation can
407
// be read back and yield the original value. To implement this
408
// efficiently, we probably have to modify dtoa.
410
while ((decimal_part > 0.0) && (decimal_pos < kBufferSize - 1)) {
411
decimal_part *= radix;
412
decimal_buffer[decimal_pos++] =
413
chars[static_cast<int>(floor(decimal_part))];
414
decimal_part -= floor(decimal_part);
416
decimal_buffer[decimal_pos] = '\0';
418
// Compute the result size.
419
int integer_part_size = kBufferSize - 2 - integer_pos;
420
// Make room for zero termination.
421
unsigned result_size = integer_part_size + decimal_pos;
422
// If the number has a decimal part, leave room for the period.
423
if (decimal_pos > 0) result_size++;
424
// Allocate result and fill in the parts.
425
SimpleStringBuilder builder(result_size + 1);
426
builder.AddSubstring(integer_buffer + integer_pos + 1, integer_part_size);
427
if (decimal_pos > 0) builder.AddCharacter('.');
428
builder.AddSubstring(decimal_buffer, decimal_pos);
429
return builder.Finalize();
432
} } // namespace v8::internal