@@ -9,8 +9,6 @@ but UTF-8 unsafe operations should be avoided.
9
9
For some heavy-duty uses, try std::rope.
10
10
*/
11
11
12
- import option:: { some, none} ;
13
-
14
12
export
15
13
// Creating a string
16
14
from_bytes,
@@ -276,10 +274,10 @@ Remove the final character from a string and return it.
276
274
Failure:
277
275
If the string does not contain any characters.
278
276
*/
279
- fn pop_char ( & s: str ) -> char unsafe {
277
+ fn pop_char ( & s: str ) -> char {
280
278
let end = len ( s) ;
281
- let { ch: ch , prev : end } = char_range_at_reverse ( s, end) ;
282
- s = unsafe :: slice_bytes ( s, 0 u , end ) ;
279
+ let { ch, prev} = char_range_at_reverse ( s, end) ;
280
+ unsafe { unsafe :: set_len ( s, prev ) ; }
283
281
ret ch;
284
282
}
285
283
@@ -1125,12 +1123,8 @@ fn is_whitespace(s: str) -> bool {
1125
1123
// Returns the string length/size in bytes
1126
1124
// not counting the null terminator
1127
1125
pure fn len ( s : str ) -> uint unsafe {
1128
- as_bytes ( s) { |v|
1129
- let vlen = vec:: len ( v) ;
1130
- // There should always be a null terminator
1131
- assert ( vlen > 0 u) ;
1132
- vlen - 1 u
1133
- }
1126
+ let repr: * vec:: unsafe:: vec_repr = :: unsafe:: reinterpret_cast ( s) ;
1127
+ ( * repr) . fill - 1 u
1134
1128
}
1135
1129
1136
1130
// FIXME: delete?
@@ -1466,7 +1460,8 @@ mod unsafe {
1466
1460
push_byte,
1467
1461
push_bytes,
1468
1462
pop_byte,
1469
- shift_byte;
1463
+ shift_byte,
1464
+ set_len;
1470
1465
1471
1466
// Function: unsafe::from_bytes
1472
1467
//
@@ -1540,7 +1535,7 @@ mod unsafe {
1540
1535
let len = len ( s) ;
1541
1536
assert ( len > 0 u) ;
1542
1537
let b = s[ len - 1 u] ;
1543
- s = unsafe :: slice_bytes ( s , 0 u , len - 1 u) ;
1538
+ set_len ( s , len - 1 u) ;
1544
1539
ret b;
1545
1540
}
1546
1541
@@ -1554,7 +1549,13 @@ mod unsafe {
1554
1549
s = unsafe :: slice_bytes ( s, 1 u, len) ;
1555
1550
ret b;
1556
1551
}
1557
-
1552
+
1553
+ unsafe fn set_len ( & v: str , new_len : uint ) {
1554
+ let repr: * vec:: unsafe:: vec_repr = :: unsafe:: reinterpret_cast ( v) ;
1555
+ ( * repr) . fill = new_len + 1 u;
1556
+ let null = ptr:: mut_offset ( ptr:: mut_addr_of ( ( * repr) . data ) , new_len) ;
1557
+ * null = 0u8 ;
1558
+ }
1558
1559
}
1559
1560
1560
1561
0 commit comments