Add substring function

This commit is contained in:
elkowar 2023-08-16 16:24:04 +02:00
parent 9d0b667dea
commit 4450ef55fa
No known key found for this signature in database
GPG Key ID: 50E76B4711E4C3E4
3 changed files with 14 additions and 0 deletions

View File

@ -18,6 +18,7 @@ All notable changes to eww will be listed here, starting at changes since versio
- Add `justify` property to the label widget, allowing text justification (By: n3oney)
- Add `EWW_TIME` magic variable (By: Erenoit)
- Add trigonometric functions (`sin`, `cos`, `tan`, `cot`) and degree/radian conversions (`degtorad`, `radtodeg`) (By: end-4)
- Add `substring` function to simplexpr
## [0.4.0] (04.09.2022)

View File

@ -379,6 +379,18 @@ fn call_expr_function(name: &str, args: Vec<DynVal>) -> Result<DynVal, EvalError
}
_ => Err(EvalError::WrongArgCount(name.to_string())),
},
"substring" => match args.as_slice() {
[string, start, len] => {
let result: String = string
.as_string()?
.chars()
.skip(start.as_i32()?.max(0) as usize)
.take(len.as_i32()?.max(0) as usize)
.collect();
Ok(DynVal::from(result))
}
_ => Err(EvalError::WrongArgCount(name.to_string())),
},
"search" => match args.as_slice() {
[string, pattern] => {
use serde_json::Value;

View File

@ -47,6 +47,7 @@ Supported currently are the following features:
- `matches(string, regex)`: check if a given string matches a given regex (returns bool)
- `captures(string, regex)`: Get the captures of a given regex in a string (returns array)
- `strlength(value)`: Gets the length of the string
- `substring(string, start, length)`: Return a substring of given length starting at the given index
- `arraylength(value)`: Gets the length of the array
- `objectlength(value)`: Gets the amount of entries in the object
- `jq(value, jq_filter_string)`: run a [jq](https://stedolan.github.io/jq/manual/) style command on a json value. (Uses [jaq](https://crates.io/crates/jaq) internally).