New length command (strlen)

This commit is contained in:
sagie gur ari 2020-01-08 23:21:06 +00:00
parent 1fd5a00242
commit a2f425056e
7 changed files with 123 additions and 0 deletions

View file

@ -2,6 +2,7 @@
### v0.1.5
* New length command (strlen)
* New substring command #37
* New uname/os_family command #43
* Commands should accept empty string ("") inputs #47

View file

@ -47,6 +47,7 @@
* [std::string::IndexOf (indexof)](#std__string__IndexOf)
* [std::string::IsEmpty (is_empty)](#std__string__IsEmpty)
* [std::string::LastIndexOf (last_indexof)](#std__string__LastIndexOf)
* [std::string::Length (length, strlen)](#std__string__Length)
* [std::string::StartsWith (starts_with)](#std__string__StartsWith)
* [std::string::SubString (substring)](#std__string__SubString)
* [std::string::Trim (trim)](#std__string__Trim)
@ -1692,6 +1693,32 @@ index = last_indexof " some text " some
#### Aliases:
last_indexof
<a name="std__string__Length"></a>
## std::string::Length
```sh
var = length text
```
Returns the text length.
#### Parameters
The text to extract the length from.
#### Return Value
The text length value.
#### Examples
```sh
len = length "Hello World"
```
#### Aliases:
length, strlen
<a name="std__string__StartsWith"></a>
## std::string::StartsWith
```sh

View file

@ -0,0 +1,19 @@
```sh
var = length text
```
Returns the text length.
#### Parameters
The text to extract the length from.
#### Return Value
The text length value.
#### Examples
```sh
len = length "Hello World"
```

View file

@ -0,0 +1,40 @@
use crate::utils::pckg;
use duckscript::types::command::{Command, CommandResult};
#[cfg(test)]
#[path = "./mod_test.rs"]
mod mod_test;
struct CommandImpl {
package: String,
}
impl Command for CommandImpl {
fn name(&self) -> String {
pckg::concat(&self.package, "Length")
}
fn aliases(&self) -> Vec<String> {
vec!["length".to_string(), "strlen".to_string()]
}
fn help(&self) -> String {
include_str!("help.md").to_string()
}
fn run(&self, arguments: Vec<String>) -> CommandResult {
if arguments.is_empty() {
CommandResult::Error("No argument provided.".to_string())
} else {
let string_len = arguments[0].len();
CommandResult::Continue(Some(string_len.to_string()))
}
}
}
pub(crate) fn create(package: &str) -> Box<dyn Command> {
Box::new(CommandImpl {
package: package.to_string(),
})
}

View file

@ -0,0 +1,31 @@
use super::*;
use crate::test;
use crate::test::CommandValidation;
#[test]
fn common_functions() {
test::test_common_command_functions(create(""));
}
#[test]
fn run_no_args() {
test::run_script_and_error(vec![create("")], "out = length", "out");
}
#[test]
fn run_empty_text() {
test::run_script_and_validate(
vec![create("")],
r#"out = length """#,
CommandValidation::Match("out".to_string(), "0".to_string()),
);
}
#[test]
fn run_text() {
test::run_script_and_validate(
vec![create("")],
"out = length text",
CommandValidation::Match("out".to_string(), "4".to_string()),
);
}

View file

@ -4,6 +4,7 @@ mod equals;
mod indexof;
mod is_empty;
mod last_indexof;
mod length;
mod starts_with;
mod substring;
mod trim;
@ -25,6 +26,7 @@ pub(crate) fn load(commands: &mut Commands, parent: &str) -> Result<(), ScriptEr
commands.set(indexof::create(&package))?;
commands.set(is_empty::create(&package))?;
commands.set(last_indexof::create(&package))?;
commands.set(length::create(&package))?;
commands.set(starts_with::create(&package))?;
commands.set(substring::create(&package))?;
commands.set(trim::create(&package))?;

View file

@ -14,3 +14,6 @@ echo ${string}
# string is 'Hello W'
string = substring "Hello World" -4
echo ${string}
len = length "Hello World"
echo The text length is: ${len}