Skip to content

Commit e52c807

Browse files
fea(stdlib): Add comparison operators to char stdlib (#1744)
* feat: Add comparison operators to char stdlib * chore: Apply suggestions from code review * Apply suggestions from code review Co-authored-by: Oscar Spencer <oscar.spen@gmail.com> * chore: regenerate grain doc * chore: Apply suggestions from code review * chore: Rebase on main --------- Co-authored-by: Oscar Spencer <oscar.spen@gmail.com>
1 parent 008a735 commit e52c807

File tree

3 files changed

+204
-0
lines changed

3 files changed

+204
-0
lines changed

compiler/test/stdlib/char.test.gr

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,35 @@ while (!done) {
5454
Char.code(chars[charPosition])
5555
charPosition += 1
5656
}
57+
58+
// <
59+
from Char use { (<) }
60+
assert 'a' < 'z'
61+
assert 'a' < 'b'
62+
assert 'A' < 'Z'
63+
assert !('z' < 'a')
64+
65+
// <=
66+
from Char use { (<=) }
67+
assert 'a' <= 'z'
68+
assert 'a' <= 'b'
69+
assert 'A' <= 'Z'
70+
assert !('z' <= 'a')
71+
assert 'z' <= 'z'
72+
assert 'Z' <= 'Z'
73+
74+
// >
75+
from Char use { (>) }
76+
assert 'z' > 'a'
77+
assert 'b' > 'a'
78+
assert 'Z' > 'A'
79+
assert !('a' > 'b')
80+
81+
// >=
82+
from Char use { (>=) }
83+
assert 'z' >= 'a'
84+
assert 'b' >= 'a'
85+
assert 'Z' >= 'A'
86+
assert !('a' >= 'b')
87+
assert 'a' >= 'a'
88+
assert 'B' >= 'B'

stdlib/char.gr

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,3 +191,71 @@ provide let toString = (char: Char) => {
191191

192192
result
193193
}
194+
195+
/**
196+
* Checks if the first character is less than the second character by Unicode scalar value.
197+
*
198+
* @param x: The first character
199+
* @param y: The second character
200+
* @returns `true` if the first character is less than the second character or `false` otherwise
201+
*
202+
* @since v0.6.0
203+
*/
204+
@unsafe
205+
provide let (<) = (x: Char, y: Char) => {
206+
from WasmI32 use { (<) }
207+
let x = WasmI32.fromGrain(x)
208+
let y = WasmI32.fromGrain(y)
209+
x < y
210+
}
211+
212+
/**
213+
* Checks if the first character is less than or equal to the second character by Unicode scalar value.
214+
*
215+
* @param x: The first character
216+
* @param y: The second character
217+
* @returns `true` if the first character is less than or equal to the second character or `false` otherwise
218+
*
219+
* @since v0.6.0
220+
*/
221+
@unsafe
222+
provide let (<=) = (x: Char, y: Char) => {
223+
from WasmI32 use { (<=) }
224+
let x = WasmI32.fromGrain(x)
225+
let y = WasmI32.fromGrain(y)
226+
x <= y
227+
}
228+
229+
/**
230+
* Checks if the first character is greater than the second character by Unicode scalar value.
231+
*
232+
* @param x: The first character
233+
* @param y: The second character
234+
* @returns `true` if the first character is greater than the second character or `false` otherwise
235+
*
236+
* @since v0.6.0
237+
*/
238+
@unsafe
239+
provide let (>) = (x: Char, y: Char) => {
240+
from WasmI32 use { (>) }
241+
let x = WasmI32.fromGrain(x)
242+
let y = WasmI32.fromGrain(y)
243+
x > y
244+
}
245+
246+
/**
247+
* Checks if the first character is greater than or equal to the second character by Unicode scalar value.
248+
*
249+
* @param x: The first character
250+
* @param y: The second character
251+
* @returns `true` if the first character is greater than or equal to the second character or `false` otherwise
252+
*
253+
* @since v0.6.0
254+
*/
255+
@unsafe
256+
provide let (>=) = (x: Char, y: Char) => {
257+
from WasmI32 use { (>=) }
258+
let x = WasmI32.fromGrain(x)
259+
let y = WasmI32.fromGrain(y)
260+
x >= y
261+
}

stdlib/char.md

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,3 +213,107 @@ Returns:
213213
|----|-----------|
214214
|`String`|A string containing the given character|
215215

216+
### Char.**(<)**
217+
218+
<details disabled>
219+
<summary tabindex="-1">Added in <code>next</code></summary>
220+
No other changes yet.
221+
</details>
222+
223+
```grain
224+
(<) : (x: Char, y: Char) -> Bool
225+
```
226+
227+
Checks if the first character is less than the second character by Unicode scalar value.
228+
229+
Parameters:
230+
231+
|param|type|description|
232+
|-----|----|-----------|
233+
|`x`|`Char`|The first character|
234+
|`y`|`Char`|The second character|
235+
236+
Returns:
237+
238+
|type|description|
239+
|----|-----------|
240+
|`Bool`|`true` if the first character is less than the second character or `false` otherwise|
241+
242+
### Char.**(<=)**
243+
244+
<details disabled>
245+
<summary tabindex="-1">Added in <code>next</code></summary>
246+
No other changes yet.
247+
</details>
248+
249+
```grain
250+
(<=) : (x: Char, y: Char) -> Bool
251+
```
252+
253+
Checks if the first character is less than or equal to the second character by Unicode scalar value.
254+
255+
Parameters:
256+
257+
|param|type|description|
258+
|-----|----|-----------|
259+
|`x`|`Char`|The first character|
260+
|`y`|`Char`|The second character|
261+
262+
Returns:
263+
264+
|type|description|
265+
|----|-----------|
266+
|`Bool`|`true` if the first character is less than or equal to the second character or `false` otherwise|
267+
268+
### Char.**(>)**
269+
270+
<details disabled>
271+
<summary tabindex="-1">Added in <code>next</code></summary>
272+
No other changes yet.
273+
</details>
274+
275+
```grain
276+
(>) : (x: Char, y: Char) -> Bool
277+
```
278+
279+
Checks if the first character is greater than the second character by Unicode scalar value.
280+
281+
Parameters:
282+
283+
|param|type|description|
284+
|-----|----|-----------|
285+
|`x`|`Char`|The first character|
286+
|`y`|`Char`|The second character|
287+
288+
Returns:
289+
290+
|type|description|
291+
|----|-----------|
292+
|`Bool`|`true` if the first character is greater than the second character or `false` otherwise|
293+
294+
### Char.**(>=)**
295+
296+
<details disabled>
297+
<summary tabindex="-1">Added in <code>next</code></summary>
298+
No other changes yet.
299+
</details>
300+
301+
```grain
302+
(>=) : (x: Char, y: Char) -> Bool
303+
```
304+
305+
Checks if the first character is greater than or equal to the second character by Unicode scalar value.
306+
307+
Parameters:
308+
309+
|param|type|description|
310+
|-----|----|-----------|
311+
|`x`|`Char`|The first character|
312+
|`y`|`Char`|The second character|
313+
314+
Returns:
315+
316+
|type|description|
317+
|----|-----------|
318+
|`Bool`|`true` if the first character is greater than or equal to the second character or `false` otherwise|
319+

0 commit comments

Comments
 (0)