Skip to content

Commit f66b4f0

Browse files
committed
---
yaml --- r: 15440 b: refs/heads/try c: 9fda157 h: refs/heads/master v: v3
1 parent e5931f0 commit f66b4f0

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
refs/heads/master: 61b1875c16de39c166b0f4d54bba19f9c6777d1a
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: 4a81779abd786ff22d71434c6d9a5917ea4cdfff
5-
refs/heads/try: d65df5d4d4a1d46434944482ae92bf1b162c6985
5+
refs/heads/try: 9fda1578a219a8762fadddfd37c45abdd6a271a1
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105

branches/try/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)