Skip to content

String.unsafeGet #223

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Core__String.res
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ let compare = (a: string, b: string) =>

@get external length: string => int = "length"
@get_index external get: (string, int) => option<string> = ""
@get_index external getUnsafe: (string, int) => string = ""
@send external charAt: (string, int) => string = "charAt"

@send external charCodeAt: (string, int) => float = "charCodeAt"
Expand Down
16 changes: 16 additions & 0 deletions src/Core__String.resi
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,22 @@ String.get(`JS`, 4) == None
@get_index
external get: (string, int) => option<string> = ""

/**
`getUnsafe(str, index)` returns an `string` at the given `index` number.

This is _unsafe_, meaning it will return `undefined` value if `index` does not exist in `string`.

Use `String.getUnsafe` only when you are sure the `index` exists.
## Examples

```rescript
String.getUnsafe("ReScript", 0) == "R"
String.getUnsafe("Hello", 4) == "o"
```
*/
@get_index
external getUnsafe: (string, int) => string = ""

/**
`charAt(str, index)` gets the character at `index` within string `str`. If
`index` is negative or greater than the length of `str`, it returns the empty
Expand Down
Loading