Files
marqdo/src/host/re.rs
T
cflmy 9331af78e2 release(2.8.0): 合入 CHANGELOG/README/发版说明/skills prove 任务;CI 链接指向 doc/releases
- CHANGELOG.md:v2.8.0 条目(中英双语);
- README.md:版本行 2.7.1→2.8.0 + 愿景第 6 条(知识可证明);
- doc/releases/v2.8.0.md:双语发版说明(亮点/用法/已知限制);
- skills/marqdo-dev/index.mq.md:新增 prove 任务(路径/目录模式/
  require_lean/json;参数用 + 行声明升参,check 与 narrative-ratio 通过);
- 设计文档 §12.5 标记已合入;发版链接统一指向 doc/releases/v2.8.0.md。
2026-11-18 10:29:26 +08:00

103 lines
2.6 KiB
Rust
Executable File

//! Regex host primitives for Mid M1 (`lib/re`).
use crate::host::as_text;
use crate::value::Value;
use regex::Regex;
fn compile(pattern: &str) -> Result<Regex, String> {
Regex::new(pattern).map_err(|e| format!("invalid regex: {e}"))
}
pub fn is_match(text: &Value, pattern: &Value) -> Result<Value, String> {
let t = as_text(text, "text")?;
let p = as_text(pattern, "pattern")?;
let re = compile(p)?;
Ok(Value::Bool(re.is_match(t)))
}
pub fn find(text: &Value, pattern: &Value) -> Result<Value, String> {
let t = as_text(text, "text")?;
let p = as_text(pattern, "pattern")?;
let re = compile(p)?;
Ok(match re.find(t) {
Some(m) => Value::Text(m.as_str().to_string()),
None => Value::None,
})
}
pub fn find_all(text: &Value, pattern: &Value) -> Result<Value, String> {
let t = as_text(text, "text")?;
let p = as_text(pattern, "pattern")?;
let re = compile(p)?;
let xs: Vec<Value> = re
.find_iter(t)
.map(|m| Value::Text(m.as_str().to_string()))
.collect();
Ok(Value::List(xs))
}
pub fn replace(
text: &Value,
pattern: &Value,
with: &Value,
count: Option<&Value>,
) -> Result<Value, String> {
let t = as_text(text, "text")?;
let p = as_text(pattern, "pattern")?;
let w = as_text(with, "with")?;
let re = compile(p)?;
let limit = match count {
None | Some(Value::None) => None,
Some(Value::Int(n)) => {
if *n < 0 {
return Err("count must be non-negative".into());
}
Some(*n as usize)
}
Some(_) => return Err("count needs int".into()),
};
let out = match limit {
None => re.replace_all(t, w).into_owned(),
Some(0) => t.to_string(),
Some(n) => re.replacen(t, n, w).into_owned(),
};
Ok(Value::Text(out))
}
pub fn split(text: &Value, pattern: &Value) -> Result<Value, String> {
let t = as_text(text, "text")?;
let p = as_text(pattern, "pattern")?;
let re = compile(p)?;
let xs: Vec<Value> = re
.split(t)
.map(|s| Value::Text(s.to_string()))
.collect();
Ok(Value::List(xs))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn match_and_find() {
assert_eq!(
is_match(
&Value::Text("abc123".into()),
&Value::Text(r"\d+".into())
)
.unwrap(),
Value::Bool(true)
);
assert_eq!(
find(
&Value::Text("ab12cd34".into()),
&Value::Text(r"\d+".into())
)
.unwrap(),
Value::Text("12".into())
);
}
}