diff options
Diffstat (limited to 'src/utils')
-rw-r--r-- | src/utils/stringutils.cpp | 14 | ||||
-rw-r--r-- | src/utils/stringutils.h | 2 | ||||
-rw-r--r-- | src/utils/stringutils_unittest.cc | 13 |
3 files changed, 29 insertions, 0 deletions
diff --git a/src/utils/stringutils.cpp b/src/utils/stringutils.cpp index da3f8dd06..a54d9d5ab 100644 --- a/src/utils/stringutils.cpp +++ b/src/utils/stringutils.cpp @@ -697,3 +697,17 @@ std::string toStringPrint(const unsigned int val) str[99] = 0; return str; } + +bool isDigit(const std::string &str) +{ + if (str.empty()) + return false; + const size_t sz = str.size(); + for (size_t f = 0; f < sz; f ++) + { + const char &chr = str[f]; + if (chr < '0' || chr > '9') + return false; + } + return true; +} diff --git a/src/utils/stringutils.h b/src/utils/stringutils.h index 737b5c8f3..7f40b3673 100644 --- a/src/utils/stringutils.h +++ b/src/utils/stringutils.h @@ -230,4 +230,6 @@ std::string encodeLinkText(std::string data); std::string decodeLinkText(std::string data); +bool isDigit(const std::string &str); + #endif // UTILS_STRINGUTILS_H diff --git a/src/utils/stringutils_unittest.cc b/src/utils/stringutils_unittest.cc index 75f4f2f35..8f28f5c10 100644 --- a/src/utils/stringutils_unittest.cc +++ b/src/utils/stringutils_unittest.cc @@ -551,3 +551,16 @@ TEST(stringuntils, decodeLinkText) str = decodeLinkText(str); EXPECT_EQ(str, "test|line"); } + +TEST(stringuntils, isDigit) +{ + std::string str; + + EXPECT_FALSE(isDigit("")); + EXPECT_TRUE(isDigit("1")); + EXPECT_TRUE(isDigit("123")); + EXPECT_FALSE(isDigit("+123")); + EXPECT_FALSE(isDigit("-123")); + EXPECT_FALSE(isDigit("1.23")); + EXPECT_FALSE(isDigit("12-34")); +} |