Skip to content

Commit b46e228

Browse files
author
Jesse
committed
made _is_number recognize c++ float strings
1 parent cba806e commit b46e228

1 file changed

Lines changed: 6 additions & 19 deletions

File tree

argparse.h

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,7 @@ class ArgumentParser {
9595
size_t slen = std::strlen(argv[i]);
9696
if (slen == 0) {
9797
continue;
98-
} else if (slen >= 2 && argv[i][0] == '-' &&
99-
!_is_number(argv[i], slen)) {
98+
} else if (slen >= 2 && argv[i][0] == '-' && !_is_number(argv[i])) {
10099
push_arg();
101100
if (i == argc - 1) {
102101
name = &(argv[i][1]);
@@ -235,23 +234,11 @@ class ArgumentParser {
235234
}
236235
return ss.str();
237236
}
238-
static inline bool _is_number(const char *arg, size_t len) {
239-
if (std::isdigit(arg[0])) {
240-
return true;
241-
} else if (len >= 2) {
242-
if (arg[0] == '-') {
243-
if (std::isdigit(arg[1])) {
244-
return true;
245-
} else if (len >= 3) {
246-
if (arg[1] == '.') {
247-
if (std::isdigit(arg[2])) {
248-
return true;
249-
}
250-
}
251-
}
252-
}
253-
}
254-
return false;
237+
static inline bool _is_number(const char *arg) {
238+
std::istringstream iss{std::string(arg)};
239+
float f;
240+
iss >> std::noskipws >> f;
241+
return iss.eof() && !iss.fail();
255242
}
256243

257244
std::string _desc;

0 commit comments

Comments
 (0)