[Libreoffice-commits] core.git: wizards/source

Jean-Pierre Ledure (via logerrit) logerrit at kemper.freedesktop.org
Tue Feb 9 09:57:05 UTC 2021


 wizards/source/scriptforge/SF_Array.xba |   52 ++++++++++++++++++++++++++++++++
 1 file changed, 52 insertions(+)

New commits:
commit c8ac8acca8442335321e20801f4c89357cf64894
Author:     Jean-Pierre Ledure <jp at ledure.be>
AuthorDate: Mon Feb 8 18:25:58 2021 +0100
Commit:     Jean-Pierre Ledure <jp at ledure.be>
CommitDate: Tue Feb 9 10:56:24 2021 +0100

    ScriptForge - (SF_Array) new Copy() method
    
    Duplicate the array
    A simple assignment copies the reference, not the data
    In code like:
       Dim a, b
       a = Array(1, 2, 3)
       b = a
       a(2) = 30
       MsgBox b(2)   '   Displays 30
    
    Replace by
       b = SF_Array.Copy(a)
    to get a real duplication of the initial array
    
    Change-Id: I62b931b92995c6607a3b354c60f0ebafb042b88a
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/110591
    Tested-by: Jean-Pierre Ledure <jp at ledure.be>
    Tested-by: Jenkins
    Reviewed-by: Jean-Pierre Ledure <jp at ledure.be>

diff --git a/wizards/source/scriptforge/SF_Array.xba b/wizards/source/scriptforge/SF_Array.xba
index 20c4632aa7ae..b51bba63adbe 100644
--- a/wizards/source/scriptforge/SF_Array.xba
+++ b/wizards/source/scriptforge/SF_Array.xba
@@ -370,6 +370,58 @@ Catch:
 	GoTo Finally
 End Function	'	ScriptForge.SF_Array.ConvertToDictionary
 
+REM -----------------------------------------------------------------------------
+Public Function Copy(Optional ByRef Array_ND As Variant) As Variant
+'''	Duplicate a 1D or 2D array
+'''	A usual assignment copies an array by reference, i.e. shares the same memory location
+'''		Dim a, b
+'''		a = Array(1, 2, 3)
+'''		b = a
+'''		a(2) = 30
+'''		MsgBox b(2)		'	30
+'''	Args
+'''		Array_ND: the array to copy, may be empty
+'''	Return:
+'''		the copied array. Subarrays however still remain assigned by reference
+'''	Examples:
+'''		SF_Array.Copy(Array(1, 2, 3)) returns (1, 2, 3)
+
+Dim vCopy As Variant		'	Return value
+Dim iDims As Integer		'	Number of dimensions of the input array
+Const cstThisSub = "Array.Copy"
+Const cstSubArgs = "Array_ND"
+
+	If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
+	vCopy = Array()
+
+Check:
+	If SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
+		If Not SF_Utils._ValidateArray(Array_ND, "Array_ND") Then GoTo Finally
+		iDims = SF_Array.CountDims(Array_ND)
+		If iDims > 2 Then
+			If Not SF_Utils._ValidateArray(Array_ND, "Array_ND", 2) Then GoTo Finally
+		End If
+	End If
+
+Try:
+	Select Case iDims
+		Case 0
+		Case 1
+			vCopy = Array_ND
+			ReDim Preserve vCopy(LBound(Array_ND) To UBound(Array_ND))
+		Case 2
+			vCopy = Array_ND
+			ReDim Preserve vCopy(LBound(Array_ND, 1) To UBound(Array_ND, 1), LBound(Array_ND, 2) To UBound(Array_ND, 2))
+	End Select
+
+Finally:
+	Copy = vCopy()
+	SF_Utils._ExitFunction(cstThisSub)
+	Exit Function
+Catch:
+	GoTo Finally
+End Function	'	ScriptForge.SF_Array.Copy
+
 REM -----------------------------------------------------------------------------
 Public Function CountDims(Optional ByRef Array_ND As Variant) As Integer
 '''	Count the number of dimensions of an array - may be > 2


More information about the Libreoffice-commits mailing list