summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/platform/wtf/text/string_to_number.cc
blob: d915df41740ae4bd699e59202812dbbc86f5e4ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "third_party/blink/renderer/platform/wtf/text/string_to_number.h"

#include <type_traits>
#include "third_party/blink/renderer/platform/wtf/dtoa.h"
#include "third_party/blink/renderer/platform/wtf/text/ascii_ctype.h"
#include "third_party/blink/renderer/platform/wtf/text/string_impl.h"

namespace WTF {

template <int base>
bool IsCharacterAllowedInBase(UChar);

template <>
bool IsCharacterAllowedInBase<10>(UChar c) {
  return IsASCIIDigit(c);
}

template <>
bool IsCharacterAllowedInBase<16>(UChar c) {
  return IsASCIIHexDigit(c);
}

template <typename IntegralType, typename CharType, int base>
static inline IntegralType ToIntegralType(const CharType* data,
                                          size_t length,
                                          NumberParsingOptions options,
                                          NumberParsingResult* parsing_result) {
  static_assert(std::is_integral<IntegralType>::value,
                "IntegralType must be an integral type.");
  static constexpr IntegralType kIntegralMax =
      std::numeric_limits<IntegralType>::max();
  static constexpr IntegralType kIntegralMin =
      std::numeric_limits<IntegralType>::min();
  static constexpr bool kIsSigned =
      std::numeric_limits<IntegralType>::is_signed;
  DCHECK(parsing_result);

  IntegralType value = 0;
  NumberParsingResult result = NumberParsingResult::kError;
  bool is_negative = false;
  bool overflow = false;
  const bool accept_minus = kIsSigned || options.AcceptMinusZeroForUnsigned();

  if (!data)
    goto bye;

  if (options.AcceptWhitespace()) {
    while (length && IsSpaceOrNewline(*data)) {
      --length;
      ++data;
    }
  }

  if (accept_minus && length && *data == '-') {
    --length;
    ++data;
    is_negative = true;
  } else if (length && options.AcceptLeadingPlus() && *data == '+') {
    --length;
    ++data;
  }

  if (!length || !IsCharacterAllowedInBase<base>(*data))
    goto bye;

  while (length && IsCharacterAllowedInBase<base>(*data)) {
    --length;
    IntegralType digit_value;
    CharType c = *data;
    if (IsASCIIDigit(c))
      digit_value = c - '0';
    else if (c >= 'a')
      digit_value = c - 'a' + 10;
    else
      digit_value = c - 'A' + 10;

    if (is_negative) {
      if (!kIsSigned && options.AcceptMinusZeroForUnsigned()) {
        if (digit_value != 0) {
          result = NumberParsingResult::kError;
          overflow = true;
        }
      } else {
        // Overflow condition:
        //       value * base - digit_value < kIntegralMin
        //   <=> value < (kIntegralMin + digit_value) / base
        // We must be careful of rounding errors here, but the default rounding
        // mode (round to zero) works well, so we can use this formula as-is.
        if (value < (kIntegralMin + digit_value) / base) {
          result = NumberParsingResult::kOverflowMin;
          overflow = true;
        }
      }
    } else {
      // Overflow condition:
      //       value * base + digit_value > kIntegralMax
      //   <=> value > (kIntegralMax + digit_value) / base
      // Ditto regarding rounding errors.
      if (value > (kIntegralMax - digit_value) / base) {
        result = NumberParsingResult::kOverflowMax;
        overflow = true;
      }
    }

    if (!overflow) {
      if (is_negative)
        value = base * value - digit_value;
      else
        value = base * value + digit_value;
    }
    ++data;
  }

  if (options.AcceptWhitespace()) {
    while (length && IsSpaceOrNewline(*data)) {
      --length;
      ++data;
    }
  }

  if (length == 0 || options.AcceptTrailingGarbage()) {
    if (!overflow)
      result = NumberParsingResult::kSuccess;
  } else {
    // Even if we detected overflow, we return kError for trailing garbage.
    result = NumberParsingResult::kError;
  }
bye:
  *parsing_result = result;
  return result == NumberParsingResult::kSuccess ? value : 0;
}

template <typename IntegralType, typename CharType, int base>
static inline IntegralType ToIntegralType(const CharType* data,
                                          size_t length,
                                          NumberParsingOptions options,
                                          bool* ok) {
  NumberParsingResult result;
  IntegralType value = ToIntegralType<IntegralType, CharType, base>(
      data, length, options, &result);
  if (ok)
    *ok = result == NumberParsingResult::kSuccess;
  return value;
}

unsigned CharactersToUInt(const LChar* data,
                          size_t length,
                          NumberParsingOptions options,
                          NumberParsingResult* result) {
  return ToIntegralType<unsigned, LChar, 10>(data, length, options, result);
}

unsigned CharactersToUInt(const UChar* data,
                          size_t length,
                          NumberParsingOptions options,
                          NumberParsingResult* result) {
  return ToIntegralType<unsigned, UChar, 10>(data, length, options, result);
}

unsigned HexCharactersToUInt(const LChar* data,
                             size_t length,
                             NumberParsingOptions options,
                             bool* ok) {
  return ToIntegralType<unsigned, LChar, 16>(data, length, options, ok);
}

unsigned HexCharactersToUInt(const UChar* data,
                             size_t length,
                             NumberParsingOptions options,
                             bool* ok) {
  return ToIntegralType<unsigned, UChar, 16>(data, length, options, ok);
}

uint64_t HexCharactersToUInt64(const LChar* data,
                               size_t length,
                               NumberParsingOptions options,
                               bool* ok) {
  return ToIntegralType<uint64_t, LChar, 16>(data, length, options, ok);
}

uint64_t HexCharactersToUInt64(const UChar* data,
                               size_t length,
                               NumberParsingOptions options,
                               bool* ok) {
  return ToIntegralType<uint64_t, UChar, 16>(data, length, options, ok);
}

int CharactersToInt(const LChar* data,
                    size_t length,
                    NumberParsingOptions options,
                    bool* ok) {
  return ToIntegralType<int, LChar, 10>(data, length, options, ok);
}

int CharactersToInt(const UChar* data,
                    size_t length,
                    NumberParsingOptions options,
                    bool* ok) {
  return ToIntegralType<int, UChar, 10>(data, length, options, ok);
}

unsigned CharactersToUInt(const LChar* data,
                          size_t length,
                          NumberParsingOptions options,
                          bool* ok) {
  return ToIntegralType<unsigned, LChar, 10>(data, length, options, ok);
}

unsigned CharactersToUInt(const UChar* data,
                          size_t length,
                          NumberParsingOptions options,
                          bool* ok) {
  return ToIntegralType<unsigned, UChar, 10>(data, length, options, ok);
}

int64_t CharactersToInt64(const LChar* data,
                          size_t length,
                          NumberParsingOptions options,
                          bool* ok) {
  return ToIntegralType<int64_t, LChar, 10>(data, length, options, ok);
}

int64_t CharactersToInt64(const UChar* data,
                          size_t length,
                          NumberParsingOptions options,
                          bool* ok) {
  return ToIntegralType<int64_t, UChar, 10>(data, length, options, ok);
}

uint64_t CharactersToUInt64(const LChar* data,
                            size_t length,
                            NumberParsingOptions options,
                            bool* ok) {
  return ToIntegralType<uint64_t, LChar, 10>(data, length, options, ok);
}

uint64_t CharactersToUInt64(const UChar* data,
                            size_t length,
                            NumberParsingOptions options,
                            bool* ok) {
  return ToIntegralType<uint64_t, UChar, 10>(data, length, options, ok);
}

enum TrailingJunkPolicy { kDisallowTrailingJunk, kAllowTrailingJunk };

template <typename CharType, TrailingJunkPolicy policy>
static inline double ToDoubleType(const CharType* data,
                                  size_t length,
                                  bool* ok,
                                  size_t& parsed_length) {
  size_t leading_spaces_length = 0;
  while (leading_spaces_length < length &&
         IsASCIISpace(data[leading_spaces_length]))
    ++leading_spaces_length;

  double number = ParseDouble(data + leading_spaces_length,
                              length - leading_spaces_length, parsed_length);
  if (!parsed_length) {
    if (ok)
      *ok = false;
    return 0.0;
  }

  parsed_length += leading_spaces_length;
  if (ok)
    *ok = policy == kAllowTrailingJunk || parsed_length == length;
  return number;
}

double CharactersToDouble(const LChar* data, size_t length, bool* ok) {
  size_t parsed_length;
  return ToDoubleType<LChar, kDisallowTrailingJunk>(data, length, ok,
                                                    parsed_length);
}

double CharactersToDouble(const UChar* data, size_t length, bool* ok) {
  size_t parsed_length;
  return ToDoubleType<UChar, kDisallowTrailingJunk>(data, length, ok,
                                                    parsed_length);
}

double CharactersToDouble(const LChar* data,
                          size_t length,
                          size_t& parsed_length) {
  return ToDoubleType<LChar, kAllowTrailingJunk>(data, length, nullptr,
                                                 parsed_length);
}

double CharactersToDouble(const UChar* data,
                          size_t length,
                          size_t& parsed_length) {
  return ToDoubleType<UChar, kAllowTrailingJunk>(data, length, nullptr,
                                                 parsed_length);
}

float CharactersToFloat(const LChar* data, size_t length, bool* ok) {
  // FIXME: This will return ok even when the string fits into a double but
  // not a float.
  size_t parsed_length;
  return static_cast<float>(ToDoubleType<LChar, kDisallowTrailingJunk>(
      data, length, ok, parsed_length));
}

float CharactersToFloat(const UChar* data, size_t length, bool* ok) {
  // FIXME: This will return ok even when the string fits into a double but
  // not a float.
  size_t parsed_length;
  return static_cast<float>(ToDoubleType<UChar, kDisallowTrailingJunk>(
      data, length, ok, parsed_length));
}

float CharactersToFloat(const LChar* data,
                        size_t length,
                        size_t& parsed_length) {
  // FIXME: This will return ok even when the string fits into a double but
  // not a float.
  return static_cast<float>(ToDoubleType<LChar, kAllowTrailingJunk>(
      data, length, nullptr, parsed_length));
}

float CharactersToFloat(const UChar* data,
                        size_t length,
                        size_t& parsed_length) {
  // FIXME: This will return ok even when the string fits into a double but
  // not a float.
  return static_cast<float>(ToDoubleType<UChar, kAllowTrailingJunk>(
      data, length, nullptr, parsed_length));
}

}  // namespace WTF