Skip to content

Commit 40b56a1

Browse files
committed
fix deletion by key with leading space
1 parent 275f2e8 commit 40b56a1

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

parser.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@ func findTokenStart(data []byte, token byte) int {
4949
}
5050

5151
func findKeyStart(data []byte, key string) (int, error) {
52-
i := 0
52+
i := nextToken(data)
5353
ln := len(data)
54-
if ln > 0 && (data[0] == '{' || data[0] == '[') {
55-
i = 1
54+
if ln > 0 && (data[i] == '{' || data[i] == '[') {
55+
i += 1
5656
}
5757
var stackbuf [unescapeStackBufSize]byte // stack-allocated array for allocation-free unescaping of small strings
5858

parser_test.go

+12-6
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,12 @@ var deleteTests = []DeleteTest{
221221
path: []string{""},
222222
data: `^_�^C^A^@{`,
223223
},
224+
{
225+
desc: "Issue #150: leading space",
226+
json: ` {"test":"input"}`,
227+
path: []string{"test"},
228+
data: ` {}`,
229+
},
224230
}
225231

226232
var setTests = []SetTest{
@@ -989,15 +995,15 @@ var getStringTests = []GetTest{
989995
isErr: true,
990996
},
991997
{
992-
desc: `empty array index`,
993-
json: `[""]`,
994-
path: []string{"[]"},
998+
desc: `empty array index`,
999+
json: `[""]`,
1000+
path: []string{"[]"},
9951001
isFound: false,
9961002
},
9971003
{
998-
desc: `malformed array index`,
999-
json: `[""]`,
1000-
path: []string{"["},
1004+
desc: `malformed array index`,
1005+
json: `[""]`,
1006+
path: []string{"["},
10011007
isFound: false,
10021008
},
10031009
}

0 commit comments

Comments
 (0)