Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Joshix-1 committed Oct 15, 2024
1 parent 335331b commit 7336e0e
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 33 deletions.
16 changes: 5 additions & 11 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ fn write_words_data(words_data: &WordsData) {

let mut max_string_lit_len: usize = 0;

let mut output = String::from("match length.get() {");
let mut output = String::from("match length {");
for chunk in
words.chunk_by(|(length_a, _), (length_b, _)| *length_a == *length_b)
{
Expand Down Expand Up @@ -237,16 +237,10 @@ pub enum Language {{
impl Language {{
#[must_use]
pub const fn read_words(self, length: usize) -> WordSequence {{
match NonZeroUsize::new(length) {{
None => EMPTY_WORD_SEQUENCE,
Some(length) => {{
let (padded_length, words): (NonZeroUsize, &'static str) = match self {{
{}
}};
WordSequence::new(length, words, padded_length)
}},
}}
let (padded_length, words): (NonZeroUsize, &'static str) = match self {{
{}
}};
WordSequence::new(length, words, padded_length)
}}
#[inline]
Expand Down
2 changes: 1 addition & 1 deletion src/language/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use pyo3::exceptions::PyValueError;
#[cfg(feature = "pyo3")]
use pyo3::prelude::*;

pub use word_sequence::{WordSequence, EMPTY_WORD_SEQUENCE};
pub use word_sequence::WordSequence;

#[cfg_attr(feature = "pyo3", pyclass)]
pub struct StringChunkIter {
Expand Down
49 changes: 28 additions & 21 deletions src/language/word_sequence.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// SPDX-License-Identifier: EUPL-1.2
use std::num::NonZeroUsize;
use std::ops::Div;

#[cfg(feature = "pyo3")]
use pyo3::prelude::*;
Expand All @@ -9,15 +8,23 @@ use std::hash::{DefaultHasher, Hasher};

use super::StringChunkIter;

pub const EMPTY_WORD_SEQUENCE: WordSequence = WordSequence {
word_length: NonZeroUsize::MIN,
#[allow(dead_code)]
const EMPTY_WORD_SEQUENCE: WordSequence = WordSequence {
word_length: 0,
padded_word_byte_count: NonZeroUsize::MIN,
data: "",
};

const _: () = assert!(EMPTY_WORD_SEQUENCE.len() == 0);
const _: () = assert!(EMPTY_WORD_SEQUENCE.word_char_count() == 0);
const _: () = assert!(EMPTY_WORD_SEQUENCE.is_empty());
#[cfg(feature = "pyo3")]
const _: () =
assert!(EMPTY_WORD_SEQUENCE.const_convert_to_iter().__len__() == 0);

#[cfg_attr(feature = "pyo3", pyclass(frozen))]
pub struct WordSequence {
word_length: NonZeroUsize,
word_length: usize,
data: &'static str,
padded_word_byte_count: NonZeroUsize,
}
Expand All @@ -26,7 +33,7 @@ impl WordSequence {
#[inline]
#[must_use]
pub(crate) const fn new(
word_length: NonZeroUsize,
word_length: usize,
data: &'static str,
padded_word_byte_count: NonZeroUsize,
) -> Self {
Expand All @@ -40,13 +47,13 @@ impl WordSequence {
#[inline]
#[must_use]
pub const fn word_char_count(&self) -> usize {
self.word_length.get()
self.word_length
}

#[inline]
#[must_use]
pub fn len(&self) -> usize {
self.data.len().div(self.padded_word_byte_count)
pub const fn len(&self) -> usize {
self.data.len() / self.padded_word_byte_count.get()
}

#[inline]
Expand All @@ -58,7 +65,7 @@ impl WordSequence {
#[inline]
#[must_use]
pub fn contains(&self, word: &str) -> bool {
if word.chars().count() == self.word_length.get() {
if word.chars().count() == self.word_length {
self.iter().any(|w| w == word)
} else {
false
Expand All @@ -70,6 +77,16 @@ impl WordSequence {
pub fn iter(&self) -> StringChunkIter {
self.into_iter()
}

#[inline]
const fn const_convert_to_iter(&self) -> StringChunkIter {
StringChunkIter {
index: 0,
is_ascii: self.word_length == self.padded_word_byte_count.get(),
padded_word_byte_count: self.padded_word_byte_count,
string: self.data,
}
}
}

impl IntoIterator for WordSequence {
Expand All @@ -80,12 +97,7 @@ impl IntoIterator for WordSequence {
#[inline]
#[must_use]
fn into_iter(self) -> Self::IntoIter {
StringChunkIter {
index: 0,
padded_word_byte_count: self.padded_word_byte_count,
string: self.data,
is_ascii: self.padded_word_byte_count == self.word_length,
}
self.const_convert_to_iter()
}
}

Expand All @@ -96,12 +108,7 @@ impl IntoIterator for &WordSequence {

#[inline]
fn into_iter(self) -> Self::IntoIter {
StringChunkIter {
index: 0,
is_ascii: self.word_length == self.padded_word_byte_count,
padded_word_byte_count: self.padded_word_byte_count,
string: self.data,
}
self.const_convert_to_iter()
}
}

Expand Down

0 comments on commit 7336e0e

Please sign in to comment.