diff options
author | Jean-Pierre Ledure <jp@ledure.be> | 2021-02-08 18:25:58 +0100 |
---|---|---|
committer | Jean-Pierre Ledure <jp@ledure.be> | 2021-02-09 10:56:24 +0100 |
commit | c8ac8acca8442335321e20801f4c89357cf64894 (patch) | |
tree | a2de332e11ac9a7d2f2e7d2c74d2b6f4eee09bb3 /wizards/source/scriptforge | |
parent | 5177c672bb1eb848cc1da5d96c5871e8da6adb0f (diff) |
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@ledure.be>
Tested-by: Jenkins
Reviewed-by: Jean-Pierre Ledure <jp@ledure.be>
Diffstat (limited to 'wizards/source/scriptforge')
-rw-r--r-- | wizards/source/scriptforge/SF_Array.xba | 52 |
1 files changed, 52 insertions, 0 deletions
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 @@ -371,6 +371,58 @@ Catch: 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 ''' Args: |