Source file irc_helpers.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
let split ~str ~c =
let rec rev_split' ~str ~i ~c ~acc =
try
let index = String.index_from str i c in
let before = String.sub str i (index-i) in
rev_split' ~str ~c ~i:(index+1) ~acc:(before :: acc)
with Not_found ->
String.sub str i (String.length str - i) :: acc
in
List.rev (rev_split' ~str ~i:0 ~c ~acc:[])
let split1_exn ~str ~c =
let index = String.index str c in
let before = String.sub str 0 index in
let after = String.sub str (index + 1) (String.length str - index - 1) in
before, after
let get_whole_lines ~str =
let rec find i acc =
try
let j = String.index_from str i '\n' in
if i=j then find (j+1) acc
else
let line = String.sub str i (j-i-1) in
find (j+1) (line :: acc)
with Not_found ->
if i=String.length str
then List.rev acc, `NoRest
else List.rev acc, `Rest (String.sub str i (String.length str - i))
in
find 0 []
let handle_input ~buffer ~input =
Buffer.add_string buffer input;
let whole_lines, rest = get_whole_lines ~str:(Buffer.contents buffer) in
Buffer.clear buffer;
begin match rest with
| `NoRest -> ()
| `Rest s -> Buffer.add_string buffer s;
end;
whole_lines
module Log = (val Logs.src_log (Logs.Src.create ~doc:"irc-client low level logging" "irc-client"))