diff options
author | Clombrong <cromblong@egregore.fun> | 2025-07-01 11:30:15 +0200 |
---|---|---|
committer | Clombrong <cromblong@egregore.fun> | 2025-07-26 21:55:11 +0200 |
commit | 4256e76d7a8cfdb2d6b3afafd281c9fc59e800ec (patch) | |
tree | 548aba87dd83ccdaed1811ea5b9a0f91f12234be | |
parent | bc1b86cdccb70bce9fb30e9366bcbd04dd38ddc9 (diff) |
feat(jid): add utf8-converting function uchars_of_string
-rw-r--r-- | lib/jid.ml | 16 |
1 files changed, 16 insertions, 0 deletions
@@ -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 |