Skip to content

Commit ad4db2e

Browse files
authored
Explicit String.at/2 access cost and alternatives (#13838)
1 parent e0967c2 commit ad4db2e

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

lib/elixir/lib/string.ex

+12-4
Original file line numberDiff line numberDiff line change
@@ -646,10 +646,12 @@ defmodule String do
646646
The offset is capped to the length of the string. Returns a tuple with
647647
two elements.
648648
649-
Note: keep in mind this function splits on graphemes and for such it
650-
has to linearly traverse the string. If you want to split a string or
651-
a binary based on the number of bytes, use `Kernel.binary_part/3`
652-
instead.
649+
> #### Linear Access {: .warning}
650+
>
651+
> This function splits on graphemes and for such it has to linearly traverse
652+
> the string.
653+
> If you want to split a string or a binary based on the number of bytes,
654+
> use `Kernel.binary_part/3` instead.
653655
654656
## Examples
655657
@@ -2231,6 +2233,12 @@ defmodule String do
22312233
Returns the grapheme at the `position` of the given UTF-8 `string`.
22322234
If `position` is greater than `string` length, then it returns `nil`.
22332235
2236+
> #### Linear Access {: .warning}
2237+
>
2238+
> This function has to linearly traverse the string.
2239+
> If you want to access a string or a binary in constant time based on the
2240+
> number of bytes, use `Kernel.binary_slice/3` or `:binary.at/2` instead.
2241+
22342242
## Examples
22352243
22362244
iex> String.at("elixir", 0)

0 commit comments

Comments
 (0)