-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibArray.cmd
More file actions
104 lines (91 loc) · 3.78 KB
/
libArray.cmd
File metadata and controls
104 lines (91 loc) · 3.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
@echo off
REM *** Trampoline jump for function calls of the form ex. "C:\:libraryFunction:\..\temp\libThisLibraryName.cmd"
FOR /F "tokens=3 delims=:" %%L in ("%~0") DO goto :%%L
REM *** Library guard, forces to load libBase and prevents multiple loads of the same library
%$LIB_LOAD_ONCE:call "%~dp0\libBase.cmd" :=%
%$LIB_INCLUDE% libEndlocal
%$LIB_NEW_MACRO% $split
%$LIB_NEW_MACRO% $join
exit /b
:define_$split <resultArray>[=| ]<stringVar> [<delimCharVariable>] [<combine>]
::: splits the @stringVar into an array (@resultArray)
::: The delimiter can be set (default is line feed)
::: @TODO: Combine (or not) multiple adjacent delimiters
::: @TODO: support characters *?=~
::: @TODO: instead of toggling EDE, count the delimiters and use one FOR /L loop, count=strlen(tmp)-strlen(tmp_without_delims)
::: @TODO: OR toggling EDE only when disabled context
%$lib.macrodefine.free% set ^"%%Z=for %$FOR:param=$% in (1 2) do if %$FOR:param=$%==2 (%$\n%
for /F "tokens=1,2,3" %$FOR:param=1% in ("%%!argv%%! = = =") do (%$\n%
%= *** remove the local variable "argv" =% %$\n%
endlocal%$\n%
if defined %$FOR:param=~2% ( %$\n%
(set%%^^ tmp=%%!%$FOR:param=~2%%%!)%$\n%
FOR %$FOR:param=L% IN ("%%!$LF%%!") DO (%$\n%
if defined %$FOR:param=3% ( %$\n%
%= *** replace delim char with LF *** =% %$\n%
FOR %$FOR:param=#% in ("%%!%$FOR:param=3%%%!") DO ( %$\n%
set "tmp=%%!tmp:%$FOR:param=~#%=%$FOR:param=~L%%%!" %$\n%
) %$\n%
) %$\n%
%= *** prepare content, enclose each line of a multiline variable into quotes =% %$\n%
set "tmp="%%!tmp:%$FOR:param=~L%="%$FOR:param=~L%"%%!"" %$\n%
set "%$FOR:param=1%.len=" %$\n%
for /F "delims=" %$FOR:param=V% in ("%%!tmp%%!") DO ( %$\n%
if not defined %$FOR:param=1%.len ( %$\n%
endlocal %$\n%
set "%$FOR:param=1%.len=0" %$\n%
) %$\n%
setlocal EnableDelayedExpansion %$\n%
for %$FOR:param=n% in (%%!%$FOR:param=1%.len%%!) DO ( %$\n%
endlocal %$\n%
(set%%^^ %$FOR:param=1%[%$FOR:param=n%]=%$FOR:param=~V%)%$\n%
set /a %$FOR:param=1%.len=%$FOR:param=n%+1 %$\n%
) %$\n%
) %$\n%
) %$\n%
) else ( %$\n%
endlocal %$\n%
set /a %$FOR:param=1%.len=0 %$\n%
) %$\n%
set /a %$FOR:param=1%.max=%$FOR:param=1%.len-1 %$\n%
%= *** result is now in arg1[...], arg1.len and arg1.max =% %$\n%
)%$\n%
) else setlocal EnableDelayedExpansion ^& setlocal ^& set argv= "
%$endlocal% %MACRO_NAME%
exit /b
:define_$join <destVar> <srcArray> [<delimVar>]
%$lib.macrodefine.disabled%
set ^"%MACRO_NAME%=for %%$ in (1 2) do if %%$==2 (%$\n%
for /F "tokens=1,2,3" %%1 in ("!argv! $LF") do (%$\n%
%= *** remove the local variable "argv" =% %$\n%
endlocal%$\n%
%= *** copy content to temporary variable, the carets are for extended length, up to 8191chars =% %$\n%
set "joined=" %$\n%
for /L %%n in (0 1 !%%2.max!) DO ( %$\n%
if %%n == 0 ( %$\n%
(set^^ joined=!%%~2[%%n]!) %$\n%
) ELSE ( %$\n%
(set^^ joined=!joined!!%%~3!!%%~2[%%n]!) %$\n%
) %$\n%
) %$\n%
%= *** transfer the result out of scope =% %$\n%
%$$endlocalForParam1:arg1=joined% %$\n%
)%$\n%
) else setlocal EnableDelayedExpansion ^& setlocal ^& set argv= "
%$endlocal% %MACRO_NAME%
exit /b
:define_$array.join <array> <newEntryVar>
%$lib.macrodefine.disabled%
set ^"%MACRO_NAME%=for %%$ in (1 2) do if %%$==2 (%$\n%
for /F "tokens=1,2,3" %%1 in ("!argv! $LF") do (%$\n%
%= *** remove the local variable "argv" =% %$\n%
endlocal%$\n%
%= *** copy content to temporary variable, the carets are for extended length, up to 8191chars =% %$\n%
(set^^ %%1[!%%1.len!]=!%%2!) %$\n%
%= *** transfer the result out of scope =% %$\n%
%$$returnToParam1:arg1=joined% %%1[!%%1.len!] %$\n%
set /a %%1.len+=1, %%1.max=%%1.len-1 %$\n%
)%$\n%
) else setlocal EnableDelayedExpansion ^& setlocal ^& set argv= "
%$endlocal% %MACRO_NAME%
exit /b