aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMelody Horn <melody@boringcactus.com>2021-04-05 11:30:21 -0600
committerMelody Horn <melody@boringcactus.com>2021-04-05 11:30:21 -0600
commit9267fb0387a87edc4b059dbbdf0258ec6b7cc62d (patch)
treef43105666bf88e4d39073068328a663b5dfb015f
parent0d8aed06c3be7765caf98385c1883b3db7a507b8 (diff)
downloadmakers-9267fb0387a87edc4b059dbbdf0258ec6b7cc62d.tar.gz
makers-9267fb0387a87edc4b059dbbdf0258ec6b7cc62d.zip
implement function `findstring`
-rw-r--r--src/makefile/functions.rs25
1 files changed, 25 insertions, 0 deletions
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<String> {
+ 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<String> {
let patterns = macros.expand(patterns)?;
let patterns = patterns.split_whitespace().collect::<Vec<_>>();
@@ -380,6 +398,13 @@ mod test {
}
#[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");
assert_eq!(result, "word");