-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathExpand.ahk
36 lines (30 loc) · 1 KB
/
Expand.ahk
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
;Expand env vars in string, ignoring %% (double percent sequences)
;by LogicDaemon <www.logicdaemon.ru>
;This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License <http://creativecommons.org/licenses/by-sa/4.0/deed.ru>.
Expand(string) {
PrevPctChr:=0
LastPctChr:=0
VarnameJustFound:=0
output:=""
While ( LastPctChr:=InStr(string, "%", true, LastPctChr+1) ) {
If VarnameJustFound
{
EnvGet CurrEnvVar,% SubStr(string,PrevPctChr+1,LastPctChr-PrevPctChr-1)
output .= CurrEnvVar
VarnameJustFound:=0
} else {
output .= SubStr(string,PrevPctChr+1,LastPctChr-PrevPctChr-1)
If (SubStr(string, LastPctChr+1, 1) == "%") { ;double-percent %% skipped ouside of varname
output .= "%"
LastPctChr++
} else {
VarnameJustFound:=1
}
}
PrevPctChr:=LastPctChr
}
If VarnameJustFound ; That's bad, non-closed varname
Throw Exception("Var name not closed")
output .= SubStr(string,PrevPctChr+1)
return % output
}