Crate scan_fmt[][src]

This crate provides a simple sscanf()-like interface to extract data from strings and stdin.

In version 0.2 scan_fmt! changed to return a Result. Use scan_fmt_some! for the 0.1.x behavior.

To use this crate, do:

#[macro_use] extern crate scan_fmt;

Example to read from a string:

  if let Ok((a,b)) = scan_fmt!( "-11 0x22", // input string
                                "{d} {x}",  // format
                                i8, [hex u8]) { // types
    assert_eq!( a, -11 ) ;
    assert_eq!( b, 0x22 ) ;
  }

  let (a,b,c) = scan_fmt_some!( "hello 12 345 bye", // input string
                                "hello {} {d} {}",  // format
                                u8, i32, String);   // type of a-c Options
  assert_eq!( a, Some(12) ) ;
  assert_eq!( b, Some(345) ) ;
  assert_eq!( c, Some("bye".into()) ) ;

Special format_string tokens:

  {{ = escape for '{'
  }} = escape for '}'
  {} = return any value (until next whitespace)
  {d} = return base-10 decimal
  {x} = return hex (0xab or ab)
      = you must wrap the type in [hex type], e.g. "[hex u32]"
  {f} = return float
  {*d} = "*" as the first character means "match but don't return"
  {2d} or {2x} or {2f} = limit the maximum width to 2.  Any positive integer works.
  {[...]} = return pattern.
    ^ inverts if it is the first character
    - is for ranges.  For a literal - put it at the start or end.
    To add a literal ] do "[]abc]"
  {e} = doesn't return a value, but matches end of line.  Use this if you
        don't want to ignore potential extra characters at end of input.
  Examples:
    {[0-9ab]} = match 0-9 or a or b
    {[^,.]} = match anything but , or .
    {/.../} = return regex inside of `//`. (if regex feature is installed)
     If there is a single capture group inside of the slashes then
     that group will make up the pattern.
  Examples:
    {/[0-9ab]/} = same as {[0-9ab]}, above
    {/a+/} = matches at least one `a`, greedily
    {/jj(a*)jj/} = matches any number of `a`s, but only if
      they're surrounded by two `j`s

Example to read from stdin:

    let (a,b) = scanln_fmt!( "{}-{}", u16, u8) ? ;
    println!("Got {} and {}",a,b);

    let (a,b) = scanln_fmt_some!( "{}-{}",   // format
                                 u16, u8);    // type of a&b Options
    match (a,b) {
      (Some(aa),Some(bb)) => println!("Got {} and {}",aa,bb),
      _ => println!("input error")
    }
    Ok(())

LIMITATIONS:

There are no compile-time checks to make sure the format strings matches the number of return arguments. Extra return values will be None or cause a Result error.

Like sscanf(), whitespace (including \n) is largely ignored.

Conversion to output values is done using parse::().

Modules

parse

Macros

scan_fmt
scan_fmt_help
scan_fmt_some
scanln_fmt

(a,+) = scanln_fmt!( format_string, types,+ )

scanln_fmt_some

(a,+) = scanln_fmt_some!( format_string, types,+ )

Functions

get_input_unwrap