13
13
// Creating a string
14
14
from_bytes,
15
15
unsafe_from_bytes,
16
+ from_byte,
16
17
unsafe_from_byte,
17
18
//push_utf8_bytes,
18
19
from_char,
@@ -117,14 +118,11 @@ Section: Creating a string
117
118
/*
118
119
Function: from_bytes
119
120
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.
121
122
*/
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) ;
128
126
}
129
127
130
128
/*
@@ -133,7 +131,7 @@ Function: unsafe_from_bytes
133
131
Converts a vector of bytes to a string. Does not verify that the
134
132
vector contains valid UTF-8.
135
133
136
- // FIXME: remove ?
134
+ FIXME: don't export ?
137
135
*/
138
136
fn unsafe_from_bytes ( v : [ const u8 ] ) -> str unsafe {
139
137
let vcopy: [ u8 ] = v + [ 0u8 ] ;
@@ -152,6 +150,16 @@ FIXME: rename to 'from_byte'
152
150
*/
153
151
fn unsafe_from_byte ( u : u8 ) -> str { unsafe_from_bytes ( [ u] ) }
154
152
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
+
155
163
fn push_utf8_bytes ( & s: str , ch : char ) {
156
164
let code = ch as uint ;
157
165
let bytes =
@@ -526,7 +534,7 @@ fn split(s: str, sep: u8) -> [str] {
526
534
v += [ accum] ;
527
535
accum = "" ;
528
536
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 ; }
530
538
}
531
539
if byte_len ( accum) != 0 u || ends_with_sep { v += [ accum] ; }
532
540
ret v;
@@ -554,7 +562,7 @@ fn splitn(s: str, sep: u8, count: uint) -> [str] {
554
562
v += [ accum] ;
555
563
accum = "" ;
556
564
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 ; }
558
566
}
559
567
if byte_len ( accum) != 0 u || ends_with_sep { v += [ accum] ; }
560
568
ret v;
@@ -575,26 +583,26 @@ FIXME: should behave like split and split_char:
575
583
*/
576
584
fn split_str ( s : str , sep : str ) -> [ str ] {
577
585
assert byte_len ( sep) > 0 u;
578
- let v: [ str ] = [ ] , accum = "" , sep_match = 0 u, leading = true ;
586
+ let v: [ str ] = [ ] , accum = [ ] , sep_match = 0 u, leading = true ;
579
587
for c: u8 in s {
580
588
// Did we match the entire separator?
581
589
if sep_match == byte_len ( sep) {
582
- if !leading { v += [ accum] ; }
583
- accum = "" ;
590
+ if !leading { vec :: push ( v , from_bytes ( accum) ) ; }
591
+ accum = [ ] ;
584
592
sep_match = 0 u;
585
593
}
586
594
587
595
if c == sep[ sep_match] {
588
596
sep_match += 1 u;
589
597
} else {
590
598
sep_match = 0 u;
591
- accum += unsafe_from_byte ( c) ;
599
+ vec :: push ( accum, c) ;
592
600
leading = false ;
593
601
}
594
602
}
595
603
596
- if byte_len ( accum) > 0 u { v += [ accum] ; }
597
- if sep_match == byte_len ( sep) { v += [ "" ] ; }
604
+ if vec :: len ( accum) > 0 u { vec :: push ( v , from_bytes ( accum) ) ; }
605
+ if sep_match == byte_len ( sep) { vec :: push ( v , "" ) ; }
598
606
599
607
ret v;
600
608
}
@@ -1783,7 +1791,24 @@ mod tests {
1783
1791
0x20_u8 , 0x4e_u8 , 0x61_u8 ,
1784
1792
0x6d_u8 ] ;
1785
1793
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) ;
1787
1812
}
1788
1813
1789
1814
#[ test]
@@ -1821,7 +1846,7 @@ mod tests {
1821
1846
let s1: str = "All mimsy were the borogoves" ;
1822
1847
1823
1848
let v: [ u8 ] = bytes ( s1) ;
1824
- let s2: str = unsafe_from_bytes ( v) ;
1849
+ let s2: str = from_bytes ( v) ;
1825
1850
let i: uint = 0 u;
1826
1851
let n1: uint = byte_len ( s1) ;
1827
1852
let n2: uint = vec:: len :: < u8 > ( v) ;
0 commit comments