From 257f8e35ca82fbd22668033f7cab11c9e0e05071 Mon Sep 17 00:00:00 2001 From: TudbuT Date: Mon, 2 Jan 2023 18:23:52 +0100 Subject: [PATCH] fix not finding a result in some patterns --- Cargo.toml | 2 +- src/lib.rs | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 8bc97f2..7df3ef3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "readformat" -version = "0.1.1" +version = "0.1.2" edition = "2021" repository = "https://github.com/tudbut/readformat" license = "MIT" diff --git a/src/lib.rs b/src/lib.rs index 9116128..42a599f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -26,7 +26,7 @@ pub fn readf(format: &str, mut s: &str) -> Option> { for n in 0..=occurences { // shave off space until next {} let i = if let Some(x) = f.find("{}") { x } else { f.len() }; - if f.len() <= i || s.len() <= i { + if f.len() < i || s.len() < i { return None; } if &f[0..i] != &s[0..i] { @@ -35,6 +35,7 @@ pub fn readf(format: &str, mut s: &str) -> Option> { if n == occurences { break; } + // abcde | a{}cd{} => bcde | cd{} f = &f[(i + 2)..]; s = &s[i..]; @@ -84,6 +85,7 @@ mod tests { assert_eq!(readf1("Hello, {}!", "hello, person"), None); assert_eq!(readf1("hello!", "hello!"), Some("".into())); assert_eq!(readf1("Hello!", "hello!"), None); + assert_eq!(readf1("{}", "person"), Some("person".into())); } #[test] @@ -96,5 +98,6 @@ mod tests { assert_eq!(readf("Hello, {} and {}!", "hello, person 1 and person 2!"), None); assert_eq!(readf("hello!", "hello!"), Some(vec![])); assert_eq!(readf("Hello!", "hello!"), None); + assert_eq!(readf("{}, {}", "person 1, person 2"), Some(vec!["person 1".into(), "person 2".into()])); } }