cmdgo_quoted.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // Code generated by golang.org/x/tools/cmd/bundle. DO NOT EDIT.
  2. // Package quoted provides string manipulation utilities.
  3. //
  4. package main
  5. import (
  6. "flag"
  7. "fmt"
  8. "strings"
  9. "unicode"
  10. )
  11. func cmdgoQuotedisSpaceByte(c byte) bool {
  12. return c == ' ' || c == '\t' || c == '\n' || c == '\r'
  13. }
  14. // Split splits s into a list of fields,
  15. // allowing single or double quotes around elements.
  16. // There is no unescaping or other processing within
  17. // quoted fields.
  18. //
  19. // Keep in sync with cmd/dist/quoted.go
  20. func cmdgoQuotedSplit(s string) ([]string, error) {
  21. // Split fields allowing '' or "" around elements.
  22. // Quotes further inside the string do not count.
  23. var f []string
  24. for len(s) > 0 {
  25. for len(s) > 0 && cmdgoQuotedisSpaceByte(s[0]) {
  26. s = s[1:]
  27. }
  28. if len(s) == 0 {
  29. break
  30. }
  31. // Accepted quoted string. No unescaping inside.
  32. if s[0] == '"' || s[0] == '\'' {
  33. quote := s[0]
  34. s = s[1:]
  35. i := 0
  36. for i < len(s) && s[i] != quote {
  37. i++
  38. }
  39. if i >= len(s) {
  40. return nil, fmt.Errorf("unterminated %c string", quote)
  41. }
  42. f = append(f, s[:i])
  43. s = s[i+1:]
  44. continue
  45. }
  46. i := 0
  47. for i < len(s) && !cmdgoQuotedisSpaceByte(s[i]) {
  48. i++
  49. }
  50. f = append(f, s[:i])
  51. s = s[i:]
  52. }
  53. return f, nil
  54. }
  55. // Join joins a list of arguments into a string that can be parsed
  56. // with Split. Arguments are quoted only if necessary; arguments
  57. // without spaces or quotes are kept as-is. No argument may contain both
  58. // single and double quotes.
  59. func cmdgoQuotedJoin(args []string) (string, error) {
  60. var buf []byte
  61. for i, arg := range args {
  62. if i > 0 {
  63. buf = append(buf, ' ')
  64. }
  65. var sawSpace, sawSingleQuote, sawDoubleQuote bool
  66. for _, c := range arg {
  67. switch {
  68. case c > unicode.MaxASCII:
  69. continue
  70. case cmdgoQuotedisSpaceByte(byte(c)):
  71. sawSpace = true
  72. case c == '\'':
  73. sawSingleQuote = true
  74. case c == '"':
  75. sawDoubleQuote = true
  76. }
  77. }
  78. switch {
  79. case !sawSpace && !sawSingleQuote && !sawDoubleQuote:
  80. buf = append(buf, arg...)
  81. case !sawSingleQuote:
  82. buf = append(buf, '\'')
  83. buf = append(buf, arg...)
  84. buf = append(buf, '\'')
  85. case !sawDoubleQuote:
  86. buf = append(buf, '"')
  87. buf = append(buf, arg...)
  88. buf = append(buf, '"')
  89. default:
  90. return "", fmt.Errorf("argument %q contains both single and double quotes and cannot be quoted", arg)
  91. }
  92. }
  93. return string(buf), nil
  94. }
  95. // A Flag parses a list of string arguments encoded with Join.
  96. // It is useful for flags like cmd/link's -extldflags.
  97. type cmdgoQuotedFlag []string
  98. var _ flag.Value = (*cmdgoQuotedFlag)(nil)
  99. func (f *cmdgoQuotedFlag) Set(v string) error {
  100. fs, err := cmdgoQuotedSplit(v)
  101. if err != nil {
  102. return err
  103. }
  104. *f = fs[:len(fs):len(fs)]
  105. return nil
  106. }
  107. func (f *cmdgoQuotedFlag) String() string {
  108. if f == nil {
  109. return ""
  110. }
  111. s, err := cmdgoQuotedJoin(*f)
  112. if err != nil {
  113. return strings.Join(*f, " ")
  114. }
  115. return s
  116. }