From af830cc5770f76c4c8bba45b078a37fe9bdc20fb Mon Sep 17 00:00:00 2001 From: Neil Roberts Date: Thu, 18 Jun 2026 00:36:25 +0200 Subject: [PATCH] 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 --- basctl/source/basicide/baside2b.cxx | 15 +------- basctl/source/basicide/bastypes.cxx | 5 ++- basctl/source/inc/bastypes.hxx | 3 -- basic/CppunitTest_basic_util.mk | 20 ++++++++++ basic/Library_sb.mk | 1 + basic/Module_basic.mk | 1 + basic/qa/cppunit/test_util.cxx | 58 +++++++++++++++++++++++++++++ basic/source/util/sbutil.cxx | 37 ++++++++++++++++++ include/basic/sbutil.hxx | 37 ++++++++++++++++++ 9 files changed, 159 insertions(+), 18 deletions(-) create mode 100644 basic/CppunitTest_basic_util.mk create mode 100644 basic/qa/cppunit/test_util.cxx create mode 100644 basic/source/util/sbutil.cxx create mode 100644 include/basic/sbutil.hxx diff --git a/basctl/source/basicide/baside2b.cxx b/basctl/source/basicide/baside2b.cxx index 1b1212b75b52..d1c2c65f5b93 100644 --- a/basctl/source/basicide/baside2b.cxx +++ b/basctl/source/basicide/baside2b.cxx @@ -36,6 +36,7 @@ #include #include #include +#include #include #include #include @@ -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); diff --git a/basctl/source/basicide/bastypes.cxx b/basctl/source/basicide/bastypes.cxx index 7ae314179584..ef7c2027ca68 100644 --- a/basctl/source/basicide/bastypes.cxx +++ b/basctl/source/basicide/bastypes.cxx @@ -33,6 +33,7 @@ #include #include +#include #include #include #include @@ -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(); diff --git a/basctl/source/inc/bastypes.hxx b/basctl/source/inc/bastypes.hxx index ac7a967f35cb..e5f3f9a503dd 100644 --- a/basctl/source/inc/bastypes.hxx +++ b/basctl/source/inc/bastypes.hxx @@ -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. diff --git a/basic/CppunitTest_basic_util.mk b/basic/CppunitTest_basic_util.mk new file mode 100644 index 000000000000..880030be5ab1 --- /dev/null +++ b/basic/CppunitTest_basic_util.mk @@ -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: diff --git a/basic/Library_sb.mk b/basic/Library_sb.mk index aa1de14b01f8..7dd6fbecf58f 100644 --- a/basic/Library_sb.mk +++ b/basic/Library_sb.mk @@ -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 diff --git a/basic/Module_basic.mk b/basic/Module_basic.mk index a01c883268bc..2a73d270c55f 100644 --- a/basic/Module_basic.mk +++ b/basic/Module_basic.mk @@ -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 diff --git a/basic/qa/cppunit/test_util.cxx b/basic/qa/cppunit/test_util.cxx new file mode 100644 index 000000000000..124468e205e7 --- /dev/null +++ b/basic/qa/cppunit/test_util.cxx @@ -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 + +#include + +#include +#include +#include + +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: */ diff --git a/basic/source/util/sbutil.cxx b/basic/source/util/sbutil.cxx new file mode 100644 index 000000000000..97c925dade5c --- /dev/null +++ b/basic/source/util/sbutil.cxx @@ -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 + +#include + +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: */ diff --git a/include/basic/sbutil.hxx b/include/basic/sbutil.hxx new file mode 100644 index 000000000000..d0329243a29f --- /dev/null +++ b/include/basic/sbutil.hxx @@ -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 + +#include + +#include + +#include + +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: */