Kaydet (Commit) 095ffaf5 authored tarafından Luboš Luňák's avatar Luboš Luňák

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
üst 5c804dce
......@@ -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 \
))
......
......@@ -662,6 +662,32 @@ public:
literal, internal::ConstCharArrayDetector< T, void >::size - 1, internal::ConstCharArrayDetector< T, void >::size - 1 ) == 0;
}
/**
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.
......
......@@ -880,6 +880,65 @@ public:
const;
#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.
......
......@@ -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 ));
......
/* -*- 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: */
......@@ -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" );
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment