Tue, 06 Jan 2015 21:39:09 +0100
Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.
michael@0 | 1 | // Copyright 2012 the V8 project authors. All rights reserved. |
michael@0 | 2 | // Redistribution and use in source and binary forms, with or without |
michael@0 | 3 | // modification, are permitted provided that the following conditions are |
michael@0 | 4 | // met: |
michael@0 | 5 | // |
michael@0 | 6 | // * Redistributions of source code must retain the above copyright |
michael@0 | 7 | // notice, this list of conditions and the following disclaimer. |
michael@0 | 8 | // * Redistributions in binary form must reproduce the above |
michael@0 | 9 | // copyright notice, this list of conditions and the following |
michael@0 | 10 | // disclaimer in the documentation and/or other materials provided |
michael@0 | 11 | // with the distribution. |
michael@0 | 12 | // * Neither the name of Google Inc. nor the names of its |
michael@0 | 13 | // contributors may be used to endorse or promote products derived |
michael@0 | 14 | // from this software without specific prior written permission. |
michael@0 | 15 | // |
michael@0 | 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
michael@0 | 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
michael@0 | 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
michael@0 | 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
michael@0 | 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
michael@0 | 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
michael@0 | 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
michael@0 | 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
michael@0 | 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
michael@0 | 25 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
michael@0 | 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
michael@0 | 27 | |
michael@0 | 28 | #ifndef DOUBLE_CONVERSION_DOUBLE_CONVERSION_H_ |
michael@0 | 29 | #define DOUBLE_CONVERSION_DOUBLE_CONVERSION_H_ |
michael@0 | 30 | |
michael@0 | 31 | #include "mozilla/Types.h" |
michael@0 | 32 | #include "utils.h" |
michael@0 | 33 | |
michael@0 | 34 | namespace double_conversion { |
michael@0 | 35 | |
michael@0 | 36 | class DoubleToStringConverter { |
michael@0 | 37 | public: |
michael@0 | 38 | // When calling ToFixed with a double > 10^kMaxFixedDigitsBeforePoint |
michael@0 | 39 | // or a requested_digits parameter > kMaxFixedDigitsAfterPoint then the |
michael@0 | 40 | // function returns false. |
michael@0 | 41 | static const int kMaxFixedDigitsBeforePoint = 60; |
michael@0 | 42 | static const int kMaxFixedDigitsAfterPoint = 60; |
michael@0 | 43 | |
michael@0 | 44 | // When calling ToExponential with a requested_digits |
michael@0 | 45 | // parameter > kMaxExponentialDigits then the function returns false. |
michael@0 | 46 | static const int kMaxExponentialDigits = 120; |
michael@0 | 47 | |
michael@0 | 48 | // When calling ToPrecision with a requested_digits |
michael@0 | 49 | // parameter < kMinPrecisionDigits or requested_digits > kMaxPrecisionDigits |
michael@0 | 50 | // then the function returns false. |
michael@0 | 51 | static const int kMinPrecisionDigits = 1; |
michael@0 | 52 | static const int kMaxPrecisionDigits = 120; |
michael@0 | 53 | |
michael@0 | 54 | enum Flags { |
michael@0 | 55 | NO_FLAGS = 0, |
michael@0 | 56 | EMIT_POSITIVE_EXPONENT_SIGN = 1, |
michael@0 | 57 | EMIT_TRAILING_DECIMAL_POINT = 2, |
michael@0 | 58 | EMIT_TRAILING_ZERO_AFTER_POINT = 4, |
michael@0 | 59 | UNIQUE_ZERO = 8 |
michael@0 | 60 | }; |
michael@0 | 61 | |
michael@0 | 62 | // Flags should be a bit-or combination of the possible Flags-enum. |
michael@0 | 63 | // - NO_FLAGS: no special flags. |
michael@0 | 64 | // - EMIT_POSITIVE_EXPONENT_SIGN: when the number is converted into exponent |
michael@0 | 65 | // form, emits a '+' for positive exponents. Example: 1.2e+2. |
michael@0 | 66 | // - EMIT_TRAILING_DECIMAL_POINT: when the input number is an integer and is |
michael@0 | 67 | // converted into decimal format then a trailing decimal point is appended. |
michael@0 | 68 | // Example: 2345.0 is converted to "2345.". |
michael@0 | 69 | // - EMIT_TRAILING_ZERO_AFTER_POINT: in addition to a trailing decimal point |
michael@0 | 70 | // emits a trailing '0'-character. This flag requires the |
michael@0 | 71 | // EXMIT_TRAILING_DECIMAL_POINT flag. |
michael@0 | 72 | // Example: 2345.0 is converted to "2345.0". |
michael@0 | 73 | // - UNIQUE_ZERO: "-0.0" is converted to "0.0". |
michael@0 | 74 | // |
michael@0 | 75 | // Infinity symbol and nan_symbol provide the string representation for these |
michael@0 | 76 | // special values. If the string is NULL and the special value is encountered |
michael@0 | 77 | // then the conversion functions return false. |
michael@0 | 78 | // |
michael@0 | 79 | // The exponent_character is used in exponential representations. It is |
michael@0 | 80 | // usually 'e' or 'E'. |
michael@0 | 81 | // |
michael@0 | 82 | // When converting to the shortest representation the converter will |
michael@0 | 83 | // represent input numbers in decimal format if they are in the interval |
michael@0 | 84 | // [10^decimal_in_shortest_low; 10^decimal_in_shortest_high[ |
michael@0 | 85 | // (lower boundary included, greater boundary excluded). |
michael@0 | 86 | // Example: with decimal_in_shortest_low = -6 and |
michael@0 | 87 | // decimal_in_shortest_high = 21: |
michael@0 | 88 | // ToShortest(0.000001) -> "0.000001" |
michael@0 | 89 | // ToShortest(0.0000001) -> "1e-7" |
michael@0 | 90 | // ToShortest(111111111111111111111.0) -> "111111111111111110000" |
michael@0 | 91 | // ToShortest(100000000000000000000.0) -> "100000000000000000000" |
michael@0 | 92 | // ToShortest(1111111111111111111111.0) -> "1.1111111111111111e+21" |
michael@0 | 93 | // |
michael@0 | 94 | // When converting to precision mode the converter may add |
michael@0 | 95 | // max_leading_padding_zeroes before returning the number in exponential |
michael@0 | 96 | // format. |
michael@0 | 97 | // Example with max_leading_padding_zeroes_in_precision_mode = 6. |
michael@0 | 98 | // ToPrecision(0.0000012345, 2) -> "0.0000012" |
michael@0 | 99 | // ToPrecision(0.00000012345, 2) -> "1.2e-7" |
michael@0 | 100 | // Similarily the converter may add up to |
michael@0 | 101 | // max_trailing_padding_zeroes_in_precision_mode in precision mode to avoid |
michael@0 | 102 | // returning an exponential representation. A zero added by the |
michael@0 | 103 | // EMIT_TRAILING_ZERO_AFTER_POINT flag is counted for this limit. |
michael@0 | 104 | // Examples for max_trailing_padding_zeroes_in_precision_mode = 1: |
michael@0 | 105 | // ToPrecision(230.0, 2) -> "230" |
michael@0 | 106 | // ToPrecision(230.0, 2) -> "230." with EMIT_TRAILING_DECIMAL_POINT. |
michael@0 | 107 | // ToPrecision(230.0, 2) -> "2.3e2" with EMIT_TRAILING_ZERO_AFTER_POINT. |
michael@0 | 108 | DoubleToStringConverter(int flags, |
michael@0 | 109 | const char* infinity_symbol, |
michael@0 | 110 | const char* nan_symbol, |
michael@0 | 111 | char exponent_character, |
michael@0 | 112 | int decimal_in_shortest_low, |
michael@0 | 113 | int decimal_in_shortest_high, |
michael@0 | 114 | int max_leading_padding_zeroes_in_precision_mode, |
michael@0 | 115 | int max_trailing_padding_zeroes_in_precision_mode) |
michael@0 | 116 | : flags_(flags), |
michael@0 | 117 | infinity_symbol_(infinity_symbol), |
michael@0 | 118 | nan_symbol_(nan_symbol), |
michael@0 | 119 | exponent_character_(exponent_character), |
michael@0 | 120 | decimal_in_shortest_low_(decimal_in_shortest_low), |
michael@0 | 121 | decimal_in_shortest_high_(decimal_in_shortest_high), |
michael@0 | 122 | max_leading_padding_zeroes_in_precision_mode_( |
michael@0 | 123 | max_leading_padding_zeroes_in_precision_mode), |
michael@0 | 124 | max_trailing_padding_zeroes_in_precision_mode_( |
michael@0 | 125 | max_trailing_padding_zeroes_in_precision_mode) { |
michael@0 | 126 | // When 'trailing zero after the point' is set, then 'trailing point' |
michael@0 | 127 | // must be set too. |
michael@0 | 128 | ASSERT(((flags & EMIT_TRAILING_DECIMAL_POINT) != 0) || |
michael@0 | 129 | !((flags & EMIT_TRAILING_ZERO_AFTER_POINT) != 0)); |
michael@0 | 130 | } |
michael@0 | 131 | |
michael@0 | 132 | // Returns a converter following the EcmaScript specification. |
michael@0 | 133 | static MFBT_API const DoubleToStringConverter& EcmaScriptConverter(); |
michael@0 | 134 | |
michael@0 | 135 | // Computes the shortest string of digits that correctly represent the input |
michael@0 | 136 | // number. Depending on decimal_in_shortest_low and decimal_in_shortest_high |
michael@0 | 137 | // (see constructor) it then either returns a decimal representation, or an |
michael@0 | 138 | // exponential representation. |
michael@0 | 139 | // Example with decimal_in_shortest_low = -6, |
michael@0 | 140 | // decimal_in_shortest_high = 21, |
michael@0 | 141 | // EMIT_POSITIVE_EXPONENT_SIGN activated, and |
michael@0 | 142 | // EMIT_TRAILING_DECIMAL_POINT deactived: |
michael@0 | 143 | // ToShortest(0.000001) -> "0.000001" |
michael@0 | 144 | // ToShortest(0.0000001) -> "1e-7" |
michael@0 | 145 | // ToShortest(111111111111111111111.0) -> "111111111111111110000" |
michael@0 | 146 | // ToShortest(100000000000000000000.0) -> "100000000000000000000" |
michael@0 | 147 | // ToShortest(1111111111111111111111.0) -> "1.1111111111111111e+21" |
michael@0 | 148 | // |
michael@0 | 149 | // Note: the conversion may round the output if the returned string |
michael@0 | 150 | // is accurate enough to uniquely identify the input-number. |
michael@0 | 151 | // For example the most precise representation of the double 9e59 equals |
michael@0 | 152 | // "899999999999999918767229449717619953810131273674690656206848", but |
michael@0 | 153 | // the converter will return the shorter (but still correct) "9e59". |
michael@0 | 154 | // |
michael@0 | 155 | // Returns true if the conversion succeeds. The conversion always succeeds |
michael@0 | 156 | // except when the input value is special and no infinity_symbol or |
michael@0 | 157 | // nan_symbol has been given to the constructor. |
michael@0 | 158 | bool ToShortest(double value, StringBuilder* result_builder) const { |
michael@0 | 159 | return ToShortestIeeeNumber(value, result_builder, SHORTEST); |
michael@0 | 160 | } |
michael@0 | 161 | |
michael@0 | 162 | // Same as ToShortest, but for single-precision floats. |
michael@0 | 163 | bool ToShortestSingle(float value, StringBuilder* result_builder) const { |
michael@0 | 164 | return ToShortestIeeeNumber(value, result_builder, SHORTEST_SINGLE); |
michael@0 | 165 | } |
michael@0 | 166 | |
michael@0 | 167 | |
michael@0 | 168 | // Computes a decimal representation with a fixed number of digits after the |
michael@0 | 169 | // decimal point. The last emitted digit is rounded. |
michael@0 | 170 | // |
michael@0 | 171 | // Examples: |
michael@0 | 172 | // ToFixed(3.12, 1) -> "3.1" |
michael@0 | 173 | // ToFixed(3.1415, 3) -> "3.142" |
michael@0 | 174 | // ToFixed(1234.56789, 4) -> "1234.5679" |
michael@0 | 175 | // ToFixed(1.23, 5) -> "1.23000" |
michael@0 | 176 | // ToFixed(0.1, 4) -> "0.1000" |
michael@0 | 177 | // ToFixed(1e30, 2) -> "1000000000000000019884624838656.00" |
michael@0 | 178 | // ToFixed(0.1, 30) -> "0.100000000000000005551115123126" |
michael@0 | 179 | // ToFixed(0.1, 17) -> "0.10000000000000001" |
michael@0 | 180 | // |
michael@0 | 181 | // If requested_digits equals 0, then the tail of the result depends on |
michael@0 | 182 | // the EMIT_TRAILING_DECIMAL_POINT and EMIT_TRAILING_ZERO_AFTER_POINT. |
michael@0 | 183 | // Examples, for requested_digits == 0, |
michael@0 | 184 | // let EMIT_TRAILING_DECIMAL_POINT and EMIT_TRAILING_ZERO_AFTER_POINT be |
michael@0 | 185 | // - false and false: then 123.45 -> 123 |
michael@0 | 186 | // 0.678 -> 1 |
michael@0 | 187 | // - true and false: then 123.45 -> 123. |
michael@0 | 188 | // 0.678 -> 1. |
michael@0 | 189 | // - true and true: then 123.45 -> 123.0 |
michael@0 | 190 | // 0.678 -> 1.0 |
michael@0 | 191 | // |
michael@0 | 192 | // Returns true if the conversion succeeds. The conversion always succeeds |
michael@0 | 193 | // except for the following cases: |
michael@0 | 194 | // - the input value is special and no infinity_symbol or nan_symbol has |
michael@0 | 195 | // been provided to the constructor, |
michael@0 | 196 | // - 'value' > 10^kMaxFixedDigitsBeforePoint, or |
michael@0 | 197 | // - 'requested_digits' > kMaxFixedDigitsAfterPoint. |
michael@0 | 198 | // The last two conditions imply that the result will never contain more than |
michael@0 | 199 | // 1 + kMaxFixedDigitsBeforePoint + 1 + kMaxFixedDigitsAfterPoint characters |
michael@0 | 200 | // (one additional character for the sign, and one for the decimal point). |
michael@0 | 201 | MFBT_API bool ToFixed(double value, |
michael@0 | 202 | int requested_digits, |
michael@0 | 203 | StringBuilder* result_builder) const; |
michael@0 | 204 | |
michael@0 | 205 | // Computes a representation in exponential format with requested_digits |
michael@0 | 206 | // after the decimal point. The last emitted digit is rounded. |
michael@0 | 207 | // If requested_digits equals -1, then the shortest exponential representation |
michael@0 | 208 | // is computed. |
michael@0 | 209 | // |
michael@0 | 210 | // Examples with EMIT_POSITIVE_EXPONENT_SIGN deactivated, and |
michael@0 | 211 | // exponent_character set to 'e'. |
michael@0 | 212 | // ToExponential(3.12, 1) -> "3.1e0" |
michael@0 | 213 | // ToExponential(5.0, 3) -> "5.000e0" |
michael@0 | 214 | // ToExponential(0.001, 2) -> "1.00e-3" |
michael@0 | 215 | // ToExponential(3.1415, -1) -> "3.1415e0" |
michael@0 | 216 | // ToExponential(3.1415, 4) -> "3.1415e0" |
michael@0 | 217 | // ToExponential(3.1415, 3) -> "3.142e0" |
michael@0 | 218 | // ToExponential(123456789000000, 3) -> "1.235e14" |
michael@0 | 219 | // ToExponential(1000000000000000019884624838656.0, -1) -> "1e30" |
michael@0 | 220 | // ToExponential(1000000000000000019884624838656.0, 32) -> |
michael@0 | 221 | // "1.00000000000000001988462483865600e30" |
michael@0 | 222 | // ToExponential(1234, 0) -> "1e3" |
michael@0 | 223 | // |
michael@0 | 224 | // Returns true if the conversion succeeds. The conversion always succeeds |
michael@0 | 225 | // except for the following cases: |
michael@0 | 226 | // - the input value is special and no infinity_symbol or nan_symbol has |
michael@0 | 227 | // been provided to the constructor, |
michael@0 | 228 | // - 'requested_digits' > kMaxExponentialDigits. |
michael@0 | 229 | // The last condition implies that the result will never contain more than |
michael@0 | 230 | // kMaxExponentialDigits + 8 characters (the sign, the digit before the |
michael@0 | 231 | // decimal point, the decimal point, the exponent character, the |
michael@0 | 232 | // exponent's sign, and at most 3 exponent digits). |
michael@0 | 233 | MFBT_API bool ToExponential(double value, |
michael@0 | 234 | int requested_digits, |
michael@0 | 235 | StringBuilder* result_builder) const; |
michael@0 | 236 | |
michael@0 | 237 | // Computes 'precision' leading digits of the given 'value' and returns them |
michael@0 | 238 | // either in exponential or decimal format, depending on |
michael@0 | 239 | // max_{leading|trailing}_padding_zeroes_in_precision_mode (given to the |
michael@0 | 240 | // constructor). |
michael@0 | 241 | // The last computed digit is rounded. |
michael@0 | 242 | // |
michael@0 | 243 | // Example with max_leading_padding_zeroes_in_precision_mode = 6. |
michael@0 | 244 | // ToPrecision(0.0000012345, 2) -> "0.0000012" |
michael@0 | 245 | // ToPrecision(0.00000012345, 2) -> "1.2e-7" |
michael@0 | 246 | // Similarily the converter may add up to |
michael@0 | 247 | // max_trailing_padding_zeroes_in_precision_mode in precision mode to avoid |
michael@0 | 248 | // returning an exponential representation. A zero added by the |
michael@0 | 249 | // EMIT_TRAILING_ZERO_AFTER_POINT flag is counted for this limit. |
michael@0 | 250 | // Examples for max_trailing_padding_zeroes_in_precision_mode = 1: |
michael@0 | 251 | // ToPrecision(230.0, 2) -> "230" |
michael@0 | 252 | // ToPrecision(230.0, 2) -> "230." with EMIT_TRAILING_DECIMAL_POINT. |
michael@0 | 253 | // ToPrecision(230.0, 2) -> "2.3e2" with EMIT_TRAILING_ZERO_AFTER_POINT. |
michael@0 | 254 | // Examples for max_trailing_padding_zeroes_in_precision_mode = 3, and no |
michael@0 | 255 | // EMIT_TRAILING_ZERO_AFTER_POINT: |
michael@0 | 256 | // ToPrecision(123450.0, 6) -> "123450" |
michael@0 | 257 | // ToPrecision(123450.0, 5) -> "123450" |
michael@0 | 258 | // ToPrecision(123450.0, 4) -> "123500" |
michael@0 | 259 | // ToPrecision(123450.0, 3) -> "123000" |
michael@0 | 260 | // ToPrecision(123450.0, 2) -> "1.2e5" |
michael@0 | 261 | // |
michael@0 | 262 | // Returns true if the conversion succeeds. The conversion always succeeds |
michael@0 | 263 | // except for the following cases: |
michael@0 | 264 | // - the input value is special and no infinity_symbol or nan_symbol has |
michael@0 | 265 | // been provided to the constructor, |
michael@0 | 266 | // - precision < kMinPericisionDigits |
michael@0 | 267 | // - precision > kMaxPrecisionDigits |
michael@0 | 268 | // The last condition implies that the result will never contain more than |
michael@0 | 269 | // kMaxPrecisionDigits + 7 characters (the sign, the decimal point, the |
michael@0 | 270 | // exponent character, the exponent's sign, and at most 3 exponent digits). |
michael@0 | 271 | MFBT_API bool ToPrecision(double value, |
michael@0 | 272 | int precision, |
michael@0 | 273 | bool* used_exponential_notation, |
michael@0 | 274 | StringBuilder* result_builder) const; |
michael@0 | 275 | |
michael@0 | 276 | enum DtoaMode { |
michael@0 | 277 | // Produce the shortest correct representation. |
michael@0 | 278 | // For example the output of 0.299999999999999988897 is (the less accurate |
michael@0 | 279 | // but correct) 0.3. |
michael@0 | 280 | SHORTEST, |
michael@0 | 281 | // Same as SHORTEST, but for single-precision floats. |
michael@0 | 282 | SHORTEST_SINGLE, |
michael@0 | 283 | // Produce a fixed number of digits after the decimal point. |
michael@0 | 284 | // For instance fixed(0.1, 4) becomes 0.1000 |
michael@0 | 285 | // If the input number is big, the output will be big. |
michael@0 | 286 | FIXED, |
michael@0 | 287 | // Fixed number of digits (independent of the decimal point). |
michael@0 | 288 | PRECISION |
michael@0 | 289 | }; |
michael@0 | 290 | |
michael@0 | 291 | // The maximal number of digits that are needed to emit a double in base 10. |
michael@0 | 292 | // A higher precision can be achieved by using more digits, but the shortest |
michael@0 | 293 | // accurate representation of any double will never use more digits than |
michael@0 | 294 | // kBase10MaximalLength. |
michael@0 | 295 | // Note that DoubleToAscii null-terminates its input. So the given buffer |
michael@0 | 296 | // should be at least kBase10MaximalLength + 1 characters long. |
michael@0 | 297 | static const MFBT_DATA int kBase10MaximalLength = 17; |
michael@0 | 298 | |
michael@0 | 299 | // Converts the given double 'v' to ascii. 'v' must not be NaN, +Infinity, or |
michael@0 | 300 | // -Infinity. In SHORTEST_SINGLE-mode this restriction also applies to 'v' |
michael@0 | 301 | // after it has been casted to a single-precision float. That is, in this |
michael@0 | 302 | // mode static_cast<float>(v) must not be NaN, +Infinity or -Infinity. |
michael@0 | 303 | // |
michael@0 | 304 | // The result should be interpreted as buffer * 10^(point-length). |
michael@0 | 305 | // |
michael@0 | 306 | // The output depends on the given mode: |
michael@0 | 307 | // - SHORTEST: produce the least amount of digits for which the internal |
michael@0 | 308 | // identity requirement is still satisfied. If the digits are printed |
michael@0 | 309 | // (together with the correct exponent) then reading this number will give |
michael@0 | 310 | // 'v' again. The buffer will choose the representation that is closest to |
michael@0 | 311 | // 'v'. If there are two at the same distance, than the one farther away |
michael@0 | 312 | // from 0 is chosen (halfway cases - ending with 5 - are rounded up). |
michael@0 | 313 | // In this mode the 'requested_digits' parameter is ignored. |
michael@0 | 314 | // - SHORTEST_SINGLE: same as SHORTEST but with single-precision. |
michael@0 | 315 | // - FIXED: produces digits necessary to print a given number with |
michael@0 | 316 | // 'requested_digits' digits after the decimal point. The produced digits |
michael@0 | 317 | // might be too short in which case the caller has to fill the remainder |
michael@0 | 318 | // with '0's. |
michael@0 | 319 | // Example: toFixed(0.001, 5) is allowed to return buffer="1", point=-2. |
michael@0 | 320 | // Halfway cases are rounded towards +/-Infinity (away from 0). The call |
michael@0 | 321 | // toFixed(0.15, 2) thus returns buffer="2", point=0. |
michael@0 | 322 | // The returned buffer may contain digits that would be truncated from the |
michael@0 | 323 | // shortest representation of the input. |
michael@0 | 324 | // - PRECISION: produces 'requested_digits' where the first digit is not '0'. |
michael@0 | 325 | // Even though the length of produced digits usually equals |
michael@0 | 326 | // 'requested_digits', the function is allowed to return fewer digits, in |
michael@0 | 327 | // which case the caller has to fill the missing digits with '0's. |
michael@0 | 328 | // Halfway cases are again rounded away from 0. |
michael@0 | 329 | // DoubleToAscii expects the given buffer to be big enough to hold all |
michael@0 | 330 | // digits and a terminating null-character. In SHORTEST-mode it expects a |
michael@0 | 331 | // buffer of at least kBase10MaximalLength + 1. In all other modes the |
michael@0 | 332 | // requested_digits parameter and the padding-zeroes limit the size of the |
michael@0 | 333 | // output. Don't forget the decimal point, the exponent character and the |
michael@0 | 334 | // terminating null-character when computing the maximal output size. |
michael@0 | 335 | // The given length is only used in debug mode to ensure the buffer is big |
michael@0 | 336 | // enough. |
michael@0 | 337 | static MFBT_API void DoubleToAscii(double v, |
michael@0 | 338 | DtoaMode mode, |
michael@0 | 339 | int requested_digits, |
michael@0 | 340 | char* buffer, |
michael@0 | 341 | int buffer_length, |
michael@0 | 342 | bool* sign, |
michael@0 | 343 | int* length, |
michael@0 | 344 | int* point); |
michael@0 | 345 | |
michael@0 | 346 | private: |
michael@0 | 347 | // Implementation for ToShortest and ToShortestSingle. |
michael@0 | 348 | MFBT_API bool ToShortestIeeeNumber(double value, |
michael@0 | 349 | StringBuilder* result_builder, |
michael@0 | 350 | DtoaMode mode) const; |
michael@0 | 351 | |
michael@0 | 352 | // If the value is a special value (NaN or Infinity) constructs the |
michael@0 | 353 | // corresponding string using the configured infinity/nan-symbol. |
michael@0 | 354 | // If either of them is NULL or the value is not special then the |
michael@0 | 355 | // function returns false. |
michael@0 | 356 | MFBT_API bool HandleSpecialValues(double value, StringBuilder* result_builder) const; |
michael@0 | 357 | // Constructs an exponential representation (i.e. 1.234e56). |
michael@0 | 358 | // The given exponent assumes a decimal point after the first decimal digit. |
michael@0 | 359 | MFBT_API void CreateExponentialRepresentation(const char* decimal_digits, |
michael@0 | 360 | int length, |
michael@0 | 361 | int exponent, |
michael@0 | 362 | StringBuilder* result_builder) const; |
michael@0 | 363 | // Creates a decimal representation (i.e 1234.5678). |
michael@0 | 364 | MFBT_API void CreateDecimalRepresentation(const char* decimal_digits, |
michael@0 | 365 | int length, |
michael@0 | 366 | int decimal_point, |
michael@0 | 367 | int digits_after_point, |
michael@0 | 368 | StringBuilder* result_builder) const; |
michael@0 | 369 | |
michael@0 | 370 | const int flags_; |
michael@0 | 371 | const char* const infinity_symbol_; |
michael@0 | 372 | const char* const nan_symbol_; |
michael@0 | 373 | const char exponent_character_; |
michael@0 | 374 | const int decimal_in_shortest_low_; |
michael@0 | 375 | const int decimal_in_shortest_high_; |
michael@0 | 376 | const int max_leading_padding_zeroes_in_precision_mode_; |
michael@0 | 377 | const int max_trailing_padding_zeroes_in_precision_mode_; |
michael@0 | 378 | |
michael@0 | 379 | DISALLOW_IMPLICIT_CONSTRUCTORS(DoubleToStringConverter); |
michael@0 | 380 | }; |
michael@0 | 381 | |
michael@0 | 382 | |
michael@0 | 383 | class StringToDoubleConverter { |
michael@0 | 384 | public: |
michael@0 | 385 | // Enumeration for allowing octals and ignoring junk when converting |
michael@0 | 386 | // strings to numbers. |
michael@0 | 387 | enum Flags { |
michael@0 | 388 | NO_FLAGS = 0, |
michael@0 | 389 | ALLOW_HEX = 1, |
michael@0 | 390 | ALLOW_OCTALS = 2, |
michael@0 | 391 | ALLOW_TRAILING_JUNK = 4, |
michael@0 | 392 | ALLOW_LEADING_SPACES = 8, |
michael@0 | 393 | ALLOW_TRAILING_SPACES = 16, |
michael@0 | 394 | ALLOW_SPACES_AFTER_SIGN = 32 |
michael@0 | 395 | }; |
michael@0 | 396 | |
michael@0 | 397 | // Flags should be a bit-or combination of the possible Flags-enum. |
michael@0 | 398 | // - NO_FLAGS: no special flags. |
michael@0 | 399 | // - ALLOW_HEX: recognizes the prefix "0x". Hex numbers may only be integers. |
michael@0 | 400 | // Ex: StringToDouble("0x1234") -> 4660.0 |
michael@0 | 401 | // In StringToDouble("0x1234.56") the characters ".56" are trailing |
michael@0 | 402 | // junk. The result of the call is hence dependent on |
michael@0 | 403 | // the ALLOW_TRAILING_JUNK flag and/or the junk value. |
michael@0 | 404 | // With this flag "0x" is a junk-string. Even with ALLOW_TRAILING_JUNK, |
michael@0 | 405 | // the string will not be parsed as "0" followed by junk. |
michael@0 | 406 | // |
michael@0 | 407 | // - ALLOW_OCTALS: recognizes the prefix "0" for octals: |
michael@0 | 408 | // If a sequence of octal digits starts with '0', then the number is |
michael@0 | 409 | // read as octal integer. Octal numbers may only be integers. |
michael@0 | 410 | // Ex: StringToDouble("01234") -> 668.0 |
michael@0 | 411 | // StringToDouble("012349") -> 12349.0 // Not a sequence of octal |
michael@0 | 412 | // // digits. |
michael@0 | 413 | // In StringToDouble("01234.56") the characters ".56" are trailing |
michael@0 | 414 | // junk. The result of the call is hence dependent on |
michael@0 | 415 | // the ALLOW_TRAILING_JUNK flag and/or the junk value. |
michael@0 | 416 | // In StringToDouble("01234e56") the characters "e56" are trailing |
michael@0 | 417 | // junk, too. |
michael@0 | 418 | // - ALLOW_TRAILING_JUNK: ignore trailing characters that are not part of |
michael@0 | 419 | // a double literal. |
michael@0 | 420 | // - ALLOW_LEADING_SPACES: skip over leading spaces. |
michael@0 | 421 | // - ALLOW_TRAILING_SPACES: ignore trailing spaces. |
michael@0 | 422 | // - ALLOW_SPACES_AFTER_SIGN: ignore spaces after the sign. |
michael@0 | 423 | // Ex: StringToDouble("- 123.2") -> -123.2. |
michael@0 | 424 | // StringToDouble("+ 123.2") -> 123.2 |
michael@0 | 425 | // |
michael@0 | 426 | // empty_string_value is returned when an empty string is given as input. |
michael@0 | 427 | // If ALLOW_LEADING_SPACES or ALLOW_TRAILING_SPACES are set, then a string |
michael@0 | 428 | // containing only spaces is converted to the 'empty_string_value', too. |
michael@0 | 429 | // |
michael@0 | 430 | // junk_string_value is returned when |
michael@0 | 431 | // a) ALLOW_TRAILING_JUNK is not set, and a junk character (a character not |
michael@0 | 432 | // part of a double-literal) is found. |
michael@0 | 433 | // b) ALLOW_TRAILING_JUNK is set, but the string does not start with a |
michael@0 | 434 | // double literal. |
michael@0 | 435 | // |
michael@0 | 436 | // infinity_symbol and nan_symbol are strings that are used to detect |
michael@0 | 437 | // inputs that represent infinity and NaN. They can be null, in which case |
michael@0 | 438 | // they are ignored. |
michael@0 | 439 | // The conversion routine first reads any possible signs. Then it compares the |
michael@0 | 440 | // following character of the input-string with the first character of |
michael@0 | 441 | // the infinity, and nan-symbol. If either matches, the function assumes, that |
michael@0 | 442 | // a match has been found, and expects the following input characters to match |
michael@0 | 443 | // the remaining characters of the special-value symbol. |
michael@0 | 444 | // This means that the following restrictions apply to special-value symbols: |
michael@0 | 445 | // - they must not start with signs ('+', or '-'), |
michael@0 | 446 | // - they must not have the same first character. |
michael@0 | 447 | // - they must not start with digits. |
michael@0 | 448 | // |
michael@0 | 449 | // Examples: |
michael@0 | 450 | // flags = ALLOW_HEX | ALLOW_TRAILING_JUNK, |
michael@0 | 451 | // empty_string_value = 0.0, |
michael@0 | 452 | // junk_string_value = NaN, |
michael@0 | 453 | // infinity_symbol = "infinity", |
michael@0 | 454 | // nan_symbol = "nan": |
michael@0 | 455 | // StringToDouble("0x1234") -> 4660.0. |
michael@0 | 456 | // StringToDouble("0x1234K") -> 4660.0. |
michael@0 | 457 | // StringToDouble("") -> 0.0 // empty_string_value. |
michael@0 | 458 | // StringToDouble(" ") -> NaN // junk_string_value. |
michael@0 | 459 | // StringToDouble(" 1") -> NaN // junk_string_value. |
michael@0 | 460 | // StringToDouble("0x") -> NaN // junk_string_value. |
michael@0 | 461 | // StringToDouble("-123.45") -> -123.45. |
michael@0 | 462 | // StringToDouble("--123.45") -> NaN // junk_string_value. |
michael@0 | 463 | // StringToDouble("123e45") -> 123e45. |
michael@0 | 464 | // StringToDouble("123E45") -> 123e45. |
michael@0 | 465 | // StringToDouble("123e+45") -> 123e45. |
michael@0 | 466 | // StringToDouble("123E-45") -> 123e-45. |
michael@0 | 467 | // StringToDouble("123e") -> 123.0 // trailing junk ignored. |
michael@0 | 468 | // StringToDouble("123e-") -> 123.0 // trailing junk ignored. |
michael@0 | 469 | // StringToDouble("+NaN") -> NaN // NaN string literal. |
michael@0 | 470 | // StringToDouble("-infinity") -> -inf. // infinity literal. |
michael@0 | 471 | // StringToDouble("Infinity") -> NaN // junk_string_value. |
michael@0 | 472 | // |
michael@0 | 473 | // flags = ALLOW_OCTAL | ALLOW_LEADING_SPACES, |
michael@0 | 474 | // empty_string_value = 0.0, |
michael@0 | 475 | // junk_string_value = NaN, |
michael@0 | 476 | // infinity_symbol = NULL, |
michael@0 | 477 | // nan_symbol = NULL: |
michael@0 | 478 | // StringToDouble("0x1234") -> NaN // junk_string_value. |
michael@0 | 479 | // StringToDouble("01234") -> 668.0. |
michael@0 | 480 | // StringToDouble("") -> 0.0 // empty_string_value. |
michael@0 | 481 | // StringToDouble(" ") -> 0.0 // empty_string_value. |
michael@0 | 482 | // StringToDouble(" 1") -> 1.0 |
michael@0 | 483 | // StringToDouble("0x") -> NaN // junk_string_value. |
michael@0 | 484 | // StringToDouble("0123e45") -> NaN // junk_string_value. |
michael@0 | 485 | // StringToDouble("01239E45") -> 1239e45. |
michael@0 | 486 | // StringToDouble("-infinity") -> NaN // junk_string_value. |
michael@0 | 487 | // StringToDouble("NaN") -> NaN // junk_string_value. |
michael@0 | 488 | StringToDoubleConverter(int flags, |
michael@0 | 489 | double empty_string_value, |
michael@0 | 490 | double junk_string_value, |
michael@0 | 491 | const char* infinity_symbol, |
michael@0 | 492 | const char* nan_symbol) |
michael@0 | 493 | : flags_(flags), |
michael@0 | 494 | empty_string_value_(empty_string_value), |
michael@0 | 495 | junk_string_value_(junk_string_value), |
michael@0 | 496 | infinity_symbol_(infinity_symbol), |
michael@0 | 497 | nan_symbol_(nan_symbol) { |
michael@0 | 498 | } |
michael@0 | 499 | |
michael@0 | 500 | // Performs the conversion. |
michael@0 | 501 | // The output parameter 'processed_characters_count' is set to the number |
michael@0 | 502 | // of characters that have been processed to read the number. |
michael@0 | 503 | // Spaces than are processed with ALLOW_{LEADING|TRAILING}_SPACES are included |
michael@0 | 504 | // in the 'processed_characters_count'. Trailing junk is never included. |
michael@0 | 505 | double StringToDouble(const char* buffer, |
michael@0 | 506 | int length, |
michael@0 | 507 | int* processed_characters_count) const { |
michael@0 | 508 | return StringToIeee(buffer, length, processed_characters_count, true); |
michael@0 | 509 | } |
michael@0 | 510 | |
michael@0 | 511 | // Same as StringToDouble but reads a float. |
michael@0 | 512 | // Note that this is not equivalent to static_cast<float>(StringToDouble(...)) |
michael@0 | 513 | // due to potential double-rounding. |
michael@0 | 514 | float StringToFloat(const char* buffer, |
michael@0 | 515 | int length, |
michael@0 | 516 | int* processed_characters_count) const { |
michael@0 | 517 | return static_cast<float>(StringToIeee(buffer, length, |
michael@0 | 518 | processed_characters_count, false)); |
michael@0 | 519 | } |
michael@0 | 520 | |
michael@0 | 521 | private: |
michael@0 | 522 | const int flags_; |
michael@0 | 523 | const double empty_string_value_; |
michael@0 | 524 | const double junk_string_value_; |
michael@0 | 525 | const char* const infinity_symbol_; |
michael@0 | 526 | const char* const nan_symbol_; |
michael@0 | 527 | |
michael@0 | 528 | double StringToIeee(const char* buffer, |
michael@0 | 529 | int length, |
michael@0 | 530 | int* processed_characters_count, |
michael@0 | 531 | bool read_as_double) const; |
michael@0 | 532 | |
michael@0 | 533 | DISALLOW_IMPLICIT_CONSTRUCTORS(StringToDoubleConverter); |
michael@0 | 534 | }; |
michael@0 | 535 | |
michael@0 | 536 | } // namespace double_conversion |
michael@0 | 537 | |
michael@0 | 538 | #endif // DOUBLE_CONVERSION_DOUBLE_CONVERSION_H_ |