Permalink
Browse files

Merge pull request #1289 from Karlson2k/String_Utils_Fixes

StringUtils fixes
  • Loading branch information...
2 parents cabcd97 + 2e02493 commit f18393b9dd8a4ca691a1bc473754b792b0b705c2 @jmarshallnz jmarshallnz committed Oct 13, 2012
Showing with 25 additions and 15 deletions.
  1. +13 −10 xbmc/utils/StringUtils.cpp
  2. +12 −5 xbmc/utils/test/TestStringUtils.cpp
View
23 xbmc/utils/StringUtils.cpp
@@ -269,7 +269,7 @@ int StringUtils::SplitString(const CStdString& input, const CStdString& delimite
newPos = input.Find (delimiter, iPos + sizeS2);
}
- // numFound is the number of delimeters which is one less
+ // numFound is the number of delimiters which is one less
// than the number of substrings
unsigned int numFound = positions.size();
if (iMaxStrings > 0 && numFound >= iMaxStrings)
@@ -322,7 +322,7 @@ vector<string> StringUtils::Split(const CStdString& input, const CStdString& del
return strArray;
}
-// returns the number of occurences of strFind in strInput.
+// returns the number of occurrences of strFind in strInput.
int StringUtils::FindNumber(const CStdString& strInput, const CStdString &strFind)
{
int pos = strInput.Find(strFind, 0);
@@ -417,17 +417,20 @@ int StringUtils::DateStringToYYYYMMDD(const CStdString &dateString)
long StringUtils::TimeStringToSeconds(const CStdString &timeString)
{
- if(timeString.Right(4).Equals(" min"))
+ CStdString strCopy(timeString);
+ strCopy.TrimLeft(" \n\r\t");
+ strCopy.TrimRight(" \n\r\t");
+ if(strCopy.Right(4).Equals(" min"))
{
// this is imdb format of "XXX min"
- return 60 * atoi(timeString.c_str());
+ return 60 * atoi(strCopy.c_str());
}
else
{
CStdStringArray secs;
- StringUtils::SplitString(timeString, ":", secs);
+ StringUtils::SplitString(strCopy, ":", secs);
int timeInSecs = 0;
- for (unsigned int i = 0; i < secs.size(); i++)
+ for (unsigned int i = 0; i < 3 && i < secs.size(); i++)
{
timeInSecs *= 60;
timeInSecs += atoi(secs[i]);
@@ -704,8 +707,8 @@ size_t StringUtils::utf8_strlen(const char *s)
size_t length = 0;
while (*s)
{
- if ((*s++ & 0xC0) != 0x80)
- length++;
- }
- return length;
+ if ((*s++ & 0xC0) != 0x80)
+ length++;
+ }
+ return length;
}
View
17 xbmc/utils/test/TestStringUtils.cpp
@@ -303,11 +303,18 @@ TEST(TestStringUtils, AlphaNumericCompare)
TEST(TestStringUtils, TimeStringToSeconds)
{
- long ref, var;
-
- ref = 77455;
- var = StringUtils::TimeStringToSeconds("21:30:55");
- EXPECT_EQ(ref, var);
+ EXPECT_EQ(77455, StringUtils::TimeStringToSeconds("21:30:55"));
+ EXPECT_EQ(7*60, StringUtils::TimeStringToSeconds("7 min"));
+ EXPECT_EQ(7*60, StringUtils::TimeStringToSeconds("7 min\t"));
+ EXPECT_EQ(154*60, StringUtils::TimeStringToSeconds(" 154 min"));
+ EXPECT_EQ(1*60+1, StringUtils::TimeStringToSeconds("1:01"));
+ EXPECT_EQ(4*60+3, StringUtils::TimeStringToSeconds("4:03"));
+ EXPECT_EQ(2*3600+4*60+3, StringUtils::TimeStringToSeconds("2:04:03"));
+ EXPECT_EQ(2*3600+4*60+3, StringUtils::TimeStringToSeconds(" 2:4:3"));
+ EXPECT_EQ(2*3600+4*60+3, StringUtils::TimeStringToSeconds(" \t\t 02:04:03 \n "));
+ EXPECT_EQ(1*3600+5*60+2, StringUtils::TimeStringToSeconds("01:05:02:04:03 \n "));
+ EXPECT_EQ(0, StringUtils::TimeStringToSeconds("blah"));
+ EXPECT_EQ(0, StringUtils::TimeStringToSeconds("ля-ля"));
}
TEST(TestStringUtils, RemoveCRLF)

0 comments on commit f18393b

Please sign in to comment.