Skip to content

Commit

Permalink
refactor: use break in for loop to avoid issues and improve perf
Browse files Browse the repository at this point in the history
  • Loading branch information
qazxcdswe123 authored and bobzhang committed Jun 21, 2024
1 parent 26aaef9 commit 1046766
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 31 deletions.
32 changes: 16 additions & 16 deletions json/lex_main.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -30,67 +30,67 @@ fn lex_value(
for ; ; {
match read_char(ctx) {
Some('\t' | ' ' | '\n' | '\r') => continue
Some('{') => return Ok(LBrace)
Some('[') => return Ok(LBracket)
Some('{') => break Ok(LBrace)
Some('[') => break Ok(LBracket)
Some(']') =>
if allow_rbracket {
return Ok(RBracket)
break Ok(RBracket)
} else {
return Err(invalid_char(ctx, shift=-1))
break Err(invalid_char(ctx, shift=-1))
}
Some('n') => {
lex_assert_char(ctx, 'u')?
lex_assert_char(ctx, 'l')?
lex_assert_char(ctx, 'l')?
return Ok(Null)
break Ok(Null)
}
Some('t') => {
lex_assert_char(ctx, 'r')?
lex_assert_char(ctx, 'u')?
lex_assert_char(ctx, 'e')?
return Ok(True)
break Ok(True)
}
Some('f') => {
lex_assert_char(ctx, 'a')?
lex_assert_char(ctx, 'l')?
lex_assert_char(ctx, 's')?
lex_assert_char(ctx, 'e')?
return Ok(False)
break Ok(False)
}
Some('-') =>
match read_char(ctx) {
Some('0') => {
let n = lex_zero(ctx, start=ctx.offset - 2)!!
return Ok(Number(n))
break Ok(Number(n))
}
Some(c2) => {
if c2 >= '1' && c2 <= '9' {
let n = lex_decimal_integer(ctx, start=ctx.offset - 2)!!
return Ok(Number(n))
break Ok(Number(n))
}
return Err(invalid_char(ctx, shift=-1))
break Err(invalid_char(ctx, shift=-1))
}
None => return Err(InvalidEof)
None => break Err(InvalidEof)
}
Some('0') => {
let n = lex_zero(ctx, start=ctx.offset - 1)!!
return Ok(Number(n))
break Ok(Number(n))
}
Some('1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9') => {
let n = lex_decimal_integer(ctx, start=ctx.offset - 1)!!
return Ok(Number(n))
break Ok(Number(n))
}
Some('"') => {
let s = lex_string(ctx)?
return Ok(String(s))
break Ok(String(s))
}
Some(c) => {
if c > '\x7f' && non_ascii_whitespace.contains(c) {
continue
}
return Err(invalid_char(ctx, shift=-1))
break Err(invalid_char(ctx, shift=-1))
}
None => return Err(InvalidEof)
None => break Err(InvalidEof)
}
}
}
30 changes: 15 additions & 15 deletions json/lex_number.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@
fn lex_decimal_integer(ctx : ParseContext, ~start : Int) -> Double!ParseError {
for ; ; {
match read_char(ctx) {
Some('.') => return lex_decimal_point(ctx, ~start)!
Some('e' | 'E') => return lex_decimal_exponent(ctx, ~start)!
Some('.') => break lex_decimal_point(ctx, ~start)!
Some('e' | 'E') => break lex_decimal_exponent(ctx, ~start)!
Some(c) => {
if c >= '0' && c <= '9' {
continue
}
ctx.offset -= 1
return lex_number_end(ctx, start, ctx.offset)!
break lex_number_end(ctx, start, ctx.offset)!
}
None => return lex_number_end(ctx, start, ctx.offset)!
None => break lex_number_end(ctx, start, ctx.offset)!
}
}
}
Expand All @@ -33,7 +33,7 @@ fn lex_decimal_point(ctx : ParseContext, ~start : Int) -> Double!ParseError {
match read_char(ctx) {
Some(c) =>
if c >= '0' && c <= '9' {
return lex_decimal_fraction(ctx, ~start)!
lex_decimal_fraction(ctx, ~start)!
} else {
raise invalid_char(ctx, shift=-1)
}
Expand All @@ -46,22 +46,22 @@ fn lex_decimal_point(ctx : ParseContext, ~start : Int) -> Double!ParseError {
fn lex_decimal_fraction(ctx : ParseContext, ~start : Int) -> Double!ParseError {
for ; ; {
match read_char(ctx) {
Some('e' | 'E') => return lex_decimal_exponent(ctx, ~start)!
Some('e' | 'E') => break lex_decimal_exponent(ctx, ~start)!
Some(c) => {
if c >= '0' && c <= '9' {
continue
}
ctx.offset -= 1
return lex_number_end(ctx, start, ctx.offset)!
break lex_number_end(ctx, start, ctx.offset)!
}
None => return lex_number_end(ctx, start, ctx.offset)!
None => break lex_number_end(ctx, start, ctx.offset)!
}
}
}

fn lex_decimal_exponent(ctx : ParseContext, ~start : Int) -> Double!ParseError {
match read_char(ctx) {
Some('+') | Some('-') => return lex_decimal_exponent_sign(ctx, ~start)!
Some('+') | Some('-') => lex_decimal_exponent_sign(ctx, ~start)!
Some(c) => {
if c >= '0' && c <= '9' {
return lex_decimal_exponent_integer(ctx, ~start)!
Expand Down Expand Up @@ -104,26 +104,26 @@ fn lex_decimal_exponent_integer(
continue
}
ctx.offset -= 1
return lex_number_end(ctx, start, ctx.offset)!
break lex_number_end(ctx, start, ctx.offset)!
}
None => return lex_number_end(ctx, start, ctx.offset)!
None => break lex_number_end(ctx, start, ctx.offset)!
}
}
}

fn lex_zero(ctx : ParseContext, ~start : Int) -> Double!ParseError {
match read_char(ctx) {
Some('.') => return lex_decimal_point(ctx, ~start)!
Some('e' | 'E') => return lex_decimal_exponent(ctx, ~start)!
Some('.') => lex_decimal_point(ctx, ~start)!
Some('e' | 'E') => lex_decimal_exponent(ctx, ~start)!
Some(c) => {
if c >= '0' && c <= '9' {
ctx.offset -= 1
raise invalid_char(ctx)
}
ctx.offset -= 1
return lex_number_end(ctx, start, ctx.offset)!
lex_number_end(ctx, start, ctx.offset)!
}
None => return lex_number_end(ctx, start, ctx.offset)!
None => lex_number_end(ctx, start, ctx.offset)!
}
}

Expand Down

0 comments on commit 1046766

Please sign in to comment.