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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions base/interactiveutil.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ by setting `JULIA_EDITOR`, `VISUAL` or `EDITOR` as an environment variable.
function edit(path::AbstractString, line::Integer=0)
command = editor()
name = basename(first(command))
issrc = length(path)>2 && path[end-2:end] == ".jl"
if issrc
if endswith(path, ".jl")
f = find_source_file(path)
f !== nothing && (path = f)
end
Expand Down
2 changes: 1 addition & 1 deletion base/loading.jl
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ function find_in_path(name::String, wd::Union{Void,String})
isabspath(name) && return name
base = name
if endswith(name,".jl")
base = name[1:end-3]
base = name[1:prevind(name, end-2)]
else
name = string(base,".jl")
end
Expand Down
4 changes: 2 additions & 2 deletions base/profile.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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.

end
end
function ltruncto(str::String, w::Int)
if length(str) <= w
return str
else
return string(str[1:w-4], "...")
return string(str[1:chr2ind(str,w-4)], "...")
end
end

Expand Down