MCPcopy
hub / github.com/uber/aresdb / ScanDelimited

Function ScanDelimited

query/expr/scanner.go:470–516  ·  view source on GitHub ↗
(r io.RuneScanner, start, end rune, escapes map[rune]rune, escapesPassThru bool)

Source from the content-addressed store, hash-verified

468const eof = rune(0)
469
470func 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.
519func ScanString(r io.RuneScanner) (string, error) {

Callers

nothing calls this directly

Calls 4

ReadRuneMethod · 0.80
UnreadRuneMethod · 0.80
ErrorfMethod · 0.65
ResetMethod · 0.45

Tested by

no test coverage detected