golang.org/x/build@v0.0.0-20240506185731-218518f32b70/perf/app/parse.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 app 6 7 import "strings" 8 9 // parseQueryString splits a user-entered query into one or more perfdata server queries. 10 // The supported query formats are: 11 // 12 // prefix | one vs two - parsed as "prefix", {"one", "two"} 13 // prefix one vs two - parsed as "", {"prefix one", "two"} 14 // anything else - parsed as "", {"anything else"} 15 // 16 // The vs and | separators must not be quoted. 17 func parseQueryString(q string) (string, []string) { 18 var queries []string 19 var parts []string 20 var prefix string 21 quoting := false 22 for r := 0; r < len(q); { 23 switch c := q[r]; { 24 case c == '"' && quoting: 25 quoting = false 26 r++ 27 case quoting: 28 if c == '\\' { 29 r++ 30 } 31 r++ 32 case c == '"': 33 quoting = true 34 r++ 35 case c == ' ', c == '\t': 36 switch part := q[:r]; { 37 case part == "|" && prefix == "": 38 prefix = strings.Join(parts, " ") 39 parts = nil 40 case part == "vs": 41 queries = append(queries, strings.Join(parts, " ")) 42 parts = nil 43 default: 44 parts = append(parts, part) 45 } 46 q = q[r+1:] 47 r = 0 48 default: 49 if c == '\\' { 50 r++ 51 } 52 r++ 53 } 54 } 55 if len(q) > 0 { 56 parts = append(parts, q) 57 } 58 if len(parts) > 0 { 59 queries = append(queries, strings.Join(parts, " ")) 60 } 61 return prefix, queries 62 }