aboutsummaryrefslogtreecommitdiff
path: root/src/base/StringToNumberConverter.cpp
blob: 65aec48e9b064d785ffc8b518aad7b0b5cf3d243 (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
#include "cru/base/StringToNumberConverter.h"
#include "cru/base/Exception.h"

namespace cru {
bool StringToIntegerConverter::CheckParams() const {
  return base == 0 || base >= 2 & base <= 36;
}

static bool IsSpace(char c) {
  return c == ' ' || c == '\t' || c == '\n' || c == '\r';
}

StringToIntegerResult StringToIntegerConverter::Parse(
    const char* const str, const Index size,
    Index* processed_characters_count) const {
  if (str == nullptr) throw std::invalid_argument("Invalid str.");
  if (size < 0) throw std::invalid_argument("Invalid size.");
  if (!CheckParams()) throw std::invalid_argument("Invalid parsing flags.");

  const bool throw_on_error = flags.Has(StringToNumberFlags::kThrowOnError);

  auto const end = str + size;

  auto current = str;

  if (flags & StringToNumberFlags::kAllowLeadingSpaces) {
    while (current != end && IsSpace(*current)) {
      current++;
    }
  }

  if (current == end) {
    if (processed_characters_count) {
      *processed_characters_count = 0;
    }
    if (throw_on_error) {
      throw Exception(u"Empty string (after reading leading spaces).");
    } else {
      return {false, 0};
    }
  }

  bool negate = false;

  if (*current == '-') {
    ++current;
    negate = true;
  } else if (*current == '+') {
    ++current;
  }

  if (current == end) {
    if (processed_characters_count) {
      *processed_characters_count = 0;
    }
    if (throw_on_error) {
      throw Exception(u"Empty string (after reading sign).");
    } else {
      return {false, 0};
    }
  }

  int real_base = base;

  if (real_base == 0) {
    if (*current == '0') {
      ++current;
      if (current == end) {
        if (processed_characters_count) {
          *processed_characters_count = current - str;
        }
        return {negate, 0};
      } else if (*current == 'x' || *current == 'X') {
        ++current;
        real_base = 16;
      } else if (*current == 'b' || *current == 'B') {
        ++current;
        real_base = 2;
      } else {
        real_base = 8;
      }
    } else {
      real_base = 10;
    }
  }

  if (current == end) {
    if (processed_characters_count) {
      *processed_characters_count = 0;
    }
    if (throw_on_error) {
      throw Exception(u"Empty string (after reading head base indicator).");
    } else {
      return {false, 0};
    }
  }

  const bool allow_leading_zero =
      flags.Has(StringToNumberFlags::kAllowLeadingZeroForInteger);

  while (current != end && *current == '0') {
    current++;
  }

  if (current == end) {
    if (processed_characters_count) {
      *processed_characters_count = current - str;
    }
    return {negate, 0};
  }

  const bool allow_trailing_junk =
      flags.Has(StringToNumberFlags::kAllowTrailingJunk);
  const bool allow_trailing_spaces =
      flags.Has(StringToNumberFlags::kAllowTrailingSpaces);

  unsigned long long result = 0;

  while (current != end) {
    const char c = *current;
    if (c >= '0' && c <= (real_base > 10 ? '9' : real_base + '0' - 1)) {
      result = result * real_base + c - '0';
      current++;
    } else if (real_base > 10 && c >= 'a' && c <= (real_base + 'a' - 10 - 1)) {
      result = result * real_base + c - 'a' + 10;
      current++;
    } else if (real_base > 10 && c >= 'A' && c <= (real_base + 'A' - 10 - 1)) {
      result = result * real_base + c - 'A' + 10;
      current++;
    } else if (allow_trailing_junk) {
      break;
    } else if (allow_trailing_spaces && IsSpace(c)) {
      break;
    } else {
      if (processed_characters_count) {
        *processed_characters_count = 0;
      }
      if (throw_on_error) {
        throw Exception(String(u"Read invalid character '") + c + u"'.");
      } else {
        return {false, 0};
      }
    }
  }

  if (allow_trailing_spaces) {
    while (current != end && IsSpace(*current)) {
      current++;
    }

    if (current != end) {
      if (processed_characters_count) {
        *processed_characters_count = 0;
      }
      if (throw_on_error) {
        throw Exception(u"There is trailing junk.");
      } else {
        return {false, 0};
      }
    }
  }

  if (processed_characters_count) {
    *processed_characters_count = current - str;
  }

  return {negate, result};
}

}  // namespace cru