Restrict pretty indent and prefixes to whitespace

This commit is contained in:
tidwall 2020-12-25 06:42:20 -07:00
parent bf4efcb3c1
commit 97ec619cbe
2 changed files with 29 additions and 11 deletions

View File

@ -1950,7 +1950,6 @@ func Get(json, path string) Result {
} }
} }
} }
var i int var i int
var c = &parseContext{json: json} var c = &parseContext{json: json}
if len(path) >= 2 && path[0] == '.' && path[1] == '.' { if len(path) >= 2 && path[0] == '.' && path[1] == '.' {
@ -2624,6 +2623,26 @@ func ModifierExists(name string, fn func(json, arg string) string) bool {
return ok return ok
} }
// cleanWS remove any non-whitespace from string
func cleanWS(s string) string {
for i := 0; i < len(s); i++ {
switch s[i] {
case ' ', '\t', '\n', '\r':
continue
default:
var s2 []byte
for i := 0; i < len(s); i++ {
switch s[i] {
case ' ', '\t', '\n', '\r':
s2 = append(s2, s[i])
}
}
return string(s2)
}
}
return s
}
// @pretty modifier makes the json look nice. // @pretty modifier makes the json look nice.
func modPretty(json, arg string) string { func modPretty(json, arg string) string {
if len(arg) > 0 { if len(arg) > 0 {
@ -2633,9 +2652,9 @@ func modPretty(json, arg string) string {
case "sortKeys": case "sortKeys":
opts.SortKeys = value.Bool() opts.SortKeys = value.Bool()
case "indent": case "indent":
opts.Indent = value.String() opts.Indent = cleanWS(value.String())
case "prefix": case "prefix":
opts.Prefix = value.String() opts.Prefix = cleanWS(value.String())
case "width": case "width":
opts.Width = int(value.Int()) opts.Width = int(value.Int())
} }

View File

@ -2179,8 +2179,8 @@ func TestJoin152(t *testing.T) {
assert(t, res.Raw == `[92.0,92.0,91.0,91.0,67.0]`) assert(t, res.Raw == `[92.0,92.0,91.0,91.0,67.0]`)
} }
func TestIssue192(t *testing.T) { func TestVariousFuzz(t *testing.T) {
assert(t, squash(`"000"hello`) == `"000"`) // Issue #192 assert(t, squash(`"000"hello`) == `"000"`)
assert(t, squash(`"000"`) == `"000"`) assert(t, squash(`"000"`) == `"000"`)
assert(t, squash(`"000`) == `"000`) assert(t, squash(`"000`) == `"000`)
assert(t, squash(`"`) == `"`) assert(t, squash(`"`) == `"`)
@ -2193,15 +2193,14 @@ func TestIssue192(t *testing.T) {
testJSON := `0.#[[{}]].@valid:"000` testJSON := `0.#[[{}]].@valid:"000`
Get(testJSON, testJSON) Get(testJSON, testJSON)
}
func TestIssue195(t *testing.T) { // Issue #195
testJSON := `\************************************` + testJSON = `\************************************` +
`**********{**",**,,**,**,**,**,"",**,**,**,**,**,**,**,**,**,**]` `**********{**",**,,**,**,**,**,"",**,**,**,**,**,**,**,**,**,**]`
Get(testJSON, testJSON) Get(testJSON, testJSON)
}
func TestIssue196(t *testing.T) { // Issue #196
testJSON := `[#.@pretty.@join:{""[]""preserve"3,"][{]]]` testJSON = `[#.@pretty.@join:{""[]""preserve"3,"][{]]]`
Get(testJSON, testJSON) Get(testJSON, testJSON)
} }