From cb2cd5b76b0981bf07b8d1264f4b3074543a19eb Mon Sep 17 00:00:00 2001 From: Stephan Bergmann Date: Tue, 13 Nov 2018 13:05:01 +0100 Subject: tdf#121325: Replace all of given length, even if replacement is shorter MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Both LO and MS Basic restrict the amount of replacement text ("If the Length parameter in the Mid statement is less than the length of the text that you want to replace, the text is reduced to the specified length." in helpcontent2/source/text/sbasic/shared/03120306.xhp, resp. "The number of characters replaced is always less than or equal to the number of characters in Target." at ). But cc20344010e94eda22fee662aab966d395a0796a "tdf#111313: Honor bWriteNoLenParam in !bCompatibility, too" had introduced a regression (in the non--compatibility- mode case), restricting the amount of replaced text to be no more than the amount of replacement text, even if the given length argument was larger. (Which had already regressed in the past, see "Mid statement doesn't work as expected".) Added test cases now. Change-Id: I21d4409f49a2437eb0e1a1e200561d803c42a24c Reviewed-on: https://gerrit.libreoffice.org/63328 Tested-by: Jenkins Reviewed-by: Stephan Bergmann (cherry picked from commit a6a48eeef16e473be14642469cd922f177f54998) Reviewed-on: https://gerrit.libreoffice.org/63332 Tested-by: Xisco Faulí Reviewed-by: Caolán McNamara Tested-by: Caolán McNamara --- basic/qa/basic_coverage/test_mid_replace_less.vb | 19 +++++++++++++++++++ basic/qa/basic_coverage/test_mid_replace_more.vb | 17 +++++++++++++++++ basic/qa/basic_coverage/test_mid_replace_more_end.vb | 19 +++++++++++++++++++ basic/source/runtime/methods.cxx | 14 +++++--------- 4 files changed, 60 insertions(+), 9 deletions(-) create mode 100644 basic/qa/basic_coverage/test_mid_replace_less.vb create mode 100644 basic/qa/basic_coverage/test_mid_replace_more.vb create mode 100644 basic/qa/basic_coverage/test_mid_replace_more_end.vb (limited to 'basic') diff --git a/basic/qa/basic_coverage/test_mid_replace_less.vb b/basic/qa/basic_coverage/test_mid_replace_less.vb new file mode 100644 index 000000000000..27a02382c3fd --- /dev/null +++ b/basic/qa/basic_coverage/test_mid_replace_less.vb @@ -0,0 +1,19 @@ +' +' 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/. +' + +' cf. "Mid statement doesn't work as +' expected": +Function doUnitTest as Integer + s = "The lightbrown fox" + Mid(s, 5, 10, "lazy") + If (s = "The lazy fox") Then + doUnitTest = 1 + Else + doUnitTest = 0 + End If +End Function diff --git a/basic/qa/basic_coverage/test_mid_replace_more.vb b/basic/qa/basic_coverage/test_mid_replace_more.vb new file mode 100644 index 000000000000..c6d75ca90245 --- /dev/null +++ b/basic/qa/basic_coverage/test_mid_replace_more.vb @@ -0,0 +1,17 @@ +' +' 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/. +' + +Function doUnitTest as Integer + s = "The fox jumps" + Mid(s, 5, 3, "duck") + If (s = "The duc jumps") Then + doUnitTest = 1 + Else + doUnitTest = 0 + End If +End Function diff --git a/basic/qa/basic_coverage/test_mid_replace_more_end.vb b/basic/qa/basic_coverage/test_mid_replace_more_end.vb new file mode 100644 index 000000000000..c5d26a46a8db --- /dev/null +++ b/basic/qa/basic_coverage/test_mid_replace_more_end.vb @@ -0,0 +1,19 @@ +' +' 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/. +' + +' cf. examples at : +Function doUnitTest as Integer + s = "The fox jumps" + Mid(s, 5, 100, "cow jumped over") + If (s = "The cow jumpe") Then + doUnitTest = 1 + Else + doUnitTest = 0 + End If +End Function diff --git a/basic/source/runtime/methods.cxx b/basic/source/runtime/methods.cxx index 589105ff3935..a980e19b48b3 100644 --- a/basic/source/runtime/methods.cxx +++ b/basic/source/runtime/methods.cxx @@ -1155,26 +1155,22 @@ void SbRtl_Mid(StarBASIC *, SbxArray & rPar, bool bWrite) sal_Int32 nReplaceLen; if( bWriteNoLenParam ) { - nReplaceLen = nReplaceStrLen; + nReplaceLen = nArgLen - nStartPos; } else { nReplaceLen = nLen; - if( nReplaceLen < 0 || nReplaceLen > nReplaceStrLen ) + if( nReplaceLen < 0 || nReplaceLen > nArgLen - nStartPos ) { - nReplaceLen = nReplaceStrLen; + nReplaceLen = nArgLen - nStartPos; } } - sal_Int32 nReplaceEndPos = nStartPos + nReplaceLen; - if( nReplaceEndPos > nArgLen ) - { - nReplaceLen -= (nReplaceEndPos - nArgLen); - } OUStringBuffer aResultStr = aArgStr; sal_Int32 nErase = nReplaceLen; aResultStr.remove( nStartPos, nErase ); - aResultStr.insert( nStartPos, aReplaceStr.getStr(), nReplaceLen); + aResultStr.insert( + nStartPos, aReplaceStr.getStr(), std::min(nReplaceLen, nReplaceStrLen)); rPar.Get(1)->PutString( aResultStr.makeStringAndClear() ); } -- cgit