-
-
Notifications
You must be signed in to change notification settings - Fork 671
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
Change TokenFilter trait to simplify (a bit) the boxing of filters #2101
Changes from 7 commits
ad9b825
6c6b97d
54f4313
b82cd08
dc783f8
2cab111
f6a6b4a
f777de1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,24 @@ | ||
use std::mem; | ||
|
||
use super::{Token, TokenFilter, TokenStream, Tokenizer}; | ||
use super::{Token, TokenFilter, TokenStream}; | ||
|
||
/// Token filter that lowercase terms. | ||
#[derive(Clone)] | ||
pub struct LowerCaser; | ||
|
||
impl TokenFilter for LowerCaser { | ||
type Tokenizer<T: Tokenizer> = LowerCaserFilter<T>; | ||
type OutputTokenStream<T: TokenStream> = LowerCaserTokenStream<T>; | ||
|
||
fn transform<T: Tokenizer>(self, tokenizer: T) -> Self::Tokenizer<T> { | ||
LowerCaserFilter { | ||
tokenizer, | ||
fn filter<T: TokenStream>(&self, token_stream: T) -> Self::OutputTokenStream<T> { | ||
LowerCaserTokenStream { | ||
tail: token_stream, | ||
buffer: String::new(), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you are reverting here the work done by Pascal to reduce the number of allocation we have. Is this necessary? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @PSeitz can you confirm? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes indeed :/ On each That was not the case before. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd prefer if we can have an instance with a buffer in the pipeline like before There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, of course. I should have fixed it by adding the allocation at the |
||
} | ||
} | ||
} | ||
|
||
#[derive(Clone)] | ||
pub struct LowerCaserFilter<T> { | ||
tokenizer: T, | ||
pub struct LowerCaserTokenStream<T> { | ||
buffer: String, | ||
} | ||
|
||
impl<T: Tokenizer> Tokenizer for LowerCaserFilter<T> { | ||
type TokenStream<'a> = LowerCaserTokenStream<'a, T::TokenStream<'a>>; | ||
|
||
fn token_stream<'a>(&'a mut self, text: &'a str) -> Self::TokenStream<'a> { | ||
self.buffer.clear(); | ||
LowerCaserTokenStream { | ||
tail: self.tokenizer.token_stream(text), | ||
buffer: &mut self.buffer, | ||
} | ||
} | ||
} | ||
|
||
pub struct LowerCaserTokenStream<'a, T> { | ||
buffer: &'a mut String, | ||
tail: T, | ||
} | ||
|
||
|
@@ -51,7 +33,7 @@ fn to_lowercase_unicode(text: &str, output: &mut String) { | |
} | ||
} | ||
|
||
impl<'a, T: TokenStream> TokenStream for LowerCaserTokenStream<'a, T> { | ||
impl<T: TokenStream> TokenStream for LowerCaserTokenStream<T> { | ||
fn advance(&mut self) -> bool { | ||
if !self.tail.advance() { | ||
return false; | ||
|
@@ -60,8 +42,8 @@ impl<'a, T: TokenStream> TokenStream for LowerCaserTokenStream<'a, T> { | |
// fast track for ascii. | ||
self.token_mut().text.make_ascii_lowercase(); | ||
} else { | ||
to_lowercase_unicode(&self.tail.token().text, self.buffer); | ||
mem::swap(&mut self.tail.token_mut().text, self.buffer); | ||
to_lowercase_unicode(&self.tail.token().text, &mut self.buffer); | ||
mem::swap(&mut self.tail.token_mut().text, &mut self.buffer); | ||
} | ||
true | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
clippy...