From: Joel Rosdahl Date: Sat, 7 Sep 2019 17:55:38 +0000 (+0200) Subject: Move parse_int to util X-Git-Tag: v4.0~770 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=fd8508d2b613d8ada2e305bf8268020415327263;p=thirdparty%2Fccache.git Move parse_int to util --- diff --git a/src/Config.cpp b/src/Config.cpp index 36b98f004..ee875b170 100644 --- a/src/Config.cpp +++ b/src/Config.cpp @@ -340,24 +340,6 @@ format_umask(uint32_t umask) } } -int -parse_int(const std::string& value) -{ - size_t end; - long result; - bool failed = false; - try { - result = std::stol(value, &end, 10); - } catch (std::exception&) { - failed = true; - } - if (failed || end != value.size() || result < std::numeric_limits::min() - || result > std::numeric_limits::max()) { - throw Error(fmt::format("invalid integer: \"{}\"", value)); - } - return result; -} - unsigned parse_unsigned(const std::string& value) { @@ -703,7 +685,7 @@ Config::set_item(const std::string& key, break; case ConfigItem::compression_level: { - auto level = parse_int(value); + auto level = util::parse_int(value); if (level < -128 || level > 127) { throw Error("compression level must be between -128 and 127"); } diff --git a/src/util.cpp b/src/util.cpp index 7948938ba..f74fb1509 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -1848,6 +1848,24 @@ get_level_1_files(const std::string& dir, get_cache_files_internal(dir, 1, progress_receiver, files); } +int +parse_int(const std::string& value) +{ + size_t end; + long result; + bool failed = false; + try { + result = std::stol(value, &end, 10); + } catch (std::exception&) { + failed = true; + } + if (failed || end != value.size() || result < std::numeric_limits::min() + || result > std::numeric_limits::max()) { + throw Error(fmt::format("invalid integer: \"{}\"", value)); + } + return result; +} + std::string read_file(const std::string& path) { diff --git a/src/util.hpp b/src/util.hpp index 597b18fd6..decff6ce9 100644 --- a/src/util.hpp +++ b/src/util.hpp @@ -79,6 +79,11 @@ void get_level_1_files(const std::string& dir, const ProgressReceiver& progress_receiver, std::vector>& files); +// Parse a string into an integer. +// +// Throws Error on error. +int parse_int(const std::string& value); + // Read file data as a string. // // Throws Error on error. diff --git a/unittest/test_util.cpp b/unittest/test_util.cpp index 5beec601d..89d505bca 100644 --- a/unittest/test_util.cpp +++ b/unittest/test_util.cpp @@ -20,6 +20,8 @@ #include +using Catch::Equals; + TEST_CASE("util::base_name") { CHECK(util::base_name("") == ""); @@ -153,6 +155,22 @@ TEST_CASE("util::get_level_1_files") } } +TEST_CASE("util::parse_int") +{ + CHECK(util::parse_int("0") == 0); + CHECK(util::parse_int("2") == 2); + CHECK(util::parse_int("-17") == -17); + CHECK(util::parse_int("42") == 42); + CHECK(util::parse_int("0666") == 666); + CHECK(util::parse_int(" 777") == 777); + + CHECK_THROWS_WITH(util::parse_int(""), Equals("invalid integer: \"\"")); + CHECK_THROWS_WITH(util::parse_int("x"), Equals("invalid integer: \"x\"")); + CHECK_THROWS_WITH(util::parse_int("0x"), Equals("invalid integer: \"0x\"")); + CHECK_THROWS_WITH(util::parse_int("0x4"), Equals("invalid integer: \"0x4\"")); + CHECK_THROWS_WITH(util::parse_int("0 "), Equals("invalid integer: \"0 \"")); +} + TEST_CASE("util::read_file and util::write_file") { util::write_file("test", "foo\nbar\n");