]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
rust: add nom7 combinator take_until_and_consume
authorPierre Chifflier <chifflier@wzdftpd.net>
Fri, 12 Nov 2021 12:45:40 +0000 (13:45 +0100)
committerVictor Julien <vjulien@oisf.net>
Mon, 13 Dec 2021 12:06:21 +0000 (13:06 +0100)
rust/src/common.rs

index 1d28ba5f0a789cd1b3a26d3612a65abd8d749221..71eaad728fa8c5da76f78bb350a4dcd1a31da62a 100644 (file)
@@ -1,6 +1,27 @@
 use std::ffi::CString;
 use std::os::raw::c_char;
 
+pub mod nom7 {
+    use nom7::bytes::streaming::{tag, take_until};
+    use nom7::error::ParseError;
+    use nom7::IResult;
+
+    /// Reimplementation of `take_until_and_consume` for nom 7
+    ///
+    /// `take_until` does not consume the matched tag, and
+    /// `take_until_and_consume` was removed in nom 7. This function
+    /// provides an implementation (specialized for `&[u8]`).
+    pub fn take_until_and_consume<'a, E: ParseError<&'a [u8]>>(t: &'a [u8])
+         -> impl Fn(&'a [u8]) -> IResult<&'a [u8], &'a [u8], E>
+    {
+        move |i: &'a [u8]| {
+            let (i, res) = take_until(t)(i)?;
+            let (i, _) = tag(t)(i)?;
+            Ok((i, res))
+        }
+    }
+}
+
 #[macro_export]
 macro_rules! take_until_and_consume (
  ( $i:expr, $needle:expr ) => (