rhai/src/packages/string_more.rs

408 lines
13 KiB
Rust
Raw Normal View History

2020-08-14 18:04:10 +02:00
#![allow(non_snake_case)]
use crate::plugin::*;
2020-04-24 06:39:24 +02:00
use crate::stdlib::{
any::TypeId, boxed::Box, format, mem, string::String, string::ToString, vec::Vec,
2020-04-24 06:39:24 +02:00
};
use crate::{def_package, Dynamic, ImmutableString, StaticVec, INT};
use super::string_basic::{print_with_func, FUNC_TO_STRING};
2020-04-22 08:55:40 +02:00
def_package!(crate:MoreStringPackage:"Additional string utilities, including string building.", lib, {
2020-09-13 16:12:11 +02:00
combine_with_exported_module!(lib, "string", string_functions);
2020-06-16 03:34:30 +02:00
// Register string iterator
lib.set_iter(
TypeId::of::<ImmutableString>(),
2021-03-07 15:33:02 +01:00
|string| Box::new(string.cast::<ImmutableString>().chars().collect::<Vec<_>>().into_iter().map(Into::into))
2020-06-16 03:34:30 +02:00
);
2020-04-21 17:01:10 +02:00
});
2020-08-14 18:04:10 +02:00
#[export_module]
mod string_functions {
use crate::ImmutableString;
#[rhai_fn(name = "+", name = "append")]
pub fn add_append(ctx: NativeCallContext, string: &str, mut item: Dynamic) -> ImmutableString {
let s = print_with_func(FUNC_TO_STRING, &ctx, &mut item);
format!("{}{}", string, s).into()
}
#[rhai_fn(name = "+", pure)]
pub fn add_prepend(
ctx: NativeCallContext,
item: &mut Dynamic,
string: &str,
) -> ImmutableString {
let s = print_with_func(FUNC_TO_STRING, &ctx, item);
format!("{}{}", s, string).into()
}
#[rhai_fn(name = "+")]
pub fn add_append_unit(string: ImmutableString, _item: ()) -> ImmutableString {
string
}
#[rhai_fn(name = "+")]
pub fn add_prepend_unit(_item: (), string: ImmutableString) -> ImmutableString {
string
}
#[rhai_fn(name = "+=")]
pub fn add_append_assign_unit(_string: &mut ImmutableString, _item: ()) {}
#[rhai_fn(name = "len", get = "len")]
pub fn len(string: &str) -> INT {
string.chars().count() as INT
2020-08-14 18:04:10 +02:00
}
2021-03-07 15:33:02 +01:00
#[rhai_fn(name = "bytes", get = "bytes")]
pub fn bytes(string: &str) -> INT {
string.len() as INT
}
pub fn remove(string: &mut ImmutableString, sub_string: ImmutableString) {
*string -= sub_string;
}
#[rhai_fn(name = "remove")]
pub fn remove_char(string: &mut ImmutableString, character: char) {
*string -= character;
}
pub fn clear(string: &mut ImmutableString) {
string.make_mut().clear();
2020-08-14 18:04:10 +02:00
}
pub fn truncate(string: &mut ImmutableString, len: INT) {
2020-08-14 18:04:10 +02:00
if len > 0 {
let chars: StaticVec<_> = string.chars().collect();
let copy = string.make_mut();
2020-08-14 18:04:10 +02:00
copy.clear();
copy.extend(chars.into_iter().take(len as usize));
} else {
string.make_mut().clear();
2020-08-14 18:04:10 +02:00
}
}
pub fn trim(string: &mut ImmutableString) {
let trimmed = string.trim();
2020-08-14 18:04:10 +02:00
if trimmed.len() < string.len() {
*string = trimmed.to_string().into();
2020-08-14 18:04:10 +02:00
}
}
#[rhai_fn(name = "index_of")]
pub fn index_of_char_starting_from(string: &str, character: char, start: INT) -> INT {
2020-08-14 18:04:10 +02:00
let start = if start < 0 {
0
} else if start as usize >= string.chars().count() {
2020-08-14 18:04:10 +02:00
return -1 as INT;
} else {
string
.chars()
.take(start as usize)
.collect::<String>()
.len()
2020-08-14 18:04:10 +02:00
};
string[start..]
.find(character)
.map(|index| string[0..start + index].chars().count() as INT)
2020-08-14 18:04:10 +02:00
.unwrap_or(-1 as INT)
}
#[rhai_fn(name = "index_of")]
pub fn index_of_char(string: &str, character: char) -> INT {
string
.find(character)
.map(|index| string[0..index].chars().count() as INT)
2020-08-14 18:04:10 +02:00
.unwrap_or(-1 as INT)
}
#[rhai_fn(name = "index_of")]
pub fn index_of_string_starting_from(string: &str, find_string: &str, start: INT) -> INT {
2020-08-14 18:04:10 +02:00
let start = if start < 0 {
0
} else if start as usize >= string.chars().count() {
2020-08-14 18:04:10 +02:00
return -1 as INT;
} else {
string
.chars()
.take(start as usize)
.collect::<String>()
.len()
2020-08-14 18:04:10 +02:00
};
string[start..]
.find(find_string)
.map(|index| string[0..start + index].chars().count() as INT)
2020-08-14 18:04:10 +02:00
.unwrap_or(-1 as INT)
}
#[rhai_fn(name = "index_of")]
pub fn index_of(string: &str, find_string: &str) -> INT {
string
.find(find_string)
.map(|index| string[0..index].chars().count() as INT)
2020-08-14 18:04:10 +02:00
.unwrap_or(-1 as INT)
}
pub fn sub_string(string: &str, start: INT, len: INT) -> ImmutableString {
let offset = if string.is_empty() || len <= 0 {
2020-08-14 18:04:10 +02:00
return "".to_string().into();
} else if start < 0 {
0
} else if start as usize >= string.chars().count() {
2020-08-14 18:04:10 +02:00
return "".to_string().into();
} else {
start as usize
};
let chars: StaticVec<_> = string.chars().collect();
2020-08-14 18:04:10 +02:00
2020-10-18 16:36:58 +02:00
let len = if offset + len as usize > chars.len() {
2020-08-14 18:04:10 +02:00
chars.len() - offset
} else {
len as usize
};
chars
.iter()
.skip(offset)
.take(len)
.cloned()
.collect::<String>()
.into()
}
#[rhai_fn(name = "sub_string")]
pub fn sub_string_starting_from(string: &str, start: INT) -> ImmutableString {
let len = string.len() as INT;
sub_string(string, start, len)
2020-08-14 18:04:10 +02:00
}
#[rhai_fn(name = "crop")]
pub fn crop(string: &mut ImmutableString, start: INT, len: INT) {
let offset = if string.is_empty() || len <= 0 {
string.make_mut().clear();
2020-08-14 18:04:10 +02:00
return;
} else if start < 0 {
0
} else if start as usize >= string.chars().count() {
string.make_mut().clear();
2020-08-14 18:04:10 +02:00
return;
} else {
start as usize
};
let chars: StaticVec<_> = string.chars().collect();
2020-08-14 18:04:10 +02:00
2020-10-18 16:36:58 +02:00
let len = if offset + len as usize > chars.len() {
2020-08-14 18:04:10 +02:00
chars.len() - offset
} else {
len as usize
};
let copy = string.make_mut();
2020-08-14 18:04:10 +02:00
copy.clear();
copy.extend(chars.iter().skip(offset).take(len));
}
#[rhai_fn(name = "crop")]
pub fn crop_string_starting_from(string: &mut ImmutableString, start: INT) {
crop(string, start, string.len() as INT);
2020-08-14 18:04:10 +02:00
}
#[rhai_fn(name = "replace")]
pub fn replace(string: &mut ImmutableString, find_string: &str, substitute_string: &str) {
*string = string.replace(find_string, substitute_string).into();
2020-08-14 18:04:10 +02:00
}
#[rhai_fn(name = "replace")]
pub fn replace_string_with_char(
string: &mut ImmutableString,
find_string: &str,
substitute_character: char,
) {
*string = string
.replace(find_string, &substitute_character.to_string())
.into();
2020-08-14 18:04:10 +02:00
}
#[rhai_fn(name = "replace")]
pub fn replace_char_with_string(
string: &mut ImmutableString,
find_character: char,
substitute_string: &str,
) {
*string = string
.replace(&find_character.to_string(), substitute_string)
.into();
2020-08-14 18:04:10 +02:00
}
#[rhai_fn(name = "replace")]
pub fn replace_char(
string: &mut ImmutableString,
find_character: char,
substitute_character: char,
) {
*string = string
.replace(
&find_character.to_string(),
&substitute_character.to_string(),
)
.into();
2020-08-14 18:04:10 +02:00
}
2020-10-19 17:49:01 +02:00
2020-10-18 16:36:58 +02:00
#[rhai_fn(return_raw)]
pub fn pad(
2020-11-02 04:04:45 +01:00
_ctx: NativeCallContext,
string: &mut ImmutableString,
2020-10-18 16:36:58 +02:00
len: INT,
character: char,
2021-03-22 04:18:09 +01:00
) -> Result<(), Box<crate::EvalAltResult>> {
2020-10-18 16:36:58 +02:00
// Check if string will be over max size limit
#[cfg(not(feature = "unchecked"))]
2020-11-02 04:04:45 +01:00
if _ctx.engine().max_string_size() > 0 && len as usize > _ctx.engine().max_string_size() {
2020-11-16 09:28:04 +01:00
return crate::EvalAltResult::ErrorDataTooLarge(
"Length of string".to_string(),
2020-11-20 09:52:28 +01:00
crate::Position::NONE,
2020-11-16 09:28:04 +01:00
)
.into();
2020-10-18 16:36:58 +02:00
}
if len > 0 {
let orig_len = string.chars().count();
2020-10-18 16:36:58 +02:00
if len as usize > orig_len {
let p = string.make_mut();
2020-10-18 16:36:58 +02:00
for _ in 0..(len as usize - orig_len) {
p.push(character);
2020-10-18 16:36:58 +02:00
}
#[cfg(not(feature = "unchecked"))]
if _ctx.engine().max_string_size() > 0
&& string.len() > _ctx.engine().max_string_size()
2020-10-18 16:36:58 +02:00
{
2020-11-16 09:28:04 +01:00
return crate::EvalAltResult::ErrorDataTooLarge(
2020-10-18 16:36:58 +02:00
"Length of string".to_string(),
2020-11-20 09:52:28 +01:00
crate::Position::NONE,
2020-10-18 16:36:58 +02:00
)
.into();
}
}
}
2021-03-22 04:18:09 +01:00
Ok(())
2020-10-18 16:36:58 +02:00
}
#[rhai_fn(name = "pad", return_raw)]
pub fn pad_with_string(
2020-11-02 04:04:45 +01:00
_ctx: NativeCallContext,
string: &mut ImmutableString,
2020-10-18 16:36:58 +02:00
len: INT,
padding: &str,
2021-03-22 04:18:09 +01:00
) -> Result<(), Box<crate::EvalAltResult>> {
2020-10-18 16:36:58 +02:00
// Check if string will be over max size limit
#[cfg(not(feature = "unchecked"))]
2020-11-02 04:04:45 +01:00
if _ctx.engine().max_string_size() > 0 && len as usize > _ctx.engine().max_string_size() {
2020-11-16 09:28:04 +01:00
return crate::EvalAltResult::ErrorDataTooLarge(
"Length of string".to_string(),
2020-11-20 09:52:28 +01:00
crate::Position::NONE,
2020-11-16 09:28:04 +01:00
)
.into();
2020-10-18 16:36:58 +02:00
}
if len > 0 {
let mut str_len = string.chars().count();
2020-10-18 16:36:58 +02:00
let padding_len = padding.chars().count();
if len as usize > str_len {
let p = string.make_mut();
2020-10-18 16:36:58 +02:00
while str_len < len as usize {
if str_len + padding_len <= len as usize {
p.push_str(padding);
str_len += padding_len;
} else {
p.extend(padding.chars().take(len as usize - str_len));
str_len = len as usize;
}
2020-10-18 16:36:58 +02:00
}
#[cfg(not(feature = "unchecked"))]
if _ctx.engine().max_string_size() > 0
&& string.len() > _ctx.engine().max_string_size()
2020-10-18 16:36:58 +02:00
{
2020-11-16 09:28:04 +01:00
return crate::EvalAltResult::ErrorDataTooLarge(
2020-10-18 16:36:58 +02:00
"Length of string".to_string(),
2020-11-20 09:52:28 +01:00
crate::Position::NONE,
2020-10-18 16:36:58 +02:00
)
.into();
}
}
}
2021-03-22 04:18:09 +01:00
Ok(())
2020-10-18 16:36:58 +02:00
}
2020-08-22 16:26:49 +02:00
#[cfg(not(feature = "no_index"))]
pub mod arrays {
use crate::stdlib::vec;
use crate::{Array, ImmutableString};
#[rhai_fn(name = "split")]
2021-02-23 05:52:47 +01:00
pub fn chars(string: &str) -> Array {
string.chars().map(Into::<Dynamic>::into).collect()
}
#[rhai_fn(name = "split")]
pub fn split_at(string: ImmutableString, start: INT) -> Array {
if start <= 0 {
vec!["".into(), string.into()]
} else {
let prefix: String = string.chars().take(start as usize).collect();
let prefix_len = prefix.len();
vec![prefix.into(), string[prefix_len..].into()]
}
}
pub fn split(string: &str, delimiter: &str) -> Array {
string.split(delimiter).map(Into::<Dynamic>::into).collect()
2020-09-25 17:02:49 +02:00
}
#[rhai_fn(name = "split")]
pub fn splitn(string: &str, delimiter: &str, segments: INT) -> Array {
let pieces: usize = if segments < 1 { 1 } else { segments as usize };
string
.splitn(pieces, delimiter)
.map(Into::<Dynamic>::into)
.collect()
}
#[rhai_fn(name = "split")]
pub fn split_char(string: &str, delimiter: char) -> Array {
string.split(delimiter).map(Into::<Dynamic>::into).collect()
2020-09-25 17:02:49 +02:00
}
#[rhai_fn(name = "split")]
pub fn splitn_char(string: &str, delimiter: char, segments: INT) -> Array {
let pieces: usize = if segments < 1 { 1 } else { segments as usize };
string
.splitn(pieces, delimiter)
.map(Into::<Dynamic>::into)
.collect()
}
#[rhai_fn(name = "split_rev")]
pub fn rsplit(string: &str, delimiter: &str) -> Array {
string
.rsplit(delimiter)
.map(Into::<Dynamic>::into)
.collect()
}
#[rhai_fn(name = "split_rev")]
pub fn rsplitn(string: &str, delimiter: &str, segments: INT) -> Array {
let pieces: usize = if segments < 1 { 1 } else { segments as usize };
string
.rsplitn(pieces, delimiter)
.map(Into::<Dynamic>::into)
.collect()
}
#[rhai_fn(name = "split_rev")]
pub fn rsplit_char(string: &str, delimiter: char) -> Array {
string
.rsplit(delimiter)
.map(Into::<Dynamic>::into)
.collect()
}
#[rhai_fn(name = "split_rev")]
pub fn rsplitn_char(string: &str, delimiter: char, segments: INT) -> Array {
let pieces: usize = if segments < 1 { 1 } else { segments as usize };
string
.rsplitn(pieces, delimiter)
.map(Into::<Dynamic>::into)
.collect()
}
}
}