cmdgo_quoted.go 2.8 KB

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