Skip to content

Commit

Permalink
feat(stdlib): Add charCodeAt function to String module (#1376)
Browse files Browse the repository at this point in the history
  • Loading branch information
spotandjake committed Aug 4, 2022
1 parent 0af0669 commit c3abbc9
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 14 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 @@ -81,6 +81,12 @@ assert String.lastIndexOf(emoji, emojis) == None
assert String.lastIndexOf("aa", "aaa") == Some(1)
assert String.lastIndexOf("world", "Hello world world") == Some(12)

// charCodeAt tests
assert String.charCodeAt(0, emojis) == 119
assert String.charCodeAt(15, emojis) == 128640
assert String.charCodeAt(16, emojis) == 32
assert String.charCodeAt(17, emojis) == 116

// charAt tests
assert String.charAt(0, emojis) == 'w'
assert String.charAt(15, emojis) == '🚀'
Expand Down
50 changes: 36 additions & 14 deletions stdlib/string.gr
Original file line number Diff line number Diff line change
Expand Up @@ -281,19 +281,9 @@ let getCodePoint = (ptr: WasmI32) => {
}
result
}
/**
* Get the character at the position in the input string.
*
* @param position: The position to check
* @param string: The string to search
* @returns The character at the provided position
*
* @example String.charAt(5, "Hello world") == ' '
*
* @since v0.4.0
*/

@unsafe
export let charAt = (position, string: String) => {
let charAtHelp = (position, string: String) => {
if (length(string) <= position || position < 0) {
fail "Invalid offset: " ++ toString(position)
}
Expand All @@ -309,10 +299,10 @@ export let charAt = (position, string: String) => {
let mut ptr = string + 8n
let end = ptr + size
let mut counter = 0n
let mut result = WasmI32.toGrain(0n): Char
let mut result = 0n
while (ptr < end) {
if (counter == position) {
result = tagChar(getCodePoint(ptr))
result = getCodePoint(ptr)
break
}
let byte = WasmI32.load8U(ptr, 0n)
Expand All @@ -334,6 +324,38 @@ export let charAt = (position, string: String) => {
result
}

/**
* Get the Unicode code point at the position in the input string.
*
* @param position: The position to check
* @param string: The string to search
* @returns The character code at the provided position
*
* @example String.charCodeAt(5, "Hello world") == 32
*
* @since v0.5.3
*/
@unsafe
export let charCodeAt = (position, string: String) => {
tagSimpleNumber(charAtHelp(position, string))
}

/**
* Get the character at the position in the input string.
*
* @param position: The position to check
* @param string: The string to search
* @returns The character at the provided position
*
* @example String.charAt(5, "Hello world") == ' '
*
* @since v0.4.0
*/
@unsafe
export let charAt = (position, string: String) => {
tagChar(charAtHelp(position, string))
}

@unsafe
let explodeHelp = (s: String, chars) => {
let (>>>) = WasmI32.shrU
Expand Down
32 changes: 32 additions & 0 deletions stdlib/string.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,38 @@ Examples:
String.lastIndexOf("world", "Hello world world") == Some(12)
```

### String.**charCodeAt**

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

```grain
charCodeAt : (Number, String) -> Number
```

Get the Unicode code point at the position in the input string.

Parameters:

|param|type|description|
|-----|----|-----------|
|`position`|`Number`|The position to check|
|`string`|`String`|The string to search|

Returns:

|type|description|
|----|-----------|
|`Number`|The character code at the provided position|

Examples:

```grain
String.charCodeAt(5, "Hello world") == 32
```

### String.**charAt**

<details disabled>
Expand Down

0 comments on commit c3abbc9

Please sign in to comment.