Skip to content

Commit 49ed4c2

Browse files
committed
Provide an implementation of strlen to be used as a fallback
1 parent 42a4b79 commit 49ed4c2

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#![cfg_attr(not(feature = "no-asm"), feature(global_asm))]
55
#![feature(cfg_target_has_atomic)]
66
#![feature(compiler_builtins)]
7+
#![feature(core_ffi_c)]
78
#![feature(core_intrinsics)]
89
#![feature(lang_items)]
910
#![feature(linkage)]

src/mem/mod.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ type c_int = i16;
88
#[cfg(not(target_pointer_width = "16"))]
99
type c_int = i32;
1010

11+
use core::ffi::c_char;
1112
use core::intrinsics::{atomic_load_unordered, atomic_store_unordered, exact_div};
1213
use core::mem;
1314
use core::ops::{BitOr, Shl};
@@ -68,6 +69,18 @@ intrinsics! {
6869
pub unsafe extern "C" fn bcmp(s1: *const u8, s2: *const u8, n: usize) -> i32 {
6970
memcmp(s1, s2, n)
7071
}
72+
73+
#[mem_builtin]
74+
#[cfg_attr(not(all(target_os = "windows", target_env = "gnu")), linkage = "weak")]
75+
pub unsafe extern "C" fn strlen(s: *const c_char) -> usize {
76+
let mut n = 0;
77+
let mut s = s;
78+
while *s != 0 {
79+
n += 1;
80+
s = s.offset(1);
81+
}
82+
n
83+
}
7184
}
7285

7386
// `bytes` must be a multiple of `mem::size_of::<T>()`

0 commit comments

Comments
 (0)