BLOB + string -> string.

This commit is contained in:
Stephen Chung 2022-07-21 14:01:10 +08:00
parent 753e527cbb
commit 55dd55322b

View File

@ -6,9 +6,6 @@ use std::{any::TypeId, mem};
use super::string_basic::{print_with_func, FUNC_TO_STRING}; use super::string_basic::{print_with_func, FUNC_TO_STRING};
#[cfg(not(feature = "no_index"))]
use crate::Blob;
def_package! { def_package! {
/// Package of additional string utilities over [`BasicStringPackage`][super::BasicStringPackage] /// Package of additional string utilities over [`BasicStringPackage`][super::BasicStringPackage]
pub MoreStringPackage(lib) { pub MoreStringPackage(lib) {
@ -86,6 +83,8 @@ mod string_functions {
#[cfg(not(feature = "no_index"))] #[cfg(not(feature = "no_index"))]
pub mod blob_functions { pub mod blob_functions {
use crate::Blob;
#[rhai_fn(name = "+", pure)] #[rhai_fn(name = "+", pure)]
pub fn add_append(string: &mut ImmutableString, utf8: Blob) -> ImmutableString { pub fn add_append(string: &mut ImmutableString, utf8: Blob) -> ImmutableString {
if utf8.is_empty() { if utf8.is_empty() {
@ -115,13 +114,18 @@ mod string_functions {
} }
} }
#[rhai_fn(name = "+")] #[rhai_fn(name = "+")]
pub fn add_prepend(utf8: Blob, string: ImmutableString) -> Blob { pub fn add_prepend(utf8: Blob, string: ImmutableString) -> ImmutableString {
let mut blob = utf8; let s = String::from_utf8_lossy(&utf8);
let mut s = match s {
std::borrow::Cow::Borrowed(_) => String::from_utf8(utf8).unwrap(),
std::borrow::Cow::Owned(_) => s.into_owned(),
};
if !string.is_empty() { if !string.is_empty() {
blob.extend(string.as_bytes()); s.push_str(&string);
} }
blob
s.into()
} }
} }