github.com/shaardie/u-root@v4.0.1-0.20190127173353-f24a1c26aa2e+incompatible/integration/fields.go (about)

     1  // Copyright 2019 the u-root 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 integration
     6  
     7  func isWhitespace(b byte) bool {
     8  	return b == '\t' || b == '\n' || b == '\v' ||
     9  		b == '\f' || b == '\r' || b == ' '
    10  }
    11  
    12  // fields splits the string s around each instance of one or more consecutive white space
    13  // characters, returning a slice of substrings of s or an
    14  // empty slice if s contains only white space.
    15  //
    16  // fields is similar to strings.Fields() method, two main differences are:
    17  //     fields doesn't split substring of s if substring is inside of double quotes
    18  //     fields works only with ASCII strings.
    19  func fields(s string) []string {
    20  	var ret []string
    21  	var token []byte
    22  
    23  	var quotes bool
    24  	for i := range s {
    25  		if s[i] == '"' {
    26  			quotes = !quotes
    27  		}
    28  
    29  		if !isWhitespace(s[i]) || quotes {
    30  			token = append(token, s[i])
    31  		} else if len(token) > 0 {
    32  			ret = append(ret, string(token))
    33  			token = token[:0]
    34  		}
    35  	}
    36  
    37  	if len(token) > 0 {
    38  		ret = append(ret, string(token))
    39  	}
    40  	return ret
    41  }