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