diff options
author | Clombrong <cromblong@egregore.fun> | 2025-07-01 22:05:53 +0200 |
---|---|---|
committer | Clombrong <cromblong@egregore.fun> | 2025-07-26 21:55:50 +0200 |
commit | 960536e7892fcffc01586980aff3999d41e789a2 (patch) | |
tree | 2a5634b87becf4cc876bcfa195cf5fca1fd8d421 | |
parent | b772cf7a9b5abc28859022d3ad30527fe8e19cbb (diff) |
fix(jid): rework of_string by incremental splitting
instead of returning indexes that were hard to calculate
-rw-r--r-- | lib/jid.ml | 19 |
1 files changed, 11 insertions, 8 deletions
@@ -32,23 +32,26 @@ let string_of_uchars (u : uchars) = let of_string (s : string) : t = let open List in let jid = uchars_of_string s in - let dend, resourcepart = + let rest, resourcepart = find_mapi (fun i c -> if c = Uchar.of_char '/' - then Some (i, drop i jid |> string_of_uchars) + then Some (take i jid, drop (i+1) jid |> string_of_uchars) else None) jid - |> Option.fold ~none:(length jid, None) ~some:(fun (i, s) -> i, Some s) - and dlen, localpart = - rev jid + |> Option.fold ~none:(jid, None) ~some:(fun (rest, res) -> rest, Some res) + in + let localpart, domainpart = + rev rest |> find_mapi (fun i c -> if c = Uchar.of_char '@' - then Some (i, take (length jid - i - 1) jid |> string_of_uchars) + then let idx = length rest - i + in Some (take (idx-1) rest |> string_of_uchars, + drop idx rest |> string_of_uchars) else None) - |> Option.fold ~none:(0, None) ~some:(fun (i, s) -> i, Some s) + |> Option.fold ~none:(None, rest |> string_of_uchars) ~some:(fun (loc, dom) -> Some loc, dom) in { localpart; resourcepart; - domainpart = drop (dend-dlen) jid |> take dlen |> string_of_uchars + domainpart; } let%expect_test {|A "bare JID"|} = |