<commit_before>#![feature(libc)]

extern crate libc;

mod ffi;

use std::ffi::CString;

use ffi::nginx::{ngx_str_t, ngx_http_request_t, ngx_pool_t, size_t};


#[no_mangle]
pub unsafe extern fn memcpy(dest: *mut u8, src: *const u8,
                            n: usize) -> *mut u8 {
   let mut i = 0;
   while i < n {
      *dest.offset(i as isize) = *src.offset(i as isize);
      i += 1;
   }
   return dest;
}


#[no_mangle]
pub extern fn sample_text_from_rust(
   r: *const ngx_http_request_t,
   ngx_pcalloc: extern fn(pool: *mut ngx_pool_t, size: size_t) -> *mut ::libc::c_void
) -> ngx_str_t
{
   let s = CString::new("hellohihi").unwrap();

   let result = ngx_str_t {
      len: 9,
      data: unsafe { ngx_pcalloc((*r).pool, 10) as *mut u8 },
   };

   unsafe {
      memcpy(result.data, s.as_ptr() as *const u8, 9);
   }

   result
}<commit_msg>Using the libc::memcpy function instead of a custom one.<commit_after>#![feature(libc)]

extern crate libc;

mod ffi;

use std::ffi::CString;

use ffi::nginx::{ngx_str_t, ngx_http_request_t, ngx_pool_t};

use libc::{c_void, size_t};


extern {
   pub fn memcpy(dest: *mut c_void, src: *const c_void, n: size_t) -> *mut c_void;
}


#[no_mangle]
pub extern fn sample_text_from_rust(
   r: *const ngx_http_request_t,
   ngx_pcalloc: extern fn(pool: *mut ngx_pool_t, size: size_t) -> *mut c_void
) -> ngx_str_t
{
   let s = CString::new("hellohihi").unwrap();

   let result = ngx_str_t {
      len: 9,
      data: unsafe { ngx_pcalloc((*r).pool, 10) as *mut u8 },
   };

   unsafe {
      memcpy(result.data as *mut c_void, s.as_ptr() as *const c_void, 9);
   }

   result
}
{'fraction_non_alphanumeric': 0.14883148831488316, 'fraction_numerical': 0.00984009840098401, 'mean_word_length': 3.2591623036649215, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3453045', 'n_tokens_mistral': 701, 'n_tokens_neox': 680, 'n_words': 166}