-
Notifications
You must be signed in to change notification settings - Fork 13.3k
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
Added slice_chars_from
and slice_chars_to
methods to StrSlice
#10544
Conversation
Hm, do we want this extra non-DRY code? I was thinking about this a little while ago, and though maybe making either or both arguments to Furthermore, having it explicitly in the types hopefully makes someone less like to do |
(At the very least, I think an internal helper function of that form would be an antidote to repeating similar code 3 times.) |
Yeah, at the very least it could all live in a internal helper, that's true. In a way, this is only a temporary API improvement anyway, as the string API still needs some major overhauls, and decisions (For example #9391 and #9440)
I think the most general solution would be to factor out the the ability to view a string as a sequence of bytes, a sequence of chars, and a sequence of grapheme clusters, and the provide an identical API for those three:
|
I pushed a commit that moves the common code into one function |
// only finding the char boundaries | ||
for (idx, _) in s.char_offset_iter() { | ||
match (begin, begin_byte) { | ||
(Some(begin), None) if count == begin => { begin_byte = Some(idx) }, _ => () |
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.
I think this would be clearer if the _
arm wasn't hiding at the end (also the ,
is unnecessary).
Addressed the stylistic issue |
They use a `slice_char_common` function internally Also made source code section markers more visible
Closing due to a lack of activity. I'm worried that if we start adding methods for chars as well as bytes then are we also going to have to start adding methods for graphemes as well? I don't think that this PR should fundamentally not be merged, but we need to give the |
…=Jarcho Ignore `file!()` macro in `print_literal`, `write_literal` changelog: [`print_literal`], [`write_literal`]: Ignore the `file!()` macro `file!()` expands to a string literal with its span set to that of the `file!()` callsite, but isn't marked as coming from an expansion. To fix this we make sure we actually find a string/char literal instead of assuming it's one and slicing It would also ignore any other macros that result in the same situation, but that shouldn't be common as `proc_macro::Span::call_site()` returns a span that is marked as from expansion Fixes rust-lang#10544
They are occasional useful, and not having them is inconsistent to the byte-indice slice methods.
slice_chars
,slice_chars_from
andslice_chars_to
are now implemented in terms of a commonslice_chars_common
function.I also made the section headers in
str.rs
more visible, as they are they where to small to be really useful.