1// Copyright 2022 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5package main
6
7import "fmt"
8
9// quotedSplit is a verbatim copy from cmd/internal/quoted.go:Split and its
10// dependencies (isSpaceByte). Since this package is built using the host's
11// Go compiler, it cannot use `cmd/internal/...`. We also don't want to export
12// it to all Go users.
13//
14// Please keep those in sync.
15func quotedSplit(s string) ([]string, error) {
16	// Split fields allowing '' or "" around elements.
17	// Quotes further inside the string do not count.
18	var f []string
19	for len(s) > 0 {
20		for len(s) > 0 && isSpaceByte(s[0]) {
21			s = s[1:]
22		}
23		if len(s) == 0 {
24			break
25		}
26		// Accepted quoted string. No unescaping inside.
27		if s[0] == '"' || s[0] == '\'' {
28			quote := s[0]
29			s = s[1:]
30			i := 0
31			for i < len(s) && s[i] != quote {
32				i++
33			}
34			if i >= len(s) {
35				return nil, fmt.Errorf("unterminated %c string", quote)
36			}
37			f = append(f, s[:i])
38			s = s[i+1:]
39			continue
40		}
41		i := 0
42		for i < len(s) && !isSpaceByte(s[i]) {
43			i++
44		}
45		f = append(f, s[:i])
46		s = s[i:]
47	}
48	return f, nil
49}
50
51func isSpaceByte(c byte) bool {
52	return c == ' ' || c == '\t' || c == '\n' || c == '\r'
53}
54