Fix BLOB and string operations.
This commit is contained in:
@@ -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.
|
||||
|
Reference in New Issue
Block a user