diff options
author | Robert Griesemer <gri@golang.org> | 2010-11-08 15:13:35 -0800 |
---|---|---|
committer | Robert Griesemer <gri@golang.org> | 2010-11-08 15:13:35 -0800 |
commit | 9f19392f1ab275ad97454477a64ed1a016edc30f (patch) | |
tree | 907e45040b5d2b71521cd5bc06c7ac21345ed3ad | |
parent | 02469b82006f354c72b488a7b764e4c28a918bab (diff) | |
download | go-9f19392f1ab275ad97454477a64ed1a016edc30f.tar.gz go-9f19392f1ab275ad97454477a64ed1a016edc30f.zip |
os.Expand: don't call append for each non-variable char
R=r
CC=golang-dev
https://golang.org/cl/2993041
-rw-r--r-- | src/pkg/os/env.go | 18 |
1 files changed, 9 insertions, 9 deletions
diff --git a/src/pkg/os/env.go b/src/pkg/os/env.go index 9783674a7f..3a6d79dd09 100644 --- a/src/pkg/os/env.go +++ b/src/pkg/os/env.go @@ -11,17 +11,17 @@ package os func Expand(s string, mapping func(string) string) string { buf := make([]byte, 0, 2*len(s)) // ${} is all ASCII, so bytes are fine for this operation. - for i := 0; i < len(s); { - if s[i] != '$' || i == len(s)-1 { - buf = append(buf, s[i]) - i++ - continue + i := 0 + for j := 0; j < len(s); j++ { + if s[j] == '$' && j+1 < len(s) { + buf = append(buf, []byte(s[i:j])...) + name, w := getShellName(s[j+1:]) + buf = append(buf, []byte(mapping(name))...) + j += w + i = j + 1 } - name, w := getShellName(s[i+1:]) - buf = append(buf, []byte(mapping(name))...) - i += 1 + w } - return string(buf) + return string(buf) + s[i:] } // ShellExpand replaces ${var} or $var in the string according to the values |