]> Cypherpunks.ru repositories - gorecfile.git/commitdiff
Workaround for backslash in multilines v0.6.0
authorSergey Matveev <stargrave@stargrave.org>
Sun, 12 Mar 2023 12:04:42 +0000 (15:04 +0300)
committerSergey Matveev <stargrave@stargrave.org>
Sun, 12 Mar 2023 12:27:11 +0000 (15:27 +0300)
README
r.go
w.go

diff --git a/README b/README
index b36645765288f7b5e4824ba7c37db2f07502b017..5ed591aab4362102572d4d52d8d8a419c4432e3f 100644 (file)
--- a/README
+++ b/README
@@ -7,4 +7,8 @@ an example usage.
 * ignore comments
 * support continuation lines (\$) and multilines (^+)
 
 * 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.
 It is free software: see the file COPYING for copying conditions.
diff --git a/r.go b/r.go
index 1cd3cb6071a6c6d345f108561dbeb5fb4ee95f71..5de2a455a4f5d0c315c406bd1d1aa3a56c3a375e 100644 (file)
--- a/r.go
+++ b/r.go
@@ -80,7 +80,9 @@ func (r *Reader) Next() ([]Field, error) {
                text = r.scanner.Text()
 
                if continuation {
                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)
                                lines = append(lines, text[:len(text)-1])
                        } else {
                                lines = append(lines, text)
diff --git a/w.go b/w.go
index 4525222c1f2b69cd4c8d8d8b64d95b3fadb7ed32..32ee966c1e22e14454866f5366c08ff3ac304d25 100644 (file)
--- 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 {
 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
                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
 
 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:] {
        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
                written += n
                if err != nil {
                        return