Skip to content

Commit e9f2bdd

Browse files
bicycle1885simeonschaub
authored andcommitted
Add note on the complexity of length for strings (JuliaLang#35999)
1 parent 2fe63ce commit e9f2bdd

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

base/strings/basic.jl

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -346,15 +346,22 @@ isless(a::Symbol, b::Symbol) = cmp(a, b) < 0
346346
length(s::AbstractString) -> Int
347347
length(s::AbstractString, i::Integer, j::Integer) -> Int
348348
349-
The number of characters in string `s` from indices `i` through `j`. This is
350-
computed as the number of code unit indices from `i` to `j` which are valid
351-
character indices. With only a single string argument, this computes the
352-
number of characters in the entire string. With `i` and `j` arguments it
349+
Return the number of characters in string `s` from indices `i` through `j`.
350+
351+
This is computed as the number of code unit indices from `i` to `j` which are
352+
valid character indices. With only a single string argument, this computes
353+
the number of characters in the entire string. With `i` and `j` arguments it
353354
computes the number of indices between `i` and `j` inclusive that are valid
354355
indices in the string `s`. In addition to in-bounds values, `i` may take the
355356
out-of-bounds value `ncodeunits(s) + 1` and `j` may take the out-of-bounds
356357
value `0`.
357358
359+
!!! note
360+
The time complexity of this operation is linear in general. That is, it
361+
will take the time proportional to the number of bytes or characters in
362+
the string because it counts the value on the fly. This is in contrast to
363+
the method for arrays, which is a constant-time operation.
364+
358365
See also: [`isvalid`](@ref), [`ncodeunits`](@ref), [`lastindex`](@ref),
359366
[`thisind`](@ref), [`nextind`](@ref), [`prevind`](@ref)
360367

0 commit comments

Comments
 (0)