-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Implement drain RFC #23071
Closed
Closed
Implement drain RFC #23071
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,14 +22,14 @@ use core::fmt; | |
use core::hash; | ||
use core::iter::{IntoIterator, FromIterator}; | ||
use core::mem; | ||
use core::ops::{self, Deref, Add, Index}; | ||
use core::ops::{self, Deref, Add, Index, Range, RangeTo, RangeFrom, RangeFull}; | ||
use core::ptr; | ||
use core::raw::Slice as RawSlice; | ||
use unicode::str as unicode_str; | ||
use unicode::str::Utf16Item; | ||
|
||
use borrow::{Cow, IntoCow}; | ||
use str::{self, CharRange, FromStr, Utf8Error}; | ||
use str::{self, CharRange, FromStr, Utf8Error, Chars}; | ||
use vec::{DerefVec, Vec, as_vec}; | ||
|
||
/// A growable string stored as a UTF-8 encoded buffer. | ||
|
@@ -670,6 +670,123 @@ impl String { | |
pub fn clear(&mut self) { | ||
self.vec.clear() | ||
} | ||
|
||
/// Creates a draining iterator that clears the specified byte-range in the String | ||
/// and iterates over the characters contained in the range, backshifting the | ||
/// remaining bytes. | ||
/// | ||
/// # Example | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also I thought the convention for Strings was that indices were always byte-wise? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. They are. |
||
/// | ||
/// ``` | ||
/// let mut s = "Hello Wörld!".to_string(); | ||
/// let s2: String = s.drain(6..12).collect(); | ||
/// assert_eq!(s, "Hello !"); | ||
/// assert_eq!(s2, "Wörld"); | ||
/// ``` | ||
/// | ||
/// # Panics | ||
/// | ||
/// Panics if the range is decreasing, if the upper bound is larger than the | ||
/// length of the String, or if the start and the end of the range don't lie on | ||
/// character boundaries. | ||
pub fn drain<'a, T: DrainRange>(&'a mut self, range: T) -> Drain<'a> { | ||
range.drain(self) | ||
} | ||
} | ||
|
||
/// A trait for draining a string. | ||
/// | ||
/// See the documentation of `String::drain`. | ||
pub trait DrainRange { | ||
fn drain<'a>(&self, s: &'a mut String) -> Drain<'a>; | ||
} | ||
|
||
impl DrainRange for Range<usize> { | ||
fn drain<'a>(&self, s: &'a mut String) -> Drain<'a> { | ||
assert!(self.start <= self.end, "Range not increasing"); | ||
assert!(self.end <= s.len(), "Range out of bounds"); | ||
unsafe { | ||
let slice = mem::transmute::<&str, &'static str>(&s[self.start..self.end]); | ||
let tail = s.len() - self.end; | ||
s.as_mut_vec().set_len(tail + self.start); | ||
let ptr = s.as_mut_vec().as_mut_ptr(); | ||
Drain { | ||
tail: tail, | ||
start: ptr.offset(self.start as isize), | ||
end: ptr.offset(self.end as isize), | ||
chars: slice.chars(), | ||
} | ||
} | ||
} | ||
} | ||
|
||
impl DrainRange for RangeFrom<usize> { | ||
fn drain<'a>(&self, s: &'a mut String) -> Drain<'a> { | ||
assert!(self.start <= s.len(), "Range out of bounds"); | ||
(self.start..s.len()).drain(s) | ||
} | ||
} | ||
|
||
impl DrainRange for RangeTo<usize> { | ||
fn drain<'a>(&self, s: &'a mut String) -> Drain<'a> { | ||
(0..self.end).drain(s) | ||
} | ||
} | ||
|
||
impl DrainRange for RangeFull { | ||
fn drain<'a>(&self, s: &'a mut String) -> Drain<'a> { | ||
(0..s.len()).drain(s) | ||
} | ||
} | ||
|
||
impl DrainRange for usize { | ||
fn drain<'a>(&self, s: &'a mut String) -> Drain<'a> { | ||
(*self..*self+1).drain(s) | ||
} | ||
} | ||
|
||
/// An iterator that drains part of string. | ||
#[unsafe_no_drop_flag] | ||
pub struct Drain<'a> { | ||
tail: usize, | ||
start: *const u8, | ||
end: *const u8, | ||
chars: Chars<'a>, | ||
} | ||
|
||
unsafe impl<'a> Sync for Drain<'a> {} | ||
unsafe impl<'a> Send for Drain<'a> {} | ||
|
||
impl<'a> Iterator for Drain<'a> { | ||
type Item = char; | ||
|
||
#[inline] | ||
fn next(&mut self) -> Option<char> { | ||
self.chars.next() | ||
} | ||
|
||
#[inline] | ||
fn size_hint(&self) -> (usize, Option<usize>) { | ||
self.chars.size_hint() | ||
} | ||
} | ||
|
||
impl<'a> DoubleEndedIterator for Drain<'a> { | ||
#[inline] | ||
fn next_back(&mut self) -> Option<char> { | ||
self.chars.next_back() | ||
} | ||
} | ||
|
||
#[unsafe_destructor] | ||
impl<'a> Drop for Drain<'a> { | ||
fn drop(&mut self) { | ||
// self.start == null if drop has already been called, so we can use | ||
// #[unsafe_no_drop_flag]. | ||
if !self.start.is_null() { | ||
unsafe { ptr::copy(self.start as *mut _, self.end, self.tail); } | ||
} | ||
} | ||
} | ||
|
||
impl FromUtf8Error { | ||
|
@@ -1442,4 +1559,12 @@ mod tests { | |
r | ||
}); | ||
} | ||
|
||
#[test] | ||
fn test_drain() { | ||
let mut s = "Hello Wörld!".to_string(); | ||
let s2: String = s.drain(6..12).collect(); | ||
assert_eq!(s, "Hello !"); | ||
assert_eq!(s2, "Wörld"); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 should clarify that it will backshift if necessary.