| 1 | #![no_std] |
| 2 | #![forbid(unsafe_code)] |
| 3 | #![warn(clippy::pedantic, clippy::perf, missing_docs, clippy::panic, clippy::cargo)] |
| 4 | #![allow(clippy::type_complexity)] |
| 5 | #![cfg_attr(docsrs, feature(doc_cfg))] |
| 6 | |
| 7 | |
| 8 | /*! |
| 9 | |
| 10 | # descape |
| 11 | |
| 12 | Provides utilities for easily parsing escape sequences in a string via [`UnescapeExt`], using [`alloc::borrow::Cow`] to only borrow when needed. |
| 13 | |
| 14 | This library supports many escape sequences: |
| 15 | - `\\a` -> `\x07` |
| 16 | - `\\b` -> `\x08` |
| 17 | - `\\t` -> `\x09` |
| 18 | - `\\n` -> `\x0A` |
| 19 | - `\\v` -> `\x0B` |
| 20 | - `\\f` -> `\x0C` |
| 21 | - `\\r` -> `\x0D` |
| 22 | - `\\e` -> `\x1B` |
| 23 | - `\\'` -> `'` |
| 24 | - `\\"` -> `"` |
| 25 | - <code>\\`</code> -> <code>`</code> |
| 26 | - `\\\\` -> `\\` |
| 27 | - `\\xNN` -> `\xNN` |
| 28 | - `\\o` -> `\o`, for all octal digits `o` |
| 29 | - `\\oo` -> `\oo`, for all octal digits `o` |
| 30 | - `\\ooo` -> `\ooo`, for all octal digits `o` |
| 31 | - `\\uXXXX` -> `\u{XXXX}` |
| 32 | - `\\u{HEX}` -> `\u{HEX}` |
| 33 | |
| 34 | Along with this, you can define your own custom escape handlers! See [`UnescapeExt::to_unescaped_with`] for more information on that. |
| 35 | |
| 36 | This crate supports `no-std`. |
| 37 | |
| 38 | Optionally, this crate has the `std` and `core_error` features, |
| 39 | to allow the error type of an invalid escape to implement the `Error` trait. |
| 40 | |
| 41 | `std` uses `std::error::Error`, and `core_error` depends on `core::error::Error`, which is stable on Rust 1.82.0 or greater. |
| 42 | |
| 43 | */ |
| 44 | |
| 45 | |
| 46 | #[cfg(any(feature = "std", docsrs))] |
| 47 | extern crate stdrust-analyzer cargo descape 3.0.0 std/; |
| 48 | #[cfg(any(feature = "std", docsrs))] |
| 49 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std error/::Errorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core error/Error# as ErrorTraitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core error/Error#; |
| 50 | #[cfg(all(feature = "core_error", not(feature = "std")))] |
| 51 | use corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::errorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core error/::Errorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core error/Error# as ErrorTraitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core error/Error#; |
| 52 | |
| 53 | extern crate allocrust-analyzer cargo descape 3.0.0 alloc/; |
| 54 | |
| 55 | use allocrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc crate/::{ |
| 56 | borrowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/::Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#, |
| 57 | stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/::Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#, |
| 58 | strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/::CharIndicesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/iter/CharIndices# |
| 59 | }; |
| 60 | |
| 61 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:, PartialOrdrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd:, Ordrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ord:, Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default:, Hashrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/macros/Hash:)] |
| 62 | /// An error representing an invalid escape sequence in a string. |
| 63 | pub struct InvalidEscaperust-analyzer cargo descape 3.0.0 InvalidEscape# { |
| 64 | /// The index of the invalid escape sequence. |
| 65 | pub indexrust-analyzer cargo descape 3.0.0 InvalidEscape#index.: usize, |
| 66 | } |
| 67 | |
| 68 | impl InvalidEscaperust-analyzer cargo descape 3.0.0 InvalidEscape# { |
| 69 | /// Constructs an invalid escape error from an index. |
| 70 | #[must_use] |
| 71 | pub const fn newrust-analyzer cargo descape 3.0.0 impl#[InvalidEscape]new().(index: usize) -> Selfrust-analyzer cargo descape 3.0.0 impl#[InvalidEscape] { |
| 72 | Selfrust-analyzer cargo descape 3.0.0 impl#[InvalidEscape] { indexrust-analyzer cargo descape 3.0.0 InvalidEscape#index. } |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | impl corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::Displayrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Display# for InvalidEscaperust-analyzer cargo descape 3.0.0 InvalidEscape# { |
| 77 | fn fmtrust-analyzer cargo descape 3.0.0 impl#[InvalidEscape][Display]fmt().(&self, f: &mut corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::Formatterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Formatter#<'_>) -> corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Result# { |
| 78 | writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(f, "invalid escape sequence at index {}", self.indexrust-analyzer cargo descape 3.0.0 InvalidEscape#index.)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 79 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | #[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "core_error"))))] |
| 84 | #[cfg(any(feature = "std", feature = "core_error", docsrs))] |
| 85 | impl ErrorTraitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core error/Error# for InvalidEscaperust-analyzer cargo descape 3.0.0 InvalidEscape# {} |
| 86 | |
| 87 | /// An enum defining all possible non-error returns for a macro. |
| 88 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:)] |
| 89 | pub enum EscapeValuerust-analyzer cargo descape 3.0.0 EscapeValue#<'s> { |
| 90 | /// Remove the escape sequence entirely. |
| 91 | Removerust-analyzer cargo descape 3.0.0 EscapeValue#Remove#, |
| 92 | /// Leave the escape sequence as is. |
| 93 | Skiprust-analyzer cargo descape 3.0.0 EscapeValue#Skip#, |
| 94 | /// Replace the escape sequence with a single character. |
| 95 | Characterrust-analyzer cargo descape 3.0.0 EscapeValue#Character#(char), |
| 96 | /// Replace the escape sequence with a string of characters. |
| 97 | Stringrust-analyzer cargo descape 3.0.0 EscapeValue#String#(Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#<'s, str>) |
| 98 | } |
| 99 | |
| 100 | impl Fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/From#<char> for EscapeValuerust-analyzer cargo descape 3.0.0 EscapeValue#<'static> { |
| 101 | fn fromrust-analyzer cargo descape 3.0.0 impl#[`EscapeValue<'static>`][`From<char>`]from().(value: char) -> Selfrust-analyzer cargo descape 3.0.0 impl#[`EscapeValue<'static>`][`From<char>`] { |
| 102 | Selfrust-analyzer cargo descape 3.0.0 impl#[`EscapeValue<'static>`][`From<char>`]::Characterrust-analyzer cargo descape 3.0.0 EscapeValue#Character#(value) |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | impl<'string> Fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/From#<&'string str> for EscapeValuerust-analyzer cargo descape 3.0.0 EscapeValue#<'string> { |
| 107 | fn fromrust-analyzer cargo descape 3.0.0 impl#[`EscapeValue<'string>`][`From<&'string str>`]from().(value: &'string str) -> Selfrust-analyzer cargo descape 3.0.0 impl#[`EscapeValue<'string>`][`From<&'string str>`] { |
| 108 | Selfrust-analyzer cargo descape 3.0.0 impl#[`EscapeValue<'string>`][`From<&'string str>`]::Stringrust-analyzer cargo descape 3.0.0 EscapeValue#String#(Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#::Borrowedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#Borrowed#(value)) |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | impl<'string> Fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/From#<Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#<'string, str>> for EscapeValuerust-analyzer cargo descape 3.0.0 EscapeValue#<'string> { |
| 113 | fn fromrust-analyzer cargo descape 3.0.0 impl#[`EscapeValue<'string>`][`From<Cow<'string, str>>`]from().(value: Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#<'string, str>) -> Selfrust-analyzer cargo descape 3.0.0 impl#[`EscapeValue<'string>`][`From<Cow<'string, str>>`] { |
| 114 | Selfrust-analyzer cargo descape 3.0.0 impl#[`EscapeValue<'string>`][`From<Cow<'string, str>>`]::Stringrust-analyzer cargo descape 3.0.0 EscapeValue#String#(value) |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | impl<'string> Fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/From#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#> for EscapeValuerust-analyzer cargo descape 3.0.0 EscapeValue#<'string> { |
| 119 | fn fromrust-analyzer cargo descape 3.0.0 impl#[`EscapeValue<'string>`][`From<String>`]from().(value: Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#) -> Selfrust-analyzer cargo descape 3.0.0 impl#[`EscapeValue<'string>`][`From<String>`] { |
| 120 | Selfrust-analyzer cargo descape 3.0.0 impl#[`EscapeValue<'string>`][`From<String>`]::Stringrust-analyzer cargo descape 3.0.0 EscapeValue#String#(Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#::Ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#Owned#(value)) |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | /// A trait distinguishing an object as a handler for custom escape sequences. |
| 125 | /// |
| 126 | /// For convenience, this trait is **automatically implemented** for all implementors of `FnMut` with the correct signature. |
| 127 | /// |
| 128 | pub trait EscapeHandlerrust-analyzer cargo descape 3.0.0 EscapeHandler# { |
| 129 | /// Definition of a custom escape handler. |
| 130 | /// |
| 131 | /// Custom escape handlers are called before parsing any escape sequences, |
| 132 | /// and are given 3 arguments: |
| 133 | /// - `idx`: The index of the current character (e.g. `Hello\nthere` gets `5`) |
| 134 | /// - `chr`: The current character in the string (e.g. `\\n` gets `'n'`) |
| 135 | /// - `iter`: A mutable reference to the underlying character iterator - |
| 136 | /// use this to get the rest of the string via `CharIndices::as_str`, |
| 137 | /// or get the next characters |
| 138 | /// |
| 139 | /// Handlers return a `Result<Option<char>, ()>`. |
| 140 | /// Returning `Ok(Some(char))` replaces the sequence with the given character, |
| 141 | /// returning `Ok(None)` removes the sequence entirely, |
| 142 | /// and returning `Err` errors the unescaping at the index of the escape sequence. |
| 143 | /// |
| 144 | /// |
| 145 | /// # Examples |
| 146 | |
| 147 | /// ## Permitting any escape, handing it back raw |
| 148 | /// ```rust |
| 149 | /// # use descape::*; use std::str::CharIndices; |
| 150 | /// fn raw<'i, 's>(idx: usize, chr: char, iter: &'i mut CharIndices<'s>) -> Result<EscapeValue<'s>, ()> { |
| 151 | /// Ok(chr.into()) |
| 152 | /// } |
| 153 | |
| 154 | /// let escaped = r"\H\e\l\l\o \n \W\o\r\l\d"; |
| 155 | /// let unescaped = escaped.to_unescaped_with(raw).unwrap(); |
| 156 | /// assert_eq!(unescaped, "Hello n World"); |
| 157 | /// ``` |
| 158 | |
| 159 | /// ## Removing escape sequences entirely |
| 160 | /// ```rust |
| 161 | /// # use descape::*; use std::str::CharIndices; |
| 162 | /// fn raw<'i, 's>(idx: usize, chr: char, iter: &'i mut CharIndices<'s>) -> Result<EscapeValue<'s>, ()> { |
| 163 | /// Ok(EscapeValue::Remove) |
| 164 | /// } |
| 165 | |
| 166 | /// let escaped = r"What if I want a \nnewline?"; |
| 167 | /// let unescaped = escaped.to_unescaped_with(raw).unwrap(); |
| 168 | /// assert_eq!(unescaped, "What if I want a newline?"); |
| 169 | /// ``` |
| 170 | |
| 171 | /// ## Replacing escape sequences with their character number |
| 172 | /// ```rust |
| 173 | /// # use descape::*; use std::str::CharIndices; |
| 174 | /// fn charnum<'i, 's>(idx: usize, chr: char, iter: &'i mut CharIndices<'s>) -> Result<EscapeValue<'s>, ()> { |
| 175 | /// Ok(format!("[{:02X}]", chr as u32).into()) |
| 176 | /// } |
| 177 | |
| 178 | /// let escaped = r"Well this\Ais a\Zthing."; |
| 179 | /// let unescaped = escaped.to_unescaped_with(charnum).unwrap(); |
| 180 | /// assert_eq!(unescaped, "Well this[41]is a[5A]thing."); |
| 181 | /// ``` |
| 182 | |
| 183 | /// ## Not allowing escape sequences unsupported by Rust |
| 184 | /// ```rust |
| 185 | /// # use descape::*; use std::str::CharIndices; |
| 186 | /// fn rust_only<'i, 's>(idx: usize, chr: char, iter: &'i mut CharIndices<'s>) -> Result<EscapeValue<'s>, ()> { |
| 187 | /// match chr { |
| 188 | /// 'a' | 'b' | 'v' | 'f' | 'e' | '`' => Err(()), |
| 189 | /// _ => descape::DefaultEscapeHandler.escape(idx, chr, iter) |
| 190 | /// } |
| 191 | /// } |
| 192 | |
| 193 | /// r"This is \nfine".to_unescaped_with(rust_only).expect(r"\n is valid"); |
| 194 | /// r"This is not \fine".to_unescaped_with(rust_only).expect_err(r"\f is invalid"); |
| 195 | /// ``` |
| 196 | |
| 197 | /// # An informal note |
| 198 | /// Ideally, this trait would return `Result<EscapeValue<'source>, Option<Box<dyn Error>>>`, but `Error` has only been in `core` |
| 199 | /// since Rust version `1.82.0`. Using it would bump the MSRV by a tremendous amount, |
| 200 | /// and as such it has been left out. |
| 201 | #[allow(clippy::result_unit_err, clippy::missing_errors_doc)] |
| 202 | fn escaperust-analyzer cargo descape 3.0.0 EscapeHandler#escape().<'iter, 'source>(&mut self, idx: usize, chr: char, iter: &'iter mut CharIndicesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/iter/CharIndices#<'source>) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<EscapeValuerust-analyzer cargo descape 3.0.0 EscapeValue#<'source>, ()>; |
| 203 | /// The escape prefix to use for this handler. |
| 204 | /// Defaults to `\\`. |
| 205 | /// |
| 206 | /// # Examples |
| 207 | /// ```rust |
| 208 | /// # use descape::*; use std::str::CharIndices; |
| 209 | /// struct PercentEscape; |
| 210 | /// impl EscapeHandler for PercentEscape { |
| 211 | /// fn prefix(&self) -> char { '%' } |
| 212 | /// fn escape<'iter, 'source>(&mut self, idx: usize, chr: char, iter: &'iter mut CharIndices<'source>) -> Result<EscapeValue<'source>, ()> { |
| 213 | /// descape::DefaultEscapeHandler.escape(idx, chr, iter) |
| 214 | /// } |
| 215 | /// } |
| 216 | /// |
| 217 | /// assert_eq!( |
| 218 | /// r"Hello,%tworld!".to_unescaped_with(PercentEscape).unwrap(), |
| 219 | /// "Hello,\tworld!" |
| 220 | /// ) |
| 221 | /// ``` |
| 222 | fn prefixrust-analyzer cargo descape 3.0.0 EscapeHandler#prefix().(&self) -> char { '\\' } |
| 223 | } |
| 224 | |
| 225 | impl<F> EscapeHandlerrust-analyzer cargo descape 3.0.0 EscapeHandler# for F |
| 226 | where F: for<'iter, 'source> FnMutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnMut#(usize, char, &'iter mut CharIndicesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/iter/CharIndices#<'source>) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<EscapeValuerust-analyzer cargo descape 3.0.0 EscapeValue#<'source>, ()> |
| 227 | { |
| 228 | fn escaperust-analyzer cargo descape 3.0.0 impl#[F][EscapeHandler]escape().<'iter, 'source>(&mut self, idx: usize, chr: char, iter: &'iter mut CharIndicesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/iter/CharIndices#<'source>) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<EscapeValuerust-analyzer cargo descape 3.0.0 EscapeValue#<'source>, ()> { |
| 229 | self(idx, chr, iter) |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | mod sealedrust-analyzer cargo descape 3.0.0 sealed/ { |
| 234 | pub trait Sealedrust-analyzer cargo descape 3.0.0 sealed/Sealed# {} |
| 235 | impl Sealedrust-analyzer cargo descape 3.0.0 sealed/Sealed# for str {} |
| 236 | } |
| 237 | |
| 238 | /// An extension trait for [`&str`](str) to allow parsing escape sequences in strings, only copying when needed. |
| 239 | pub trait UnescapeExtrust-analyzer cargo descape 3.0.0 UnescapeExt#: sealedrust-analyzer cargo descape 3.0.0 sealed/::Sealedrust-analyzer cargo descape 3.0.0 sealed/Sealed# { |
| 240 | |
| 241 | /** |
| 242 | Unescapes a string, returning an [`alloc::borrow::Cow`]. |
| 243 | Will only allocate if the string has any escape sequences. |
| 244 | |
| 245 | Uses [`crate::DefaultHandler`]. |
| 246 | |
| 247 | # Errors |
| 248 | Errors if there's an invalid escape sequence in the string. |
| 249 | Passes back the byte index of the invalid character. |
| 250 | */ |
| 251 | fn to_unescapedrust-analyzer cargo descape 3.0.0 UnescapeExt#to_unescaped().(&self) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#<'_, str>, InvalidEscaperust-analyzer cargo descape 3.0.0 InvalidEscape#>; |
| 252 | /** |
| 253 | Unescapes a string using a custom escape handler. See the documentation of [`crate::EscapeHandler`] for more details. |
| 254 | |
| 255 | # Errors |
| 256 | |
| 257 | Errors if there's an invalid escape sequence in the string. |
| 258 | Passes back the byte index of the invalid character. |
| 259 | |
| 260 | */ |
| 261 | fn to_unescaped_withrust-analyzer cargo descape 3.0.0 UnescapeExt#to_unescaped_with().( |
| 262 | &self, |
| 263 | callback: impl EscapeHandlerrust-analyzer cargo descape 3.0.0 EscapeHandler# |
| 264 | ) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#<'_, str>, InvalidEscaperust-analyzer cargo descape 3.0.0 InvalidEscape#>; |
| 265 | } |
| 266 | |
| 267 | |
| 268 | impl UnescapeExtrust-analyzer cargo descape 3.0.0 UnescapeExt# for str { |
| 269 | #[inline] |
| 270 | fn to_unescapedrust-analyzer cargo descape 3.0.0 impl#[str][UnescapeExt]to_unescaped().(&'_ self) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#<'_, str>, InvalidEscaperust-analyzer cargo descape 3.0.0 InvalidEscape#> { |
| 271 | self.to_unescaped_withrust-analyzer cargo descape 3.0.0 impl#[str][UnescapeExt]to_unescaped_with().(DefaultEscapeHandlerrust-analyzer cargo descape 3.0.0 DefaultEscapeHandler#) |
| 272 | } |
| 273 | |
| 274 | // Put this outside to prevent monomorphization bloat |
| 275 | fn to_unescaped_withrust-analyzer cargo descape 3.0.0 impl#[str][UnescapeExt]to_unescaped_with().( |
| 276 | &'_ self, |
| 277 | mut callback: impl EscapeHandlerrust-analyzer cargo descape 3.0.0 EscapeHandler# |
| 278 | ) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#<'_, str>, InvalidEscaperust-analyzer cargo descape 3.0.0 InvalidEscape#> { |
| 279 | to_unescaped_with_monorust-analyzer cargo descape 3.0.0 to_unescaped_with_mono().(self, &mut callback) |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | fn to_unescaped_with_monorust-analyzer cargo descape 3.0.0 to_unescaped_with_mono().<'this, 'cb>( |
| 284 | this: &'this str, |
| 285 | callback: &'cb mut dyn EscapeHandlerrust-analyzer cargo descape 3.0.0 EscapeHandler# |
| 286 | ) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#<'this, str>, InvalidEscaperust-analyzer cargo descape 3.0.0 InvalidEscape#> { |
| 287 | // Iterates over each character as a UTF-8 string slice |
| 288 | let mut iter = this.char_indicesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]char_indices().(); |
| 289 | let mut seen: &'this str = ""; |
| 290 | let mut owned = Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#::<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>; |
| 291 | let prefix = callback.prefixrust-analyzer cargo descape 3.0.0 EscapeHandler#prefix().(); |
| 292 | |
| 293 | while let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#((index, chr)) = iter.nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/iter/impl#[`CharIndices<'a>`][Iterator]next().() { |
| 294 | if chr rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[char][`PartialEq<Self>`]ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[char][`PartialEq<Self>`]ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[char][`PartialEq<Self>`]ne().prefix { |
| 295 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(owned) = &mut owned { |
| 296 | owned.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push().(chr); |
| 297 | } else { |
| 298 | seen = &this[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeTo#index rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().chr.len_utf8rust-analyzer cargo core https://github.com/rust-lang/rust/library/core char/methods/impl#[char]len_utf8().()]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().; |
| 299 | } |
| 300 | continue; |
| 301 | } |
| 302 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#((i, chr)) = iter.nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/iter/impl#[`CharIndices<'a>`][Iterator]next().() { |
| 303 | let res = callback.escaperust-analyzer cargo descape 3.0.0 EscapeHandler#escape().(index, chr, &mut iter) |
| 304 | .map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(|()| InvalidEscaperust-analyzer cargo descape 3.0.0 InvalidEscape# { indexrust-analyzer cargo descape 3.0.0 InvalidEscape#index. })?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 305 | if res rust-analyzer cargo descape 3.0.0 impl#[`EscapeValue<'s>`][`PartialEq<Self>`]eq().==rust-analyzer cargo descape 3.0.0 impl#[`EscapeValue<'s>`][`PartialEq<Self>`]eq(). rust-analyzer cargo descape 3.0.0 impl#[`EscapeValue<'s>`][`PartialEq<Self>`]eq().EscapeValuerust-analyzer cargo descape 3.0.0 EscapeValue#::Skiprust-analyzer cargo descape 3.0.0 EscapeValue#Skip# && owned.is_nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_none().() { |
| 306 | seen = &this[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeTo#i rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().chr.len_utf8rust-analyzer cargo core https://github.com/rust-lang/rust/library/core char/methods/impl#[char]len_utf8().()]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().; |
| 307 | continue; |
| 308 | } |
| 309 | let owned = owned.get_or_insert_withrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]get_or_insert_with().(|| seen.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()); |
| 310 | match res { |
| 311 | EscapeValuerust-analyzer cargo descape 3.0.0 EscapeValue#::Characterrust-analyzer cargo descape 3.0.0 EscapeValue#Character#(c) => { owned.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push().(c); continue; }, |
| 312 | EscapeValuerust-analyzer cargo descape 3.0.0 EscapeValue#::Stringrust-analyzer cargo descape 3.0.0 EscapeValue#String#(str) => { owned.push_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push_str().(&*rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/impl#[`Cow<'_, B>`][Deref]deref().str); continue; }, |
| 313 | EscapeValuerust-analyzer cargo descape 3.0.0 EscapeValue#::Removerust-analyzer cargo descape 3.0.0 EscapeValue#Remove# => { continue; }, |
| 314 | EscapeValuerust-analyzer cargo descape 3.0.0 EscapeValue#::Skiprust-analyzer cargo descape 3.0.0 EscapeValue#Skip# => { owned.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push().(prefix); owned.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push().(chr); continue; } |
| 315 | } |
| 316 | } else { |
| 317 | // No matches found |
| 318 | return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(InvalidEscaperust-analyzer cargo descape 3.0.0 InvalidEscape#::newrust-analyzer cargo descape 3.0.0 impl#[InvalidEscape]new().(index)); |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | match owned { |
| 323 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(string) => Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#::Ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#Owned#(string)), |
| 324 | Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# => Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#::Borrowedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#Borrowed#(this)), |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | /// The default escape sequence handler. |
| 329 | /// |
| 330 | /// The following escapes are valid: |
| 331 | /// - `\\a` -> `\x07` |
| 332 | /// - `\\b` -> `\x08` |
| 333 | /// - `\\t` -> `\x09` |
| 334 | /// - `\\n` -> `\x0A` |
| 335 | /// - `\\v` -> `\x0B` |
| 336 | /// - `\\f` -> `\x0C` |
| 337 | /// - `\\r` -> `\x0D` |
| 338 | /// - `\\e` -> `\x1B` |
| 339 | /// - `\\'` -> `'` |
| 340 | /// - `\\"` -> `"` |
| 341 | /// - <code>\\`</code> -> <code>`</code> |
| 342 | /// - `\\\\` -> `\\` |
| 343 | /// - `\\xNN` -> `\xNN` |
| 344 | /// - `\\o` -> `\o`, for all octal digits `o` |
| 345 | /// - `\\oo` -> `\oo`, for all octal digits `o` |
| 346 | /// - `\\ooo` -> `\ooo`, for all octal digits `o` |
| 347 | /// - `\\uXXXX` -> `\u{XXXX}` |
| 348 | /// - `\\u{HEX}` -> `\u{HEX}` |
| 349 | /// |
| 350 | pub struct DefaultEscapeHandlerrust-analyzer cargo descape 3.0.0 DefaultEscapeHandler#; |
| 351 | |
| 352 | impl EscapeHandlerrust-analyzer cargo descape 3.0.0 EscapeHandler# for DefaultEscapeHandlerrust-analyzer cargo descape 3.0.0 DefaultEscapeHandler# { |
| 353 | fn escaperust-analyzer cargo descape 3.0.0 impl#[DefaultEscapeHandler][EscapeHandler]escape().<'iter, 'source>(&mut self, _idx: usize, chr: char, iter: &'iter mut CharIndicesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/iter/CharIndices#<'source>) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<EscapeValuerust-analyzer cargo descape 3.0.0 EscapeValue#<'source>, ()> { |
| 354 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#( match chr { |
| 355 | 'a' => '\x07'.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(), |
| 356 | 'b' => '\x08'.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(), |
| 357 | 't' => '\x09'.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(), |
| 358 | 'n' => '\x0A'.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(), |
| 359 | 'v' => '\x0B'.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(), |
| 360 | 'f' => '\x0C'.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(), |
| 361 | 'r' => '\x0D'.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(), |
| 362 | 'e' => '\x1B'.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(), |
| 363 | '`' => '`'.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(), |
| 364 | '\'' => '\''.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(), |
| 365 | '"' => '"'.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(), |
| 366 | '\\' => '\\'.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(), |
| 367 | 'u' => { |
| 368 | let (chr, skip) = unescape_unicoderust-analyzer cargo descape 3.0.0 unescape_unicode().(iter).ok_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]ok_or().(())?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 369 | // Skip the needed amount of characters |
| 370 | for _ in 0..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#skip { iter.nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/iter/impl#[`CharIndices<'a>`][Iterator]next().(); } |
| 371 | chr.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().() |
| 372 | }, |
| 373 | 'x' => { |
| 374 | // Skip two characters |
| 375 | let res = unescape_hexrust-analyzer cargo descape 3.0.0 unescape_hex().(iter).ok_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]ok_or().(())?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 376 | iter.nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/iter/impl#[`CharIndices<'a>`][Iterator]next().(); |
| 377 | iter.nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/iter/impl#[`CharIndices<'a>`][Iterator]next().(); |
| 378 | res.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().() |
| 379 | }, |
| 380 | c if c.is_digitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core char/methods/impl#[char]is_digit().(8) => { |
| 381 | let (chr, skip) = unescape_octrust-analyzer cargo descape 3.0.0 unescape_oct().(c, iter).ok_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]ok_or().(())?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 382 | for _ in 0..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#skip { iter.nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/iter/impl#[`CharIndices<'a>`][Iterator]next().(); } |
| 383 | chr.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().() |
| 384 | }, |
| 385 | _ => return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(()), |
| 386 | } ) |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | fn unescape_unicoderust-analyzer cargo descape 3.0.0 unescape_unicode().( |
| 391 | iter: &mut CharIndicesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/iter/CharIndices# |
| 392 | ) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<(char, usize)> { |
| 393 | let string = iter.as_strrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/iter/impl#[`CharIndices<'a>`]as_str().(); |
| 394 | let (_, next) = iter.nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/iter/impl#[`CharIndices<'a>`][Iterator]next().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][Try]branch().; |
| 395 | if next rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[char][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[char][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[char][`PartialEq<Self>`]eq().'{' { |
| 396 | // \u{HEX} |
| 397 | let end = string[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().1 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFrom#..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFrom#]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()..findrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]find().('}')?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][Try]branch().; |
| 398 | let num = &string[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().1 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeInclusive#..=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeInclusive# rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeInclusive#end]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().; |
| 399 | let codepoint = u32::from_str_radixrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u32]from_str_radix().(num, 16).okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]ok().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][Try]branch().; |
| 400 | char::from_u32rust-analyzer cargo core https://github.com/rust-lang/rust/library/core char/methods/impl#[char]from_u32().(codepoint).maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]map().(|v| (v, end rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().1)) |
| 401 | } else { |
| 402 | // \uNNNN |
| 403 | // If any of these are non-ASCII, then it's already invalid, |
| 404 | // so a direct slice is fine |
| 405 | let next_four = string.getrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]get().( ..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeTo#4 )?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][Try]branch().; |
| 406 | let codepoint = u32::from_str_radixrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u32]from_str_radix().(next_four, 16).okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]ok().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][Try]branch().; |
| 407 | // Encode the u32 |
| 408 | char::from_u32rust-analyzer cargo core https://github.com/rust-lang/rust/library/core char/methods/impl#[char]from_u32().(codepoint).maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]map().(|v| (v, 3)) |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | // FIXME: This could be factored out along with part of unescape_unicode into its own function. |
| 413 | fn unescape_hexrust-analyzer cargo descape 3.0.0 unescape_hex().( |
| 414 | iter: &mut CharIndicesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/iter/CharIndices# |
| 415 | ) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<char> { |
| 416 | |
| 417 | // Must be \xNN |
| 418 | let codepoint = iter.as_strrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/iter/impl#[`CharIndices<'a>`]as_str().() |
| 419 | .getrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]get().(..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeTo#2) |
| 420 | .and_thenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]and_then().(|num| u32::from_str_radixrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u32]from_str_radix().(num, 16).okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]ok().())?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][Try]branch().; |
| 421 | char::from_u32rust-analyzer cargo core https://github.com/rust-lang/rust/library/core char/methods/impl#[char]from_u32().(codepoint) |
| 422 | } |
| 423 | |
| 424 | #[allow(clippy::cast_possible_truncation)] // Can't actually happen |
| 425 | fn unescape_octrust-analyzer cargo descape 3.0.0 unescape_oct().( |
| 426 | chr: char, |
| 427 | iter: &mut CharIndicesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/iter/CharIndices# |
| 428 | ) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<(char, usize)> { |
| 429 | |
| 430 | // Could be \o, \oo, or \ooo |
| 431 | let str = iter.as_strrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/iter/impl#[`CharIndices<'a>`]as_str().(); |
| 432 | let end = iter.clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/iter/impl#[`CharIndices<'a>`][Clone]clone().() // Cloning this is pretty cheap |
| 433 | .takerust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#take().(2) |
| 434 | .take_whilerust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#take_while().(|(_, c)| c.is_digitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core char/methods/impl#[char]is_digit().(8)) |
| 435 | .enumeraterust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#enumerate().() |
| 436 | .lastrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#last().() |
| 437 | .map_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]map_or().(0, |(idx, _)| idx rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().1); |
| 438 | let num = &str[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeTo# rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeTo#end]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().; |
| 439 | // These are the characters _after_ the first |
| 440 | let mut codepoint = if num.is_emptyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]is_empty().() { 0 } else { u32::from_str_radixrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u32]from_str_radix().(num, 8).okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]ok().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][Try]branch(). }; |
| 441 | // Add the first character at the top of the number |
| 442 | codepoint rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`AddAssign<Self>`]add_assign().(chr as u32 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`Sub<Self>`]sub().'0' as u32) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`Mul<Self>`]mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`Mul<Self>`]mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`Mul<Self>`]mul().8u32.powrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u32]pow().(end as u32); |
| 443 | char::from_u32rust-analyzer cargo core https://github.com/rust-lang/rust/library/core char/methods/impl#[char]from_u32().(codepoint).maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]map().(|chr| (chr, end)) |
| 444 | } |
| 445 |