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

Refactor to use more idiomatic Rust patterns #14

Merged
merged 7 commits into from
Jul 6, 2023
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
28 changes: 13 additions & 15 deletions core/src/codepoints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ pub(crate) const CASE_SENSITIVE_CODEPOINTS_COUNT: u16 =
pub(crate) const CASE_SENSITIVE_CODEPOINTS_OFFSET: u16 = read_u16_le(CODEPOINTS);
pub(crate) const CODEPOINTS_COUNT: u16 = ((CASE_SENSITIVE_CODEPOINTS_OFFSET - 6) / 5) - 1;

const CODEPOINT_MASK: u32 = 0x1fffff;
const RANGE_MASK: u32 = 0x20000000;
const STRING_TRANSLATION_MASK: u32 = 0x40000000;
const CODEPOINT_MASK: u32 = 0x001f_ffff;
const RANGE_MASK: u32 = 0x2000_0000;
const STRING_TRANSLATION_MASK: u32 = 0x4000_0000;

pub(crate) struct Codepoint(u32, u8);

Expand All @@ -29,29 +29,25 @@ impl Codepoint {
}

pub(crate) const fn matches(&self, other: u32) -> Ordering {
let conf: u32 = self.0 & CODEPOINT_MASK;
let mut conf: u32 = self.0 & CODEPOINT_MASK;

if other < conf {
return Ordering::Less;
}

let mut max = conf;

if (self.0 & RANGE_MASK) != 0 {
max += (self.1 & 0x7f) as u32;
conf += (self.1 & 0x7f) as u32;
}

if other > max {
Ordering::Greater
} else {
Ordering::Equal
if other > conf {
return Ordering::Greater;
}

Ordering::Equal
}

pub(crate) const fn translation(&self, other: u32) -> Translation {
if (self.0 & STRING_TRANSLATION_MASK) != 0 {
Translation::string(self.0, self.1)
} else {
if (self.0 & STRING_TRANSLATION_MASK) == 0 {
let mut code = (self.0 >> 21) & 0xff;

if code == 0 {
Expand All @@ -60,7 +56,9 @@ impl Codepoint {
code += other - (self.0 & CODEPOINT_MASK);
}

Translation::character(code)
return Translation::character(code);
}

Translation::string(self.0, self.1)
}
}
11 changes: 4 additions & 7 deletions core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const fn translate(code: u32, offset: i32, mut end: i32) -> Option<Translation>
match codepoint.matches(code) {
Ordering::Equal => return Some(codepoint.translation(code)),
Ordering::Greater => start = mid + 1,
_ => end = mid - 1,
Ordering::Less => end = mid - 1,
};
}

Expand Down Expand Up @@ -71,13 +71,10 @@ const fn translate(code: u32, offset: i32, mut end: i32) -> Option<Translation>
///
/// assert!(matches!(cured_surrogate, Translation::None));
/// ```
pub fn cure_char<C>(code: C) -> Translation
where
C: Into<u32>,
{
pub fn cure_char<C: Into<u32>>(code: C) -> Translation {
let code = code.into();

if code <= 31 || code == 127 || (0xd800..=0xf8ff).contains(&code) || code >= 0xe0100 {
if matches!(code, 0..=31 | 127 | 0xd800..=0xf8ff | 0xe0100..) {
return Translation::None;
}

Expand All @@ -101,7 +98,7 @@ where
}

translate(code_lowercased, 6, CODEPOINTS_COUNT as _)
.unwrap_or(Translation::character(code_lowercased))
.unwrap_or_else(|| Translation::character(code_lowercased))
}

/// Cures a string.
Expand Down
72 changes: 36 additions & 36 deletions core/src/similar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,45 +6,45 @@ pub(crate) const SIMILAR_END: u16 = read_u16_le(unsafe { CODEPOINTS.offset(4) })
pub(crate) fn is(self_char: u32, other_char: char) -> bool {
let other_char = unsafe { other_char.to_lowercase().next().unwrap_unchecked() as u32 };

if self_char != other_char {
if self_char <= 0x7f && other_char <= 0x7f {
let mut offset = SIMILAR_START;
let mut contains_a = false;
let mut contains_b = false;

loop {
let cur = unsafe { *(CODEPOINTS.offset(offset as _)) };
let sim = cur & 0x7f;

if sim == (self_char as u8) {
contains_a = true;
}

if sim == (other_char as u8) {
contains_b = true;
}

if contains_a && contains_b {
return true;
}

if cur >= 0x80 {
contains_a = false;
contains_b = false;
}

offset += 1;

if offset == SIMILAR_END {
return false;
}
if self_char == other_char {
return true;
}

if self_char <= 0x7f && other_char <= 0x7f {
let mut offset = SIMILAR_START;
let mut contains_a = false;
let mut contains_b = false;

loop {
let cur = unsafe { *(CODEPOINTS.offset(offset as _)) };
let sim = cur & 0x7f;

if sim == (self_char as u8) {
contains_a = true;
}

if sim == (other_char as u8) {
contains_b = true;
}
}

false
} else {
true
if contains_a && contains_b {
return true;
}

if cur >= 0x80 {
contains_a = false;
contains_b = false;
}

offset += 1;

if offset == SIMILAR_END {
return false;
}
}
}

false
}

#[inline(always)]
Expand Down
2 changes: 1 addition & 1 deletion core/src/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ impl Extend<CuredString> for String {
where
I: IntoIterator<Item = CuredString>,
{
self.extend(iter.into_iter().map(|s| s.into_str()))
self.extend(iter.into_iter().map(CuredString::into_str));
}
}

Expand Down
6 changes: 3 additions & 3 deletions core/src/translation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ where
}

Self::String(s) => s.len() == o.len() && similar::is_str(s, o),
_ => o.is_empty(),
Self::None => o.is_empty(),
}
}
}
Expand All @@ -90,7 +90,7 @@ impl Display for Translation {
match self {
Self::Character(ch) => Display::fmt(ch, f),
Self::String(s) => Display::fmt(s, f),
_ => Ok(()),
Self::None => Ok(()),
}
}
}
Expand All @@ -107,7 +107,7 @@ impl Extend<Translation> for String {
match part {
Translation::Character(c) => self.push(c),
Translation::String(s) => self.push_str(s),
_ => {}
Translation::None => {}
}
}
}
Expand Down