Source

ocaml-sip / src / Sip_utils.ml

Full commit
(*********************************************************************************************************************
 *
 *)
open Batteries

(********************************************************************************************************************
 * Utils
 *)

exception Invalid_sip_message

let eol_sep = "\r\n"

let is_sep seps = function
    | c when List.mem c seps -> true
    | _ -> false

let rec skip_seps seps buf = function
    | n when n = String.length buf ->
        n

    | n when is_sep seps (String.get buf n) ->
        skip_seps seps buf (succ n)

    | n ->
        n

let find_sep seps start buf =
    let rec find_end start = function
        | n when n = String.length buf ->
            (String.slice buf ~first:start ~last:n, n)

        | n when is_sep seps (String.get buf n) ->
            (String.slice buf ~first:start ~last:n, n)

        | n ->
            find_end start (succ n)

        in

    let remember_arg f x = f x x 
        in
    (remember_arg find_end -| skip_seps seps buf) start

let find_word = find_sep [' '; '\r'; '\n'] 

let find_endline index buf =
    let last = String.find_from buf index eol_sep in
    (String.slice buf ~first:index ~last:last, succ (succ last))