aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/course.ml14
1 files changed, 13 insertions, 1 deletions
diff --git a/src/course.ml b/src/course.ml
index 4d02df3..461c99c 100644
--- a/src/course.ml
+++ b/src/course.ml
@@ -19,18 +19,30 @@ open CalendarLib
module J = Json_encoding
+let memoize f =
+ let hashtbl = Hashtbl.create ~random:true 100 in
+ fun v ->
+ match Hashtbl.find_opt hashtbl v with
+ | Some r -> r
+ | None ->
+ let r = f v in
+ Hashtbl.add hashtbl v r;
+ r
+
let get_unicode v =
let b = Buffer.create 1 in
Buffer.add_utf_8_uchar b (Uchar.of_int v);
Buffer.contents b
+let memoized_get_unicode = memoize get_unicode
+
let html_entities_regex = Re.Perl.compile_pat "&#(\\d+);"
let replace_entities str =
Re.Pcre.full_split ~rex:html_entities_regex str
|> List.filter_map (function
| Re.Pcre.Group (_, v) ->
- Some ("&#" ^ v ^ ";", get_unicode @@ int_of_string v)
+ Some ("&#" ^ v ^ ";", memoized_get_unicode @@ int_of_string v)
| _ -> None)
|> Stringext.replace_all_assoc str