Kaydet (Commit) 350b9e36 authored tarafından Stephan Bergmann's avatar Stephan Bergmann

Add OString::startsWithIgnoreAsciiCase

...analogous to the existing OUString::startsWihtIgnoreAsciiCase, to be used in
the next commit

Change-Id: Iad6989c16e1bda6b2b0a58e6c768f7852560bb00
üst f9a41236
......@@ -762,6 +762,55 @@ public:
return b;
}
/**
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 substring to be compared
@param rest if non-null, and this function returns true, then assign a
copy of the remainder of this string to *rest.
@return true if and only if the given str appears as a substring at the
start of this string, ignoring the case of ASCII letters ("A"--"Z" and
"a"--"z")
@since LibreOffice 5.1
*/
bool startsWithIgnoreAsciiCase(OString const & str, OString * rest = 0)
const
{
bool b = matchIgnoreAsciiCase(str);
if (b && rest != 0) {
*rest = copy(str.getLength());
}
return b;
}
/**
@overload
This function accepts an ASCII string literal as its argument.
@since LibreOffice 5.1
*/
template< typename T >
typename libreoffice_internal::ConstCharArrayDetector< T, bool >::Type
startsWithIgnoreAsciiCase(T & literal, OString * rest = 0) const
{
RTL_STRING_CONST_FUNCTION
assert(
libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal));
bool b = matchIgnoreAsciiCase(literal);
if (b && rest != 0) {
*rest = copy(
libreoffice_internal::ConstCharArrayDetector<T>::length);
}
return b;
}
/**
Check whether this string ends with a given substring.
......
......@@ -12,6 +12,7 @@ $(eval $(call gb_CppunitTest_CppunitTest,sal_rtl_strings))
$(eval $(call gb_CppunitTest_add_exception_objects,sal_rtl_strings,\
sal/qa/rtl/strings/test_strings_replace \
sal/qa/rtl/strings/test_ostring \
sal/qa/rtl/strings/test_ostring_concat \
sal/qa/rtl/strings/test_ostring_stringliterals \
sal/qa/rtl/strings/test_oustring_compare \
......
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#include <sal/config.h>
#include <cppunit/TestAssert.h>
#include <cppunit/TestFixture.h>
#include <cppunit/extensions/HelperMacros.h>
#include <rtl/string.hxx>
namespace {
class Test: public CppUnit::TestFixture {
private:
void testStartsWithIgnoreAsciiCase();
CPPUNIT_TEST_SUITE(Test);
CPPUNIT_TEST(testStartsWithIgnoreAsciiCase);
CPPUNIT_TEST_SUITE_END();
};
void Test::testStartsWithIgnoreAsciiCase() {
{
OString r;
CPPUNIT_ASSERT(OString().startsWithIgnoreAsciiCase(OString(), &r));
CPPUNIT_ASSERT(r.isEmpty());
}
{
OString r;
CPPUNIT_ASSERT(OString("foo").startsWithIgnoreAsciiCase(OString(), &r));
CPPUNIT_ASSERT_EQUAL(OString("foo"), r);
}
{
OString r;
CPPUNIT_ASSERT(
OString("foo").startsWithIgnoreAsciiCase(OString("F"), &r));
CPPUNIT_ASSERT_EQUAL(OString("oo"), r);
}
{
OString r("other");
CPPUNIT_ASSERT(
!OString("foo").startsWithIgnoreAsciiCase(OString("bar"), &r));
CPPUNIT_ASSERT_EQUAL(OString("other"), r);
}
{
OString r("other");
CPPUNIT_ASSERT(
!OString("foo").startsWithIgnoreAsciiCase(OString("foobar"), &r));
CPPUNIT_ASSERT_EQUAL(OString("other"), r);
}
{
OString r;
CPPUNIT_ASSERT(OString().startsWithIgnoreAsciiCase("", &r));
CPPUNIT_ASSERT(r.isEmpty());
}
{
OString r;
CPPUNIT_ASSERT(OString("foo").startsWithIgnoreAsciiCase("", &r));
CPPUNIT_ASSERT_EQUAL(OString("foo"), r);
}
{
OString r;
CPPUNIT_ASSERT(
OString("foo").startsWithIgnoreAsciiCase("F", &r));
CPPUNIT_ASSERT_EQUAL(OString("oo"), r);
}
{
OString r("other");
CPPUNIT_ASSERT(
!OString("foo").startsWithIgnoreAsciiCase("bar", &r));
CPPUNIT_ASSERT_EQUAL(OString("other"), r);
}
{
OString r("other");
CPPUNIT_ASSERT(
!OString("foo").startsWithIgnoreAsciiCase("foobar", &r));
CPPUNIT_ASSERT_EQUAL(OString("other"), r);
}
}
CPPUNIT_TEST_SUITE_REGISTRATION(Test);
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
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