From c5b0ecbfbb73113e99f8baa29891fbe0ba598b68 Mon Sep 17 00:00:00 2001 From: Melody Horn Date: Mon, 5 Apr 2021 11:58:17 -0600 Subject: implement function `subst` --- src/makefile/functions.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'src/makefile') diff --git a/src/makefile/functions.rs b/src/makefile/functions.rs index 6e9d3cb..46caca5 100644 --- a/src/makefile/functions.rs +++ b/src/makefile/functions.rs @@ -16,6 +16,10 @@ pub fn expand_call( to_eval: Option>>>, ) -> Result { match name { + "subst" => { + assert_eq!(args.len(), 3); + text::subst(macros, &args[0], &args[1], &args[2]) + } "strip" => { assert_eq!(args.len(), 1); text::strip(macros, &args[0]) @@ -123,6 +127,18 @@ pub fn expand_call( mod text { use super::*; + pub fn subst( + macros: &MacroSet, + from: &TokenString, + to: &TokenString, + text: &TokenString, + ) -> Result { + let from = macros.expand(from)?; + let to = macros.expand(to)?; + let text = macros.expand(text)?; + Ok(text.replace(&from, &to)) + } + pub fn strip(macros: &MacroSet, text: &TokenString) -> Result { let text = macros.expand(text)?; // TODO don't allocate this vec -- cgit v1.2.3