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

time: fix and cleanup int validity checks (#18885)

This commit is contained in:
Turiiya 2023-07-17 21:24:52 +02:00 committed by GitHub
parent 8c8d21d130
commit 2eea59c4cc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 41 additions and 22 deletions

View File

@ -31,7 +31,10 @@ fn (mut p DateTimeParser) peek(length int) !string {
}
fn (mut p DateTimeParser) must_be_int(length int) !int {
val := p.next(length) or { return err }
val := p.next(length)!
if !val.contains_only('0123456789') {
return error('expected int, found: ${val}')
}
return val.int()
}
@ -39,15 +42,12 @@ fn (mut p DateTimeParser) must_be_int_with_minimum_length(min int, max int, allo
mut length := max + 1 - min
mut val := ''
for _ in 0 .. length {
maybe_int := p.peek(1) or { break }
if maybe_int == '0' || maybe_int == '1' || maybe_int == '2' || maybe_int == '4'
|| maybe_int == '5' || maybe_int == '6' || maybe_int == '7' || maybe_int == '8'
|| maybe_int == '9' {
p.next(1)!
val += maybe_int
} else {
tok := p.peek(1) or { break }
if !tok.contains_only('0123456789') {
break
}
p.next(1)!
val += tok
}
if val.len < min {
return error('expected int with a minimum length of ${min}, found: ${val.len}')
@ -59,22 +59,26 @@ fn (mut p DateTimeParser) must_be_int_with_minimum_length(min int, max int, allo
}
fn (mut p DateTimeParser) must_be_single_int_with_optional_leading_zero() !int {
mut val := p.next(1) or { return err }
mut val := p.next(1)!
if val == '0' {
val += p.next(1) or { '' }
next := p.next(1) or { '' }
if !next.contains_only('0123456789') {
return error('expected int, found: ${next}')
}
val += next
}
return val.int()
}
fn (mut p DateTimeParser) must_be_string(must string) ! {
val := p.next(must.len) or { return err }
val := p.next(must.len)!
if val != must {
return error('invalid string: "${val}"!="${must}" at: ${p.current_pos_datetime}')
}
}
fn (mut p DateTimeParser) must_be_string_one_of(oneof []string) !string {
for _, must in oneof {
for must in oneof {
val := p.peek(must.len) or { continue }
if val == must {
return must
@ -84,7 +88,7 @@ fn (mut p DateTimeParser) must_be_string_one_of(oneof []string) !string {
}
fn (mut p DateTimeParser) must_be_valid_month() !int {
for _, v in long_months {
for v in long_months {
if p.current_pos_datetime + v.len < p.datetime.len {
month_name := p.datetime[p.current_pos_datetime..p.current_pos_datetime + v.len]
if v == month_name {
@ -97,8 +101,8 @@ fn (mut p DateTimeParser) must_be_valid_month() !int {
}
fn (mut p DateTimeParser) must_be_valid_week_day(letters int) !string {
val := p.next(letters) or { return err }
for _, v in long_days {
val := p.next(letters)!
for v in long_days {
if v[0..letters] == val {
return v
}
@ -151,7 +155,7 @@ fn (mut p DateTimeParser) parse() !Time {
tokens := extract_tokens(p.format) or {
return error_invalid_time(0, 'malformed format string: ${err}')
}
for _, token in tokens {
for token in tokens {
match token {
'YYYY' {
year_ = p.must_be_int(4) or {

View File

@ -3,6 +3,8 @@
// that can be found in the LICENSE file.
module time
import strconv
// parse_rfc3339 returns time from a date string in RFC 3339 datetime format.
// See also https://ijmacd.github.io/rfc3339-iso8601/ for a visual reference of
// the differences between ISO-8601 and RFC 3339.
@ -79,12 +81,25 @@ pub fn parse(s string) !Time {
minute_ := hms[1]
second_ := hms[2]
//
iyear := ymd[0].int()
imonth := ymd[1].int()
iday := ymd[2].int()
ihour := hour_.int()
iminute := minute_.int()
isecond := second_.int()
iyear := strconv.atoi(ymd[0]) or {
return error_invalid_time(0, 'invalid year format: ${ymd[0]}')
}
imonth := strconv.atoi(ymd[1]) or {
return error_invalid_time(0, 'invalid month format: ${ymd[1]}')
}
iday := strconv.atoi(ymd[2]) or {
return error_invalid_time(0, 'invalid day format: ${ymd[2]}')
}
ihour := strconv.atoi(hour_) or {
return error_invalid_time(0, 'invalid hour format: ${hour_}')
}
iminute := strconv.atoi(minute_) or {
return error_invalid_time(0, 'invalid minute format: ${minute_}')
}
isecond := strconv.atoi(second_) or {
return error_invalid_time(0, 'invalid second format: ${second_}')
}
// eprintln('>> iyear: $iyear | imonth: $imonth | iday: $iday | ihour: $ihour | iminute: $iminute | isecond: $isecond')
if iyear > 9999 || iyear < -9999 {
return error_invalid_time(3, 'year must be between -10000 and 10000')