Skip to content

Commit 070ac10

Browse files
committed
---
yaml --- r: 12522 b: refs/heads/master c: 75cf13e h: refs/heads/master v: v3
1 parent d8a3e42 commit 070ac10

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
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: 956bc773c684fe0f9e3ebc165cadb0b02d918805
2+
refs/heads/master: 75cf13ec7279ff7a56e3329ab6d6d8fc03e66e4f
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: 4a81779abd786ff22d71434c6d9a5917ea4cdfff
55
refs/heads/try: 2898dcc5d97da9427ac367542382b6239d9c0bbf

trunk/src/libcore/str.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1539,6 +1539,21 @@ fn as_c_str<T>(s: str, f: fn(*libc::c_char) -> T) -> T unsafe {
15391539
as_buf(s) {|buf| f(buf as *libc::c_char) }
15401540
}
15411541

1542+
1543+
#[doc = "
1544+
Work with the byte buffer and length of a slice.
1545+
1546+
The unpacked length is one byte longer than the 'official' indexable
1547+
length of the string. This is to permit probing the byte past the
1548+
indexable area for a null byte, as is the case in slices pointing
1549+
to full strings, or suffixes of them.
1550+
"]
1551+
fn unpack_slice<T>(s: str/&, f: fn(*u8, uint) -> T) -> T unsafe {
1552+
let v : *(*u8,uint) = ::unsafe::reinterpret_cast(ptr::addr_of(s));
1553+
let (buf,len) = *v;
1554+
f(buf, len)
1555+
}
1556+
15421557
#[doc = "
15431558
Reserves capacity for exactly `n` bytes in the given string, not including
15441559
the null terminator.
@@ -2706,4 +2721,16 @@ mod tests {
27062721
}
27072722
assert found_b;
27082723
}
2724+
2725+
#[test]
2726+
fn test_unpack_slice() unsafe {
2727+
let a = "hello";
2728+
unpack_slice(a) {|buf, len|
2729+
assert a[0] == 'h' as u8;
2730+
assert *buf == 'h' as u8;
2731+
assert len == 6u;
2732+
assert *ptr::offset(buf,4u) == 'o' as u8;
2733+
assert *ptr::offset(buf,5u) == 0u8;
2734+
}
2735+
}
27092736
}

0 commit comments

Comments
 (0)