Programming Language

Nocter

A self-contained systems language built around simplicity, encapsulation, and foolproof design.

/development/std/internal/path/darwin.nct

darwin.nct

//! Darwin UTF-8-to-C-path argument encoding.

see ./index.nct

use /mem.{RawBuffer, page_try_allocator}
use /path.validate
use /ptr.addr
use /internal/ptr.{copy_str_to_ptr, store_u8_to_ptr}

#target: "arm64-darwin"
struct TargetPathArgument {
    storage: RawBuffer
}

#target: "arm64-darwin"
func target_path_argument(value: &str): TargetPathArgument! {
    validate(value)?
    if value.len() == 18446744073709551615 {
        return error.new("std.io.invalid_input", "path length cannot be represented")
    }

    let allocation_size: usize = value.len() + 1
    var allocator = page_try_allocator()
    let storage = allocator.try_alloc(allocation_size, 1)?
    copy_str_to_ptr(storage.bytes().ptr(), 0, value)
    store_u8_to_ptr(storage.bytes().ptr(), allocation_size - 1, 0)
    return TargetPathArgument { storage: move storage }
}

#target: "arm64-darwin"
func target_path_address(value: &TargetPathArgument): usize {
    return addr(value.storage.bytes().ptr())
}