func ExampleCanBackquote() { fmt.Println(strconv.CanBackquote("Fran & Freddie's Diner ☺")) fmt.Println(strconv.CanBackquote("`can't backquote this`")) // Output: // true // false }
func replaceStructTag(structField string, replacer func(s string) (string, error)) (string, error) { pos, end, err := parseStructTag(structField) if err != nil { return "", err } tag := structField[pos:end] if tag != "" { tag, err = strconv.Unquote(tag) if err != nil { return "", err } } tag, err = replacer(tag) if err != nil { return "", err } if strconv.CanBackquote(tag) { tag = "`" + tag + "`" } else { tag = strconv.Quote(tag) } if pos == end { tag = " " + tag } return structField[:pos] + tag + structField[end:], nil }
// fmt_q formats a string as a double-quoted, escaped Go string constant. func (f *fmt) fmt_q(s string) { var quoted string if f.sharp && strconv.CanBackquote(s) { quoted = "`" + s + "`" } else { quoted = strconv.Quote(s) } f.padString(quoted) }
// Fmt_q formats a string as a double-quoted, escaped Go string constant. func (f *Fmt) Fmt_q(s string) *Fmt { var quoted string if f.sharp && strconv.CanBackquote(s) { quoted = "`" + s + "`" } else { quoted = strconv.Quote(s) } f.pad(quoted) f.clearflags() return f }
// fmt_q formats a string as a double-quoted, escaped Go string constant. // If f.sharp is set a raw (backquoted) string may be returned instead // if the string does not contain any control characters other than tab. func (f *fmt) fmt_q(s string) { s = f.truncate(s) if f.sharp && strconv.CanBackquote(s) { f.padString("`" + s + "`") return } buf := f.intbuf[:0] if f.plus { f.pad(strconv.AppendQuoteToASCII(buf, s)) } else { f.pad(strconv.AppendQuote(buf, s)) } }
func formatTag(fieldName string) string { tag := "" tag = addSubTag(tag, "json", fieldName) if tag == "" { return "" } if strconv.CanBackquote(tag) { return "`" + tag + "`" } return strconv.Quote(tag) }
// fmt_q formats a string as a double-quoted, escaped Go string constant. func (f *fmt) fmt_q(s string) { s = f.truncate(s) var quoted string if f.sharp && strconv.CanBackquote(s) { quoted = "`" + s + "`" } else { if f.plus { quoted = strconv.QuoteToASCII(s) } else { quoted = strconv.Quote(s) } } f.padString(quoted) }
func main() { s := strconv.CanBackquote("C:\\Windows\n") fmt.Println(s) // false s = strconv.CanBackquote("C:\\Windows\r") fmt.Println(s) // false s = strconv.CanBackquote("C:\\Windows\f") fmt.Println(s) // false s = strconv.CanBackquote("C:\\Windows\t") fmt.Println(s) // true s = strconv.CanBackquote("C:\\`Windows`") fmt.Println(s) fmt.Println(strconv.CanBackquote("Fran & Freddie's Diner ☺")) fmt.Println(strconv.CanBackquote("`can't backquote this`")) }
func quote(s string) string { if strconv.CanBackquote(s) { return "`" + s + "`" } return strconv.Quote(s) }