Kaydet (Commit) 50057a37 authored tarafından Stephan Bergmann's avatar Stephan Bergmann

Introduce o3tl::string_view.hxx approximation of C++17 <string_view>

...and use it in configmgr/source/writemodfile.hxx

Change-Id: Ie683dc21010ed45cc454ff89bea0376994b351f2
Reviewed-on: https://gerrit.libreoffice.org/36270Tested-by: 's avatarJenkins <ci@libreoffice.org>
Reviewed-by: 's avatarStephan Bergmann <sbergman@redhat.com>
üst 39038a35
...@@ -149,7 +149,7 @@ void dumpWindowsRegistryKey(HKEY hKey, OUString const & aKeyName, TempFile &aFil ...@@ -149,7 +149,7 @@ void dumpWindowsRegistryKey(HKEY hKey, OUString const & aKeyName, TempFile &aFil
bool bHasNode = false; bool bHasNode = false;
sal_Int32 nCloseNode = 0; sal_Int32 nCloseNode = 0;
writeData(aFileHandle, "<item oor:path=\""); aFileHandle.writeString("<item oor:path=\"");
for(sal_Int32 nIndex = 0;; ++nIndex) for(sal_Int32 nIndex = 0;; ++nIndex)
{ {
OUString aNextPathPart = aPathAndNodes.getToken(nIndex, '\\'); OUString aNextPathPart = aPathAndNodes.getToken(nIndex, '\\');
...@@ -160,13 +160,13 @@ void dumpWindowsRegistryKey(HKEY hKey, OUString const & aKeyName, TempFile &aFil ...@@ -160,13 +160,13 @@ void dumpWindowsRegistryKey(HKEY hKey, OUString const & aKeyName, TempFile &aFil
{ {
bHasNode = true; bHasNode = true;
nCloseNode++; nCloseNode++;
writeData(aFileHandle, "\"><node oor:name=\""); aFileHandle.writeString("\"><node oor:name=\"");
sal_Int32 nCommandSeparator = aNextPathPart.lastIndexOf('#'); sal_Int32 nCommandSeparator = aNextPathPart.lastIndexOf('#');
if(nCommandSeparator != -1) if(nCommandSeparator != -1)
{ {
OUString aNodeOp = aNextPathPart.copy(nCommandSeparator + 1); OUString aNodeOp = aNextPathPart.copy(nCommandSeparator + 1);
writeAttributeValue(aFileHandle, aNextPathPart.copy(0, nCommandSeparator - 1)); writeAttributeValue(aFileHandle, aNextPathPart.copy(0, nCommandSeparator - 1));
writeData(aFileHandle, "\" oor:op=\""); aFileHandle.writeString("\" oor:op=\"");
writeAttributeValue(aFileHandle, aNodeOp); writeAttributeValue(aFileHandle, aNodeOp);
} }
else else
...@@ -176,33 +176,34 @@ void dumpWindowsRegistryKey(HKEY hKey, OUString const & aKeyName, TempFile &aFil ...@@ -176,33 +176,34 @@ void dumpWindowsRegistryKey(HKEY hKey, OUString const & aKeyName, TempFile &aFil
} }
else else
{ {
writeAttributeValue(aFileHandle, "/" + aNextPathPart); writeAttributeValue(
aFileHandle, OUString("/" + aNextPathPart));
} }
} }
else else
{ {
writeData(aFileHandle, "\">"); aFileHandle.writeString("\">");
break; break;
} }
} }
writeData(aFileHandle, "<prop oor:name=\""); aFileHandle.writeString("<prop oor:name=\"");
writeAttributeValue(aFileHandle, aProp); writeAttributeValue(aFileHandle, aProp);
writeData(aFileHandle, "\""); aFileHandle.writeString("\"");
if(!aType.isEmpty()) if(!aType.isEmpty())
{ {
writeData(aFileHandle, " oor:type=\""); aFileHandle.writeString(" oor:type=\"");
writeAttributeValue(aFileHandle, aType); writeAttributeValue(aFileHandle, aType);
writeData(aFileHandle, "\""); aFileHandle.writeString("\"");
} }
if(bFinal) if(bFinal)
writeData(aFileHandle, " oor:finalized=\"true\""); aFileHandle.writeString(" oor:finalized=\"true\"");
writeData(aFileHandle, "><value>"); aFileHandle.writeString("><value>");
writeValueContent(aFileHandle, aValue); writeValueContent(aFileHandle, aValue);
writeData(aFileHandle, "</value></prop>"); aFileHandle.writeString("</value></prop>");
for(; nCloseNode > 0; nCloseNode--) for(; nCloseNode > 0; nCloseNode--)
writeData(aFileHandle, "</node>"); aFileHandle.writeString("</node>");
writeData(aFileHandle, "</item>\n"); aFileHandle.writeString("</item>\n");
} }
RegCloseKey(hCurKey); RegCloseKey(hCurKey);
} }
...@@ -235,14 +236,13 @@ bool dumpWindowsRegistry(OUString* pFileURL, WinRegType eType) ...@@ -235,14 +236,13 @@ bool dumpWindowsRegistry(OUString* pFileURL, WinRegType eType)
"cannot create temporary file"); "cannot create temporary file");
} }
aFileHandle.url = *pFileURL; aFileHandle.url = *pFileURL;
writeData( aFileHandle.writeString(
aFileHandle, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<oor:items"
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<oor:items" " xmlns:oor=\"http://openoffice.org/2001/registry\""
" xmlns:oor=\"http://openoffice.org/2001/registry\"" " xmlns:xs=\"http://www.w3.org/2001/XMLSchema\""
" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\"" " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n");
" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n");
dumpWindowsRegistryKey(hKey, "", aFileHandle); dumpWindowsRegistryKey(hKey, "", aFileHandle);
writeData(aFileHandle, "</oor:items>"); aFileHandle.writeString("</oor:items>");
oslFileError e = aFileHandle.closeWithoutUnlink(); oslFileError e = aFileHandle.closeWithoutUnlink();
if (e != osl_File_E_None) if (e != osl_File_E_None)
SAL_WARN("configmgr", "osl_closeFile failed with " << +e); SAL_WARN("configmgr", "osl_closeFile failed with " << +e);
......
...@@ -21,6 +21,8 @@ ...@@ -21,6 +21,8 @@
#define INCLUDED_CONFIGMGR_SOURCE_WRITEMODFILE_HXX #define INCLUDED_CONFIGMGR_SOURCE_WRITEMODFILE_HXX
#include <sal/config.h> #include <sal/config.h>
#include <o3tl/string_view.hxx>
#include <rtl/strbuf.hxx> #include <rtl/strbuf.hxx>
namespace configmgr { namespace configmgr {
...@@ -41,16 +43,15 @@ struct TempFile { ...@@ -41,16 +43,15 @@ struct TempFile {
#ifdef _WIN32 #ifdef _WIN32
oslFileError closeWithoutUnlink(); oslFileError closeWithoutUnlink();
#endif #endif
void writeString(char const *begin, sal_Int32 length); void writeString(o3tl::string_view text);
private: private:
TempFile(const TempFile&) = delete; TempFile(const TempFile&) = delete;
TempFile& operator=(const TempFile&) = delete; TempFile& operator=(const TempFile&) = delete;
}; };
void writeData(TempFile &handle, OString const & text); void writeAttributeValue(TempFile &handle, o3tl::u16string_view value);
void writeAttributeValue(TempFile &handle, OUString const & value); void writeValueContent(TempFile &handle, o3tl::u16string_view value);
void writeValueContent(TempFile &handle, OUString const & value);
void writeModFile( void writeModFile(
Components & components, OUString const & url, Data const & data); Components & components, OUString const & url, Data const & data);
......
This diff is collapsed.
...@@ -31,6 +31,7 @@ $(eval $(call gb_CppunitTest_add_exception_objects,o3tl_tests,\ ...@@ -31,6 +31,7 @@ $(eval $(call gb_CppunitTest_add_exception_objects,o3tl_tests,\
o3tl/qa/test-cow_wrapper \ o3tl/qa/test-cow_wrapper \
o3tl/qa/test-lru_map \ o3tl/qa/test-lru_map \
o3tl/qa/test-sorted_vector \ o3tl/qa/test-sorted_vector \
o3tl/qa/test-string_view \
o3tl/qa/test-typed_flags \ o3tl/qa/test-typed_flags \
o3tl/qa/test-vector_pool \ o3tl/qa/test-vector_pool \
)) ))
......
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
/*
* 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 <stdexcept>
#include <cppunit/TestAssert.h>
#include <cppunit/TestFixture.h>
#include <cppunit/extensions/HelperMacros.h>
#include <o3tl/string_view.hxx>
namespace {
class Test: public CppUnit::TestFixture {
private:
CPPUNIT_TEST_SUITE(Test);
CPPUNIT_TEST(testCharLiteral);
CPPUNIT_TEST(testChar16Literal);
CPPUNIT_TEST(testChar32Literal);
CPPUNIT_TEST(testWcharLiteral);
CPPUNIT_TEST(testOperations);
CPPUNIT_TEST_SUITE_END();
void testCharLiteral() {
char * const s1 = const_cast<char *>("foo");
o3tl::string_view v1(s1);
CPPUNIT_ASSERT_EQUAL(o3tl::string_view::size_type(3), v1.size());
char const * const s2 = "foo";
o3tl::string_view v2(s2);
CPPUNIT_ASSERT_EQUAL(o3tl::string_view::size_type(3), v2.size());
char s3[] = "foo";
o3tl::string_view v3(s3);
CPPUNIT_ASSERT_EQUAL(o3tl::string_view::size_type(3), v3.size());
o3tl::string_view v4("foo");
CPPUNIT_ASSERT_EQUAL(o3tl::string_view::size_type(3), v4.size());
}
void testChar16Literal() {
char16_t * const s1 = const_cast<char16_t *>(u"foo");
o3tl::u16string_view v1(s1);
CPPUNIT_ASSERT_EQUAL(o3tl::u16string_view::size_type(3), v1.size());
char16_t const * const s2 = u"foo";
o3tl::u16string_view v2(s2);
CPPUNIT_ASSERT_EQUAL(o3tl::u16string_view::size_type(3), v2.size());
char16_t s3[] = u"foo";
o3tl::u16string_view v3(s3);
CPPUNIT_ASSERT_EQUAL(o3tl::u16string_view::size_type(3), v3.size());
o3tl::u16string_view v4(u"foo");
CPPUNIT_ASSERT_EQUAL(o3tl::u16string_view::size_type(3), v4.size());
}
void testChar32Literal() {
char32_t * const s1 = const_cast<char32_t *>(U"foo");
o3tl::u32string_view v1(s1);
CPPUNIT_ASSERT_EQUAL(o3tl::u32string_view::size_type(3), v1.size());
char32_t const * const s2 = U"foo";
o3tl::u32string_view v2(s2);
CPPUNIT_ASSERT_EQUAL(o3tl::u32string_view::size_type(3), v2.size());
char32_t s3[] = U"foo";
o3tl::u32string_view v3(s3);
CPPUNIT_ASSERT_EQUAL(o3tl::u32string_view::size_type(3), v3.size());
o3tl::u32string_view v4(U"foo");
CPPUNIT_ASSERT_EQUAL(o3tl::u32string_view::size_type(3), v4.size());
}
void testWcharLiteral() {
wchar_t * const s1 = const_cast<wchar_t *>(L"foo");
o3tl::wstring_view v1(s1);
CPPUNIT_ASSERT_EQUAL(o3tl::wstring_view::size_type(3), v1.size());
wchar_t const * const s2 = L"foo";
o3tl::wstring_view v2(s2);
CPPUNIT_ASSERT_EQUAL(o3tl::wstring_view::size_type(3), v2.size());
wchar_t s3[] = L"foo";
o3tl::wstring_view v3(s3);
CPPUNIT_ASSERT_EQUAL(o3tl::wstring_view::size_type(3), v3.size());
o3tl::wstring_view v4(L"foo");
CPPUNIT_ASSERT_EQUAL(o3tl::wstring_view::size_type(3), v4.size());
}
void testOperations() {
o3tl::string_view const v("fox");
auto npos = o3tl::string_view::npos;
// o3tl::basic_string_view::npos will be (implicitly) inline with
// C++17, but for now can't be passed as 'const T& expected'
// argument into CppUnit::assertEquals, so take this detour
CPPUNIT_ASSERT_EQUAL('f', *v.begin());
CPPUNIT_ASSERT_EQUAL(
o3tl::string_view::difference_type(3), v.end() - v.begin());
CPPUNIT_ASSERT_EQUAL('f', *v.cbegin());
CPPUNIT_ASSERT_EQUAL(
o3tl::string_view::difference_type(3), v.cend() - v.cbegin());
CPPUNIT_ASSERT_EQUAL('x', *v.rbegin());
CPPUNIT_ASSERT_EQUAL(
o3tl::string_view::difference_type(3), v.rend() - v.rbegin());
CPPUNIT_ASSERT_EQUAL('x', *v.crbegin());
CPPUNIT_ASSERT_EQUAL(
o3tl::string_view::difference_type(3), v.crend() - v.crbegin());
CPPUNIT_ASSERT_EQUAL(o3tl::string_view::size_type(3), v.size());
CPPUNIT_ASSERT_EQUAL(o3tl::string_view::size_type(3), v.length());
CPPUNIT_ASSERT_EQUAL(o3tl::string_view::npos - 1, v.max_size());
CPPUNIT_ASSERT(!v.empty());
CPPUNIT_ASSERT_EQUAL('o', v[1]);
try {
v.at(o3tl::string_view::npos);
CPPUNIT_FAIL("missing exception");
} catch (std::out_of_range &) {}
CPPUNIT_ASSERT_EQUAL('f', v.at(0));
CPPUNIT_ASSERT_EQUAL('x', v.at(2));
try {
v.at(3);
CPPUNIT_FAIL("missing exception");
} catch (std::out_of_range &) {}
CPPUNIT_ASSERT_EQUAL('f', v.front());
CPPUNIT_ASSERT_EQUAL('x', v.back());
CPPUNIT_ASSERT_EQUAL('f', *v.data());
{
o3tl::string_view v1("fox");
v1.remove_prefix(2);
CPPUNIT_ASSERT_EQUAL('x', v1.front());
CPPUNIT_ASSERT_EQUAL(o3tl::string_view::size_type(1), v1.size());
}
{
o3tl::string_view v1("fox");
v1.remove_suffix(2);
CPPUNIT_ASSERT_EQUAL('f', v1.front());
CPPUNIT_ASSERT_EQUAL(o3tl::string_view::size_type(1), v1.size());
}
{
o3tl::string_view v1("fox");
o3tl::string_view v2("giraffe");
v1.swap(v2);
CPPUNIT_ASSERT_EQUAL(o3tl::string_view::size_type(7), v1.size());
CPPUNIT_ASSERT_EQUAL(o3tl::string_view::size_type(3), v2.size());
}
{
char a[2];
auto n = v.copy(a, 10, 1);
CPPUNIT_ASSERT_EQUAL(o3tl::string_view::size_type(2), n);
CPPUNIT_ASSERT_EQUAL('o', a[0]);
CPPUNIT_ASSERT_EQUAL('x', a[1]);
}
{
auto v1 = v.substr(1);
CPPUNIT_ASSERT_EQUAL('o', v1.front());
CPPUNIT_ASSERT_EQUAL(o3tl::string_view::size_type(2), v1.size());
}
CPPUNIT_ASSERT(v.compare(o3tl::string_view("foo")) > 0);
CPPUNIT_ASSERT(v.compare(0, 2, o3tl::string_view("foo")) < 0);
CPPUNIT_ASSERT_EQUAL(
0, v.compare(0, 2, o3tl::string_view("foo"), 0, 2));
CPPUNIT_ASSERT_EQUAL(0, v.compare("fox"));
CPPUNIT_ASSERT(v.compare(1, 2, "abc") > 0);
CPPUNIT_ASSERT_EQUAL(0, v.compare(1, 2, "oxx", 2));
CPPUNIT_ASSERT_EQUAL(o3tl::string_view::size_type(1), v.find("ox"));
CPPUNIT_ASSERT_EQUAL(o3tl::string_view::size_type(1), v.find('o'));
CPPUNIT_ASSERT_EQUAL(
o3tl::string_view::size_type(1), v.find("oxx", 0, 2));
CPPUNIT_ASSERT_EQUAL(npos, v.find("oxx"));
CPPUNIT_ASSERT_EQUAL(o3tl::string_view::size_type(1), v.rfind("ox"));
CPPUNIT_ASSERT_EQUAL(o3tl::string_view::size_type(1), v.rfind('o'));
CPPUNIT_ASSERT_EQUAL(
o3tl::string_view::size_type(1),
v.rfind("oxx", o3tl::string_view::npos, 2));
CPPUNIT_ASSERT_EQUAL(npos, v.rfind("oxx"));
CPPUNIT_ASSERT_EQUAL(
o3tl::string_view::size_type(1), v.find_first_of("nop"));
CPPUNIT_ASSERT_EQUAL(
o3tl::string_view::size_type(1), v.find_first_of('o'));
CPPUNIT_ASSERT_EQUAL(
o3tl::string_view::size_type(1), v.find_first_of("nofx", 0, 2));
CPPUNIT_ASSERT_EQUAL(
o3tl::string_view::size_type(0), v.find_first_of("nofx"));
CPPUNIT_ASSERT_EQUAL(
o3tl::string_view::size_type(1), v.find_last_of("nop"));
CPPUNIT_ASSERT_EQUAL(
o3tl::string_view::size_type(1), v.find_last_of('o'));
CPPUNIT_ASSERT_EQUAL(
o3tl::string_view::size_type(1),
v.find_last_of("nofx", o3tl::string_view::npos, 2));
CPPUNIT_ASSERT_EQUAL(
o3tl::string_view::size_type(2), v.find_last_of("nofx"));
CPPUNIT_ASSERT_EQUAL(
o3tl::string_view::size_type(1), v.find_first_not_of("fx"));
CPPUNIT_ASSERT_EQUAL(
o3tl::string_view::size_type(1), v.find_first_not_of('f'));
CPPUNIT_ASSERT_EQUAL(
o3tl::string_view::size_type(1), v.find_first_not_of("fxo", 0, 2));
CPPUNIT_ASSERT_EQUAL(npos, v.find_first_not_of("fxo"));
CPPUNIT_ASSERT_EQUAL(
o3tl::string_view::size_type(1), v.find_last_not_of("fx"));
CPPUNIT_ASSERT_EQUAL(
o3tl::string_view::size_type(1), v.find_last_not_of('x'));
CPPUNIT_ASSERT_EQUAL(
o3tl::string_view::size_type(1),
v.find_last_not_of("fxo", o3tl::string_view::npos, 2));
CPPUNIT_ASSERT_EQUAL(npos, v.find_last_not_of("fxo"));
}
};
CPPUNIT_TEST_SUITE_REGISTRATION(Test);
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */
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