#endif
}
-unsigned
-parse_duration_with_suffix_to_seconds(const std::string& value)
+uint32_t
+parse_duration(const std::string& duration)
{
- size_t end;
- long result;
- bool failed = false;
-
- try {
- result = std::stol(value, &end, 10);
- } catch (std::exception&) {
- failed = true;
- }
+ unsigned factor = 0;
+ char last_ch = duration.empty() ? '\0' : duration[duration.length() - 1];
- if (failed || result < 0) {
- throw Error(fmt::format("invalid unsigned integer: \"{}\"", value));
- }
-
- if (end + 1 != value.size()) {
- throw Error(
- fmt::format("Invalid suffix, Supported: d(ay)/s(econd): \"{}\"", value));
- }
-
- switch (value[end]) {
+ switch (last_ch) {
case 'd':
- result *= 24 * 3600;
+ factor = 24 * 60 * 60;
+ break;
case 's':
+ factor = 1;
break;
default:
- throw Error(
- fmt::format("Invalid suffix, Supported: d(ay)/s(econd): \"{}\"", value));
+ throw Error(fmt::format(
+ "invalid suffix (supported: d (day) and s (second)): \"{}\"", duration));
}
- return result;
+
+ const size_t end = factor == 0 ? duration.length() : duration.length() - 1;
+ return factor * parse_uint32(duration.substr(0, end));
}
int
// On Windows: Backslashes are replaced with forward slashes.
std::string normalize_absolute_path(nonstd::string_view path);
-// Parse the given string into an unsigned integer. Then based on suffix
-// provided convert the number to seconds possible suffixes = d(ays)/s(econds)
-//
-// Throws `Error` for any other suffix
-// Throws `Error` if parse value is <0
-unsigned parse_duration_with_suffix_to_seconds(const std::string& value);
+// Parse `duration`, an unsigned 32-bit integer with d (days) or s (seconds)
+// suffix, into seconds. Throws `Error` on error.
+uint32_t parse_duration(const std::string& duration);
// Parse a string into a signed integer.
//
}
case EVICT_OLDER_THAN: {
- unsigned seconds = Util::parse_duration_with_suffix_to_seconds(optarg);
- ProgressBar progress_bar("Clearing ...");
+ auto seconds = Util::parse_duration(optarg);
+ ProgressBar progress_bar("Evicting ...");
clean_old(
ctx, [&](double progress) { progress_bar.update(progress); }, seconds);
if (isatty(STDOUT_FILENO)) {
$CCACHE -c >/dev/null
expect_file_count 1 '.nfs*' $CCACHE_DIR
expect_stat 'files in cache' 10
+
# -------------------------------------------------------------------------
- TEST "cleanup of old files by age"
+ TEST "Cleanup of old files by age"
prepare_cleanup_test_dir $CCACHE_DIR/a
touch $CCACHE_DIR/a/now.result
#endif
}
-TEST_CASE("Util::parse_duration_with_suffix_to_seconds")
-{
- CHECK(Util::parse_duration_with_suffix_to_seconds("0s") == 0);
- CHECK(Util::parse_duration_with_suffix_to_seconds("2s") == 2);
- CHECK(Util::parse_duration_with_suffix_to_seconds("1d") == 3600 * 24);
- CHECK(Util::parse_duration_with_suffix_to_seconds("2d") == 2 * 3600 * 24);
- CHECK_THROWS_WITH(Util::parse_duration_with_suffix_to_seconds("-2"),
- "invalid unsigned integer: \"-2\"");
- CHECK_THROWS_WITH(Util::parse_duration_with_suffix_to_seconds("2x"),
- "Invalid suffix, Supported: d(ay)/s(econd): \"2x\"");
- CHECK_THROWS_WITH(Util::parse_duration_with_suffix_to_seconds("2"),
- "Invalid suffix, Supported: d(ay)/s(econd): \"2\"");
+TEST_CASE("Util::parse_duration")
+{
+ CHECK(Util::parse_duration("0s") == 0);
+ CHECK(Util::parse_duration("2s") == 2);
+ CHECK(Util::parse_duration("1d") == 3600 * 24);
+ CHECK(Util::parse_duration("2d") == 2 * 3600 * 24);
+ CHECK_THROWS_WITH(
+ Util::parse_duration("-2"),
+ "invalid suffix (supported: d (day) and s (second)): \"-2\"");
+ CHECK_THROWS_WITH(
+ Util::parse_duration("2x"),
+ "invalid suffix (supported: d (day) and s (second)): \"2x\"");
+ CHECK_THROWS_WITH(
+ Util::parse_duration("2"),
+ "invalid suffix (supported: d (day) and s (second)): \"2\"");
}
TEST_CASE("Util::parse_int")