Skip to content

Address &str.slice is bloated #16698

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

Merged
merged 1 commit into from
Aug 24, 2014
Merged
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
42 changes: 31 additions & 11 deletions src/libcore/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1705,6 +1705,13 @@ pub trait StrSlice<'a> {
fn utf16_units(&self) -> Utf16CodeUnits<'a>;
}

#[inline(never)]
Copy link
Contributor

Choose a reason for hiding this comment

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

This can be #[cold], too

fn slice_error_fail(s: &str, begin: uint, end: uint) -> ! {
assert!(begin <= end);
fail!("index {} and/or {} in `{}` do not lie on character boundary",
begin, end, s);
}

impl<'a> StrSlice<'a> for &'a str {
#[inline]
fn contains<'a>(&self, needle: &'a str) -> bool {
Expand Down Expand Up @@ -1808,22 +1815,34 @@ impl<'a> StrSlice<'a> for &'a str {

#[inline]
fn slice(&self, begin: uint, end: uint) -> &'a str {
assert!(self.is_char_boundary(begin) && self.is_char_boundary(end),
"index {} and/or {} in `{}` do not lie on character boundary", begin,
end, *self);
unsafe { raw::slice_bytes(*self, begin, end) }
// is_char_boundary checks that the index is in [0, .len()]
if begin <= end &&
self.is_char_boundary(begin) &&
self.is_char_boundary(end) {
unsafe { raw::slice_unchecked(*self, begin, end) }
} else {
slice_error_fail(*self, begin, end)
}
}

#[inline]
fn slice_from(&self, begin: uint) -> &'a str {
self.slice(begin, self.len())
// is_char_boundary checks that the index is in [0, .len()]
if self.is_char_boundary(begin) {
unsafe { raw::slice_unchecked(*self, begin, self.len()) }
} else {
slice_error_fail(*self, begin, self.len())
}
}

#[inline]
fn slice_to(&self, end: uint) -> &'a str {
assert!(self.is_char_boundary(end), "index {} in `{}` does not lie on \
a character boundary", end, *self);
unsafe { raw::slice_bytes(*self, 0, end) }
// is_char_boundary checks that the index is in [0, .len()]
if self.is_char_boundary(end) {
unsafe { raw::slice_unchecked(*self, 0, end) }
} else {
slice_error_fail(*self, 0, end)
}
}

fn slice_chars(&self, begin: uint, end: uint) -> &'a str {
Expand Down Expand Up @@ -1898,9 +1917,10 @@ impl<'a> StrSlice<'a> for &'a str {
#[inline]
fn is_char_boundary(&self, index: uint) -> bool {
if index == self.len() { return true; }
if index > self.len() { return false; }
let b = self.as_bytes()[index];
return b < 128u8 || b >= 192u8;
match self.as_bytes().get(index) {
None => false,
Some(&b) => b < 128u8 || b >= 192u8,
}
}

#[inline]
Expand Down