Skip to content

Commit 6162ba5

Browse files
killerswanbrson
authored andcommitted
---
yaml --- r: 10965 b: refs/heads/master c: cccf9e5 h: refs/heads/master i: 10963: 55bcf6e v: v3
1 parent 5d6692f commit 6162ba5

File tree

2 files changed

+47
-4
lines changed

2 files changed

+47
-4
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: 5a19bafeeac4b22615dc183cab896af79c92a411
2+
refs/heads/master: cccf9e5389eaeb93566c83da266aa5df70be812c
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: 4a81779abd786ff22d71434c6d9a5917ea4cdfff
55
refs/heads/try: 2898dcc5d97da9427ac367542382b6239d9c0bbf

trunk/src/libcore/str.rs

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ Failure:
425425
If `begin` + `len` is is greater than the byte length of the string
426426
*/
427427
fn substr(s: str, begin: uint, len: uint) -> str {
428-
ret slice(s, begin, begin + len);
428+
ret unsafe::slice(s, begin, begin + len);
429429
}
430430

431431
/*
@@ -712,7 +712,7 @@ fn replace(s: str, from: str, to: str) : is_not_empty(from) -> str {
712712
if byte_len(s) == 0u {
713713
ret "";
714714
} else if starts_with(s, from) {
715-
ret to + replace(slice(s, byte_len(from), byte_len(s)), from, to);
715+
ret to + replace(unsafe::slice(s, byte_len(from), byte_len(s)), from, to);
716716
} else {
717717
let idx = find(s, from);
718718
if idx == -1 {
@@ -1346,7 +1346,9 @@ mod unsafe {
13461346
export
13471347
// UNSAFE
13481348
from_bytes,
1349-
from_byte;
1349+
from_byte,
1350+
slice,
1351+
safe_slice;
13501352

13511353
// Function: unsafe::from_bytes
13521354
//
@@ -1364,6 +1366,47 @@ mod unsafe {
13641366
// Converts a byte to a string. Does not verify that the byte is
13651367
// valid UTF-8.
13661368
unsafe fn from_byte(u: u8) -> str { unsafe::from_bytes([u]) }
1369+
1370+
/*
1371+
Function: slice
1372+
1373+
Takes a bytewise slice from a string. Returns the substring from
1374+
[`begin`..`end`).
1375+
1376+
This function is not unicode-safe.
1377+
1378+
Failure:
1379+
1380+
- If begin is greater than end.
1381+
- If end is greater than the length of the string.
1382+
1383+
FIXME: rename to byte_slice
1384+
*/
1385+
unsafe fn slice(s: str, begin: uint, end: uint) -> str unsafe {
1386+
// FIXME: Typestate precondition
1387+
assert (begin <= end);
1388+
assert (end <= byte_len(s));
1389+
1390+
let v: [u8] = ::unsafe::reinterpret_cast(s);
1391+
let v2 = vec::slice(v, begin, end);
1392+
::unsafe::leak(v);
1393+
v2 += [0u8];
1394+
let s2: str = ::unsafe::reinterpret_cast(v2);
1395+
::unsafe::leak(v2);
1396+
ret s2;
1397+
}
1398+
1399+
/*
1400+
Function: safe_slice
1401+
1402+
FIXME: rename to safe_range_byte_slice
1403+
*/
1404+
unsafe fn safe_slice(s: str, begin: uint, end: uint) : uint::le(begin, end) -> str {
1405+
// would need some magic to make this a precondition
1406+
assert (end <= byte_len(s));
1407+
ret slice(s, begin, end);
1408+
}
1409+
13671410
}
13681411

13691412

0 commit comments

Comments
 (0)