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

Method formatString

cmp/report_reflect.go:314–355  ·  view source on GitHub ↗
(prefix, s string)

Source from the content-addressed store, hash-verified

312}
313
314func (opts formatOptions) formatString(prefix, s string) textNode {
315 maxLen := len(s)
316 maxLines := strings.Count(s, "\n") + 1
317 if opts.LimitVerbosity {
318 maxLen = (1 << opts.verbosity()) << 5 // 32, 64, 128, 256, etc...
319 maxLines = (1 << opts.verbosity()) << 2 // 4, 8, 16, 32, 64, etc...
320 }
321
322 // For multiline strings, use the triple-quote syntax,
323 // but only use it when printing removed or inserted nodes since
324 // we only want the extra verbosity for those cases.
325 lines := strings.Split(strings.TrimSuffix(s, "\n"), "\n")
326 isTripleQuoted := len(lines) >= 4 && (opts.DiffMode == '-' || opts.DiffMode == '+')
327 for i := 0; i < len(lines) && isTripleQuoted; i++ {
328 lines[i] = strings.TrimPrefix(strings.TrimSuffix(lines[i], "\r"), "\r") // trim leading/trailing carriage returns for legacy Windows endline support
329 isPrintable := func(r rune) bool {
330 return unicode.IsPrint(r) || r == '\t' // specially treat tab as printable
331 }
332 line := lines[i]
333 isTripleQuoted = !strings.HasPrefix(strings.TrimPrefix(line, prefix), `"""`) && !strings.HasPrefix(line, "...") && strings.TrimFunc(line, isPrintable) == "" && len(line) <= maxLen
334 }
335 if isTripleQuoted {
336 var list textList
337 list = append(list, textRecord{Diff: opts.DiffMode, Value: textLine(prefix + `"""`), ElideComma: true})
338 for i, line := range lines {
339 if numElided := len(lines) - i; i == maxLines-1 && numElided > 1 {
340 comment := commentString(fmt.Sprintf("%d elided lines", numElided))
341 list = append(list, textRecord{Diff: opts.DiffMode, Value: textEllipsis, ElideComma: true, Comment: comment})
342 break
343 }
344 list = append(list, textRecord{Diff: opts.DiffMode, Value: textLine(line), ElideComma: true})
345 }
346 list = append(list, textRecord{Diff: opts.DiffMode, Value: textLine(prefix + `"""`), ElideComma: true})
347 return &textWrap{Prefix: "(", Value: list, Suffix: ")"}
348 }
349
350 // Format the string as a single-line quoted string.
351 if len(s) > maxLen+len(textEllipsis) {
352 return textLine(prefix + formatString(s[:maxLen]) + string(textEllipsis))
353 }
354 return textLine(prefix + formatString(s))
355}
356
357// formatMapKey formats v as if it were a map key.
358// The result is guaranteed to be a single line.

Callers 1

FormatValueMethod · 0.95

Calls 4

verbosityMethod · 0.95
textLineTypeAlias · 0.85
commentStringTypeAlias · 0.85
formatStringFunction · 0.85

Tested by

no test coverage detected