Skip to content
Open
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
8 changes: 4 additions & 4 deletions matcher/src/chars.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ pub(crate) trait Char: Copy + Eq + Ord + fmt::Display {
pub(crate) struct AsciiChar(pub u8);

impl AsciiChar {
pub fn cast(bytes: &[u8]) -> &[AsciiChar] {
unsafe { &*(bytes as *const [u8] as *const [AsciiChar]) }
pub fn cast(bytes: &[u8]) -> &[Self] {
unsafe { &*(bytes as *const [u8] as *const [Self]) }
}
}

Expand All @@ -40,7 +40,7 @@ impl fmt::Display for AsciiChar {

impl PartialEq<AsciiChar> for char {
fn eq(&self, other: &AsciiChar) -> bool {
other.0 as char == *self
other.0 as Self == *self
}
}

Expand Down Expand Up @@ -111,7 +111,7 @@ impl Char for char {
fn char_class_and_normalize(mut self, config: &Config) -> (Self, CharClass) {
if self.is_ascii() {
let (c, class) = AsciiChar(self as u8).char_class_and_normalize(config);
return (c.0 as char, class);
return (c.0 as Self, class);
}
let char_class = char_class_non_ascii(self);
#[cfg(feature = "unicode-casefold")]
Expand Down
2 changes: 1 addition & 1 deletion matcher/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ impl Config {
/// The default config for nucleo, implemented as a constant since
/// Default::default can not be called in a const context
pub const DEFAULT: Self = {
Config {
Self {
delimiter_chars: b"/,:;|",
bonus_boundary_white: BONUS_BOUNDARY + 2,
bonus_boundary_delimiter: BONUS_BOUNDARY + 1,
Expand Down
4 changes: 2 additions & 2 deletions matcher/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ pub struct Matcher {
// this is just here for convenience not sure if we should implement this
impl Clone for Matcher {
fn clone(&self) -> Self {
Matcher {
Self {
config: self.config.clone(),
slab: MatrixSlab::new(),
}
Expand All @@ -175,7 +175,7 @@ impl std::fmt::Debug for Matcher {

impl Default for Matcher {
fn default() -> Self {
Matcher {
Self {
config: Config::DEFAULT,
slab: MatrixSlab::new(),
}
Expand Down
6 changes: 3 additions & 3 deletions matcher/src/matrix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ struct MatrixLayout<C: Char> {
_phantom: PhantomData<C>,
}
impl<C: Char> MatrixLayout<C> {
fn new(haystack_len: usize, needle_len: usize) -> MatrixLayout<C> {
fn new(haystack_len: usize, needle_len: usize) -> Self {
assert!(haystack_len >= needle_len);
assert!(haystack_len <= u32::MAX as usize);
let mut layout = Layout::from_size_align(0, 1).unwrap();
Expand All @@ -45,7 +45,7 @@ impl<C: Char> MatrixLayout<C> {
(layout, score_off) = layout.extend(score_layout).unwrap();
let matrix_off;
(layout, matrix_off) = layout.extend(matrix_layout).unwrap();
MatrixLayout {
Self {
haystack_len,
needle_len,
layout,
Expand Down Expand Up @@ -151,7 +151,7 @@ impl MatrixSlab {
let Some(ptr) = NonNull::new(ptr) else {
handle_alloc_error(layout)
};
MatrixSlab(ptr.cast())
Self(ptr.cast())
}

pub(crate) fn alloc<C: Char>(
Expand Down
20 changes: 10 additions & 10 deletions matcher/src/pattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ impl Atom {
normalize: Normalization,
kind: AtomKind,
escape_whitespace: bool,
) -> Atom {
Atom::new_inner(needle, case, normalize, kind, escape_whitespace, false)
) -> Self {
Self::new_inner(needle, case, normalize, kind, escape_whitespace, false)
}

fn new_inner(
Expand All @@ -112,7 +112,7 @@ impl Atom {
kind: AtomKind,
escape_whitespace: bool,
append_dollar: bool,
) -> Atom {
) -> Self {
let mut ignore_case;
let mut normalize;
#[cfg(feature = "unicode-normalization")]
Expand Down Expand Up @@ -227,7 +227,7 @@ impl Atom {
}
Utf32String::Unicode(needle_.into_boxed_slice())
};
Atom {
Self {
kind,
needle,
negative: false,
Expand All @@ -239,7 +239,7 @@ impl Atom {
/// Parse a pattern atom from a string. Some special trailing and leading
/// characters can be used to control the atom kind. See [`AtomKind`] for
/// details.
pub fn parse(raw: &str, case: CaseMatching, normalize: Normalization) -> Atom {
pub fn parse(raw: &str, case: CaseMatching, normalize: Normalization) -> Self {
let mut atom = raw;
let invert = match atom.as_bytes() {
[b'!', ..] => {
Expand Down Expand Up @@ -290,7 +290,7 @@ impl Atom {
kind = AtomKind::Substring
}

let mut pattern = Atom::new_inner(atom, case, normalize, kind, true, append_dollar);
let mut pattern = Self::new_inner(atom, case, normalize, kind, true, append_dollar);
pattern.negative = invert;
pattern
}
Expand Down Expand Up @@ -429,27 +429,27 @@ impl Pattern {
case_matching: CaseMatching,
normalize: Normalization,
kind: AtomKind,
) -> Pattern {
) -> Self {
let atoms = pattern_atoms(pattern)
.filter_map(|pat| {
let pat = Atom::new(pat, case_matching, normalize, kind, true);
(!pat.needle.is_empty()).then_some(pat)
})
.collect();
Pattern { atoms }
Self { atoms }
}
/// Creates a pattern where each word is matched individually (whitespaces
/// can be escaped with `\`). And `$`, `!`, `'` and `^` at word boundaries will
/// cause different matching behaviour (see [`AtomKind`]). These can be
/// escaped with backslash.
pub fn parse(pattern: &str, case_matching: CaseMatching, normalize: Normalization) -> Pattern {
pub fn parse(pattern: &str, case_matching: CaseMatching, normalize: Normalization) -> Self {
let atoms = pattern_atoms(pattern)
.filter_map(|pat| {
let pat = Atom::parse(pat, case_matching, normalize);
(!pat.needle.is_empty()).then_some(pat)
})
.collect();
Pattern { atoms }
Self { atoms }
}

/// Convenience function to easily match (and sort) a (relatively small)
Expand Down
66 changes: 32 additions & 34 deletions matcher/src/utf32_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,36 +105,36 @@ impl<'a> Utf32Str<'a> {
/// Convenience method to construct a `Utf32Str` from a normal UTF-8 str
pub fn new(str: &'a str, buf: &'a mut Vec<char>) -> Self {
if has_ascii_graphemes(str) {
Utf32Str::Ascii(str.as_bytes())
Self::Ascii(str.as_bytes())
} else {
buf.clear();
buf.extend(crate::chars::graphemes(str));
Utf32Str::Unicode(buf)
Self::Unicode(buf)
}
}

/// Returns the number of characters in this string.
#[inline]
pub fn len(self) -> usize {
match self {
Utf32Str::Unicode(codepoints) => codepoints.len(),
Utf32Str::Ascii(ascii_bytes) => ascii_bytes.len(),
Self::Unicode(codepoints) => codepoints.len(),
Self::Ascii(ascii_bytes) => ascii_bytes.len(),
}
}

/// Returns whether this string is empty.
#[inline]
pub fn is_empty(self) -> bool {
match self {
Utf32Str::Unicode(codepoints) => codepoints.is_empty(),
Utf32Str::Ascii(ascii_bytes) => ascii_bytes.is_empty(),
Self::Unicode(codepoints) => codepoints.is_empty(),
Self::Ascii(ascii_bytes) => ascii_bytes.is_empty(),
}
}

/// Creates a slice with a string that contains the characters in
/// the specified **character range**.
#[inline]
pub fn slice(self, range: impl RangeBounds<usize>) -> Utf32Str<'a> {
pub fn slice(self, range: impl RangeBounds<usize>) -> Self {
let start = match range.start_bound() {
Bound::Included(&start) => start,
Bound::Excluded(&start) => start + 1,
Expand All @@ -146,20 +146,20 @@ impl<'a> Utf32Str<'a> {
Bound::Unbounded => self.len(),
};
match self {
Utf32Str::Ascii(bytes) => Utf32Str::Ascii(&bytes[start..end]),
Utf32Str::Unicode(codepoints) => Utf32Str::Unicode(&codepoints[start..end]),
Self::Ascii(bytes) => Self::Ascii(&bytes[start..end]),
Self::Unicode(codepoints) => Self::Unicode(&codepoints[start..end]),
}
}

/// Returns the number of leading whitespaces in this string
#[inline]
pub(crate) fn leading_white_space(self) -> usize {
match self {
Utf32Str::Ascii(bytes) => bytes
Self::Ascii(bytes) => bytes
.iter()
.position(|b| !b.is_ascii_whitespace())
.unwrap_or(0),
Utf32Str::Unicode(codepoints) => codepoints
Self::Unicode(codepoints) => codepoints
.iter()
.position(|c| !c.is_whitespace())
.unwrap_or(0),
Expand All @@ -170,12 +170,12 @@ impl<'a> Utf32Str<'a> {
#[inline]
pub(crate) fn trailing_white_space(self) -> usize {
match self {
Utf32Str::Ascii(bytes) => bytes
Self::Ascii(bytes) => bytes
.iter()
.rev()
.position(|b| !b.is_ascii_whitespace())
.unwrap_or(0),
Utf32Str::Unicode(codepoints) => codepoints
Self::Unicode(codepoints) => codepoints
.iter()
.rev()
.position(|c| !c.is_whitespace())
Expand All @@ -186,7 +186,7 @@ impl<'a> Utf32Str<'a> {
/// Same as `slice` but accepts a u32 range for convenience since
/// those are the indices returned by the matcher.
#[inline]
pub fn slice_u32(self, range: impl RangeBounds<u32>) -> Utf32Str<'a> {
pub fn slice_u32(self, range: impl RangeBounds<u32>) -> Self {
let start = match range.start_bound() {
Bound::Included(&start) => start as usize,
Bound::Excluded(&start) => start as usize + 1,
Expand All @@ -198,8 +198,8 @@ impl<'a> Utf32Str<'a> {
Bound::Unbounded => self.len(),
};
match self {
Utf32Str::Ascii(bytes) => Utf32Str::Ascii(&bytes[start..end]),
Utf32Str::Unicode(codepoints) => Utf32Str::Unicode(&codepoints[start..end]),
Self::Ascii(bytes) => Self::Ascii(&bytes[start..end]),
Self::Unicode(codepoints) => Self::Unicode(&codepoints[start..end]),
}
}

Expand All @@ -215,8 +215,8 @@ impl<'a> Utf32Str<'a> {
/// Returns the `n`th character in this string, zero-indexed
pub fn get(self, n: u32) -> char {
match self {
Utf32Str::Ascii(bytes) => bytes[n as usize] as char,
Utf32Str::Unicode(codepoints) => codepoints[n as usize],
Self::Ascii(bytes) => bytes[n as usize] as char,
Self::Unicode(codepoints) => codepoints[n as usize],
}
}

Expand All @@ -225,8 +225,8 @@ impl<'a> Utf32Str<'a> {
/// Panics if the string is empty.
pub(crate) fn last(self) -> char {
match self {
Utf32Str::Ascii(bytes) => bytes[bytes.len() - 1] as char,
Utf32Str::Unicode(codepoints) => codepoints[codepoints.len() - 1],
Self::Ascii(bytes) => bytes[bytes.len() - 1] as char,
Self::Unicode(codepoints) => codepoints[codepoints.len() - 1],
}
}

Expand All @@ -235,16 +235,16 @@ impl<'a> Utf32Str<'a> {
/// Panics if the string is empty.
pub(crate) fn first(self) -> char {
match self {
Utf32Str::Ascii(bytes) => bytes[0] as char,
Utf32Str::Unicode(codepoints) => codepoints[0],
Self::Ascii(bytes) => bytes[0] as char,
Self::Unicode(codepoints) => codepoints[0],
}
}

/// Returns an iterator over the characters in this string
pub fn chars(self) -> Chars<'a> {
match self {
Utf32Str::Ascii(bytes) => Chars::Ascii(bytes.iter()),
Utf32Str::Unicode(codepoints) => Chars::Unicode(codepoints.iter()),
Self::Ascii(bytes) => Chars::Ascii(bytes.iter()),
Self::Unicode(codepoints) => Chars::Unicode(codepoints.iter()),
}
}
}
Expand Down Expand Up @@ -318,17 +318,17 @@ impl Utf32String {
#[inline]
pub fn len(&self) -> usize {
match self {
Utf32String::Unicode(codepoints) => codepoints.len(),
Utf32String::Ascii(ascii_bytes) => ascii_bytes.len(),
Self::Unicode(codepoints) => codepoints.len(),
Self::Ascii(ascii_bytes) => ascii_bytes.len(),
}
}

/// Returns whether this string is empty.
#[inline]
pub fn is_empty(&self) -> bool {
match self {
Utf32String::Unicode(codepoints) => codepoints.is_empty(),
Utf32String::Ascii(ascii_bytes) => ascii_bytes.is_empty(),
Self::Unicode(codepoints) => codepoints.is_empty(),
Self::Ascii(ascii_bytes) => ascii_bytes.is_empty(),
}
}

Expand All @@ -347,8 +347,8 @@ impl Utf32String {
Bound::Unbounded => self.len(),
};
match self {
Utf32String::Ascii(bytes) => Utf32Str::Ascii(&bytes.as_bytes()[start..end]),
Utf32String::Unicode(codepoints) => Utf32Str::Unicode(&codepoints[start..end]),
Self::Ascii(bytes) => Utf32Str::Ascii(&bytes.as_bytes()[start..end]),
Self::Unicode(codepoints) => Utf32Str::Unicode(&codepoints[start..end]),
}
}

Expand All @@ -367,10 +367,8 @@ impl Utf32String {
Bound::Unbounded => self.len() as u32,
};
match self {
Utf32String::Ascii(bytes) => {
Utf32Str::Ascii(&bytes.as_bytes()[start as usize..end as usize])
}
Utf32String::Unicode(codepoints) => {
Self::Ascii(bytes) => Utf32Str::Ascii(&bytes.as_bytes()[start as usize..end as usize]),
Self::Unicode(codepoints) => {
Utf32Str::Unicode(&codepoints[start as usize..end as usize])
}
}
Expand Down
Loading