Skip to content

Commit 2496dcc

Browse files
committed
Changed type of str::from_bytes and added str::from_byte
1 parent 4d096a8 commit 2496dcc

File tree

1 file changed

+43
-18
lines changed

1 file changed

+43
-18
lines changed

src/libcore/str.rs

Lines changed: 43 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export
1313
// Creating a string
1414
from_bytes,
1515
unsafe_from_bytes,
16+
from_byte,
1617
unsafe_from_byte,
1718
//push_utf8_bytes,
1819
from_char,
@@ -117,14 +118,11 @@ Section: Creating a string
117118
/*
118119
Function: from_bytes
119120
120-
Safely convert a vector of bytes to a UTF-8 string, or error
121+
Convert a vector of bytes to a UTF-8 string. Fails if invalid UTF-8.
121122
*/
122-
fn from_bytes(vv: [u8]) -> result::t<str, str> {
123-
if is_utf8(vv) {
124-
ret result::ok(unsafe_from_bytes(vv));
125-
} else {
126-
ret result::err("vector doesn't contain valid UTF-8");
127-
}
123+
fn from_bytes(vv: [u8]) -> str {
124+
assert is_utf8(vv);
125+
ret unsafe_from_bytes(vv);
128126
}
129127

130128
/*
@@ -133,7 +131,7 @@ Function: unsafe_from_bytes
133131
Converts a vector of bytes to a string. Does not verify that the
134132
vector contains valid UTF-8.
135133
136-
// FIXME: remove?
134+
FIXME: don't export?
137135
*/
138136
fn unsafe_from_bytes(v: [const u8]) -> str unsafe {
139137
let vcopy: [u8] = v + [0u8];
@@ -152,6 +150,16 @@ FIXME: rename to 'from_byte'
152150
*/
153151
fn unsafe_from_byte(u: u8) -> str { unsafe_from_bytes([u]) }
154152

153+
154+
/*
155+
Function: from_byte
156+
157+
Convert a byte to a UTF-8 string. Fails if invalid UTF-8.
158+
*/
159+
fn from_byte(uu: u8) -> str {
160+
from_bytes([uu])
161+
}
162+
155163
fn push_utf8_bytes(&s: str, ch: char) {
156164
let code = ch as uint;
157165
let bytes =
@@ -526,7 +534,7 @@ fn split(s: str, sep: u8) -> [str] {
526534
v += [accum];
527535
accum = "";
528536
ends_with_sep = true;
529-
} else { accum += unsafe_from_byte(c); ends_with_sep = false; }
537+
} else { accum += from_byte(c); ends_with_sep = false; }
530538
}
531539
if byte_len(accum) != 0u || ends_with_sep { v += [accum]; }
532540
ret v;
@@ -554,7 +562,7 @@ fn splitn(s: str, sep: u8, count: uint) -> [str] {
554562
v += [accum];
555563
accum = "";
556564
ends_with_sep = true;
557-
} else { accum += unsafe_from_byte(c); ends_with_sep = false; }
565+
} else { accum += from_byte(c); ends_with_sep = false; }
558566
}
559567
if byte_len(accum) != 0u || ends_with_sep { v += [accum]; }
560568
ret v;
@@ -575,26 +583,26 @@ FIXME: should behave like split and split_char:
575583
*/
576584
fn split_str(s: str, sep: str) -> [str] {
577585
assert byte_len(sep) > 0u;
578-
let v: [str] = [], accum = "", sep_match = 0u, leading = true;
586+
let v: [str] = [], accum = [], sep_match = 0u, leading = true;
579587
for c: u8 in s {
580588
// Did we match the entire separator?
581589
if sep_match == byte_len(sep) {
582-
if !leading { v += [accum]; }
583-
accum = "";
590+
if !leading { vec::push(v, from_bytes(accum)); }
591+
accum = [];
584592
sep_match = 0u;
585593
}
586594

587595
if c == sep[sep_match] {
588596
sep_match += 1u;
589597
} else {
590598
sep_match = 0u;
591-
accum += unsafe_from_byte(c);
599+
vec::push(accum, c);
592600
leading = false;
593601
}
594602
}
595603

596-
if byte_len(accum) > 0u { v += [accum]; }
597-
if sep_match == byte_len(sep) { v += [""]; }
604+
if vec::len(accum) > 0u { vec::push(v, from_bytes(accum)); }
605+
if sep_match == byte_len(sep) { vec::push(v, ""); }
598606

599607
ret v;
600608
}
@@ -1783,7 +1791,24 @@ mod tests {
17831791
0x20_u8, 0x4e_u8, 0x61_u8,
17841792
0x6d_u8];
17851793

1786-
assert ss == result::get(from_bytes(bb));
1794+
assert ss == from_bytes(bb);
1795+
}
1796+
1797+
#[test]
1798+
#[should_fail]
1799+
fn test_from_bytes_fail() {
1800+
let bb = [0xff_u8, 0xb8_u8, 0xa8_u8,
1801+
0xe0_u8, 0xb9_u8, 0x84_u8,
1802+
0xe0_u8, 0xb8_u8, 0x97_u8,
1803+
0xe0_u8, 0xb8_u8, 0xa2_u8,
1804+
0xe4_u8, 0xb8_u8, 0xad_u8,
1805+
0xe5_u8, 0x8d_u8, 0x8e_u8,
1806+
0x56_u8, 0x69_u8, 0xe1_u8,
1807+
0xbb_u8, 0x87_u8, 0x74_u8,
1808+
0x20_u8, 0x4e_u8, 0x61_u8,
1809+
0x6d_u8];
1810+
1811+
let _x = from_bytes(bb);
17871812
}
17881813

17891814
#[test]
@@ -1821,7 +1846,7 @@ mod tests {
18211846
let s1: str = "All mimsy were the borogoves";
18221847

18231848
let v: [u8] = bytes(s1);
1824-
let s2: str = unsafe_from_bytes(v);
1849+
let s2: str = from_bytes(v);
18251850
let i: uint = 0u;
18261851
let n1: uint = byte_len(s1);
18271852
let n2: uint = vec::len::<u8>(v);

0 commit comments

Comments
 (0)