]> granicus.if.org Git - taglib/commitdiff
Fix upgrading of ID3v2.3 genre with number 0 (Blues)
authorLukáš Lalinský <lalinsky@gmail.com>
Mon, 12 Apr 2010 18:27:59 +0000 (18:27 +0000)
committerLukáš Lalinský <lalinsky@gmail.com>
Mon, 12 Apr 2010 18:27:59 +0000 (18:27 +0000)
git-svn-id: svn://anonsvn.kde.org/home/kde/trunk/kdesupport/taglib@1114089 283d02a7-25f6-0310-bc7c-ecb5cbfe19da

taglib/mpeg/id3v2/id3v2framefactory.cpp
taglib/mpeg/id3v2/id3v2tag.cpp
taglib/toolkit/tstring.cpp
taglib/toolkit/tstring.h
tests/test_string.cpp

index 1d39a713d4d5db6a5ecd45859e826cac0ecca436..323e809121ed1a83509fdff81c8017d43a36c9f8 100644 (file)
@@ -412,10 +412,11 @@ void FrameFactory::updateGenre(TextIdentificationFrame *frame) const
     if(s.startsWith("(") && end > 0) {
       // "(12)Genre"
       String text = s.substr(end + 1);
-      int number = s.substr(1, end - 1).toInt();
-      if (number > 0 && number <= 255 && !(ID3v1::genre(number) == text))
+      bool ok;
+      int number = s.substr(1, end - 1).toInt(&ok);
+      if(ok && number >= 0 && number <= 255 && !(ID3v1::genre(number) == text))
         newfields.append(s.substr(1, end - 1));
-      if (!text.isEmpty())
+      if(!text.isEmpty())
         newfields.append(text);
     }
     else {
index 1d4e8d4cfff792cb25d4408d77572c952a9f3f31..2ef0027005fcb6d5ff878156c2b0f65ed4e61e09 100644 (file)
@@ -164,19 +164,10 @@ String ID3v2::Tag::genre() const
     if((*it).isEmpty())
       continue;
 
-    bool isNumber = true;
-
-    for(String::ConstIterator charIt = (*it).begin();
-        isNumber && charIt != (*it).end();
-        ++charIt)
-    {
-      isNumber = *charIt >= '0' && *charIt <= '9';
-    }
-
-    if(isNumber) {
-      int number = (*it).toInt();
-      if(number >= 0 && number <= 255)
-        *it = ID3v1::genre(number);
+    bool ok;
+    int number = (*it).toInt(&ok);
+    if(ok && number >= 0 && number <= 255) {
+      *it = ID3v1::genre(number);
     }
 
     if(std::find(genres.begin(), genres.end(), *it) == genres.end())
index 755a877050074d1f5f4e4c56f30efb0de7037ab9..876575b6d4f18f3fcb585c96d9f23cfc1c18c6e7 100644 (file)
@@ -431,18 +431,28 @@ ByteVector String::data(Type t) const
 }
 
 int String::toInt() const
+{
+  return toInt(0);
+}
+
+int String::toInt(bool *ok) const
 {
   int value = 0;
 
-  bool negative = d->data[0] == '-';
-  uint i = negative ? 1 : 0;
+  uint size = d->data.size();
+  bool negative = size > 0 && d->data[0] == '-';
+  uint start = negative ? 1 : 0;
+  uint i = start;
 
-  for(; i < d->data.size() && d->data[i] >= '0' && d->data[i] <= '9'; i++)
+  for(; i < size && d->data[i] >= '0' && d->data[i] <= '9'; i++)
     value = value * 10 + (d->data[i] - '0');
 
   if(negative)
     value = value * -1;
 
+  if(ok)
+    *ok = (size > start && i == size);
+
   return value;
 }
 
index 9289f7da86dc975d8300d9d91f594aa13741980e..6534b923ac3fb94f416055a2200a52a91754dd81 100644 (file)
@@ -291,9 +291,21 @@ namespace TagLib {
 
     /*!
      * Convert the string to an integer.
+     *
+     * Returns the integer if the conversion was successfull or 0 if the
+     * string does not represent a number.
      */
     int toInt() const;
 
+    /*!
+     * Convert the string to an integer.
+     *
+     * If the conversion was successfull, it sets the value of \a *ok to
+     * true and returns the integer. Otherwise it sets \a *ok to false
+     * and the result is undefined.
+     */
+    int toInt(bool *ok) const;
+
     /*!
      * Returns a string with the leading and trailing whitespace stripped.
      */
index f677d059fca16dc8611657030fea0ef65ba27515..b6ff972e0c44edbfef86f71252e50834ad2dd0bd 100644 (file)
@@ -40,6 +40,7 @@ class TestString : public CppUnit::TestFixture
   CPPUNIT_TEST(testUTF16DecodeEmptyWithBOM);
   CPPUNIT_TEST(testAppendCharDetach);
   CPPUNIT_TEST(testAppendStringDetach);
+  CPPUNIT_TEST(testToInt);
   CPPUNIT_TEST_SUITE_END();
 
 public:
@@ -165,6 +166,33 @@ public:
     CPPUNIT_ASSERT_EQUAL(3, String("foo.bar").rfind("."));
   }
 
+  void testToInt()
+  {
+    bool ok;
+    CPPUNIT_ASSERT_EQUAL(String("123").toInt(&ok), 123);
+    CPPUNIT_ASSERT_EQUAL(ok, true);
+
+    CPPUNIT_ASSERT_EQUAL(String("-123").toInt(&ok), -123);
+    CPPUNIT_ASSERT_EQUAL(ok, true);
+
+    CPPUNIT_ASSERT_EQUAL(String("abc").toInt(&ok), 0);
+    CPPUNIT_ASSERT_EQUAL(ok, false);
+
+    CPPUNIT_ASSERT_EQUAL(String("1x").toInt(&ok), 1);
+    CPPUNIT_ASSERT_EQUAL(ok, false);
+
+    CPPUNIT_ASSERT_EQUAL(String("").toInt(&ok), 0);
+    CPPUNIT_ASSERT_EQUAL(ok, false);
+
+    CPPUNIT_ASSERT_EQUAL(String("-").toInt(&ok), 0);
+    CPPUNIT_ASSERT_EQUAL(ok, false);
+
+    CPPUNIT_ASSERT_EQUAL(String("123").toInt(), 123);
+    CPPUNIT_ASSERT_EQUAL(String("-123").toInt(), -123);
+    CPPUNIT_ASSERT_EQUAL(String("123aa").toInt(), 123);
+    CPPUNIT_ASSERT_EQUAL(String("-123aa").toInt(), -123);
+  }
+
 };
 
 CPPUNIT_TEST_SUITE_REGISTRATION(TestString);