From d8b7a4e509a641ea2949646c5a3577bc848fea30 Mon Sep 17 00:00:00 2001 From: Shulhan Date: Fri, 10 Nov 2023 15:07:52 +0700 Subject: script: respect spaces when joining multi lines command If a multi lines command does not have spaces or have multiple spaces, join them as is. For example, a\ b should return the value as "ab", while a \ b should return "a b". --- script.go | 11 +++++------ script_example_test.go | 4 ++-- script_test.go | 26 ++++++++++++++++---------- testdata/script_joinStatements_test.data | 14 ++++++++++++++ 4 files changed, 37 insertions(+), 18 deletions(-) create mode 100644 testdata/script_joinStatements_test.data diff --git a/script.go b/script.go index 0d0b1b1..dd54eac 100644 --- a/script.go +++ b/script.go @@ -197,12 +197,13 @@ func joinStatements(in [][]byte) (out [][]byte) { continue } - stmt = bytes.TrimRight(stmt, "\\ \t") - stmt = append(stmt, ' ') + // Start joining multiline statement. + + stmt = bytes.TrimRight(stmt, "\\") y = x + 1 for ; y < len(in); y++ { - nextStmt = bytes.TrimSpace(in[y]) + nextStmt = in[y] if len(nextStmt) == 0 { in[y] = nil out[y] = nil @@ -213,10 +214,8 @@ func joinStatements(in [][]byte) (out [][]byte) { lastc = nextStmt[endc] if lastc == '\\' { - nextStmt = bytes.TrimRight(nextStmt, "\\ \t") - nextStmt = append(nextStmt, ' ') + nextStmt = bytes.TrimRight(nextStmt, "\\") } - stmt = append(stmt, nextStmt...) in[y] = nil diff --git a/script_example_test.go b/script_example_test.go index d53bf60..dbaa8be 100644 --- a/script_example_test.go +++ b/script_example_test.go @@ -18,8 +18,8 @@ key=value ` scriptContent = ` -multiline\ -command {{.Val "section::key"}};\ +multiline \ +command {{.Val "section::key"}}; \ end; ` diff --git a/script_test.go b/script_test.go index eb6432b..476bdab 100644 --- a/script_test.go +++ b/script_test.go @@ -49,24 +49,30 @@ func TestJoinStatements(t *testing.T) { exp [][]byte } + var ( + tdata *test.Data + err error + ) + + tdata, err = test.LoadData(`testdata/script_joinStatements_test.data`) + if err != nil { + t.Fatal(err) + } + var cases = []testCase{{ - in: bytes.Split([]byte(` -a -b \ -c -d \ -e \ -f -g`), []byte("\n")), + in: bytes.Split(tdata.Input[`case_0`], []byte("\n")), exp: [][]byte{ nil, []byte("a"), []byte("b c"), nil, - []byte("d e f"), + []byte("d e f"), + nil, + nil, + []byte(`gh,i`), nil, + []byte(`j k`), nil, - []byte("g"), }, }} diff --git a/testdata/script_joinStatements_test.data b/testdata/script_joinStatements_test.data new file mode 100644 index 0000000..67a36b9 --- /dev/null +++ b/testdata/script_joinStatements_test.data @@ -0,0 +1,14 @@ +Test data for script joinStatements. + +>>> case_0 + +a +b \ +c +d \ +e \ +f +g\ +h,i +j\ + k -- cgit v1.3