From 9267fb0387a87edc4b059dbbdf0258ec6b7cc62d Mon Sep 17 00:00:00 2001 From: Melody Horn Date: Mon, 5 Apr 2021 11:30:21 -0600 Subject: implement function `findstring` --- src/makefile/functions.rs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/makefile/functions.rs b/src/makefile/functions.rs index 6e04b7a..6dacd10 100644 --- a/src/makefile/functions.rs +++ b/src/makefile/functions.rs @@ -20,6 +20,10 @@ pub fn expand_call( assert_eq!(args.len(), 1); text::strip(macros, &args[0]) } + "findstring" => { + assert_eq!(args.len(), 2); + text::findstring(macros, &args[0], &args[1]) + } "filter" => { assert_eq!(args.len(), 2); text::filter(macros, &args[0], &args[1]) @@ -110,6 +114,20 @@ mod text { Ok(words.join(" ")) } + pub fn findstring( + macros: &MacroSet, + needle: &TokenString, + haystack: &TokenString, + ) -> Result { + let needle = macros.expand(needle)?; + let haystack = macros.expand(haystack)?; + if haystack.contains(&needle) { + Ok(needle) + } else { + Ok(String::new()) + } + } + pub fn filter(macros: &MacroSet, patterns: &TokenString, text: &TokenString) -> Result { let patterns = macros.expand(patterns)?; let patterns = patterns.split_whitespace().collect::>(); @@ -379,6 +397,13 @@ mod test { Ok(()) } + #[test] + fn findstring() -> R { + assert_eq!(call!(findstring "hi", "thighs"), "hi"); + assert_eq!(call!(findstring "hey", "hello there"), ""); + Ok(()) + } + #[test] fn filter() -> R { let result = call!(filter "word", "this contains a word inside it"); -- cgit v1.2.3