Debian Codegraph @

src/Util/Strings/String.v

raw
1 From Coq Require Import Lia.
2 From Coq Require Import PeanoNat.
3 From Coq Require Import String.
4 From Coq Require Import Ascii.
5 Require Import Crypto.Util.Strings.Ascii.
6
7 Local Open Scope list_scope.
8 Local Open Scope string_scope.
9
10
11 (** TODO: Remove this once we drop support for Coq 8.9 (it's in the stdlib in Coq 8.10 *)
12 (** *** Conversion to/from [list ascii] *)
13
14 Fixpoint string_of_list_ascii (s : list ascii) : string
15 := match s with
16 | nil => EmptyString
17 | cons ch s => String ch (string_of_list_ascii s)
18 end.
19
20 Fixpoint list_ascii_of_string (s : string) : list ascii
21 := match s with
22 | EmptyString => nil
23 | String ch s => cons ch (list_ascii_of_string s)
24 end.
25
26 Lemma string_of_list_ascii_of_string s : string_of_list_ascii (list_ascii_of_string s) = s.
27 Proof.
28 induction s as [|? ? IHs]; [ reflexivity | cbn; apply f_equal, IHs ].
29 Defined.
30
31 Lemma list_ascii_of_string_of_list_ascii s : list_ascii_of_string (string_of_list_ascii s) = s.
32 Proof.
33 induction s as [|? ? IHs]; [ reflexivity | cbn; apply f_equal, IHs ].
34 Defined.
35
36 (** *** String concatenation *)
37
38 Lemma string_of_list_ascii_app s1 s2 : string_of_list_ascii (s1 ++ s2) = string_of_list_ascii s1 ++ string_of_list_ascii s2.
39 Proof. induction s1 as [|x xs IHxs]; cbn; [ | rewrite IHxs ]; reflexivity. Qed.
40 Lemma list_ascii_of_string_app s1 s2 : list_ascii_of_string (s1 ++ s2) = (list_ascii_of_string s1 ++ list_ascii_of_string s2)%list.
41 Proof. induction s1 as [|x xs IHxs]; cbn; [ | rewrite IHxs ]; reflexivity. Qed.
42
43 Definition lift_list_function (f : list ascii -> list ascii) (s : string) : string
44 := string_of_list_ascii (f (list_ascii_of_string s)).
45
46 (** *** Mapping over a string *)
47 (** [map f s] applies function [f] in turn to all the characters of
48 [s], creating a new string to return *)
49 Definition map (f : ascii -> ascii) : string -> string
50 := lift_list_function (List.map f).
51 Lemma map_step f s
52 : map f s = match s with
53 | EmptyString => EmptyString
54 | String x xs => String (f x) (map f xs)
55 end.
56 Proof. destruct s; reflexivity. Qed.
57
58
59 (** *** Lower and upper case *)
60 Definition to_lower (s : string) : string := map Ascii.to_lower s.
61 Definition to_upper (s : string) : string := map Ascii.to_upper s.
62
63 (** *** string reversal *)
64 (** [rev s] reverses the string [s] *)
65 (** Note that we use [rev_append . []] for non-quadratic behavior *)
66 Definition rev : string -> string
67 := lift_list_function (fun s => List.rev_append s nil).
68
69 Lemma rev_step s
70 : rev s
71 = match s with
72 | EmptyString => EmptyString
73 | String x xs => rev xs ++ String x EmptyString
74 end.
75 Proof. destruct s; cbn; rewrite ?List.rev_append_rev, ?List.app_nil_r, ?string_of_list_ascii_app; reflexivity. Qed.
76
77 Fixpoint take_while_drop_while (f : ascii -> bool) (s : string) : string * string
78 := match s with
79 | EmptyString => (EmptyString, EmptyString)
80 | String ch s
81 => if f ch
82 then let '(s1, s2) := take_while_drop_while f s in
83 (String ch s1, s2)
84 else (EmptyString, String ch s)
85 end.
86
87 (** *** trimming a string *)
88 (** [trim s] returns a copy of the argument, without leading and trailing
89 whitespace. The characters regarded as whitespace are: ' ',
90 '\012', '\n', '\r', and '\t'. *)
91 Definition ltrim (s : string) : string
92 := snd (take_while_drop_while Ascii.is_whitespace s).
93
94 Definition rtrim (s : string) : string
95 := rev (ltrim (rev s)).
96
97 Definition trim (s : string) : string
98 := rtrim (ltrim s).
99
100 (** Finds last occurence of [s1] in [s2], where the first character
101 after [s1] is at position less than or equal to [n + 1]. Returns
102 the location of the first character of [s1] in that occurence in
103 [s2], if it exists. *)
104
105 Definition rindex (n : nat) (s1 s2 : string) : option nat :=
106 option_map
107 (fun n => length s2 - n - length s1)
108 (index (length s2 - n - 1) (rev s1) (rev s2)).
109
110
111 Lemma append_alt s1 s2 : s1 ++ s2 = string_of_list_ascii (list_ascii_of_string s1 ++ list_ascii_of_string s2).
112 Proof. rewrite string_of_list_ascii_app, !string_of_list_ascii_of_string; reflexivity. Qed.
113 Lemma append_empty_r s : s ++ "" = s.
114 Proof.
115 rewrite append_alt; cbn; rewrite List.app_nil_r, string_of_list_ascii_of_string. reflexivity.
116 Qed.
117 Lemma append_assoc s1 s2 s3 : s1 ++ (s2 ++ s3) = (s1 ++ s2) ++ s3.
118 Proof.
119 rewrite !append_alt, !list_ascii_of_string_of_list_ascii; cbn; rewrite List.app_assoc; reflexivity.
120 Qed.
121
122 Lemma substring_alt n m s
123 : substring n m s = string_of_list_ascii (List.firstn m (List.skipn n (list_ascii_of_string s))).
124 Proof.
125 revert n m; induction s as [|ch s IHs]; intros [|n] [|m]; try reflexivity; cbn;
126 rewrite IHs; cbn; try reflexivity.
127 Qed.
128
129 Lemma length_substring n1 n2 s
130 : length (substring n1 n2 s) = Nat.min n2 (length s - n1).
131 Proof.
132 revert n1 n2; induction s as [|a s IHs]; intros; cbn.
133 { destruct n1, n2; cbn; reflexivity. }
134 { destruct n1; [ destruct n2 | ]; cbn; rewrite ?IHs, ?Nat.sub_0_r; reflexivity. }
135 Qed.
136
137 Lemma length_append s1 s2 : length (s1 ++ s2) = length s1 + length s2.
138 Proof.
139 revert s1; induction s1 as [|a s1 IHs1]; cbn; congruence.
140 Qed.
141
142 (** *** membership *)
143 (** [contains n s1 s2] returns [true] if [s1] occurs in [s2] starting
144 at position [n], and false otherwise *)
145 Definition contains (n : nat) (s1 s2 : string) : bool :=
146 match index n s1 s2 with
147 | Some _ => true
148 | None => false
149 end.
150
151 (** [startswith prefix s] returns [true] iff [s] starts with [prefix] *)
152 Definition startswith (prefix s : string) : bool
153 := (prefix =? (substring 0 (length prefix) s))%string.
154
155 Lemma startswith_correct prefix s
156 : startswith prefix s = true <-> substring 0 (length prefix) s = prefix.
157 Proof.
158 pose proof (String.eqb_spec prefix (substring 0 (length prefix) s)) as H'.
159 cbv [startswith].
160 split; intro H; destruct H'; congruence.
161 Qed.
162
163 (** [endswith postfix s] returns [true] iff [s] ends with [postfix] *)
164 Definition endswith (postfix s : string) : bool
165 := (postfix =? (substring (length s - length postfix) (length postfix) s))%string.
166
167 Lemma endswith_correct postfix s
168 : endswith postfix s = true <-> substring (length s - length postfix) (length postfix) s = postfix.
169 Proof.
170 pose proof (String.eqb_spec postfix (substring (length s - length postfix) (length postfix) s)) as H'.
171 cbv [endswith].
172 split; intro H; destruct H'; congruence.
173 Qed.
174
175 Section strip_prefix_cps.
176 Context {R} (found : string -> R)
177 (remaining : string -> string -> R).
178
179 Fixpoint strip_prefix_cps (sepch : ascii) (sep : string) (s2 : string) {struct s2} : R
180 := match s2 with
181 | EmptyString => remaining (String sepch sep) s2
182 | String x xs
183 => if (sepch =? x)%char
184 then match sep with
185 | EmptyString => found xs
186 | String sepch sep
187 => strip_prefix_cps sepch sep xs
188 end
189 else remaining (String sepch sep) s2
190 end.
191
192 Lemma strip_prefix_cps_found sepch sep s2 s2'
193 (Hfound : String sepch sep ++ s2' = s2)
194 : strip_prefix_cps sepch sep s2 = found s2'.
195 Proof using Type.
196 revert sepch sep Hfound; induction s2 as [|ch s2 IHs2]; intros; cbn in Hfound; inversion Hfound; clear Hfound; subst.
197 destruct sep; cbn [strip_prefix_cps]; now rewrite Ascii.eqb_refl; auto.
198 Qed.
199 End strip_prefix_cps.
200
201 (** *** splitting a string *)
202 (** [split sep s] returns the list of all (possibly empty) substrings
203 of [s] that are delimited by the [sep] character.
204
205 The function's output is specified by the following invariants:
206
207 - The list is not empty.
208
209 - Concatenating its elements using sep as a separator returns a
210 string equal to the input
211
212 [concat sep (split sep s) = s]
213
214 - No string in the result contains [sep] as a substring, unless
215 [sep] is the empty string, in which case this function returns
216 [map (fun ch => String ch "") (list_ascii_of_string s)] if [s] is non-empty,
217 and [""::nil] if [s] is also empty. *)
218
219 Fixpoint split_helper (sepch : ascii) (sep : string) (s : string) (rev_acc : string) {struct s}
220 : list string
221 := strip_prefix_cps
222 (fun s => rev rev_acc :: split_helper sepch sep s "")
223 (fun _ _
224 => match s with
225 | EmptyString => rev rev_acc :: nil
226 | String x xs
227 => split_helper sepch sep xs (String x rev_acc)
228 end)
229 sepch sep s.
230
231 Definition split (sep : string) (s : string) : list string
232 := match sep with
233 | EmptyString
234 => if (s =? "")%string
235 then "" :: nil
236 else List.map (fun ch => String ch "") (list_ascii_of_string s)
237 | String sepch sep
238 => split_helper sepch sep s ""
239 end.
240
241 Fixpoint split_helper_non_empty sepch sep s rev_acc {struct s}
242 : split_helper sepch sep s rev_acc <> nil.
243 Proof.
244 destruct s as [|x xs]; cbn [split_helper];
245 [ cbn; clear split_helper_non_empty; congruence | ].
246 generalize (split_helper_non_empty sepch sep xs (String x rev_acc)).
247 generalize (split_helper sepch sep xs (String x rev_acc)).
248 intros ls H.
249 generalize (String x xs).
250 generalize sep at 2.
251 generalize sepch at 2.
252 fix H' 3.
253 intros sepch' sep' s'.
254 destruct s' as [|a s']; cbn; [ assumption | ].
255 destruct (sepch' =? a)%char, sep'; try (clear H' split_helper_non_empty; congruence).
256 apply H'.
257 Qed.
258
259 Lemma split_non_empty sep s : split sep s <> nil.
260 Proof.
261 unfold split; destruct sep, s; try apply split_helper_non_empty; cbn;
262 congruence.
263 Qed.
264
265 Lemma split_empty_empty : split EmptyString EmptyString = "" :: nil.
266 Proof. reflexivity. Qed.
267 Lemma split_empty_non_empty s
268 : s <> EmptyString
269 -> split EmptyString s = List.map (fun ch => String ch "") (list_ascii_of_string s).
270 Proof.
271 intro H; destruct s; cbn; congruence.
272 Qed.
273
274 Fixpoint split_helper_no_substring sepch sep s rev_acc
275 : List.Forall (fun s' => contains 0 (rev rev_acc ++ String sepch sep) s' = false)
276 (split_helper sepch sep s rev_acc).
277 Proof.
278 destruct s as [|x xs]; cbn [split_helper];
279 [ cbn; clear split_helper_no_substring;
280 repeat constructor
281 | ].
282 { unfold contains.
283 edestruct index eqn:H; [ | reflexivity ].
284 apply index_correct1 in H.
285 apply (f_equal length) in H.
286 rewrite length_substring, !length_append in H.
287 revert H; apply PeanoNat.Nat.min_case_strong; cbn; intros; lia. }
288 { generalize (split_helper_no_substring sepch sep xs (String x rev_acc)).
289 generalize (split_helper sepch sep xs (String x rev_acc)).
290 intros ls.
291 clear split_helper_no_substring; intro split_helper_no_substring.
292 Abort.
293 Lemma split_no_substring sep s (Hsep : sep <> EmptyString)
294 : List.Forall (fun s' => contains 0 sep s' = false)
295 (split sep s).
296 Proof.
297 destruct sep as [|sepch sep]; [ congruence | ].
298 unfold split.
299 Abort.
300
301 Fixpoint concat_split_helper sepch sep s rev_acc
302 : concat (String sepch sep) (split_helper sepch sep s rev_acc)
303 = rev rev_acc ++ s.
304 Proof.
305 destruct s as [|x xs]; cbn [split_helper];
306 [ cbn; rewrite append_empty_r; reflexivity | ].
307 generalize (concat_split_helper sepch sep xs (String x rev_acc)).
308 generalize (split_helper sepch sep xs (String x rev_acc)).
309 intros ls H.
310 rewrite rev_step, <- append_assoc in H; cbn in H; revert H.
311 generalize (String x xs); clear x xs.
312 revert sep.
313 revert sepch.
314 fix H' 3.
315 intros sepch' sep' s'; intros.
316 destruct s' as [|a s']; cbn; [ assumption | ].
317 destruct (Ascii.eqb_spec sepch' a), sep'; try (clear H' concat_split_helper; congruence).
318 { cbn [concat].
319 rewrite (concat_split_helper sepch' "" s').
320 destruct (split_helper sepch' "" s' "") eqn:H''.
321 { apply split_helper_non_empty in H''; exfalso; assumption. }
322 { cbn; subst; reflexivity. } }
323 { subst.
324 Abort.
325
326 Lemma concat_split sep s : concat sep (split sep s) = s.
327 Proof.
328 destruct sep; cbn.
329 { destruct s as [|x xs]; [ reflexivity | cbn ].
330 revert x; induction xs as [|x' xs' IHxs]; intro x; cbn; trivial.
331 apply f_equal; auto. }
332 {
333 Abort.
334
335 Definition replace (from to s : string) : string
336 := concat to (split from s).
337
338 Notation Null := (String Ascii.Null "").
339 Notation Backspace := (String Ascii.Backspace "").
340 Notation NewLine := (String Ascii.NewLine "").
341 Notation CR := (String Ascii.CR "").
342 Notation LF := (String Ascii.LF "").
343 Notation CRLF := (String Ascii.CR (String Ascii.LF "")).
344 Notation Tab := (String Ascii.Tab "").
345 Notation NewPage := (String Ascii.NewPage "").
346
347 (** Given a list of strings, breaks all strings within the list at
348 CFLF, CF, and LF. Useful for normalizing a newline-separated list
349 of strings, where some substrings might contain newlines *)
350 Definition split_newlines (s : list string) : list string
351 := List.flat_map (split CR) (List.flat_map (split LF) (List.flat_map (split CRLF) s)).
352
353 (** Title case makes all words after a space begin with a capital letter *)
354 Definition capitalize_first_letter (s : string) : string
355 := match s with
356 | "" => ""
357 | String ch s => String (Ascii.to_upper ch) s
358 end.
359 Definition uncapitalize_first_letter (s : string) : string
360 := match s with
361 | "" => ""
362 | String ch s => String (Ascii.to_lower ch) s
363 end.
364 Definition to_title_case_sep (sep : string) (s : string) : string
365 := concat sep (List.map capitalize_first_letter (split sep s)).
366 Definition to_title_case (s : string) : string
367 := to_title_case_sep " " s.
368
369 Lemma substring_0_0 :
370 forall s, substring 0 0 s = "".
371 Proof. destruct s; reflexivity. Qed.
372
373 Lemma append_eq_r_iff s1 s2 s3 :
374 s1 ++ s2 = s1 ++ s3 <-> s2 = s3.
375 Proof.
376 induction s1; cbn [append]; split;
377 try inversion 1; intros; auto; [ ].
378 apply IHs1. auto.
379 Qed.
380
381 Lemma append_eq_prefix :
382 forall s1 s2 s3 s4,
383 s1 ++ s2 = s3 ++ s4 ->
384 prefix s1 s3 = true \/ prefix s3 s1 = true.
385 Proof.
386 induction s1; destruct s3; cbn [append prefix] in *;
387 intros; try tauto; [ ].
388 match goal with H : String _ _ = String _ _ |- _ =>
389 inversion H; clear H; subst end.
390 match goal with |- context [ascii_dec ?x ?y] =>
391 destruct (ascii_dec x y) end;
392 subst; try tauto; [ ].
393 eapply IHs1; eauto.
394 Qed.
395
396 Definition repeat (ch : ascii) (n : nat) : string
397 := string_of_list_ascii (List.repeat ch n).
398
399 (** TODO: Better name for this? *)
400 Definition rfill (ch : ascii) (ls : list string) : list string
401 := let len := List.fold_right Nat.max 0 (List.map String.length ls) in
402 let fill s := s ++ repeat ch (len - String.length s) in
403 List.map fill ls.
404
405 Definition strip_trailing_spaces (s : string) : string
406 := concat NewLine (List.map rtrim (split NewLine s)).
407
408 Fixpoint strip_leading_newlines (s : list string) : list string
409 := match s with
410 | nil => nil
411 | s :: ls => if (rtrim s =? "")%string
412 then strip_leading_newlines ls
413 else s :: ls
414 end.
415
416 Definition strip_trailing_newlines (s : list string) : list string
417 := List.rev_append (strip_leading_newlines (List.rev_append s nil)) nil.
418
419 (** [is_substring s1 s2] returns [true] if [s1] is a substring of [s2] *)
420 Definition is_substring (s1 s2 : string) : bool :=
421 if (s1 =? "")%string
422 then true
423 else
424 match split s1 s2 with
425 | nil => false
426 | _ :: nil => false
427 | _ :: _ :: _ => true
428 end.
429
430 Lemma is_substring_correct s1 s2 :
431 is_substring s1 s2 = true <-> exists n, substring n (String.length s1) s2 = s1.
432 Proof.
433 Abort.
434
435 Definition strip_trailing_newline (s : string) : string :=
436 if endswith LF s
437 then if endswith CRLF s
438 then substring 0 (String.length s - 2) s
439 else substring 0 (String.length s - 1) s
440 else s.