-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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 str::char_offset_iter() and str::rev_char_offset_iter() #8082
Conversation
priv string: &'self str, | ||
} | ||
|
||
impl<'self> Iterator<(char, uint)> for StrCharOffsetIterator<'self> { |
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.
You don't need to re-implement the StrCharIterator
, methods within the same package can read the private fields of that struct (index
)
While we're renaming things, could you also drop the If you'd rather not do that here though, I'll r+ with the changes. |
This iterator should be bidirectional. So far none of the str iterators are. |
@@ -1136,8 +1136,10 @@ pub trait StrSlice<'self> { | |||
fn contains_char(&self, needle: char) -> bool; | |||
fn iter(&self) -> StrCharIterator<'self>; | |||
fn rev_iter(&self) -> StrCharRevIterator<'self>; | |||
fn bytes_iter(&self) -> StrBytesIterator<'self>; | |||
fn bytes_rev_iter(&self) -> StrBytesRevIterator<'self>; | |||
fn byte_iter(&self) -> StrByteIterator<'self>; |
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.
Along the same lines as these should be bi-directional, why can't this just be a VecIterator
with some various bounds?
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.
It's a vec iterator composed with a dereference from &u8 to u8
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.
Oh good point... Although then it seems like there should be:
pub type ByteIterator = TransformIterator<..., ..., VecIterator<..., ...>>;
And then it just returns one of those.
Whoa, lots of notes
If I where to reuse the same struct, this:
would turn into this:
|
@Kimundi fwiw, when I was thinking about implementing this, I was going to write the type StrCharIterator<'self> = MapIterator<'self, (uint, char), char, StrCharOffsetIterator<'self>>;
fn iter() -> StrCharIterator<'self> {
self.char_offset_iter().transform(|(_, c)| c)
} (Similar to how |
Writing the iterators with |
@alexcrichton: Sounds good, it will have to wait a day more though :) |
I'm running the testsuite right now to catch remaining bugs, but here is what I've done so far:
|
* Fails during iteration if the string contains a non-whitespace | ||
* sequence longer than the limit. | ||
*/ | ||
priv fn each_split_within<'a>(ss: &'a str, |
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.
Could you add a note about why this was moved here, and note the fact that it's incorrect wrt multibyte codepoints.
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'm not sure if both of these things need to be noted here. It's there because the function above it needs it, and
it's working correctly, the function above just misuses it, which already has an Issue number.
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.
Oh, ok, I misinterpreted it; this seems fine then.
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.
That said, I could add a line like "Was moved here from std::str because this is the only place that uses it, and because it was to specific for a general sting function."
it's beautiful (my comments were addressed, too) |
Someone on irc raised an interesting point: Just having a How about replacing the CharOffsetIterator by a CharContextIterator that instead of a
Anyway, that would be an future patch. |
An iterator producing only |
Everything other than |
I'm sorry for the conflicts with pull #8090. Should be mostly |
Renamed bytes_iter to byte_iter to match other iterators Refactored str Iterators to use DoubleEnded Iterators and typedefs instead of wrapper structs Reordered the Iterator section Whitespace fixup Moved clunky `each_split_within` function to the one place in the tree where it's actually needed Replaced all block doccomments in str with line doccomments
Also renamed bytes_iter to byte_iter to match other iterators
Also renamed bytes_iter to byte_iter to match other iterators