expand.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. Copyright 2018 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. // Package expansion provides functions find and replace $(FOO) style variables in strings.
  14. package expansion
  15. import (
  16. "bytes"
  17. )
  18. const (
  19. operator = '$'
  20. referenceOpener = '('
  21. referenceCloser = ')'
  22. )
  23. // syntaxWrap returns the input string wrapped by the expansion syntax.
  24. func syntaxWrap(input string) string {
  25. return string(operator) + string(referenceOpener) + input + string(referenceCloser)
  26. }
  27. // MappingFuncFor returns a mapping function for use with Expand that
  28. // implements the expansion semantics defined in the expansion spec; it
  29. // returns the input string wrapped in the expansion syntax if no mapping
  30. // for the input is found.
  31. func MappingFuncFor(
  32. counts map[string]int,
  33. context ...map[string]string) func(string) string {
  34. return func(input string) string {
  35. for _, vars := range context {
  36. val, ok := vars[input]
  37. if ok {
  38. counts[input]++
  39. return val
  40. }
  41. }
  42. return syntaxWrap(input)
  43. }
  44. }
  45. // Expand replaces variable references in the input string according to
  46. // the expansion spec using the given mapping function to resolve the
  47. // values of variables.
  48. func Expand(input string, mapping func(string) string) string {
  49. var buf bytes.Buffer
  50. checkpoint := 0
  51. for cursor := 0; cursor < len(input); cursor++ {
  52. if input[cursor] == operator && cursor+1 < len(input) {
  53. // Copy the portion of the input string since the last
  54. // checkpoint into the buffer
  55. buf.WriteString(input[checkpoint:cursor])
  56. // Attempt to read the variable name as defined by the
  57. // syntax from the input string
  58. read, isVar, advance := tryReadVariableName(input[cursor+1:])
  59. if isVar {
  60. // We were able to read a variable name correctly;
  61. // apply the mapping to the variable name and copy the
  62. // bytes into the buffer
  63. buf.WriteString(mapping(read))
  64. } else {
  65. // Not a variable name; copy the read bytes into the buffer
  66. buf.WriteString(read)
  67. }
  68. // Advance the cursor in the input string to account for
  69. // bytes consumed to read the variable name expression
  70. cursor += advance
  71. // Advance the checkpoint in the input string
  72. checkpoint = cursor + 1
  73. }
  74. }
  75. // Return the buffer and any remaining unwritten bytes in the
  76. // input string.
  77. return buf.String() + input[checkpoint:]
  78. }
  79. // tryReadVariableName attempts to read a variable name from the input
  80. // string and returns the content read from the input, whether that content
  81. // represents a variable name to perform mapping on, and the number of bytes
  82. // consumed in the input string.
  83. //
  84. // The input string is assumed not to contain the initial operator.
  85. func tryReadVariableName(input string) (string, bool, int) {
  86. switch input[0] {
  87. case operator:
  88. // Escaped operator; return it.
  89. return input[0:1], false, 1
  90. case referenceOpener:
  91. // Scan to expression closer
  92. for i := 1; i < len(input); i++ {
  93. if input[i] == referenceCloser {
  94. return input[1:i], true, i + 1
  95. }
  96. }
  97. // Incomplete reference; return it.
  98. return string(operator) + string(referenceOpener), false, 1
  99. default:
  100. // Not the beginning of an expression, ie, an operator
  101. // that doesn't begin an expression. Return the operator
  102. // and the first rune in the string.
  103. return string(operator) + string(input[0]), false, 1
  104. }
  105. }