Skip to content

Commit db4d60a

Browse files
author
Jakub Bukaj
committed
rollup merge of #18911: canndrew/slice_shift_char
`slice_shift_char` splits a `str` into it's leading `char` and the remainder of the `str`. Currently, it returns a `(Option<char>, &str)` such that: "bar".slice_shift_char() => (Some('b'), "ar") "ar".slice_shift_char() => (Some('a'), "r") "r".slice_shift_char() => (Some('r'), "") "".slice_shift_char() => (None, "") This is a little odd. Either a `str` can be split into both a head and a tail or it cannot. So the return type should be `Option<(char, &str)>`. With the current behaviour, in the case of the empty string, the `str` returned is meaningless - it is always the empty string. This PR changes `slice_shift_char` so that: "bar".slice_shift_char() => Some(('b', "ar")) "ar".slice_shift_char() => Some(('a', "r")) "r".slice_shift_char() => Some(('r', "")) "".slice_shift_char() => None
2 parents 7137c2c + 197a0ac commit db4d60a

File tree

5 files changed

+25
-25
lines changed

5 files changed

+25
-25
lines changed

src/libcollections/str.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1263,13 +1263,13 @@ mod tests {
12631263
#[test]
12641264
fn test_slice_shift_char() {
12651265
let data = "ประเทศไทย中";
1266-
assert_eq!(data.slice_shift_char(), (Some('ป'), "ระเทศไทย中"));
1266+
assert_eq!(data.slice_shift_char(), Some(('ป', "ระเทศไทย中")));
12671267
}
12681268

12691269
#[test]
12701270
fn test_slice_shift_char_2() {
12711271
let empty = "";
1272-
assert_eq!(empty.slice_shift_char(), (None, ""));
1272+
assert_eq!(empty.slice_shift_char(), None);
12731273
}
12741274

12751275
#[test]

src/libcore/num/mod.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1457,10 +1457,10 @@ macro_rules! from_str_radix_float_impl {
14571457
}
14581458

14591459
let (is_positive, src) = match src.slice_shift_char() {
1460-
(None, _) => return None,
1461-
(Some('-'), "") => return None,
1462-
(Some('-'), src) => (false, src),
1463-
(Some(_), _) => (true, src),
1460+
None => return None,
1461+
Some(('-', "")) => return None,
1462+
Some(('-', src)) => (false, src),
1463+
Some((_, _)) => (true, src),
14641464
};
14651465

14661466
// The significand to accumulate
@@ -1563,10 +1563,10 @@ macro_rules! from_str_radix_float_impl {
15631563
// Parse the exponent as decimal integer
15641564
let src = src[offset..];
15651565
let (is_positive, exp) = match src.slice_shift_char() {
1566-
(Some('-'), src) => (false, from_str::<uint>(src)),
1567-
(Some('+'), src) => (true, from_str::<uint>(src)),
1568-
(Some(_), _) => (true, from_str::<uint>(src)),
1569-
(None, _) => return None,
1566+
Some(('-', src)) => (false, from_str::<uint>(src)),
1567+
Some(('+', src)) => (true, from_str::<uint>(src)),
1568+
Some((_, _)) => (true, from_str::<uint>(src)),
1569+
None => return None,
15701570
};
15711571

15721572
match (is_positive, exp) {
@@ -1606,7 +1606,7 @@ macro_rules! from_str_radix_int_impl {
16061606
let is_signed_ty = (0 as $T) > Int::min_value();
16071607

16081608
match src.slice_shift_char() {
1609-
(Some('-'), src) if is_signed_ty => {
1609+
Some(('-', src)) if is_signed_ty => {
16101610
// The number is negative
16111611
let mut result = 0;
16121612
for c in src.chars() {
@@ -1625,7 +1625,7 @@ macro_rules! from_str_radix_int_impl {
16251625
}
16261626
Some(result)
16271627
},
1628-
(Some(_), _) => {
1628+
Some((_, _)) => {
16291629
// The number is signed
16301630
let mut result = 0;
16311631
for c in src.chars() {
@@ -1644,7 +1644,7 @@ macro_rules! from_str_radix_int_impl {
16441644
}
16451645
Some(result)
16461646
},
1647-
(None, _) => None,
1647+
None => None,
16481648
}
16491649
}
16501650
}

src/libcore/str.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1811,21 +1811,21 @@ pub trait StrPrelude for Sized? {
18111811
/// it. This does not allocate a new string; instead, it returns a
18121812
/// slice that point one character beyond the character that was
18131813
/// shifted. If the string does not contain any characters,
1814-
/// a tuple of None and an empty string is returned instead.
1814+
/// None is returned instead.
18151815
///
18161816
/// # Example
18171817
///
18181818
/// ```rust
18191819
/// let s = "Löwe 老虎 Léopard";
1820-
/// let (c, s1) = s.slice_shift_char();
1821-
/// assert_eq!(c, Some('L'));
1820+
/// let (c, s1) = s.slice_shift_char().unwrap();
1821+
/// assert_eq!(c, 'L');
18221822
/// assert_eq!(s1, "öwe 老虎 Léopard");
18231823
///
1824-
/// let (c, s2) = s1.slice_shift_char();
1825-
/// assert_eq!(c, Some('ö'));
1824+
/// let (c, s2) = s1.slice_shift_char().unwrap();
1825+
/// assert_eq!(c, 'ö');
18261826
/// assert_eq!(s2, "we 老虎 Léopard");
18271827
/// ```
1828-
fn slice_shift_char<'a>(&'a self) -> (Option<char>, &'a str);
1828+
fn slice_shift_char<'a>(&'a self) -> Option<(char, &'a str)>;
18291829

18301830
/// Returns the byte offset of an inner slice relative to an enclosing outer slice.
18311831
///
@@ -2197,13 +2197,13 @@ impl StrPrelude for str {
21972197
}
21982198

21992199
#[inline]
2200-
fn slice_shift_char(&self) -> (Option<char>, &str) {
2200+
fn slice_shift_char(&self) -> Option<(char, &str)> {
22012201
if self.is_empty() {
2202-
return (None, self);
2202+
None
22032203
} else {
22042204
let CharRange {ch, next} = self.char_range_at(0u);
22052205
let next_s = unsafe { raw::slice_bytes(self, next, self.len()) };
2206-
return (Some(ch), next_s);
2206+
Some((ch, next_s))
22072207
}
22082208
}
22092209

src/libsyntax/ext/asm.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,8 @@ pub fn expand_asm<'cx>(cx: &'cx mut ExtCtxt, sp: Span, tts: &[ast::TokenTree])
9797
// cannot be shared with any other operand (usually when
9898
// a register is clobbered early.)
9999
let output = match constraint.get().slice_shift_char() {
100-
(Some('='), _) => None,
101-
(Some('+'), operand) => {
100+
Some(('=', _)) => None,
101+
Some(('+', operand)) => {
102102
Some(token::intern_and_get_ident(format!(
103103
"={}",
104104
operand).as_slice()))

src/libsyntax/print/pprust.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1855,7 +1855,7 @@ impl<'a> State<'a> {
18551855
try!(self.commasep(Inconsistent, a.outputs.as_slice(),
18561856
|s, &(ref co, ref o, is_rw)| {
18571857
match co.get().slice_shift_char() {
1858-
(Some('='), operand) if is_rw => {
1858+
Some(('=', operand)) if is_rw => {
18591859
try!(s.print_string(format!("+{}", operand).as_slice(),
18601860
ast::CookedStr))
18611861
}

0 commit comments

Comments
 (0)