Skip to content

Commit 9fda157

Browse files
committed
Add ptr::position and ptr::buf_len, close #2183.
1 parent d65df5d commit 9fda157

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

src/libcore/ptr.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,21 @@ fn mut_offset<T>(ptr: *mut T, count: uint) -> *mut T {
4848
(ptr as uint + count * sys::size_of::<T>()) as *mut T
4949
}
5050

51+
#[doc = "Return the offset of the first null pointer in `buf`."]
52+
#[inline(always)]
53+
unsafe fn buf_len<T>(buf: **T) -> uint {
54+
position(buf) {|i| i == null() }
55+
}
56+
57+
#[doc = "Return the first offset `i` such that `f(buf[i]) == true`."]
58+
#[inline(always)]
59+
unsafe fn position<T>(buf: *T, f: fn(T) -> bool) -> uint {
60+
let mut i = 0u;
61+
loop {
62+
if f(*offset(buf, i)) { ret i; }
63+
else { i += 1u; }
64+
}
65+
}
5166

5267
#[doc = "Create an unsafe null pointer"]
5368
#[inline(always)]
@@ -111,4 +126,32 @@ fn test() unsafe {
111126
ptr::memcpy(ptr::offset(vec::unsafe::to_ptr(v1), 2u),
112127
vec::unsafe::to_ptr(v0), 1u);
113128
assert (v1[0] == 32002u16 && v1[1] == 32001u16 && v1[2] == 32000u16);
129+
}
130+
131+
#[test]
132+
fn test_position() unsafe {
133+
import str::as_c_str;
134+
import libc::c_char;
135+
136+
let s = "hello";
137+
assert 2u == as_c_str(s) {|p| position(p) {|c| c == 'l' as c_char} };
138+
assert 4u == as_c_str(s) {|p| position(p) {|c| c == 'o' as c_char} };
139+
assert 5u == as_c_str(s) {|p| position(p) {|c| c == 0 as c_char } };
140+
}
141+
142+
#[test]
143+
fn test_buf_len() unsafe {
144+
let s0 = "hello";
145+
let s1 = "there";
146+
let s2 = "thing";
147+
str::as_c_str(s0) {|p0|
148+
str::as_c_str(s1) {|p1|
149+
str::as_c_str(s2) {|p2|
150+
let v = [p0, p1, p2, null()];
151+
vec::as_buf(v) {|vp|
152+
assert buf_len(vp) == 3u;
153+
}
154+
}
155+
}
156+
}
114157
}

0 commit comments

Comments
 (0)