From 61c8554e80f921a4afe8974d211ee70f001281f1 Mon Sep 17 00:00:00 2001 From: Ferdinand Prantl Date: Wed, 17 Apr 2024 22:04:38 +0200 Subject: [PATCH] fix: Replace constant arrays with fixed arrays --- Makefile | 2 +- bench/stringify_bench.vsh | 2 +- src/parse.v | 6 +++--- src/stringify.v | 29 +++++++++++++++++++++++++---- 4 files changed, 30 insertions(+), 9 deletions(-) diff --git a/Makefile b/Makefile index 7ece7a0..3644e13 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,7 @@ check: v vet . test: - NO_COLOR=1 v -use-os-system-to-run test . + NO_COLOR=1 v test . clean: rm -rf src/*_test src/*.dSYM diff --git a/bench/stringify_bench.vsh b/bench/stringify_bench.vsh index 2745497..98449c4 100755 --- a/bench/stringify_bench.vsh +++ b/bench/stringify_bench.vsh @@ -10,7 +10,7 @@ const repeats = 10 har := os.read_file('vlang.io.har.condensed.json')! any2 := json2.raw_decode(har)! -any3 := json3.parse(har, &json3.ParseOpts{})! +any3 := json3.parse(har)! pretty := json3.StringifyOpts{ pretty: true diff --git a/src/parse.v b/src/parse.v index 73ff2c5..ea81978 100644 --- a/src/parse.v +++ b/src/parse.v @@ -375,7 +375,7 @@ fn (mut p Parser) parse_string(from int, quote u8) !(string, int) { } else { if c < 32 { - idx := escapable.index(c) + idx := index_u8(escapable, c) if idx >= 0 { return p.fail(i, 'Unescaped whitespace "\\${rune(escaped[idx])}" encountered when parsing a string') } @@ -400,7 +400,7 @@ fn (mut p Parser) parse_escape_sequence(mut builder Builder, from int) !int { return p.fail(i, 'Unfinished escape sequence encountered when parsing a string') } mut c := p.str[i] - idx := escaped.index(c) + idx := index_u8(escaped, c) if idx >= 0 { builder.write_u8(escapable[idx]) } else if c == `\\` || c == `/` || c == `"` || (c == `'` && p.opts.allow_single_quotes) { @@ -452,7 +452,7 @@ fn (mut p Parser) detect_escape(from int, quote u8) !(int, bool) { } else { if c < 32 { - idx := escapable.index(c) + idx := index_u8(escapable, c) if idx >= 0 { return p.fail(i, 'Unescaped whitespace "\\${rune(escaped[idx])}" encountered when parsing a string') } diff --git a/src/stringify.v b/src/stringify.v index 6dc92ce..710915e 100644 --- a/src/stringify.v +++ b/src/stringify.v @@ -68,9 +68,9 @@ fn write_raw(mut builder Builder, s string) { unsafe { builder.write_ptr(s.str, s.len) } } -const escapable = [u8(`\b`), u8(`\f`), u8(`\n`), u8(`\r`), u8(`\t`)] +const escapable = [u8(`\b`), u8(`\f`), u8(`\n`), u8(`\r`), u8(`\t`)]! -const escaped = [u8(`b`), u8(`f`), u8(`n`), u8(`r`), u8(`t`)] +const escaped = [u8(`b`), u8(`f`), u8(`n`), u8(`r`), u8(`t`)]! /* fn write_string(mut builder Builder, s string, opts &StringifyOpts) { @@ -83,7 +83,7 @@ fn write_string(mut builder Builder, s string, opts &StringifyOpts) { ch := s[cur] rune_len := utf8_char_len(ch) if rune_len == 1 { - idx := escapable.index(ch) + idx := index_u8(escapable, ch) if idx >= 0 { if prev < cur { unsafe { builder.write_ptr(s.str + prev, cur - prev) } @@ -122,7 +122,7 @@ fn write_string(mut builder Builder, s string, opts &StringifyOpts, include_quot builder.write_u8(`\\`) builder.write_u8(ch) } else { - idx := json.escapable.index(ch) + idx := index_u8(json.escapable, ch) if idx >= 0 { builder.write_u8(`\\`) builder.write_u8(json.escaped[idx]) @@ -274,3 +274,24 @@ fn u16_to_hex(nn u16, mut buf []u8) { n >>= 4 } } + +@[direct_array_access] +fn index_u8(arr [5]u8, val u8) int { + for i in 0 .. arr.len { + if arr[i] == val { + return i + } + } + return -1 +} + +// fn C.memchr(ptr charptr, value u8, num usize) charptr + +// fn index_u8(ptr charptr, value u8, num int) int { +// pos := C.memchr(ptr, value, usize(num)) +// return if unsafe { pos != nil } { +// unsafe { pos - ptr } +// } else { +// -1 +// } +// }