Use immutable strings.

This commit is contained in:
Stephen Chung
2020-05-25 13:44:28 +08:00
parent bbed4c6ef4
commit 99ea2b33c9
14 changed files with 251 additions and 191 deletions

View File

@@ -1,7 +1,7 @@
use crate::def_package;
use crate::engine::{FUNC_TO_STRING, KEYWORD_DEBUG, KEYWORD_PRINT};
use crate::module::FuncReturn;
use crate::parser::INT;
use crate::parser::{ImmutableString, INT};
#[cfg(not(feature = "no_index"))]
use crate::engine::Array;
@@ -16,15 +16,15 @@ use crate::stdlib::{
};
// Register print and debug
fn to_debug<T: Debug>(x: &mut T) -> FuncReturn<String> {
Ok(format!("{:?}", x))
fn to_debug<T: Debug>(x: &mut T) -> FuncReturn<ImmutableString> {
Ok(format!("{:?}", x).into())
}
fn to_string<T: Display>(x: &mut T) -> FuncReturn<String> {
Ok(format!("{}", x))
fn to_string<T: Display>(x: &mut T) -> FuncReturn<ImmutableString> {
Ok(format!("{}", x).into())
}
#[cfg(not(feature = "no_object"))]
fn format_map(x: &mut Map) -> FuncReturn<String> {
Ok(format!("#{:?}", x))
fn format_map(x: &mut Map) -> FuncReturn<ImmutableString> {
Ok(format!("#{:?}", x).into())
}
macro_rules! reg_op {
@@ -41,10 +41,10 @@ def_package!(crate:BasicStringPackage:"Basic string utilities, including printin
lib.set_fn_1(KEYWORD_PRINT, |_: ()| Ok("".to_string()));
lib.set_fn_1(FUNC_TO_STRING, |_: ()| Ok("".to_string()));
lib.set_fn_1_mut(KEYWORD_PRINT, |s: &mut String| Ok(s.clone()));
lib.set_fn_1_mut(FUNC_TO_STRING, |s: &mut String| Ok(s.clone()));
lib.set_fn_1(KEYWORD_PRINT, |s: ImmutableString| Ok(s.clone()));
lib.set_fn_1(FUNC_TO_STRING, |s: ImmutableString| Ok(s.clone()));
reg_op!(lib, KEYWORD_DEBUG, to_debug, INT, bool, (), char, String);
reg_op!(lib, KEYWORD_DEBUG, to_debug, INT, bool, (), char, ImmutableString);
#[cfg(not(feature = "only_i32"))]
#[cfg(not(feature = "only_i64"))]
@@ -80,26 +80,32 @@ def_package!(crate:BasicStringPackage:"Basic string utilities, including printin
lib.set_fn_2(
"+",
|mut s: String, ch: char| {
|s: ImmutableString, ch: char| {
let mut s = (*s).clone();
s.push(ch);
Ok(s)
},
);
lib.set_fn_2(
"+",
|mut s: String, s2: String| {
s.push_str(&s2);
|s:ImmutableString, s2:ImmutableString| {
let mut s = (*s).clone();
s.push_str(s2.as_str());
Ok(s)
},
);
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, s2: String| {
s.push_str(&s2);
|s: &mut ImmutableString, s2: ImmutableString| {
let mut copy = (**s).clone();
copy.push_str(s2.as_str());
*s = copy.into();
Ok(())
}
);