@@ -5,21 +5,49 @@ module Transfer = struct
5
5
end
6
6
7
7
module Header = struct
8
+ (* [caseless_equal a b] must be equivalent to
9
+ [String.equal (String.lowercase_ascii a) (String.lowercase_ascii b)]. *)
8
10
let caseless_equal a b =
9
11
if a == b then true
10
12
else
11
13
let len = String. length a in
12
14
len = String. length b
15
+ (* Note: at this point we konw that [a] and [b] have the same length. *)
13
16
&&
14
- let stop = ref false in
15
- let idx = ref 0 in
16
- while (not ! stop) && ! idx < len do
17
- let c1 = String. unsafe_get a ! idx in
18
- let c2 = String. unsafe_get b ! idx in
19
- if Char. lowercase_ascii c1 <> Char. lowercase_ascii c2 then stop := true ;
20
- incr idx
21
- done ;
22
- not ! stop
17
+ (* [word_loop a b i len] compares strings [a] and [b] from
18
+ offsets [i] (included) to [len] (excluded), one word at a time.
19
+ [i] is a world-aligned index into the strings.
20
+ *)
21
+ let rec word_loop a b i len =
22
+ if i = len then true
23
+ else
24
+ let i' = i + 8 in
25
+ (* If [i' > len], what remains to be compared is strictly
26
+ less than a word long, use byte-per-byte comparison. *)
27
+ if i' > len then byte_loop a b i len
28
+ else if String. get_int64_ne a i = String. get_int64_ne b i then
29
+ word_loop a b i' len
30
+ else
31
+ (* If the words at [i] differ, it may due to a case
32
+ difference; we check the individual bytes of this
33
+ work, and then we continue checking the other
34
+ words. *)
35
+ byte_loop a b i i' && word_loop a b i' len
36
+ (* [byte_loop a b i len] compares the strings [a] and [b] from
37
+ offsets [i] (included) to [len] (excluded), one byte at
38
+ a time.
39
+
40
+ This function assumes that [i < len] holds -- its only called
41
+ by [word_loop] when this is known to hold. *)
42
+ and byte_loop a b i len =
43
+ let c1 = String. unsafe_get a i in
44
+ let c2 = String. unsafe_get b i in
45
+ Char. lowercase_ascii c1 = Char. lowercase_ascii c2
46
+ &&
47
+ let i' = i + 1 in
48
+ i' = len || byte_loop a b i' len
49
+ in
50
+ word_loop a b 0 len
23
51
24
52
type t = (string * string ) list
25
53
0 commit comments