Skip to main content

luaur_ast/methods/
parser_copy_bytes.rs

1use crate::records::ast_array::AstArray;
2use crate::records::parser::Parser;
3use core::ffi::c_char;
4
5impl Parser {
6    pub fn copy_bytes(&mut self, data: &[u8]) -> AstArray<c_char> {
7        let len = data.len();
8        let mut result: AstArray<c_char> = AstArray {
9            data: core::ptr::null_mut(),
10            size: len,
11        };
12
13        unsafe {
14            let storage =
15                crate::records::allocator::Allocator::allocate(&mut *self.allocator, len + 1)
16                    as *mut c_char;
17
18            if len > 0 {
19                core::ptr::copy_nonoverlapping(data.as_ptr() as *const c_char, storage, len);
20            }
21            *storage.add(len) = 0;
22
23            result.data = storage;
24        }
25
26        result
27    }
28}