mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-25 20:53:33 +00:00
Merge pull request #8736 from pmorie/expansion-buffer
Use byte buffer in expansion/expand.go
This commit is contained in:
commit
d8d6fdfea7
14
third_party/golang/expansion/expand.go
vendored
14
third_party/golang/expansion/expand.go
vendored
@ -1,5 +1,9 @@
|
|||||||
package expansion
|
package expansion
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
operator = '$'
|
operator = '$'
|
||||||
referenceOpener = '('
|
referenceOpener = '('
|
||||||
@ -32,13 +36,13 @@ func MappingFuncFor(context ...map[string]string) func(string) string {
|
|||||||
// the expansion spec using the given mapping function to resolve the
|
// the expansion spec using the given mapping function to resolve the
|
||||||
// values of variables.
|
// values of variables.
|
||||||
func Expand(input string, mapping func(string) string) string {
|
func Expand(input string, mapping func(string) string) string {
|
||||||
buf := make([]byte, 0, 2*len(input))
|
var buf bytes.Buffer
|
||||||
checkpoint := 0
|
checkpoint := 0
|
||||||
for cursor := 0; cursor < len(input); cursor++ {
|
for cursor := 0; cursor < len(input); cursor++ {
|
||||||
if input[cursor] == operator && cursor+1 < len(input) {
|
if input[cursor] == operator && cursor+1 < len(input) {
|
||||||
// Copy the portion of the input string since the last
|
// Copy the portion of the input string since the last
|
||||||
// checkpoint into the buffer
|
// checkpoint into the buffer
|
||||||
buf = append(buf, input[checkpoint:cursor]...)
|
buf.WriteString(input[checkpoint:cursor])
|
||||||
|
|
||||||
// Attempt to read the variable name as defined by the
|
// Attempt to read the variable name as defined by the
|
||||||
// syntax from the input string
|
// syntax from the input string
|
||||||
@ -48,10 +52,10 @@ func Expand(input string, mapping func(string) string) string {
|
|||||||
// We were able to read a variable name correctly;
|
// We were able to read a variable name correctly;
|
||||||
// apply the mapping to the variable name and copy the
|
// apply the mapping to the variable name and copy the
|
||||||
// bytes into the buffer
|
// bytes into the buffer
|
||||||
buf = append(buf, mapping(read)...)
|
buf.WriteString(mapping(read))
|
||||||
} else {
|
} else {
|
||||||
// Not a variable name; copy the read bytes into the buffer
|
// Not a variable name; copy the read bytes into the buffer
|
||||||
buf = append(buf, read...)
|
buf.WriteString(read)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Advance the cursor in the input string to account for
|
// Advance the cursor in the input string to account for
|
||||||
@ -65,7 +69,7 @@ func Expand(input string, mapping func(string) string) string {
|
|||||||
|
|
||||||
// Return the buffer and any remaining unwritten bytes in the
|
// Return the buffer and any remaining unwritten bytes in the
|
||||||
// input string.
|
// input string.
|
||||||
return string(buf) + input[checkpoint:]
|
return buf.String() + input[checkpoint:]
|
||||||
}
|
}
|
||||||
|
|
||||||
// tryReadVariableName attempts to read a variable name from the input
|
// tryReadVariableName attempts to read a variable name from the input
|
||||||
|
Loading…
Reference in New Issue
Block a user