| 1 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::ffirust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/::OsStrrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/OsStr#; |
| 2 | |
| 3 | use bstrrust-analyzer cargo bstr 1.12.0 crate/::{ByteSlicerust-analyzer cargo bstr 1.12.0 ext_slice/ByteSlice#, ByteVecrust-analyzer cargo bstr 1.12.0 ext_vec/ByteVec#}; |
| 4 | |
| 5 | /// Escapes arbitrary bytes into a human readable string. |
| 6 | /// |
| 7 | /// This converts `\t`, `\r` and `\n` into their escaped forms. It also |
| 8 | /// converts the non-printable subset of ASCII in addition to invalid UTF-8 |
| 9 | /// bytes to hexadecimal escape sequences. Everything else is left as is. |
| 10 | /// |
| 11 | /// The dual of this routine is [`unescape`]. |
| 12 | /// |
| 13 | /// # Example |
| 14 | /// |
| 15 | /// This example shows how to convert a byte string that contains a `\n` and |
| 16 | /// invalid UTF-8 bytes into a `String`. |
| 17 | /// |
| 18 | /// Pay special attention to the use of raw strings. That is, `r"\n"` is |
| 19 | /// equivalent to `"\\n"`. |
| 20 | /// |
| 21 | /// ``` |
| 22 | /// use grep_cli::escape; |
| 23 | /// |
| 24 | /// assert_eq!(r"foo\nbar\xFFbaz", escape(b"foo\nbar\xFFbaz")); |
| 25 | /// ``` |
| 26 | pub fn escaperust-analyzer cargo grep-cli 0.1.12 escape/escape().(bytes: &[u8]) -> Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String# { |
| 27 | bytes.escape_bytesrust-analyzer cargo bstr 1.12.0 ext_slice/ByteSlice#escape_bytes().().to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().() |
| 28 | } |
| 29 | |
| 30 | /// Escapes an OS string into a human readable string. |
| 31 | /// |
| 32 | /// This is like [`escape`], but accepts an OS string. |
| 33 | pub fn escape_osrust-analyzer cargo grep-cli 0.1.12 escape/escape_os().(string: &OsStrrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/OsStr#) -> Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String# { |
| 34 | escaperust-analyzer cargo grep-cli 0.1.12 escape/escape().(Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::from_os_str_lossyrust-analyzer cargo bstr 1.12.0 ext_vec/ByteVec#from_os_str_lossy().(string).as_bytesrust-analyzer cargo bstr 1.12.0 ext_slice/impl#[`[u8]`][ByteSlice]as_bytes().()) |
| 35 | } |
| 36 | |
| 37 | /// Unescapes a string. |
| 38 | /// |
| 39 | /// It supports a limited set of escape sequences: |
| 40 | /// |
| 41 | /// * `\t`, `\r` and `\n` are mapped to their corresponding ASCII bytes. |
| 42 | /// * `\xZZ` hexadecimal escapes are mapped to their byte. |
| 43 | /// |
| 44 | /// Everything else is left as is, including non-hexadecimal escapes like |
| 45 | /// `\xGG`. |
| 46 | /// |
| 47 | /// This is useful when it is desirable for a command line argument to be |
| 48 | /// capable of specifying arbitrary bytes or otherwise make it easier to |
| 49 | /// specify non-printable characters. |
| 50 | /// |
| 51 | /// The dual of this routine is [`escape`]. |
| 52 | /// |
| 53 | /// # Example |
| 54 | /// |
| 55 | /// This example shows how to convert an escaped string (which is valid UTF-8) |
| 56 | /// into a corresponding sequence of bytes. Each escape sequence is mapped to |
| 57 | /// its bytes, which may include invalid UTF-8. |
| 58 | /// |
| 59 | /// Pay special attention to the use of raw strings. That is, `r"\n"` is |
| 60 | /// equivalent to `"\\n"`. |
| 61 | /// |
| 62 | /// ``` |
| 63 | /// use grep_cli::unescape; |
| 64 | /// |
| 65 | /// assert_eq!(&b"foo\nbar\xFFbaz"[..], &*unescape(r"foo\nbar\xFFbaz")); |
| 66 | /// ``` |
| 67 | pub fn unescaperust-analyzer cargo grep-cli 0.1.12 escape/unescape().(s: &str) -> Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8> { |
| 68 | Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::unescape_bytesrust-analyzer cargo bstr 1.12.0 ext_vec/ByteVec#unescape_bytes().(s) |
| 69 | } |
| 70 | |
| 71 | /// Unescapes an OS string. |
| 72 | /// |
| 73 | /// This is like [`unescape`], but accepts an OS string. |
| 74 | /// |
| 75 | /// Note that this first lossily decodes the given OS string as UTF-8. That |
| 76 | /// is, an escaped string (the thing given) should be valid UTF-8. |
| 77 | pub fn unescape_osrust-analyzer cargo grep-cli 0.1.12 escape/unescape_os().(string: &OsStrrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/OsStr#) -> Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8> { |
| 78 | unescaperust-analyzer cargo grep-cli 0.1.12 escape/unescape().(&string.to_string_lossyrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/impl#[OsStr]to_string_lossy().()) |
| 79 | } |
| 80 | |
| 81 | #[cfg(test)] |
| 82 | mod testsrust-analyzer cargo grep-cli 0.1.12 escape/tests/ { |
| 83 | use superrust-analyzer cargo grep-cli 0.1.12 escape/::{escaperust-analyzer cargo grep-cli 0.1.12 escape/escape()., unescaperust-analyzer cargo grep-cli 0.1.12 escape/unescape().}; |
| 84 | |
| 85 | fn brust-analyzer cargo grep-cli 0.1.12 escape/tests/b().(bytes: &'static [u8]) -> Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8> { |
| 86 | bytes.to_vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc slice/impl#[`[T]`]to_vec().() |
| 87 | } |
| 88 | |
| 89 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 90 | fn emptyrust-analyzer cargo grep-cli 0.1.12 escape/tests/empty().() { |
| 91 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(brust-analyzer cargo grep-cli 0.1.12 escape/tests/b().(b""), unescaperust-analyzer cargo grep-cli 0.1.12 escape/unescape().(r"")); |
| 92 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(r"", escaperust-analyzer cargo grep-cli 0.1.12 escape/escape().(b"")); |
| 93 | } |
| 94 | |
| 95 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 96 | fn backslashrust-analyzer cargo grep-cli 0.1.12 escape/tests/backslash().() { |
| 97 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(brust-analyzer cargo grep-cli 0.1.12 escape/tests/b().(b"\\"), unescaperust-analyzer cargo grep-cli 0.1.12 escape/unescape().(r"\\")); |
| 98 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(r"\\", escaperust-analyzer cargo grep-cli 0.1.12 escape/escape().(b"\\")); |
| 99 | } |
| 100 | |
| 101 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 102 | fn nulrust-analyzer cargo grep-cli 0.1.12 escape/tests/nul().() { |
| 103 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(brust-analyzer cargo grep-cli 0.1.12 escape/tests/b().(b"\x00"), unescaperust-analyzer cargo grep-cli 0.1.12 escape/unescape().(r"\x00")); |
| 104 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(brust-analyzer cargo grep-cli 0.1.12 escape/tests/b().(b"\x00"), unescaperust-analyzer cargo grep-cli 0.1.12 escape/unescape().(r"\0")); |
| 105 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(r"\0", escaperust-analyzer cargo grep-cli 0.1.12 escape/escape().(b"\x00")); |
| 106 | } |
| 107 | |
| 108 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 109 | fn nlrust-analyzer cargo grep-cli 0.1.12 escape/tests/nl().() { |
| 110 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(brust-analyzer cargo grep-cli 0.1.12 escape/tests/b().(b"\n"), unescaperust-analyzer cargo grep-cli 0.1.12 escape/unescape().(r"\n")); |
| 111 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(r"\n", escaperust-analyzer cargo grep-cli 0.1.12 escape/escape().(b"\n")); |
| 112 | } |
| 113 | |
| 114 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 115 | fn tabrust-analyzer cargo grep-cli 0.1.12 escape/tests/tab().() { |
| 116 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(brust-analyzer cargo grep-cli 0.1.12 escape/tests/b().(b"\t"), unescaperust-analyzer cargo grep-cli 0.1.12 escape/unescape().(r"\t")); |
| 117 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(r"\t", escaperust-analyzer cargo grep-cli 0.1.12 escape/escape().(b"\t")); |
| 118 | } |
| 119 | |
| 120 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 121 | fn carriagerust-analyzer cargo grep-cli 0.1.12 escape/tests/carriage().() { |
| 122 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(brust-analyzer cargo grep-cli 0.1.12 escape/tests/b().(b"\r"), unescaperust-analyzer cargo grep-cli 0.1.12 escape/unescape().(r"\r")); |
| 123 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(r"\r", escaperust-analyzer cargo grep-cli 0.1.12 escape/escape().(b"\r")); |
| 124 | } |
| 125 | |
| 126 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 127 | fn nothing_simplerust-analyzer cargo grep-cli 0.1.12 escape/tests/nothing_simple().() { |
| 128 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(brust-analyzer cargo grep-cli 0.1.12 escape/tests/b().(b"\\a"), unescaperust-analyzer cargo grep-cli 0.1.12 escape/unescape().(r"\a")); |
| 129 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(brust-analyzer cargo grep-cli 0.1.12 escape/tests/b().(b"\\a"), unescaperust-analyzer cargo grep-cli 0.1.12 escape/unescape().(r"\\a")); |
| 130 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(r"\\a", escaperust-analyzer cargo grep-cli 0.1.12 escape/escape().(b"\\a")); |
| 131 | } |
| 132 | |
| 133 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 134 | fn nothing_hex0rust-analyzer cargo grep-cli 0.1.12 escape/tests/nothing_hex0().() { |
| 135 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(brust-analyzer cargo grep-cli 0.1.12 escape/tests/b().(b"\\x"), unescaperust-analyzer cargo grep-cli 0.1.12 escape/unescape().(r"\x")); |
| 136 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(brust-analyzer cargo grep-cli 0.1.12 escape/tests/b().(b"\\x"), unescaperust-analyzer cargo grep-cli 0.1.12 escape/unescape().(r"\\x")); |
| 137 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(r"\\x", escaperust-analyzer cargo grep-cli 0.1.12 escape/escape().(b"\\x")); |
| 138 | } |
| 139 | |
| 140 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 141 | fn nothing_hex1rust-analyzer cargo grep-cli 0.1.12 escape/tests/nothing_hex1().() { |
| 142 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(brust-analyzer cargo grep-cli 0.1.12 escape/tests/b().(b"\\xz"), unescaperust-analyzer cargo grep-cli 0.1.12 escape/unescape().(r"\xz")); |
| 143 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(brust-analyzer cargo grep-cli 0.1.12 escape/tests/b().(b"\\xz"), unescaperust-analyzer cargo grep-cli 0.1.12 escape/unescape().(r"\\xz")); |
| 144 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(r"\\xz", escaperust-analyzer cargo grep-cli 0.1.12 escape/escape().(b"\\xz")); |
| 145 | } |
| 146 | |
| 147 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 148 | fn nothing_hex2rust-analyzer cargo grep-cli 0.1.12 escape/tests/nothing_hex2().() { |
| 149 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(brust-analyzer cargo grep-cli 0.1.12 escape/tests/b().(b"\\xzz"), unescaperust-analyzer cargo grep-cli 0.1.12 escape/unescape().(r"\xzz")); |
| 150 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(brust-analyzer cargo grep-cli 0.1.12 escape/tests/b().(b"\\xzz"), unescaperust-analyzer cargo grep-cli 0.1.12 escape/unescape().(r"\\xzz")); |
| 151 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(r"\\xzz", escaperust-analyzer cargo grep-cli 0.1.12 escape/escape().(b"\\xzz")); |
| 152 | } |
| 153 | |
| 154 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 155 | fn invalid_utf8rust-analyzer cargo grep-cli 0.1.12 escape/tests/invalid_utf8().() { |
| 156 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(r"\xFF", escaperust-analyzer cargo grep-cli 0.1.12 escape/escape().(b"\xFF")); |
| 157 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(r"a\xFFb", escaperust-analyzer cargo grep-cli 0.1.12 escape/escape().(b"a\xFFb")); |
| 158 | } |
| 159 | } |