Skip to content

Commit

Permalink
reduce number of stacks required by inlining ihier part
Browse files Browse the repository at this point in the history
  • Loading branch information
farooqkz committed Mar 16, 2024
1 parent 1ee692c commit d0e9ace
Showing 1 changed file with 21 additions and 35 deletions.
56 changes: 21 additions & 35 deletions src/parser/link_url.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,10 +192,6 @@ fn is_scheme(c: char) -> bool {
is_alpha(c) || is_digit(c) || is_scheme(c)
}

fn is_ipv4(c: char) -> bool {
is_digit(c) || c == '.'
}

fn ipv4(input: &str) -> IResult<&str, &str, CustomError<&str>> {
let (input, ipv4_) =
recognize(tuple((u8, char('.'), u8, char('.'), u8, char('.'), u8)))(input)?;
Expand Down Expand Up @@ -316,10 +312,6 @@ fn take_while_ireg(input: &str) -> IResult<&str, &str, CustomError<&str>> {
))(input)
}

fn is_userinfo_not_pct_encoded(c: char) -> bool {
is_iunreserved(c) || is_sub_delim(c)
}

fn iauthority(input: &str) -> IResult<&str, (&str, &str, bool), CustomError<&str>> /* (iauthority, host, bool) */
{
let i = <&str>::clone(&input);
Expand All @@ -343,29 +335,6 @@ fn is_iuserinfo_not_pct_encoded(c: char) -> bool {
is_iunreserved(c) || is_sub_delim(c) || c == ':'
}

fn ihier_part(input: &str) -> IResult<&str, (&str, &str, bool), CustomError<&str>> {
let i = <&str>::clone(&input);
let (input, _double_slash) = tag("//")(input)?;
let (input, (authority, host, is_ipv6_or_future)) = iauthority(input)?;
let (input, path) = opt(alt((
recognize(tuple((
char('/'),
opt(tuple((
take_while_ipchar1,
many0(tuple((char('/'), take_while_ipchar))),
))),
))), // ipath-absolute
recognize(tuple((
take_while_ipchar,
many0(tuple((char('/'), take_while_ipchar))),
))), // ipath_rootless
)))(input)?;
let path = path.unwrap_or(""); // it's ipath_empty
let len = 2 + authority.len() + path.len();
// 2 is for double_slash
Ok((input, (&i[0..len], host, is_ipv6_or_future)))
}

fn is_ipchar_not_pct_encoded(c: char) -> bool {
is_iunreserved(c) || is_sub_delim(c) || matches!(c, ':' | '@')
}
Expand Down Expand Up @@ -466,12 +435,28 @@ fn get_puny_code_warning(link: &str, host: &str) -> Option<PunycodeWarning> {
fn parse_iri(input: &str) -> IResult<&str, LinkDestination, CustomError<&str>> {
let input_ = <&str>::clone(&input);

Check warning on line 436 in src/parser/link_url.rs

View workflow job for this annotation

GitHub Actions / clippy

unused variable: `input_`

warning: unused variable: `input_` --> src/parser/link_url.rs:436:9 | 436 | let input_ = <&str>::clone(&input); | ^^^^^^ help: if this is intentional, prefix it with an underscore: `_input_` | = note: `#[warn(unused_variables)]` on by default

Check failure on line 436 in src/parser/link_url.rs

View workflow job for this annotation

GitHub Actions / Build and test (ubuntu-latest, 1.64.0)

unused variable: `input_`
let (input, scheme) = scheme(input)?;
let (input, (ihier, host, is_ipv6_or_future)) = ihier_part(input)?;
let (input, _double_slash) = tag("//")(input)?;
let (input, (authority, host, is_ipv6_or_future)) = iauthority(input)?;
let (input, path) = opt(alt((
recognize(tuple((
char('/'),
opt(tuple((
take_while_ipchar1,
many0(tuple((char('/'), take_while_ipchar))),
))),
))), // ipath-absolute
recognize(tuple((
take_while_ipchar,
many0(tuple((char('/'), take_while_ipchar))),
))), // ipath-rootless
)))(input)?;
let path = path.unwrap_or(""); // it's ipath-empty
let (input, query) = opt(recognize(tuple((char('?'), iquery))))(input)?;
let (input_, fragment) = opt(recognize(tuple((char('#'), take_while_ifragment))))(input)?;
let query = query.unwrap_or("");
let fragment = fragment.unwrap_or("");
let len = scheme.len() + ihier.len() + query.len() + fragment.len();
let ihier_len = 2 + authority.len() + host.len() + path.len();

Check failure on line 458 in src/parser/link_url.rs

View workflow job for this annotation

GitHub Actions / clippy

integer arithmetic detected

error: integer arithmetic detected --> src/parser/link_url.rs:458:21 | 458 | let ihier_len = 2 + authority.len() + host.len() + path.len(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#integer_arithmetic
let len = scheme.len() + ihier_len + query.len() + fragment.len();

Check failure on line 459 in src/parser/link_url.rs

View workflow job for this annotation

GitHub Actions / clippy

integer arithmetic detected

error: integer arithmetic detected --> src/parser/link_url.rs:459:15 | 459 | let len = scheme.len() + ihier_len + query.len() + fragment.len(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#integer_arithmetic
let link = &input_[0..len];

Check failure on line 460 in src/parser/link_url.rs

View workflow job for this annotation

GitHub Actions / clippy

slicing may panic

error: slicing may panic --> src/parser/link_url.rs:460:17 | 460 | let link = &input_[0..len]; | ^^^^^^^^^^^^^^ | = help: consider using `.get(n..m)` or `.get_mut(n..m)` instead = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#indexing_slicing

Check failure on line 460 in src/parser/link_url.rs

View workflow job for this annotation

GitHub Actions / clippy

indexing into a string may panic if the index is within a UTF-8 character

error: indexing into a string may panic if the index is within a UTF-8 character --> src/parser/link_url.rs:460:17 | 460 | let link = &input_[0..len]; | ^^^^^^^^^^^^^^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#string_slice
Ok((
input,
Expand All @@ -488,16 +473,17 @@ fn parse_iri(input: &str) -> IResult<&str, LinkDestination, CustomError<&str>> {
))
}


/*
// For future
fn parse_irelative_ref(input: &str) -> IResult<&str, Element, CustomError<&str>> {
todo!()
}

*/

// White listed links in this format: scheme:some_char like tel:+989164364485
fn parse_generic(input: &str) -> IResult<&str, LinkDestination, CustomError<&str>> {
let (input, scheme) = scheme(input)?;

let (input, target) = take_while(is_not_white_space)(input)?;

Ok((input, LinkDestination {
Expand Down

0 comments on commit d0e9ace

Please sign in to comment.