-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibString.cmd
More file actions
283 lines (259 loc) · 10.4 KB
/
libString.cmd
File metadata and controls
283 lines (259 loc) · 10.4 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
@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
%$LIB_LOAD_ONCE:call "%~dp0\libBase.cmd" :=%
%$LIB_INCLUDE% libEndlocal
%$LIB_NEW_MACRO% $strLen
%$LIB_NEW_MACRO% $trim
%$LIB_NEW_MACRO% $rightTrim
%$LIB_NEW_MACRO% $leftTrim
%$LIB_NEW_MACRO% $strNOP
%$LIB_NEW_MACRO% $toUpper
%$LIB_NEW_MACRO% $toLower
exit /b
:define_$strLen <resultVar> <strVar>
REM *** To build a real long string use:
REM *** set "longTest=."
REM *** for /L %%n in (1 1 14) DO (set^ longTest=!longTest:~-4000!!longTest:~-4191!)
REM setlocal DisableDelayedExpansion
%$lib.macrodefine.free% set ^"%%Z=for %$FOR:param=$% in (1 2) do if %$FOR:param=$%==2 (%$\n%
for /F "tokens=1,2 delims== " %$FOR:param=1% in ("%%!argv%%!") 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%%^^ tmp=%%!%$FOR:param=~2%%%!)%$\n%
if defined tmp (%$\n%
set len=1%$\n%
for %$FOR:param=#% in (4096 2048 1024 512 256 128 64 32 16 8 4 2 1) do (%$\n%
if "%%!tmp:~%$FOR:param=#%,1%%!" neq "" (%$\n%
set /a "len+=%$FOR:param=#%"%$\n%
set "tmp=%%!tmp:~%$FOR:param=#%%%!"%$\n%
)%$\n%
)%$\n%
) ELSE set len=0%$\n%
%= *** return the result out of scope =% %$\n%
%= SIMPLE Return =% %$\n%
for %$FOR:param=V% in (%%!len%%!) do endlocal ^& set "%$FOR:param=~1%=%$FOR:param=V%" %$\n%
)%$\n%
) else setlocal EnableDelayedExpansion ^& setlocal ^& set argv= "
@exit /b
REM *** To left trim a string, remove all white spaces from the left side
:define_$leftTrim <resultVar> <strVar> [<delimCharacters>]
%$lib.macrodefine.disabled%
@set ^"%MACRO_NAME%=for %%$ in (1 2) do if %%$==2 (%$\n%
for /F "tokens=1,2,3" %%1 in ("!argv!") 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%
if defined %%~2 (%$\n%
%= *** if arg3 is set to 1, treat CR and LF also as white spaces, else as normal chars *** =% %$\n%
(set^^ str=!%%~2!)%$\n%
if "%%~3" == "1" ( %$\n%
set "CRLF= " %$\n%
) ELSE ( %$\n%
set "CRLF=#" %$\n%
) %$\n%
set /a headLen=0 %$\n%
%= *** Replace CR and LF with space or #, depends on arg3 *** =% %$\n%
FOR %%R in ("!CRLF!") DO ( %$\n%
for %%C in ("!$LF!" "!$CR!") DO set "tmp=!str:%%~C=%%~R!" %$\n%
) %$\n%
%= replace all ; with #, to avoid the EOL char =% %$\n%
set "tmp=!tmp:;=#!" %$\n%
%= *** Binary search for white spaces *** =% %$\n%
FOR %%n in (4096 2048 1024 512 256 128 64 32 16 8 4 2 1) do ( %$\n%
%= *** left trim, search for spaces *** =% %$\n%
set /a totalLen=headLen + %%n %$\n%
for %%P in (!totalLen!) DO set "head=!tmp:~0,%%P!" %$\n%
set "foundChars=" %$\n%
FOR /F "tokens=1" %%5 in ("!head!") DO set foundChars=yes %$\n%
if not defined foundChars ( %$\n%
%= *** remove <n> more spaces *** = % %$\n%
set /a headLen+=%%n %$\n%
) %$\n%
) %$\n%
for /f "tokens=1" %%1 in ("!headLen!") do ( %$\n%
set "str=!str:~%%1!" %$\n%
) %$\n%
) ELSE ( %$\n%
set "str=" %$\n%
set #1 %$\n%
) %$\n%
%= *** copy the result =% %$\n%
%$$endlocalForParam1:arg1=str% %$\n%
)%$\n%
) else setlocal EnableDelayedExpansion ^& setlocal EnableDelayedExpansion ^& set argv= "
@%$endlocal% %MACRO_NAME%
@exit /b
:define_$rightTrim <resultVar> <strVar> [<delimCharacters>]
REM *** To right trim a string, remove all white spaces from the right side
REM *** Line feed and carriage returns can also be treated as white spaces
%$lib.macrodefine.disabled%
@set ^"%MACRO_NAME%=for %%$ in (1 2) do if %%$==2 (%$\n%
for /F "tokens=1,2,3" %%1 in ("!argv!") 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%
if defined %%~2 (%$\n%
%= *** if arg3 is set to 1, treat CR and LF also as white spaces, else as normal chars *** =% %$\n%
(set^^ str=!%%~2!) %$\n%
if "%%~3" == "1" ( %$\n%
set "CRLF= " %$\n%
) ELSE ( %$\n%
set "CRLF=#" %$\n%
) %$\n%
set /a trailLen=0 %$\n%
%= *** Replace CR and LF with space or #, depends on arg3 *** =% %$\n%
FOR %%R in ("!CRLF!") DO ( %$\n%
for %%C in ("!$LF!" "!$CR!") DO set "tmp=!str:%%~C=%%~R!" %$\n%
) %$\n%
%= replace all ; with #, to avoid the EOL char =% %$\n%
set "tmp=!tmp:;=#!" %$\n%
%= *** Binary search for white spaces *** =% %$\n%
FOR %%n in (4096 2048 1024 512 256 128 64 32 16 8 4 2 1) do ( %$\n%
set /a totalLen=trailLen + %%n %$\n%
for %%7 in (!totalLen!) DO set "tail=!tmp:~-%%7!" %$\n%
set "foundChars=" %$\n%
FOR /F "tokens=1" %%8 in ("!tail!") DO set foundChars=yes %$\n%
if not defined foundChars ( %$\n%
%= *** remove <n> more spaces *** = % %$\n%
set /a trailLen+=%%n %$\n%
) %$\n%
) %$\n%
if "!trailLen!" NEQ "0" for %%n in (!trailLen!) do ( %$\n%
set "str=!%%~2:~0,-%%n!" %$\n%
) %$\n%
) ELSE ( %$\n%
set "str=" %$\n%
) %$\n%
%= *** copy the result =% %$\n%
%$$endlocalForParam1:arg1=str% %$\n%
)%$\n%
) else setlocal EnableDelayedExpansion ^& setlocal EnableDelayedExpansion ^& set argv= "
@%$endlocal% %MACRO_NAME%
@exit /b
:define_$trim <resultVar> <strVar> [<delimCharacters>]
REM *** To trim a string, remove all white spaces from the left and right side
REM *** Line feed and carriage returns can also be treated as white spaces
%$lib.macrodefine.disabled%
@set ^"%MACRO_NAME%=for %%$ in (1 2) do if %%$==2 (%$\n%
for /F "tokens=1,2,3" %%1 in ("!argv!") 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%
if defined %%~2 (%$\n%
%= *** if arg3 is set to 1, treat CR and LF also as white spaces, else as normal chars *** =% %$\n%
(set^^ str=!%%~2!)%$\n%
if "%%~3" == "1" ( %$\n%
set "CRLF= " %$\n%
) ELSE ( %$\n%
set "CRLF=#" %$\n%
) %$\n%
set /a headLen=0 %$\n%
set /a trailLen=0 %$\n%
%= *** Replace CR and LF with space or #, depends on arg3 *** =% %$\n%
FOR %%R in ("!CRLF!") DO ( %$\n%
for %%C in ("!$LF!" "!$CR!") DO set "tmp=!str:%%~C=%%~R!" %$\n%
) %$\n%
%= replace all ; with #, to avoid the EOL char =% %$\n%
set "tmp=!tmp:;=#!" %$\n%
%= *** Binary search for white spaces *** =% %$\n%
FOR %%n in (4096 2048 1024 512 256 128 64 32 16 8 4 2 1) do ( %$\n%
%= *** left trim, search for spaces *** =% %$\n%
set /a totalLen=headLen + %%n %$\n%
for %%7 in (!totalLen!) DO set "head=!tmp:~0,%%7!" %$\n%
set "foundChars=" %$\n%
FOR /F "tokens=1" %%8 in ("!head!") DO set foundChars=yes %$\n%
if not defined foundChars ( %$\n%
%= *** remove <n> more spaces *** = % %$\n%
set /a headLen+=%%n %$\n%
) %$\n%
%= *** right trim, search for spaces *** =% %$\n%
set /a totalLen=trailLen + %%n %$\n%
for %%7 in (!totalLen!) DO set "tail=!tmp:~-%%7!" %$\n%
set "foundChars=" %$\n%
FOR /F "tokens=1" %%8 in ("!tail!") DO set foundChars=yes %$\n%
if not defined foundChars ( %$\n%
%= *** remove <n> more spaces *** = % %$\n%
set /a trailLen+=%%n %$\n%
) %$\n%
) %$\n%
for /f "tokens=1,2" %%1 in ("!headLen! !trailLen!") do ( %$\n%
if "!trailLen!" EQU "0" ( %$\n%
set "str=!str:~%%1!" %$\n%
) ELSE ( %$\n%
set "str=!str:~%%1,-%%2!" %$\n%
) %$\n%
) %$\n%
) ELSE ( %$\n%
set "str=" %$\n%
) %$\n%
%= *** copy the result =% %$\n%
%$$endlocalForParam1:arg1=str% %$\n%
)%$\n%
) else setlocal EnableDelayedExpansion ^& setlocal EnableDelayedExpansion ^& set argv= "
@%$endlocal% %MACRO_NAME%
@exit /b
:define_$strNOP
::: Limits: The maximum length is 8179 characters (limited by $$endlocalForParam1)
%$lib.macrodefine.disabled%
@SET "DEBUG_SCOPE_DEPTH= ^& set /a scopeDepth+=1"
@set ^"%MACRO_NAME%=for %%$ in (1 2) do if %%$==2 (%$\n%
for /F "tokens=1,2" %%1 in ("!argv!") do (%$\n%
%= *** remove the local variable "argv" =% %$\n%
endlocal%$\n%
%= *** copy variable content to V, the carets are for extended length > 8189 chars =% %$\n%
(set^^^ str=!%%~2!)%$\n%
%$$endlocalForParam1:arg1=str% %$\n%
)%$\n%
) else setlocal EnableDelayedExpansion%DEBUG_SCOPE_DEPTH% ^& setlocal EnableDelayedExpansion%DEBUG_SCOPE_DEPTH% ^& set argv= "
@%$endlocal% %MACRO_NAME%
@exit /b
:define_$toLower <resultVar>=<strVar>
::: toLower <strVar> - Stores the result in the same variable
%$lib.macrodefine.disabled%
@set ^"%MACRO_NAME%=for %%$ in (1 2) do if %%$==2 (%$\n%
for /F "tokens=1,2 delims== " %%1 in ("!argv! !argv!") 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%
if defined %%~2 (%$\n%
(set^^ str=!%%~2!) %$\n%
for %%C in ("A=a" "B=b" "C=c" "D=d" "E=e" "F=f" "G=g" "H=h" "I=i" %$\n%
"J=j" "K=k" "L=l" "M=m" "N=n" "O=o" "P=p" "Q=q" "R=r" %$\n%
"S=s" "T=t" "U=u" "V=v" "W=w" "X=x" "Y=y" "Z=z" "Ä=ä" %$\n%
"Ö=ö" "Ü=ü") do ( %$\n%
(set^^ str=!str:%%~C!) %$\n%
)%$\n%
) ELSE ( %$\n%
set "str=" %$\n%
) %$\n%
%$$endlocalForParam1:arg1=str% %$\n%
)%$\n%
) else setlocal EnableDelayedExpansion ^& setlocal EnableDelayedExpansion ^& set argv= "
@%$endlocal% %MACRO_NAME%
@exit /b
:define_$toUpper <resultVar>=<strVar>
::: toUpper <strVar> - Stores the result in the same variable
%$lib.macrodefine.disabled%
@set ^"%MACRO_NAME%=for %%$ in (1 2) do if %%$==2 (%$\n%
for /F "tokens=1,2 delims== " %%1 in ("!argv! !argv!") 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%
if defined %%~2 (%$\n%
(set^^ str=!%%~2!)%$\n%
for %%C in ("A=A" "B=B" "C=C" "D=D" "E=E" "F=F" "G=G" "H=H" "I=I" %$\n%
"J=J" "K=K" "L=L" "M=M" "N=N" "O=O" "P=P" "Q=Q" "R=R" %$\n%
"S=S" "T=T" "U=U" "V=V" "W=W" "X=X" "Y=Y" "Z=Z" "Ä=Ä" %$\n%
"Ö=Ö" "Ü=Ü") do ( %$\n%
(set^^ str=!str:%%~C!) %$\n%
)%$\n%
) ELSE ( %$\n%
set "str=" %$\n%
) %$\n%
%$$endlocalForParam1:arg1=str% %$\n%
)%$\n%
) else setlocal EnableDelayedExpansion ^& setlocal EnableDelayedExpansion ^& set argv= "
@%$endlocal% %MACRO_NAME%
@exit /b