Skip to content

Commit e8cb932

Browse files
spotandjakephated
andauthored
feat(stdlib): add isEmpty to String module (#1861)
Co-authored-by: Blaine Bublitz <blaine.bublitz@gmail.com>
1 parent 7362189 commit e8cb932

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

compiler/test/stdlib/string.test.gr

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@ assert String.byteLength("a") == 1
6363
assert String.byteLength(emoji) == 4
6464
assert String.byteLength(emojis) == 98
6565

66+
// String.isEmpty
67+
assert String.isEmpty(empty) == true
68+
assert String.isEmpty(short) == false
69+
assert String.isEmpty(emoji) == false
70+
assert String.isEmpty(emojis) == false
71+
6672
// indexOf tests
6773
assert String.indexOf(empty, empty) == Some(0)
6874
assert String.indexOf(empty, short) == Some(0)

stdlib/string.gr

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,21 @@ provide let byteLength = (string: String) => {
9898
Conv.wasmI32ToNumber(WasmI32.load(string, 4n))
9999
}
100100

101+
/**
102+
* Determines if the string contains no characters.
103+
*
104+
* @param string: The string to inspect
105+
* @returns `true` if the string is empty and `false` otherwise
106+
*
107+
* @since v0.6.0
108+
*/
109+
@unsafe
110+
provide let isEmpty = (string: String) => {
111+
from WasmI32 use { (==) }
112+
let strPtr = WasmI32.fromGrain(string)
113+
WasmI32.load(strPtr, 4n) == 0n
114+
}
115+
101116
/**
102117
* Finds the first position of a substring in the input string.
103118
*

stdlib/string.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,31 @@ Examples:
137137
String.byteLength("🌾") == 4
138138
```
139139

140+
### String.**isEmpty**
141+
142+
<details disabled>
143+
<summary tabindex="-1">Added in <code>next</code></summary>
144+
No other changes yet.
145+
</details>
146+
147+
```grain
148+
isEmpty : (string: String) => Bool
149+
```
150+
151+
Determines if the string contains no characters.
152+
153+
Parameters:
154+
155+
|param|type|description|
156+
|-----|----|-----------|
157+
|`string`|`String`|The string to inspect|
158+
159+
Returns:
160+
161+
|type|description|
162+
|----|-----------|
163+
|`Bool`|`true` if the string is empty and `false` otherwise|
164+
140165
### String.**indexOf**
141166

142167
<details disabled>

0 commit comments

Comments
 (0)