summaryrefslogtreecommitdiff
path: root/src/eval/str.rs
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2022-05-03 16:59:13 +0200
committerLaurenz <laurmaedje@gmail.com>2022-05-04 00:14:31 +0200
commit507c5fc92563560426db0d86c0348880b0493467 (patch)
tree45116b62fb0fffe8e88d6c96ae1fcf11c73f8ee9 /src/eval/str.rs
parente18a896a93cae987aa30addd40e678bf0064fd31 (diff)
Text replacement show rules
Diffstat (limited to 'src/eval/str.rs')
-rw-r--r--src/eval/str.rs46
1 files changed, 46 insertions, 0 deletions
diff --git a/src/eval/str.rs b/src/eval/str.rs
index 3b4349a1..514bf318 100644
--- a/src/eval/str.rs
+++ b/src/eval/str.rs
@@ -1,3 +1,7 @@
+use std::fmt::{self, Debug, Formatter};
+use std::hash::{Hash, Hasher};
+use std::ops::Deref;
+
use super::{Array, Value};
use crate::diag::StrResult;
use crate::util::EcoString;
@@ -35,3 +39,45 @@ impl StrExt for EcoString {
}
}
}
+
+/// A regular expression.
+#[derive(Clone)]
+pub struct Regex(regex::Regex);
+
+impl Regex {
+ /// Create a new regex.
+ pub fn new(re: &str) -> StrResult<Self> {
+ regex::Regex::new(re).map(Self).map_err(|err| err.to_string())
+ }
+
+ /// Whether the regex matches the given `text`.
+ pub fn matches(&self, text: &str) -> bool {
+ self.0.is_match(text)
+ }
+}
+
+impl Deref for Regex {
+ type Target = regex::Regex;
+
+ fn deref(&self) -> &Self::Target {
+ &self.0
+ }
+}
+
+impl Debug for Regex {
+ fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
+ write!(f, "regex({:?})", self.0.as_str())
+ }
+}
+
+impl PartialEq for Regex {
+ fn eq(&self, other: &Self) -> bool {
+ self.0.as_str() == other.0.as_str()
+ }
+}
+
+impl Hash for Regex {
+ fn hash<H: Hasher>(&self, state: &mut H) {
+ self.0.as_str().hash(state);
+ }
+}