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:
@ -31,7 +31,10 @@ fn (mut p DateTimeParser) peek(length int) !string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn (mut p DateTimeParser) must_be_int(length int) !int {
|
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()
|
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 length := max + 1 - min
|
||||||
mut val := ''
|
mut val := ''
|
||||||
for _ in 0 .. length {
|
for _ in 0 .. length {
|
||||||
maybe_int := p.peek(1) or { break }
|
tok := p.peek(1) or { break }
|
||||||
if maybe_int == '0' || maybe_int == '1' || maybe_int == '2' || maybe_int == '4'
|
if !tok.contains_only('0123456789') {
|
||||||
|| maybe_int == '5' || maybe_int == '6' || maybe_int == '7' || maybe_int == '8'
|
|
||||||
|| maybe_int == '9' {
|
|
||||||
p.next(1)!
|
|
||||||
val += maybe_int
|
|
||||||
} else {
|
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
p.next(1)!
|
||||||
|
val += tok
|
||||||
}
|
}
|
||||||
if val.len < min {
|
if val.len < min {
|
||||||
return error('expected int with a minimum length of ${min}, found: ${val.len}')
|
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 {
|
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' {
|
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()
|
return val.int()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (mut p DateTimeParser) must_be_string(must string) ! {
|
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 {
|
if val != must {
|
||||||
return error('invalid string: "${val}"!="${must}" at: ${p.current_pos_datetime}')
|
return error('invalid string: "${val}"!="${must}" at: ${p.current_pos_datetime}')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (mut p DateTimeParser) must_be_string_one_of(oneof []string) !string {
|
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 }
|
val := p.peek(must.len) or { continue }
|
||||||
if val == must {
|
if val == must {
|
||||||
return 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 {
|
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 {
|
if p.current_pos_datetime + v.len < p.datetime.len {
|
||||||
month_name := p.datetime[p.current_pos_datetime..p.current_pos_datetime + v.len]
|
month_name := p.datetime[p.current_pos_datetime..p.current_pos_datetime + v.len]
|
||||||
if v == month_name {
|
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 {
|
fn (mut p DateTimeParser) must_be_valid_week_day(letters int) !string {
|
||||||
val := p.next(letters) or { return err }
|
val := p.next(letters)!
|
||||||
for _, v in long_days {
|
for v in long_days {
|
||||||
if v[0..letters] == val {
|
if v[0..letters] == val {
|
||||||
return v
|
return v
|
||||||
}
|
}
|
||||||
@ -151,7 +155,7 @@ fn (mut p DateTimeParser) parse() !Time {
|
|||||||
tokens := extract_tokens(p.format) or {
|
tokens := extract_tokens(p.format) or {
|
||||||
return error_invalid_time(0, 'malformed format string: ${err}')
|
return error_invalid_time(0, 'malformed format string: ${err}')
|
||||||
}
|
}
|
||||||
for _, token in tokens {
|
for token in tokens {
|
||||||
match token {
|
match token {
|
||||||
'YYYY' {
|
'YYYY' {
|
||||||
year_ = p.must_be_int(4) or {
|
year_ = p.must_be_int(4) or {
|
||||||
|
@ -3,6 +3,8 @@
|
|||||||
// that can be found in the LICENSE file.
|
// that can be found in the LICENSE file.
|
||||||
module time
|
module time
|
||||||
|
|
||||||
|
import strconv
|
||||||
|
|
||||||
// parse_rfc3339 returns time from a date string in RFC 3339 datetime format.
|
// 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
|
// See also https://ijmacd.github.io/rfc3339-iso8601/ for a visual reference of
|
||||||
// the differences between ISO-8601 and RFC 3339.
|
// the differences between ISO-8601 and RFC 3339.
|
||||||
@ -79,12 +81,25 @@ pub fn parse(s string) !Time {
|
|||||||
minute_ := hms[1]
|
minute_ := hms[1]
|
||||||
second_ := hms[2]
|
second_ := hms[2]
|
||||||
//
|
//
|
||||||
iyear := ymd[0].int()
|
iyear := strconv.atoi(ymd[0]) or {
|
||||||
imonth := ymd[1].int()
|
return error_invalid_time(0, 'invalid year format: ${ymd[0]}')
|
||||||
iday := ymd[2].int()
|
}
|
||||||
ihour := hour_.int()
|
imonth := strconv.atoi(ymd[1]) or {
|
||||||
iminute := minute_.int()
|
return error_invalid_time(0, 'invalid month format: ${ymd[1]}')
|
||||||
isecond := second_.int()
|
}
|
||||||
|
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')
|
// eprintln('>> iyear: $iyear | imonth: $imonth | iday: $iday | ihour: $ihour | iminute: $iminute | isecond: $isecond')
|
||||||
if iyear > 9999 || iyear < -9999 {
|
if iyear > 9999 || iyear < -9999 {
|
||||||
return error_invalid_time(3, 'year must be between -10000 and 10000')
|
return error_invalid_time(3, 'year must be between -10000 and 10000')
|
||||||
|
Reference in New Issue
Block a user