From: Sergey Matveev Date: Sun, 12 Mar 2023 12:04:42 +0000 (+0300) Subject: Workaround for backslash in multilines X-Git-Tag: v0.6.0^0 X-Git-Url: http://www.git.cypherpunks.ru/?p=gorecfile.git;a=commitdiff_plain;h=a24ce24d791284ed7ded63fc824c1c4e239d6d6d Workaround for backslash in multilines --- diff --git a/README b/README index b366457..5ed591a 100644 --- a/README +++ b/README @@ -7,4 +7,8 @@ an example usage. * ignore comments * support continuation lines (\$) and multilines (^+) +Limitations: +* leading spaces in the first line of the value are ignored +* trailing backslash at the end of lines is cut + It is free software: see the file COPYING for copying conditions. diff --git a/r.go b/r.go index 1cd3cb6..5de2a45 100644 --- a/r.go +++ b/r.go @@ -80,7 +80,9 @@ func (r *Reader) Next() ([]Field, error) { text = r.scanner.Text() if continuation { - if text[len(text)-1] == '\\' { + if len(text) == 0 { + continuation = false + } else if text[len(text)-1] == '\\' { lines = append(lines, text[:len(text)-1]) } else { lines = append(lines, text) diff --git a/w.go b/w.go index 4525222..32ee966 100644 --- a/w.go +++ b/w.go @@ -37,7 +37,9 @@ func (w *Writer) RecordStart() (written int, err error) { func (w *Writer) WriteFields(fs ...Field) (written int, err error) { var n int for _, f := range fs { - n, err = w.w.WriteString(f.Name + ": " + strings.TrimLeft(f.Value, " ") + "\n") + n, err = w.w.WriteString( + f.Name + ": " + strings.TrimRight(strings.TrimLeft(f.Value, " "), "\\") + "\n", + ) written += n if err != nil { return @@ -48,13 +50,15 @@ func (w *Writer) WriteFields(fs ...Field) (written int, err error) { func (w *Writer) WriteFieldMultiline(name string, lines []string) (written int, err error) { var n int - n, err = w.w.WriteString(name + ": " + strings.TrimLeft(lines[0], " ") + "\n") + n, err = w.w.WriteString( + name + ": " + strings.TrimRight(strings.TrimLeft(lines[0], " "), "\\") + "\n", + ) written += n if err != nil { return } for _, l := range lines[1:] { - n, err = w.w.WriteString("+ " + l + "\n") + n, err = w.w.WriteString("+ " + strings.TrimRight(l, "\\") + "\n") written += n if err != nil { return