mirror of
https://github.com/typst/typst
synced 2025-08-23 11:14:13 +08:00
Add default
argument for str.first
and str.last
This commit is contained in:
parent
22a57fcf5c
commit
d8ee74f5d3
@ -178,25 +178,39 @@ impl Str {
|
||||
self.0.len()
|
||||
}
|
||||
|
||||
/// Extracts the first grapheme cluster of the string.
|
||||
/// Fails with an error if the string is empty.
|
||||
/// Extracts the first grapheme cluster of the string. Fails with an error
|
||||
/// if the string is empty. Returns the default value if the string is empty
|
||||
/// or fails with an error is no default value was specified.
|
||||
#[func]
|
||||
pub fn first(&self) -> StrResult<Str> {
|
||||
pub fn first(
|
||||
&self,
|
||||
/// A default value to return if the string is empty.
|
||||
#[named]
|
||||
default: Option<Str>,
|
||||
) -> StrResult<Str> {
|
||||
self.0
|
||||
.graphemes(true)
|
||||
.next()
|
||||
.map(Into::into)
|
||||
.or(default)
|
||||
.ok_or_else(string_is_empty)
|
||||
}
|
||||
|
||||
/// Extracts the last grapheme cluster of the string.
|
||||
/// Fails with an error if the string is empty.
|
||||
/// Extracts the last grapheme cluster of the string. Fails with an error if
|
||||
/// the string is empty. Returns the default value if the string is empty or
|
||||
/// fails with an error is no default value was specified.
|
||||
#[func]
|
||||
pub fn last(&self) -> StrResult<Str> {
|
||||
pub fn last(
|
||||
&self,
|
||||
/// A default value to return if the string is empty.
|
||||
#[named]
|
||||
default: Option<Str>,
|
||||
) -> StrResult<Str> {
|
||||
self.0
|
||||
.graphemes(true)
|
||||
.next_back()
|
||||
.map(Into::into)
|
||||
.or(default)
|
||||
.ok_or_else(string_is_empty)
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user