From: Ian Jackson Date: Sun, 15 May 2022 14:09:29 +0000 (+0100) Subject: subst: Have substitutor handle any needle syntax X-Git-Tag: otter-1.1.0~109 X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=bd0776f89ebf49254c20951113f9b3b84d37514e;p=otter.git subst: Have substitutor handle any needle syntax Signed-off-by: Ian Jackson --- diff --git a/src/shapelib.rs b/src/shapelib.rs index d0aa4e99..92a606db 100644 --- a/src/shapelib.rs +++ b/src/shapelib.rs @@ -1129,13 +1129,20 @@ impl<'i> Substituting<'i> { }, Dollars::Text => { }, } - let needle: Cow = if self.do_dollars() { - let token = needle.strip_prefix('_') - .ok_or_else(|| self.internal_err("needle has no '_'"))?; - format!("${{{}}}", token).into() - } else { - needle - }; + let needle: Cow = (move || Some({ + if let Some(rhs) = needle.strip_prefix("${") { + let token = rhs.strip_suffix("}")?; + if self.do_dollars() { needle.into() } + else { format!("_{}", token).into() } + } else if let Some(token) = needle.strip_prefix("_") { + if ! self.do_dollars() { needle.into() } + else { format!("${{{}}}", token).into() } + } else { + return None + } + }))() + .ok_or_else(|| self.internal_err("needle has no '_'"))?; + let (r, count) = self.subst_general_precisely(&needle, replacement)?; (r, count, needle) }