add readme

This commit is contained in:
Daniella / Tove 2023-01-01 19:37:49 +01:00
parent 90c32e1bd5
commit 7e82f682f7
2 changed files with 16 additions and 6 deletions

10
README.md Normal file
View file

@ -0,0 +1,10 @@
# readformat
The inverse of format!().
The format argument is the format string, and the s argument is the string to match the format
against.
Examples:
- `readf1("Hello, {}!", "Hello, world!")` => `Some("world")`
- `readf("I hope {} are {}!", "I hope you are doing well!")` => `Some(vec!["you", "well"])`
- `readf1("Goodbye, {}!", "Hello, world!")` => `None`

View file

@ -4,9 +4,9 @@
/// against. /// against.
/// ///
/// Examples: /// Examples:
/// `readf("Hello, {}!", "Hello, world!")` => `Some(vec!["world"])` /// - `readf("Hello, {}!", "Hello, world!")` => `Some(vec!["world"])`
/// `readf("I hope {} are {}!", "I hope you are doing well!")` => `Some(vec!["you", "well"])` /// - `readf("I hope {} are {}!", "I hope you are doing well!")` => `Some(vec!["you", "well"])`
/// `readf("Goodbye, {}!", "Hello, world!")` => `None` /// - `readf("Goodbye, {}!", "Hello, world!")` => `None`
pub fn readf(format: &str, mut s: &str) -> Option<Vec<String>> { pub fn readf(format: &str, mut s: &str) -> Option<Vec<String>> {
if !format.contains("{}") { if !format.contains("{}") {
return if format == s { Some(vec![]) } else { None }; return if format == s { Some(vec![]) } else { None };
@ -61,9 +61,9 @@ pub fn readf(format: &str, mut s: &str) -> Option<Vec<String>> {
/// Convenience function for single-item format strings. Extra items are dropped. /// Convenience function for single-item format strings. Extra items are dropped.
/// ///
/// Examples: /// Examples:
/// `readf1("Hello, {}!", "Hello, world!")` => `Some("world")` /// - `readf1("Hello, {}!", "Hello, world!")` => `Some("world")`
/// `readf1("I hope {} are doing well!", "I hope you are doing well!")` => `Some("you")` /// - `readf1("I hope {} are doing well!", "I hope you are doing well!")` => `Some("you")`
/// `readf1("Goodbye, {}!", "Hello, world!")` => `None` /// - `readf1("Goodbye, {}!", "Hello, world!")` => `None`
pub fn readf1(format: &str, s: &str) -> Option<String> { pub fn readf1(format: &str, s: &str) -> Option<String> {
let r = readf(format, s); let r = readf(format, s);
r.map(|x| if x.len() == 0 { "".into() } else { x[0].clone() }) r.map(|x| if x.len() == 0 { "".into() } else { x[0].clone() })