Sha256: 34f7caf79316f4b03908832fdbd4aff367f2bc30eae291478cc5a0a108ce6e76

Contents?: true

Size: 1.19 KB

Versions: 15

Compression:

Stored size: 1.19 KB

Contents

// This module defines safe wrappers around memchr (POSIX) and memrchr (GNU
// extension).

#![allow(dead_code)]

use libc::{c_int, c_void, size_t};

pub fn memchr(needle: u8, haystack: &[u8]) -> Option<usize> {
    // SAFETY: This is safe to call since all pointers are valid.
    let p = unsafe {
        libc::memchr(
            haystack.as_ptr() as *const c_void,
            needle as c_int,
            haystack.len() as size_t,
        )
    };
    if p.is_null() {
        None
    } else {
        Some(p as usize - (haystack.as_ptr() as usize))
    }
}

// memrchr is a GNU extension. We know it's available on Linux at least.
#[cfg(target_os = "linux")]
pub fn memrchr(needle: u8, haystack: &[u8]) -> Option<usize> {
    // GNU's memrchr() will - unlike memchr() - error if haystack is empty.
    if haystack.is_empty() {
        return None;
    }
    // SAFETY: This is safe to call since all pointers are valid.
    let p = unsafe {
        libc::memrchr(
            haystack.as_ptr() as *const c_void,
            needle as c_int,
            haystack.len() as size_t,
        )
    };
    if p.is_null() {
        None
    } else {
        Some(p as usize - (haystack.as_ptr() as usize))
    }
}

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
wasmtime-13.0.0 ./ext/cargo-vendor/memchr-2.5.0/src/memchr/c.rs
wasmtime-12.0.1 ./ext/cargo-vendor/memchr-2.5.0/src/memchr/c.rs
wasmtime-12.0.0 ./ext/cargo-vendor/memchr-2.5.0/src/memchr/c.rs
wasmtime-11.0.0 ./ext/cargo-vendor/memchr-2.5.0/src/memchr/c.rs
wasmtime-10.0.1 ./ext/cargo-vendor/memchr-2.5.0/src/memchr/c.rs
wasmtime-10.0.0 ./ext/cargo-vendor/memchr-2.5.0/src/memchr/c.rs
wasmtime-9.0.4 ./ext/cargo-vendor/memchr-2.5.0/src/memchr/c.rs
wasmtime-9.0.1 ./ext/cargo-vendor/memchr-2.5.0/src/memchr/c.rs
wasmtime-8.0.0 ./ext/cargo-vendor/memchr-2.5.0/src/memchr/c.rs
wasmtime-7.0.0 ./ext/cargo-vendor/memchr-2.5.0/src/memchr/c.rs
wasmtime-6.0.1 ./ext/cargo-vendor/memchr-2.5.0/src/memchr/c.rs
wasmtime-6.0.0 ./ext/cargo-vendor/memchr-2.5.0/src/memchr/c.rs
wasmtime-5.0.0 ./ext/cargo-vendor/memchr-2.5.0/src/memchr/c.rs
wasmtime-0.4.1 ./ext/cargo-vendor/memchr-2.5.0/src/memchr/c.rs
wasmtime-0.4.0 ./ext/cargo-vendor/memchr-2.5.0/src/memchr/c.rs