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

Add String::extend_from_within #85801

Merged
merged 1 commit into from
May 30, 2021
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
1 change: 1 addition & 0 deletions library/alloc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@
#![feature(associated_type_bounds)]
#![feature(slice_group_by)]
#![feature(decl_macro)]
#![feature(bindings_after_at)]
// Allow testing this library

#[cfg(test)]
Expand Down
36 changes: 36 additions & 0 deletions library/alloc/src/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -843,6 +843,42 @@ impl String {
self.vec.extend_from_slice(string.as_bytes())
}

/// Copies elements from `src` range to the end of the string.
///
/// ## Panics
///
/// Panics if the starting point or end point do not lie on a [`char`]
/// boundary, or if they're out of bounds.
///
/// ## Examples
///
/// ```
/// #![feature(string_extend_from_within)]
/// let mut string = String::from("abcde");
///
/// string.extend_from_within(2..);
/// assert_eq!(string, "abcdecde");
///
/// string.extend_from_within(..2);
/// assert_eq!(string, "abcdecdeab");
///
/// string.extend_from_within(4..8);
/// assert_eq!(string, "abcdecdeabecde");
/// ```
#[cfg(not(no_global_oom_handling))]
#[unstable(feature = "string_extend_from_within", issue = "none")]
pub fn extend_from_within<R>(&mut self, src: R)
where
R: RangeBounds<usize>,
{
let src @ Range { start, end } = slice::range(src, ..self.len());

assert!(self.is_char_boundary(start));
assert!(self.is_char_boundary(end));
Comment on lines +876 to +877
Copy link
Contributor

Choose a reason for hiding this comment

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

If panic happens, how does one know if it panics because of start or end?

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 think it should be possible by checking the line number in the panic message. It isn't convenient, but it is possible.

Also, note that the same pattern appears multiple times in this file:

assert!(self.is_char_boundary(start));
assert!(self.is_char_boundary(end));
let start = range.start_bound();
match start {
Included(&n) => assert!(self.is_char_boundary(n)),
Excluded(&n) => assert!(self.is_char_boundary(n + 1)),
Unbounded => {}
};
// WARNING: Inlining this variable would be unsound (#81138)
let end = range.end_bound();
match end {
Included(&n) => assert!(self.is_char_boundary(n + 1)),
Excluded(&n) => assert!(self.is_char_boundary(n)),
Unbounded => {}
};

Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe we should have a separate issue for this.

Copy link
Contributor

@pickfire pickfire May 31, 2021

Choose a reason for hiding this comment

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

Wait, it's not necessary. The current panic error message is good enough. (but still without the function name, like drain)

thread 'main' panicked at 'assertion failed: self.is_char_boundary(start)', /rustc/9bc8c42bb2f19e745a63f3445f1ac248fb015e53/library/alloc/src/string.rs:1571:9

https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=4e29eec2c640b0a42b054f58b1f6753d

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah, so when called without a message, assert! prints the passed expression. That's nice.


self.vec.extend_from_within(src);
}

/// Returns this `String`'s capacity, in bytes.
///
/// # Examples
Expand Down