-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Document invalid UTF-8 indexing and concatenation #26952
Merged
Merged
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
47f307b
Document invalid UTF-8 indexing, concatenation, thisind, prevind, nex…
bkamins 0c080fd
language corrections after a review
bkamins 60395c7
language corrections after review
bkamins 32d8d65
iproved character parsing description
bkamins f6dc4ca
Merge branch 'master' into str_indexing_doc
StefanKarpinski c0f0509
use julia-repl
bkamins a563e33
clean up thisind documentation
bkamins 14b157a
indent bullet points
bkamins File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -365,25 +365,32 @@ end | |
If `i` is in bounds in `s` return the index of the start of the character whose | ||
encoding code unit `i` is part of. In other words, if `i` is the start of a | ||
character, return `i`; if `i` is not the start of a character, rewind until the | ||
start of a character and return that index. If `i` is out of bounds in `s` | ||
return `i`. | ||
start of a character and return that index. If `i` is equal to 0 or `ncodeunits(s)+1` | ||
return `i`. In all other cases throw `BoundsError`. | ||
|
||
# Examples | ||
```jldoctest | ||
julia> thisind("αβγdef", 1) | ||
julia> thisind("α", 0) | ||
0 | ||
|
||
julia> thisind("α", 1) | ||
1 | ||
|
||
julia> thisind("αβγdef", 3) | ||
3 | ||
julia> thisind("α", 2) | ||
1 | ||
|
||
julia> thisind("αβγdef", 4) | ||
julia> thisind("α", 3) | ||
3 | ||
|
||
julia> thisind("αβγdef", 9) | ||
9 | ||
julia> thisind("α", 4) | ||
ERROR: BoundsError: attempt to access "α" | ||
at index [4] | ||
[...] | ||
|
||
julia> thisind("αβγdef", 10) | ||
10 | ||
julia> thisind("α", -1) | ||
ERROR: BoundsError: attempt to access "α" | ||
at index [-1] | ||
[...] | ||
``` | ||
""" | ||
thisind(s::AbstractString, i::Integer) = thisind(s, Int(i)) | ||
|
@@ -401,28 +408,46 @@ end | |
""" | ||
prevind(str::AbstractString, i::Integer, n::Integer=1) -> Int | ||
|
||
* Case `n == 1` | ||
|
||
If `i` is in bounds in `s` return the index of the start of the character whose | ||
encoding starts before index `i`. In other words, if `i` is the start of a | ||
character, return the start of the previous character; if `i` is not the start | ||
of a character, rewind until the start of a character and return that index. | ||
If `i` is out of bounds in `s` return `i - 1`. If `n == 0` return `i`. | ||
If `i` is equal to `1` return `0`. | ||
If `i` is equal to `ncodeunits(str)+1` return `lastindex(str)`. | ||
Otherwise throw `BoundsError`. | ||
|
||
* Case `n > 1` | ||
|
||
Behaves like applying `n` times `prevind` for `n==1`. The only difference | ||
is that if `n` is so large that applying `prevind` would reach `0` then each remaining | ||
iteration decreases the returned value by `1`. | ||
This means that in this case `prevind` can return a negative value. | ||
|
||
* Case `n == 0` | ||
|
||
Return `i` only if `i` is a valid index in `str` or is equal to `ncodeunits(str)+1`. | ||
Otherwise `StringIndexError` or `BoundsError` is thrown. | ||
|
||
# Examples | ||
```jldoctest | ||
julia> prevind("αβγdef", 3) | ||
julia> prevind("α", 3) | ||
1 | ||
|
||
julia> prevind("αβγdef", 1) | ||
julia> prevind("α", 1) | ||
0 | ||
|
||
julia> prevind("αβγdef", 0) | ||
ERROR: BoundsError: attempt to access "αβγdef" | ||
julia> prevind("α", 0) | ||
ERROR: BoundsError: attempt to access "α" | ||
at index [0] | ||
Stacktrace: | ||
[...] | ||
|
||
julia> prevind("αβγdef", 3, 2) | ||
julia> prevind("α", 2, 2) | ||
0 | ||
|
||
julia> prevind("α", 2, 3) | ||
-1 | ||
``` | ||
""" | ||
prevind(s::AbstractString, i::Integer, n::Integer) = prevind(s, Int(i), Int(n)) | ||
|
@@ -443,25 +468,46 @@ end | |
""" | ||
nextind(str::AbstractString, i::Integer, n::Integer=1) -> Int | ||
|
||
* Case `n == 1` | ||
|
||
If `i` is in bounds in `s` return the index of the start of the character whose | ||
encoding starts after index `i`. If `i` is out of bounds in `s` return `i + 1`. | ||
If `n == 0` return `i`. | ||
encoding starts after index `i`. In other words, if `i` is the start of a | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this section need to be indented (and same below) if they should render as bullet points, right? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed, thanks. |
||
character, return the start of the next character; if `i` is not the start | ||
of a character, move forward until the start of a character and return that index. | ||
If `i` is equal to `0` return `1`. | ||
If `i` is in bounds but greater or equal to `lastindex(str)` return `ncodeunits(str)+1`. | ||
Otherwise throw `BoundsError`. | ||
|
||
* Case `n > 1` | ||
|
||
Behaves like applying `n` times `nextind` for `n==1`. The only difference | ||
is that if `n` is so large that applying `nextind` would reach `ncodeunits(str)+1` then each | ||
remaining iteration increases the returned value by `1`. | ||
This means that in this case `nextind` can return a value greater than `ncodeunits(str)+1`. | ||
|
||
* Case `n == 0` | ||
|
||
Return `i` only if `i` is a valid index in `s` or is equal to `0`. | ||
Otherwise `StringIndexError` or `BoundsError` is thrown. | ||
|
||
# Examples | ||
```jldoctest | ||
julia> str = "αβγdef"; | ||
julia> nextind("α", 0) | ||
1 | ||
|
||
julia> nextind(str, 1) | ||
julia> nextind("α", 1) | ||
3 | ||
|
||
julia> nextind(str, 1, 2) | ||
5 | ||
julia> nextind("α", 3) | ||
ERROR: BoundsError: attempt to access "α" | ||
at index [3] | ||
[...] | ||
|
||
julia> lastindex(str) | ||
9 | ||
julia> nextind("α", 0, 2) | ||
3 | ||
|
||
julia> nextind(str, 9) | ||
10 | ||
julia> nextind("α", 1, 2) | ||
4 | ||
``` | ||
""" | ||
nextind(s::AbstractString, i::Integer, n::Integer) = nextind(s, Int(i), Int(n)) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line has no output
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed (a copy-paste glitch)