From 1a27e733f961893b5cb1a92b1b9927e31631c936 Mon Sep 17 00:00:00 2001 From: Qrius Date: Tue, 12 Nov 2024 14:49:14 +0100 Subject: Add explode function, add auto filename metadata --- src/macro_processor/macro_processor.rs | 39 +++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) (limited to 'src/macro_processor/macro_processor.rs') diff --git a/src/macro_processor/macro_processor.rs b/src/macro_processor/macro_processor.rs index a9585bb..0b714f0 100644 --- a/src/macro_processor/macro_processor.rs +++ b/src/macro_processor/macro_processor.rs @@ -397,7 +397,10 @@ fn smp_builtin_array_each( let mut out = String::new(); for el in array.clone() { let exp = match el { - MacroType::String(s) => smp.expand_macro(&args[1], &mut [s])?, + MacroType::String(s) => { + let n = smp.expand_macro(&args[1], &mut [s])?; + smp.process_input(&n)? + } MacroType::Array(a) => { let mut out = Vec::new(); for _el in a { @@ -440,6 +443,36 @@ fn smp_builtin_array_size( Ok(array.len().to_string()) } +/// Split input into array +fn smp_builtin_explode( + smp: &mut MacroProcessor, + macro_name: &str, + args: &mut [String], +) -> Result { + if args.len() != 3 { + smp.warnings + .push(MacroProcessorWarning::from_macro_invocation( + macro_name, + args, + format!("Wrong number of arguments, expected 3"), + )); + return Ok(macro_name.to_string()); + } + let split = smp.process_input(&args[1])?; + for c in smp.process_input(&args[2])?.split(&split) { + if let Err(e) = smp.array_push(&args[0], MacroType::String(String::from(c))) { + smp.warnings + .push(MacroProcessorWarning::from_macro_invocation( + macro_name, + args, + format!("Error executing array_push ({:?})", e), + )); + } + } + + Ok(String::new()) +} + #[cfg(feature = "time")] fn smp_builtin_format_time( smp: &mut MacroProcessor, @@ -664,6 +697,10 @@ impl MacroProcessor { String::from("array_size"), MacroType::Function(smp_builtin_array_size), ); + self.define_macro( + String::from("explode"), + MacroType::Function(smp_builtin_explode), + ); #[cfg(feature = "time")] self.define_macro( String::from("format_time"), -- cgit v1.2.3