Created
July 7, 2013 20:44
-
-
Save kahrl/5944866 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/src/test.cpp b/src/test.cpp | |
index e609fe2..ddbcee7 100644 | |
--- a/src/test.cpp | |
+++ b/src/test.cpp | |
@@ -164,6 +164,10 @@ struct TestUtilities: public TestBase | |
UASSERT(is_yes("YeS") == true); | |
UASSERT(is_yes("") == false); | |
UASSERT(is_yes("FAlse") == false); | |
+ UASSERT(is_yes("-1") == true); | |
+ UASSERT(is_yes("0") == false); | |
+ UASSERT(is_yes("1") == true); | |
+ UASSERT(is_yes("2") == true); | |
const char *ends[] = {"abc", "c", "bc", NULL}; | |
UASSERT(removeStringEnd("abc", ends) == ""); | |
UASSERT(removeStringEnd("bc", ends) == "b"); | |
diff --git a/src/util/string.h b/src/util/string.h | |
index 6c48ade..428b1ad 100644 | |
--- a/src/util/string.h | |
+++ b/src/util/string.h | |
@@ -143,10 +143,23 @@ inline std::string lowercase(const std::string &s) | |
return s2; | |
} | |
+inline bool is_nonzero_number(const std::string &s) | |
+{ | |
+ for(size_t i=0; i<s.size(); i++){ | |
+ if(i == 0 && s[i] == '-') | |
+ continue; | |
+ else if(s[i] >= '1' && s[i] <= '9') | |
+ return true; | |
+ else if(s[i] != '0') | |
+ return false; | |
+ } | |
+ return false; | |
+} | |
+ | |
inline bool is_yes(const std::string &s) | |
{ | |
std::string s2 = lowercase(trim(s)); | |
- if(s2 == "y" || s2 == "yes" || s2 == "true" || s2 == "1") | |
+ if(s2 == "y" || s2 == "yes" || s2 == "true" || is_nonzero_number(s2)) | |
return true; | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment