Skip to content

Commit 44e8021

Browse files
committed
Even more fallout, this time in std::str
- StrSlice.to_utf16() now returns a Vec<u8>. - Other miscellaneous fallout in std::str.
1 parent 8e42fde commit 44e8021

File tree

1 file changed

+85
-92
lines changed

1 file changed

+85
-92
lines changed

src/libstd/str.rs

Lines changed: 85 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ use iter::{Iterator, range, AdditiveIterator};
8787
use option::{None, Option, Some};
8888
use ptr;
8989
use from_str::FromStr;
90-
use slice::{OwnedVector, ImmutableVector, MutableVector};
90+
use slice::{OwnedVector, ImmutableVector, MutableVector, CloneableVector};
9191
use slice::{Vector};
9292
use vec::Vec;
9393
use default::Default;
@@ -674,19 +674,17 @@ pub mod raw {
674674
use ptr;
675675
use slice::{MutableVector, OwnedVector, Vector};
676676
use str::{is_utf8};
677-
use vec::Vec;
678677

679678
pub use core::str::raw::{from_utf8, c_str_to_static_slice, slice_bytes};
680679
pub use core::str::raw::{slice_unchecked};
681680

682681
/// Create a Rust string from a *u8 buffer of the given length
683682
pub unsafe fn from_buf_len(buf: *u8, len: uint) -> ~str {
684-
let mut v = Vec::with_capacity(len);
685-
ptr::copy_memory(v.as_mut_ptr(), buf, len);
686-
v.set_len(len);
687-
688-
assert!(is_utf8(v.as_slice()));
689-
::cast::transmute(v.move_iter().collect::<~[u8]>())
683+
let v = Slice { data: buf, len: len };
684+
let bytes: &[u8] = ::cast::transmute(v);
685+
assert!(is_utf8(bytes));
686+
let s: &str = ::cast::transmute(bytes);
687+
s.to_owned()
690688
}
691689

692690
#[lang="strdup_uniq"]
@@ -824,27 +822,21 @@ pub trait StrAllocating: Str {
824822
/// Copy a slice into a new owned str.
825823
#[inline]
826824
fn to_owned(&self) -> ~str {
827-
let me = self.as_slice();
828-
let len = me.len();
829825
unsafe {
830-
let mut v = Vec::with_capacity(len);
831-
832-
ptr::copy_memory(v.as_mut_ptr(), me.as_ptr(), len);
833-
v.set_len(len);
834-
::cast::transmute(v.move_iter().collect::<~[u8]>())
826+
::cast::transmute(self.as_bytes().to_owned())
835827
}
836828
}
837829

838830
/// Converts to a vector of `u16` encoded as UTF-16.
839-
fn to_utf16(&self) -> ~[u16] {
831+
fn to_utf16(&self) -> Vec<u16> {
840832
let me = self.as_slice();
841-
let mut u = Vec::new();;
833+
let mut u = Vec::new();
842834
for ch in me.chars() {
843835
let mut buf = [0u16, ..2];
844836
let n = ch.encode_utf16(buf /* as mut slice! */);
845837
u.push_all(buf.slice_to(n));
846838
}
847-
u.move_iter().collect()
839+
u
848840
}
849841

850842
/// Given a string, make a new string with repeated copies of it.
@@ -1554,7 +1546,8 @@ mod tests {
15541546
assert_eq!(a.subslice_offset(c), 0);
15551547

15561548
let string = "a\nb\nc";
1557-
let lines: ~[&str] = string.lines().collect();
1549+
let lines: Vec<&str> = string.lines().collect();
1550+
let lines = lines.as_slice();
15581551
assert_eq!(string.subslice_offset(lines[0]), 0);
15591552
assert_eq!(string.subslice_offset(lines[1]), 2);
15601553
assert_eq!(string.subslice_offset(lines[2]), 4);
@@ -1617,21 +1610,21 @@ mod tests {
16171610
fn test_utf16() {
16181611
let pairs =
16191612
[("𐍅𐌿𐌻𐍆𐌹𐌻𐌰\n".to_owned(),
1620-
box [0xd800_u16, 0xdf45_u16, 0xd800_u16, 0xdf3f_u16,
1613+
vec![0xd800_u16, 0xdf45_u16, 0xd800_u16, 0xdf3f_u16,
16211614
0xd800_u16, 0xdf3b_u16, 0xd800_u16, 0xdf46_u16,
16221615
0xd800_u16, 0xdf39_u16, 0xd800_u16, 0xdf3b_u16,
16231616
0xd800_u16, 0xdf30_u16, 0x000a_u16]),
16241617

16251618
("𐐒𐑉𐐮𐑀𐐲𐑋 𐐏𐐲𐑍\n".to_owned(),
1626-
box [0xd801_u16, 0xdc12_u16, 0xd801_u16,
1619+
vec![0xd801_u16, 0xdc12_u16, 0xd801_u16,
16271620
0xdc49_u16, 0xd801_u16, 0xdc2e_u16, 0xd801_u16,
16281621
0xdc40_u16, 0xd801_u16, 0xdc32_u16, 0xd801_u16,
16291622
0xdc4b_u16, 0x0020_u16, 0xd801_u16, 0xdc0f_u16,
16301623
0xd801_u16, 0xdc32_u16, 0xd801_u16, 0xdc4d_u16,
16311624
0x000a_u16]),
16321625

16331626
("𐌀𐌖𐌋𐌄𐌑𐌉·𐌌𐌄𐌕𐌄𐌋𐌉𐌑\n".to_owned(),
1634-
box [0xd800_u16, 0xdf00_u16, 0xd800_u16, 0xdf16_u16,
1627+
vec![0xd800_u16, 0xdf00_u16, 0xd800_u16, 0xdf16_u16,
16351628
0xd800_u16, 0xdf0b_u16, 0xd800_u16, 0xdf04_u16,
16361629
0xd800_u16, 0xdf11_u16, 0xd800_u16, 0xdf09_u16,
16371630
0x00b7_u16, 0xd800_u16, 0xdf0c_u16, 0xd800_u16,
@@ -1640,7 +1633,7 @@ mod tests {
16401633
0xdf09_u16, 0xd800_u16, 0xdf11_u16, 0x000a_u16 ]),
16411634

16421635
("𐒋𐒘𐒈𐒑𐒛𐒒 𐒕𐒓 𐒈𐒚𐒍 𐒏𐒜𐒒𐒖𐒆 𐒕𐒆\n".to_owned(),
1643-
box [0xd801_u16, 0xdc8b_u16, 0xd801_u16, 0xdc98_u16,
1636+
vec![0xd801_u16, 0xdc8b_u16, 0xd801_u16, 0xdc98_u16,
16441637
0xd801_u16, 0xdc88_u16, 0xd801_u16, 0xdc91_u16,
16451638
0xd801_u16, 0xdc9b_u16, 0xd801_u16, 0xdc92_u16,
16461639
0x0020_u16, 0xd801_u16, 0xdc95_u16, 0xd801_u16,
@@ -1653,18 +1646,18 @@ mod tests {
16531646
0x000a_u16 ]),
16541647
// Issue #12318, even-numbered non-BMP planes
16551648
("\U00020000".to_owned(),
1656-
box [0xD840, 0xDC00])];
1649+
vec![0xD840, 0xDC00])];
16571650

16581651
for p in pairs.iter() {
16591652
let (s, u) = (*p).clone();
1660-
assert!(is_utf16(u));
1653+
assert!(is_utf16(u.as_slice()));
16611654
assert_eq!(s.to_utf16(), u);
16621655

1663-
assert_eq!(from_utf16(u).unwrap(), s);
1664-
assert_eq!(from_utf16_lossy(u), s);
1656+
assert_eq!(from_utf16(u.as_slice()).unwrap(), s);
1657+
assert_eq!(from_utf16_lossy(u.as_slice()), s);
16651658

1666-
assert_eq!(from_utf16(s.to_utf16()).unwrap(), s);
1667-
assert_eq!(from_utf16(u).unwrap().to_utf16(), u);
1659+
assert_eq!(from_utf16(s.to_utf16().as_slice()).unwrap(), s);
1660+
assert_eq!(from_utf16(u.as_slice()).unwrap().to_utf16(), u);
16681661
}
16691662
}
16701663

@@ -1921,105 +1914,105 @@ mod tests {
19211914
fn test_split_char_iterator() {
19221915
let data = "\nMäry häd ä little lämb\nLittle lämb\n";
19231916

1924-
let split: ~[&str] = data.split(' ').collect();
1925-
assert_eq!( split, box ["\nMäry", "häd", "ä", "little", "lämb\nLittle", "lämb\n"]);
1917+
let split: Vec<&str> = data.split(' ').collect();
1918+
assert_eq!( split, vec!["\nMäry", "häd", "ä", "little", "lämb\nLittle", "lämb\n"]);
19261919

1927-
let mut rsplit: ~[&str] = data.split(' ').rev().collect();
1920+
let mut rsplit: Vec<&str> = data.split(' ').rev().collect();
19281921
rsplit.reverse();
1929-
assert_eq!(rsplit, box ["\nMäry", "häd", "ä", "little", "lämb\nLittle", "lämb\n"]);
1922+
assert_eq!(rsplit, vec!["\nMäry", "häd", "ä", "little", "lämb\nLittle", "lämb\n"]);
19301923

1931-
let split: ~[&str] = data.split(|c: char| c == ' ').collect();
1932-
assert_eq!( split, box ["\nMäry", "häd", "ä", "little", "lämb\nLittle", "lämb\n"]);
1924+
let split: Vec<&str> = data.split(|c: char| c == ' ').collect();
1925+
assert_eq!( split, vec!["\nMäry", "häd", "ä", "little", "lämb\nLittle", "lämb\n"]);
19331926

1934-
let mut rsplit: ~[&str] = data.split(|c: char| c == ' ').rev().collect();
1927+
let mut rsplit: Vec<&str> = data.split(|c: char| c == ' ').rev().collect();
19351928
rsplit.reverse();
1936-
assert_eq!(rsplit, box ["\nMäry", "häd", "ä", "little", "lämb\nLittle", "lämb\n"]);
1929+
assert_eq!(rsplit, vec!["\nMäry", "häd", "ä", "little", "lämb\nLittle", "lämb\n"]);
19371930

19381931
// Unicode
1939-
let split: ~[&str] = data.split('ä').collect();
1940-
assert_eq!( split, box ["\nM", "ry h", "d ", " little l", "mb\nLittle l", "mb\n"]);
1932+
let split: Vec<&str> = data.split('ä').collect();
1933+
assert_eq!( split, vec!["\nM", "ry h", "d ", " little l", "mb\nLittle l", "mb\n"]);
19411934

1942-
let mut rsplit: ~[&str] = data.split('ä').rev().collect();
1935+
let mut rsplit: Vec<&str> = data.split('ä').rev().collect();
19431936
rsplit.reverse();
1944-
assert_eq!(rsplit, box ["\nM", "ry h", "d ", " little l", "mb\nLittle l", "mb\n"]);
1937+
assert_eq!(rsplit, vec!["\nM", "ry h", "d ", " little l", "mb\nLittle l", "mb\n"]);
19451938

1946-
let split: ~[&str] = data.split(|c: char| c == 'ä').collect();
1947-
assert_eq!( split, box ["\nM", "ry h", "d ", " little l", "mb\nLittle l", "mb\n"]);
1939+
let split: Vec<&str> = data.split(|c: char| c == 'ä').collect();
1940+
assert_eq!( split, vec!["\nM", "ry h", "d ", " little l", "mb\nLittle l", "mb\n"]);
19481941

1949-
let mut rsplit: ~[&str] = data.split(|c: char| c == 'ä').rev().collect();
1942+
let mut rsplit: Vec<&str> = data.split(|c: char| c == 'ä').rev().collect();
19501943
rsplit.reverse();
1951-
assert_eq!(rsplit, box ["\nM", "ry h", "d ", " little l", "mb\nLittle l", "mb\n"]);
1944+
assert_eq!(rsplit, vec!["\nM", "ry h", "d ", " little l", "mb\nLittle l", "mb\n"]);
19521945
}
19531946

19541947
#[test]
19551948
fn test_splitn_char_iterator() {
19561949
let data = "\nMäry häd ä little lämb\nLittle lämb\n";
19571950

1958-
let split: ~[&str] = data.splitn(' ', 3).collect();
1959-
assert_eq!(split, box ["\nMäry", "häd", "ä", "little lämb\nLittle lämb\n"]);
1951+
let split: Vec<&str> = data.splitn(' ', 3).collect();
1952+
assert_eq!(split, vec!["\nMäry", "häd", "ä", "little lämb\nLittle lämb\n"]);
19601953

1961-
let split: ~[&str] = data.splitn(|c: char| c == ' ', 3).collect();
1962-
assert_eq!(split, box ["\nMäry", "häd", "ä", "little lämb\nLittle lämb\n"]);
1954+
let split: Vec<&str> = data.splitn(|c: char| c == ' ', 3).collect();
1955+
assert_eq!(split, vec!["\nMäry", "häd", "ä", "little lämb\nLittle lämb\n"]);
19631956

19641957
// Unicode
1965-
let split: ~[&str] = data.splitn('ä', 3).collect();
1966-
assert_eq!(split, box ["\nM", "ry h", "d ", " little lämb\nLittle lämb\n"]);
1958+
let split: Vec<&str> = data.splitn('ä', 3).collect();
1959+
assert_eq!(split, vec!["\nM", "ry h", "d ", " little lämb\nLittle lämb\n"]);
19671960

1968-
let split: ~[&str] = data.splitn(|c: char| c == 'ä', 3).collect();
1969-
assert_eq!(split, box ["\nM", "ry h", "d ", " little lämb\nLittle lämb\n"]);
1961+
let split: Vec<&str> = data.splitn(|c: char| c == 'ä', 3).collect();
1962+
assert_eq!(split, vec!["\nM", "ry h", "d ", " little lämb\nLittle lämb\n"]);
19701963
}
19711964

19721965
#[test]
19731966
fn test_rsplitn_char_iterator() {
19741967
let data = "\nMäry häd ä little lämb\nLittle lämb\n";
19751968

1976-
let mut split: ~[&str] = data.rsplitn(' ', 3).collect();
1969+
let mut split: Vec<&str> = data.rsplitn(' ', 3).collect();
19771970
split.reverse();
1978-
assert_eq!(split, box ["\nMäry häd ä", "little", "lämb\nLittle", "lämb\n"]);
1971+
assert_eq!(split, vec!["\nMäry häd ä", "little", "lämb\nLittle", "lämb\n"]);
19791972

1980-
let mut split: ~[&str] = data.rsplitn(|c: char| c == ' ', 3).collect();
1973+
let mut split: Vec<&str> = data.rsplitn(|c: char| c == ' ', 3).collect();
19811974
split.reverse();
1982-
assert_eq!(split, box ["\nMäry häd ä", "little", "lämb\nLittle", "lämb\n"]);
1975+
assert_eq!(split, vec!["\nMäry häd ä", "little", "lämb\nLittle", "lämb\n"]);
19831976

19841977
// Unicode
1985-
let mut split: ~[&str] = data.rsplitn('ä', 3).collect();
1978+
let mut split: Vec<&str> = data.rsplitn('ä', 3).collect();
19861979
split.reverse();
1987-
assert_eq!(split, box ["\nMäry häd ", " little l", "mb\nLittle l", "mb\n"]);
1980+
assert_eq!(split, vec!["\nMäry häd ", " little l", "mb\nLittle l", "mb\n"]);
19881981

1989-
let mut split: ~[&str] = data.rsplitn(|c: char| c == 'ä', 3).collect();
1982+
let mut split: Vec<&str> = data.rsplitn(|c: char| c == 'ä', 3).collect();
19901983
split.reverse();
1991-
assert_eq!(split, box ["\nMäry häd ", " little l", "mb\nLittle l", "mb\n"]);
1984+
assert_eq!(split, vec!["\nMäry häd ", " little l", "mb\nLittle l", "mb\n"]);
19921985
}
19931986

19941987
#[test]
19951988
fn test_split_char_iterator_no_trailing() {
19961989
let data = "\nMäry häd ä little lämb\nLittle lämb\n";
19971990

1998-
let split: ~[&str] = data.split('\n').collect();
1999-
assert_eq!(split, box ["", "Märy häd ä little lämb", "Little lämb", ""]);
1991+
let split: Vec<&str> = data.split('\n').collect();
1992+
assert_eq!(split, vec!["", "Märy häd ä little lämb", "Little lämb", ""]);
20001993

2001-
let split: ~[&str] = data.split_terminator('\n').collect();
2002-
assert_eq!(split, box ["", "Märy häd ä little lämb", "Little lämb"]);
1994+
let split: Vec<&str> = data.split_terminator('\n').collect();
1995+
assert_eq!(split, vec!["", "Märy häd ä little lämb", "Little lämb"]);
20031996
}
20041997

20051998
#[test]
20061999
fn test_rev_split_char_iterator_no_trailing() {
20072000
let data = "\nMäry häd ä little lämb\nLittle lämb\n";
20082001

2009-
let mut split: ~[&str] = data.split('\n').rev().collect();
2002+
let mut split: Vec<&str> = data.split('\n').rev().collect();
20102003
split.reverse();
2011-
assert_eq!(split, box ["", "Märy häd ä little lämb", "Little lämb", ""]);
2004+
assert_eq!(split, vec!["", "Märy häd ä little lämb", "Little lämb", ""]);
20122005

2013-
let mut split: ~[&str] = data.split_terminator('\n').rev().collect();
2006+
let mut split: Vec<&str> = data.split_terminator('\n').rev().collect();
20142007
split.reverse();
2015-
assert_eq!(split, box ["", "Märy häd ä little lämb", "Little lämb"]);
2008+
assert_eq!(split, vec!["", "Märy häd ä little lämb", "Little lämb"]);
20162009
}
20172010

20182011
#[test]
20192012
fn test_words() {
20202013
let data = "\n \tMäry häd\tä little lämb\nLittle lämb\n";
2021-
let words: ~[&str] = data.words().collect();
2022-
assert_eq!(words, box ["Märy", "häd", "ä", "little", "lämb", "Little", "lämb"])
2014+
let words: Vec<&str> = data.words().collect();
2015+
assert_eq!(words, vec!["Märy", "häd", "ä", "little", "lämb", "Little", "lämb"])
20232016
}
20242017

20252018
#[test]
@@ -2053,34 +2046,34 @@ mod tests {
20532046
#[test]
20542047
fn test_lines() {
20552048
let data = "\nMäry häd ä little lämb\n\nLittle lämb\n";
2056-
let lines: ~[&str] = data.lines().collect();
2057-
assert_eq!(lines, box ["", "Märy häd ä little lämb", "", "Little lämb"]);
2049+
let lines: Vec<&str> = data.lines().collect();
2050+
assert_eq!(lines, vec!["", "Märy häd ä little lämb", "", "Little lämb"]);
20582051

20592052
let data = "\nMäry häd ä little lämb\n\nLittle lämb"; // no trailing \n
2060-
let lines: ~[&str] = data.lines().collect();
2061-
assert_eq!(lines, box ["", "Märy häd ä little lämb", "", "Little lämb"]);
2053+
let lines: Vec<&str> = data.lines().collect();
2054+
assert_eq!(lines, vec!["", "Märy häd ä little lämb", "", "Little lämb"]);
20622055
}
20632056

20642057
#[test]
20652058
fn test_split_strator() {
2066-
fn t<'a>(s: &str, sep: &'a str, u: ~[&str]) {
2067-
let v: ~[&str] = s.split_str(sep).collect();
2068-
assert_eq!(v, u);
2059+
fn t(s: &str, sep: &str, u: &[&str]) {
2060+
let v: Vec<&str> = s.split_str(sep).collect();
2061+
assert_eq!(v.as_slice(), u.as_slice());
20692062
}
2070-
t("--1233345--", "12345", box ["--1233345--"]);
2071-
t("abc::hello::there", "::", box ["abc", "hello", "there"]);
2072-
t("::hello::there", "::", box ["", "hello", "there"]);
2073-
t("hello::there::", "::", box ["hello", "there", ""]);
2074-
t("::hello::there::", "::", box ["", "hello", "there", ""]);
2075-
t("ประเทศไทย中华Việt Nam", "中华", box ["ประเทศไทย", "Việt Nam"]);
2076-
t("zzXXXzzYYYzz", "zz", box ["", "XXX", "YYY", ""]);
2077-
t("zzXXXzYYYz", "XXX", box ["zz", "zYYYz"]);
2078-
t(".XXX.YYY.", ".", box ["", "XXX", "YYY", ""]);
2079-
t("", ".", box [""]);
2080-
t("zz", "zz", box ["",""]);
2081-
t("ok", "z", box ["ok"]);
2082-
t("zzz", "zz", box ["","z"]);
2083-
t("zzzzz", "zz", box ["","","z"]);
2063+
t("--1233345--", "12345", ["--1233345--"]);
2064+
t("abc::hello::there", "::", ["abc", "hello", "there"]);
2065+
t("::hello::there", "::", ["", "hello", "there"]);
2066+
t("hello::there::", "::", ["hello", "there", ""]);
2067+
t("::hello::there::", "::", ["", "hello", "there", ""]);
2068+
t("ประเทศไทย中华Việt Nam", "中华", ["ประเทศไทย", "Việt Nam"]);
2069+
t("zzXXXzzYYYzz", "zz", ["", "XXX", "YYY", ""]);
2070+
t("zzXXXzYYYz", "XXX", ["zz", "zYYYz"]);
2071+
t(".XXX.YYY.", ".", ["", "XXX", "YYY", ""]);
2072+
t("", ".", [""]);
2073+
t("zz", "zz", ["",""]);
2074+
t("ok", "z", ["ok"]);
2075+
t("zzz", "zz", ["","z"]);
2076+
t("zzzzz", "zz", ["","","z"]);
20842077
}
20852078

20862079
#[test]

0 commit comments

Comments
 (0)