1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

scanner: make 0o prefix the only way to define octals

This commit is contained in:
SleepyRoy
2020-02-24 06:43:04 +08:00
committed by GitHub
parent 9d2a60bb11
commit 7d2eb4f604
7 changed files with 50 additions and 10 deletions

View File

@ -346,6 +346,19 @@ fn (s mut Scanner) scan() ScanRes {
}
// `123`, `.123`
else if c.is_digit() || (c == `.` && nextc.is_digit()) {
if !s.inside_string {
// In C ints with `0` prefix are octal (in V they're decimal), so discarding heading zeros is needed.
mut start_pos := s.pos
for start_pos < s.text.len && s.text[start_pos] == `0` {
start_pos++
}
mut prefix_zero_num := start_pos - s.pos // how many prefix zeros should be jumped
// for 0b, 0o, 0x the heading zero shouldn't be jumped
if c == `0` && start_pos < s.text.len && !s.text[start_pos].is_digit() {
prefix_zero_num--
}
s.pos += prefix_zero_num // jump these zeros
}
num := s.ident_number()
return scan_res(.number, num)
}