Skip to content

Commit

Permalink
feat(stdlib): add isEmpty to String module (#1861)
Browse files Browse the repository at this point in the history
Co-authored-by: Blaine Bublitz <blaine.bublitz@gmail.com>
  • Loading branch information
spotandjake and phated authored Jun 25, 2023
1 parent 7362189 commit e8cb932
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
6 changes: 6 additions & 0 deletions compiler/test/stdlib/string.test.gr
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
15 changes: 15 additions & 0 deletions stdlib/string.gr
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
25 changes: 25 additions & 0 deletions stdlib/string.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,31 @@ Examples:
String.byteLength("🌾") == 4
```

### String.**isEmpty**

<details disabled>
<summary tabindex="-1">Added in <code>next</code></summary>
No other changes yet.
</details>

```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**

<details disabled>
Expand Down

0 comments on commit e8cb932

Please sign in to comment.