From 8ffed1822bcbc1b6ce6647b840fb03996b0635ea Mon Sep 17 00:00:00 2001 From: Titus Wormer Date: Tue, 2 Aug 2022 14:27:31 +0200 Subject: Refactor to remove most closures --- src/construct/autolink.rs | 45 ++++++++++++++++++++++++++++++--------------- 1 file changed, 30 insertions(+), 15 deletions(-) (limited to 'src/construct/autolink.rs') diff --git a/src/construct/autolink.rs b/src/construct/autolink.rs index bac291e..1444c61 100644 --- a/src/construct/autolink.rs +++ b/src/construct/autolink.rs @@ -158,7 +158,9 @@ fn scheme_or_email_atext(tokenizer: &mut Tokenizer) -> State { match tokenizer.current { // ASCII alphanumeric and `+`, `-`, and `.`. Some(b'+' | b'-' | b'.' | b'0'..=b'9' | b'A'..=b'Z' | b'a'..=b'z') => { - scheme_inside_or_email_atext(tokenizer, 1) + // Count the previous alphabetical from `open` too. + tokenizer.tokenize_state.size = 1; + scheme_inside_or_email_atext(tokenizer) } _ => email_atext(tokenizer), } @@ -172,20 +174,25 @@ fn scheme_or_email_atext(tokenizer: &mut Tokenizer) -> State { /// > | ab /// ^ /// ``` -fn scheme_inside_or_email_atext(tokenizer: &mut Tokenizer, size: usize) -> State { +fn scheme_inside_or_email_atext(tokenizer: &mut Tokenizer) -> State { match tokenizer.current { Some(b':') => { tokenizer.consume(); + tokenizer.tokenize_state.size = 0; State::Fn(Box::new(url_inside)) } // ASCII alphanumeric and `+`, `-`, and `.`. Some(b'+' | b'-' | b'.' | b'0'..=b'9' | b'A'..=b'Z' | b'a'..=b'z') - if size < AUTOLINK_SCHEME_SIZE_MAX => + if tokenizer.tokenize_state.size < AUTOLINK_SCHEME_SIZE_MAX => { + tokenizer.tokenize_state.size += 1; tokenizer.consume(); - State::Fn(Box::new(move |t| scheme_inside_or_email_atext(t, size + 1))) + State::Fn(Box::new(scheme_inside_or_email_atext)) + } + _ => { + tokenizer.tokenize_state.size = 0; + email_atext(tokenizer) } - _ => email_atext(tokenizer), } } @@ -220,7 +227,7 @@ fn email_atext(tokenizer: &mut Tokenizer) -> State { match tokenizer.current { Some(b'@') => { tokenizer.consume(); - State::Fn(Box::new(|t| email_at_sign_or_dot(t, 0))) + State::Fn(Box::new(email_at_sign_or_dot)) } // ASCII atext. // @@ -255,10 +262,10 @@ fn email_atext(tokenizer: &mut Tokenizer) -> State { /// > | ab /// ^ ^ /// ``` -fn email_at_sign_or_dot(tokenizer: &mut Tokenizer, size: usize) -> State { +fn email_at_sign_or_dot(tokenizer: &mut Tokenizer) -> State { match tokenizer.current { // ASCII alphanumeric. - Some(b'0'..=b'9' | b'A'..=b'Z' | b'a'..=b'z') => email_value(tokenizer, size), + Some(b'0'..=b'9' | b'A'..=b'Z' | b'a'..=b'z') => email_value(tokenizer), _ => State::Nok, } } @@ -269,13 +276,15 @@ fn email_at_sign_or_dot(tokenizer: &mut Tokenizer, size: usize) -> State { /// > | ab /// ^ /// ``` -fn email_label(tokenizer: &mut Tokenizer, size: usize) -> State { +fn email_label(tokenizer: &mut Tokenizer) -> State { match tokenizer.current { Some(b'.') => { + tokenizer.tokenize_state.size = 0; tokenizer.consume(); - State::Fn(Box::new(|t| email_at_sign_or_dot(t, 0))) + State::Fn(Box::new(email_at_sign_or_dot)) } Some(b'>') => { + tokenizer.tokenize_state.size = 0; let index = tokenizer.events.len(); tokenizer.exit(Token::AutolinkProtocol); // Change the token type. @@ -283,7 +292,7 @@ fn email_label(tokenizer: &mut Tokenizer, size: usize) -> State { tokenizer.events[index].token_type = Token::AutolinkEmail; end(tokenizer) } - _ => email_value(tokenizer, size), + _ => email_value(tokenizer), } } @@ -295,19 +304,25 @@ fn email_label(tokenizer: &mut Tokenizer, size: usize) -> State { /// > | ab /// ^ /// ``` -fn email_value(tokenizer: &mut Tokenizer, size: usize) -> State { +fn email_value(tokenizer: &mut Tokenizer) -> State { match tokenizer.current { // ASCII alphanumeric or `-`. - Some(b'-' | b'0'..=b'9' | b'A'..=b'Z' | b'a'..=b'z') if size < AUTOLINK_DOMAIN_SIZE_MAX => { + Some(b'-' | b'0'..=b'9' | b'A'..=b'Z' | b'a'..=b'z') + if tokenizer.tokenize_state.size < AUTOLINK_DOMAIN_SIZE_MAX => + { let func = if matches!(tokenizer.current, Some(b'-')) { email_value } else { email_label }; + tokenizer.tokenize_state.size += 1; tokenizer.consume(); - State::Fn(Box::new(move |t| func(t, size + 1))) + State::Fn(Box::new(func)) + } + _ => { + tokenizer.tokenize_state.size = 0; + State::Nok } - _ => State::Nok, } } -- cgit