summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2023-11-10 15:07:52 +0700
committerShulhan <ms@kilabit.info>2023-11-11 18:37:15 +0700
commitd8b7a4e509a641ea2949646c5a3577bc848fea30 (patch)
tree09f9bb2d3f3afe83a17fc2c1e85cba606da276b7
parentcf6d0e5bb0baf5acbc70f8084d087e3a41b5452e (diff)
downloadawwan-d8b7a4e509a641ea2949646c5a3577bc848fea30.tar.xz
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".
-rw-r--r--script.go11
-rw-r--r--script_example_test.go4
-rw-r--r--script_test.go26
-rw-r--r--testdata/script_joinStatements_test.data14
4 files changed, 37 insertions, 18 deletions
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