feat(utils): utils namespace and custom stoul

This commit is contained in:
Quinten Mennen 2025-09-24 13:00:45 +02:00
parent 04e8292009
commit 5214ed3af1
2 changed files with 22 additions and 3 deletions

View File

@ -1,7 +1,21 @@
#include <webserv/config/utils.hpp>
#include <stdexcept>
#include <string>
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
}
return std::string::npos;
}
} // namespace utils

View File

@ -4,6 +4,10 @@
#include <stddef.h> // 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);
size_t findCorrespondingClosingBrace(const std::string &str, size_t openPos);
} // namespace utils