mirror of
https://github.com/golang/go
synced 2024-11-14 07:20:22 -07:00
os.Expand: don't call append for each non-variable char
R=r CC=golang-dev https://golang.org/cl/2993041
This commit is contained in:
parent
02469b8200
commit
9f19392f1a
@ -11,17 +11,17 @@ package os
|
|||||||
func Expand(s string, mapping func(string) string) string {
|
func Expand(s string, mapping func(string) string) string {
|
||||||
buf := make([]byte, 0, 2*len(s))
|
buf := make([]byte, 0, 2*len(s))
|
||||||
// ${} is all ASCII, so bytes are fine for this operation.
|
// ${} is all ASCII, so bytes are fine for this operation.
|
||||||
for i := 0; i < len(s); {
|
i := 0
|
||||||
if s[i] != '$' || i == len(s)-1 {
|
for j := 0; j < len(s); j++ {
|
||||||
buf = append(buf, s[i])
|
if s[j] == '$' && j+1 < len(s) {
|
||||||
i++
|
buf = append(buf, []byte(s[i:j])...)
|
||||||
continue
|
name, w := getShellName(s[j+1:])
|
||||||
}
|
|
||||||
name, w := getShellName(s[i+1:])
|
|
||||||
buf = append(buf, []byte(mapping(name))...)
|
buf = append(buf, []byte(mapping(name))...)
|
||||||
i += 1 + w
|
j += w
|
||||||
|
i = j + 1
|
||||||
}
|
}
|
||||||
return string(buf)
|
}
|
||||||
|
return string(buf) + s[i:]
|
||||||
}
|
}
|
||||||
|
|
||||||
// ShellExpand replaces ${var} or $var in the string according to the values
|
// ShellExpand replaces ${var} or $var in the string according to the values
|
||||||
|
Loading…
Reference in New Issue
Block a user