22 It runs random operations on both the model and the real buffer and
33 checks they always give the same result. *)
44
5- open Astring
5+ module String = struct
6+ include String
7+
8+ let rec find ?(start =0 ) p t =
9+ if start = String. length t then None
10+ else if p t.[start] then Some start
11+ else find ~start: (succ start) p t
12+
13+ let drop t n = String. sub t n (String. length t - n)
14+
15+ let cut ~sep t =
16+ match String. index_opt t sep with
17+ | None -> None
18+ | Some i -> Some (String. sub t 0 i, drop t (i + 1 ))
19+ end
620
721let debug = false
822
@@ -27,15 +41,15 @@ let mock_flow next = object (self)
2741 | x :: xs ->
2842 let len = min (Cstruct. length buf) (String. length x) in
2943 Cstruct. blit_from_string x 0 buf 0 len;
30- let x' = String. with_index_range x ~first: len in
44+ let x' = String. drop x len in
3145 next < - (if x' = " " then xs else x' :: xs);
3246 len
3347end
3448
3549module Model = struct
3650 type t = string ref
3751
38- let of_chunks chunks = ref (String. concat chunks)
52+ let of_chunks chunks = ref (String. concat " " chunks)
3953
4054 let take_all t =
4155 let old = ! t in
@@ -44,11 +58,11 @@ module Model = struct
4458 old
4559
4660 let line t =
47- match String. cut ~sep: " \n " ! t with
61+ match String. cut ~sep: '\n' ! t with
4862 | Some (line , rest ) ->
4963 if String. length line > = max_size then raise Buffer_limit_exceeded ;
5064 t := rest;
51- if String. is_suffix ~affix :" \r " line then String. with_index_range line ~last: (String. length line - 2 )
65+ if String. ends_with ~suffix :" \r " line then String. sub line 0 (String. length line - 2 )
5266 else line
5367 | None when ! t = " " -> raise End_of_file
5468 | None when String. length ! t > = max_size -> raise Buffer_limit_exceeded
@@ -58,13 +72,16 @@ module Model = struct
5872 match ! t with
5973 | "" -> raise End_of_file
6074 | s ->
61- t := String. with_index_range s ~first: 1 ;
62- String. get_head s
75+ t := String. drop s 1 ;
76+ s.[ 0 ]
6377
64- let peek_char t = String. head ! t
78+ let peek_char t =
79+ match ! t with
80+ | "" -> None
81+ | s -> Some (s.[0 ])
6582
6683 let consume t n =
67- t := String. with_index_range ! t ~first: n
84+ t := String. drop ! t n
6885
6986 let char c t =
7087 match peek_char t with
@@ -75,25 +92,26 @@ module Model = struct
7592 let string s t =
7693 if debug then Fmt. pr " string %S@." s;
7794 let len_t = String. length ! t in
78- if not (String. is_prefix ~affix: (String. with_range s ~len: len_t) ! t) then failwith " string" ;
95+ let prefix = String. sub s 0 (min len_t (String. length s)) in
96+ if not (String. starts_with ~prefix ! t) then failwith " string" ;
7997 if String. length s > max_size then raise Buffer_limit_exceeded ;
80- if String. is_prefix ~affix : s ! t then consume t (String. length s)
98+ if String. starts_with ~prefix : s ! t then consume t (String. length s)
8199 else raise End_of_file
82100
83101 let take n t =
84102 if n < 0 then invalid_arg " neg" ;
85103 if n > max_size then raise Buffer_limit_exceeded
86104 else if String. length ! t > = n then (
87- let data = String. with_range ! t ~len: n in
88- t := String. with_range ! t ~first: n;
105+ let data = String. sub ! t 0 n in
106+ t := String. drop ! t n;
89107 data
90108 ) else raise End_of_file
91109
92110 let take_while p t =
93111 match String. find (Fun. negate p) ! t with
94112 | Some i when i > = max_size -> raise Buffer_limit_exceeded
95113 | Some i ->
96- let data = String. with_range ! t ~len: i in
114+ let data = String. sub ! t 0 i in
97115 consume t i;
98116 data
99117 | None -> take_all t
0 commit comments