summaryrefslogtreecommitdiffstats
path: root/src/StringUtils.h
diff options
context:
space:
mode:
authorTycho <work.tycho+git@gmail.com>2014-08-13 14:03:56 +0200
committerTycho <work.tycho+git@gmail.com>2014-08-13 14:03:56 +0200
commit781e1e6264794ec0fddccd37a76f7a78a3fa8b09 (patch)
tree694dabff0db889d7326e2cb3e7febae4271b788f /src/StringUtils.h
parentFixed type issues in CraftingRecipe.cpp (diff)
downloadcuberite-781e1e6264794ec0fddccd37a76f7a78a3fa8b09.tar
cuberite-781e1e6264794ec0fddccd37a76f7a78a3fa8b09.tar.gz
cuberite-781e1e6264794ec0fddccd37a76f7a78a3fa8b09.tar.bz2
cuberite-781e1e6264794ec0fddccd37a76f7a78a3fa8b09.tar.lz
cuberite-781e1e6264794ec0fddccd37a76f7a78a3fa8b09.tar.xz
cuberite-781e1e6264794ec0fddccd37a76f7a78a3fa8b09.tar.zst
cuberite-781e1e6264794ec0fddccd37a76f7a78a3fa8b09.zip
Diffstat (limited to '')
-rw-r--r--src/StringUtils.h62
1 files changed, 62 insertions, 0 deletions
diff --git a/src/StringUtils.h b/src/StringUtils.h
index 142aaf59b..56ac83942 100644
--- a/src/StringUtils.h
+++ b/src/StringUtils.h
@@ -99,6 +99,68 @@ extern int GetBEInt(const char * a_Mem);
/// Writes four bytes to the specified memory location so that they interpret as BigEndian int
extern void SetBEInt(char * a_Mem, Int32 a_Value);
+/// Parses any integer type. Checks bounds and
+template<class T>
+bool StringToInteger(AString a_str, T& a_Num)
+{
+ size_t i = 0;
+ T positive = true;
+ T result = 0;
+ if (a_str[0] == '+')
+ {
+ i++;
+ }
+ else if (a_str[0] == '-')
+ {
+ i++;
+ positive = false;
+ }
+ if (positive)
+ {
+ for(; i <= a_str.size(); i++)
+ {
+ if ((a_str[i] <= '0') || (a_str[i] >= '9'))
+ {
+ return false;
+ }
+ if (std::numeric_limits<T>::max() / 10 < result)
+ {
+ return false;
+ }
+ result *= 10;
+ T digit = a_str[i] - '0';
+ if (std::numeric_limits<T>::max() - digit < result)
+ {
+ return false;
+ }
+ result += digit;
+ }
+ }
+ else
+ {
+ for(; i <= a_str.size(); i++)
+ {
+ if ((a_str[i] <= '0') || (a_str[i] >= '9'))
+ {
+ return false;
+ }
+ if (std::numeric_limits<T>::min() / 10 > result)
+ {
+ return false;
+ }
+ result *= 10;
+ T digit = a_str[i] - '0';
+ if (std::numeric_limits<T>::min() + digit > result)
+ {
+ return false;
+ }
+ result -= digit;
+ }
+ }
+ a_Num = result;
+ return true;
+}
+
// If you have any other string helper functions, declare them here