Skip to content

Commit 32d7ed7

Browse files
committed
fix: use StringView for lexing results
1 parent 3fc0815 commit 32d7ed7

File tree

2 files changed

+13
-18
lines changed

2 files changed

+13
-18
lines changed

json/lex_main.mbt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,24 +63,24 @@ fn ParseContext::lex_value(
6363
match ctx.read_char() {
6464
Some('0') => {
6565
let (n, repr) = ctx.lex_zero(start=ctx.offset - 2)
66-
return Number(n, repr)
66+
return Number(n, repr.map(_.to_string()))
6767
}
6868
Some(c2) => {
6969
if c2 is ('1'..='9') {
7070
let (n, repr) = ctx.lex_decimal_integer(start=ctx.offset - 2)
71-
return Number(n, repr)
71+
return Number(n, repr.map(_.to_string()))
7272
}
7373
ctx.invalid_char(shift=-1)
7474
}
7575
None => raise InvalidEof
7676
}
7777
Some('0') => {
7878
let (n, repr) = ctx.lex_zero(start=ctx.offset - 1)
79-
return Number(n, repr)
79+
return Number(n, repr.map(_.to_string()))
8080
}
8181
Some('1'..='9') => {
8282
let (n, repr) = ctx.lex_decimal_integer(start=ctx.offset - 1)
83-
return Number(n, repr)
83+
return Number(n, repr.map(_.to_string()))
8484
}
8585
Some('"') => {
8686
let s = ctx.lex_string()

json/lex_number.mbt

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
fn ParseContext::lex_decimal_integer(
1717
ctx : ParseContext,
1818
start~ : Int,
19-
) -> (Double, String?) raise ParseError {
19+
) -> (Double, StringView?) raise ParseError {
2020
for {
2121
match ctx.read_char() {
2222
Some('.') => return ctx.lex_decimal_point(start~)
@@ -37,7 +37,7 @@ fn ParseContext::lex_decimal_integer(
3737
fn ParseContext::lex_decimal_point(
3838
ctx : ParseContext,
3939
start~ : Int,
40-
) -> (Double, String?) raise ParseError {
40+
) -> (Double, StringView?) raise ParseError {
4141
match ctx.read_char() {
4242
Some(c) =>
4343
if c >= '0' && c <= '9' {
@@ -53,7 +53,7 @@ fn ParseContext::lex_decimal_point(
5353
fn ParseContext::lex_decimal_fraction(
5454
ctx : ParseContext,
5555
start~ : Int,
56-
) -> (Double, String?) raise ParseError {
56+
) -> (Double, StringView?) raise ParseError {
5757
for {
5858
match ctx.read_char() {
5959
Some('e' | 'E') => return ctx.lex_decimal_exponent(start~)
@@ -73,7 +73,7 @@ fn ParseContext::lex_decimal_fraction(
7373
fn ParseContext::lex_decimal_exponent(
7474
ctx : ParseContext,
7575
start~ : Int,
76-
) -> (Double, String?) raise ParseError {
76+
) -> (Double, StringView?) raise ParseError {
7777
match ctx.read_char() {
7878
Some('+') | Some('-') => return ctx.lex_decimal_exponent_sign(start~)
7979
Some(c) => {
@@ -91,7 +91,7 @@ fn ParseContext::lex_decimal_exponent(
9191
fn ParseContext::lex_decimal_exponent_sign(
9292
ctx : ParseContext,
9393
start~ : Int,
94-
) -> (Double, String?) raise ParseError {
94+
) -> (Double, StringView?) raise ParseError {
9595
match ctx.read_char() {
9696
Some(c) => {
9797
if c >= '0' && c <= '9' {
@@ -108,7 +108,7 @@ fn ParseContext::lex_decimal_exponent_sign(
108108
fn ParseContext::lex_decimal_exponent_integer(
109109
ctx : ParseContext,
110110
start~ : Int,
111-
) -> (Double, String?) {
111+
) -> (Double, StringView?) {
112112
for {
113113
match ctx.read_char() {
114114
Some(c) => {
@@ -127,7 +127,7 @@ fn ParseContext::lex_decimal_exponent_integer(
127127
fn ParseContext::lex_zero(
128128
ctx : ParseContext,
129129
start~ : Int,
130-
) -> (Double, String?) raise ParseError {
130+
) -> (Double, StringView?) raise ParseError {
131131
match ctx.read_char() {
132132
Some('.') => ctx.lex_decimal_point(start~)
133133
Some('e' | 'E') => ctx.lex_decimal_exponent(start~)
@@ -148,13 +148,8 @@ fn ParseContext::lex_number_end(
148148
ctx : ParseContext,
149149
start : Int,
150150
end : Int,
151-
) -> (Double, String?) {
152-
let s = ctx.input
153-
.data()
154-
.unsafe_substring(
155-
start=ctx.input.start_offset() + start,
156-
end=ctx.input.start_offset() + end,
157-
)
151+
) -> (Double, StringView?) {
152+
let s = ctx.input.view(start_offset=start, end_offset=end)
158153
if !s.contains(".") && !s.contains("e") && !s.contains("E") {
159154
// If the string does not contain a decimal point or exponent, it is likely an integer
160155
// We can try to parse it as an integer first

0 commit comments

Comments
 (0)