|
10 | 10 | #include "cgimap/options.hpp"
|
11 | 11 | #include "cgimap/logger.hpp"
|
12 | 12 |
|
| 13 | +#include <string_view> |
| 14 | +#include <ranges> |
13 | 15 |
|
14 | 16 | global_settings_base::~global_settings_base() = default;
|
15 | 17 |
|
@@ -193,13 +195,24 @@ void global_settings_via_options::set_bbox_size_limiter_upload(const po::variabl
|
193 | 195 | }
|
194 | 196 | }
|
195 | 197 |
|
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 |
205 | 218 | }
|
0 commit comments