diff options
author | Kartik K. Agaram <vc@akkartik.com> | 2017-12-21 22:20:53 -0800 |
---|---|---|
committer | Kartik K. Agaram <vc@akkartik.com> | 2017-12-22 00:45:48 -0800 |
commit | fa708f553a3c314aa1eda75cee3c4e5e3b4f40db (patch) | |
tree | 2ad692ae2c9ebe177bbf9ad4073eaffc58dce4d5 /010vm.cc | |
parent | fe8bf967a945c7d9e6aba36e42518262d54bd348 (diff) | |
download | mu-fa708f553a3c314aa1eda75cee3c4e5e3b4f40db.tar.gz |
4162
Diffstat (limited to '010vm.cc')
-rw-r--r-- | 010vm.cc | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/010vm.cc b/010vm.cc index 09348f2d..f5138329 100644 --- a/010vm.cc +++ b/010vm.cc @@ -806,6 +806,31 @@ void dump_names_without_quotes(const type_tree* x, ostream& out) { out << ')'; } +bool is_integer(const string& s) { + return s.find_first_not_of("0123456789-") == string::npos // no other characters + && s.find_first_of("0123456789") != string::npos // at least one digit + && s.find('-', 1) == string::npos; // '-' only at first position +} + +int to_integer(string n) { + char* end = NULL; + // safe because string.c_str() is guaranteed to be null-terminated + int result = strtoll(n.c_str(), &end, /*any base*/0); + if (*end != '\0') cerr << "tried to convert " << n << " to number\n"; + assert(*end == '\0'); + return result; +} + +void test_is_integer() { + CHECK(is_integer("1234")); + CHECK(is_integer("-1")); + CHECK(!is_integer("234.0")); + CHECK(is_integer("-567")); + CHECK(!is_integer("89-0")); + CHECK(!is_integer("-")); + CHECK(!is_integer("1e3")); // not supported +} + //: helper to print numbers without excessive precision :(before "End Types") |