Move searchEOL from basicide to a general basic utility

Moves the searchEOL function from basicide to a general purpose utility
function in the basic module. This also adds some unit tests for it.

Change-Id: I8f446990654415a994b7964dd62706f3204d817a
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/207060
Tested-by: Jenkins
Reviewed-by: Neil Roberts <nroberts@libreoffice.org>
This commit is contained in:
Neil Roberts
2026-07-22 15:58:49 +02:00
parent 9b60447481
commit af830cc577
9 changed files with 159 additions and 18 deletions
+2 -13
View File
@@ -36,6 +36,7 @@
#include <basctl/basctldllpublic.hxx>
#include <basic/sbmeth.hxx>
#include <basic/sbuno.hxx>
#include <basic/sbutil.hxx>
#include <com/sun/star/beans/XMultiPropertySet.hpp>
#include <com/sun/star/beans/XPropertiesChangeListener.hpp>
#include <com/sun/star/container/XHierarchicalNameAccess.hpp>
@@ -1047,18 +1048,6 @@ void EditorWindow::SetSourceInBasic()
}
}
// Returns the position of the last character of any of the following
// EOL char combinations: CR, CR/LF, LF, return -1 if no EOL is found
sal_Int32 searchEOL( std::u16string_view rStr, sal_Int32 fromIndex )
{
size_t iLF = rStr.find( LINE_SEP, fromIndex );
if( iLF != std::u16string_view::npos )
return iLF;
size_t iCR = rStr.find( LINE_SEP_CR, fromIndex );
return iCR == std::u16string_view::npos ? -1 : iCR;
}
void EditorWindow::CreateEditEngine()
{
if (pEditEngine)
@@ -1082,7 +1071,7 @@ void EditorWindow::CreateEditEngine()
do
{
nLines++;
nIndex = searchEOL( aOUSource, nIndex+1 );
nIndex = sb::searchEOL( aOUSource, nIndex+1 );
}
while (nIndex >= 0);
+3 -2
View File
@@ -33,6 +33,7 @@
#include <com/sun/star/script/XLibraryContainer.hpp>
#include <com/sun/star/script/XLibraryContainerPassword.hpp>
#include <basic/sbutil.hxx>
#include <basctl/basctldllpublic.hxx>
#include <sal/log.hxx>
#include <sfx2/dispatch.hxx>
@@ -619,7 +620,7 @@ void CutLines( OUString& rStr, sal_Int32 nStartLine, sal_Int32 nLines )
sal_Int32 nLine = 0;
while ( nLine < nStartLine )
{
nStartPos = searchEOL( rStr, nStartPos );
nStartPos = sb::searchEOL( rStr, nStartPos );
if( nStartPos == -1 )
break;
nStartPos++; // not the \n.
@@ -634,7 +635,7 @@ void CutLines( OUString& rStr, sal_Int32 nStartLine, sal_Int32 nLines )
sal_Int32 nEndPos = nStartPos;
for ( sal_Int32 i = 0; i < nLines; i++ )
nEndPos = searchEOL( rStr, nEndPos+1 );
nEndPos = sb::searchEOL( rStr, nEndPos+1 );
if ( nEndPos == -1 ) // might happen at the last line
nEndPos = rStr.getLength();
-3
View File
@@ -48,9 +48,6 @@ class Layout;
constexpr auto LINE_SEP_CR = 0x0D;
constexpr auto LINE_SEP = 0x0A;
// Implementation: baside2b.cxx
sal_Int32 searchEOL( std::u16string_view rStr, sal_Int32 fromIndex );
// Meaning of bToBeKilled:
// While being in a reschedule-loop, I may not destroy the window.
// It must first break from the reschedule-loop to self-destroy then.
+20
View File
@@ -0,0 +1,20 @@
# -*- Mode: makefile-gmake; tab-width: 4; indent-tabs-mode: t; 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/.
#
$(eval $(call gb_CppunitTest_CppunitTest,basic_util))
$(eval $(call gb_CppunitTest_add_exception_objects,basic_util, \
basic/qa/cppunit/test_util \
))
$(eval $(call gb_CppunitTest_use_libraries,basic_util, \
sb \
))
# vim: set noet sw=4 ts=4:
+1
View File
@@ -100,6 +100,7 @@ $(eval $(call gb_Library_add_exception_objects,sb,\
basic/source/runtime/stdobj \
basic/source/runtime/stdobj1 \
basic/source/classes/errobject \
basic/source/util/sbutil \
))
endif
+1
View File
@@ -18,6 +18,7 @@ $(eval $(call gb_Module_add_l10n_targets,basic,\
$(eval $(call gb_Module_add_check_targets,basic,\
CppunitTest_basic_scanner \
CppunitTest_basic_macros \
CppunitTest_basic_util \
))
endif
+58
View File
@@ -0,0 +1,58 @@
/* -*- 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 <basic/sbutil.hxx>
#include <cppunit/extensions/HelperMacros.h>
#include <cppunit/plugin/TestPlugIn.h>
#include <cppunit/TestFixture.h>
namespace
{
class TestUtil : public CppUnit::TestFixture
{
private:
void testSearchEOL();
CPPUNIT_TEST_SUITE(TestUtil);
CPPUNIT_TEST(testSearchEOL);
CPPUNIT_TEST_SUITE_END();
};
void TestUtil::testSearchEOL()
{
// Find the ending when \n is the first one
CPPUNIT_ASSERT_EQUAL(sal_Int32(3), sb::searchEOL(u"one\ntwo\r\three\r\n"));
// Find the ending when \r\n is the first one
CPPUNIT_ASSERT_EQUAL(sal_Int32(4), sb::searchEOL(u"one\r\ntwo\r\three\n"));
// The three types of ending at at the end of the string
CPPUNIT_ASSERT_EQUAL(sal_Int32(3), sb::searchEOL(u"one\n"));
CPPUNIT_ASSERT_EQUAL(sal_Int32(3), sb::searchEOL(u"one\r"));
CPPUNIT_ASSERT_EQUAL(sal_Int32(4), sb::searchEOL(u"one\r\n"));
// No ending
CPPUNIT_ASSERT_EQUAL(sal_Int32(-1), sb::searchEOL(u""));
CPPUNIT_ASSERT_EQUAL(sal_Int32(-1), sb::searchEOL(u"unending string"));
// Skip an ending
CPPUNIT_ASSERT_EQUAL(sal_Int32(7), sb::searchEOL(u"one\ntwo\n", 4));
// Start at an ending
CPPUNIT_ASSERT_EQUAL(sal_Int32(3), sb::searchEOL(u"one\ntwo\n", 3));
}
}
CPPUNIT_TEST_SUITE_REGISTRATION(TestUtil);
CPPUNIT_PLUGIN_IMPLEMENT();
/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */
+37
View File
@@ -0,0 +1,37 @@
/* -*- 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/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#include <sal/config.h>
#include <basic/sbutil.hxx>
namespace sb
{
sal_Int32 searchEOL(std::u16string_view rStr, sal_Int32 fromIndex)
{
size_t iLF = rStr.find('\n', fromIndex);
if (iLF != std::u16string_view::npos)
return iLF;
size_t iCR = rStr.find('\r', fromIndex);
return iCR == std::u16string_view::npos ? -1 : iCR;
}
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */
+37
View File
@@ -0,0 +1,37 @@
/* -*- 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/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#pragma once
#include <sal/config.h>
#include <basic/basicdllapi.h>
#include <sal/types.h>
#include <string_view>
namespace sb
{
// Returns the position of the last character of any of the following EOL char combinations:
// CR, CR/LF, LF, return -1 if no EOL is found
BASIC_DLLPUBLIC sal_Int32 searchEOL(std::u16string_view rStr, sal_Int32 fromIndex = 0);
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */