expand.go 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // Copyright 2016 Google Inc. All rights reserved.
  2. //
  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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package android
  15. import (
  16. "fmt"
  17. "strings"
  18. "unicode"
  19. "github.com/google/blueprint/proptools"
  20. )
  21. // ExpandNinjaEscaped substitutes $() variables in a string
  22. // $(var) is passed to mapping(var), which should return the expanded value, a bool for whether the result should
  23. // be left unescaped when using in a ninja value (generally false, true if the expanded value is a ninja variable like
  24. // '${in}'), and an error.
  25. // $$ is converted to $, which is escaped back to $$.
  26. func ExpandNinjaEscaped(s string, mapping func(string) (string, bool, error)) (string, error) {
  27. return expand(s, true, mapping)
  28. }
  29. // Expand substitutes $() variables in a string
  30. // $(var) is passed to mapping(var), which should return the expanded value and an error.
  31. // $$ is converted to $.
  32. func Expand(s string, mapping func(string) (string, error)) (string, error) {
  33. return expand(s, false, func(s string) (string, bool, error) {
  34. s, err := mapping(s)
  35. return s, false, err
  36. })
  37. }
  38. func expand(s string, ninjaEscape bool, mapping func(string) (string, bool, error)) (string, error) {
  39. // based on os.Expand
  40. buf := make([]byte, 0, 2*len(s))
  41. i := 0
  42. for j := 0; j < len(s); j++ {
  43. if s[j] == '$' {
  44. if j+1 >= len(s) {
  45. return "", fmt.Errorf("expected character after '$'")
  46. }
  47. buf = append(buf, s[i:j]...)
  48. value, ninjaVariable, w, err := getMapping(s[j+1:], mapping)
  49. if err != nil {
  50. return "", err
  51. }
  52. if !ninjaVariable && ninjaEscape {
  53. value = proptools.NinjaEscape(value)
  54. }
  55. buf = append(buf, value...)
  56. j += w
  57. i = j + 1
  58. }
  59. }
  60. return string(buf) + s[i:], nil
  61. }
  62. func getMapping(s string, mapping func(string) (string, bool, error)) (string, bool, int, error) {
  63. switch s[0] {
  64. case '(':
  65. // Scan to closing brace
  66. for i := 1; i < len(s); i++ {
  67. if s[i] == ')' {
  68. ret, ninjaVariable, err := mapping(strings.TrimSpace(s[1:i]))
  69. return ret, ninjaVariable, i + 1, err
  70. }
  71. }
  72. return "", false, len(s), fmt.Errorf("missing )")
  73. case '$':
  74. return "$", false, 1, nil
  75. default:
  76. i := strings.IndexFunc(s, unicode.IsSpace)
  77. if i == 0 {
  78. return "", false, 0, fmt.Errorf("unexpected character '%c' after '$'", s[0])
  79. } else if i == -1 {
  80. i = len(s)
  81. }
  82. return "", false, 0, fmt.Errorf("expected '(' after '$', did you mean $(%s)?", s[:i])
  83. }
  84. }