MCPcopy Index your code
hub / github.com/pkg/errors / parseBlocks

Function parseBlocks

format_test.go:428–479  ·  view source on GitHub ↗

parseBlocks parses input into a slice, where: - incase entry contains a newline, its a stacktrace - incase entry contains no newline, its a solo line. Detecting stack boundaries only works incase the WithStack-calls are to be found on the same line, thats why it is optionally here. Example use: f

(input string, detectStackboundaries bool)

Source from the content-addressed store, hash-verified

426// }
427//
428func parseBlocks(input string, detectStackboundaries bool) ([]string, error) {
429 var blocks []string
430
431 stack := ""
432 wasStack := false
433 lines := map[string]bool{} // already found lines
434
435 for _, l := range strings.Split(input, "\n") {
436 isStackLine := stackLineR.MatchString(l)
437
438 switch {
439 case !isStackLine && wasStack:
440 blocks = append(blocks, stack, l)
441 stack = ""
442 lines = map[string]bool{}
443 case isStackLine:
444 if wasStack {
445 // Detecting two stacks after another, possible cause lines match in
446 // our tests due to WithStack(WithStack(io.EOF)) on same line.
447 if detectStackboundaries {
448 if lines[l] {
449 if len(stack) == 0 {
450 return nil, errors.New("len of block must not be zero here")
451 }
452
453 blocks = append(blocks, stack)
454 stack = l
455 lines = map[string]bool{l: true}
456 continue
457 }
458 }
459
460 stack = stack + "\n" + l
461 } else {
462 stack = l
463 }
464 lines[l] = true
465 case !isStackLine && !wasStack:
466 blocks = append(blocks, l)
467 default:
468 return nil, errors.New("must not happen")
469 }
470
471 wasStack = isStackLine
472 }
473
474 // Use up stack
475 if stack != "" {
476 blocks = append(blocks, stack)
477 }
478 return blocks, nil
479}
480
481func testFormatCompleteCompare(t *testing.T, n int, arg interface{}, format string, want []string, detectStackBoundaries bool) {
482 gotStr := fmt.Sprintf(format, arg)

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…