-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
75 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package varstr | ||
|
||
// Modified from stdlib's os.Expand function. Remove special shell variable expansion. | ||
// https://github.com/golang/go/blob/0da486dc72743faa6e601444dcc35728704a1545/src/os/env.go#L13 | ||
|
||
// Expand replaces ${var} or $var in the string based on the mapping function. | ||
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. | ||
i := 0 | ||
for j := 0; j < len(s); j++ { | ||
if s[j] == '$' && j+1 < len(s) { | ||
buf = append(buf, s[i:j]...) | ||
name, w := getVarName(s[j+1:]) | ||
buf = append(buf, mapping(name)...) | ||
j += w | ||
i = j + 1 | ||
} | ||
} | ||
return string(buf) + s[i:] | ||
} | ||
|
||
// isAlphaNum reports whether the byte is an ASCII letter, number, or underscore | ||
func isAlphaNum(c uint8) bool { | ||
return c == '_' || '0' <= c && c <= '9' || 'a' <= c && c <= 'z' || 'A' <= c && c <= 'Z' | ||
} | ||
|
||
// getVarName returns the name that begins the string and the number of bytes | ||
// consumed to extract it. If the name is enclosed in {}, it's part of a ${} | ||
// expansion and two more bytes are needed than the length of the name. | ||
func getVarName(s string) (string, int) { | ||
if s[0] == '{' { | ||
// Scan to closing brace | ||
for i := 1; i < len(s); i++ { | ||
if s[i] == '}' { | ||
return s[1:i], i + 1 | ||
} | ||
} | ||
return "", 1 // Bad syntax; just eat the brace. | ||
|
||
} | ||
|
||
// Scan alphanumerics. | ||
var i int | ||
for i = 0; i < len(s) && isAlphaNum(s[i]); i++ { | ||
} | ||
return s[:i], i | ||
} |