Skip to content

Commit

Permalink
String.unsafeGet (#223)
Browse files Browse the repository at this point in the history
  • Loading branch information
jderochervlk authored May 18, 2024
1 parent 1f5139a commit d1d0f8a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
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

0 comments on commit d1d0f8a

Please sign in to comment.