github.com/c-darwin/mobile@v0.0.0-20160313183840-ff625c46f7c9/cmd/gomobile/strings_flag.go (about) 1 // Copyright 2015 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 main 6 7 import "fmt" 8 9 type stringsFlag []string 10 11 func (v *stringsFlag) Set(s string) error { 12 var err error 13 *v, err = splitQuotedFields(s) 14 if *v == nil { 15 *v = []string{} 16 } 17 return err 18 } 19 20 func isSpaceByte(c byte) bool { 21 return c == ' ' || c == '\t' || c == '\n' || c == '\r' 22 } 23 24 func splitQuotedFields(s string) ([]string, error) { 25 // Split fields allowing '' or "" around elements. 26 // Quotes further inside the string do not count. 27 var f []string 28 for len(s) > 0 { 29 for len(s) > 0 && isSpaceByte(s[0]) { 30 s = s[1:] 31 } 32 if len(s) == 0 { 33 break 34 } 35 // Accepted quoted string. No unescaping inside. 36 if s[0] == '"' || s[0] == '\'' { 37 quote := s[0] 38 s = s[1:] 39 i := 0 40 for i < len(s) && s[i] != quote { 41 i++ 42 } 43 if i >= len(s) { 44 return nil, fmt.Errorf("unterminated %c string", quote) 45 } 46 f = append(f, s[:i]) 47 s = s[i+1:] 48 continue 49 } 50 i := 0 51 for i < len(s) && !isSpaceByte(s[i]) { 52 i++ 53 } 54 f = append(f, s[:i]) 55 s = s[i:] 56 } 57 return f, nil 58 } 59 60 func (v *stringsFlag) String() string { 61 return "<stringsFlag>" 62 }