Fix Array::chop.

This commit is contained in:
Stephen Chung 2022-01-14 10:04:24 +08:00
parent 96764c0d2d
commit e2e0b8d083
3 changed files with 9 additions and 8 deletions

View File

@ -8,6 +8,7 @@ Bug fixes
--------- ---------
* Padding arrays with another array via `pad` no longer loops indefinitely. * Padding arrays with another array via `pad` no longer loops indefinitely.
* `chop` for arrays and BLOB's now works properly.
* `set_bit` for bit-flags with negative index now works correctly. * `set_bit` for bit-flags with negative index now works correctly.
* Misnamed `params` field `name` in the JSON output of `Engine::gen_fn_metadata_to_json` is fixed (was incorrectly named `type`). * Misnamed `params` field `name` in the JSON output of `Engine::gen_fn_metadata_to_json` is fixed (was incorrectly named `type`).

View File

@ -171,11 +171,11 @@ pub mod array_functions {
} }
} }
pub fn chop(array: &mut Array, len: INT) { pub fn chop(array: &mut Array, len: INT) {
if !array.is_empty() && len as usize >= array.len() { if !array.is_empty() {
if len >= 0 { if len <= 0 {
array.drain(0..array.len() - len as usize);
} else {
array.clear(); array.clear();
} else if (len as usize) < array.len() {
array.drain(0..array.len() - len as usize);
} }
} }
} }

View File

@ -162,11 +162,11 @@ pub mod blob_functions {
} }
} }
pub fn chop(blob: &mut Blob, len: INT) { pub fn chop(blob: &mut Blob, len: INT) {
if !blob.is_empty() && len as usize >= blob.len() { if !blob.is_empty() {
if len >= 0 { if len <= 0 {
blob.drain(0..blob.len() - len as usize);
} else {
blob.clear(); blob.clear();
} else if (len as usize) < blob.len() {
blob.drain(0..blob.len() - len as usize);
} }
} }
} }