Skip to content

Commit

Permalink
only check if has scheme to determine if it's a relative uri (faster)
Browse files Browse the repository at this point in the history
  • Loading branch information
nbittich committed Dec 1, 2024
1 parent f829121 commit e6da8bc
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
23 changes: 16 additions & 7 deletions src/iri.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ use nom::{
combinator::complete,
error::{ParseError, VerboseError},
};
use parser::{parse_absolute_iri, parse_iri, parse_iri_reference};
use parser::{parse_absolute_iri, parse_iri, parse_iri_reference, parse_scheme};

use crate::prelude::alt;

Expand Down Expand Up @@ -111,6 +111,12 @@ impl IRI<'_> {
pub fn is_relative(&self) -> bool {
matches!(self, IRI::Reference(_))
}
pub fn has_scheme(s: &str) -> bool {
match parse_scheme(s) {
Ok((_, scheme)) => !scheme.is_empty(),
Err(_) => false,
}
}
}
mod ip {
use nom::{
Expand Down Expand Up @@ -231,7 +237,7 @@ mod parser {
pub(super) fn parse_iri(s: &str) -> ParserResult<IRI> {
map(
tuple((
terminated(parse_scheme, tag(":")),
parse_scheme,
parse_i_hier_part,
preceded(opt(tag("?")), parse_i_query),
preceded(opt(tag("#")), parse_i_fragment),
Expand All @@ -248,7 +254,7 @@ mod parser {
map(
tuple((
parse_scheme,
preceded(tag(":"), parse_i_hier_part),
parse_i_hier_part,
preceded(opt(tag("?")), parse_i_query),
)),
|(scheme, hier_part, query)| IRI::Absolute {
Expand Down Expand Up @@ -383,10 +389,13 @@ mod parser {
tag("@"),
))(s)
}
fn parse_scheme(s: &str) -> ParserResult<&str> {
verify(
take_while1(|c: char| c.is_alphanumeric() || c == '.' || c == '-' || c == '+'),
|scheme: &str| scheme.starts_with(|c: char| c.is_alphabetic()),
pub(super) fn parse_scheme(s: &str) -> ParserResult<&str> {
terminated(
verify(
take_while1(|c: char| c.is_alphanumeric() || c == '.' || c == '-' || c == '+'),
|scheme: &str| scheme.starts_with(|c: char| c.is_alphabetic()),
),
tag(":"),
)(s)
}
fn parse_userinfo(s: &str) -> ParserResult<&str> {
Expand Down
1 change: 1 addition & 0 deletions src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ fn cmp_input_file(
&mut input_buf,
)
.unwrap();
println!("len: {}", input.len());

if output_json {
let f = File::open(
Expand Down
10 changes: 6 additions & 4 deletions src/turtle/turtle_doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -408,10 +408,12 @@ impl<'a> TurtleDoc<'a> {
) -> Result<Node<'x>, TurtleDocError> {
match s {
TurtleValue::Iri(Iri::Enclosed(iri)) => {
let iri_rfc3987 = IRI::try_from(iri).map_err(|e| TurtleDocError {
message: e.to_string(),
})?;
if iri_rfc3987.is_relative() {
// FIXME this is better but slow.
// Call if iri.is_relative instead of has_scheme
// let iri_rfc3987 = IRI::try_from(iri).map_err(|e| TurtleDocError {
// message: e.to_string(),
// })?;
if !IRI::has_scheme(iri) {
if let Some(base) = base {
let iri = (*base).to_owned() + iri;
return Ok(Node::Iri(Cow::Owned(iri.to_string())));
Expand Down

0 comments on commit e6da8bc

Please sign in to comment.