Like `decode_utf8`, but decodes the last UTF-8 sequence in `src` instead of the first.
(src: &[u8])
| 121 | /// Like `decode_utf8`, but decodes the last UTF-8 sequence in `src` instead |
| 122 | /// of the first. |
| 123 | pub fn decode_last_utf8(src: &[u8]) -> Option<(char, usize)> { |
| 124 | if src.is_empty() { |
| 125 | return None; |
| 126 | } |
| 127 | let mut start = src.len() - 1; |
| 128 | if src[start] <= 0x7F { |
| 129 | return Some((src[start] as char, 1)); |
| 130 | } |
| 131 | while start > src.len().saturating_sub(4) { |
| 132 | start -= 1; |
| 133 | if is_start_byte(src[start]) { |
| 134 | break; |
| 135 | } |
| 136 | } |
| 137 | match decode_utf8(&src[start..]) { |
| 138 | None => None, |
| 139 | Some((_, n)) if n < src.len() - start => None, |
| 140 | Some((cp, n)) => Some((cp, n)), |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | fn is_start_byte(b: u8) -> bool { |
| 145 | b & 0b11_000000 != 0b1_0000000 |
no test coverage detected