@@ -13,6 +13,7 @@ export eq, lteq, hash, is_empty, is_not_empty, is_whitespace, byte_len,
13
13
push_char, is_utf8, from_chars, to_chars, char_len, char_len_range,
14
14
char_at, bytes, is_ascii, shift_byte, pop_byte,
15
15
unsafe_from_byte, unsafe_from_bytes, from_char, char_range_at,
16
+ from_bytes,
16
17
from_cstr, sbuf, as_buf, push_byte, utf8_char_width, safe_slice,
17
18
contains, iter_chars, chars_iter, bytes_iter, words_iter, lines_iter,
18
19
loop_chars, loop_chars_sub, escape, any, all, map, windowed;
@@ -191,6 +192,19 @@ fn unsafe_from_bytes(v: [const u8]) -> str unsafe {
191
192
ret scopy;
192
193
}
193
194
195
+ /*
196
+ Function: from_bytes
197
+
198
+ Safely convert a vector of bytes to a UTF-8 string, or error
199
+ */
200
+ fn from_bytes ( vv : [ u8 ] ) -> result:: t < str , str > {
201
+ if is_utf8 ( vv) {
202
+ ret result:: ok ( unsafe_from_bytes ( vv) ) ;
203
+ } else {
204
+ ret result:: err ( "vector doesn't contain valid UTF-8" ) ;
205
+ }
206
+ }
207
+
194
208
/*
195
209
Function: unsafe_from_byte
196
210
@@ -1594,6 +1608,23 @@ mod tests {
1594
1608
assert ( b == "AAAAAAA" ) ;
1595
1609
}
1596
1610
1611
+ #[ test]
1612
+ fn test_from_bytes ( ) {
1613
+ let ss = "ศไทย中华Việt Nam" ;
1614
+ let bb = [ 0xe0_u8 , 0xb8_u8 , 0xa8_u8 ,
1615
+ 0xe0_u8 , 0xb9_u8 , 0x84_u8 ,
1616
+ 0xe0_u8 , 0xb8_u8 , 0x97_u8 ,
1617
+ 0xe0_u8 , 0xb8_u8 , 0xa2_u8 ,
1618
+ 0xe4_u8 , 0xb8_u8 , 0xad_u8 ,
1619
+ 0xe5_u8 , 0x8d_u8 , 0x8e_u8 ,
1620
+ 0x56_u8 , 0x69_u8 , 0xe1_u8 ,
1621
+ 0xbb_u8 , 0x87_u8 , 0x74_u8 ,
1622
+ 0x20_u8 , 0x4e_u8 , 0x61_u8 ,
1623
+ 0x6d_u8 ] ;
1624
+
1625
+ assert ss == result:: get ( from_bytes ( bb) ) ;
1626
+ }
1627
+
1597
1628
#[ test]
1598
1629
fn test_from_cstr ( ) unsafe {
1599
1630
let a = [ 65u8 , 65u8 , 65u8 , 65u8 , 65u8 , 65u8 , 65u8 , 0u8 ] ;
0 commit comments