Skip to content
Browse files

Merge pull request #803 from jmarshallnz/stringutils_fixes

IsInteger/IsNaturalNumber should ignore whitespace
  • Loading branch information...
2 parents cbd97ea + d9b04d1 commit 5f8ba69b449c572dcffd4f7a62bd25019edd9101 @jmarshallnz jmarshallnz committed Mar 31, 2012
Showing with 66 additions and 9 deletions.
  1. +53 −9 xbmc/utils/StringUtils.cpp
  2. +13 −0 xbmc/utils/StringUtils.h
View
62 xbmc/utils/StringUtils.cpp
@@ -296,21 +296,65 @@ CStdString StringUtils::SecondsToTimeString(long lSeconds, TIME_FORMAT format)
bool StringUtils::IsNaturalNumber(const CStdString& str)
{
- if (0 == (int)str.size())
- return false;
- for (int i = 0; i < (int)str.size(); i++)
+ size_t i = 0, n = 0;
+ // allow whitespace,digits,whitespace
+ while (i < str.size() && isspace(str[i]))
+ i++;
+ while (i < str.size() && isdigit(str[i]))
{
- if ((str[i] < '0') || (str[i] > '9')) return false;
+ i++; n++;
}
- return true;
+ while (i < str.size() && isspace(str[i]))
+ i++;
+ return i == str.size() && n > 0;
}
bool StringUtils::IsInteger(const CStdString& str)
{
- if (str.size() > 0 && str[0] == '-')
- return IsNaturalNumber(str.Mid(1));
- else
- return IsNaturalNumber(str);
+ size_t i = 0, n = 0;
+ // allow whitespace,-,digits,whitespace
+ while (i < str.size() && isspace(str[i]))
+ i++;
+ if (i < str.size() && str[i] == '-')
+ i++;
+ while (i < str.size() && isdigit(str[i]))
+ {
+ i++; n++;
+ }
+ while (i < str.size() && isspace(str[i]))
+ i++;
+ return i == str.size() && n > 0;
+}
+
+bool StringUtils::Test()
+{
+ bool ret = true;
+
+ ret |= IsNaturalNumber("10");
+ ret |= IsNaturalNumber(" 10");
+ ret |= IsNaturalNumber("0");
+ ret |= !IsNaturalNumber(" 1 0");
+ ret |= !IsNaturalNumber("1.0");
+ ret |= !IsNaturalNumber("1.1");
+ ret |= !IsNaturalNumber("0x1");
+ ret |= !IsNaturalNumber("blah");
+ ret |= !IsNaturalNumber("120 h");
+ ret |= !IsNaturalNumber(" ");
+ ret |= !IsNaturalNumber("");
+
+ ret |= IsInteger("10");
+ ret |= IsInteger(" -10");
+ ret |= IsInteger("0");
+ ret |= !IsInteger(" 1 0");
+ ret |= !IsInteger("1.0");
+ ret |= !IsInteger("1.1");
+ ret |= !IsInteger("0x1");
+ ret |= !IsInteger("blah");
+ ret |= !IsInteger("120 h");
+ ret |= !IsInteger(" ");
+ ret |= !IsInteger("");
+
+ return ret;
}
void StringUtils::RemoveCRLF(CStdString& strLine)
View
13 xbmc/utils/StringUtils.h
@@ -59,7 +59,18 @@ class StringUtils
*/
static CStdString SecondsToTimeString(long seconds, TIME_FORMAT format = TIME_FORMAT_GUESS);
+ /*! \brief check whether a string is a natural number.
+ Matches [ \t]*[0-9]+[ \t]*
+ \param str the string to check
+ \return true if the string is a natural number, false otherwise.
+ */
static bool IsNaturalNumber(const CStdString& str);
+
+ /*! \brief check whether a string is an integer.
+ Matches [ \t]*[\-]*[0-9]+[ \t]*
+ \param str the string to check
+ \return true if the string is an integer, false otherwise.
+ */
static bool IsInteger(const CStdString& str);
static CStdString SizeToString(int64_t size);
static const CStdString EmptyString;
@@ -71,6 +82,8 @@ class StringUtils
static bool ValidateUUID(const CStdString &uuid); // NB only validates syntax
static double CompareFuzzy(const CStdString &left, const CStdString &right);
static int FindBestMatch(const CStdString &str, const CStdStringArray &strings, double &matchscore);
+
+ static bool Test();
private:
static CStdString m_lastUUID;
};

0 comments on commit 5f8ba69

Please sign in to comment.
Something went wrong with that request. Please try again.