@@ -41,12 +41,11 @@ let format_period (fmt : Format.formatter) (p : period) : unit =
41
41
Format. fprintf fmt " [%d years, %d months, %d days]" p.years p.months p.days
42
42
43
43
let period_of_string str =
44
- let re = Str. regexp {| \[\([0-9 ]+ \) years, \([0-9 ]+ \) months, \([0-9 ]+ \) days\]| } in
45
- assert (Str. string_match re str 0 );
46
- let years = int_of_string @@ Str. matched_group 1 str in
47
- let months = int_of_string @@ Str. matched_group 2 str in
48
- let days = int_of_string @@ Str. matched_group 3 str in
49
- make_period ~years ~months ~days
44
+ try
45
+ Scanf. sscanf str
46
+ " [%d years, %d months, %d days]"
47
+ (fun years months days -> make_period ~years ~months ~days )
48
+ with Scanf. Scan_failure _ -> invalid_arg " period_of_string"
50
49
51
50
let add_periods (d1 : period ) (d2 : period ) : period =
52
51
{
@@ -234,15 +233,10 @@ let format_date (fmt : Format.formatter) (d : date) : unit =
234
233
Format. fprintf fmt " %04d-%02d-%02d" d.year d.month d.day
235
234
236
235
let date_of_string str =
237
- let re =
238
- Str. regexp {| \([0-9 ][0-9 ][0-9 ][0-9 ]\)- \([0-9 ][0-9 ]\)- \([0-9 ][0-9 ]\)| }
239
- in
240
- assert (Str. string_match re str 0 );
241
- let year = int_of_string @@ Str. matched_group 1 str in
242
- let month = int_of_string @@ Str. matched_group 2 str in
243
- let day = int_of_string @@ Str. matched_group 3 str in
244
- make_date ~year ~month ~day
245
-
236
+ try
237
+ Scanf. sscanf str " %04d-%02d-%02d"
238
+ (fun year month day -> make_date ~year ~month ~day )
239
+ with Scanf. Scan_failure _ -> invalid_arg " date_of_string"
246
240
247
241
let first_day_of_month (d : date ) : date =
248
242
assert (is_valid_date d);
0 commit comments