Skip to content

Commit 10e8a0e

Browse files
committed
Remove test dependency on Astring
The stdlib now provides most of what we need and we can implement the rest manually.
1 parent 32c26ab commit 10e8a0e

File tree

4 files changed

+33
-17
lines changed

4 files changed

+33
-17
lines changed

dune-project

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
(psq (>= 0.2.0))
2525
(fmt (>= 0.8.9))
2626
(hmap (>= 0.8.1))
27-
(astring (and (>= 0.8.5) :with-test))
2827
(crowbar (and (>= 0.2) :with-test))
2928
(mtime (>= 2.0.0))
3029
(alcotest (and (>= 1.4.0) :with-test))

eio.opam

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ depends: [
1818
"psq" {>= "0.2.0"}
1919
"fmt" {>= "0.8.9"}
2020
"hmap" {>= "0.8.1"}
21-
"astring" {>= "0.8.5" & with-test}
2221
"crowbar" {>= "0.2" & with-test}
2322
"mtime" {>= "2.0.0"}
2423
"alcotest" {>= "1.4.0" & with-test}

fuzz/dune

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
(tests
22
(package eio)
3-
(libraries cstruct crowbar fmt astring eio eio.mock)
3+
(libraries cstruct crowbar fmt eio eio.mock)
44
(names fuzz_buf_read fuzz_buf_write))

fuzz/fuzz_buf_read.ml

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,21 @@
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

721
let 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
3347
end
3448

3549
module 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

Comments
 (0)