Skip to content

Commit

Permalink
fix: Replace constant arrays with fixed arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
prantlf committed Apr 17, 2024
1 parent 8bd47f7 commit 61c8554
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion bench/stringify_bench.vsh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/parse.v
Original file line number Diff line number Diff line change
Expand Up @@ -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')
}
Expand All @@ -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) {
Expand Down Expand Up @@ -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')
}
Expand Down
29 changes: 25 additions & 4 deletions src/stringify.v
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) }
Expand Down Expand Up @@ -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])
Expand Down Expand Up @@ -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
// }
// }

0 comments on commit 61c8554

Please # to comment.