(r io.RuneScanner, start, end rune, escapes map[rune]rune, escapesPassThru bool)
| 468 | const eof = rune(0) |
| 469 | |
| 470 | func ScanDelimited(r io.RuneScanner, start, end rune, escapes map[rune]rune, escapesPassThru bool) ([]byte, error) { |
| 471 | // Scan start delimiter. |
| 472 | if ch, _, err := r.ReadRune(); err != nil { |
| 473 | return nil, err |
| 474 | } else if ch != start { |
| 475 | return nil, fmt.Errorf("expected %s; found %s", string(start), string(ch)) |
| 476 | } |
| 477 | |
| 478 | var buf bytes.Buffer |
| 479 | for { |
| 480 | ch0, _, err := r.ReadRune() |
| 481 | if ch0 == end { |
| 482 | return buf.Bytes(), nil |
| 483 | } else if err != nil { |
| 484 | return buf.Bytes(), err |
| 485 | } else if ch0 == '\n' { |
| 486 | return nil, errors.New("delimited text contains new line") |
| 487 | } else if ch0 == '\\' { |
| 488 | // If the next character is an escape then write the escaped char. |
| 489 | // If it's not a valid escape then return an error. |
| 490 | ch1, _, err := r.ReadRune() |
| 491 | if err != nil { |
| 492 | return nil, err |
| 493 | } |
| 494 | |
| 495 | c, ok := escapes[ch1] |
| 496 | if !ok { |
| 497 | if escapesPassThru { |
| 498 | // Unread ch1 (char after the \) |
| 499 | _ = r.UnreadRune() |
| 500 | // Write ch0 (\) to the output buffer. |
| 501 | _, _ = buf.WriteRune(ch0) |
| 502 | continue |
| 503 | } else { |
| 504 | buf.Reset() |
| 505 | _, _ = buf.WriteRune(ch0) |
| 506 | _, _ = buf.WriteRune(ch1) |
| 507 | return buf.Bytes(), errBadEscape |
| 508 | } |
| 509 | } |
| 510 | |
| 511 | _, _ = buf.WriteRune(c) |
| 512 | } else { |
| 513 | _, _ = buf.WriteRune(ch0) |
| 514 | } |
| 515 | } |
| 516 | } |
| 517 | |
| 518 | // ScanString reads a quoted string from a rune reader. |
| 519 | func ScanString(r io.RuneScanner) (string, error) { |
nothing calls this directly
no test coverage detected