Skip to content

Commit da4760c

Browse files
committed
feat: add trim function for strings
1 parent 2ab86fa commit da4760c

1 file changed

Lines changed: 23 additions & 0 deletions

File tree

  • src/builtin_function/strings
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
use crate::builtin_function::utils::{param_to_datatype, returns};
2+
use crate::common::data_type::DataType;
3+
use crate::common::errors::{ChapError, Result};
4+
use crate::{common::executable::ExecutableLine, runtime::Runtime};
5+
6+
pub fn trim(runtime: &mut Runtime, executable: &ExecutableLine) -> Result<()> {
7+
let p1 = param_to_datatype(runtime, executable.params.first(), executable.line_number)?;
8+
9+
let result = match p1 {
10+
DataType::String(s) => DataType::String(s.trim().to_string()),
11+
_ => {
12+
return Err(ChapError::runtime_with_msg(
13+
executable.line_number,
14+
format!(
15+
"{} function input param should be string",
16+
executable.function_name
17+
),
18+
));
19+
}
20+
};
21+
22+
returns(runtime, executable, result)
23+
}

0 commit comments

Comments
 (0)