From 10e8a0e8101fe8976fde8f004cd10e25385d45f2 Mon Sep 17 00:00:00 2001 From: Thomas Leonard Date: Fri, 6 Jan 2023 13:22:40 +0000 Subject: [PATCH] Remove test dependency on Astring The stdlib now provides most of what we need and we can implement the rest manually. --- dune-project | 1 - eio.opam | 1 - fuzz/dune | 2 +- fuzz/fuzz_buf_read.ml | 46 ++++++++++++++++++++++++++++++------------- 4 files changed, 33 insertions(+), 17 deletions(-) diff --git a/dune-project b/dune-project index f1e25a392..cbe01e111 100644 --- a/dune-project +++ b/dune-project @@ -24,7 +24,6 @@ (psq (>= 0.2.0)) (fmt (>= 0.8.9)) (hmap (>= 0.8.1)) - (astring (and (>= 0.8.5) :with-test)) (crowbar (and (>= 0.2) :with-test)) (mtime (>= 2.0.0)) (alcotest (and (>= 1.4.0) :with-test)) diff --git a/eio.opam b/eio.opam index 988aea473..01820d295 100644 --- a/eio.opam +++ b/eio.opam @@ -18,7 +18,6 @@ depends: [ "psq" {>= "0.2.0"} "fmt" {>= "0.8.9"} "hmap" {>= "0.8.1"} - "astring" {>= "0.8.5" & with-test} "crowbar" {>= "0.2" & with-test} "mtime" {>= "2.0.0"} "alcotest" {>= "1.4.0" & with-test} diff --git a/fuzz/dune b/fuzz/dune index d3bc8020a..f0e84e3fc 100644 --- a/fuzz/dune +++ b/fuzz/dune @@ -1,4 +1,4 @@ (tests (package eio) - (libraries cstruct crowbar fmt astring eio eio.mock) + (libraries cstruct crowbar fmt eio eio.mock) (names fuzz_buf_read fuzz_buf_write)) diff --git a/fuzz/fuzz_buf_read.ml b/fuzz/fuzz_buf_read.ml index 586511b9d..5cba4dce9 100644 --- a/fuzz/fuzz_buf_read.ml +++ b/fuzz/fuzz_buf_read.ml @@ -2,7 +2,21 @@ It runs random operations on both the model and the real buffer and checks they always give the same result. *) -open Astring +module String = struct + include String + + let rec find ?(start=0) p t = + if start = String.length t then None + else if p t.[start] then Some start + else find ~start:(succ start) p t + + let drop t n = String.sub t n (String.length t - n) + + let cut ~sep t = + match String.index_opt t sep with + | None -> None + | Some i -> Some (String.sub t 0 i, drop t (i + 1)) +end let debug = false @@ -27,7 +41,7 @@ let mock_flow next = object (self) | x :: xs -> let len = min (Cstruct.length buf) (String.length x) in Cstruct.blit_from_string x 0 buf 0 len; - let x' = String.with_index_range x ~first:len in + let x' = String.drop x len in next <- (if x' = "" then xs else x' :: xs); len end @@ -35,7 +49,7 @@ end module Model = struct type t = string ref - let of_chunks chunks = ref (String.concat chunks) + let of_chunks chunks = ref (String.concat "" chunks) let take_all t = let old = !t in @@ -44,11 +58,11 @@ module Model = struct old let line t = - match String.cut ~sep:"\n" !t with + match String.cut ~sep:'\n' !t with | Some (line, rest) -> if String.length line >= max_size then raise Buffer_limit_exceeded; t := rest; - if String.is_suffix ~affix:"\r" line then String.with_index_range line ~last:(String.length line - 2) + if String.ends_with ~suffix:"\r" line then String.sub line 0 (String.length line - 2) else line | None when !t = "" -> raise End_of_file | None when String.length !t >= max_size -> raise Buffer_limit_exceeded @@ -58,13 +72,16 @@ module Model = struct match !t with | "" -> raise End_of_file | s -> - t := String.with_index_range s ~first:1; - String.get_head s + t := String.drop s 1; + s.[0] - let peek_char t = String.head !t + let peek_char t = + match !t with + | "" -> None + | s -> Some (s.[0]) let consume t n = - t := String.with_index_range !t ~first:n + t := String.drop !t n let char c t = match peek_char t with @@ -75,17 +92,18 @@ module Model = struct let string s t = if debug then Fmt.pr "string %S@." s; let len_t = String.length !t in - if not (String.is_prefix ~affix:(String.with_range s ~len:len_t) !t) then failwith "string"; + let prefix = String.sub s 0 (min len_t (String.length s)) in + if not (String.starts_with ~prefix !t) then failwith "string"; if String.length s > max_size then raise Buffer_limit_exceeded; - if String.is_prefix ~affix:s !t then consume t (String.length s) + if String.starts_with ~prefix:s !t then consume t (String.length s) else raise End_of_file let take n t = if n < 0 then invalid_arg "neg"; if n > max_size then raise Buffer_limit_exceeded else if String.length !t >= n then ( - let data = String.with_range !t ~len:n in - t := String.with_range !t ~first:n; + let data = String.sub !t 0 n in + t := String.drop !t n; data ) else raise End_of_file @@ -93,7 +111,7 @@ module Model = struct match String.find (Fun.negate p) !t with | Some i when i >= max_size -> raise Buffer_limit_exceeded | Some i -> - let data = String.with_range !t ~len:i in + let data = String.sub !t 0 i in consume t i; data | None -> take_all t