19 #ifndef IRCCD_STRING_UTIL_HPP
20 #define IRCCD_STRING_UTIL_HPP
27 #include "sysconfig.hpp"
30 #include <initializer_list>
36 #include <string_view>
37 #include <type_traits>
38 #include <unordered_map>
46 namespace string_util {
75 return static_cast<subst_flags>(
static_cast<unsigned>(v1) ^
static_cast<unsigned>(v2));
87 return static_cast<subst_flags>(
static_cast<unsigned>(v1) &
static_cast<unsigned>(v2));
99 return static_cast<subst_flags>(
static_cast<unsigned>(v1) |
static_cast<unsigned>(v2));
110 return static_cast<subst_flags>(~static_cast<unsigned>(v));
171 std::time_t
time{std::time(
nullptr)};
176 std::unordered_map<std::string, std::string>
keywords;
237 auto format(std::string text,
const subst& params = {}) -> std::string;
249 auto strip(std::string str) noexcept -> std::string;
263 auto split(std::string_view list,
const std::string& delimiters,
int max = -1) -> std::vector<std::string>;
277 template <
typename InputIt,
typename DelimType =
char>
278 auto join(InputIt first, InputIt last, DelimType delim =
':') -> std::string
280 std::ostringstream oss;
285 while (++first != last)
286 oss << delim << *first;
299 template <
typename Container,
typename DelimType =
char>
300 auto join(
const Container& c, DelimType delim =
':') -> std::string
302 return join(c.begin(), c.end(), delim);
312 template <
typename T,
typename DelimType =
char>
313 auto join(std::initializer_list<T> list, DelimType delim =
':') -> std::string
315 return join(list.begin(), list.end(), delim);
355 template <
typename T =
int>
357 T min = std::numeric_limits<T>::min(),
358 T max = std::numeric_limits<T>::max()) noexcept ->
std::optional<T>
360 static_assert(std::is_signed<T>::value,
"must be signed");
363 auto v = std::strtoll(str.c_str(), &end, 10);
365 if (*end !=
'\0' || v < min || v > max)
368 return static_cast<T
>(v);
384 template <
typename T =
unsigned>
386 T min = std::numeric_limits<T>::min(),
387 T max = std::numeric_limits<T>::max()) noexcept ->
std::optional<T>
389 static_assert(std::is_unsigned<T>::value,
"must be unsigned");
392 auto v = std::strtoull(str.c_str(), &end, 10);
394 if (*end !=
'\0' || v < min || v > max)
397 return static_cast<T
>(v);
406 #endif // !IRCCD_STRING_UTIL_HPP