Skip to content

Commit 42986f1

Browse files
authored
Merge pull request #475 from mmd-osm/patch/regex1
options.cpp: replace interval regex by simple parser
2 parents cbe474f + 739e077 commit 42986f1

File tree

3 files changed

+22
-11
lines changed

3 files changed

+22
-11
lines changed

include/cgimap/options.hpp

-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212

1313
#include <memory>
1414
#include <optional>
15-
#include <regex>
1615
#include <boost/program_options.hpp>
1716

1817
namespace po = boost::program_options;

src/http.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
#include <cctype> // for toupper, isxdigit
1818
#include <cstdlib>
1919
#include <ranges>
20-
#include <regex>
2120
#include <sstream>
2221
#include <string_view>
2322

src/options.cpp

+22-9
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
#include "cgimap/options.hpp"
1111
#include "cgimap/logger.hpp"
1212

13+
#include <string_view>
14+
#include <ranges>
1315

1416
global_settings_base::~global_settings_base() = default;
1517

@@ -193,13 +195,24 @@ void global_settings_via_options::set_bbox_size_limiter_upload(const po::variabl
193195
}
194196
}
195197

196-
bool global_settings_via_options::validate_timeout(const std::string &timeout) const {
197-
std::smatch sm;
198-
try {
199-
std::regex r("[0-9]+ (day|hour|minute|second)s?");
200-
if (std::regex_match(timeout, sm, r)) {
201-
return true;
202-
}
203-
} catch (std::regex_error &) {}
204-
return false;
198+
/// @brief Simplified parser for Postgresql interval format
199+
/// @param timeout The format is a number followed by a space and a unit
200+
/// (day, days, hour, hours, minute, minutes, second, seconds).
201+
/// @return true, if the timeout value is valid, false otherwise
202+
bool global_settings_via_options::validate_timeout( const std::string &timeout) const {
203+
204+
constexpr std::array valid_units = {
205+
"day", "days", "hour", "hours", "minute", "minutes", "second", "seconds"
206+
};
207+
208+
std::vector<std::string_view> v;
209+
// Split input string into a number and time unit
210+
for (auto parts = std::ranges::views::split(timeout, ' ');
211+
auto &&part : parts) {
212+
v.emplace_back(&*part.begin(), std::ranges::distance(part));
213+
}
214+
215+
return (v.size() == 2 &&
216+
std::ranges::all_of(v[0], ::isdigit) && // check if first part is a number
217+
std::ranges::find(valid_units, v[1]) != valid_units.end()); // check if second part is a valid unit
205218
}

0 commit comments

Comments
 (0)