Unverified Commit ad8a61f7 by Enkelmann Committed by GitHub

fix format string parsing bug (#359)

parent a16e6589
......@@ -57,7 +57,14 @@ pub fn parse_format_string_parameters(
format_string: &str,
datatype_properties: &DatatypeProperties,
) -> Result<Vec<(Datatype, ByteSize)>, Error> {
let re = Regex::new(r#"%\d{0,2}(([c,C,d,i,o,u,x,X,e,E,f,F,g,G,a,A,n,p,s,S])|(hi|hd|hu|li|ld|lu|lli|lld|llu|lf|lg|le|la|lF|lG|lE|lA|Lf|Lg|Le|La|LF|LG|LE|LA))"#)
// Regex parts:
// - `%` starts a format string parameter
// - `[+\-#0]{0,1}` matches the flags `+`, `-`, `#`, `0` if present (for printf-like functions)
// - `\d*` matches the width parameter
// - `[\.]?\d*` matches the precision parameter (for printf-like functions)
// - `[cCdiouxXeEfFgGaAnpsS]` matches a format specifier without length parameter.
// - `hi|hd|hu|li|ld|lu|lli|lld|llu|lf|lg|le|la|lF|lG|lE|lA|Lf|Lg|Le|La|LF|LG|LE|LA` matches format specifiers with length parameter.
let re = Regex::new(r#"%[+\-#0]{0,1}\d*[\.]?\d*([cCdiouxXeEfFgGaAnpsS]|hi|hd|hu|li|ld|lu|lli|lld|llu|lf|lg|le|la|lF|lG|lE|lA|Lf|Lg|Le|La|LF|LG|LE|LA)"#)
.expect("No valid regex!");
let datatype_map: Vec<(Datatype, ByteSize)> = re
......
......@@ -96,6 +96,34 @@ fn test_parse_format_string_destination_and_return_content() {
);
}
/// Test the regular expression used for format string parameter parsing on specific cases.
#[test]
fn test_format_string_regex() {
let regex = Regex::new(r#"%[+\-#0]{0,1}\d*[\.]?\d*([cCdiouxXeEfFgGaAnpsS]|hi|hd|hu|li|ld|lu|lli|lld|llu|lf|lg|le|la|lF|lG|lE|lA|Lf|Lg|Le|La|LF|LG|LE|LA)"#)
.expect("No valid regex!");
let format_string = "one %s, two %.2lf%%, three %.2lf%%";
let results: Vec<_> = regex
.captures_iter(format_string)
.map(|cap| cap[1].to_string())
.collect();
assert_eq!(&results[..], vec!["s", "lf", "lf"]);
let format_string = "test %+2.300f,%-2.300f%#2.300f%02.300f";
let results: Vec<_> = regex
.captures_iter(format_string)
.map(|cap| cap[1].to_string())
.collect();
assert_eq!(&results[..], vec!["f", "f", "f", "f"]);
let format_string = r#"%cCd %ss %256s /dev/bus/usb/%03d/%s"#;
let results: Vec<_> = regex
.captures_iter(format_string)
.map(|cap| cap[1].to_string())
.collect();
assert_eq!(&results[..], vec!["c", "s", "s", "d", "s"]);
}
#[test]
fn test_parse_format_string_parameters() {
let test_cases: Vec<&str> = vec![
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment