From e12d7fc914a1c97376ed112a92326a138fa2b3f8 Mon Sep 17 00:00:00 2001 From: Basile Henry Date: Sun, 1 Nov 2020 10:21:53 +0100 Subject: Allow `Text/replace ` to support an abstract haystack --- dhall-lang | 2 +- dhall/src/builtins.rs | 50 ++++++++++++++++++++++++++++---------------------- 2 files changed, 29 insertions(+), 23 deletions(-) diff --git a/dhall-lang b/dhall-lang index e1e726f..ada9a99 160000 --- a/dhall-lang +++ b/dhall-lang @@ -1 +1 @@ -Subproject commit e1e726f9e835f0b9ea61f8f193eff93778af2a28 +Subproject commit ada9a99c12afdf289be9be2971ee043df3700286 diff --git a/dhall/src/builtins.rs b/dhall/src/builtins.rs index 41a9f75..cc426dd 100644 --- a/dhall/src/builtins.rs +++ b/dhall/src/builtins.rs @@ -418,30 +418,36 @@ fn apply_builtin(b: Builtin, args: Vec, env: NzEnv) -> NirKind { } } - // The needle and the haystack need to be fully - // evaluated as Text otherwise no progress can be made - match (nir_to_string(needle), nir_to_string(haystack)) { - (Some(n), Some(h)) => { - // When the needle is empty the haystack is returned untouched - if n.is_empty() { - Ret::Nir(haystack.clone()) - // Fast case when replacement is fully evaluated - } else if let Some(r) = nir_to_string(replacement) { - Ret::Nir(Nir::from_text(h.replace(&n, &r))) - } else { - use itertools::Itertools; + // The needle needs to be fully evaluated as Text otherwise no + // progress can be made + match nir_to_string(needle) { + // When the needle is empty the haystack is returned untouched + Some(n) if n.is_empty() => Ret::Nir(haystack.clone()), + Some(n) => { + // The haystack needs to be fully evaluated as Text otherwise no + // progress can be made + if let Some(h) = nir_to_string(haystack) { + // Fast case when replacement is fully evaluated + if let Some(r) = nir_to_string(replacement) { + Ret::Nir(Nir::from_text(h.replace(&n, &r))) + } else { + use itertools::Itertools; - let parts = h.split(&n).map(|s| { - InterpolatedTextContents::Text(s.to_string()) - }); - let replacement = - InterpolatedTextContents::Expr(replacement.clone()); + let parts = h.split(&n).map(|s| { + InterpolatedTextContents::Text(s.to_string()) + }); + let replacement = InterpolatedTextContents::Expr( + replacement.clone(), + ); - Ret::Nir(Nir::from_kind(NirKind::TextLit( - nze::nir::TextLit::new( - parts.intersperse(replacement), - ), - ))) + Ret::Nir(Nir::from_kind(NirKind::TextLit( + nze::nir::TextLit::new( + parts.intersperse(replacement), + ), + ))) + } + } else { + Ret::DoneAsIs } } _ => Ret::DoneAsIs, -- cgit v1.2.3 From 559f64fd866fc74e930ddf8984fa11199a57ca2c Mon Sep 17 00:00:00 2001 From: Basile Henry Date: Tue, 3 Nov 2020 23:32:18 +0100 Subject: Update dhall-lang to latest master --- dhall-lang | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dhall-lang b/dhall-lang index ada9a99..8e0674e 160000 --- a/dhall-lang +++ b/dhall-lang @@ -1 +1 @@ -Subproject commit ada9a99c12afdf289be9be2971ee043df3700286 +Subproject commit 8e0674eb419876e81d9e2218ddefc503a85fd580 -- cgit v1.2.3