Fix BLOB and string operations.

This commit is contained in:
Stephen Chung
2022-07-20 21:17:21 +08:00
parent 8215c75a17
commit 753e527cbb
7 changed files with 289 additions and 105 deletions

View File

@@ -87,25 +87,42 @@ mod string_functions {
#[cfg(not(feature = "no_index"))]
pub mod blob_functions {
#[rhai_fn(name = "+", pure)]
pub fn add_append_blob(string: &mut ImmutableString, utf8: Blob) -> ImmutableString {
pub fn add_append(string: &mut ImmutableString, utf8: Blob) -> ImmutableString {
if utf8.is_empty() {
string.clone()
} else if string.is_empty() {
String::from_utf8_lossy(&utf8).into_owned().into()
return string.clone();
}
let s = String::from_utf8_lossy(&utf8);
if string.is_empty() {
match s {
std::borrow::Cow::Borrowed(_) => String::from_utf8(utf8).unwrap(),
std::borrow::Cow::Owned(_) => s.into_owned(),
}
.into()
} else {
let mut s = crate::SmartString::from(string.as_str());
s.push_str(&String::from_utf8_lossy(&utf8));
s.into()
let mut x = SmartString::from(string.as_str());
x.push_str(s.as_ref());
x.into()
}
}
#[rhai_fn(name = "append")]
pub fn add_blob(string: &mut ImmutableString, utf8: Blob) {
#[rhai_fn(name = "+=", name = "append")]
pub fn add(string: &mut ImmutableString, utf8: Blob) {
let mut s = crate::SmartString::from(string.as_str());
if !utf8.is_empty() {
s.push_str(&String::from_utf8_lossy(&utf8));
*string = s.into();
}
}
#[rhai_fn(name = "+")]
pub fn add_prepend(utf8: Blob, string: ImmutableString) -> Blob {
let mut blob = utf8;
if !string.is_empty() {
blob.extend(string.as_bytes());
}
blob
}
}
/// Return the length of the string, in number of characters.