Skip to content

Commit

Permalink
fix some problems with is_in_one_of_ranges and updating the code:
Browse files Browse the repository at this point in the history
  • Loading branch information
farooqkz committed Mar 12, 2024
1 parent 9e4f742 commit 6f16d66
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 35 deletions.
5 changes: 2 additions & 3 deletions src/parser/parse_from_text/find_range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,10 @@ fn find_range_for_char<'a>(code: u32, ranges: &'a [RangeInclusive<u32>]) -> Find
///
/// # Arguments
///
/// - `c` A character
/// - `c` A number(u32)
///
/// - `ranges` A sorted slice of ranges to see if `c` is in anyone of them
pub fn is_in_one_of_ranges(c: char, ranges: &[RangeInclusive<u32>]) -> bool {
let c = c as u32;
pub fn is_in_one_of_ranges(c: u32, ranges: &[RangeInclusive<u32>]) -> bool {
match find_range_for_char(c, ranges) {
FindRangeResult::WasOnRangeStart => true,
FindRangeResult::Range(range) => range.contains(&c),
Expand Down
48 changes: 23 additions & 25 deletions src/parser/parse_from_text/hashtag_content_char_ranges.rs
Original file line number Diff line number Diff line change
Expand Up @@ -876,41 +876,39 @@ pub(crate) fn hashtag_content_char(c: char) -> bool {
} else if matches!(c, '+' | '-' | '_') {
true
} else {
is_in_one_of_ranges(c, &HASHTAG_CONTENT_CHAR_RANGES[..])
is_in_one_of_ranges(c as u32, &HASHTAG_CONTENT_CHAR_RANGES[..])
}
}

#[cfg(test)]
mod test {
use crate::parser::parse_from_text::hashtag_content_char_ranges::hashtag_content_char;

use super::{find_range_for_char, FindRangeResult, RangeInclusive};
use crate::parser::parse_from_text::find_range::is_in_one_of_ranges;
use std::ops::RangeInclusive;

#[test]
fn test_range_function() {
// these must return WasOnRangeStart
let codes: Vec<u32> = vec![0x30000, 0xe0100, 0x23, 0x30, 0x171f, 0x176e, 0x10fb0];
for code in codes.iter() {
assert_eq!(find_range_for_char(*code), FindRangeResult::WasOnRangeStart);
}

// these must be return associated ranges
let codes: Vec<(u32, RangeInclusive<u32>)> = vec![
(0x11066 + 5, 0x11066..=0x11075), // in range
(0x11000 + 10, 0x11000..=0x11046), // in range
(0x11046 + 2, 0x11000..=0x11046), // out of range
(0x10, 0x23..=0x23),
(0x09, 0x23..=0x23),
(0x0, 0x23..=0x23),
(0x25, 0x23..=0x23),
(0x2a + 1, 0x2a..=0x2a),
(0xfffff, 0xe0100..=0xe01ef),
// ^ this is beyond ranges and must return the
// last range
let ranges: [RangeInclusive<u32>; 5] = [
0x0..=0x30,
0x99..=0x99,
0x1f..=0x2f,
0xff..=0xff,
0x1000f..=0x20000,
];

for (code, range) in codes.iter() {
assert_eq!(find_range_for_char(*code), FindRangeResult::Range(range));
let codes: Vec<(u32, bool)> = vec![
(0x30000, false),
(0x01, true),
(0x23, true),
(0x30, false),
(0x171f, false),
(0x176e, false),
(0x10fb0, true),
(0x0, true),
(0xf1, false)
];
for (code, result) in codes.iter() {
assert_eq!(is_in_one_of_ranges(*code, &ranges[..]), *result);
println!("{code}, {result}");
}
}

Expand Down
19 changes: 16 additions & 3 deletions src/parser/parse_from_text/link_element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ const UCSCHAR_RANGES: [RangeInclusive<u32>; 17] = [
];

fn is_ucschar(c: char) -> bool {
is_in_one_of_ranges(c, &UCSCHAR_RANGES[..])
is_in_one_of_ranges(c as u32, &UCSCHAR_RANGES[..])
}

fn is_unreserved(c: char) -> bool {
Expand Down Expand Up @@ -276,7 +276,7 @@ const IPRIVATE_RANGES: [RangeInclusive<u32>; 3] =
[0xe000..=0xf8ff, 0xf0000..=0xffffd, 0x100000..=0x10fffd];

fn is_iprivate(c: char) -> bool {
is_in_one_of_ranges(c, &IPRIVATE_RANGES[..])
is_in_one_of_ranges(c as u32, &IPRIVATE_RANGES[..])
}

fn is_iquery_not_pct_encoded(c: char) -> bool {
Expand Down Expand Up @@ -349,7 +349,7 @@ fn get_puny_code_warning(link: &str, host: &str) -> Option<PunycodeWarning> {
}
}

pub fn link(input: &str) -> IResult<&str, Element, CustomError<&str>> {
fn parse_iri(input: &str) -> IResult<&str, Element, CustomError<&str>> {
let input_ = <&str>::clone(&input);

Check warning on line 353 in src/parser/parse_from_text/link_element.rs

View workflow job for this annotation

GitHub Actions / clippy

unused variable: `input_`

warning: unused variable: `input_` --> src/parser/parse_from_text/link_element.rs:353:9 | 353 | 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 353 in src/parser/parse_from_text/link_element.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)?;
Expand All @@ -371,3 +371,16 @@ pub fn link(input: &str) -> IResult<&str, Element, CustomError<&str>> {
},
))
}

fn parse_irelative_ref(input: &str) -> IResult<&str, Element, CustomError<&str>> {

Check warning on line 375 in src/parser/parse_from_text/link_element.rs

View workflow job for this annotation

GitHub Actions / clippy

function `parse_irelative_ref` is never used

warning: function `parse_irelative_ref` is never used --> src/parser/parse_from_text/link_element.rs:375:4 | 375 | fn parse_irelative_ref(input: &str) -> IResult<&str, Element, CustomError<&str>> { | ^^^^^^^^^^^^^^^^^^^

Check warning on line 375 in src/parser/parse_from_text/link_element.rs

View workflow job for this annotation

GitHub Actions / clippy

unused variable: `input`

warning: unused variable: `input` --> src/parser/parse_from_text/link_element.rs:375:24 | 375 | fn parse_irelative_ref(input: &str) -> IResult<&str, Element, CustomError<&str>> { | ^^^^^ help: if this is intentional, prefix it with an underscore: `_input`

Check failure on line 375 in src/parser/parse_from_text/link_element.rs

View workflow job for this annotation

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

unused variable: `input`

Check failure on line 375 in src/parser/parse_from_text/link_element.rs

View workflow job for this annotation

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

function `parse_irelative_ref` is never used
todo!()

Check failure on line 376 in src/parser/parse_from_text/link_element.rs

View workflow job for this annotation

GitHub Actions / clippy

`todo` should not be present in production code

error: `todo` should not be present in production code --> src/parser/parse_from_text/link_element.rs:376:5 | 376 | todo!() | ^^^^^^^ | note: the lint level is defined here --> src/lib.rs:30:5 | 30 | clippy::todo, | ^^^^^^^^^^^^ = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#todo
}

Check failure on line 377 in src/parser/parse_from_text/link_element.rs

View workflow job for this annotation

GitHub Actions / clippy

used `unimplemented!()`, `unreachable!()`, `todo!()`, `panic!()` or assertion in a function that returns `Result`

error: used `unimplemented!()`, `unreachable!()`, `todo!()`, `panic!()` or assertion in a function that returns `Result` --> src/parser/parse_from_text/link_element.rs:375:1 | 375 | / fn parse_irelative_ref(input: &str) -> IResult<&str, Element, CustomError<&str>> { 376 | | todo!() 377 | | } | |_^ | note: the lint level is defined here --> src/lib.rs:27:5 | 27 | clippy::panic_in_result_fn, | ^^^^^^^^^^^^^^^^^^^^^^^^^^ = help: `unimplemented!()`, `unreachable!()`, `todo!()`, `panic!()` or assertions should not be used in a function that returns `Result` as `Result` is expected to return an error instead of crashing note: return Err() instead of panicking --> src/parser/parse_from_text/link_element.rs:376:5 | 376 | todo!() | ^^^^^^^ = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#panic_in_result_fn

pub fn parse_link(input: &str) -> IResult<&str, Element, CustomError<&str>> {
/*
match parse_iri(input) {
Ok((input, iri)) => Ok((input, iri)),
Err(..) => parse_irelative_ref(input),
}*/
parse_iri(input)
}
4 changes: 2 additions & 2 deletions src/parser/parse_from_text/markdown_elements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use super::text_elements::parse_text_element;
use super::Element;
use super::{base_parsers::*, parse_all};
use crate::parser::link_url::LinkDestination;
use crate::parser::parse_from_text::link_element::link;
use crate::parser::parse_from_text::link_element::parse_link;
///! nom parsers for markdown elements
use nom::{
bytes::complete::{is_not, tag, take, take_while},
Expand Down Expand Up @@ -111,7 +111,7 @@ pub(crate) fn delimited_link(input: &str) -> IResult<&str, Element, CustomError<
return Err(nom::Err::Failure(CustomError::));
}
};*/
let (rest, link) = link(input)?;
let (rest, link) = parse_link(input)?;
if !rest.is_empty() {
return Err(nom::Err::Error(CustomError::UnexpectedContent));
}
Expand Down
4 changes: 2 additions & 2 deletions src/parser/parse_from_text/text_elements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::parser::link_url::LinkDestination;
use super::base_parsers::CustomError;
use super::base_parsers::*;

Check warning on line 5 in src/parser/parse_from_text/text_elements.rs

View workflow job for this annotation

GitHub Actions / clippy

unused import: `super::base_parsers::*`

warning: unused import: `super::base_parsers::*` --> src/parser/parse_from_text/text_elements.rs:5:5 | 5 | use super::base_parsers::*; | ^^^^^^^^^^^^^^^^^^^^^^

Check failure on line 5 in src/parser/parse_from_text/text_elements.rs

View workflow job for this annotation

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

unused import: `super::base_parsers::*`
use super::hashtag_content_char_ranges::hashtag_content_char;
use super::link_element::link;
use super::link_element::parse_link;
use super::Element;
use crate::nom::{Offset, Slice};
use nom::bytes::complete::take_while;
Expand Down Expand Up @@ -280,7 +280,7 @@ pub(crate) fn parse_text_element(
Ok((i, elm))
} else if let Ok((i, elm)) = email_address(input) {
Ok((i, elm))
} else if let Ok((i, elm)) = link(input) {
} else if let Ok((i, elm)) = parse_link(input) {
Ok((i, elm))
} else if let Ok((i, _)) = linebreak(input) {
Ok((i, Element::Linebreak))
Expand Down

0 comments on commit 6f16d66

Please sign in to comment.