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

@@ -8,7 +8,7 @@ use crate::{
};
#[cfg(feature = "no_std")]
use std::prelude::v1::*;
use std::{any::TypeId, mem};
use std::{any::TypeId, borrow::Cow, mem};
#[cfg(not(feature = "no_float"))]
use crate::{FLOAT, FLOAT_BYTES};
@@ -104,6 +104,27 @@ pub mod blob_functions {
pub fn to_array(blob: &mut Blob) -> Array {
blob.iter().map(|&ch| (ch as INT).into()).collect()
}
/// Convert the BLOB into a string.
///
/// The byte stream must be valid UTF-8, otherwise an error is raised.
///
/// # Example
///
/// ```rhai
/// let b = blob(5, 0x42);
///
/// let x = b.as_string();
///
/// print(x); // prints "FFFFF"
/// ```
pub fn as_string(blob: Blob) -> String {
let s = String::from_utf8_lossy(&blob);
match s {
Cow::Borrowed(_) => String::from_utf8(blob).unwrap(),
Cow::Owned(_) => s.into_owned(),
}
}
/// Return the length of the BLOB.
///
/// # Example
@@ -200,6 +221,7 @@ pub mod blob_functions {
///
/// print(b); // prints "[42]"
/// ```
#[rhai_fn(name = "push", name = "append")]
pub fn push(blob: &mut Blob, value: INT) {
blob.push((value & 0x000000ff) as u8);
}
@@ -235,13 +257,13 @@ pub mod blob_functions {
///
/// print(b); // prints "[424242424268656c 6c6f]"
/// ```
#[rhai_fn(name = "+=", name = "append")]
#[rhai_fn(name = "append")]
pub fn append_str(blob: &mut Blob, string: &str) {
if !string.is_empty() {
blob.extend(string.as_bytes());
}
}
/// Add a string (as UTF-8 encoded byte-stream) to the end of the BLOB
/// Add a character (as UTF-8 encoded byte-stream) to the end of the BLOB
///
/// # Example
///
@@ -252,38 +274,12 @@ pub mod blob_functions {
///
/// print(b); // prints "[424242424221]"
/// ```
#[rhai_fn(name = "+=", name = "append")]
#[rhai_fn(name = "append")]
pub fn append_char(blob: &mut Blob, character: char) {
let mut buf = [0_u8; 4];
let x = character.encode_utf8(&mut buf);
blob.extend(x.as_bytes());
}
/// Add another BLOB to the end of the BLOB, returning it as a new BLOB.
///
/// # Example
///
/// ```rhai
/// let b1 = blob(5, 0x42);
/// let b2 = blob(3, 0x11);
///
/// print(b1 + b2); // prints "[4242424242111111]"
///
/// print(b1); // prints "[4242424242]"
/// ```
#[rhai_fn(name = "+")]
pub fn concat(blob1: Blob, blob2: Blob) -> Blob {
if !blob2.is_empty() {
if blob1.is_empty() {
blob2
} else {
let mut blob = blob1;
blob.extend(blob2);
blob
}
} else {
blob1
}
}
/// Add a byte `value` to the BLOB at a particular `index` position.
///
/// * If `index` < 0, position counts from the end of the BLOB (`-1` is the last byte).

View File

@@ -4,20 +4,20 @@ use crate::{Module, Shared};
pub(crate) mod arithmetic;
pub(crate) mod array_basic;
mod bit_field;
pub(crate) mod bit_field;
pub(crate) mod blob_basic;
mod debugging;
mod fn_basic;
pub(crate) mod debugging;
pub(crate) mod fn_basic;
pub(crate) mod iter_basic;
mod lang_core;
mod logic;
mod map_basic;
mod math_basic;
mod pkg_core;
mod pkg_std;
mod string_basic;
mod string_more;
mod time_basic;
pub(crate) mod lang_core;
pub(crate) mod logic;
pub(crate) mod map_basic;
pub(crate) mod math_basic;
pub(crate) mod pkg_core;
pub(crate) mod pkg_std;
pub(crate) mod string_basic;
pub(crate) mod string_more;
pub(crate) mod time_basic;
pub use arithmetic::ArithmeticPackage;
#[cfg(not(feature = "no_index"))]

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.