From 5eaf85254f9e2a56459ee523ed3f1cbde0c640a6 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Sun, 12 Dec 2021 17:26:15 +0800 Subject: [PATCH] Fix builds. --- src/packages/array_basic.rs | 17 +------- src/packages/blob_basic.rs | 83 ++++++++++++++----------------------- tests/blobs.rs | 67 +++++++++++++++++++++++++++++- 3 files changed, 99 insertions(+), 68 deletions(-) diff --git a/src/packages/array_basic.rs b/src/packages/array_basic.rs index 1b1f55dd..befc3d7d 100644 --- a/src/packages/array_basic.rs +++ b/src/packages/array_basic.rs @@ -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 { diff --git a/src/packages/blob_basic.rs b/src/packages/blob_basic.rs index ae7a35cc..8b0380bc 100644 --- a/src/packages/blob_basic.rs +++ b/src/packages/blob_basic.rs @@ -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] diff --git a/tests/blobs.rs b/tests/blobs.rs index b549b8d8..7036f116 100644 --- a/tests/blobs.rs +++ b/tests/blobs.rs @@ -75,6 +75,7 @@ fn test_blobs() -> Result<(), Box> { Ok(()) } +#[cfg(not(feature = "only_i32"))] #[test] fn test_blobs_parse() -> Result<(), Box> { let engine = Engine::new(); @@ -125,7 +126,71 @@ fn test_blobs_parse() -> Result<(), Box> { engine.eval::( "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::( + "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> { + let engine = Engine::new(); + + assert_eq!( + engine.eval::( + "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::( + "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::( + "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::( + "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::( + "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::( + "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::( + "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!(