Skip to content

Commit

Permalink
refactor: remove some unsafe (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
null8626 committed Oct 28, 2024
1 parent f8c1a6e commit a709397
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 20 deletions.
Binary file modified bindings/java/bin/bindings.zip
Binary file not shown.
Binary file modified bindings/wasm/bin/decancer.wasm
Binary file not shown.
8 changes: 4 additions & 4 deletions bindings/wasm/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![allow(non_snake_case, dead_code)]
#![allow(non_snake_case)]

use std::{convert::AsRef, mem::transmute, ops::Range};
use std::{convert::AsRef, ops::Range};
use wasm_bindgen::prelude::*;

#[wasm_bindgen]
Expand All @@ -27,7 +27,7 @@ impl CuredString {
Match {
start: mat.start,
end: mat.end,
portion: String::from(unsafe { self.0.get_unchecked(mat) }),
portion: String::from(&self.0[mat]),
}
}

Expand Down Expand Up @@ -83,7 +83,7 @@ impl CuredString {

#[wasm_bindgen]
pub fn cure(input: &str, options: u32) -> Result<CuredString, JsError> {
match decancer::cure(input, unsafe { transmute(options) }) {
match decancer::cure(input, options.into()) {
Ok(output) => Ok(CuredString(output)),
Err(err) => Err(JsError::new(<decancer::Error as AsRef<str>>::as_ref(&err))),
}
Expand Down
1 change: 1 addition & 0 deletions core/build.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
fn main() {
println!("cargo:rerun-if-changed=bin/bidi.bin");
println!("cargo:rerun-if-changed=bin/codepoints.bin");
}
14 changes: 10 additions & 4 deletions core/src/options.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use crate::{codepoints::Codepoint, Translation};
use paste::paste;
use std::cmp::Ordering;
#[cfg(feature = "options")]
use std::mem::transmute;

/// A configuration struct where you can customize decancer's behavior.
///
Expand Down Expand Up @@ -126,11 +124,10 @@ impl Options {
}

#[cfg(feature = "options")]
#[allow(clippy::transmute_int_to_bool)]
pub(crate) const fn refuse_cure(self, attributes: u8) -> bool {
let locale = attributes >> 1;

(unsafe { transmute(attributes & 1) } && self.is(2)) || (locale > 2 && self.is(locale))
((attributes & 1) != 0 && self.is(2)) || (locale > 2 && self.is(locale))
}

pub(crate) const fn translate(self, code: u32, offset: i32, mut end: i32) -> Option<Translation> {
Expand Down Expand Up @@ -160,3 +157,12 @@ impl Options {
None
}
}

#[doc(hidden)]
#[cfg(feature = "options")]
impl From<u32> for Options {
#[inline(always)]
fn from(value: u32) -> Self {
Self(value)
}
}
14 changes: 2 additions & 12 deletions core/src/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use crate::{
use serde::{de, Deserialize, Deserializer, Serialize, Serializer};
use std::{
fmt::{self, Debug, Display, Formatter},
mem::transmute,
ops::{Deref, Range},
};

Expand All @@ -18,12 +17,6 @@ use std::{
pub struct CuredString(pub(crate) String);

impl CuredString {
#[deprecated(since = "3.1.2", note = "use .into() instead")]
#[inline(always)]
pub fn into_str(self) -> String {
self.into()
}

/// Iterates throughout this string and yields every similar-looking match.
///
/// If you plan on using this method with an array of strings, use [`find_multiple`][CuredString::find_multiple].
Expand Down Expand Up @@ -78,9 +71,7 @@ impl CuredString {

for mat in matches {
// SAFETY: mat is always within the mat of self
let chars = unsafe { original.get_unchecked(mat.clone()) }
.chars()
.count();
let chars = original[mat.clone()].chars().count();
let mut with_str = String::with_capacity(chars);

for _ in 0..chars {
Expand Down Expand Up @@ -224,8 +215,7 @@ impl CuredString {
impl From<CuredString> for String {
#[inline(always)]
fn from(val: CuredString) -> Self {
// SAFETY: see definition of CuredString
unsafe { transmute(val) }
val.0
}
}

Expand Down

0 comments on commit a709397

Please sign in to comment.