summaryrefslogtreecommitdiff
path: root/dhall
diff options
context:
space:
mode:
authorBasile Henry2020-11-01 10:21:53 +0100
committerBasile Henry2020-11-01 10:21:53 +0100
commite12d7fc914a1c97376ed112a92326a138fa2b3f8 (patch)
tree0c5c96566b35cd2a9f140787fea4fb374e9a8b8c /dhall
parent48037367933085ca9c1c67c8c59f311e2b21be6d (diff)
Allow `Text/replace ` to support an abstract haystack
Diffstat (limited to 'dhall')
-rw-r--r--dhall/src/builtins.rs50
1 files changed, 28 insertions, 22 deletions
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<Nir>, 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,