Decodes UTF-8 characters from a string using MTREE-specific escape sequences. MTREE uses various decodings. 1. the VIS_CSTYLE encoding of `strsvis(3)`, which encodes a specific set of characters. Of these, only the following control characters are allowed in filenames: - \s Space - \t Tab - \r Carriage Return - \n Line Feed 2. `#` is encoded as `\#` to differentiate between comments. 3. For all o
(input: &mut &str)
| 28 | /// have convenient backtracking and error messages in case we encounter invalid escape |
| 29 | /// sequences or malformed escaped UTF-8. |
| 30 | pub fn decode_utf8_chars(input: &mut &str) -> ModalResult<String> { |
| 31 | // This is the string we'll accumulated the decoded path into. |
| 32 | let mut path = String::new(); |
| 33 | |
| 34 | loop { |
| 35 | // Parse the string until we hit a `\` |
| 36 | let part = take_while(0.., |c| c != '\\').parse_next(input)?; |
| 37 | path.push_str(part); |
| 38 | |
| 39 | if input.is_empty() { |
| 40 | break; |
| 41 | } |
| 42 | |
| 43 | // We hit a `\`. See if it's an expected escape sequence. |
| 44 | // If none of the expected sequences are encountered, fail and throw an error. |
| 45 | let escaped = alt(( |
| 46 | "\\s".map(|s: &str| s.to_string()), |
| 47 | "\\t".map(|s: &str| s.to_string()), |
| 48 | "\\r".map(|s: &str| s.to_string()), |
| 49 | "\\n".map(|s: &str| s.to_string()), |
| 50 | "\\#".map(|s: &str| s.to_string()), |
| 51 | unicode_char, |
| 52 | fail.context(StrContext::Label("escape sequence")) |
| 53 | .context(StrContext::Expected(StrContextValue::Description( |
| 54 | "VIS_CSTYLE encoding or encoded octal triplets for unicode chars.", |
| 55 | ))), |
| 56 | )) |
| 57 | .parse_next(input)?; |
| 58 | |
| 59 | let unescaped = match escaped.as_str() { |
| 60 | "\\s" => " ".to_string(), |
| 61 | "\\t" => "\t".to_string(), |
| 62 | "\\r" => "\r".to_string(), |
| 63 | "\\n" => "\n".to_string(), |
| 64 | "\\#" => "#".to_string(), |
| 65 | _ => escaped, |
| 66 | }; |
| 67 | |
| 68 | path.push_str(&unescaped); |
| 69 | } |
| 70 | |
| 71 | Ok(path) |
| 72 | } |
| 73 | |
| 74 | /// Parse and convert a single octal triplet string into a byte. |
| 75 | /// |