MCPcopy
hub / github.com/google/go-cmp / formatString

Function formatString

cmp/report_reflect.go:373–390  ·  view source on GitHub ↗

formatString prints s as a double-quoted or backtick-quoted string.

(s string)

Source from the content-addressed store, hash-verified

371
372// formatString prints s as a double-quoted or backtick-quoted string.
373func 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.
393func formatHex(u uint64) string {

Callers 2

FormatDiffSliceMethod · 0.85
formatStringMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected