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::split_off. #38056

Merged
merged 1 commit into from
Dec 3, 2016
Merged

Add String::split_off. #38056

merged 1 commit into from
Dec 3, 2016

Conversation

clarfonthey
Copy link
Contributor

@clarfonthey clarfonthey commented Nov 28, 2016

This seems to match up with the latest version of the collection reform, and seems useful enough to add. First pull request!

@rust-highfive
Copy link
Collaborator

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @sfackler (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see the contribution instructions for more information.

@sfackler sfackler added the T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. label Nov 28, 2016
@sfackler
Copy link
Member

cc @rust-lang/libs

@aturon
Copy link
Member

aturon commented Nov 28, 2016

LGTM.

@alexcrichton
Copy link
Member

Looks good to me too. Can you add some tests as well?

Also, should the index check be <= to return empty strings as well?

@clarfonthey
Copy link
Contributor Author

clarfonthey commented Nov 28, 2016

@alexcrichton Yes and yes. Tests added. Not sure where I should add them, although where they are now should work.

#[unstable(feature = "string_split_off", issue = "0")]
pub fn split_off(&mut self, mid: usize) -> String {
assert!(self.is_char_boundary(mid));
assert!(mid <= self.len());
Copy link
Member

Choose a reason for hiding this comment

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

The self.is_char_boundary already returns false for mid > self.len(), so this assertion is dead.

Copy link
Member

@nagisa nagisa Nov 28, 2016

Choose a reason for hiding this comment

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

You also probably want to use slice_error_fail rather than plain assertion, because it will result in less code in callers and perhaps a bit better message (even if you specify mid for both start and end arguments of that function).

never mind, the slice_error_fail is private to core::str.

Copy link
Contributor Author

@clarfonthey clarfonthey Nov 29, 2016

Choose a reason for hiding this comment

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

@nagisa It appears that a few other functions in string.rs do both checks; how about I leave the redundant check in this commit and remove the redundant calls in another pull request?

Copy link
Member

Choose a reason for hiding this comment

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

Works for me.

@nagisa
Copy link
Member

nagisa commented Nov 29, 2016

Merge remote-tracking branch 'u/master'

You ought to rebase your branch onto the master rather than merge it in like this.

@frewsxcv
Copy link
Member

The tests you added should be moved into this file:

https://github.com/rust-lang/rust/blob/master/src/libcollectionstest/str.rs

@clarfonthey
Copy link
Contributor Author

clarfonthey commented Nov 29, 2016

@nagisa Wasn't sure what was preferred; I saw that squashing commits wasn't preferred and assumed that rebasing wasn't either. I've rebased.

@frewsxcv Done! I assume that you meant string.rs, but I can move them into str.rs if that's indeed the right place.

@alexcrichton
Copy link
Member

Looks great to me, thanks! Could you also open a new issue to track this method? If you cc me I'll tag it appropriately and then we can fill it in for the tracking issue here.

It looks like the travis build has a few errors as well?

@clarfonthey
Copy link
Contributor Author

Issue made, fixed the broken test that was causing the build to fail.

/// let mut hello = String::from("Hello, World!");
/// let world = hello.split_off(7);
/// assert_eq!(s, "Hello, ");
/// assert_eq!(s2, "World!");
Copy link
Member

Choose a reason for hiding this comment

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

s and s2 are called hello and world.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is what I get for copy-pasting on a pull request. >.<

@alexcrichton
Copy link
Member

The doc tests may still need an update?

failures:

---- string::String::split_off_0 stdout ----
	error: use of unstable library feature 'string_split_off' (see issue #38080)
 --> <anon>:5:19
  |
5 | let world = hello.split_off(7);
  |                   ^^^^^^^^^
  |
  = help: add #![feature(string_split_off)] to the crate attributes to enable

error: aborting due to previous error(s)

thread 'string::String::split_off_0' panicked at 'Box<Any>', /checkout/src/librustc/session/mod.rs:198
note: Run with `RUST_BACKTRACE=1` for a backtrace.


failures:
    string::String::split_off_0

Also, could you squash the commits down into one?

@clarfonthey
Copy link
Contributor Author

Rebased, squashed, and the doc test should be passing now.

@alexcrichton
Copy link
Member

@bors: r+

Thanks!

@bors
Copy link
Contributor

bors commented Dec 1, 2016

📌 Commit cbf734f has been approved by alexcrichton

@bors
Copy link
Contributor

bors commented Dec 2, 2016

⌛ Testing commit cbf734f with merge e00b9d0...

@bors
Copy link
Contributor

bors commented Dec 2, 2016

💔 Test failed - auto-mac-64-opt

@alexcrichton
Copy link
Member

alexcrichton commented Dec 2, 2016 via email

bors added a commit that referenced this pull request Dec 3, 2016
Add String::split_off.

This seems to match up with the latest version of the collection reform, and seems useful enough to add. First pull request!
@bors
Copy link
Contributor

bors commented Dec 3, 2016

⌛ Testing commit cbf734f with merge 24175e8...

@bors bors merged commit cbf734f into rust-lang:master Dec 3, 2016
bors added a commit that referenced this pull request Dec 7, 2016
Remove redundant assertion near is_char_boundary

Follow-up from #38056. `is_char_boundary` already checks for `idx <= len`, so, an extra assertion is redundant.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants