tdf#166673 Fix Space(-1) behaviour

Change-Id: I3ea3a538dec449f752ca17122091b410221b7fe7
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/203225
Tested-by: Jenkins
Reviewed-by: Neil Roberts <nroberts@libreoffice.org>
This commit is contained in:
bl4ze4447
2026-04-22 17:46:30 +02:00
committed by Neil Roberts
parent c85d22c26f
commit c15d89a0a6
3 changed files with 48 additions and 9 deletions
+20 -7
View File
@@ -7,11 +7,24 @@
Option Explicit
Function doUnitTest as String
' SPACE
If ( Space(3) <> " " ) Then
doUnitTest = "FAIL"
Else
doUnitTest = "OK"
End If
Function doUnitTest As String
TestUtil.Testinit
verify_testSpace
doUnitTest = TestUtil.GetResult()
End Function
Sub verify_testSpace()
On Error Goto errorHandler
TestUtil.AssertEqual(Space(3), " ", "Space(3)")
TestUtil.AssertEqual(Space(2), " ", "Space(2)")
TestUtil.AssertEqual(Space(0), "", "Space(0)")
' Negative values generate an empty string
TestUtil.AssertEqual(Space(-1), "", "Space(-1)")
TestUtil.AssertEqual(Space(-4), "", "Space(-4)")
Exit Sub
errorHandler:
TestUtil.ReportErrorHandler("verify_testSpace", Err, Error$, Erl)
End Sub
+16 -1
View File
@@ -18,9 +18,24 @@ End Function
Sub verify_testSpace()
On Error GoTo errorHandler
TestUtil.AssertEqual(Space(2), " ", "Space(2)")
TestUtil.AssertEqual(Space(3), " ", "Space(3)")
TestUtil.AssertEqual(Space(2), " ", "Space(2)")
TestUtil.AssertEqual(Space(0), "", "Space(0)")
' in VBA it must throw when used with negative values
TestUtil.AssertEqual(GetNegativeSpaceError(-1), 5, "GetNegativeSpaceError(-1)")
TestUtil.AssertEqual(GetNegativeSpaceError(-4), 5, "GetNegativeSpaceError(-4)")
Exit Sub
errorHandler:
TestUtil.ReportErrorHandler("verify_testSpace", Err, Error$, Erl)
End Sub
Function GetNegativeSpaceError(negativeValue) As Long
On Error GoTo errorHandler
Space(negativeValue)
GetNegativeSpaceError = 0
Exit Function
errorHandler:
GetNegativeSpaceError = Err
End Function
+12 -1
View File
@@ -1302,7 +1302,18 @@ void SbRtl_Space(StarBASIC *, SbxArray & rPar, bool)
return StarBASIC::Error(ERRCODE_BASIC_BAD_ARGUMENT);
}
const sal_Int32 nCount = rPar.Get(1)->GetLong();
sal_Int32 nCount = rPar.Get(1)->GetLong();
if (nCount < 0)
{
if (SbiRuntime::isVBAEnabled())
{
return StarBASIC::Error(ERRCODE_BASIC_BAD_ARGUMENT);
}
nCount = 0;
}
rPar.Get(0)->PutString(OUString::Concat(RepeatedUChar(' ', nCount)));
}