Skip to content

Commit c3abbc9

Browse files
authored
feat(stdlib): Add charCodeAt function to String module (#1376)
1 parent 0af0669 commit c3abbc9

File tree

3 files changed

+74
-14
lines changed

3 files changed

+74
-14
lines changed

compiler/test/stdlib/string.test.gr

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,12 @@ assert String.lastIndexOf(emoji, emojis) == None
8181
assert String.lastIndexOf("aa", "aaa") == Some(1)
8282
assert String.lastIndexOf("world", "Hello world world") == Some(12)
8383

84+
// charCodeAt tests
85+
assert String.charCodeAt(0, emojis) == 119
86+
assert String.charCodeAt(15, emojis) == 128640
87+
assert String.charCodeAt(16, emojis) == 32
88+
assert String.charCodeAt(17, emojis) == 116
89+
8490
// charAt tests
8591
assert String.charAt(0, emojis) == 'w'
8692
assert String.charAt(15, emojis) == '🚀'

stdlib/string.gr

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -281,19 +281,9 @@ let getCodePoint = (ptr: WasmI32) => {
281281
}
282282
result
283283
}
284-
/**
285-
* Get the character at the position in the input string.
286-
*
287-
* @param position: The position to check
288-
* @param string: The string to search
289-
* @returns The character at the provided position
290-
*
291-
* @example String.charAt(5, "Hello world") == ' '
292-
*
293-
* @since v0.4.0
294-
*/
284+
295285
@unsafe
296-
export let charAt = (position, string: String) => {
286+
let charAtHelp = (position, string: String) => {
297287
if (length(string) <= position || position < 0) {
298288
fail "Invalid offset: " ++ toString(position)
299289
}
@@ -309,10 +299,10 @@ export let charAt = (position, string: String) => {
309299
let mut ptr = string + 8n
310300
let end = ptr + size
311301
let mut counter = 0n
312-
let mut result = WasmI32.toGrain(0n): Char
302+
let mut result = 0n
313303
while (ptr < end) {
314304
if (counter == position) {
315-
result = tagChar(getCodePoint(ptr))
305+
result = getCodePoint(ptr)
316306
break
317307
}
318308
let byte = WasmI32.load8U(ptr, 0n)
@@ -334,6 +324,38 @@ export let charAt = (position, string: String) => {
334324
result
335325
}
336326

327+
/**
328+
* Get the Unicode code point at the position in the input string.
329+
*
330+
* @param position: The position to check
331+
* @param string: The string to search
332+
* @returns The character code at the provided position
333+
*
334+
* @example String.charCodeAt(5, "Hello world") == 32
335+
*
336+
* @since v0.5.3
337+
*/
338+
@unsafe
339+
export let charCodeAt = (position, string: String) => {
340+
tagSimpleNumber(charAtHelp(position, string))
341+
}
342+
343+
/**
344+
* Get the character at the position in the input string.
345+
*
346+
* @param position: The position to check
347+
* @param string: The string to search
348+
* @returns The character at the provided position
349+
*
350+
* @example String.charAt(5, "Hello world") == ' '
351+
*
352+
* @since v0.4.0
353+
*/
354+
@unsafe
355+
export let charAt = (position, string: String) => {
356+
tagChar(charAtHelp(position, string))
357+
}
358+
337359
@unsafe
338360
let explodeHelp = (s: String, chars) => {
339361
let (>>>) = WasmI32.shrU

stdlib/string.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,38 @@ Examples:
201201
String.lastIndexOf("world", "Hello world world") == Some(12)
202202
```
203203

204+
### String.**charCodeAt**
205+
206+
<details disabled>
207+
<summary tabindex="-1">Added in <code>next</code></summary>
208+
No other changes yet.
209+
</details>
210+
211+
```grain
212+
charCodeAt : (Number, String) -> Number
213+
```
214+
215+
Get the Unicode code point at the position in the input string.
216+
217+
Parameters:
218+
219+
|param|type|description|
220+
|-----|----|-----------|
221+
|`position`|`Number`|The position to check|
222+
|`string`|`String`|The string to search|
223+
224+
Returns:
225+
226+
|type|description|
227+
|----|-----------|
228+
|`Number`|The character code at the provided position|
229+
230+
Examples:
231+
232+
```grain
233+
String.charCodeAt(5, "Hello world") == 32
234+
```
235+
204236
### String.**charAt**
205237

206238
<details disabled>

0 commit comments

Comments
 (0)