Skip to content

Commit 4519704

Browse files
committed
---
yaml --- r: 63221 b: refs/heads/snap-stage3 c: ba4a477 h: refs/heads/master i: 63219: cd1fdd6 v: v3
1 parent c1e2f4d commit 4519704

File tree

4 files changed

+94
-68
lines changed

4 files changed

+94
-68
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
refs/heads/master: 2d28d645422c1617be58c8ca7ad9a457264ca850
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
4-
refs/heads/snap-stage3: 8786bca7e2978e6c1d6eed7e61680b0f25db1f18
4+
refs/heads/snap-stage3: ba4a4778cc17c64c33a891a0d2565a1fb04ddffc
55
refs/heads/try: 7b78b52e602bb3ea8174f9b2006bff3315f03ef9
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b

branches/snap-stage3/src/librustc/back/link.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -637,7 +637,7 @@ pub fn symbol_hash(tcx: ty::ctxt,
637637
write_string(symbol_hasher, encoder::encoded_ty(tcx, t));
638638
let mut hash = truncated_hash_result(symbol_hasher);
639639
// Prefix with _ so that it never blends into adjacent digits
640-
str::unshift_char(&mut hash, '_');
640+
hash.unshift_char('_');
641641
// tjc: allocation is unfortunate; need to change core::hash
642642
hash.to_managed()
643643
}

branches/snap-stage3/src/libstd/str.rs

Lines changed: 87 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -304,63 +304,6 @@ impl<'self> StrVector for &'self [&'self str] {
304304
}
305305
}
306306

307-
/*
308-
Section: Adding to and removing from a string
309-
*/
310-
311-
/**
312-
* Remove the final character from a string and return it
313-
*
314-
* # Failure
315-
*
316-
* If the string does not contain any characters
317-
*/
318-
pub fn pop_char(s: &mut ~str) -> char {
319-
let end = s.len();
320-
assert!(end > 0u);
321-
let CharRange {ch, next} = s.char_range_at_reverse(end);
322-
unsafe { raw::set_len(s, next); }
323-
return ch;
324-
}
325-
326-
/**
327-
* Remove the first character from a string and return it
328-
*
329-
* # Failure
330-
*
331-
* If the string does not contain any characters
332-
*/
333-
pub fn shift_char(s: &mut ~str) -> char {
334-
let CharRange {ch, next} = s.char_range_at(0u);
335-
*s = unsafe { raw::slice_bytes_owned(*s, next, s.len()) };
336-
return ch;
337-
}
338-
339-
/**
340-
* Removes the first character from a string slice and returns it. This does
341-
* not allocate a new string; instead, it mutates a slice to point one
342-
* character beyond the character that was shifted.
343-
*
344-
* # Failure
345-
*
346-
* If the string does not contain any characters
347-
*/
348-
#[inline]
349-
pub fn slice_shift_char<'a>(s: &'a str) -> (char, &'a str) {
350-
let CharRange {ch, next} = s.char_range_at(0u);
351-
let next_s = unsafe { raw::slice_bytes(s, next, s.len()) };
352-
return (ch, next_s);
353-
}
354-
355-
/// Prepend a char to a string
356-
pub fn unshift_char(s: &mut ~str, ch: char) {
357-
// This could be more efficient.
358-
let mut new_str = ~"";
359-
new_str.push_char(ch);
360-
new_str.push_str(*s);
361-
*s = new_str;
362-
}
363-
364307
/*
365308
Section: Transforming strings
366309
*/
@@ -1546,6 +1489,8 @@ pub trait StrSlice<'self> {
15461489
fn find_str(&self, &str) -> Option<uint>;
15471490
15481491
fn repeat(&self, nn: uint) -> ~str;
1492+
1493+
fn slice_shift_char(&self) -> (char, &'self str);
15491494
}
15501495
15511496
/// Extension methods for strings
@@ -1757,7 +1702,7 @@ impl<'self> StrSlice<'self> for &'self str {
17571702
else { match_at(*self, needle, 0u) }
17581703
}
17591704
/// Returns true if `needle` is a suffix of the string.
1760-
pub fn ends_with(&self, needle: &str) -> bool {
1705+
fn ends_with(&self, needle: &str) -> bool {
17611706
let (self_len, needle_len) = (self.len(), needle.len());
17621707
if needle_len == 0u { true }
17631708
else if needle_len > self_len { false }
@@ -2085,13 +2030,35 @@ impl<'self> StrSlice<'self> for &'self str {
20852030
ret
20862031
}
20872032
}
2033+
2034+
/**
2035+
* Retrieves the first character from a string slice and returns
2036+
* it. This does not allocate a new string; instead, it returns a
2037+
* slice that point one character beyond the character that was
2038+
* shifted.
2039+
*
2040+
* # Failure
2041+
*
2042+
* If the string does not contain any characters
2043+
*/
2044+
#[inline]
2045+
fn slice_shift_char(&self) -> (char, &'self str) {
2046+
let CharRange {ch, next} = self.char_range_at(0u);
2047+
let next_s = unsafe { raw::slice_bytes(*self, next, self.len()) };
2048+
return (ch, next_s);
2049+
}
2050+
2051+
20882052
}
20892053
20902054
#[allow(missing_doc)]
20912055
pub trait OwnedStr {
20922056
fn push_str_no_overallocate(&mut self, rhs: &str);
20932057
fn push_str(&mut self, rhs: &str);
20942058
fn push_char(&mut self, c: char);
2059+
fn pop_char(&mut self) -> char;
2060+
fn shift_char(&mut self) -> char;
2061+
fn unshift_char(&mut self, ch: char);
20952062
fn append(&self, rhs: &str) -> ~str; // FIXME #4850: this should consume self.
20962063
fn reserve(&mut self, n: uint);
20972064
fn reserve_at_least(&mut self, n: uint);
@@ -2190,6 +2157,43 @@ impl OwnedStr for ~str {
21902157
raw::set_len(self, new_len);
21912158
}
21922159
}
2160+
/**
2161+
* Remove the final character from a string and return it
2162+
*
2163+
* # Failure
2164+
*
2165+
* If the string does not contain any characters
2166+
*/
2167+
fn pop_char(&mut self) -> char {
2168+
let end = self.len();
2169+
assert!(end > 0u);
2170+
let CharRange {ch, next} = self.char_range_at_reverse(end);
2171+
unsafe { raw::set_len(self, next); }
2172+
return ch;
2173+
}
2174+
2175+
/**
2176+
* Remove the first character from a string and return it
2177+
*
2178+
* # Failure
2179+
*
2180+
* If the string does not contain any characters
2181+
*/
2182+
fn shift_char(&mut self) -> char {
2183+
let CharRange {ch, next} = self.char_range_at(0u);
2184+
*self = unsafe { raw::slice_bytes_owned(*self, next, self.len()) };
2185+
return ch;
2186+
}
2187+
2188+
/// Prepend a char to a string
2189+
fn unshift_char(&mut self, ch: char) {
2190+
// This could be more efficient.
2191+
let mut new_str = ~"";
2192+
new_str.push_char(ch);
2193+
new_str.push_str(*self);
2194+
*self = new_str;
2195+
}
2196+
21932197
/// Concatenate two strings together.
21942198
#[inline]
21952199
fn append(&self, rhs: &str) -> ~str {
@@ -2421,15 +2425,15 @@ mod tests {
24212425
#[test]
24222426
fn test_pop_char() {
24232427
let mut data = ~"ประเทศไทย中华";
2424-
let cc = pop_char(&mut data);
2428+
let cc = data.pop_char();
24252429
assert_eq!(~"ประเทศไทย中", data);
24262430
assert_eq!('华', cc);
24272431
}
24282432
24292433
#[test]
24302434
fn test_pop_char_2() {
24312435
let mut data2 = ~"";
2432-
let cc2 = pop_char(&mut data2);
2436+
let cc2 = data2.pop_char();
24332437
assert_eq!(~"", data2);
24342438
assert_eq!('华', cc2);
24352439
}
@@ -2439,7 +2443,29 @@ mod tests {
24392443
#[ignore(cfg(windows))]
24402444
fn test_pop_char_fail() {
24412445
let mut data = ~"";
2442-
let _cc3 = pop_char(&mut data);
2446+
let _cc3 = data.pop_char();
2447+
}
2448+
2449+
#[test]
2450+
fn test_push_char() {
2451+
let mut data = ~"ประเทศไทย中";
2452+
data.push_char('华');
2453+
assert_eq!(~"ประเทศไทย中华", data);
2454+
}
2455+
2456+
#[test]
2457+
fn test_shift_char() {
2458+
let mut data = ~"ประเทศไทย中";
2459+
let cc = data.shift_char();
2460+
assert_eq!(~"ระเทศไทย中", data);
2461+
assert_eq!('ป', cc);
2462+
}
2463+
2464+
#[test]
2465+
fn test_unshift_char() {
2466+
let mut data = ~"ประเทศไทย中";
2467+
data.unshift_char('华');
2468+
assert_eq!(~"华ประเทศไทย中", data);
24432469
}
24442470
24452471
#[test]

branches/snap-stage3/src/test/run-pass/utf8_chars.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@ pub fn main() {
3333
assert!((!str::is_utf8(~[0xc0_u8, 0x10_u8])));
3434

3535
let mut stack = ~"a×c€";
36-
assert_eq!(str::pop_char(&mut stack), '€');
37-
assert_eq!(str::pop_char(&mut stack), 'c');
36+
assert_eq!(stack.pop_char(), '€');
37+
assert_eq!(stack.pop_char(), 'c');
3838
stack.push_char('u');
3939
assert!(stack == ~"a×u");
40-
assert_eq!(str::shift_char(&mut stack), 'a');
41-
assert_eq!(str::shift_char(&mut stack), '×');
42-
str::unshift_char(&mut stack, 'ß');
40+
assert_eq!(stack.shift_char(), 'a');
41+
assert_eq!(stack.shift_char(), '×');
42+
stack.unshift_char('ß');
4343
assert!(stack == ~"ßu");
4444
}

0 commit comments

Comments
 (0)