From 5214ed3af1b3fc640364176b7b8dcb20eda8fb25 Mon Sep 17 00:00:00 2001 From: Quinten Mennen Date: Wed, 24 Sep 2025 13:00:45 +0200 Subject: [PATCH] feat(utils): utils namespace and custom stoul --- webserv/config/utils.cpp | 19 +++++++++++++++++-- webserv/config/utils.hpp | 6 +++++- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/webserv/config/utils.cpp b/webserv/config/utils.cpp index 74e3f82..98ee024 100644 --- a/webserv/config/utils.cpp +++ b/webserv/config/utils.cpp @@ -1,7 +1,21 @@ #include +#include #include +namespace utils +{ +size_t stoul(const std::string &str, int base) +{ + size_t idx = 0; + unsigned long value = std::stoul(str, &idx, base); + if (idx != str.length()) + { + throw std::invalid_argument("Invalid characters in number: " + str); + } + return value; +} + std::string trim(const std::string &str) { size_t first = str.find_first_not_of(" \t\n\r"); @@ -48,5 +62,6 @@ size_t findCorrespondingClosingBrace(const std::string &str, size_t openPos) return i; } } - return std::string::npos; // No matching closing brace found -} \ No newline at end of file + return std::string::npos; +} +} // namespace utils diff --git a/webserv/config/utils.hpp b/webserv/config/utils.hpp index 7bf158f..34cf081 100644 --- a/webserv/config/utils.hpp +++ b/webserv/config/utils.hpp @@ -4,6 +4,10 @@ #include // for size_t +namespace utils +{ +size_t stoul(const std::string &str, int base = 10); std::string trimSemi(const std::string &str); std::string trim(const std::string &str); -size_t findCorrespondingClosingBrace(const std::string &str, size_t openPos); \ No newline at end of file +size_t findCorrespondingClosingBrace(const std::string &str, size_t openPos); +} // namespace utils