summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2023-02-17 10:29:55 +0100
committerLaurenz <laurmaedje@gmail.com>2023-02-17 10:53:47 +0100
commitdd5f07eb9110cc5e19dcb4441743a323128426fc (patch)
tree010384b8c8ab7a129df65bc0c802a1ac039beebb /src
parent585f6564874d16a8f81a6c29e73091a008ccd484 (diff)
Add `clusters` and `codepoints` methods
Diffstat (limited to 'src')
-rw-r--r--src/model/methods.rs4
-rw-r--r--src/model/str.rs23
2 files changed, 17 insertions, 10 deletions
diff --git a/src/model/methods.rs b/src/model/methods.rs
index c0b63669..38ebebda 100644
--- a/src/model/methods.rs
+++ b/src/model/methods.rs
@@ -37,6 +37,8 @@ pub fn call(
}
Value::Str(string.slice(start, end).at(span)?)
}
+ "clusters" => Value::Array(string.clusters()),
+ "codepoints" => Value::Array(string.codepoints()),
"contains" => Value::Bool(string.contains(args.expect("pattern")?)),
"starts-with" => Value::Bool(string.starts_with(args.expect("pattern")?)),
"ends-with" => Value::Bool(string.ends_with(args.expect("pattern")?)),
@@ -218,6 +220,8 @@ pub fn methods_on(type_name: &str) -> &[(&'static str, bool)] {
"string" => &[
("len", false),
("at", true),
+ ("clusters", false),
+ ("codepoints", false),
("contains", true),
("ends-with", true),
("find", true),
diff --git a/src/model/str.rs b/src/model/str.rs
index ae0ef899..8da5b50c 100644
--- a/src/model/str.rs
+++ b/src/model/str.rs
@@ -42,11 +42,6 @@ impl Str {
self
}
- /// The grapheme clusters the string consists of.
- pub fn graphemes(&self) -> Array {
- self.as_str().graphemes(true).map(|s| Value::Str(s.into())).collect()
- }
-
/// Extract the first grapheme cluster.
pub fn first(&self) -> StrResult<Self> {
self.0
@@ -82,6 +77,16 @@ impl Str {
Ok(self.0[start..end].into())
}
+ /// The grapheme clusters the string consists of.
+ pub fn clusters(&self) -> Array {
+ self.as_str().graphemes(true).map(|s| Value::Str(s.into())).collect()
+ }
+
+ /// The codepoints the string consists of.
+ pub fn codepoints(&self) -> Array {
+ self.chars().map(|c| Value::Str(c.into())).collect()
+ }
+
/// Whether the given pattern exists in this string.
pub fn contains(&self, pattern: StrPattern) -> bool {
match pattern {
@@ -350,12 +355,10 @@ impl Debug for Str {
f.write_char('"')?;
for c in self.chars() {
match c {
- '\\' => f.write_str(r"\\")?,
+ '\0' => f.write_str("\\u{0}")?,
+ '\'' => f.write_str("'")?,
'"' => f.write_str(r#"\""#)?,
- '\n' => f.write_str(r"\n")?,
- '\r' => f.write_str(r"\r")?,
- '\t' => f.write_str(r"\t")?,
- _ => f.write_char(c)?,
+ _ => Display::fmt(&c.escape_debug(), f)?,
}
}
f.write_char('"')