Fix builds.

This commit is contained in:
Stephen Chung 2021-12-12 17:26:15 +08:00
parent 6af406bafc
commit 5eaf85254f
3 changed files with 99 additions and 68 deletions

View File

@ -205,22 +205,7 @@ mod array_functions {
}
#[rhai_fn(name = "extract")]
pub fn extract_tail(array: &mut Array, start: INT) -> Array {
if array.is_empty() {
return Array::new();
}
let start = if start < 0 {
let arr_len = array.len();
start
.checked_abs()
.map_or(0, |n| arr_len - (n as usize).min(arr_len))
} else if start as usize >= array.len() {
return Array::new();
} else {
start as usize
};
array[start..].to_vec()
extract(array, start, INT::MAX)
}
#[rhai_fn(name = "split")]
pub fn split_at(array: &mut Array, start: INT) -> Array {

View File

@ -189,13 +189,13 @@ mod blob_functions {
*blob = replace;
return;
}
let blob_len = blob.len();
let start = if start < 0 {
let blob_len = blob.len();
start
.checked_abs()
.map_or(0, |n| blob_len - (n as usize).min(blob_len))
} else if start as usize >= blob.len() {
} else if start as usize >= blob_len {
blob.extend(replace.into_iter());
return;
} else {
@ -204,8 +204,8 @@ mod blob_functions {
let len = if len < 0 {
0
} else if len as usize > blob.len() - start {
blob.len() - start
} else if len as usize > blob_len - start {
blob_len - start
} else {
len as usize
};
@ -216,50 +216,29 @@ mod blob_functions {
if blob.is_empty() || len <= 0 {
return Blob::new();
}
let blob_len = blob.len();
let start = if start < 0 {
let blob_len = blob.len();
start
.checked_abs()
.map_or(0, |n| blob_len - (n as usize).min(blob_len))
} else if start as usize >= blob.len() {
} else if start as usize >= blob_len {
return Blob::new();
} else {
start as usize
};
let len = if len <= 0 {
0
} else if len as usize > blob.len() - start {
blob.len() - start
let len = if len as usize > blob_len - start {
blob_len - start
} else {
len as usize
};
if len == 0 {
Blob::new()
} else {
blob[start..start + len].to_vec()
}
blob[start..start + len].to_vec()
}
#[rhai_fn(name = "extract")]
pub fn extract_tail(blob: &mut Blob, start: INT) -> Blob {
if blob.is_empty() {
return Blob::new();
}
let start = if start < 0 {
let blob_len = blob.len();
start
.checked_abs()
.map_or(0, |n| blob_len - (n as usize).min(blob_len))
} else if start as usize >= blob.len() {
return Blob::new();
} else {
start as usize
};
blob[start..].to_vec()
extract(blob, start, INT::MAX)
}
#[rhai_fn(name = "split")]
pub fn split_at(blob: &mut Blob, start: INT) -> Blob {
@ -289,22 +268,20 @@ mod blob_functions {
if blob.is_empty() || len <= 0 {
return Blob::new();
}
let blob_len = blob.len();
let start = if start < 0 {
let blob_len = blob.len();
start
.checked_abs()
.map_or(0, |n| blob_len - (n as usize).min(blob_len))
} else if start as usize >= blob.len() {
} else if start as usize >= blob_len {
return Blob::new();
} else {
start as usize
};
let len = if len <= 0 {
0
} else if len as usize > blob.len() - start {
blob.len() - start
let len = if len as usize > blob_len - start {
blob_len - start
} else {
len as usize
};
@ -315,22 +292,20 @@ mod blob_functions {
if blob.is_empty() || len <= 0 {
return Blob::new();
}
let blob_len = blob.len();
let start = if start < 0 {
let blob_len = blob.len();
start
.checked_abs()
.map_or(0, |n| blob_len - (n as usize).min(blob_len))
} else if start as usize >= blob.len() {
} else if start as usize >= blob_len {
return mem::take(blob);
} else {
start as usize
};
let len = if len < 0 {
0
} else if len as usize > blob.len() - start {
blob.len() - start
let len = if len as usize > blob_len - start {
blob_len - start
} else {
len as usize
};
@ -340,19 +315,25 @@ mod blob_functions {
drained
}
pub fn contains(blob: &mut Blob, value: Dynamic) -> bool {
if blob.is_empty() {
return false;
}
let value = match value.as_int() {
Ok(value) => value as u8,
_ => return false,
};
blob.contains(&value)
}
#[rhai_fn(name = "==", pure)]
pub fn equals(blob1: &mut Blob, blob2: Blob) -> bool {
if blob1.len() != blob2.len() {
false
} else if blob1.is_empty() {
true
} else {
blob1.iter().zip(blob2.iter()).all(|(&v1, &v2)| v1 == v2)
}
&*blob1 == &blob2
}
#[rhai_fn(name = "!=", pure)]
pub fn not_equals(blob1: &mut Blob, blob2: Blob) -> bool {
!equals(blob1, blob2)
&*blob1 != &blob2
}
#[inline]

View File

@ -75,6 +75,7 @@ fn test_blobs() -> Result<(), Box<EvalAltResult>> {
Ok(())
}
#[cfg(not(feature = "only_i32"))]
#[test]
fn test_blobs_parse() -> Result<(), Box<EvalAltResult>> {
let engine = Engine::new();
@ -125,7 +126,71 @@ fn test_blobs_parse() -> Result<(), Box<EvalAltResult>> {
engine.eval::<INT>(
"let x = blob(16, 0); for n in range(0, 16) { x[n] = n; } write_be(x, 3, 3, -98765432); parse_be_int(x, 3, 3)"
)?,
0xffffff0000000000_u64 as i64
0xffffff0000000000_u64 as INT
);
assert_eq!(
engine.eval::<INT>(
"let x = blob(16, 0); for n in range(0, 16) { x[n] = n; } write_le(x, 3, 3, -98765432); parse_le_int(x, 3, 3)"
)?,
0x1cf588
);
Ok(())
}
#[cfg(feature = "only_i32")]
#[test]
fn test_blobs_parse() -> Result<(), Box<EvalAltResult>> {
let engine = Engine::new();
assert_eq!(
engine.eval::<INT>(
"let x = blob(16, 0); for n in range(0, 16) { x[n] = n; } parse_le_int(x,2,0)"
)?,
0
);
assert_eq!(
engine.eval::<INT>(
"let x = blob(16, 0); for n in range(0, 16) { x[n] = n; } parse_le_int(x,2,9)"
)?,
0x05040302
);
assert_eq!(
engine.eval::<INT>(
"let x = blob(16, 0); for n in range(0, 16) { x[n] = n; } parse_be_int(x,2,10)"
)?,
0x02030405
);
assert_eq!(
engine.eval::<INT>(
"let x = blob(16, 0); for n in range(0, 16) { x[n] = n; } parse_le_int(x,-5,99)"
)?,
0x0e0d0c0b
);
assert_eq!(
engine.eval::<INT>(
"let x = blob(16, 0); for n in range(0, 16) { x[n] = n; } parse_le_int(x,-5,2)"
)?,
0x0c0b
);
assert_eq!(
engine.eval::<INT>(
"let x = blob(16, 0); for n in range(0, 16) { x[n] = n; } parse_le_int(x,-99,99)"
)?,
0x03020100
);
assert_eq!(
engine.eval::<INT>(
"let x = blob(16, 0); for n in range(0, 16) { x[n] = n; } write_be(x, 3, 3, -98765432); parse_be_int(x, 3, 3)"
)?,
0xfa1cf500_u32 as INT
);
assert_eq!(