Skip to main content

vstd/
string.rs

1#![feature(rustc_attrs)]
2#![allow(unused_imports)]
3
4#[cfg(all(feature = "alloc", not(verus_verify_core)))]
5use alloc::str::Chars;
6#[cfg(all(feature = "alloc", not(verus_verify_core)))]
7use alloc::string::{self, String, ToString};
8#[cfg(all(verus_keep_ghost, not(verus_verify_core)))]
9use core::ops::{Bound, Range, RangeFrom, RangeFull, RangeInclusive, RangeTo, RangeToInclusive};
10#[cfg(all(verus_keep_ghost, not(verus_verify_core)))]
11use core::slice::SliceIndex;
12
13use super::prelude::*;
14use super::seq::Seq;
15use super::slice::*;
16#[cfg(all(verus_keep_ghost, feature = "alloc", not(verus_verify_core)))]
17use super::std_specs::fmt::DisplaySpec;
18#[cfg(verus_keep_ghost)]
19#[cfg(all(feature = "alloc", not(verus_verify_core)))]
20use super::std_specs::iter::IteratorSpec;
21#[cfg(verus_keep_ghost)]
22use super::std_specs::range::{
23    ExRange, RangeBoundsSpec, slice_range_end, slice_range_start, slice_range_valid,
24    spec_range_inclusive_is_empty,
25};
26use super::utf8::*;
27use super::view::*;
28
29#[cfg(verus_keep_ghost)]
30#[cfg(not(verus_verify_core))]
31use super::std_specs::cmp::PartialEqSpecImpl;
32
33verus! {
34
35broadcast use {super::seq::group_seq_lemmas, super::slice::group_slice_axioms};
36
37#[cfg(not(verus_verify_core))]
38impl View for str {
39    type V = Seq<char>;
40
41    uninterp spec fn view(&self) -> Seq<char>;
42}
43
44#[cfg(not(verus_verify_core))]
45impl DeepView for str {
46    type V = Seq<char>;
47
48    open spec fn deep_view(&self) -> Seq<char> {
49        self.view()
50    }
51}
52
53#[cfg(verus_keep_ghost)]
54#[cfg(not(verus_verify_core))]
55impl PartialEqSpecImpl for str {
56    open spec fn obeys_eq_spec() -> bool {
57        true
58    }
59
60    open spec fn eq_spec(&self, other: &str) -> bool {
61        self@ == other@
62    }
63}
64
65#[cfg(not(verus_verify_core))]
66pub trait StringSliceAdditionalSpecFns {
67    spec fn spec_bytes(&self) -> Seq<u8>;
68}
69
70#[cfg(not(verus_verify_core))]
71impl StringSliceAdditionalSpecFns for str {
72    open spec fn spec_bytes(&self) -> Seq<u8> {
73        encode_utf8(self@)
74    }
75}
76
77#[cfg(not(verus_verify_core))]
78pub open spec fn is_ascii(s: &str) -> bool {
79    is_ascii_chars(s@)
80}
81
82#[cfg(not(verus_verify_core))]
83pub broadcast proof fn is_ascii_spec_bytes(s: &str)
84    ensures
85        #[trigger] is_ascii(s) ==> #[trigger] s.spec_bytes() =~= Seq::new(
86            s@.len(),
87            |i| s@.index(i) as u8,
88        ),
89{
90    if (is_ascii(s)) {
91        is_ascii_chars_encode_utf8(s@);
92    }
93}
94
95#[cfg(not(verus_verify_core))]
96pub broadcast proof fn is_ascii_concat(s1: &str, s2: &str, s3: &str)
97    requires
98        s1@ =~= s2@ + s3@,
99    ensures
100        #![trigger s2@ + s3@, is_ascii(s1), is_ascii(s2), is_ascii(s3)]
101        is_ascii(s1) <==> is_ascii(s2) && is_ascii(s3),
102{
103    broadcast use is_ascii_chars_concat;
104
105    if (is_ascii(s1)) {
106        is_ascii_chars_concat(s1@, s2@, s3@);
107    }
108}
109
110#[cfg(not(verus_verify_core))]
111#[verifier::when_used_as_spec(is_ascii)]
112pub assume_specification[ str::is_ascii ](s: &str) -> (b: bool)
113    ensures
114        b == is_ascii(s),
115;
116
117#[cfg(all(feature = "alloc", not(verus_verify_core)))]
118use crate::alloc::borrow::ToOwned;
119
120#[cfg(all(feature = "alloc", not(verus_verify_core)))]
121pub assume_specification[ str::to_owned ](s: &str) -> (res: String)
122    ensures
123        s@ == res@,
124;
125
126#[cfg(not(verus_verify_core))]
127pub assume_specification[ str::as_bytes ](s: &str) -> (b: &[u8])
128    ensures
129        b@ == s.spec_bytes(),
130    opens_invariants none
131    no_unwind
132;
133
134#[cfg(not(verus_verify_core))]
135#[verifier::allow_in_spec]
136pub assume_specification[ str::len ](s: &str) -> usize
137    returns
138        s.spec_bytes().len() as usize,
139    opens_invariants none
140    no_unwind
141;
142
143#[cfg(not(verus_verify_core))]
144#[verifier::allow_in_spec]
145pub assume_specification[ str::is_empty ](s: &str) -> bool
146    returns
147        s@.len() == 0,
148    opens_invariants none
149    no_unwind
150;
151
152#[cfg(not(verus_verify_core))]
153#[verifier::allow_in_spec]
154pub assume_specification[ str::is_char_boundary ](s: &str, index: usize) -> bool
155    returns
156        is_char_boundary(s.spec_bytes(), index as int),
157;
158
159#[cfg(not(verus_verify_core))]
160pub assume_specification[ str::split_at ](s: &str, mid: usize) -> (res: (&str, &str))
161    requires
162        is_char_boundary(s.spec_bytes(), mid as int),
163    ensures
164        res.0.spec_bytes() =~= s.spec_bytes()[..mid],
165        res.1.spec_bytes() =~= s.spec_bytes()[mid..],
166;
167
168#[cfg(not(verus_verify_core))]
169pub assume_specification[ str::from_utf8_unchecked ](v: &[u8]) -> (res: &str)
170    requires
171        valid_utf8(v@),
172    ensures
173        res.spec_bytes() =~= v@,
174;
175
176#[cfg(all(feature = "alloc", not(verus_verify_core)))]
177pub uninterp spec fn to_string_from_display_ensures<T: core::fmt::Display + ?Sized>(
178    t: &T,
179    s: String,
180) -> bool;
181
182#[cfg(all(feature = "alloc", not(verus_verify_core)))]
183pub broadcast proof fn to_string_from_display_ensures_for_str(t: &str, res: String)
184    ensures
185        #[trigger] to_string_from_display_ensures::<str>(t, res) <==> (t@ == res@),
186{
187    admit();
188}
189
190#[cfg(all(feature = "alloc", not(verus_verify_core)))]
191pub assume_specification<T: core::fmt::Display + ?Sized>[ <T as ToString>::to_string ](
192    t: &T,
193) -> (res: String)
194    requires
195        forall|f: &core::fmt::Formatter<'_>| t.fmt_req(f),
196    ensures
197        to_string_from_display_ensures::<T>(t, res),
198;
199
200#[cfg(not(verus_verify_core))]
201#[verifier::external]
202pub trait StrSliceExecFns {
203    fn unicode_len(&self) -> usize;
204
205    fn get_char(&self, i: usize) -> char;
206
207    fn substring_ascii<'a>(&'a self, from: usize, to: usize) -> &'a str;
208
209    fn substring_char<'a>(&'a self, from: usize, to: usize) -> &'a str;
210
211    fn get_ascii(&self, i: usize) -> u8;
212
213    #[cfg(feature = "alloc")]
214    fn as_bytes_vec(&self) -> alloc::vec::Vec<u8>;
215}
216
217#[cfg(not(verus_verify_core))]
218impl StrSliceExecFns for str {
219    /// The len() function in rust returns the byte length.
220    /// It is more useful to talk about the length of characters and therefore this function was added.
221    /// Please note that this function counts the unicode variation selectors as characters.
222    /// Warning: O(n)
223    #[verifier::external_body]
224    fn unicode_len(&self) -> (l: usize)
225        ensures
226            l as nat == self@.len(),
227    {
228        self.chars().count()
229    }
230
231    /// Warning: O(n) not O(1) due to unicode decoding needed
232    #[verifier::external_body]
233    fn get_char(&self, i: usize) -> (c: char)
234        requires
235            i < self@.len(),
236        ensures
237            self@.index(i as int) == c,
238    {
239        self.chars().nth(i).unwrap()
240    }
241
242    #[verifier::external_body]
243    fn substring_ascii<'a>(&'a self, from: usize, to: usize) -> (ret: &'a str)
244        requires
245            self.is_ascii(),
246            from <= to <= self@.len(),
247        ensures
248            ret@ == self@[from..to],
249            ret.is_ascii(),
250    {
251        // Range::index panics if from > to or from > self@.len()
252        &self[from..to]
253    }
254
255    #[verifier::external_body]
256    fn substring_char<'a>(&'a self, from: usize, to: usize) -> (ret: &'a str)
257        requires
258            from <= to <= self@.len(),
259        ensures
260            ret@ == self@[from..to],
261    {
262        let mut char_pos = 0;
263        let mut byte_start = None;
264        let mut byte_end = None;
265        let mut byte_pos = 0;
266        let mut it = self.chars();
267        loop {
268            if char_pos == from {
269                byte_start = Some(byte_pos);
270            }
271            if char_pos == to {
272                byte_end = Some(byte_pos);
273                break;
274            }
275            if let Some(c) = it.next() {
276                char_pos += 1;
277                byte_pos += c.len_utf8();
278            } else {
279                break;
280            }
281        }
282        let byte_start = byte_start.unwrap();
283        let byte_end = byte_end.unwrap();
284        // Range::index panics if from > to or from > self@.len()
285        &self[byte_start..byte_end]
286    }
287
288    fn get_ascii(&self, i: usize) -> (b: u8)
289        requires
290            self.is_ascii(),
291            i < self@.len(),
292        ensures
293            self@.index(i as int) as u8 == b,
294    {
295        broadcast use is_ascii_spec_bytes;
296        // panics if i is not a valid index
297
298        self.as_bytes()[i]
299    }
300
301    #[cfg(feature = "alloc")]
302    fn as_bytes_vec(&self) -> (ret: alloc::vec::Vec<u8>)
303        ensures
304            ret@ == self.spec_bytes(),
305    {
306        slice_to_vec(self.as_bytes())
307    }
308}
309
310#[cfg(not(verus_verify_core))]
311pub uninterp spec fn strlit_view_id(s: Seq<char>) -> int;
312
313/// Distinct string literals have provably distinct views.
314#[cfg(not(verus_verify_core))]
315pub broadcast axiom fn axiom_new_strlit_view_id(id: int)
316    ensures
317        strlit_view_id(#[trigger] strslice_new_strlit(id).view()) == id,
318;
319
320#[cfg(not(verus_verify_core))]
321pub broadcast axiom fn axiom_str_literal_len<'a>(s: &'a str)
322    ensures
323        #[trigger] s@.len() == strslice_len(s),
324;
325
326#[cfg(not(verus_verify_core))]
327pub broadcast axiom fn axiom_str_literal_get_char<'a>(s: &'a str, i: int)
328    ensures
329        #[trigger] s@.index(i) == strslice_get_char(s, i),
330;
331
332#[cfg(all(not(feature = "alloc"), not(verus_verify_core)))]
333pub broadcast group group_string_axioms {
334    axiom_new_strlit_view_id,
335    axiom_str_literal_len,
336    axiom_str_literal_get_char,
337    is_ascii_spec_bytes,
338    is_ascii_concat,
339}
340
341#[cfg(all(feature = "alloc", not(verus_verify_core)))]
342pub broadcast group group_string_axioms {
343    axiom_new_strlit_view_id,
344    axiom_str_literal_len,
345    axiom_str_literal_get_char,
346    to_string_from_display_ensures_for_str,
347    is_ascii_spec_bytes,
348    is_ascii_concat,
349}
350
351#[cfg(all(feature = "alloc", not(verus_verify_core)))]
352impl View for String {
353    type V = Seq<char>;
354
355    uninterp spec fn view(&self) -> Seq<char>;
356}
357
358#[cfg(all(feature = "alloc", not(verus_verify_core)))]
359impl DeepView for String {
360    type V = Seq<char>;
361
362    open spec fn deep_view(&self) -> Seq<char> {
363        self.view()
364    }
365}
366
367#[cfg(verus_keep_ghost)]
368#[cfg(all(feature = "alloc", not(verus_verify_core)))]
369impl PartialEqSpecImpl for String {
370    open spec fn obeys_eq_spec() -> bool {
371        true
372    }
373
374    open spec fn eq_spec(&self, other: &String) -> bool {
375        self@ == other@
376    }
377}
378
379#[cfg(all(feature = "alloc", not(verus_verify_core)))]
380#[verifier::external_type_specification]
381#[verifier::external_body]
382pub struct ExString(String);
383
384#[cfg(all(feature = "alloc", not(verus_verify_core)))]
385pub open spec fn string_is_ascii(s: &String) -> bool {
386    is_ascii_chars(s@)
387}
388
389#[cfg(all(feature = "alloc", not(verus_verify_core)))]
390pub assume_specification<'a>[ String::as_str ](s: &'a String) -> (res: &'a str)
391    ensures
392        res@ == s@,
393    opens_invariants none
394    no_unwind
395;
396
397// same as above
398#[cfg(all(feature = "alloc", not(verus_verify_core)))]
399pub assume_specification<'a>[ <String as core::ops::Deref>::deref ](s: &'a String) -> (res: &'a str)
400    ensures
401        res@ == s@,
402;
403
404#[cfg(all(feature = "alloc", not(verus_verify_core)))]
405pub assume_specification[ <String as Clone>::clone ](s: &String) -> (res: String)
406    ensures
407        res == s,
408;
409
410#[cfg(all(feature = "alloc", not(verus_verify_core)))]
411pub assume_specification[ <String as PartialEq>::eq ](s: &String, other: &String) -> (res: bool)
412    ensures
413        res == (s@ == other@),
414;
415
416#[cfg(all(feature = "alloc", not(verus_verify_core)))]
417pub assume_specification[ String::new ]() -> (res: String)
418    ensures
419        res@ == Seq::<char>::empty(),
420;
421
422#[cfg(all(feature = "alloc", not(verus_verify_core)))]
423pub assume_specification[ String::push ](s: &mut String, c: char)
424    ensures
425        final(s)@ == old(s)@.push(c),
426;
427
428#[cfg(all(feature = "alloc", not(verus_verify_core)))]
429pub assume_specification[ String::pop ](s: &mut String) -> (res: Option<char>)
430    ensures
431        old(s)@.len() == 0 ==> res is None && final(s)@ == old(s)@,
432        old(s)@.len() > 0 ==> res == Some(old(s)@.last()) && final(s)@ == old(s)@.drop_last(),
433;
434
435#[cfg(all(feature = "alloc", not(verus_verify_core)))]
436pub assume_specification[ String::push_str ](s: &mut String, other: &str)
437    ensures
438        final(s)@ == old(s)@ + other@,
439;
440
441#[cfg(all(feature = "alloc", not(verus_verify_core)))]
442pub assume_specification[ String::is_empty ](s: &String) -> (res: bool)
443    ensures
444        res == (s@.len() == 0),
445    opens_invariants none
446    no_unwind
447;
448
449#[cfg(all(feature = "alloc", not(verus_verify_core)))]
450pub assume_specification[ String::clear ](s: &mut String)
451    ensures
452        final(s)@ == Seq::<char>::empty(),
453;
454
455#[cfg(all(feature = "alloc", not(verus_verify_core)))]
456pub assume_specification[ <String as core::default::Default>::default ]() -> (r: String)
457    ensures
458        r@ == Seq::<char>::empty(),
459;
460
461#[cfg(all(feature = "alloc", not(verus_verify_core)))]
462pub trait StringExecFnsIsAscii: Sized {
463    fn is_ascii(&self) -> bool;
464}
465
466#[cfg(all(feature = "alloc", not(verus_verify_core)))]
467impl StringExecFnsIsAscii for String {
468    #[inline(always)]
469    #[verifier::when_used_as_spec(string_is_ascii)]
470    fn is_ascii(&self) -> (ret: bool)
471        ensures
472            ret == string_is_ascii(self),
473    {
474        self.as_str().is_ascii()
475    }
476}
477
478#[cfg(all(feature = "alloc", not(verus_verify_core)))]
479#[verifier::external]
480pub trait StringExecFns: Sized {
481    fn from_str<'a>(s: &'a str) -> String;
482
483    fn append<'a, 'b>(&'a mut self, other: &'b str);
484
485    fn concat<'b>(self, other: &'b str) -> String;
486}
487
488#[cfg(all(feature = "alloc", not(verus_verify_core)))]
489impl StringExecFns for String {
490    #[verifier::external_body]
491    fn from_str<'a>(s: &'a str) -> (ret: String)
492        ensures
493            s@ == ret@,
494    {
495        s.to_string()
496    }
497
498    #[verifier::external_body]
499    fn append<'a, 'b>(&'a mut self, other: &'b str)
500        ensures
501            final(self)@ == old(self)@ + other@,
502    {
503        *self += other;
504    }
505
506    #[verifier::external_body]
507    fn concat<'b>(self, other: &'b str) -> (ret: String)
508        ensures
509            ret@ == self@ + other@,
510    {
511        self + other
512    }
513}
514
515// The `chars` method of a `str` returns an iterator of type `Chars`,
516// so we specify that type here.
517#[verifier::external_type_specification]
518#[verifier::external_body]
519#[cfg(all(feature = "alloc", not(verus_verify_core)))]
520pub struct ExChars<'a>(Chars<'a>);
521
522// To allow reasoning about the "contents" of the string iterator, without using
523// a prophecy, we need a function that gives us the underlying sequence of the original string.
524#[cfg(feature = "alloc")]
525pub uninterp spec fn into_iter_elts<'a>(i: Chars<'a>) -> Seq<char>;
526
527#[cfg(feature = "alloc")]
528pub assume_specification[ str::chars ](s: &str) -> (iter: Chars<'_>)
529    ensures
530        IteratorSpec::remaining(&iter) == s@,
531        // Every `char` takes at least one byte, and `s.len()` (a `usize`) counts bytes.
532        // This lets callers learn that the length fits in a `usize`
533        // (needed, e.g., by `enumerate` and `position`)
534        IteratorSpec::remaining(&iter).len() <= s.len(),
535        IteratorSpec::decrease(&iter) is Some,
536;
537
538#[cfg(verus_keep_ghost)]
539#[cfg(feature = "alloc")]
540impl<'a> super::std_specs::iter::IteratorSpecImpl for Chars<'a> {
541    open spec fn obeys_prophetic_iter_laws(&self) -> bool {
542        true
543    }
544
545    uninterp spec fn remaining(&self) -> Seq<Self::Item>;
546
547    #[verifier::prophetic]
548    open spec fn produces(&self) -> Seq<bool> {
549        Seq::new(IteratorSpec::remaining(self).len(), |i: int| true)
550    }
551
552    uninterp spec fn decrease(&self) -> Option<nat>;
553
554    open spec fn peek(&self, index: int) -> Option<Self::Item> {
555        if 0 <= index < into_iter_elts(*self).len() {
556            Some(into_iter_elts(*self)[index])
557        } else {
558            None
559        }
560    }
561}
562
563// There are various types you can use to index into a `str` to get a
564// slice, i.e., to implement `SliceIndex<str>`. Here we indicate, for
565// any such type (e.g., `Range<usize>`), whether an index of that
566// type is valid when applied to a given `&str`.
567#[cfg(all(verus_keep_ghost, not(verus_verify_core)))]
568pub open spec fn str_slice_in_bounds<R: RangeBoundsSpec<usize>>(range: &R, s: &str) -> bool {
569    &&& slice_range_valid(range, s.spec_bytes().len())
570    &&& is_char_boundary(s.spec_bytes(), slice_range_start(range))
571    &&& is_char_boundary(s.spec_bytes(), slice_range_end(range, s.spec_bytes().len()))
572}
573
574// There are various types you can use to index into a `str` to get a
575// slice, i.e., to implement `SliceIndex<str>`. Here we indicate, for
576// any such type (e.g., `Range<usize>`), the semantics of immutably
577// indexing a string.
578#[cfg(all(verus_keep_ghost, not(verus_verify_core)))]
579pub open spec fn str_slice_index_postcondition<R: RangeBoundsSpec<usize>>(
580    range: &R,
581    s: Seq<u8>,
582    r: Seq<u8>,
583) -> bool {
584    r == s[slice_range_start(range)..slice_range_end(range, s.len())]
585}
586
587// There are various types you can use to index into a `str` to get a
588// slice, i.e., to implement `SliceIndex<str>`. Here we indicate, for
589// any such type (e.g., `Range<usize>`), the semantics of mutably
590// indexing a string.
591#[cfg(all(verus_keep_ghost, not(verus_verify_core)))]
592pub open spec fn str_slice_index_mut_postcondition<R: RangeBoundsSpec<usize>>(
593    range: &R,
594    old_s: Seq<u8>,
595    final_s: Seq<u8>,
596    r: Seq<u8>,
597    final_r: Seq<u8>,
598) -> bool {
599    let start = slice_range_start(range);
600    let end = slice_range_end(range, old_s.len());
601    &&& r == old_s[start..end]
602    &&& final_s.len() == old_s.len()
603    &&& final_s[..start] == old_s[..start]
604    &&& final_s[start..end] == final_r
605    &&& final_s[end..old_s.len()] == old_s[end..]
606}
607
608#[cfg(all(verus_keep_ghost, not(verus_verify_core)))]
609impl super::slice::SliceIndexSpecImpl<str> for (Bound<usize>, Bound<usize>) {
610    open spec fn in_bounds(&self, s: &str) -> bool {
611        str_slice_in_bounds(self, s)
612    }
613
614    open spec fn index_postcondition(&self, s: &str, r: &str) -> bool {
615        str_slice_index_postcondition(self, s.spec_bytes(), r.spec_bytes())
616    }
617
618    open spec fn index_mut_postcondition(
619        &self,
620        old_slice: &str,
621        final_slice: &str,
622        immediate_output: &str,
623        final_output: &str,
624    ) -> bool {
625        str_slice_index_mut_postcondition(
626            self,
627            old_slice.spec_bytes(),
628            final_slice.spec_bytes(),
629            immediate_output.spec_bytes(),
630            final_output.spec_bytes(),
631        )
632    }
633}
634
635#[cfg(all(verus_keep_ghost, not(verus_verify_core)))]
636pub assume_specification[ <(Bound<usize>, Bound<usize>) as SliceIndex<str>>::get ](
637    i: (Bound<usize>, Bound<usize>),
638    s: &str,
639) -> (r: Option<&str>)
640;
641
642#[cfg(all(verus_keep_ghost, not(verus_verify_core)))]
643pub assume_specification[ <(Bound<usize>, Bound<usize>) as SliceIndex<str>>::index ](
644    i: (Bound<usize>, Bound<usize>),
645    s: &str,
646) -> (r: &str)
647;
648
649#[cfg(all(verus_keep_ghost, not(verus_verify_core)))]
650pub assume_specification[ <(Bound<usize>, Bound<usize>) as SliceIndex<str>>::get_mut ](
651    i: (Bound<usize>, Bound<usize>),
652    s: &mut str,
653) -> (r: Option<&mut str>)
654;
655
656#[cfg(all(verus_keep_ghost, not(verus_verify_core)))]
657pub assume_specification[ <(Bound<usize>, Bound<usize>) as SliceIndex<str>>::index_mut ](
658    i: (Bound<usize>, Bound<usize>),
659    s: &mut str,
660) -> (r: &mut str)
661;
662
663#[cfg(all(verus_keep_ghost, not(verus_verify_core)))]
664impl super::slice::SliceIndexSpecImpl<str> for Range<usize> {
665    open spec fn in_bounds(&self, s: &str) -> bool {
666        str_slice_in_bounds(self, s)
667    }
668
669    open spec fn index_postcondition(&self, s: &str, r: &str) -> bool {
670        str_slice_index_postcondition(self, s.spec_bytes(), r.spec_bytes())
671    }
672
673    open spec fn index_mut_postcondition(
674        &self,
675        old_slice: &str,
676        final_slice: &str,
677        immediate_output: &str,
678        final_output: &str,
679    ) -> bool {
680        str_slice_index_mut_postcondition(
681            self,
682            old_slice.spec_bytes(),
683            final_slice.spec_bytes(),
684            immediate_output.spec_bytes(),
685            final_output.spec_bytes(),
686        )
687    }
688}
689
690#[cfg(all(verus_keep_ghost, not(verus_verify_core)))]
691pub assume_specification[ <Range<usize> as SliceIndex<str>>::get ](i: Range<usize>, s: &str) -> (r:
692    Option<&<Range<usize> as SliceIndex<str>>::Output>)
693;
694
695#[cfg(all(verus_keep_ghost, not(verus_verify_core)))]
696pub assume_specification[ <Range<usize> as SliceIndex<str>>::index ](
697    i: Range<usize>,
698    s: &str,
699) -> (r: &<Range<usize> as SliceIndex<str>>::Output)
700;
701
702#[cfg(all(verus_keep_ghost, not(verus_verify_core)))]
703pub assume_specification[ <Range<usize> as SliceIndex<str>>::get_mut ](
704    i: Range<usize>,
705    s: &mut str,
706) -> (r: Option<&mut <Range<usize> as SliceIndex<str>>::Output>)
707;
708
709#[cfg(all(verus_keep_ghost, not(verus_verify_core)))]
710pub assume_specification[ <Range<usize> as SliceIndex<str>>::index_mut ](
711    i: Range<usize>,
712    s: &mut str,
713) -> (r: &mut <Range<usize> as SliceIndex<str>>::Output)
714;
715
716#[cfg(all(verus_keep_ghost, not(verus_verify_core)))]
717impl super::slice::SliceIndexSpecImpl<str> for RangeFrom<usize> {
718    open spec fn in_bounds(&self, s: &str) -> bool {
719        str_slice_in_bounds(self, s)
720    }
721
722    open spec fn index_postcondition(&self, s: &str, r: &str) -> bool {
723        str_slice_index_postcondition(self, s.spec_bytes(), r.spec_bytes())
724    }
725
726    open spec fn index_mut_postcondition(
727        &self,
728        old_slice: &str,
729        final_slice: &str,
730        immediate_output: &str,
731        final_output: &str,
732    ) -> bool {
733        str_slice_index_mut_postcondition(
734            self,
735            old_slice.spec_bytes(),
736            final_slice.spec_bytes(),
737            immediate_output.spec_bytes(),
738            final_output.spec_bytes(),
739        )
740    }
741}
742
743#[cfg(all(verus_keep_ghost, not(verus_verify_core)))]
744pub assume_specification[ <RangeFrom<usize> as SliceIndex<str>>::get ](
745    i: RangeFrom<usize>,
746    s: &str,
747) -> (r: Option<&<RangeFrom<usize> as SliceIndex<str>>::Output>)
748;
749
750#[cfg(all(verus_keep_ghost, not(verus_verify_core)))]
751pub assume_specification[ <RangeFrom<usize> as SliceIndex<str>>::index ](
752    i: RangeFrom<usize>,
753    s: &str,
754) -> (r: &<RangeFrom<usize> as SliceIndex<str>>::Output)
755;
756
757#[cfg(all(verus_keep_ghost, not(verus_verify_core)))]
758pub assume_specification[ <RangeFrom<usize> as SliceIndex<str>>::get_mut ](
759    i: RangeFrom<usize>,
760    s: &mut str,
761) -> (r: Option<&mut <RangeFrom<usize> as SliceIndex<str>>::Output>)
762;
763
764#[cfg(all(verus_keep_ghost, not(verus_verify_core)))]
765pub assume_specification[ <RangeFrom<usize> as SliceIndex<str>>::index_mut ](
766    i: RangeFrom<usize>,
767    s: &mut str,
768) -> (r: &mut <RangeFrom<usize> as SliceIndex<str>>::Output)
769;
770
771#[cfg(all(verus_keep_ghost, not(verus_verify_core)))]
772impl super::slice::SliceIndexSpecImpl<str> for RangeFull {
773    open spec fn in_bounds(&self, s: &str) -> bool {
774        str_slice_in_bounds(self, s)
775    }
776
777    open spec fn index_postcondition(&self, s: &str, r: &str) -> bool {
778        str_slice_index_postcondition(self, s.spec_bytes(), r.spec_bytes())
779    }
780
781    open spec fn index_mut_postcondition(
782        &self,
783        old_slice: &str,
784        final_slice: &str,
785        immediate_output: &str,
786        final_output: &str,
787    ) -> bool {
788        str_slice_index_mut_postcondition(
789            self,
790            old_slice.spec_bytes(),
791            final_slice.spec_bytes(),
792            immediate_output.spec_bytes(),
793            final_output.spec_bytes(),
794        )
795    }
796}
797
798#[cfg(all(verus_keep_ghost, not(verus_verify_core)))]
799pub assume_specification[ <RangeFull as SliceIndex<str>>::get ](i: RangeFull, s: &str) -> (r:
800    Option<&<RangeFull as SliceIndex<str>>::Output>)
801;
802
803#[cfg(all(verus_keep_ghost, not(verus_verify_core)))]
804pub assume_specification[ <RangeFull as SliceIndex<str>>::index ](i: RangeFull, s: &str) -> (r:
805    &<RangeFull as SliceIndex<str>>::Output)
806;
807
808#[cfg(all(verus_keep_ghost, not(verus_verify_core)))]
809pub assume_specification[ <RangeFull as SliceIndex<str>>::get_mut ](
810    i: RangeFull,
811    s: &mut str,
812) -> (r: Option<&mut <RangeFull as SliceIndex<str>>::Output>)
813;
814
815#[cfg(all(verus_keep_ghost, not(verus_verify_core)))]
816pub assume_specification[ <RangeFull as SliceIndex<str>>::index_mut ](
817    i: RangeFull,
818    s: &mut str,
819) -> (r: &mut <RangeFull as SliceIndex<str>>::Output)
820;
821
822// Indexing with an empty range doesn't verify, since std doesn't say whether it
823// panics. `get` still verifies and returns `None` or an empty string.
824#[cfg(all(verus_keep_ghost, not(verus_verify_core)))]
825pub uninterp spec fn spec_range_inclusive_unspecified_str_in_bounds(
826    range: &RangeInclusive<usize>,
827    s: &str,
828) -> bool;
829
830#[cfg(all(verus_keep_ghost, not(verus_verify_core)))]
831impl super::slice::SliceIndexSpecImpl<str> for RangeInclusive<usize> {
832    open spec fn in_bounds(&self, s: &str) -> bool {
833        if spec_range_inclusive_is_empty(self) {
834            spec_range_inclusive_unspecified_str_in_bounds(self, s)
835        } else {
836            str_slice_in_bounds(self, s)
837        }
838    }
839
840    open spec fn index_postcondition(&self, s: &str, r: &str) -> bool {
841        if spec_range_inclusive_is_empty(self) {
842            r.spec_bytes().len() == 0
843        } else {
844            str_slice_index_postcondition(self, s.spec_bytes(), r.spec_bytes())
845        }
846    }
847
848    open spec fn index_mut_postcondition(
849        &self,
850        old_slice: &str,
851        final_slice: &str,
852        immediate_output: &str,
853        final_output: &str,
854    ) -> bool {
855        if spec_range_inclusive_is_empty(self) {
856            &&& immediate_output.spec_bytes().len() == 0
857            &&& final_slice.spec_bytes() == old_slice.spec_bytes()
858        } else {
859            str_slice_index_mut_postcondition(
860                self,
861                old_slice.spec_bytes(),
862                final_slice.spec_bytes(),
863                immediate_output.spec_bytes(),
864                final_output.spec_bytes(),
865            )
866        }
867    }
868}
869
870#[cfg(all(verus_keep_ghost, not(verus_verify_core)))]
871pub assume_specification[ <RangeInclusive<usize> as SliceIndex<str>>::get ](
872    i: RangeInclusive<usize>,
873    s: &str,
874) -> (r: Option<&<RangeInclusive<usize> as SliceIndex<str>>::Output>)
875;
876
877#[cfg(all(verus_keep_ghost, not(verus_verify_core)))]
878pub assume_specification[ <RangeInclusive<usize> as SliceIndex<str>>::index ](
879    i: RangeInclusive<usize>,
880    s: &str,
881) -> (r: &<RangeInclusive<usize> as SliceIndex<str>>::Output)
882;
883
884#[cfg(all(verus_keep_ghost, not(verus_verify_core)))]
885pub assume_specification[ <RangeInclusive<usize> as SliceIndex<str>>::get_mut ](
886    i: RangeInclusive<usize>,
887    s: &mut str,
888) -> (r: Option<&mut <RangeInclusive<usize> as SliceIndex<str>>::Output>)
889;
890
891#[cfg(all(verus_keep_ghost, not(verus_verify_core)))]
892pub assume_specification[ <RangeInclusive<usize> as SliceIndex<str>>::index_mut ](
893    i: RangeInclusive<usize>,
894    s: &mut str,
895) -> (r: &mut <RangeInclusive<usize> as SliceIndex<str>>::Output)
896;
897
898#[cfg(all(verus_keep_ghost, not(verus_verify_core)))]
899impl super::slice::SliceIndexSpecImpl<str> for RangeTo<usize> {
900    open spec fn in_bounds(&self, s: &str) -> bool {
901        str_slice_in_bounds(self, s)
902    }
903
904    open spec fn index_postcondition(&self, s: &str, r: &str) -> bool {
905        str_slice_index_postcondition(self, s.spec_bytes(), r.spec_bytes())
906    }
907
908    open spec fn index_mut_postcondition(
909        &self,
910        old_slice: &str,
911        final_slice: &str,
912        immediate_output: &str,
913        final_output: &str,
914    ) -> bool {
915        str_slice_index_mut_postcondition(
916            self,
917            old_slice.spec_bytes(),
918            final_slice.spec_bytes(),
919            immediate_output.spec_bytes(),
920            final_output.spec_bytes(),
921        )
922    }
923}
924
925#[cfg(all(verus_keep_ghost, not(verus_verify_core)))]
926pub assume_specification[ <RangeTo<usize> as SliceIndex<str>>::get ](
927    i: RangeTo<usize>,
928    s: &str,
929) -> (r: Option<&<RangeTo<usize> as SliceIndex<str>>::Output>)
930;
931
932#[cfg(all(verus_keep_ghost, not(verus_verify_core)))]
933pub assume_specification[ <RangeTo<usize> as SliceIndex<str>>::index ](
934    i: RangeTo<usize>,
935    s: &str,
936) -> (r: &<RangeTo<usize> as SliceIndex<str>>::Output)
937;
938
939#[cfg(all(verus_keep_ghost, not(verus_verify_core)))]
940pub assume_specification[ <RangeTo<usize> as SliceIndex<str>>::get_mut ](
941    i: RangeTo<usize>,
942    s: &mut str,
943) -> (r: Option<&mut <RangeTo<usize> as SliceIndex<str>>::Output>)
944;
945
946#[cfg(all(verus_keep_ghost, not(verus_verify_core)))]
947pub assume_specification[ <RangeTo<usize> as SliceIndex<str>>::index_mut ](
948    i: RangeTo<usize>,
949    s: &mut str,
950) -> (r: &mut <RangeTo<usize> as SliceIndex<str>>::Output)
951;
952
953#[cfg(all(verus_keep_ghost, not(verus_verify_core)))]
954impl super::slice::SliceIndexSpecImpl<str> for RangeToInclusive<usize> {
955    open spec fn in_bounds(&self, s: &str) -> bool {
956        str_slice_in_bounds(self, s)
957    }
958
959    open spec fn index_postcondition(&self, s: &str, r: &str) -> bool {
960        str_slice_index_postcondition(self, s.spec_bytes(), r.spec_bytes())
961    }
962
963    open spec fn index_mut_postcondition(
964        &self,
965        old_slice: &str,
966        final_slice: &str,
967        immediate_output: &str,
968        final_output: &str,
969    ) -> bool {
970        str_slice_index_mut_postcondition(
971            self,
972            old_slice.spec_bytes(),
973            final_slice.spec_bytes(),
974            immediate_output.spec_bytes(),
975            final_output.spec_bytes(),
976        )
977    }
978}
979
980#[cfg(all(verus_keep_ghost, not(verus_verify_core)))]
981pub assume_specification[ <RangeToInclusive<usize> as SliceIndex<str>>::get ](
982    i: RangeToInclusive<usize>,
983    s: &str,
984) -> (r: Option<&<RangeToInclusive<usize> as SliceIndex<str>>::Output>)
985;
986
987#[cfg(all(verus_keep_ghost, not(verus_verify_core)))]
988pub assume_specification[ <RangeToInclusive<usize> as SliceIndex<str>>::index ](
989    i: RangeToInclusive<usize>,
990    s: &str,
991) -> (r: &<RangeToInclusive<usize> as SliceIndex<str>>::Output)
992;
993
994#[cfg(all(verus_keep_ghost, not(verus_verify_core)))]
995pub assume_specification[ <RangeToInclusive<usize> as SliceIndex<str>>::get_mut ](
996    i: RangeToInclusive<usize>,
997    s: &mut str,
998) -> (r: Option<&mut <RangeToInclusive<usize> as SliceIndex<str>>::Output>)
999;
1000
1001#[cfg(all(verus_keep_ghost, not(verus_verify_core)))]
1002pub assume_specification[ <RangeToInclusive<usize> as SliceIndex<str>>::index_mut ](
1003    i: RangeToInclusive<usize>,
1004    s: &mut str,
1005) -> (r: &mut <RangeToInclusive<usize> as SliceIndex<str>>::Output)
1006;
1007
1008// `<str as ops::Index<I>>::index(&self, index: I)` just invokes
1009// `index.index(self)`. So we likewise delegate determining the meaning
1010// of the string-index operation to that of the index type.
1011#[cfg(all(verus_keep_ghost, not(verus_verify_core)))]
1012impl<I: SliceIndexSpec<str>> super::std_specs::core::IndexSpecImpl<I> for str {
1013    open spec fn index_req(&self, index: &I) -> bool {
1014        index.in_bounds(self)
1015    }
1016}
1017
1018pub use super::view::View;
1019
1020} // verus!