Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make crate no_std compatible #66

Merged
merged 2 commits into from
Feb 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions src/ascii.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ use crate::simd_funcs::*;
cfg_if! {
if #[cfg(feature = "simd-accel")] {
#[allow(unused_imports)]
use ::std::intrinsics::unlikely;
use ::core::intrinsics::unlikely;
#[allow(unused_imports)]
use ::std::intrinsics::likely;
use ::core::intrinsics::likely;
} else {
#[allow(dead_code)]
#[inline(always)]
Expand Down Expand Up @@ -103,15 +103,15 @@ macro_rules! ascii_alu {
let mut until_alignment = {
// Check if the other unit aligns if we move the narrower unit
// to alignment.
// if ::std::mem::size_of::<$src_unit>() == ::std::mem::size_of::<$dst_unit>() {
// if ::core::mem::size_of::<$src_unit>() == ::core::mem::size_of::<$dst_unit>() {
// ascii_to_ascii
let src_alignment = (src as usize) & ALU_ALIGNMENT_MASK;
let dst_alignment = (dst as usize) & ALU_ALIGNMENT_MASK;
if src_alignment != dst_alignment {
break;
}
(ALU_ALIGNMENT - src_alignment) & ALU_ALIGNMENT_MASK
// } else if ::std::mem::size_of::<$src_unit>() < ::std::mem::size_of::<$dst_unit>() {
// } else if ::core::mem::size_of::<$src_unit>() < ::core::mem::size_of::<$dst_unit>() {
// ascii_to_basic_latin
// let src_until_alignment = (ALIGNMENT - ((src as usize) & ALIGNMENT_MASK)) & ALIGNMENT_MASK;
// if (dst.add(src_until_alignment) as usize) & ALIGNMENT_MASK != 0 {
Expand Down Expand Up @@ -197,7 +197,7 @@ macro_rules! basic_latin_alu {
let mut until_alignment = {
// Check if the other unit aligns if we move the narrower unit
// to alignment.
// if ::std::mem::size_of::<$src_unit>() == ::std::mem::size_of::<$dst_unit>() {
// if ::core::mem::size_of::<$src_unit>() == ::core::mem::size_of::<$dst_unit>() {
// ascii_to_ascii
// let src_alignment = (src as usize) & ALIGNMENT_MASK;
// let dst_alignment = (dst as usize) & ALIGNMENT_MASK;
Expand All @@ -206,7 +206,7 @@ macro_rules! basic_latin_alu {
// }
// (ALIGNMENT - src_alignment) & ALIGNMENT_MASK
// } else
if ::std::mem::size_of::<$src_unit>() < ::std::mem::size_of::<$dst_unit>() {
if ::core::mem::size_of::<$src_unit>() < ::core::mem::size_of::<$dst_unit>() {
// ascii_to_basic_latin
let src_until_alignment = (ALU_ALIGNMENT
- ((src as usize) & ALU_ALIGNMENT_MASK))
Expand Down Expand Up @@ -290,7 +290,7 @@ macro_rules! latin1_alu {
// This loop is only broken out of as a `goto` forward
loop {
let mut until_alignment = {
if ::std::mem::size_of::<$src_unit>() < ::std::mem::size_of::<$dst_unit>() {
if ::core::mem::size_of::<$src_unit>() < ::core::mem::size_of::<$dst_unit>() {
// unpack
let src_until_alignment = (ALU_ALIGNMENT
- ((src as usize) & ALU_ALIGNMENT_MASK))
Expand Down Expand Up @@ -447,7 +447,7 @@ macro_rules! ascii_simd_check_align_unrolled {
dst: *mut $dst_unit,
len: usize,
) -> Option<($src_unit, usize)> {
let unit_size = ::std::mem::size_of::<$src_unit>();
let unit_size = ::core::mem::size_of::<$src_unit>();
let mut offset = 0usize;
// This loop is only broken out of as a goto forward without
// actually looping
Expand Down Expand Up @@ -629,7 +629,7 @@ macro_rules! latin1_simd_check_align_unrolled {
) => {
#[inline(always)]
pub unsafe fn $name(src: *const $src_unit, dst: *mut $dst_unit, len: usize) {
let unit_size = ::std::mem::size_of::<$src_unit>();
let unit_size = ::core::mem::size_of::<$src_unit>();
let mut offset = 0usize;
if SIMD_STRIDE_SIZE <= len {
let mut until_alignment = ((SIMD_STRIDE_SIZE
Expand Down Expand Up @@ -1511,6 +1511,7 @@ pub fn iso_2022_jp_ascii_valid_up_to(bytes: &[u8]) -> usize {
#[cfg(test)]
mod tests {
use super::*;
use alloc::vec::Vec;

macro_rules! test_ascii {
($test_name:ident, $fn_tested:ident, $src_unit:ty, $dst_unit:ty) => {
Expand Down
36 changes: 18 additions & 18 deletions src/handles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,12 @@ impl UnalignedU16Slice {

#[inline(always)]
pub fn at(&self, i: usize) -> u16 {
use std::mem::MaybeUninit;
use core::mem::MaybeUninit;

assert!(i < self.len);
unsafe {
let mut u: MaybeUninit<u16> = MaybeUninit::uninit();
::std::ptr::copy_nonoverlapping(self.ptr.add(i * 2), u.as_mut_ptr() as *mut u8, 2);
::core::ptr::copy_nonoverlapping(self.ptr.add(i * 2), u.as_mut_ptr() as *mut u8, 2);
u.assume_init()
}
}
Expand Down Expand Up @@ -195,7 +195,7 @@ fn copy_unaligned_basic_latin_to_ascii_alu<E: Endian>(
dst: &mut [u8],
offset: usize,
) -> CopyAsciiResult<usize, (u16, usize)> {
let len = ::std::cmp::min(src.len(), dst.len());
let len = ::core::cmp::min(src.len(), dst.len());
let mut i = 0usize;
loop {
if i == len {
Expand Down Expand Up @@ -234,7 +234,7 @@ fn copy_unaligned_basic_latin_to_ascii<E: Endian>(
src: UnalignedU16Slice,
dst: &mut [u8],
) -> CopyAsciiResult<usize, (u16, usize)> {
let len = ::std::cmp::min(src.len(), dst.len());
let len = ::core::cmp::min(src.len(), dst.len());
let mut offset = 0;
if SIMD_STRIDE_SIZE <= len {
let len_minus_stride = len - SIMD_STRIDE_SIZE;
Expand Down Expand Up @@ -736,7 +736,7 @@ impl<'a> Utf16Destination<'a> {
let mut src_unaligned = unsafe {
UnalignedU16Slice::new(
src_remaining.as_ptr(),
::std::cmp::min(src_remaining.len() / 2, dst_remaining.len()),
::core::cmp::min(src_remaining.len() / 2, dst_remaining.len()),
)
};
if src_unaligned.len() == 0 {
Expand Down Expand Up @@ -1080,7 +1080,7 @@ impl<'a> Utf8Destination<'a> {
pub fn copy_utf8_up_to_invalid_from(&mut self, source: &mut ByteSource) {
let src_remaining = &source.slice[source.pos..];
let dst_remaining = &mut self.slice[self.pos..];
let min_len = ::std::cmp::min(src_remaining.len(), dst_remaining.len());
let min_len = ::core::cmp::min(src_remaining.len(), dst_remaining.len());
// Validate first, then memcpy to let memcpy do its thing even for
// non-ASCII. (And potentially do something better than SSE2 for ASCII.)
let valid_len = utf8_valid_up_to(&src_remaining[..min_len]);
Expand Down Expand Up @@ -1156,7 +1156,7 @@ impl<'a> Utf16Source<'a> {
self.pos += 1;
let unit_minus_surrogate_start = unit.wrapping_sub(0xD800);
if unit_minus_surrogate_start > (0xDFFF - 0xD800) {
return unsafe { ::std::char::from_u32_unchecked(u32::from(unit)) };
return unsafe { ::core::char::from_u32_unchecked(u32::from(unit)) };
}
if unit_minus_surrogate_start <= (0xDBFF - 0xD800) {
// high surrogate
Expand All @@ -1167,7 +1167,7 @@ impl<'a> Utf16Source<'a> {
// The next code unit is a low surrogate. Advance position.
self.pos += 1;
return unsafe {
::std::char::from_u32_unchecked(
::core::char::from_u32_unchecked(
(u32::from(unit) << 10) + u32::from(second)
- (((0xD800u32 << 10) - 0x10000u32) + 0xDC00u32),
)
Expand Down Expand Up @@ -1204,7 +1204,7 @@ impl<'a> Utf16Source<'a> {
// The next code unit is a low surrogate. Advance position.
self.pos += 1;
return Unicode::NonAscii(NonAscii::Astral(unsafe {
::std::char::from_u32_unchecked(
::core::char::from_u32_unchecked(
(u32::from(unit) << 10) + u32::from(second)
- (((0xD800u32 << 10) - 0x10000u32) + 0xDC00u32),
)
Expand Down Expand Up @@ -1268,7 +1268,7 @@ impl<'a> Utf16Source<'a> {
// The next code unit is a low surrogate. Advance position.
self.pos += 1;
NonAscii::Astral(unsafe {
::std::char::from_u32_unchecked(
::core::char::from_u32_unchecked(
(u32::from(unit) << 10) + u32::from(second)
- (((0xD800u32 << 10) - 0x10000u32) + 0xDC00u32),
)
Expand Down Expand Up @@ -1341,7 +1341,7 @@ impl<'a> Utf16Source<'a> {
// The next code unit is a low surrogate. Advance position.
self.pos += 1;
NonAscii::Astral(unsafe {
::std::char::from_u32_unchecked(
::core::char::from_u32_unchecked(
(u32::from(unit) << 10) + u32::from(second)
- (((0xD800u32 << 10) - 0x1_0000u32) + 0xDC00u32),
)
Expand Down Expand Up @@ -1469,21 +1469,21 @@ impl<'a> Utf8Source<'a> {
let point =
((u32::from(unit) & 0x1F) << 6) | (u32::from(self.slice[self.pos + 1]) & 0x3F);
self.pos += 2;
return unsafe { ::std::char::from_u32_unchecked(point) };
return unsafe { ::core::char::from_u32_unchecked(point) };
}
if unit < 0xF0 {
let point = ((u32::from(unit) & 0xF) << 12)
| ((u32::from(self.slice[self.pos + 1]) & 0x3F) << 6)
| (u32::from(self.slice[self.pos + 2]) & 0x3F);
self.pos += 3;
return unsafe { ::std::char::from_u32_unchecked(point) };
return unsafe { ::core::char::from_u32_unchecked(point) };
}
let point = ((u32::from(unit) & 0x7) << 18)
| ((u32::from(self.slice[self.pos + 1]) & 0x3F) << 12)
| ((u32::from(self.slice[self.pos + 2]) & 0x3F) << 6)
| (u32::from(self.slice[self.pos + 3]) & 0x3F);
self.pos += 4;
unsafe { ::std::char::from_u32_unchecked(point) }
unsafe { ::core::char::from_u32_unchecked(point) }
}
#[inline(always)]
fn read_enum(&mut self) -> Unicode {
Expand Down Expand Up @@ -1512,7 +1512,7 @@ impl<'a> Utf8Source<'a> {
| (u32::from(self.slice[self.pos + 3]) & 0x3F);
self.pos += 4;
Unicode::NonAscii(NonAscii::Astral(unsafe {
::std::char::from_u32_unchecked(point)
::core::char::from_u32_unchecked(point)
}))
}
#[inline(always)]
Expand Down Expand Up @@ -1567,7 +1567,7 @@ impl<'a> Utf8Source<'a> {
| ((u32::from(self.slice[self.pos + 2]) & 0x3F) << 6)
| (u32::from(self.slice[self.pos + 3]) & 0x3F);
self.pos += 4;
NonAscii::Astral(unsafe { ::std::char::from_u32_unchecked(point) })
NonAscii::Astral(unsafe { ::core::char::from_u32_unchecked(point) })
}
}
}
Expand Down Expand Up @@ -1617,7 +1617,7 @@ impl<'a> Utf8Source<'a> {
| ((u32::from(self.slice[self.pos + 2]) & 0x3F) << 6)
| (u32::from(self.slice[self.pos + 3]) & 0x3F);
self.pos += 4;
NonAscii::Astral(unsafe { ::std::char::from_u32_unchecked(point) })
NonAscii::Astral(unsafe { ::core::char::from_u32_unchecked(point) })
}
} else {
return CopyAsciiResult::Stop((
Expand Down Expand Up @@ -1674,7 +1674,7 @@ impl<'a> Utf8Source<'a> {
| ((u32::from(self.slice[self.pos + 2]) & 0x3F) << 6)
| (u32::from(self.slice[self.pos + 3]) & 0x3F);
self.pos += 4;
NonAscii::Astral(unsafe { ::std::char::from_u32_unchecked(point) })
NonAscii::Astral(unsafe { ::core::char::from_u32_unchecked(point) })
}
} else {
return CopyAsciiResult::Stop((
Expand Down
Loading