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

Allow nchar to be greater than string length in first/last #24556

Closed
wants to merge 4 commits into from

Conversation

bkamins
Copy link
Member

@bkamins bkamins commented Nov 9, 2017

PR following the discussion in #23960 (comment).
For string str it allows nchar to be greater than length(str). In such a case the whole contents of the original string is returned.
This returned whole string does not have to be of the same type as str as in general getindex my return a string of different type than the original string.

CC @stevengj

@bkamins
Copy link
Member Author

bkamins commented Nov 21, 2017

@stevengj Is this what you meant in your #23960 comment? If yes can it be merged?

end
str[1:nextind(str, 1, nchar-1)]
idx = min(endof(str), nextind(str, s, nchar-1))
str[s:idx]
Copy link
Member

Choose a reason for hiding this comment

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

I guess you can use @inbounds here now? That would (or could, if @boundscheck is implemented where needed) offset the cost of callind endof.

Copy link
Member Author

Choose a reason for hiding this comment

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

I am not sure that @inbounds will help (but please correct me if I am wrong or I have misunderstood something what you have meant), because neither
getindex(s::String, r::UnitRange{Int}) nor getindex(s::AbstractString, r::UnitRange{Int})
call endof (they work differently, but perform the checks without calling endof and what they do does not seem to me to be influenced by @inbounds (but maybe I am wrong).

The optimization would be to write a specialized method for first for String only which would call unsafe_string instead of getindex. It would be a bit faster, but I do not know if we want to have such a messy code (the same could be done for last).

Any thoughts on this?

Copy link
Member

Choose a reason for hiding this comment

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

OK. I guess leave it that way for now if the performance impact isn't too large.

@stevengj
Copy link
Member

this seems reasonable to me. if someone wants the old semantics they can always check the length themselves

@nalimilan
Copy link
Member

Note that there will be a small inconsistency between first(s) and first(s, 1) for the empty string. Not sure whether it's a problem or not.

@bkamins
Copy link
Member Author

bkamins commented Nov 21, 2017

@nalimilan
If s="" then first(s) throws ArgumentError and first(s, 1) throws BoundsError. Where do you see the inconsistency?

The issue is rather with first(s, 0) which will return "", as the function is implemented to always return an empty string if nchar==0. Do you feel that we should handle the case of an empty string and nchar==0 in a special way or leave it as is?

@nalimilan
Copy link
Member

If s="" then first(s) throws ArgumentError and first(s, 1) throws BoundsError. Where do you see the inconsistency?

Mmm, why doesn't first(s, 1) return an empty string? You asked for "at most 1 char".

@bkamins
Copy link
Member Author

bkamins commented Nov 21, 2017

@nalimilan Good point - I will update the implementation (but it will lead to the inconsistency you have observed).

@@ -595,7 +595,7 @@ end
"""
first(str::AbstractString, nchar::Integer)

Get a string consisting of the first `nchar` characters of `str`.
Get a string consisting of at most the first `nchar` characters of `str`.
Copy link
Member

Choose a reason for hiding this comment

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

This wording makes it sound like the function may just randomly decide to give you any number of characters up to the number you asked for. It would be clearer to leave this sentence as is but add:

unless str is shorter than nchar characters, in which case a string equal to str is returned.

Copy link
Member Author

Choose a reason for hiding this comment

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

fixed

end

"""
last(str::AbstractString, nchar::Integer)

Get a string consisting of the last `nchar` characters of `str`.
Get a string consisting of at most the last `nchar` characters of `str`.
Copy link
Member

Choose a reason for hiding this comment

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

Similar comment as above.

Copy link
Member Author

Choose a reason for hiding this comment

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

fixed

@bkamins
Copy link
Member Author

bkamins commented Nov 23, 2017

CI failures seem unrelated.

@bkamins
Copy link
Member Author

bkamins commented Dec 24, 2017

closing. current implementation of first/last is in line with this PR.

@bkamins bkamins closed this Dec 24, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants