Skip to content

Commit

Permalink
add const_utf8_char_next
Browse files Browse the repository at this point in the history
  • Loading branch information
TimLuq committed Aug 11, 2024
1 parent dd1360f commit 3e8eb4e
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
41 changes: 41 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -494,5 +494,46 @@ pub const fn const_split_once_str<'a>(
}
}

/// Helper function to get the next UTF-8 char from a byte slice.
/// Returns the value of the first char and the number of bytes the code point is encoded at.
#[inline]
#[must_use]
pub const fn const_utf8_char_next(data: &[u8]) -> Option<(u32, u8)> {
let Some((first, data)) = data.split_first() else {
return None;
};
let first = *first;
let mut value;
let len = if first & 0b1000_0000 == 0 {
return Some((first as u32, 1));
} else if first & 0b1110_0000 == 0b1100_0000 {
value = (first & 0b0001_1111) as u32;
2
} else if first & 0b1111_0000 == 0b1110_0000 {
value = (first & 0b0000_1111) as u32;
3
} else if first & 0b1111_1000 == 0b1111_0000 {
value = (first & 0b0000_0111) as u32;
4
} else {
return None;
};
if data.len() < len {
return None;
}
let mut i = 1;
while i < len {
let byte = data[i];
if byte & 0b1100_0000 != 0b1000_0000 {
return None;
}
value = (value << 6_u8) | (byte & 0b0011_1111) as u32;
i += 1;
}
#[allow(clippy::cast_possible_truncation)]
let len = len as u8;
Some((value, len))
}

#[cfg(test)]
mod test;
4 changes: 2 additions & 2 deletions src/shared_bytes_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ impl SharedBytesBuilder {
}

/// Creates a new `SharedBytesBuilder`.
///
///
/// # Panics
///
///
/// Panics if the alignment is not a power of two or is greater than 512 or the maximum allowed by the system.
#[inline]
#[must_use]
Expand Down

0 comments on commit 3e8eb4e

Please sign in to comment.