Skip to content

Commit c409b7d

Browse files
authored
Merge pull request #2 from AltGr/nostr
Avoid using Str
2 parents 0dcb42e + d847e58 commit c409b7d

File tree

2 files changed

+9
-16
lines changed

2 files changed

+9
-16
lines changed

lib/dates.ml

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,11 @@ let format_period (fmt : Format.formatter) (p : period) : unit =
4141
Format.fprintf fmt "[%d years, %d months, %d days]" p.years p.months p.days
4242

4343
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"
5049

5150
let add_periods (d1 : period) (d2 : period) : period =
5251
{
@@ -234,15 +233,10 @@ let format_date (fmt : Format.formatter) (d : date) : unit =
234233
Format.fprintf fmt "%04d-%02d-%02d" d.year d.month d.day
235234

236235
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"
246240

247241
let first_day_of_month (d : date) : date =
248242
assert(is_valid_date d);

lib/dune

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
(library
2-
(libraries str)
32
(public_name dates_calc)
43
)

0 commit comments

Comments
 (0)