@@ -31,23 +31,23 @@ fn (mut p DateTimeParser) peek(length int) !string {
31
31
}
32
32
33
33
fn (mut p DateTimeParser) must_be_int (length int ) ! int {
34
- val := p.next (length) or { return err }
34
+ val := p.next (length)!
35
+ if ! val.contains_only ('0123456789' ) {
36
+ return error ('expected int, found: ${val} ' )
37
+ }
35
38
return val.int ()
36
39
}
37
40
38
41
fn (mut p DateTimeParser) must_be_int_with_minimum_length (min int , max int , allow_leading_zero bool ) ! int {
39
42
mut length := max + 1 - min
40
43
mut val := ''
41
44
for _ in 0 .. length {
42
- maybe_int := p.peek (1 ) or { break }
43
- if maybe_int == '0' || maybe_int == '1' || maybe_int == '2' || maybe_int == '4'
44
- || maybe_int == '5' || maybe_int == '6' || maybe_int == '7' || maybe_int == '8'
45
- || maybe_int == '9' {
46
- p.next (1 )!
47
- val + = maybe_int
48
- } else {
45
+ tok := p.peek (1 ) or { break }
46
+ if ! tok.contains_only ('0123456789' ) {
49
47
break
50
48
}
49
+ p.next (1 )!
50
+ val + = tok
51
51
}
52
52
if val.len < min {
53
53
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
59
59
}
60
60
61
61
fn (mut p DateTimeParser) must_be_single_int_with_optional_leading_zero () ! int {
62
- mut val := p.next (1 ) or { return err }
62
+ mut val := p.next (1 )!
63
63
if val == '0' {
64
- val + = p.next (1 ) or { '' }
64
+ next := p.next (1 ) or { '' }
65
+ if ! next.contains_only ('0123456789' ) {
66
+ return error ('expected int, found: ${next} ' )
67
+ }
68
+ val + = next
65
69
}
66
70
return val.int ()
67
71
}
68
72
69
73
fn (mut p DateTimeParser) must_be_string (must string ) ! {
70
- val := p.next (must.len) or { return err }
74
+ val := p.next (must.len)!
71
75
if val != must {
72
76
return error ('invalid string: "${val} "!="${must} " at: ${p.current_pos_datetime} ' )
73
77
}
74
78
}
75
79
76
80
fn (mut p DateTimeParser) must_be_string_one_of (oneof []string ) ! string {
77
- for _, must in oneof {
81
+ for must in oneof {
78
82
val := p.peek (must.len) or { continue }
79
83
if val == must {
80
84
return must
@@ -84,7 +88,7 @@ fn (mut p DateTimeParser) must_be_string_one_of(oneof []string) !string {
84
88
}
85
89
86
90
fn (mut p DateTimeParser) must_be_valid_month () ! int {
87
- for _, v in long_months {
91
+ for v in long_months {
88
92
if p.current_pos_datetime + v.len < p.datetime.len {
89
93
month_name := p.datetime[p.current_pos_datetime..p.current_pos_datetime + v.len]
90
94
if v == month_name {
@@ -97,8 +101,8 @@ fn (mut p DateTimeParser) must_be_valid_month() !int {
97
101
}
98
102
99
103
fn (mut p DateTimeParser) must_be_valid_week_day (letters int ) ! string {
100
- val := p.next (letters) or { return err }
101
- for _, v in long_days {
104
+ val := p.next (letters)!
105
+ for v in long_days {
102
106
if v[0 ..letters] == val {
103
107
return v
104
108
}
@@ -151,7 +155,7 @@ fn (mut p DateTimeParser) parse() !Time {
151
155
tokens := extract_tokens (p.format) or {
152
156
return error_invalid_time (0 , 'malformed format string: ${err} ' )
153
157
}
154
- for _, token in tokens {
158
+ for token in tokens {
155
159
match token {
156
160
'YYYY' {
157
161
year_ = p.must_be_int (4 ) or {
0 commit comments