diff options
| author | Kevin K <xkeviota@gmail.com> | 2023-08-26 17:35:52 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-08-26 17:35:52 +0200 |
| commit | 45bd58fbaac4419a45246865cf44ded7920d5b84 (patch) | |
| tree | 871fe5a273b5cd46880e76200284661bf6c7c115 | |
| parent | cd13e55dd1577fd98943982b686f245940d5ab72 (diff) | |
Implement `rev()` method on string (#2013)
| -rw-r--r-- | crates/typst/src/eval/methods.rs | 1 | ||||
| -rw-r--r-- | crates/typst/src/eval/str.rs | 5 | ||||
| -rw-r--r-- | docs/reference/types.md | 5 | ||||
| -rw-r--r-- | tests/typ/compiler/string.typ | 9 |
4 files changed, 20 insertions, 0 deletions
diff --git a/crates/typst/src/eval/methods.rs b/crates/typst/src/eval/methods.rs index 6127a807..ed11d8a7 100644 --- a/crates/typst/src/eval/methods.rs +++ b/crates/typst/src/eval/methods.rs @@ -82,6 +82,7 @@ pub fn call( let count = args.named("count")?; string.replace(vm, pattern, with, count)?.into_value() } + "rev" => string.rev().into_value(), "trim" => { let pattern = args.eat()?; let at = args.named("at")?; diff --git a/crates/typst/src/eval/str.rs b/crates/typst/src/eval/str.rs index 30139f10..b21d2ff9 100644 --- a/crates/typst/src/eval/str.rs +++ b/crates/typst/src/eval/str.rs @@ -324,6 +324,11 @@ impl Str { Ok(Self(self.0.repeat(n))) } + /// Reverse the string. + pub fn rev(&self) -> Self { + self.as_str().graphemes(true).rev().collect::<String>().into() + } + /// Resolve an index or throw an out of bounds error. fn locate(&self, index: i64) -> StrResult<usize> { self.locate_opt(index)? diff --git a/docs/reference/types.md b/docs/reference/types.md index b0ee1bd1..365593f3 100644 --- a/docs/reference/types.md +++ b/docs/reference/types.md @@ -689,6 +689,11 @@ string and returns the resulting string. If given, only the first `count` matches of the pattern are placed. - returns: string +### rev() +Reverses the grapheme clusters and returns the resulting string. + +- returns: string + ### trim() Removes matches of a pattern from one or both sides of the string, once or repeatedly and returns the resulting string. diff --git a/tests/typ/compiler/string.typ b/tests/typ/compiler/string.typ index 4241361a..b0708979 100644 --- a/tests/typ/compiler/string.typ +++ b/tests/typ/compiler/string.typ @@ -211,5 +211,14 @@ #test("a123c".split(regex("\d+")), ("a", "c")) --- +// Test the `rev` method. +#test("abc".rev(), "cba") +#test("ax̂e".rev(), "ex̂a") + +--- +// Error: 12-15 unknown variable: arg +#"abc".rev(arg) + +--- // Error: 2-2:1 unclosed string #"hello\" |
