Skip to content
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

Clean up string indexing #23799

Merged
merged 1 commit into from
Sep 29, 2017
Merged

Clean up string indexing #23799

merged 1 commit into from
Sep 29, 2017

Conversation

bkamins
Copy link
Member

@bkamins bkamins commented Sep 20, 2017

Results of a review of Julia code base for arithmetic operations on string indices.

@bkamins bkamins changed the title clean up string indexing Clean up string indexing Sep 20, 2017
@@ -640,14 +640,14 @@ function rtruncto(str::String, w::Int)
if length(str) <= w
return str
else
return string("...", str[end-w+4:end])
return string("...", str[chr2ind(str, length(str)-w+4):end])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It likely won't matter in practice but it might be better to use endof(str) to ensure that it's a valid index.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately no as endof(str) returns byte index.
What we want to do here is go back w-4 characters from endof(str). We could run w+4 times prevind from endof(str), but this is cumbersome so that is why I have used chr2ind and performed the calculations on character indices not byte indices and only converted them to byte index at the end with chr2ind (which is inefficient, but I guess in this application it should not be a problem).
I hope to make such calculations simpler and more efficient with #23765 in the future.

@ararslan ararslan added the strings "Strings!" label Sep 20, 2017
@bkamins
Copy link
Member Author

bkamins commented Sep 29, 2017

@ararslan Is it good to merge?

@ararslan
Copy link
Member

Yep, CI failures look unrelated.

@ararslan ararslan merged commit 5a5dbed into JuliaLang:master Sep 29, 2017
@bkamins bkamins deleted the str_idx_fix branch September 29, 2017 18:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
strings "Strings!"
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants