github.com/teknogeek/dnscontrol/v2@v2.10.1-0.20200227202244-ae299b55ba42/models/quotes.go (about)

     1  package models
     2  
     3  import "strings"
     4  
     5  // IsQuoted returns true if the string starts and ends with a double quote.
     6  func IsQuoted(s string) bool {
     7  	if s == "" {
     8  		return false
     9  	}
    10  	if len(s) < 2 {
    11  		return false
    12  	}
    13  	if s[0] == '"' && s[len(s)-1] == s[0] {
    14  		return true
    15  	}
    16  	return false
    17  }
    18  
    19  // StripQuotes returns the string with the starting and ending quotes removed.
    20  func StripQuotes(s string) string {
    21  	if IsQuoted(s) {
    22  		return s[1 : len(s)-1]
    23  	}
    24  	return s
    25  }
    26  
    27  // ParseQuotedTxt returns the individual strings of a combined quoted string.
    28  // `foo`  -> []string{"foo"}
    29  // `"foo"` -> []string{"foo"}
    30  // `"foo" "bar"` -> []string{"foo", "bar"}
    31  // NOTE: it is assumed there is exactly one space between the quotes.
    32  func ParseQuotedTxt(s string) []string {
    33  	if !IsQuoted(s) {
    34  		return []string{s}
    35  	}
    36  	return strings.Split(StripQuotes(s), `" "`)
    37  }