Code Monkey home page Code Monkey logo

scan_fmt's Introduction

scan_fmt BuildStatus

scan_fmt provides a simple scanf()-like input for Rust. The goal is to make it easier to read data from a string or stdin.

Currently the format string supports the following special sequences:

   {{ = escape for '{'
   }} = escape for '}'
   {} = return any value (until next whitespace)
   {d} = return base-10 decimal
   {x} = return hex (0xab or ab)
   {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 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

Examples

 #[macro_use] extern crate scan_fmt;
 use std::error::Error ;
 fn main() -> Result<(),Box<dyn Error>> {
   let (a,b,c) = scan_fmt!( "hello 0x12 345 bye",  // input string
                            "hello {x} {} {}",     // format
                            [hex u8], i32, String) ? ;   // type of a-c Options
   assert_eq!( a, 0x12 ) ;
   assert_eq!( b, 345 ) ;
   assert_eq!( c, "bye" ) ;

   println!("Enter something like: 123-22");
   let (c,d) = scanln_fmt!( "{d}-{d}", // format
                            u16, u8) ? ;  // type of a&b Options
   println!("Got {} and {}",c,d) ;
   // Note - currently scanln_fmt! just calls unwrap() on read_line()

   let (a,b) = scan_fmt_some!( "hello 12 345", // input string
                               "hello {} {}",   // format
                               u8, i32) ;   // types
   assert_eq!( a, Some(12) ) ;
   assert_eq!( b, Some(345) ) ;
   Ok(())
  }

Limitations

There is no compile-time warning if the number of {}'s in the format string doesn't match the number of return values. You'll just get None for extra return values. See src/lib.rs for more details.

scan_fmt's People

Contributors

imberflur avatar kvinwang avatar quodlibetor avatar smephite avatar wlentz avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.