From e8cb93298cf8776876efc7972f09f0eef2d57c58 Mon Sep 17 00:00:00 2001 From: Spotandjake <40705786+spotandjake@users.noreply.github.com> Date: Sun, 25 Jun 2023 18:13:54 -0400 Subject: [PATCH] feat(stdlib): add `isEmpty` to `String` module (#1861) Co-authored-by: Blaine Bublitz --- compiler/test/stdlib/string.test.gr | 6 ++++++ stdlib/string.gr | 15 +++++++++++++++ stdlib/string.md | 25 +++++++++++++++++++++++++ 3 files changed, 46 insertions(+) diff --git a/compiler/test/stdlib/string.test.gr b/compiler/test/stdlib/string.test.gr index 7d99fa93fa..f9366c8141 100644 --- a/compiler/test/stdlib/string.test.gr +++ b/compiler/test/stdlib/string.test.gr @@ -63,6 +63,12 @@ assert String.byteLength("a") == 1 assert String.byteLength(emoji) == 4 assert String.byteLength(emojis) == 98 +// String.isEmpty +assert String.isEmpty(empty) == true +assert String.isEmpty(short) == false +assert String.isEmpty(emoji) == false +assert String.isEmpty(emojis) == false + // indexOf tests assert String.indexOf(empty, empty) == Some(0) assert String.indexOf(empty, short) == Some(0) diff --git a/stdlib/string.gr b/stdlib/string.gr index d22b46f619..385ef3c95b 100644 --- a/stdlib/string.gr +++ b/stdlib/string.gr @@ -98,6 +98,21 @@ provide let byteLength = (string: String) => { Conv.wasmI32ToNumber(WasmI32.load(string, 4n)) } +/** + * Determines if the string contains no characters. + * + * @param string: The string to inspect + * @returns `true` if the string is empty and `false` otherwise + * + * @since v0.6.0 + */ +@unsafe +provide let isEmpty = (string: String) => { + from WasmI32 use { (==) } + let strPtr = WasmI32.fromGrain(string) + WasmI32.load(strPtr, 4n) == 0n +} + /** * Finds the first position of a substring in the input string. * diff --git a/stdlib/string.md b/stdlib/string.md index 2510d78aae..23e9898e6e 100644 --- a/stdlib/string.md +++ b/stdlib/string.md @@ -137,6 +137,31 @@ Examples: String.byteLength("🌾") == 4 ``` +### String.**isEmpty** + +
+Added in next +No other changes yet. +
+ +```grain +isEmpty : (string: String) => Bool +``` + +Determines if the string contains no characters. + +Parameters: + +|param|type|description| +|-----|----|-----------| +|`string`|`String`|The string to inspect| + +Returns: + +|type|description| +|----|-----------| +|`Bool`|`true` if the string is empty and `false` otherwise| + ### String.**indexOf**