summaryrefslogtreecommitdiffstats
path: root/src/common/strtol.cc
blob: 11b900352968f47065217b9829234f08789e9e32 (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
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
/*
 * Ceph - scalable distributed file system
 *
 * Copyright (C) 2011 New Dream Network
 *
 * This is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License version 2.1, as published by the Free Software
 * Foundation.  See file COPYING.
 *
 */

#include "strtol.h"

#include <algorithm>
#include <climits>
#include <limits>
#include <cmath>
#include <sstream>
#include <strings.h>
#include <string_view>

using std::ostringstream;

bool strict_strtob(const char* str, std::string *err)
{
  if (strcasecmp(str, "false") == 0) {
    return false;
  } else if (strcasecmp(str, "true") == 0) {
    return true;
  } else {
    int b = strict_strtol(str, 10, err);
    return (bool)!!b;
  }
}

long long strict_strtoll(std::string_view str, int base, std::string *err)
{
  char *endptr;
  errno = 0; /* To distinguish success/failure after call (see man page) */
  long long ret = strtoll(str.data(), &endptr, base);
  if (endptr == str.data() || endptr != str.data() + str.size()) {
    *err = (std::string{"Expected option value to be integer, got '"} +
	    std::string{str} + "'");
    return 0;
  }
  if (errno) {
    *err = (std::string{"The option value '"} + std::string{str} +
	    "' seems to be invalid");
    return 0;
  }
  *err = "";
  return ret;
}

long long strict_strtoll(const char *str, int base, std::string *err)
{
  return strict_strtoll(std::string_view(str), base, err);
}

int strict_strtol(std::string_view str, int base, std::string *err)
{
  long long ret = strict_strtoll(str, base, err);
  if (!err->empty())
    return 0;
  if ((ret < INT_MIN) || (ret > INT_MAX)) {
    ostringstream errStr;
    errStr << "The option value '" << str << "' seems to be invalid";
    *err = errStr.str();
    return 0;
  }
  return static_cast<int>(ret);
}

int strict_strtol(const char *str, int base, std::string *err)
{
  return strict_strtol(std::string_view(str), base, err);
}

double strict_strtod(std::string_view str, std::string *err)
{
  char *endptr;
  errno = 0; /* To distinguish success/failure after call (see man page) */
  double ret = strtod(str.data(), &endptr);
  if (errno == ERANGE) {
    ostringstream oss;
    oss << "strict_strtod: floating point overflow or underflow parsing '"
	<< str << "'";
    *err = oss.str();
    return 0.0;
  }
  if (endptr == str) {
    ostringstream oss;
    oss << "strict_strtod: expected double, got: '" << str << "'";
    *err = oss.str();
    return 0;
  }
  if (*endptr != '\0') {
    ostringstream oss;
    oss << "strict_strtod: garbage at end of string. got: '" << str << "'";
    *err = oss.str();
    return 0;
  }
  *err = "";
  return ret;
}

double strict_strtod(const char *str, std::string *err)
{
  return strict_strtod(std::string_view(str), err);
}

float strict_strtof(std::string_view str, std::string *err)
{
  char *endptr;
  errno = 0; /* To distinguish success/failure after call (see man page) */
  float ret = strtof(str.data(), &endptr);
  if (errno == ERANGE) {
    ostringstream oss;
    oss << "strict_strtof: floating point overflow or underflow parsing '"
	<< str << "'";
    *err = oss.str();
    return 0.0;
  }
  if (endptr == str) {
    ostringstream oss;
    oss << "strict_strtof: expected float, got: '" << str << "'";
    *err = oss.str();
    return 0;
  }
  if (*endptr != '\0') {
    ostringstream oss;
    oss << "strict_strtof: garbage at end of string. got: '" << str << "'";
    *err = oss.str();
    return 0;
  }
  *err = "";
  return ret;
}

float strict_strtof(const char *str, std::string *err)
{
  return strict_strtof(std::string_view(str), err);
}

template<typename T>
T strict_iec_cast(std::string_view str, std::string *err)
{
  if (str.empty()) {
    *err = "strict_iecstrtoll: value not specified";
    return 0;
  }
  // get a view of the unit and of the value
  std::string_view unit;
  std::string_view n = str;
  size_t u = str.find_first_not_of("0123456789-+");
  int m = 0;
  // deal with unit prefix if there is one
  if (u != std::string_view::npos) {
    n = str.substr(0, u);
    unit = str.substr(u, str.length() - u);
    // we accept both old si prefixes as well as the proper iec prefixes
    // i.e. K, M, ... and Ki, Mi, ...
    if (unit.back() == 'i') {
      if (unit.front() == 'B') {
        *err = "strict_iecstrtoll: illegal prefix \"Bi\"";
        return 0;
      }
    }
    if (unit.length() > 2) {
      *err = "strict_iecstrtoll: illegal prefix (length > 2)";
      return 0;
    }
    switch(unit.front()) {
      case 'K':
        m = 10;
        break;
      case 'M':
        m = 20;
        break;
      case 'G':
        m = 30;
        break;
      case 'T':
        m = 40;
        break;
      case 'P':
        m = 50;
        break;
      case 'E':
        m = 60;
        break;
      case 'B':
        break;
      default:
        *err = "strict_iecstrtoll: unit prefix not recognized";
        return 0;
    }
  }

  long long ll = strict_strtoll(n, 10, err);
  if (ll < 0 && !std::numeric_limits<T>::is_signed) {
    *err = "strict_iecstrtoll: value should not be negative";
    return 0;
  }
  if (static_cast<unsigned>(m) >= sizeof(T) * CHAR_BIT) {
    *err = ("strict_iecstrtoll: the IEC prefix is too large for the designated "
        "type");
    return 0;
  }
  using promoted_t = typename std::common_type<decltype(ll), T>::type;
  if (static_cast<promoted_t>(ll) <
      static_cast<promoted_t>(std::numeric_limits<T>::min()) >> m) {
    *err = "strict_iecstrtoll: value seems to be too small";
    return 0;
  }
  if (static_cast<promoted_t>(ll) >
      static_cast<promoted_t>(std::numeric_limits<T>::max()) >> m) {
    *err = "strict_iecstrtoll: value seems to be too large";
    return 0;
  }
  return (ll << m);
}

template int strict_iec_cast<int>(std::string_view str, std::string *err);
template long strict_iec_cast<long>(std::string_view str, std::string *err);
template long long strict_iec_cast<long long>(std::string_view str, std::string *err);
template uint64_t strict_iec_cast<uint64_t>(std::string_view str, std::string *err);
template uint32_t strict_iec_cast<uint32_t>(std::string_view str, std::string *err);

uint64_t strict_iecstrtoll(std::string_view str, std::string *err)
{
  return strict_iec_cast<uint64_t>(str, err);
}

uint64_t strict_iecstrtoll(const char *str, std::string *err)
{
  return strict_iec_cast<uint64_t>(std::string_view(str), err);
}

template<typename T>
T strict_iec_cast(const char *str, std::string *err)
{
  return strict_iec_cast<T>(std::string_view(str), err);
}

template int strict_iec_cast<int>(const char *str, std::string *err);
template long strict_iec_cast<long>(const char *str, std::string *err);
template long long strict_iec_cast<long long>(const char *str, std::string *err);
template uint64_t strict_iec_cast<uint64_t>(const char *str, std::string *err);
template uint32_t strict_iec_cast<uint32_t>(const char *str, std::string *err);

template<typename T>
T strict_si_cast(std::string_view str, std::string *err)
{
  if (str.empty()) {
    *err = "strict_sistrtoll: value not specified";
    return 0;
  }
  std::string_view n = str;
  int m = 0;
  // deal with unit prefix is there is one
  if (str.find_first_not_of("0123456789+-") != std::string_view::npos) {
    const char &u = str.back();
    if (u == 'K')
      m = 3;
    else if (u == 'M')
      m = 6;
    else if (u == 'G')
      m = 9;
    else if (u == 'T')
      m = 12;
    else if (u == 'P')
      m = 15;
    else if (u == 'E')
      m = 18;
    else if (u != 'B') {
      *err = "strict_si_cast: unit prefix not recognized";
      return 0;
    }

    if (m >= 3)
      n = str.substr(0, str.length() -1);
  }

  long long ll = strict_strtoll(n, 10, err);
  if (ll < 0 && !std::numeric_limits<T>::is_signed) {
    *err = "strict_sistrtoll: value should not be negative";
    return 0;
  }
  using promoted_t = typename std::common_type<decltype(ll), T>::type;
  auto v = static_cast<promoted_t>(ll);
  auto coefficient = static_cast<promoted_t>(powl(10, m));
  if (v != std::clamp(v,
		      (static_cast<promoted_t>(std::numeric_limits<T>::min()) /
		       coefficient),
		      (static_cast<promoted_t>(std::numeric_limits<T>::max()) /
		       coefficient))) {
    *err = "strict_sistrtoll: value out of range";
    return 0;
  }
  return v * coefficient;
}

template int strict_si_cast<int>(std::string_view str, std::string *err);
template long strict_si_cast<long>(std::string_view str, std::string *err);
template long long strict_si_cast<long long>(std::string_view str, std::string *err);
template uint64_t strict_si_cast<uint64_t>(std::string_view str, std::string *err);
template uint32_t strict_si_cast<uint32_t>(std::string_view str, std::string *err);

uint64_t strict_sistrtoll(std::string_view str, std::string *err)
{
  return strict_si_cast<uint64_t>(str, err);
}

uint64_t strict_sistrtoll(const char *str, std::string *err)
{
  return strict_si_cast<uint64_t>(str, err);
}

template<typename T>
T strict_si_cast(const char *str, std::string *err)
{
  return strict_si_cast<T>(std::string_view(str), err);
}

template int strict_si_cast<int>(const char *str, std::string *err);
template long strict_si_cast<long>(const char *str, std::string *err);
template long long strict_si_cast<long long>(const char *str, std::string *err);
template uint64_t strict_si_cast<uint64_t>(const char *str, std::string *err);
template uint32_t strict_si_cast<uint32_t>(const char *str, std::string *err);