read_until_pattern

Function read_until_pattern 

Source
pub fn read_until_pattern(
    reader: &mut impl BufRead,
    pattern: &str,
    to: &mut Vec<u8>,
) -> Result<(Vec<u8>, usize)>
Expand description

Read from a BufRead stream until a regex pattern is found.

This function extends the standard read_until functionality to support multi-byte patterns using regex. It reads data from the stream until the specified regex pattern is found, storing all data up to and including the match in the provided buffer.

§Arguments

  • reader - A BufRead stream to read from
  • pattern - A regex pattern string to search for
  • to - Buffer to store the read data

§Returns

Returns a tuple containing:

  • The matched substring as bytes
  • The total number of bytes read

§Examples

use async_regex::read_until_pattern;
use std::io::Cursor;
 
let mut reader = Cursor::new(b"hello world test pattern");
let mut buffer = Vec::new();
 
let (matched, size) = read_until_pattern(&mut reader, r"\w+", &mut buffer).unwrap();
assert_eq!(matched, b"hello");
assert_eq!(buffer, b"hello");