diff options
author | Luboš Luňák <l.lunak@suse.cz> | 2012-09-06 13:51:21 +0200 |
---|---|---|
committer | Luboš Luňák <l.lunak@suse.cz> | 2012-09-06 13:51:21 +0200 |
commit | 095ffaf5efef5e8fdc4d7dfa8cd90fff7b768661 (patch) | |
tree | 55c50cd9e38b447df614e25948c82e5bb042938b /sal | |
parent | 5c804dce946a928adcaf97533f5345b33e688ff5 (diff) |
add O(U)String::startsWith() to complement endsWith()
There's match(), with the second argument defaulting to 0, which
does the same, but that's pretty non-obvious.
Change-Id: Idd4de9388f53e1b5dc5d48446d1001af32c0af3d
Diffstat (limited to 'sal')
-rw-r--r-- | sal/CppunitTest_sal_rtl_strings.mk | 1 | ||||
-rw-r--r-- | sal/inc/rtl/string.hxx | 26 | ||||
-rw-r--r-- | sal/inc/rtl/ustring.hxx | 59 | ||||
-rw-r--r-- | sal/qa/rtl/strings/test_ostring_stringliterals.cxx | 5 | ||||
-rw-r--r-- | sal/qa/rtl/strings/test_oustring_startswith.cxx | 63 | ||||
-rw-r--r-- | sal/qa/rtl/strings/test_oustring_stringliterals.cxx | 2 |
6 files changed, 156 insertions, 0 deletions
diff --git a/sal/CppunitTest_sal_rtl_strings.mk b/sal/CppunitTest_sal_rtl_strings.mk index b93d644b80d8..2762133e24f3 100644 --- a/sal/CppunitTest_sal_rtl_strings.mk +++ b/sal/CppunitTest_sal_rtl_strings.mk @@ -34,6 +34,7 @@ $(eval $(call gb_CppunitTest_add_exception_objects,sal_rtl_strings,\ sal/qa/rtl/strings/test_oustring_convert \ sal/qa/rtl/strings/test_oustring_endswith \ sal/qa/rtl/strings/test_oustring_noadditional \ + sal/qa/rtl/strings/test_oustring_startswith \ sal/qa/rtl/strings/test_oustring_stringliterals \ )) diff --git a/sal/inc/rtl/string.hxx b/sal/inc/rtl/string.hxx index 497e4e3a9dc6..84ab48e05dc6 100644 --- a/sal/inc/rtl/string.hxx +++ b/sal/inc/rtl/string.hxx @@ -663,6 +663,32 @@ public: } /** + Check whether this string starts with a given substring. + + @param str the substring to be compared + + @return true if and only if the given str appears as a substring at the + start of this string + + @since LibreOffice 3.7 + */ + bool startsWith(OString const & str) const { + return match(str, 0); + } + + /** + @overload + This function accepts an ASCII string literal as its argument. + @since LibreOffice 3.7 + */ + template< typename T > + typename internal::ConstCharArrayDetector< T, bool >::Type startsWith( T& literal ) const + { + RTL_STRING_CONST_FUNCTION + return match(literal, 0); + } + + /** Check whether this string ends with a given substring. @param str the substring to be compared diff --git a/sal/inc/rtl/ustring.hxx b/sal/inc/rtl/ustring.hxx index 448f6201e773..3e4e5414d1bb 100644 --- a/sal/inc/rtl/ustring.hxx +++ b/sal/inc/rtl/ustring.hxx @@ -881,6 +881,65 @@ public: #endif /** + Check whether this string starts with a given substring. + + @param str the substring to be compared + + @return true if and only if the given str appears as a substring at the + start of this string + + @since LibreOffice 3.7 + */ + bool startsWith(OUString const & str) const { + return match(str, 0); + } + + /** + @overload + This function accepts an ASCII string literal as its argument. + @since LibreOffice 3.7 + */ + template< typename T > + typename internal::ConstCharArrayDetector< T, bool >::Type startsWith( T& literal ) const + { + return rtl_ustr_asciil_reverseEquals_WithLength( pData->buffer, literal, + internal::ConstCharArrayDetector< T, void >::size - 1); + } + + /** + Check whether this string starts with a given string, ignoring the case of + ASCII letters. + + Character values between 65 and 90 (ASCII A-Z) are interpreted as + values between 97 and 122 (ASCII a-z). + This function can't be used for language specific comparison. + + @param str the object (substring) to be compared. + @return true if this string starts with str, ignoring the case of ASCII + letters ("A"--"Z" and "a"--"z"); otherwise, false is returned + @since LibreOffice 3.7 + */ + sal_Bool startsWithIgnoreAsciiCase( const OUString & str ) const SAL_THROW(()) + { + return matchIgnoreAsciiCase(str, 0); + } + + /** + @overload + This function accepts an ASCII string literal as its argument. + @since LibreOffice 3.7 + */ + template< typename T > + typename internal::ConstCharArrayDetector< T, bool >::Type startsWithIgnoreAsciiCase( T& literal ) const SAL_THROW(()) + { + return (rtl_ustr_ascii_compareIgnoreAsciiCase_WithLengths( + pData->buffer, + internal::ConstCharArrayDetector< T, void >::size - 1, literal, + internal::ConstCharArrayDetector< T, void >::size - 1) + == 0); + } + + /** Check whether this string ends with a given substring. @param str the substring to be compared diff --git a/sal/qa/rtl/strings/test_ostring_stringliterals.cxx b/sal/qa/rtl/strings/test_ostring_stringliterals.cxx index 0f06c3f35096..877c45e6b808 100644 --- a/sal/qa/rtl/strings/test_ostring_stringliterals.cxx +++ b/sal/qa/rtl/strings/test_ostring_stringliterals.cxx @@ -190,6 +190,9 @@ void test::ostring::StringLiterals::checkUsage() CPPUNIT_ASSERT( FooBaR.matchIgnoreAsciiCase( "fOo" )); CPPUNIT_ASSERT( rtl_string_unittest_const_literal_function == true ); rtl_string_unittest_const_literal_function = false; + CPPUNIT_ASSERT( foobar.startsWith( "foo" )); + CPPUNIT_ASSERT( rtl_string_unittest_const_literal_function == true ); + rtl_string_unittest_const_literal_function = false; CPPUNIT_ASSERT( foobar.endsWith( "bar" )); CPPUNIT_ASSERT( rtl_string_unittest_const_literal_function == true ); // rtl_string_unittest_const_literal_function = false; @@ -249,6 +252,8 @@ void test::ostring::StringLiterals::checkNonConstUsage() CPPUNIT_ASSERT( FooBaRfoo.matchIgnoreAsciiCase( bAr_c, 3 )); CPPUNIT_ASSERT( FooBaR.matchIgnoreAsciiCase( (const char*)fOo_c )); CPPUNIT_ASSERT( FooBaR.matchIgnoreAsciiCase( fOo_c )); + CPPUNIT_ASSERT( foobar.startsWith( (const char*)foo_c )); + CPPUNIT_ASSERT( foobar.startsWith( foo_c )); CPPUNIT_ASSERT( foobar.endsWith( (const char*)bar_c )); CPPUNIT_ASSERT( foobar.endsWith( bar_c )); // CPPUNIT_ASSERT( FooBaR.endsWithIgnoreAsciiCase( (const char*)bar_c )); diff --git a/sal/qa/rtl/strings/test_oustring_startswith.cxx b/sal/qa/rtl/strings/test_oustring_startswith.cxx new file mode 100644 index 000000000000..a16a24447897 --- /dev/null +++ b/sal/qa/rtl/strings/test_oustring_startswith.cxx @@ -0,0 +1,63 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2000, 2010 Oracle and/or its affiliates. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +#include <sal/types.h> +#include <cppunit/TestFixture.h> +#include <cppunit/extensions/HelperMacros.h> +#include "rtl/strbuf.hxx" +#include "rtl/string.h" +#include "rtl/string.hxx" +#include "rtl/textenc.h" +#include "rtl/ustring.hxx" +#include "sal/types.h" +#include <sal/macros.h> + +namespace test { namespace oustring { + +class StartsWith: public CppUnit::TestFixture +{ +private: + void startsWith(); + + CPPUNIT_TEST_SUITE(StartsWith); + CPPUNIT_TEST(startsWith); + CPPUNIT_TEST_SUITE_END(); +}; + +} } + +CPPUNIT_TEST_SUITE_REGISTRATION(test::oustring::StartsWith); + +void test::oustring::StartsWith::startsWith() +{ + CPPUNIT_ASSERT( rtl::OUString( "foobar" ).startsWith( "foo" )); + CPPUNIT_ASSERT( !rtl::OUString( "foo" ).startsWith( "foobar" )); + CPPUNIT_ASSERT( !rtl::OUString( "foobar" ).startsWith( "oo" )); +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sal/qa/rtl/strings/test_oustring_stringliterals.cxx b/sal/qa/rtl/strings/test_oustring_stringliterals.cxx index 84394b79a55f..cd9809746690 100644 --- a/sal/qa/rtl/strings/test_oustring_stringliterals.cxx +++ b/sal/qa/rtl/strings/test_oustring_stringliterals.cxx @@ -129,6 +129,8 @@ void test::oustring::StringLiterals::checkUsage() CPPUNIT_ASSERT( foobar.match( "foo" )); CPPUNIT_ASSERT( FooBaRfoo.matchIgnoreAsciiCase( "bAr", 3 )); CPPUNIT_ASSERT( FooBaR.matchIgnoreAsciiCase( "fOo" )); + CPPUNIT_ASSERT( foobar.startsWith( "foo" )); + CPPUNIT_ASSERT( FooBaR.startsWithIgnoreAsciiCase( "foo" )); CPPUNIT_ASSERT( foobar.endsWith( "bar" )); CPPUNIT_ASSERT( FooBaR.endsWithIgnoreAsciiCase( "bar" )); CPPUNIT_ASSERT( foo == "foo" ); |