Skip to content

Commit 75cf13e

Browse files
committed
Add str/& component accessor and test.
1 parent 956bc77 commit 75cf13e

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

src/libcore/str.rs

+27
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)