aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorClombrong <cromblong@egregore.fun>2025-07-01 11:30:15 +0200
committerClombrong <cromblong@egregore.fun>2025-07-26 21:55:11 +0200
commit4256e76d7a8cfdb2d6b3afafd281c9fc59e800ec (patch)
tree548aba87dd83ccdaed1811ea5b9a0f91f12234be
parentbc1b86cdccb70bce9fb30e9366bcbd04dd38ddc9 (diff)
feat(jid): add utf8-converting function uchars_of_string
-rw-r--r--lib/jid.ml16
1 files changed, 16 insertions, 0 deletions
diff --git a/lib/jid.ml b/lib/jid.ml
index 827cf9d..2ca0795 100644
--- a/lib/jid.ml
+++ b/lib/jid.ml
@@ -8,6 +8,22 @@ exception InvalidUTF8
type uchars = Uchar.t list
+let uchars_of_string s : uchars =
+ let open Uchar in
+ let len = String.length s in
+ let rec loop acc i =
+ if i >= len then
+ List.rev acc
+ else
+ let c = String.get_utf_8_uchar s i in
+ if not (utf_decode_is_valid c)
+ then raise InvalidUTF8
+ else
+ let k = utf_decode_length c in
+ let u = utf_decode_uchar c in
+ loop (u :: acc) (i + k)
+ in loop [] (String.length s)
+
let of_string (jid : string) : t =
let open Uchar in
let len = String.length jid in