Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(190)

Side by Side Diff: third_party/WebKit/Source/wtf/dtoa.cpp

Issue 2764243002: Move files in wtf/ to platform/wtf/ (Part 9). (Closed)
Patch Set: Rebase. Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « third_party/WebKit/Source/wtf/dtoa.h ('k') | third_party/WebKit/Source/wtf/dtoa/COPYING » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /****************************************************************
2 *
3 * The author of this software is David M. Gay.
4 *
5 * Copyright (c) 1991, 2000, 2001 by Lucent Technologies.
6 * Copyright (C) 2002, 2005, 2006, 2007, 2008, 2010, 2012 Apple Inc.
7 * All rights reserved.
8 *
9 * Permission to use, copy, modify, and distribute this software for any
10 * purpose without fee is hereby granted, provided that this entire notice
11 * is included in all copies of any software which is or includes a copy
12 * or modification of this software and in all copies of the supporting
13 * documentation for such software.
14 *
15 * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED
16 * WARRANTY. IN PARTICULAR, NEITHER THE AUTHOR NOR LUCENT MAKES ANY
17 * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY
18 * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.
19 *
20 ***************************************************************/
21
22 /* Please send bug reports to David M. Gay (dmg at acm dot org,
23 * with " at " changed at "@" and " dot " changed to "."). */
24
25 /* On a machine with IEEE extended-precision registers, it is
26 * necessary to specify double-precision (53-bit) rounding precision
27 * before invoking strtod or dtoa. If the machine uses (the equivalent
28 * of) Intel 80x87 arithmetic, the call
29 * _control87(PC_53, MCW_PC);
30 * does this with many compilers. Whether this or another call is
31 * appropriate depends on the compiler; for this to work, it may be
32 * necessary to #include "float.h" or another system-dependent header
33 * file.
34 */
35
36 #include "wtf/dtoa.h"
37
38 #include "wtf/Vector.h"
39 #include <string.h>
40
41 namespace WTF {
42
43 const char* numberToString(double d, NumberToStringBuffer buffer) {
44 double_conversion::StringBuilder builder(buffer, NumberToStringBufferLength);
45 const double_conversion::DoubleToStringConverter& converter =
46 double_conversion::DoubleToStringConverter::EcmaScriptConverter();
47 converter.ToShortest(d, &builder);
48 return builder.Finalize();
49 }
50
51 static inline const char* formatStringTruncatingTrailingZerosIfNeeded(
52 NumberToStringBuffer buffer,
53 double_conversion::StringBuilder& builder) {
54 size_t length = builder.position();
55
56 // If there is an exponent, stripping trailing zeros would be incorrect.
57 // FIXME: Zeros should be stripped before the 'e'.
58 if (memchr(buffer, 'e', length))
59 return builder.Finalize();
60
61 size_t decimalPointPosition = 0;
62 for (; decimalPointPosition < length; ++decimalPointPosition) {
63 if (buffer[decimalPointPosition] == '.')
64 break;
65 }
66
67 // No decimal seperator found, early exit.
68 if (decimalPointPosition == length)
69 return builder.Finalize();
70
71 size_t truncatedLength = length - 1;
72 for (; truncatedLength > decimalPointPosition; --truncatedLength) {
73 if (buffer[truncatedLength] != '0')
74 break;
75 }
76
77 // No trailing zeros found to strip.
78 if (truncatedLength == length - 1)
79 return builder.Finalize();
80
81 // If we removed all trailing zeros, remove the decimal point as well.
82 if (truncatedLength == decimalPointPosition) {
83 DCHECK_GT(truncatedLength, 0u);
84 --truncatedLength;
85 }
86
87 // Truncate the StringBuilder, and return the final result.
88 builder.SetPosition(truncatedLength + 1);
89 return builder.Finalize();
90 }
91
92 const char* numberToFixedPrecisionString(double d,
93 unsigned significantFigures,
94 NumberToStringBuffer buffer) {
95 // Mimic String::format("%.[precision]g", ...), but use dtoas rounding
96 // facilities.
97 // "g": Signed value printed in f or e format, whichever is more compact for
98 // the given value and precision.
99 // The e format is used only when the exponent of the value is less than -4 or
100 // greater than or equal to the precision argument. Trailing zeros are
101 // truncated, and the decimal point appears only if one or more digits follow
102 // it.
103 // "precision": The precision specifies the maximum number of significant
104 // digits printed.
105 double_conversion::StringBuilder builder(buffer, NumberToStringBufferLength);
106 const double_conversion::DoubleToStringConverter& converter =
107 double_conversion::DoubleToStringConverter::EcmaScriptConverter();
108 converter.ToPrecision(d, significantFigures, &builder);
109 // FIXME: Trailing zeros should never be added in the first place. The
110 // current implementation does not strip when there is an exponent, eg.
111 // 1.50000e+10.
112 return formatStringTruncatingTrailingZerosIfNeeded(buffer, builder);
113 }
114
115 const char* numberToFixedWidthString(double d,
116 unsigned decimalPlaces,
117 NumberToStringBuffer buffer) {
118 // Mimic String::format("%.[precision]f", ...), but use dtoas rounding
119 // facilities.
120 // "f": Signed value having the form [ - ]dddd.dddd, where dddd is one or more
121 // decimal digits. The number of digits before the decimal point depends on
122 // the magnitude of the number, and the number of digits after the decimal
123 // point depends on the requested precision.
124 // "precision": The precision value specifies the number of digits after the
125 // decimal point. If a decimal point appears, at least one digit appears
126 // before it. The value is rounded to the appropriate number of digits.
127 double_conversion::StringBuilder builder(buffer, NumberToStringBufferLength);
128 const double_conversion::DoubleToStringConverter& converter =
129 double_conversion::DoubleToStringConverter::EcmaScriptConverter();
130 converter.ToFixed(d, decimalPlaces, &builder);
131 return builder.Finalize();
132 }
133
134 namespace Internal {
135
136 double parseDoubleFromLongString(const UChar* string,
137 size_t length,
138 size_t& parsedLength) {
139 Vector<LChar> conversionBuffer(length);
140 for (size_t i = 0; i < length; ++i)
141 conversionBuffer[i] = isASCII(string[i]) ? string[i] : 0;
142 return parseDouble(conversionBuffer.data(), length, parsedLength);
143 }
144
145 } // namespace Internal
146
147 } // namespace WTF
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/wtf/dtoa.h ('k') | third_party/WebKit/Source/wtf/dtoa/COPYING » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698