diff options
author | Titus Wormer <tituswormer@gmail.com> | 2022-09-05 12:00:33 +0200 |
---|---|---|
committer | Titus Wormer <tituswormer@gmail.com> | 2022-09-05 12:00:33 +0200 |
commit | 16de10fe2395002644d685fdfcf76823346d1cc4 (patch) | |
tree | f2f01942d8aa218ba9bdc8ce47990d1da6cbc1b9 /src/util | |
parent | 4aa3e7fb4cf0e42fff25d836ce99a82d00cba120 (diff) | |
download | markdown-rs-16de10fe2395002644d685fdfcf76823346d1cc4.tar.gz markdown-rs-16de10fe2395002644d685fdfcf76823346d1cc4.tar.bz2 markdown-rs-16de10fe2395002644d685fdfcf76823346d1cc4.zip |
Add support for getting `char`s from bytes
Diffstat (limited to 'src/util')
-rw-r--r-- | src/util/slice.rs | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/src/util/slice.rs b/src/util/slice.rs index 0734d78..d02a526 100644 --- a/src/util/slice.rs +++ b/src/util/slice.rs @@ -5,6 +5,28 @@ use crate::util::constant::TAB_SIZE; use alloc::string::String; use core::str; +/// Get a [`char`][] right before `index` in bytes (`&[u8]`). +/// +/// In most cases, markdown operates on ASCII bytes. +/// In a few cases, it is unicode aware, so we need to find an actual char. +pub fn char_before_index(bytes: &[u8], index: usize) -> Option<char> { + let start = if index < 4 { 0 } else { index - 4 }; + String::from_utf8_lossy(&bytes[start..index]).chars().last() +} + +/// Get a [`char`][] right at `index` in bytes (`&[u8]`). +/// +/// In most cases, markdown operates on ASCII bytes. +/// In a few cases, it is unicode aware, so we need to find an actual char. +pub fn char_after_index(bytes: &[u8], index: usize) -> Option<char> { + let end = if index + 4 > bytes.len() { + bytes.len() + } else { + index + 4 + }; + String::from_utf8_lossy(&bytes[index..end]).chars().next() +} + /// A range between two points. #[derive(Debug)] pub struct Position<'a> { |