mirror of
https://github.com/typst/typst
synced 2025-08-20 01:48:34 +08:00
Compare commits
5 Commits
6c44c2a70f
...
c782c0c3d5
Author | SHA1 | Date | |
---|---|---|---|
|
c782c0c3d5 | ||
|
36ecbb2c8d | ||
|
51ab5b815c | ||
|
dabef05488 | ||
|
d8ee74f5d3 |
@ -20,7 +20,7 @@ use crate::foundations::{
|
|||||||
///
|
///
|
||||||
/// You can call a function by writing a comma-separated list of function
|
/// You can call a function by writing a comma-separated list of function
|
||||||
/// _arguments_ enclosed in parentheses directly after the function name.
|
/// _arguments_ enclosed in parentheses directly after the function name.
|
||||||
/// Additionally, you can pass any number of trailing content blocks arguments
|
/// Additionally, you can pass any number of trailing content block arguments
|
||||||
/// to a function _after_ the normal argument list. If the normal argument list
|
/// to a function _after_ the normal argument list. If the normal argument list
|
||||||
/// would become empty, it can be omitted. Typst supports positional and named
|
/// would become empty, it can be omitted. Typst supports positional and named
|
||||||
/// arguments. The former are identified by position and type, while the latter
|
/// arguments. The former are identified by position and type, while the latter
|
||||||
|
@ -178,25 +178,39 @@ impl Str {
|
|||||||
self.0.len()
|
self.0.len()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Extracts the first grapheme cluster of the string.
|
/// Extracts the first grapheme cluster of the string. Fails with an error
|
||||||
/// Fails with an error if the string is empty.
|
/// 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]
|
#[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
|
self.0
|
||||||
.graphemes(true)
|
.graphemes(true)
|
||||||
.next()
|
.next()
|
||||||
.map(Into::into)
|
.map(Into::into)
|
||||||
|
.or(default)
|
||||||
.ok_or_else(string_is_empty)
|
.ok_or_else(string_is_empty)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Extracts the last grapheme cluster of the string.
|
/// Extracts the last grapheme cluster of the string. Fails with an error if
|
||||||
/// Fails with an error if the string is empty.
|
/// 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]
|
#[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
|
self.0
|
||||||
.graphemes(true)
|
.graphemes(true)
|
||||||
.next_back()
|
.next_back()
|
||||||
.map(Into::into)
|
.map(Into::into)
|
||||||
|
.or(default)
|
||||||
.ok_or_else(string_is_empty)
|
.ok_or_else(string_is_empty)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -205,7 +205,7 @@
|
|||||||
single or double quotes.
|
single or double quotes.
|
||||||
|
|
||||||
The value is always of type [string]($str). More complex data
|
The value is always of type [string]($str). More complex data
|
||||||
may be parsed manually using functions like [`json.decode`]($json.decode).
|
may be parsed manually using functions like [`json`]($json).
|
||||||
|
|
||||||
- name: sym
|
- name: sym
|
||||||
title: General
|
title: General
|
||||||
|
@ -103,6 +103,10 @@
|
|||||||
#test("Hello".last(), "o")
|
#test("Hello".last(), "o")
|
||||||
#test("🏳️🌈A🏳️⚧️".first(), "🏳️🌈")
|
#test("🏳️🌈A🏳️⚧️".first(), "🏳️🌈")
|
||||||
#test("🏳️🌈A🏳️⚧️".last(), "🏳️⚧️")
|
#test("🏳️🌈A🏳️⚧️".last(), "🏳️⚧️")
|
||||||
|
#test("hey".first(default: "d"), "h")
|
||||||
|
#test("".first(default: "d"), "d")
|
||||||
|
#test("hey".last(default: "d"), "y")
|
||||||
|
#test("".last(default: "d"), "d")
|
||||||
|
|
||||||
--- string-first-empty ---
|
--- string-first-empty ---
|
||||||
// Error: 2-12 string is empty
|
// Error: 2-12 string is empty
|
||||||
|
Loading…
x
Reference in New Issue
Block a user