diff options
author | Caolán McNamara <caolanm@redhat.com> | 2011-11-09 22:07:24 +0000 |
---|---|---|
committer | Caolán McNamara <caolanm@redhat.com> | 2011-11-10 09:28:39 +0000 |
commit | 8e482e60c52ba427199a29045711b3f9cc870ac0 (patch) | |
tree | c10d502127e9a097953955ca45c317f30ec1a813 /comphelper | |
parent | 0a2286a7244ca80f8109765f0ff78a7c36e7cd68 (diff) |
an indexOfL would be useful
Diffstat (limited to 'comphelper')
-rw-r--r-- | comphelper/inc/comphelper/string.hxx | 23 | ||||
-rw-r--r-- | comphelper/qa/string/test_string.cxx | 19 |
2 files changed, 42 insertions, 0 deletions
diff --git a/comphelper/inc/comphelper/string.hxx b/comphelper/inc/comphelper/string.hxx index aeec120080c7..4545302b4086 100644 --- a/comphelper/inc/comphelper/string.hxx +++ b/comphelper/inc/comphelper/string.hxx @@ -251,6 +251,29 @@ COMPHELPER_DLLPUBLIC inline sal_Bool matchL(const rtl::OString& rStr, const char rStr.pData->length-fromIndex, pMatch, nMatchLen, nMatchLen ) == 0; } +/** + Returns the index within this string of the first occurrence of the + specified substring, starting at the specified index. + + If str doesn't include any character, always -1 is + returned. This is also the case, if both strings are empty. + + @param rStr The string that pSearch will be searched within. + @param pSearch the substring to search for. + @param nSearchLen the length of pSearch + @param fromIndex the index to start the search from. + @return If the string argument occurs one or more times as a substring + within this string at the starting index, then the index + of the first character of the first such substring is + returned. If it does not occur as a substring starting + at fromIndex or beyond, -1 is returned. +*/ +COMPHELPER_DLLPUBLIC inline sal_Int32 indexOfL(const rtl::OString& rStr, const char *pSearch, sal_Int32 nSearchLen, sal_Int32 fromIndex = 0) SAL_THROW(()) +{ + sal_Int32 ret = rtl_str_indexOfStr_WithLength(rStr.pData->buffer+fromIndex, + rStr.pData->length-fromIndex, pSearch, nSearchLen); + return (ret < 0 ? ret : ret+fromIndex); +} namespace detail { diff --git a/comphelper/qa/string/test_string.cxx b/comphelper/qa/string/test_string.cxx index db6d52ea1128..3e30ad22f440 100644 --- a/comphelper/qa/string/test_string.cxx +++ b/comphelper/qa/string/test_string.cxx @@ -53,6 +53,7 @@ public: void testIsdigitAsciiString(); void testIsalnumAsciiString(); void testIsupperAsciiString(); + void testIndexOfL(); CPPUNIT_TEST_SUITE(TestString); CPPUNIT_TEST(testSearchAndReplaceAsciiL); @@ -63,6 +64,7 @@ public: CPPUNIT_TEST(testIsdigitAsciiString); CPPUNIT_TEST(testIsalnumAsciiString); CPPUNIT_TEST(testIsupperAsciiString); + CPPUNIT_TEST(testIndexOfL); CPPUNIT_TEST_SUITE_END(); }; @@ -147,6 +149,23 @@ void TestString::testIsupperAsciiString() CPPUNIT_ASSERT_EQUAL(comphelper::string::isupperAsciiString(s3), true); } +void TestString::testIndexOfL() +{ + rtl::OString s1(RTL_CONSTASCII_STRINGPARAM("one two three")); + + CPPUNIT_ASSERT_EQUAL(comphelper::string::indexOfL(s1, + RTL_CONSTASCII_STRINGPARAM("one")), 0); + + CPPUNIT_ASSERT_EQUAL(comphelper::string::indexOfL(s1, + RTL_CONSTASCII_STRINGPARAM("two")), 4); + + CPPUNIT_ASSERT_EQUAL(comphelper::string::indexOfL(s1, + RTL_CONSTASCII_STRINGPARAM("four")), -1); + + CPPUNIT_ASSERT_EQUAL(comphelper::string::indexOfL(s1, + RTL_CONSTASCII_STRINGPARAM("two"), 5), -1); +} + using namespace ::com::sun::star; class testCollator : public cppu::WeakImplHelper1< i18n::XCollator > |