Implement rev()
method on string (#2013)
This commit is contained in:
parent
cd13e55dd1
commit
45bd58fbaa
@ -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")?;
|
||||
|
@ -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)?
|
||||
|
@ -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.
|
||||
|
@ -210,6 +210,15 @@
|
||||
#test("a123c".split(regex("\d")), ("a", "", "", "c"))
|
||||
#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\"
|
||||
|
Loading…
x
Reference in New Issue
Block a user