From 9734e7fbe04dbf938e268ac8172ad36344d235b8 Mon Sep 17 00:00:00 2001 From: Riyyi Date: Mon, 1 Feb 2021 03:55:19 +0100 Subject: [PATCH] Add std::stou, cant believe its not in the STL --- inferno/src/inferno/util/string.h | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 inferno/src/inferno/util/string.h diff --git a/inferno/src/inferno/util/string.h b/inferno/src/inferno/util/string.h new file mode 100644 index 0000000..d179fea --- /dev/null +++ b/inferno/src/inferno/util/string.h @@ -0,0 +1,28 @@ +#ifndef UTIL_STRING_H +#define UTIL_STRING_H + +#include // std::numeric_limits +#include // std::string, std::stoul + +#include "inferno/assert.h" + +namespace std { + + // Can't believe this is not in the standard library + + inline uint32_t stou(const char* string) + { + unsigned long size = std::stoul(string); + ASSERT(size <= std::numeric_limits::max(), "String util not in uint32_t range '{}'", string); + return static_cast(size); + } + + inline uint32_t stou(const std::string& string) + { + return stou(string.c_str()); + } + +} // namespace std + + +#endif // UTIL_STRING_H