From 502d597aebc3e122bc9b6065d2eccd6f2c27c664 Mon Sep 17 00:00:00 2001 From: Misa Date: Thu, 6 Aug 2020 21:22:10 -0700 Subject: [PATCH] Add is_number() This will be used to check if a string is a valid integer. So negative numbers are accepted, unlike is_positive_num(). --- desktop_version/src/UtilityClass.cpp | 12 ++++++++++++ desktop_version/src/UtilityClass.h | 2 ++ 2 files changed, 14 insertions(+) diff --git a/desktop_version/src/UtilityClass.cpp b/desktop_version/src/UtilityClass.cpp index 9ff685f6..8d27de2a 100644 --- a/desktop_version/src/UtilityClass.cpp +++ b/desktop_version/src/UtilityClass.cpp @@ -205,6 +205,18 @@ void UtilityClass::updateglow() } } +bool is_number(const char* str) +{ + for (int i = 0; str[i] != '\0'; i++) + { + if (!SDL_isdigit(static_cast(str[i])) && (i != 0 || str[0] != '-')) + { + return false; + } + } + return true; +} + bool is_positive_num(const std::string& str, bool hex) { for (size_t i = 0; i < str.length(); i++) diff --git a/desktop_version/src/UtilityClass.h b/desktop_version/src/UtilityClass.h index 42558fe1..923f8d64 100644 --- a/desktop_version/src/UtilityClass.h +++ b/desktop_version/src/UtilityClass.h @@ -11,6 +11,8 @@ std::vector split(const std::string &s, char delim, std::vector split(const std::string &s, char delim); +bool is_number(const char* str); + bool is_positive_num(const std::string& str, bool hex); bool endsWith(const std::string& str, const std::string& suffix);