From 9c3443538a774d821a85954ab6a1df2999070a5a Mon Sep 17 00:00:00 2001 From: ltabis Date: Tue, 23 Aug 2022 16:36:43 +0200 Subject: [PATCH 1/3] feat(array): add an `empty` getter for arrays. --- examples/definitions/.rhai/definitions/__static__.d.rhai | 3 +++ src/packages/array_basic.rs | 5 +++++ 2 files changed, 8 insertions(+) diff --git a/examples/definitions/.rhai/definitions/__static__.d.rhai b/examples/definitions/.rhai/definitions/__static__.d.rhai index 108ab58e..a7cf38ef 100644 --- a/examples/definitions/.rhai/definitions/__static__.d.rhai +++ b/examples/definitions/.rhai/definitions/__static__.d.rhai @@ -1973,6 +1973,9 @@ fn get is_zero(x: u8) -> bool; /// Number of elements in the array. fn get len(array: Array) -> int; +/// Return true if the array is empty. +fn get empty(array: Array) -> bool; + /// Return the length of the BLOB. /// /// # Example diff --git a/src/packages/array_basic.rs b/src/packages/array_basic.rs index d0a0b671..dd4f51fc 100644 --- a/src/packages/array_basic.rs +++ b/src/packages/array_basic.rs @@ -30,6 +30,11 @@ pub mod array_functions { pub fn len(array: &mut Array) -> INT { array.len() as INT } + /// Return true if the array is empty. + #[rhai_fn(name = "empty", get = "empty", pure)] + pub fn empty(array: &mut Array) -> bool { + array.len() == 0 + } /// Get a copy of the element at the `index` position in the array. /// /// * If `index` < 0, position counts from the end of the array (`-1` is the last element). From 4b3608cedc1ed3e9dc0216b946e884ea96ef45c8 Mon Sep 17 00:00:00 2001 From: ltabis Date: Wed, 24 Aug 2022 09:54:41 +0200 Subject: [PATCH 2/3] feat(data-structures): add `is_empty` getter to map, string and blob. --- examples/definitions/.rhai/all_in_one.d.rhai | 12 ++++++++++++ .../definitions/.rhai/definitions/__static__.d.rhai | 12 ++++++++++++ src/packages/array_basic.rs | 4 ++-- src/packages/blob_basic.rs | 5 +++++ src/packages/map_basic.rs | 6 +++++- src/packages/string_more.rs | 5 +++++ 6 files changed, 41 insertions(+), 3 deletions(-) diff --git a/examples/definitions/.rhai/all_in_one.d.rhai b/examples/definitions/.rhai/all_in_one.d.rhai index 05c0f164..962551a7 100644 --- a/examples/definitions/.rhai/all_in_one.d.rhai +++ b/examples/definitions/.rhai/all_in_one.d.rhai @@ -3047,6 +3047,9 @@ fn keys(map: Map) -> Array; /// Number of elements in the array. fn len(array: Array) -> int; +/// Return true if the array is empty. +fn is_empty(array: Array) -> bool; + /// Return the length of the BLOB. /// /// # Example @@ -3060,9 +3063,15 @@ fn len(array: Array) -> int; /// ``` fn len(blob: Blob) -> int; +/// Return true if the blob is empty. +fn is_empty(blob: Blob) -> bool; + /// Return the number of properties in the object map. fn len(map: Map) -> int; +/// Return true if the map is empty. +fn is_empty(map: Map) -> bool; + /// Return the length of the string, in number of characters. /// /// # Example @@ -3074,6 +3083,9 @@ fn len(map: Map) -> int; /// ``` fn len(string: String) -> int; +/// Return true if the string is empty. +fn is_empty(string: String) -> bool; + /// Return the natural log of the decimal number. fn ln(x: Decimal) -> Decimal; diff --git a/examples/definitions/.rhai/definitions/__static__.d.rhai b/examples/definitions/.rhai/definitions/__static__.d.rhai index a7cf38ef..35ceba47 100644 --- a/examples/definitions/.rhai/definitions/__static__.d.rhai +++ b/examples/definitions/.rhai/definitions/__static__.d.rhai @@ -2532,6 +2532,9 @@ fn keys(map: Map) -> Array; /// Number of elements in the array. fn len(array: Array) -> int; +/// Return true if the array is empty. +fn is_empty(array: Array) -> bool; + /// Return the length of the BLOB. /// /// # Example @@ -2545,9 +2548,15 @@ fn len(array: Array) -> int; /// ``` fn len(blob: Blob) -> int; +/// Return true if the blob is empty. +fn is_empty(blob: Blob) -> bool; + /// Return the number of properties in the object map. fn len(map: Map) -> int; +/// Return true if the map is empty. +fn is_empty(map: Map) -> bool; + /// Return the length of the string, in number of characters. /// /// # Example @@ -2559,6 +2568,9 @@ fn len(map: Map) -> int; /// ``` fn len(string: String) -> int; +/// Return true if the string is empty. +fn is_empty(string: String) -> bool; + /// Return the natural log of the decimal number. fn ln(x: Decimal) -> Decimal; diff --git a/src/packages/array_basic.rs b/src/packages/array_basic.rs index dd4f51fc..6018a982 100644 --- a/src/packages/array_basic.rs +++ b/src/packages/array_basic.rs @@ -31,8 +31,8 @@ pub mod array_functions { array.len() as INT } /// Return true if the array is empty. - #[rhai_fn(name = "empty", get = "empty", pure)] - pub fn empty(array: &mut Array) -> bool { + #[rhai_fn(name = "is_empty", get = "is_empty", pure)] + pub fn is_empty(array: &mut Array) -> bool { array.len() == 0 } /// Get a copy of the element at the `index` position in the array. diff --git a/src/packages/blob_basic.rs b/src/packages/blob_basic.rs index c3f43aff..d37f0ef0 100644 --- a/src/packages/blob_basic.rs +++ b/src/packages/blob_basic.rs @@ -140,6 +140,11 @@ pub mod blob_functions { pub fn len(blob: &mut Blob) -> INT { blob.len() as INT } + /// Return true if the blob is empty. + #[rhai_fn(name = "is_empty", get = "is_empty", pure)] + pub fn is_empty(blob: &mut Blob) -> bool { + blob.len() == 0 + } /// Get the byte value at the `index` position in the BLOB. /// /// * If `index` < 0, position counts from the end of the BLOB (`-1` is the last element). diff --git a/src/packages/map_basic.rs b/src/packages/map_basic.rs index bb990cb4..4d75ba15 100644 --- a/src/packages/map_basic.rs +++ b/src/packages/map_basic.rs @@ -25,7 +25,11 @@ mod map_functions { pub fn len(map: &mut Map) -> INT { map.len() as INT } - + /// Return true if the map is empty. + #[rhai_fn(name = "is_empty", get = "is_empty", pure)] + pub fn is_empty(map: &mut Map) -> bool { + map.len() == 0 + } /// Get the value of the `property` in the object map and return a copy. /// /// If `property` does not exist in the object map, `()` is returned. diff --git a/src/packages/string_more.rs b/src/packages/string_more.rs index 0d476a87..8e018c22 100644 --- a/src/packages/string_more.rs +++ b/src/packages/string_more.rs @@ -163,6 +163,11 @@ mod string_functions { string.chars().count() as INT } } + /// Return true if the string is empty. + #[rhai_fn(name = "is_empty", get = "is_empty")] + pub fn is_empty(string: &str) -> bool { + string.len() == 0 + } /// Return the length of the string, in number of bytes used to store it in UTF-8 encoding. /// /// # Example From 1269d4b34ddcd68efacf7b1f5b12023013252b8d Mon Sep 17 00:00:00 2001 From: ltabis Date: Wed, 24 Aug 2022 13:17:43 +0200 Subject: [PATCH 3/3] feat(ranges): add `is_empty` function to inclusive/exclusive ranges. --- src/packages/iter_basic.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/packages/iter_basic.rs b/src/packages/iter_basic.rs index 3ec29366..02089952 100644 --- a/src/packages/iter_basic.rs +++ b/src/packages/iter_basic.rs @@ -666,6 +666,11 @@ mod range_functions { let _ = range; true } + /// Returns true if the range contains no items. + #[rhai_fn(get = "is_empty", name = "is_empty", pure)] + pub fn is_empty_exclusive(range: &mut ExclusiveRange) -> bool { + range.is_empty() + } /// Return the start of the inclusive range. #[rhai_fn(get = "start", name = "start", pure)] pub fn start_inclusive(range: &mut InclusiveRange) -> INT { @@ -688,4 +693,9 @@ mod range_functions { let _ = range; false } + /// Returns true if the range contains no items. + #[rhai_fn(get = "is_empty", name = "is_empty", pure)] + pub fn is_empty_inclusive(range: &mut InclusiveRange) -> bool { + range.is_empty() + } }