formatString prints s as a double-quoted or backtick-quoted string.
(s string)
| 371 | |
| 372 | // formatString prints s as a double-quoted or backtick-quoted string. |
| 373 | func formatString(s string) string { |
| 374 | // Use quoted string if it the same length as a raw string literal. |
| 375 | // Otherwise, attempt to use the raw string form. |
| 376 | qs := strconv.Quote(s) |
| 377 | if len(qs) == 1+len(s)+1 { |
| 378 | return qs |
| 379 | } |
| 380 | |
| 381 | // Disallow newlines to ensure output is a single line. |
| 382 | // Only allow printable runes for readability purposes. |
| 383 | rawInvalid := func(r rune) bool { |
| 384 | return r == '`' || r == '\n' || !(unicode.IsPrint(r) || r == '\t') |
| 385 | } |
| 386 | if utf8.ValidString(s) && strings.IndexFunc(s, rawInvalid) < 0 { |
| 387 | return "`" + s + "`" |
| 388 | } |
| 389 | return qs |
| 390 | } |
| 391 | |
| 392 | // formatHex prints u as a hexadecimal integer in Go notation. |
| 393 | func formatHex(u uint64) string { |
no outgoing calls
no test coverage detected