@@ -12,9 +12,7 @@ For some heavy-duty uses, we recommend trying std::rope.
12
12
export
13
13
// Creating a string
14
14
from_bytes,
15
- unsafe_from_bytes,
16
15
from_byte,
17
- unsafe_from_byte,
18
16
//push_utf8_bytes,
19
17
from_char,
20
18
from_chars,
@@ -120,37 +118,11 @@ Function: from_bytes
120
118
121
119
Convert a vector of bytes to a UTF-8 string. Fails if invalid UTF-8.
122
120
*/
123
- fn from_bytes ( vv : [ u8 ] ) -> str {
121
+ fn from_bytes ( vv : [ u8 ] ) -> str unsafe {
124
122
assert is_utf8 ( vv) ;
125
- ret unsafe_from_bytes ( vv) ;
123
+ ret unsafe :: from_bytes ( vv) ;
126
124
}
127
125
128
- /*
129
- Function: unsafe_from_bytes
130
-
131
- Converts a vector of bytes to a string. Does not verify that the
132
- vector contains valid UTF-8.
133
-
134
- FIXME: stop exporting
135
- */
136
- fn unsafe_from_bytes ( v : [ const u8 ] ) -> str unsafe {
137
- let vcopy: [ u8 ] = v + [ 0u8 ] ;
138
- let scopy: str = unsafe :: reinterpret_cast ( vcopy) ;
139
- unsafe :: leak ( vcopy) ;
140
- ret scopy;
141
- }
142
-
143
- /*
144
- Function: unsafe_from_byte
145
-
146
- Converts a byte to a string. Does not verify that the byte is
147
- valid UTF-8.
148
-
149
- FIXME: stop exporting
150
- */
151
- fn unsafe_from_byte ( u : u8 ) -> str { unsafe_from_bytes ( [ u] ) }
152
-
153
-
154
126
/*
155
127
Function: from_byte
156
128
@@ -211,6 +183,7 @@ fn from_chars(chs: [char]) -> str {
211
183
ret buf;
212
184
}
213
185
186
+ // FIXME: not unsafe now
214
187
/*
215
188
Function: from_cstr
216
189
@@ -413,9 +386,9 @@ Converts a string to a vector of bytes. The result vector is not
413
386
null-terminated.
414
387
*/
415
388
fn bytes ( s : str ) -> [ u8 ] unsafe {
416
- let v = unsafe :: reinterpret_cast ( s) ;
389
+ let v = :: unsafe:: reinterpret_cast ( s) ;
417
390
let vcopy = vec:: slice ( v, 0 u, vec:: len ( v) - 1 u) ;
418
- unsafe :: leak ( v) ;
391
+ :: unsafe:: leak ( v) ;
419
392
ret vcopy;
420
393
}
421
394
@@ -492,12 +465,12 @@ fn slice(s: str, begin: uint, end: uint) -> str unsafe {
492
465
assert ( begin <= end) ;
493
466
assert ( end <= byte_len ( s) ) ;
494
467
495
- let v: [ u8 ] = unsafe :: reinterpret_cast ( s) ;
468
+ let v: [ u8 ] = :: unsafe:: reinterpret_cast ( s) ;
496
469
let v2 = vec:: slice ( v, begin, end) ;
497
- unsafe :: leak ( v) ;
470
+ :: unsafe:: leak ( v) ;
498
471
v2 += [ 0u8 ] ;
499
- let s2: str = unsafe :: reinterpret_cast ( v2) ;
500
- unsafe :: leak ( v2) ;
472
+ let s2: str = :: unsafe:: reinterpret_cast ( v2) ;
473
+ :: unsafe:: leak ( v2) ;
501
474
ret s2;
502
475
}
503
476
@@ -1093,9 +1066,9 @@ Returns the length in bytes of a string
1093
1066
FIXME: rename to 'len_bytes'?
1094
1067
*/
1095
1068
pure fn byte_len ( s : str ) -> uint unsafe {
1096
- let v: [ u8 ] = unsafe :: reinterpret_cast ( s) ;
1069
+ let v: [ u8 ] = :: unsafe:: reinterpret_cast ( s) ;
1097
1070
let vlen = vec:: len ( v) ;
1098
- unsafe :: leak ( v) ;
1071
+ :: unsafe:: leak ( v) ;
1099
1072
// There should always be a null terminator
1100
1073
assert ( vlen > 0 u) ;
1101
1074
ret vlen - 1 u;
@@ -1371,7 +1344,7 @@ const tag_six_b: uint = 252u;
1371
1344
// no guarantee that the string is rooted). Instead, use as_buf below.
1372
1345
unsafe fn buf ( s : str ) -> sbuf {
1373
1346
let saddr = ptr:: addr_of ( s) ;
1374
- let vaddr: * [ u8 ] = unsafe :: reinterpret_cast ( saddr) ;
1347
+ let vaddr: * [ u8 ] = :: unsafe:: reinterpret_cast ( saddr) ;
1375
1348
let buf = vec:: to_ptr ( * vaddr) ;
1376
1349
ret buf;
1377
1350
}
@@ -1398,6 +1371,38 @@ An unsafe buffer of bytes. Corresponds to a C char pointer.
1398
1371
*/
1399
1372
type sbuf = * u8 ;
1400
1373
1374
+ mod unsafe {
1375
+ export
1376
+ // UNSAFE
1377
+ from_bytes,
1378
+ from_byte;
1379
+
1380
+ /*
1381
+ Function: unsafe::from_bytes
1382
+
1383
+ Converts a vector of bytes to a string. Does not verify that the
1384
+ vector contains valid UTF-8.
1385
+
1386
+ FIXME: stop exporting
1387
+ */
1388
+ unsafe fn from_bytes ( v : [ const u8 ] ) -> str unsafe {
1389
+ let vcopy: [ u8 ] = v + [ 0u8 ] ;
1390
+ let scopy: str = :: unsafe:: reinterpret_cast ( vcopy) ;
1391
+ :: unsafe:: leak ( vcopy) ;
1392
+ ret scopy;
1393
+ }
1394
+
1395
+ /*
1396
+ Function: unsafe::from_byte
1397
+
1398
+ Converts a byte to a string. Does not verify that the byte is
1399
+ valid UTF-8.
1400
+
1401
+ FIXME: stop exporting
1402
+ */
1403
+ unsafe fn from_byte ( u : u8 ) -> str { unsafe :: from_bytes ( [ u] ) }
1404
+ }
1405
+
1401
1406
1402
1407
#[ cfg( test) ]
1403
1408
mod tests {
@@ -1771,9 +1776,9 @@ mod tests {
1771
1776
}
1772
1777
1773
1778
#[ test]
1774
- fn test_unsafe_from_bytes ( ) {
1779
+ fn test_unsafe_from_bytes ( ) unsafe {
1775
1780
let a = [ 65u8 , 65u8 , 65u8 , 65u8 , 65u8 , 65u8 , 65u8 ] ;
1776
- let b = unsafe_from_bytes ( a) ;
1781
+ let b = unsafe :: from_bytes ( a) ;
1777
1782
assert ( b == "AAAAAAA" ) ;
1778
1783
}
1779
1784
0 commit comments