}
}
-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<int>::min()
- || result > std::numeric_limits<int>::max()) {
- throw Error(fmt::format("invalid integer: \"{}\"", value));
- }
- return result;
-}
-
unsigned
parse_unsigned(const std::string& value)
{
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");
}
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<int>::min()
+ || result > std::numeric_limits<int>::max()) {
+ throw Error(fmt::format("invalid integer: \"{}\"", value));
+ }
+ return result;
+}
+
std::string
read_file(const std::string& path)
{
const ProgressReceiver& progress_receiver,
std::vector<std::shared_ptr<CacheFile>>& 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.
#include <catch.hpp>
+using Catch::Equals;
+
TEST_CASE("util::base_name")
{
CHECK(util::base_name("") == "");
}
}
+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");