Replacinator

Struct Replacinator 

Source
pub struct Replacinator<'a> { /* private fields */ }

Implementations§

Source§

impl<'a> Replacinator<'a>

Source

pub fn new_in<R>( value: &'a mut str, with: impl FnMut(&mut Replacinator<'a>) -> R, ) -> R

Examples found in repository?
examples/json_parse.rs (line 17)
10fn main() {
11    let mut buf = String::new();
12    let _ = File::open("examples/test.json")
13        .unwrap()
14        .read_to_string(&mut buf)
15        .unwrap();
16
17    let json_values = Replacinator::new_in(&mut buf, parse_json_array);
18    dbg!(json_values);
19    println!("Buffer is now: {}", buf);
20}
Source

pub unsafe fn construct(from: &'a mut str) -> Self

Source

pub fn remainder(&self) -> &str

Source

pub fn get_begin(&mut self) -> &'a mut str

Examples found in repository?
examples/json_parse.rs (line 29)
22fn parse_json_array<'a>(src: &mut Replacinator<'a>) -> JsonArray<'a> {
23    let mut values = Vec::new();
24    assert_eq!(src.skip_char(), Some('['));
25    loop {
26        match src.skip_char() {
27            Some('"') => {
28                // Reset the replacinator to the beginning of this string
29                let _ = src.get_begin();
30                loop {
31                    match src
32                        .read_char()
33                        .expect("JSON value should not end in the middle of a string")
34                    {
35                        '\\' => match src
36                            .read_char()
37                            .expect("JSON value should not end in the middle of an escape sequence")
38                        {
39                            '"' => src.write_char('"'),
40                            '\\' => src.write_char('\\'),
41                            '/' => src.write_char('/'),
42                            'b' => src.write_char('\x08'),
43                            'f' => src.write_char('\x0c'),
44                            'n' => src.write_char('\n'),
45                            'r' => src.write_char('\r'),
46                            't' => src.write_char('\t'),
47                            'u' => {
48                                let mut res = 0;
49                                for _ in 0..4 {
50                                    let v = src
51                                        .read_char()
52                                        .expect("String ended in unicode")
53                                        .to_digit(16)
54                                        .expect("Invalid hex digit in escape");
55                                    res = res * 16 + v;
56                                }
57                                src.write_char(
58                                    std::char::from_u32(res).expect("Valid character code"),
59                                )
60                            }
61                            other => panic!("Invalid escape {:?}", other),
62                        },
63                        '"' => {
64                            values.push(src.get_begin());
65                            src.write_char('"');
66                            break;
67                        }
68                        other => src.write_char(other),
69                    }
70                }
71            }
72            Some(']') => break,
73            Some(' ') | Some('\n') | Some('\t') => (),
74            res => panic!(
75                r#"JSON array should be continued with '"' or ']', got {:?}"#,
76                res
77            ),
78        }
79    }
80    JsonArray { values }
81}
Source

pub fn skip_char(&mut self) -> Option<char>

Examples found in repository?
examples/json_parse.rs (line 24)
22fn parse_json_array<'a>(src: &mut Replacinator<'a>) -> JsonArray<'a> {
23    let mut values = Vec::new();
24    assert_eq!(src.skip_char(), Some('['));
25    loop {
26        match src.skip_char() {
27            Some('"') => {
28                // Reset the replacinator to the beginning of this string
29                let _ = src.get_begin();
30                loop {
31                    match src
32                        .read_char()
33                        .expect("JSON value should not end in the middle of a string")
34                    {
35                        '\\' => match src
36                            .read_char()
37                            .expect("JSON value should not end in the middle of an escape sequence")
38                        {
39                            '"' => src.write_char('"'),
40                            '\\' => src.write_char('\\'),
41                            '/' => src.write_char('/'),
42                            'b' => src.write_char('\x08'),
43                            'f' => src.write_char('\x0c'),
44                            'n' => src.write_char('\n'),
45                            'r' => src.write_char('\r'),
46                            't' => src.write_char('\t'),
47                            'u' => {
48                                let mut res = 0;
49                                for _ in 0..4 {
50                                    let v = src
51                                        .read_char()
52                                        .expect("String ended in unicode")
53                                        .to_digit(16)
54                                        .expect("Invalid hex digit in escape");
55                                    res = res * 16 + v;
56                                }
57                                src.write_char(
58                                    std::char::from_u32(res).expect("Valid character code"),
59                                )
60                            }
61                            other => panic!("Invalid escape {:?}", other),
62                        },
63                        '"' => {
64                            values.push(src.get_begin());
65                            src.write_char('"');
66                            break;
67                        }
68                        other => src.write_char(other),
69                    }
70                }
71            }
72            Some(']') => break,
73            Some(' ') | Some('\n') | Some('\t') => (),
74            res => panic!(
75                r#"JSON array should be continued with '"' or ']', got {:?}"#,
76                res
77            ),
78        }
79    }
80    JsonArray { values }
81}
Source

pub fn peek(&self) -> Option<char>

Source

pub fn read_char(&mut self) -> Option<char>

Examples found in repository?
examples/json_parse.rs (line 32)
22fn parse_json_array<'a>(src: &mut Replacinator<'a>) -> JsonArray<'a> {
23    let mut values = Vec::new();
24    assert_eq!(src.skip_char(), Some('['));
25    loop {
26        match src.skip_char() {
27            Some('"') => {
28                // Reset the replacinator to the beginning of this string
29                let _ = src.get_begin();
30                loop {
31                    match src
32                        .read_char()
33                        .expect("JSON value should not end in the middle of a string")
34                    {
35                        '\\' => match src
36                            .read_char()
37                            .expect("JSON value should not end in the middle of an escape sequence")
38                        {
39                            '"' => src.write_char('"'),
40                            '\\' => src.write_char('\\'),
41                            '/' => src.write_char('/'),
42                            'b' => src.write_char('\x08'),
43                            'f' => src.write_char('\x0c'),
44                            'n' => src.write_char('\n'),
45                            'r' => src.write_char('\r'),
46                            't' => src.write_char('\t'),
47                            'u' => {
48                                let mut res = 0;
49                                for _ in 0..4 {
50                                    let v = src
51                                        .read_char()
52                                        .expect("String ended in unicode")
53                                        .to_digit(16)
54                                        .expect("Invalid hex digit in escape");
55                                    res = res * 16 + v;
56                                }
57                                src.write_char(
58                                    std::char::from_u32(res).expect("Valid character code"),
59                                )
60                            }
61                            other => panic!("Invalid escape {:?}", other),
62                        },
63                        '"' => {
64                            values.push(src.get_begin());
65                            src.write_char('"');
66                            break;
67                        }
68                        other => src.write_char(other),
69                    }
70                }
71            }
72            Some(']') => break,
73            Some(' ') | Some('\n') | Some('\t') => (),
74            res => panic!(
75                r#"JSON array should be continued with '"' or ']', got {:?}"#,
76                res
77            ),
78        }
79    }
80    JsonArray { values }
81}
Source

pub fn write_char(&mut self, c: char)

Examples found in repository?
examples/json_parse.rs (line 39)
22fn parse_json_array<'a>(src: &mut Replacinator<'a>) -> JsonArray<'a> {
23    let mut values = Vec::new();
24    assert_eq!(src.skip_char(), Some('['));
25    loop {
26        match src.skip_char() {
27            Some('"') => {
28                // Reset the replacinator to the beginning of this string
29                let _ = src.get_begin();
30                loop {
31                    match src
32                        .read_char()
33                        .expect("JSON value should not end in the middle of a string")
34                    {
35                        '\\' => match src
36                            .read_char()
37                            .expect("JSON value should not end in the middle of an escape sequence")
38                        {
39                            '"' => src.write_char('"'),
40                            '\\' => src.write_char('\\'),
41                            '/' => src.write_char('/'),
42                            'b' => src.write_char('\x08'),
43                            'f' => src.write_char('\x0c'),
44                            'n' => src.write_char('\n'),
45                            'r' => src.write_char('\r'),
46                            't' => src.write_char('\t'),
47                            'u' => {
48                                let mut res = 0;
49                                for _ in 0..4 {
50                                    let v = src
51                                        .read_char()
52                                        .expect("String ended in unicode")
53                                        .to_digit(16)
54                                        .expect("Invalid hex digit in escape");
55                                    res = res * 16 + v;
56                                }
57                                src.write_char(
58                                    std::char::from_u32(res).expect("Valid character code"),
59                                )
60                            }
61                            other => panic!("Invalid escape {:?}", other),
62                        },
63                        '"' => {
64                            values.push(src.get_begin());
65                            src.write_char('"');
66                            break;
67                        }
68                        other => src.write_char(other),
69                    }
70                }
71            }
72            Some(']') => break,
73            Some(' ') | Some('\n') | Some('\t') => (),
74            res => panic!(
75                r#"JSON array should be continued with '"' or ']', got {:?}"#,
76                res
77            ),
78        }
79    }
80    JsonArray { values }
81}
Source

pub fn synchronise(&mut self)

Source

pub fn no_mans_land(&mut self) -> &mut [u8]

Trait Implementations§

Source§

impl<'a> Drop for Replacinator<'a>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<'a> Freeze for Replacinator<'a>

§

impl<'a> RefUnwindSafe for Replacinator<'a>

§

impl<'a> !Send for Replacinator<'a>

§

impl<'a> !Sync for Replacinator<'a>

§

impl<'a> Unpin for Replacinator<'a>

§

impl<'a> !UnwindSafe for Replacinator<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.