fn grep_offset<O: Write, P: Display, B: AsRef<[u8]>>(
  out: &mut O,
  options: &args::Options,
  pattern: &Regex,
  path: P,
  buffer: B
) -> io::Result<bool> {
  let buffer = buffer.as_ref();

  let mut write_hex = |x| {
    if options.print_filename {
      writeln!(out, "{}: 0x{:x}", path, x)
    } else {
      writeln!(out, "0x{:x}", x)
    }
  };


  let mut matches = pattern.find_iter(buffer);

  let mut matched = false;

  if options.inverse {
    // if the pattern matches multiple times, comprising the entire buffer, then no
    // inverse match is present.
    let mut end = 0; // Start from the beginning of the buffer.

    for m in matches {
      if m.start() > end {
        write_hex(end)?;
        matched = true;
      }

      end = m.end()
    }

    if end < buffer.len() { // Also check for a "hole" after the last match.
      write_hex(end)?;
      matched = true;
    }
  }
  else {
    // Set `matched` if there is a first occurrence:
    if let Some(m) = matches.next() {
      write_hex(m.start())?;
      matched = true;
    }

    // Iterate the remaining matches:
    for m in matches {
      write_hex(m.start())?;
    }
  }


  Ok(matched)
}
/// Run bgrep, outputting the matche's offset in hex to the given `out`.
/// Returns whether there was a match.
Run bgrep, outputting the matche's offset in hex to the given `out`.
Returns whether there was a match.
Run bgrep outputting the matche ' s offset in hex to the given ` out ` . Returns whether there was a match .
fn grep_offset<O: Write, P: Display, B: AsRef<[u8]>>(
  out: &mut O,
  options: &args::Options,
  pattern: &Regex,
  path: P,
  buffer: B
) -> io::Result<bool> {
  let buffer = buffer.as_ref();
  let mut write_hex = |x| {
    if options.print_filename {
      writeln!(out, "{}: 0x{:x}", path, x)
    } else {
      writeln!(out, "0x{:x}", x)
    }
  };
  let mut matches = pattern.find_iter(buffer);
  let mut matched = false;
  if options.inverse {
    // if the pattern matches multiple times, comprising the entire buffer, then no
    // inverse match is present.
    let mut end = 0; // Start from the beginning of the buffer.
    for m in matches {
      if m.start() > end {
        write_hex(end)?;
        matched = true;
      }
      end = m.end()
    }
    if end < buffer.len() { // Also check for a "hole" after the last match.
      write_hex(end)?;
      matched = true;
    }
  }
  else {
    // Set `matched` if there is a first occurrence:
    if let Some(m) = matches.next() {
      write_hex(m.start())?;
      matched = true;
    }
    // Iterate the remaining matches:
    for m in matches {
      write_hex(m.start())?;
    }
  }
  Ok(matched)
}
fn grep_offset < O : Write , P : Display , B : AsRef < [ u8 ] > > ( out : & mut O , options : & args :: Options , pattern : & Regex , path : P , buffer : B ) -> io :: Result < bool > { let buffer = buffer . as_ref ( ) ; let mut write_hex = | x | { if options . print_filename { writeln ! ( out " " path x ) } else { writeln ! ( out " " x ) } } ; let mut matches = pattern . find_iter ( buffer ) ; let mut matched = false ; if options . inverse { // if the pattern matches multiple times, comprising the entire buffer, then no // inverse match is present. let mut end = 0 ; // Start from the beginning of the buffer. for m in matches { if m . start ( ) > end { write_hex ( end ) ? ; matched = true ; } end = m . end ( ) } if end < buffer . len ( ) { // Also check for a "hole" after the last match. write_hex ( end ) ? ; matched = true ; } } else { // Set `matched` if there is a first occurrence: if let Some ( m ) = matches . next ( ) { write_hex ( m . start ( ) ) ? ; matched = true ; } // Iterate the remaining matches: for m in matches { write_hex ( m . start ( ) ) ? ; } } Ok ( matched ) }
Run bgrep, outputting the matche's offset in hex to the given `out`.
Run bgrep outputting the matche ' s offset in hex to the given ` out ` .
out
&mut O
options
&args::Options
pattern
&Regex
path
P
buffer
B
out
&mut O
options
&args::Options
pattern
&Regex
path
P
buffer
B