c

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 5, 2026 License: BSD-3-Clause Imports: 1 Imported by: 0

Documentation

Overview

Package c provides convenience helpers for C interop. It bridges C's null-terminated strings and raw pointers with So's string and slice types.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Alignof

func Alignof[T any]() int

Alignof returns the alignment of type T in bytes.

alignof(T)

func Alloca

func Alloca[T any](n int) *T

Alloca allocates an array of the given length on the stack and returns a pointer to it.

func Assert

func Assert(cond bool, msg string)

Assert aborts the program with the given message if the condition is not true. If assertions are disabled, does nothing.

assert((cond) && msg)

func Bytes

func Bytes(ptr *byte, n int) []byte

Bytes wraps a raw byte pointer and length into a []byte without copying. If ptr is nil, returns nil.

(so_Slice){ptr, n, n}

func PtrAdd

func PtrAdd[T any](ptr *T, offset int) *T

PtrAdd adds offset bytes to a pointer and returns the result.

ptr + offset

func PtrAs

func PtrAs[T any](ptr any) *T

PtrAs casts a raw pointer (void*) to *T.

(T*)(ptr)

func PtrAt

func PtrAt[T any](ptr *T, index int) *T

PtrAt returns a pointer to the element at the given index in an array or slice.

&ptr[index]

func Raw

func Raw(code string)

Raw emits a raw block of C code.

func Sizeof

func Sizeof[T any]() int

Sizeof returns the size of type T in bytes.

sizeof(T)

func Slice

func Slice[T any](ptr *T, len int, cap int) []T

Slice wraps a raw pointer and length into a []T without copying. If ptr is nil, returns nil.

(so_Slice){ptr, len, cap}

func String

func String[T Char | ConstChar](ptr *T) string

String converts a null-terminated C string to a So string. If ptr is nil, returns "".

(so_String){s, strlen(s)}

func Val

func Val[T any](expr string) T

Val emits a typed C expression.

func Zero

func Zero[T any]() T

Zero returns the zero value of type T.

{0}

Types

type Char

type Char byte

Char represents a C char type.

func CString

func CString(s string) *Char

CString converts a So string to a null-terminated C string. Allocates memory on the stack using alloca until the calling function returns.

type ConstChar

type ConstChar byte

ConstChar represents a C char type with a const modifier.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL