Skip to content

Commit

Permalink
add API ValidSyntax
Browse files Browse the repository at this point in the history
  • Loading branch information
AsterDY committed Jan 22, 2024
1 parent 583741e commit 4a981f2
Show file tree
Hide file tree
Showing 15 changed files with 66 additions and 45 deletions.
22 changes: 22 additions & 0 deletions ast/api_amd64.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,3 +171,25 @@ func DecodeString(src string, pos int) (ret int, v string) {
return -int(_ERR_UNSUPPORT_TYPE), ""
}
}

// ValidSyntax check if a json has a valid JSON syntax,
// while not validate UTF-8 charset
func ValidSyntax(json string) bool {
fsm := types.NewStateMachine()
p := 0
ret := native.ValidateOne(&json, &p, fsm, 0)
types.FreeStateMachine(fsm)

if ret < 0 {
return false
}

/* check for trailing spaces */
for ;p < len(json); p++ {
if !isSpace(json[p]) {
return false
}
}

return true
}
2 changes: 2 additions & 0 deletions ast/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -781,6 +781,8 @@ func (self Parser) ExportError(err types.ParsingError) error {
}.Description())
}

// SkipFast skip a json value in fast-skip algs,
// while not strictly validate JSON syntax and UTF-8 charset.
func SkipFast(src string, i int) (int, int, error) {
p := NewParserObj(src)
p.p = i
Expand Down
6 changes: 3 additions & 3 deletions internal/native/avx/native_amd64.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions internal/native/avx/native_subr_amd64.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 6 additions & 7 deletions internal/native/avx/native_text_amd64.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions internal/native/avx2/native_amd64.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions internal/native/avx2/native_subr_amd64.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 6 additions & 7 deletions internal/native/avx2/native_text_amd64.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions internal/native/dispatch_amd64.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ var (

__GetByPath func(s unsafe.Pointer, p unsafe.Pointer, path unsafe.Pointer, m unsafe.Pointer) int

__ValidateOne func(s unsafe.Pointer, p unsafe.Pointer, m unsafe.Pointer) int
__ValidateOne func(s unsafe.Pointer, p unsafe.Pointer, m unsafe.Pointer, flags uint64) int

__I64toa func(out unsafe.Pointer, val int64) (ret int)

Expand Down Expand Up @@ -134,8 +134,8 @@ func GetByPath(s *string, p *int, path *[]interface{}, m *types.StateMachine) in
}

//go:nosplit
func ValidateOne(s *string, p *int, m *types.StateMachine) int {
return __ValidateOne(rt.NoEscape(unsafe.Pointer(s)), rt.NoEscape(unsafe.Pointer(p)), rt.NoEscape(unsafe.Pointer(m)))
func ValidateOne(s *string, p *int, m *types.StateMachine, flags uint64) int {
return __ValidateOne(rt.NoEscape(unsafe.Pointer(s)), rt.NoEscape(unsafe.Pointer(p)), rt.NoEscape(unsafe.Pointer(m)), flags)
}

//go:nosplit
Expand Down
6 changes: 3 additions & 3 deletions internal/native/native_amd64.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ var (

__skip_number func(s unsafe.Pointer, p unsafe.Pointer) (ret int)

__validate_one func(s unsafe.Pointer, p unsafe.Pointer, m unsafe.Pointer) (ret int)
__validate_one func(s unsafe.Pointer, p unsafe.Pointer, m unsafe.Pointer, flags uint64) (ret int)

__get_by_path func(s unsafe.Pointer, p unsafe.Pointer, path unsafe.Pointer, m unsafe.Pointer) (ret int)

Expand Down Expand Up @@ -164,8 +164,8 @@ func skip_number(s *string, p *int) (ret int) {
}

//go:nosplit
func validate_one(s *string, p *int, m *types.StateMachine) (ret int) {
return __validate_one(rt.NoEscape(unsafe.Pointer(s)), rt.NoEscape(unsafe.Pointer(p)), rt.NoEscape(unsafe.Pointer(m)))
func validate_one(s *string, p *int, m *types.StateMachine, flags uint64) (ret int) {
return __validate_one(rt.NoEscape(unsafe.Pointer(s)), rt.NoEscape(unsafe.Pointer(p)), rt.NoEscape(unsafe.Pointer(m)), flags)
}

//go:nosplit
Expand Down
6 changes: 3 additions & 3 deletions internal/native/sse/native_amd64.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions internal/native/sse/native_subr_amd64.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 6 additions & 7 deletions internal/native/sse/native_text_amd64.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion native/native.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ bool atof_eisel_lemire64(uint64_t mant, int exp10, int sgn, double *val);
double atof_native(const char *sp, ssize_t nb, char *dbuf, ssize_t cap);

long validate_string(const GoString *src, long *p);
long validate_one(const GoString *src, long *p, StateMachine *m);
long validate_one(const GoString *src, long *p, StateMachine *m, uint64_t flags);
long validate_utf8(const GoString *src, long *p, StateMachine *m);
long validate_utf8_fast(const GoString *src);

Expand Down
4 changes: 2 additions & 2 deletions native/scanning.c
Original file line number Diff line number Diff line change
Expand Up @@ -1445,9 +1445,9 @@ long skip_one(const GoString *src, long *p, StateMachine *m, uint64_t flags) {
return fsm_exec(m, src, p, flags);
}

long validate_one(const GoString *src, long *p, StateMachine *m) {
long validate_one(const GoString *src, long *p, StateMachine *m, uint64_t flags) {
fsm_init(m, FSM_VAL);
return fsm_exec(m, src, p, MASK_VALIDATE_STRING);
return fsm_exec(m, src, p, flags);
}

/* Faster skip api for sonic.ast */
Expand Down

0 comments on commit 4a981f2

Please sign in to comment.