golang.org/x/build@v0.0.0-20240506185731-218518f32b70/perfdata/query/query.go (about) 1 // Copyright 2017 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 5 // Package query provides tools for parsing a query. 6 package query 7 8 // SplitWords splits q into words using shell syntax (whitespace 9 // can be escaped with double quotes or with a backslash). 10 func SplitWords(q string) []string { 11 var words []string 12 word := make([]byte, len(q)) 13 w := 0 14 quoting := false 15 for r := 0; r < len(q); r++ { 16 switch c := q[r]; { 17 case c == '"' && quoting: 18 quoting = false 19 case quoting: 20 if c == '\\' { 21 r++ 22 } 23 if r < len(q) { 24 word[w] = q[r] 25 w++ 26 } 27 case c == '"': 28 quoting = true 29 case c == ' ', c == '\t': 30 if w > 0 { 31 words = append(words, string(word[:w])) 32 } 33 w = 0 34 case c == '\\': 35 r++ 36 fallthrough 37 default: 38 if r < len(q) { 39 word[w] = q[r] 40 w++ 41 } 42 } 43 } 44 if w > 0 { 45 words = append(words, string(word[:w])) 46 } 47 return words 48 }