Skip to main content

rustc_builtin_macros/
format_foreign.rs

1pub(crate) mod printf;
2
3pub(crate) mod shell;
4
5#[derive(#[automatically_derived]
#[doc(hidden)]
unsafe impl<'a> ::core::clone::TrivialClone for StrCursor<'a> { }
#[automatically_derived]
impl<'a> ::core::clone::Clone for StrCursor<'a> {
    #[inline]
    fn clone(&self) -> StrCursor<'a> {
        let _: ::core::clone::AssertParamIsClone<&'a str>;
        let _: ::core::clone::AssertParamIsClone<usize>;
        *self
    }
}Clone, #[automatically_derived]
impl<'a> ::core::marker::Copy for StrCursor<'a> { }Copy)]
6struct StrCursor<'a> {
7    s: &'a str,
8    pub at: usize,
9}
10
11impl<'a> StrCursor<'a> {
12    fn new_at(s: &'a str, at: usize) -> StrCursor<'a> {
13        StrCursor { s, at }
14    }
15
16    fn at_next_cp(mut self) -> Option<StrCursor<'a>> {
17        match self.try_seek_right_cp() {
18            true => Some(self),
19            false => None,
20        }
21    }
22
23    fn next_cp(mut self) -> Option<(char, StrCursor<'a>)> {
24        let cp = self.cp_after()?;
25        self.seek_right(cp.len_utf8());
26        Some((cp, self))
27    }
28
29    fn slice_before(&self) -> &'a str {
30        &self.s[0..self.at]
31    }
32
33    fn slice_after(&self) -> &'a str {
34        &self.s[self.at..]
35    }
36
37    fn slice_between(&self, until: StrCursor<'a>) -> Option<&'a str> {
38        if !str_eq_literal(self.s, until.s) {
39            None
40        } else {
41            use std::cmp::{max, min};
42            let beg = min(self.at, until.at);
43            let end = max(self.at, until.at);
44            Some(&self.s[beg..end])
45        }
46    }
47
48    fn cp_after(&self) -> Option<char> {
49        self.slice_after().chars().next()
50    }
51
52    fn try_seek_right_cp(&mut self) -> bool {
53        match self.slice_after().chars().next() {
54            Some(c) => {
55                self.at += c.len_utf8();
56                true
57            }
58            None => false,
59        }
60    }
61
62    fn seek_right(&mut self, bytes: usize) {
63        self.at += bytes;
64    }
65}
66
67impl std::fmt::Debug for StrCursor<'_> {
68    fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
69        fmt.write_fmt(format_args!("StrCursor({0:?} | {1:?})", self.slice_before(),
        self.slice_after()))write!(fmt, "StrCursor({:?} | {:?})", self.slice_before(), self.slice_after())
70    }
71}
72
73fn str_eq_literal(a: &str, b: &str) -> bool {
74    a.as_bytes().as_ptr() == b.as_bytes().as_ptr() && a.len() == b.len()
75}