2011-11-15 22:30:03 -06:00
|
|
|
use std;
|
|
|
|
|
2011-12-13 16:25:51 -08:00
|
|
|
import vec;
|
|
|
|
import str;
|
2011-11-15 22:30:03 -06:00
|
|
|
|
2011-11-16 22:49:38 -06:00
|
|
|
#[link_name = ""]
|
|
|
|
#[abi = "cdecl"]
|
|
|
|
native mod libc {
|
2011-11-14 21:06:39 +08:00
|
|
|
#[link_name = "strlen"]
|
|
|
|
fn my_strlen(str: *u8) -> uint;
|
2011-11-15 22:30:03 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn strlen(str: str) -> uint unsafe {
|
|
|
|
// C string is terminated with a zero
|
|
|
|
let bytes = str::bytes(str) + [0u8];
|
|
|
|
ret libc::my_strlen(vec::unsafe::to_ptr(bytes));
|
|
|
|
}
|
|
|
|
|
2011-11-15 10:09:28 -08:00
|
|
|
fn main() {
|
2011-11-15 22:30:03 -06:00
|
|
|
let len = strlen("Rust");
|
|
|
|
assert(len == 4u);
|
|
|
|
}
|