Skip to content

Commit 34413c3

Browse files
committed
---
yaml --- r: 11446 b: refs/heads/master c: 35e9192 h: refs/heads/master v: v3
1 parent 4e2cde5 commit 34413c3

File tree

2 files changed

+16
-15
lines changed

2 files changed

+16
-15
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
refs/heads/master: c2984b46b4c3b8b91d877ebc85efcc9f783c53d5
2+
refs/heads/master: 35e9192762f30ec893e4e72f49abffe7f77c4615
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: 4a81779abd786ff22d71434c6d9a5917ea4cdfff
55
refs/heads/try: 2898dcc5d97da9427ac367542382b6239d9c0bbf

trunk/src/libcore/str.rs

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ but UTF-8 unsafe operations should be avoided.
99
For some heavy-duty uses, try std::rope.
1010
*/
1111

12-
import option::{some, none};
13-
1412
export
1513
// Creating a string
1614
from_bytes,
@@ -276,10 +274,10 @@ Remove the final character from a string and return it.
276274
Failure:
277275
If the string does not contain any characters.
278276
*/
279-
fn pop_char(&s: str) -> char unsafe {
277+
fn pop_char(&s: str) -> char {
280278
let end = len(s);
281-
let {ch:ch, prev:end} = char_range_at_reverse(s, end);
282-
s = unsafe::slice_bytes(s, 0u, end);
279+
let {ch, prev} = char_range_at_reverse(s, end);
280+
unsafe { unsafe::set_len(s, prev); }
283281
ret ch;
284282
}
285283

@@ -1125,12 +1123,8 @@ fn is_whitespace(s: str) -> bool {
11251123
// Returns the string length/size in bytes
11261124
// not counting the null terminator
11271125
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 > 0u);
1132-
vlen - 1u
1133-
}
1126+
let repr: *vec::unsafe::vec_repr = ::unsafe::reinterpret_cast(s);
1127+
(*repr).fill - 1u
11341128
}
11351129

11361130
// FIXME: delete?
@@ -1466,7 +1460,8 @@ mod unsafe {
14661460
push_byte,
14671461
push_bytes,
14681462
pop_byte,
1469-
shift_byte;
1463+
shift_byte,
1464+
set_len;
14701465

14711466
// Function: unsafe::from_bytes
14721467
//
@@ -1540,7 +1535,7 @@ mod unsafe {
15401535
let len = len(s);
15411536
assert (len > 0u);
15421537
let b = s[len - 1u];
1543-
s = unsafe::slice_bytes(s, 0u, len - 1u);
1538+
set_len(s, len - 1u);
15441539
ret b;
15451540
}
15461541

@@ -1554,7 +1549,13 @@ mod unsafe {
15541549
s = unsafe::slice_bytes(s, 1u, len);
15551550
ret b;
15561551
}
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 + 1u;
1556+
let null = ptr::mut_offset(ptr::mut_addr_of((*repr).data), new_len);
1557+
*null = 0u8;
1558+
}
15581559
}
15591560

15601561

0 commit comments

Comments
 (0)