Skip to content

Commit

Permalink
little optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
nbittich committed Nov 25, 2024
1 parent ff71924 commit 8c7ce70
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/iri.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
mod ip {
use nom::{
bytes::complete::take_while_m_n,
combinator::verify,
combinator::{success, verify},
error::{ParseError, VerboseError},
multi::many_m_n,
};
Expand Down Expand Up @@ -30,7 +30,7 @@ mod ip {
preceded(opt(tag(":")), map(hextet, Segment::Hextet)),
))(s)
}
let mut ipv6: Vec<u16> = vec![];
let mut ipv6: Vec<u16> = Vec::with_capacity(8);
let (rest, list) = verify(many_m_n(1, 8, segment), |l: &[Segment]| {
l.iter()
.filter(|seg| matches!(seg, Segment::Compressed))
Expand All @@ -50,7 +50,7 @@ mod ip {
compression_pos = Some(idx);
}
Segment::IpV4(_) if idx == 0 => {
let err = VerboseError::from_error_kind(s, ErrorKind::IsNot);
let err = VerboseError::from_error_kind(s, ErrorKind::Verify);
return Err(nom::Err::Error(err));
}
Segment::IpV4(l) => {
Expand All @@ -64,6 +64,7 @@ mod ip {
ipv6.insert(idx, 0x0);
}
}
let (_, ipv6) = verify(success(ipv6), |l: &[u16]| l.len() == 8)("")?;

Ok((rest, ipv6))
}
Expand Down

0 comments on commit 8c7ce70

Please sign in to comment.