]> granicus.if.org Git - taglib/commitdiff
Add String:rfind
authorLukáš Lalinský <lalinsky@gmail.com>
Wed, 29 Apr 2009 15:57:05 +0000 (15:57 +0000)
committerLukáš Lalinský <lalinsky@gmail.com>
Wed, 29 Apr 2009 15:57:05 +0000 (15:57 +0000)
git-svn-id: svn://anonsvn.kde.org/home/kde/trunk/kdesupport/taglib@961188 283d02a7-25f6-0310-bc7c-ecb5cbfe19da

taglib/toolkit/tstring.cpp
taglib/toolkit/tstring.h
tests/test_string.cpp

index fa93c4cf37a05d11a36ff13c02fbdac658248bf5..755a877050074d1f5f4e4c56f30efb0de7037ab9 100644 (file)
@@ -293,6 +293,17 @@ int String::find(const String &s, int offset) const
     return -1;
 }
 
+int String::rfind(const String &s, int offset) const
+{
+  wstring::size_type position =
+    d->data.rfind(s.d->data, offset == -1 ? wstring::npos : offset);
+
+  if(position != wstring::npos)
+    return position;
+  else
+    return -1;
+}
+
 bool String::startsWith(const String &s) const
 {
   if(s.length() > length())
index 660023badae5bc8da0fb63f63550320de27b3441..cd0474e28be54ead4f1b9c3c68f6c4f27c69e139 100644 (file)
@@ -225,6 +225,13 @@ namespace TagLib {
      */
     int find(const String &s, int offset = 0) const;
 
+    /*!
+     * Finds the last occurrence of pattern \a s in this string, searched backwards,
+     * either from the end of the string or starting from \a offset. If the pattern
+     * is not found, -1 is returned.
+     */
+    int rfind(const String &s, int offset = -1) const;
+
     /*!
      * Returns true if the strings starts with the substring \a s.
      */
index c3b42ea4101b5e8da4468b27ea64f4206fed03d5..f677d059fca16dc8611657030fea0ef65ba27515 100644 (file)
@@ -33,6 +33,7 @@ class TestString : public CppUnit::TestFixture
 {
   CPPUNIT_TEST_SUITE(TestString);
   CPPUNIT_TEST(testString);
+  CPPUNIT_TEST(testRfind);
   CPPUNIT_TEST(testUTF16Encode);
   CPPUNIT_TEST(testUTF16Decode);
   CPPUNIT_TEST(testUTF16DecodeInvalidBOM);
@@ -151,6 +152,19 @@ public:
     CPPUNIT_ASSERT_EQUAL(String("a"), b);
   }
 
+  void testRfind()
+  {
+    CPPUNIT_ASSERT_EQUAL(-1, String("foo.bar").rfind(".", 0));
+    CPPUNIT_ASSERT_EQUAL(-1, String("foo.bar").rfind(".", 1));
+    CPPUNIT_ASSERT_EQUAL(-1, String("foo.bar").rfind(".", 2));
+    CPPUNIT_ASSERT_EQUAL(3, String("foo.bar").rfind(".", 3));
+    CPPUNIT_ASSERT_EQUAL(3, String("foo.bar").rfind(".", 4));
+    CPPUNIT_ASSERT_EQUAL(3, String("foo.bar").rfind(".", 5));
+    CPPUNIT_ASSERT_EQUAL(3, String("foo.bar").rfind(".", 6));
+    CPPUNIT_ASSERT_EQUAL(3, String("foo.bar").rfind(".", 7));
+    CPPUNIT_ASSERT_EQUAL(3, String("foo.bar").rfind("."));
+  }
+
 };
 
 CPPUNIT_TEST_SUITE_REGISTRATION(TestString);