Use immutable strings.
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
use crate::def_package;
|
||||
use crate::module::FuncReturn;
|
||||
use crate::parser::INT;
|
||||
use crate::parser::{ImmutableString, INT};
|
||||
use crate::utils::StaticVec;
|
||||
|
||||
#[cfg(not(feature = "no_index"))]
|
||||
@@ -10,22 +10,21 @@ use crate::stdlib::{
|
||||
fmt::Display,
|
||||
format,
|
||||
string::{String, ToString},
|
||||
vec::Vec,
|
||||
};
|
||||
|
||||
fn prepend<T: Display>(x: T, y: String) -> FuncReturn<String> {
|
||||
Ok(format!("{}{}", x, y))
|
||||
fn prepend<T: Display>(x: T, y: ImmutableString) -> FuncReturn<ImmutableString> {
|
||||
Ok(format!("{}{}", x, y).into())
|
||||
}
|
||||
fn append<T: Display>(x: String, y: T) -> FuncReturn<String> {
|
||||
Ok(format!("{}{}", x, y))
|
||||
fn append<T: Display>(x: ImmutableString, y: T) -> FuncReturn<ImmutableString> {
|
||||
Ok(format!("{}{}", x, y).into())
|
||||
}
|
||||
fn sub_string(s: &mut String, start: INT, len: INT) -> FuncReturn<String> {
|
||||
fn sub_string(s: ImmutableString, start: INT, len: INT) -> FuncReturn<ImmutableString> {
|
||||
let offset = if s.is_empty() || len <= 0 {
|
||||
return Ok("".to_string());
|
||||
return Ok("".to_string().into());
|
||||
} else if start < 0 {
|
||||
0
|
||||
} else if (start as usize) >= s.chars().count() {
|
||||
return Ok("".to_string());
|
||||
return Ok("".to_string().into());
|
||||
} else {
|
||||
start as usize
|
||||
};
|
||||
@@ -38,37 +37,45 @@ fn sub_string(s: &mut String, start: INT, len: INT) -> FuncReturn<String> {
|
||||
len as usize
|
||||
};
|
||||
|
||||
Ok(chars.iter().skip(offset).take(len).cloned().collect())
|
||||
}
|
||||
fn crop_string(s: &mut String, start: INT, len: INT) -> FuncReturn<()> {
|
||||
let offset = if s.is_empty() || len <= 0 {
|
||||
s.clear();
|
||||
return Ok(());
|
||||
} else if start < 0 {
|
||||
0
|
||||
} else if (start as usize) >= s.chars().count() {
|
||||
s.clear();
|
||||
return Ok(());
|
||||
} else {
|
||||
start as usize
|
||||
};
|
||||
|
||||
let chars: StaticVec<_> = s.chars().collect();
|
||||
|
||||
let len = if offset + (len as usize) > chars.len() {
|
||||
chars.len() - offset
|
||||
} else {
|
||||
len as usize
|
||||
};
|
||||
|
||||
s.clear();
|
||||
|
||||
chars
|
||||
Ok(chars
|
||||
.iter()
|
||||
.skip(offset)
|
||||
.take(len)
|
||||
.for_each(|&ch| s.push(ch));
|
||||
.cloned()
|
||||
.collect::<String>()
|
||||
.into())
|
||||
}
|
||||
fn crop_string(s: &mut ImmutableString, start: INT, len: INT) -> FuncReturn<()> {
|
||||
let mut copy = (**s).clone();
|
||||
|
||||
let offset = if copy.is_empty() || len <= 0 {
|
||||
copy.clear();
|
||||
*s = copy.into();
|
||||
return Ok(());
|
||||
} else if start < 0 {
|
||||
0
|
||||
} else if (start as usize) >= copy.chars().count() {
|
||||
copy.clear();
|
||||
*s = copy.into();
|
||||
return Ok(());
|
||||
} else {
|
||||
start as usize
|
||||
};
|
||||
|
||||
let chars: StaticVec<_> = copy.chars().collect();
|
||||
|
||||
let len = if offset + (len as usize) > chars.len() {
|
||||
chars.len() - offset
|
||||
} else {
|
||||
len as usize
|
||||
};
|
||||
|
||||
*s = chars
|
||||
.iter()
|
||||
.skip(offset)
|
||||
.take(len)
|
||||
.collect::<String>()
|
||||
.into();
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -80,10 +87,10 @@ macro_rules! reg_op {
|
||||
|
||||
def_package!(crate:MoreStringPackage:"Additional string utilities, including string building.", lib, {
|
||||
reg_op!(lib, "+", append, INT, bool, char);
|
||||
lib.set_fn_2_mut( "+", |x: &mut String, _: ()| Ok(x.clone()));
|
||||
lib.set_fn_2( "+", |x: ImmutableString, _: ()| Ok(x));
|
||||
|
||||
reg_op!(lib, "+", prepend, INT, bool, char);
|
||||
lib.set_fn_2("+", |_: (), y: String| Ok(y));
|
||||
lib.set_fn_2("+", |_: (), y: ImmutableString| Ok(y));
|
||||
|
||||
#[cfg(not(feature = "only_i32"))]
|
||||
#[cfg(not(feature = "only_i64"))]
|
||||
@@ -100,22 +107,22 @@ def_package!(crate:MoreStringPackage:"Additional string utilities, including str
|
||||
|
||||
#[cfg(not(feature = "no_index"))]
|
||||
{
|
||||
lib.set_fn_2("+", |x: String, y: Array| Ok(format!("{}{:?}", x, y)));
|
||||
lib.set_fn_2("+", |x: Array, y: String| Ok(format!("{:?}{}", x, y)));
|
||||
lib.set_fn_2("+", |x: ImmutableString, y: Array| Ok(format!("{}{:?}", x, y)));
|
||||
lib.set_fn_2_mut("+", |x: &mut Array, y: ImmutableString| Ok(format!("{:?}{}", x, y)));
|
||||
}
|
||||
|
||||
lib.set_fn_1_mut("len", |s: &mut String| Ok(s.chars().count() as INT));
|
||||
lib.set_fn_2_mut(
|
||||
lib.set_fn_1("len", |s: ImmutableString| Ok(s.chars().count() as INT));
|
||||
lib.set_fn_2(
|
||||
"contains",
|
||||
|s: &mut String, ch: char| Ok(s.contains(ch)),
|
||||
|s: ImmutableString, ch: char| Ok(s.contains(ch)),
|
||||
);
|
||||
lib.set_fn_2_mut(
|
||||
lib.set_fn_2(
|
||||
"contains",
|
||||
|s: &mut String, find: String| Ok(s.contains(&find)),
|
||||
|s: ImmutableString, find: ImmutableString| Ok(s.contains(find.as_str())),
|
||||
);
|
||||
lib.set_fn_3_mut(
|
||||
lib.set_fn_3(
|
||||
"index_of",
|
||||
|s: &mut String, ch: char, start: INT| {
|
||||
|s: ImmutableString, ch: char, start: INT| {
|
||||
let start = if start < 0 {
|
||||
0
|
||||
} else if (start as usize) >= s.chars().count() {
|
||||
@@ -130,17 +137,17 @@ def_package!(crate:MoreStringPackage:"Additional string utilities, including str
|
||||
.unwrap_or(-1 as INT))
|
||||
},
|
||||
);
|
||||
lib.set_fn_2_mut(
|
||||
lib.set_fn_2(
|
||||
"index_of",
|
||||
|s: &mut String, ch: char| {
|
||||
|s: ImmutableString, ch: char| {
|
||||
Ok(s.find(ch)
|
||||
.map(|index| s[0..index].chars().count() as INT)
|
||||
.unwrap_or(-1 as INT))
|
||||
},
|
||||
);
|
||||
lib.set_fn_3_mut(
|
||||
lib.set_fn_3(
|
||||
"index_of",
|
||||
|s: &mut String, find: String, start: INT| {
|
||||
|s: ImmutableString, find: ImmutableString, start: INT| {
|
||||
let start = if start < 0 {
|
||||
0
|
||||
} else if (start as usize) >= s.chars().count() {
|
||||
@@ -150,109 +157,108 @@ def_package!(crate:MoreStringPackage:"Additional string utilities, including str
|
||||
};
|
||||
|
||||
Ok(s[start..]
|
||||
.find(&find)
|
||||
.find(find.as_str())
|
||||
.map(|index| s[0..start + index].chars().count() as INT)
|
||||
.unwrap_or(-1 as INT))
|
||||
},
|
||||
);
|
||||
lib.set_fn_2_mut(
|
||||
lib.set_fn_2(
|
||||
"index_of",
|
||||
|s: &mut String, find: String| {
|
||||
Ok(s.find(&find)
|
||||
|s: ImmutableString, find: ImmutableString| {
|
||||
Ok(s.find(find.as_str())
|
||||
.map(|index| s[0..index].chars().count() as INT)
|
||||
.unwrap_or(-1 as INT))
|
||||
},
|
||||
);
|
||||
lib.set_fn_1_mut("clear", |s: &mut String| {
|
||||
s.clear();
|
||||
lib.set_fn_1_mut("clear", |s: &mut ImmutableString| {
|
||||
*s = "".to_string().into();
|
||||
Ok(())
|
||||
});
|
||||
lib.set_fn_2_mut( "append", |s: &mut String, ch: char| {
|
||||
s.push(ch);
|
||||
lib.set_fn_2_mut("append", |s: &mut ImmutableString, ch: char| {
|
||||
let mut copy = (**s).clone();
|
||||
copy.push(ch);
|
||||
*s = copy.into();
|
||||
Ok(())
|
||||
});
|
||||
lib.set_fn_2_mut(
|
||||
"append",
|
||||
|s: &mut String, add: String| {
|
||||
s.push_str(&add);
|
||||
|s: &mut ImmutableString, add: ImmutableString| {
|
||||
let mut copy = (**s).clone();
|
||||
copy.push_str(add.as_str());
|
||||
*s = copy.into();
|
||||
Ok(())
|
||||
}
|
||||
);
|
||||
lib.set_fn_3_mut( "sub_string", sub_string);
|
||||
lib.set_fn_2_mut(
|
||||
lib.set_fn_3("sub_string", sub_string);
|
||||
lib.set_fn_2(
|
||||
"sub_string",
|
||||
|s: &mut String, start: INT| sub_string(s, start, s.len() as INT),
|
||||
|s: ImmutableString, start: INT| {
|
||||
let len = s.len() as INT;
|
||||
sub_string(s, start, len)
|
||||
},
|
||||
);
|
||||
lib.set_fn_3_mut( "crop", crop_string);
|
||||
lib.set_fn_3_mut("crop", crop_string);
|
||||
lib.set_fn_2_mut(
|
||||
"crop",
|
||||
|s: &mut String, start: INT| crop_string(s, start, s.len() as INT),
|
||||
|s: &mut ImmutableString, start: INT| crop_string(s, start, s.len() as INT),
|
||||
);
|
||||
lib.set_fn_2_mut(
|
||||
"truncate",
|
||||
|s: &mut String, len: INT| {
|
||||
|s: &mut ImmutableString, len: INT| {
|
||||
if len >= 0 {
|
||||
let chars: StaticVec<_> = s.chars().take(len as usize).collect();
|
||||
s.clear();
|
||||
chars.iter().for_each(|&ch| s.push(ch));
|
||||
*s = (**s).clone().chars().take(len as usize).collect::<String>().into();
|
||||
} else {
|
||||
s.clear();
|
||||
*s = "".to_string().into();
|
||||
}
|
||||
Ok(())
|
||||
},
|
||||
);
|
||||
lib.set_fn_3_mut(
|
||||
"pad",
|
||||
|s: &mut String, len: INT, ch: char| {
|
||||
for _ in 0..s.chars().count() - len as usize {
|
||||
s.push(ch);
|
||||
|s: &mut ImmutableString, len: INT, ch: char| {
|
||||
let mut copy = (**s).clone();
|
||||
for _ in 0..copy.chars().count() - len as usize {
|
||||
copy.push(ch);
|
||||
}
|
||||
*s = copy.into();
|
||||
Ok(())
|
||||
},
|
||||
);
|
||||
lib.set_fn_3_mut(
|
||||
"replace",
|
||||
|s: &mut String, find: String, sub: String| {
|
||||
let new_str = s.replace(&find, &sub);
|
||||
s.clear();
|
||||
s.push_str(&new_str);
|
||||
|s: &mut ImmutableString, find: ImmutableString, sub: ImmutableString| {
|
||||
*s = s.replace(find.as_str(), sub.as_str()).into();
|
||||
Ok(())
|
||||
},
|
||||
);
|
||||
lib.set_fn_3_mut(
|
||||
"replace",
|
||||
|s: &mut String, find: String, sub: char| {
|
||||
let new_str = s.replace(&find, &sub.to_string());
|
||||
s.clear();
|
||||
s.push_str(&new_str);
|
||||
|s: &mut ImmutableString, find: ImmutableString, sub: char| {
|
||||
*s = s.replace(find.as_str(), &sub.to_string()).into();
|
||||
Ok(())
|
||||
},
|
||||
);
|
||||
lib.set_fn_3_mut(
|
||||
"replace",
|
||||
|s: &mut String, find: char, sub: String| {
|
||||
let new_str = s.replace(&find.to_string(), &sub);
|
||||
s.clear();
|
||||
s.push_str(&new_str);
|
||||
|s: &mut ImmutableString, find: char, sub: ImmutableString| {
|
||||
*s = s.replace(&find.to_string(), sub.as_str()).into();
|
||||
Ok(())
|
||||
},
|
||||
);
|
||||
lib.set_fn_3_mut(
|
||||
"replace",
|
||||
|s: &mut String, find: char, sub: char| {
|
||||
let new_str = s.replace(&find.to_string(), &sub.to_string());
|
||||
s.clear();
|
||||
s.push_str(&new_str);
|
||||
|s: &mut ImmutableString, find: char, sub: char| {
|
||||
*s = s.replace(&find.to_string(), &sub.to_string()).into();
|
||||
Ok(())
|
||||
},
|
||||
);
|
||||
lib.set_fn_1_mut(
|
||||
"trim",
|
||||
|s: &mut String| {
|
||||
|s: &mut ImmutableString| {
|
||||
let trimmed = s.trim();
|
||||
|
||||
if trimmed.len() < s.len() {
|
||||
*s = trimmed.to_string();
|
||||
*s = trimmed.to_string().into();
|
||||
}
|
||||
Ok(())
|
||||
},
|
||||
|
Reference in New Issue
Block a user