escafers/
lib.rs

1// MIT License
2//
3// Copyright (c) 2021 Ferhat Geçdoğan All Rights Reserved.
4// Distributed under the terms of the MIT License.
5//
6//
7// escafers - parser & generator for escape sequences (external whitespace sequence support (\w))
8//
9// github.com/ferhatgec/escafers
10// github.com/ferhatgec/escafe
11// github.com/ferhatgec/escafe.py
12
13pub mod escafe {
14    pub fn run(data: &String, use_unsupported_seq: bool) -> String {
15        let mut __data = String::new();
16        let mut collect= String::new();
17        let (mut is_escape, mut is_ansi) = (false, false);
18
19        for ch in data.chars() {
20            if !is_ansi && !collect.is_empty() {
21                //match collect.as_str() {
22                if collect == "x1b" { // => {
23                    __data.push_str("\x1b");
24                } //, _ => {}
25
26                collect.clear();
27            }
28
29            if is_escape {
30                match ch {
31                    '\'' => {
32                        __data.push('\''); is_escape = false;
33                    },
34                    '"' => {
35                        __data.push('\"'); is_escape = false;
36                    },
37                    '\\' => {
38                        __data.push('\\'); is_escape = false;
39                    },
40                    'a' => {
41                        if use_unsupported_seq {
42                            __data.push_str("\\a");
43                        } is_escape = false;
44                    },
45                    'b' => {
46                        if is_ansi {
47                            collect.push('b');
48                            is_ansi = false;
49                        } else {
50                            if use_unsupported_seq {
51                                __data.push_str("\\b");
52                            }
53                        } is_escape = false;
54                    },
55                    'f' => {
56                        if use_unsupported_seq {
57                            __data.push_str("\\f");
58                        } is_escape = false;
59                    },
60                    'n' => {
61                        __data.push('\n'); is_escape = false;
62                    },
63                    'r' => {
64                        __data.push('\r'); is_escape = false;
65                    },
66                    't' => {
67                        __data.push('\t'); is_escape = false;
68                    },
69                    'v' => {
70                        if use_unsupported_seq {
71                            __data.push_str("\\v");
72                        } is_escape = false;
73                    },
74                    'w' => {
75                        __data.push(' '); is_escape = false;
76                    },
77                    'x' => {
78                        is_ansi = true; collect.push('x');
79                    },
80                    '1' => {
81                        if is_ansi {
82                            collect.push('1');
83                        }
84                    }
85                    _ => {
86                        is_escape = false;
87                    }
88                } continue;
89            }
90
91            if ch == '\\' {
92                is_escape = true; continue;
93            } __data.push(ch);
94        }
95
96        __data
97    }
98}
99
100#[cfg(test)]
101mod tests {
102    #[test]
103    fn ok() {
104        let data = String::from("escafe\\wis\\winteresting\\n\\x1b[0;33mtest\\x1b[0m\\n\\\\");
105        //                                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
106        //                                raw data taken from github.com/ferhatgec/escafe/
107
108        println!("{}", super::escafe::run(&data, false));
109    }
110}