Documentation ¶
Index ¶
- func AddAssign[T number](p *T, v T) T
- func AndAssign[T constraints.Integer](p *T, v T) T
- func Assert(cond bool)
- func Assign[T any](p *T, v T) T
- func BoolToInt(v bool) int
- func CString(s string) *byte
- func CString16(s string) *uint16
- func CStringS(s string) []byte
- func CStringS16(s string) []uint16
- func Calloc(cnt, sz uintptr) unsafe.Pointer
- func Clone[T any](p *T) *T
- func CloneS[T any](p []T) []T
- func DivAssign[T number](p *T, v T) T
- func Free[T any](p *T)
- func FreeP(p unsafe.Pointer)
- func FreeS[T any](p []T)
- func GoString(p *byte) stringdeprecated
- func GoString16(p *uint16) stringdeprecated
- func GoStringS(p []byte) string
- func GoStringS16(p []uint16) string
- func If[T any](cond bool, then, els T) T
- func IfFunc[T any](cond bool, then, els func() T) T
- func IndexS[T comparable](s []T, v T) int
- func LastIndexS[T comparable](s []T, v T) int
- func LshAssign[T constraints.Integer](p *T, v T) T
- func Make[T any](sz int) []T
- func Malloc(sz uintptr) unsafe.Pointer
- func MemChar[T comparable](p *T, v T, sz int) *T
- func MemCharP(p unsafe.Pointer, v byte, sz int) unsafe.Pointer
- func MemCmp[T comparable](p1, p2 *T, sz int) int
- func MemCmpP(p1, p2 unsafe.Pointer, sz int) int
- func MemCmpS[T comparable](p1, p2 []T) int
- func MemCopy[T any](dst, src *T, sz int)
- func MemCopyP(dst, src unsafe.Pointer, sz int)
- func MemIndex[T comparable](p *T, v T, sz int) int
- func MemLastIndex[T comparable](p *T, v T, sz int) int
- func MemRChar[T comparable](p *T, v T, sz int) *T
- func MemRCharP(p unsafe.Pointer, v byte, sz int) unsafe.Pointer
- func MemSet[T any](p *T, v T, sz int)
- func MemSetS[T any](dst []T, v T)
- func ModAssign[T constraints.Integer](p *T, v T) T
- func MulAssign[T number](p *T, v T) T
- func New[T any]() *T
- func OrAssign[T constraints.Integer](p *T, v T) T
- func PostDec[T number](p *T) T
- func PostInc[T number](p *T) T
- func PreDec[T number](p *T) T
- func PreInc[T number](p *T) T
- func PtrAdd[T any](p *T, off int) *T
- func Realloc[T any](p *T, sz int) *T
- func ReallocP(p unsafe.Pointer, cnt, sz uintptr) unsafe.Pointer
- func Remake[T any](p []T, sz int) []T
- func RshAssign[T constraints.Integer](p *T, v T) T
- func StrCaseCmp(p1, p2 *byte) intdeprecated
- func StrCaseCmp16(p1, p2 *uint16) intdeprecated
- func StrCaseCmpN(p1, p2 *byte, max int) int
- func StrCaseCmpN16(p1, p2 *uint16, max int) int
- func StrCaseCmpS(p1, p2 []byte) int
- func StrCaseCmpS16(p1, p2 []uint16) int
- func StrCat[T comparable](dst *T, src *T) *T
- func StrCatS[T comparable](dst []T, src []T) []T
- func StrCatStr(dst *byte, src string) *byte
- func StrCatStrS(dst []byte, src string) []byte
- func StrChar[T comparable](p *T, v T) *Tdeprecated
- func StrCharS[T comparable](p []T, v T) []T
- func StrCmp[T constraints.Ordered](p1, p2 *T) intdeprecated
- func StrCmpN[T constraints.Ordered](p1, p2 *T, max int) int
- func StrCmpS[T constraints.Ordered](p1, p2 []T) int
- func StrCopy[T comparable](p *T, s *T)deprecated
- func StrCopyS[T comparable](p []T, s []T)
- func StrCopySlice[T comparable](p *T, s []T)deprecated
- func StrCopyStr(p *byte, s string)deprecated
- func StrCopyStr16(p *uint16, s string)deprecated
- func StrCopyStr16S(p []uint16, s string)
- func StrCopyStrS(p []byte, s string)
- func StrDup[T comparable](s *T) *Tdeprecated
- func StrDupS[T comparable](s []T) []T
- func StrIndex[T comparable](p *T, v T) intdeprecated
- func StrIndexS[T comparable](p []T, v T) int
- func StrLastIndex[T comparable](p *T, v T) intdeprecated
- func StrLastIndexS[T comparable](p []T, v T) int
- func StrLen[T comparable](p *T) intdeprecated
- func StrLenS[T comparable](p []T) int
- func StrNCopy[T comparable](p *T, s *T, sz int)deprecated
- func StrNCopyS[T comparable](p []T, s []T)
- func StrNCopySlice[T comparable](p *T, s []T, sz int)deprecated
- func StrNCopyStr(p *byte, s string, sz int)deprecated
- func StrNCopyStr16(p *uint16, s string, sz int)deprecated
- func StrNCopyStr16S(p []uint16, s string)
- func StrNCopyStrS(p []byte, s string)
- func StrNLen[T comparable](p *T, max int) int
- func StrNSlice[T comparable](p *T, max int) []T
- func StrRChar[T comparable](p *T, v T) *Tdeprecated
- func StrRCharS[T comparable](p []T, v T) []T
- func StrSlice[T comparable](p *T) []Tdeprecated
- func StrSliceS[T comparable](p []T) []T
- func StrSliceStr(s string) string
- func SubAssign[T number](p *T, v T) T
- func XorAssign[T constraints.Integer](p *T, v T) T
- type Allocator
- type GoAllocator
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AddAssign ¶
func AddAssign[T number](p *T, v T) T
AddAssign is similar to x += v expression in C, but accepts a pointer to a variable.
func AndAssign ¶
func AndAssign[T constraints.Integer](p *T, v T) T
AndAssign is similar to x &= v expression in C, but accepts a pointer to a variable.
func Assign ¶
func Assign[T any](p *T, v T) T
Assign is similar to x = v expression in C, but accepts a pointer to a variable.
func CString ¶
CString is similar to C.CString, but uses the allocator provided by this package.
Caller must free the string with Free.
func CStringS ¶
CStringS is similar to C.CString, but uses the allocator provided by this package. It returns slice.
Caller must free the string with Free.
func CStringS16 ¶
CStringS16 is similar to CString, but produces a UTF-16 string. It returns slice.
func Clone ¶
func Clone[T any](p *T) *T
Clone the object, using memory provided by the allocator. Caller must free the result with Free.
func CloneS ¶
func CloneS[T any](p []T) []T
CloneS the slice, using memory provided by the allocator. Caller must free the result with FreeS.
func DivAssign ¶
func DivAssign[T number](p *T, v T) T
DivAssign is similar to x /= v expression in C, but accepts a pointer to a variable.
func GoString16
deprecated
func GoStringS16 ¶
GoStringS16 is similar to GoString, but operates on UTF-16 string. It accepts slice.
func If ¶
If is similar to a ternary operator in C. It ALWAYS evaluates side effects. For correctly handling side effects, use IfFunc.
func IfFunc ¶
IfFunc is similar to a ternary operator in C. It preserves C semantic in terms of expression evaluation.
func IndexS ¶
func IndexS[T comparable](s []T, v T) int
IndexS returns first index of value v in slice s. It returns -1 if v is not in s.
func LastIndexS ¶
func LastIndexS[T comparable](s []T, v T) int
LastIndexS returns last index of value v in slice s. It returns -1 if v is not in s.
func LshAssign ¶
func LshAssign[T constraints.Integer](p *T, v T) T
LshAssign is similar to x <<= v expression in C, but accepts a pointer to a variable.
func Make ¶
Make is similar to builtin make, but uses the allocator. Caller must free the result with FreeS.
func Malloc ¶
Malloc allocates memory using the allocator. It is similar malloc in C, but always zero-initializes memory.
func MemChar ¶
func MemChar[T comparable](p *T, v T, sz int) *T
MemChar is a generic implementation of C memchr.
func MemCmp ¶
func MemCmp[T comparable](p1, p2 *T, sz int) int
MemCmp is a generic version of C memcmp.
func MemCmpS ¶
func MemCmpS[T comparable](p1, p2 []T) int
MemCmpS is similar to MemCmp, but accepts slices.
func MemIndex ¶
func MemIndex[T comparable](p *T, v T, sz int) int
MemIndex is similar to IndexS, but accepts a pointer and a size.
func MemLastIndex ¶
func MemLastIndex[T comparable](p *T, v T, sz int) int
MemLastIndex is similar to LastIndexS, but accepts a pointer and a size.
func MemRChar ¶
func MemRChar[T comparable](p *T, v T, sz int) *T
MemRChar is a generic implementation of C memchr, but searches the value from the end.
func MemSetS ¶
func MemSetS[T any](dst []T, v T)
MemSetS is a generic implementation of C memset that accepts a slice.
func ModAssign ¶
func ModAssign[T constraints.Integer](p *T, v T) T
ModAssign is similar to x %= v expression in C, but accepts a pointer to a variable.
func MulAssign ¶
func MulAssign[T number](p *T, v T) T
MulAssign is similar to x *= v expression in C, but accepts a pointer to a variable.
func New ¶
func New[T any]() *T
New is similar to builtin new, but uses the allocator. Caller must free the result with Free.
func OrAssign ¶
func OrAssign[T constraints.Integer](p *T, v T) T
OrAssign is similar to x |= v expression in C, but accepts a pointer to a variable.
func PostDec ¶
func PostDec[T number](p *T) T
PostDec is similar to x-- expression in C, but accepts a pointer to a variable.
func PostInc ¶
func PostInc[T number](p *T) T
PostInc is similar to x++ expression in C, but accepts a pointer to a variable.
func PreDec ¶
func PreDec[T number](p *T) T
PreDec is similar to --x expression in C, but accepts a pointer to a variable.
func PreInc ¶
func PreInc[T number](p *T) T
PreInc is similar to ++x expression in C, but accepts a pointer to a variable.
func PtrAdd ¶
PtrAdd is a generic version of unsafe.Add. It can be used in places where C pointer arithmetic was used.
func Remake ¶
Remake is similar to builtin make and C realloc. Caller must free the result with FreeS.
func RshAssign ¶
func RshAssign[T constraints.Integer](p *T, v T) T
RshAssign is similar to x >>= v expression in C, but accepts a pointer to a variable.
func StrCaseCmp
deprecated
func StrCaseCmp16
deprecated
func StrCaseCmpN ¶
StrCaseCmpN is safe version of StrCaseCmp which requires length.
func StrCaseCmpN16 ¶
StrCaseCmpN16 is safe version of StrCaseCmp16 which requires length.
func StrCaseCmpS16 ¶
StrCaseCmpS16 is safe version of StrCaseCmp16.
func StrCat ¶
func StrCat[T comparable](dst *T, src *T) *T
func StrCatS ¶
func StrCatS[T comparable](dst []T, src []T) []T
func StrCatStrS ¶
func StrChar
deprecated
func StrChar[T comparable](p *T, v T) *T
StrChar returns a pointer to the first occurrence of value in the C array/string p.
The terminating null-value is considered part of the C array. Therefore, it can also be located in order to retrieve a pointer to the end of array/string.
Deprecated: Unsafe, use StrCharS.
func StrCmp
deprecated
func StrCmp[T constraints.Ordered](p1, p2 *T) int
StrCmp is a generic version of C strcmp.
Deprecated: Unsafe, use StrCmpS.
func StrCmpN ¶
func StrCmpN[T constraints.Ordered](p1, p2 *T, max int) int
StrCmpN is the same as StrCmp, but accepts length for underlying arrays. It still looks for a zero value as a terminator.
func StrCmpS ¶
func StrCmpS[T constraints.Ordered](p1, p2 []T) int
StrCmpS is the same as StrCmp, but accepts slices. It still looks for a zero value as a terminator.
func StrCopy
deprecated
func StrCopy[T comparable](p *T, s *T)
StrCopy copies zero-terminated string s into a buffer pointed by p, including zero terminator.
Size of buffer p must be large enough for the string and zero terminator.
Deprecated: Unsafe, use StrCopyS.
func StrCopyS ¶
func StrCopyS[T comparable](p []T, s []T)
StrCopyS copies zero-terminated string s into a buffer p, including zero terminator.
If size of the buffer is too small, the string will be trimmed.
There is subtle difference between StrCopyS and StrNCopyS. Both accept slices and will stop writing if buffer is too small. However, StrNCopyS is allowed to omit zero terminator in this case, while StrCopyS will always add it. Thus, effective capacity of buffer for StrCopyS is len(p)-1, while for StrNCopyS it's len(p).
func StrCopySlice
deprecated
func StrCopySlice[T comparable](p *T, s []T)
StrCopySlice copies zero-terminated string s into a buffer pointed by p, including zero terminator.
Size of buffer p must be large enough for the string and zero terminator.
Deprecated: Unsafe, use StrCopyS.
func StrCopyStr
deprecated
func StrCopyStr16
deprecated
func StrCopyStr16S ¶
StrCopyStr16S copies string s into a UTF-16 buffer p, including zero terminator.
If size of the buffer is too small, the string will be trimmed.
For difference between StrCopyStr16S and StrNCopyStr16S see docs for StrCopyS or StrNCopyS.
func StrCopyStrS ¶
StrCopyStrS copies string s into a buffer p, including zero terminator.
If size of the buffer is too small, the string will be trimmed.
For difference between StrCopyStrS and StrNCopyStrS see docs for StrCopyS or StrNCopyS.
func StrDup
deprecated
func StrDup[T comparable](s *T) *T
StrDup makes a copy of the array or string.
Deprecated: Unsafe, use StrDupS.
func StrDupS ¶
func StrDupS[T comparable](s []T) []T
StrDupS makes a copy of the array or string. It accepts and returns slices.
func StrIndex
deprecated
func StrIndex[T comparable](p *T, v T) int
StrIndex is similar to C strchr, but returns an index.
Deprecated: Unsafe, use StrIndexS.
func StrIndexS ¶
func StrIndexS[T comparable](p []T, v T) int
StrIndexS is a safe version of StrIndex.
func StrLastIndex
deprecated
func StrLastIndex[T comparable](p *T, v T) int
StrLastIndex is similar to C strrchr, but returns an index.
Deprecated: Unsafe, use StrLastIndexS.
func StrLastIndexS ¶
func StrLastIndexS[T comparable](p []T, v T) int
StrLastIndexS is a safe version of StrLastIndex.
func StrLen
deprecated
func StrLen[T comparable](p *T) int
StrLen is a generic version of C strlen.
It accepts a pointer to the first array element and advances it until it reaches a zero value. Position of this zero terminator is returned, which equals to a length of the array/string.
Additionally, function checks for a nil pointer and returns 0 length in this case.
The behavior is undefined if p is not a pointer to a null-terminated array.
Deprecated: Unsafe, use StrNLen or StrLenS.
func StrLenS ¶
func StrLenS[T comparable](p []T) int
StrLenS is similar to StrLen, but accepts a slice. It still looks for a zero value as a terminator in the slice.
func StrNCopy
deprecated
func StrNCopy[T comparable](p *T, s *T, sz int)
StrNCopy copies zero-terminated string s into a buffer pointed by p, including zero terminator.
Size of buffer p must be large enough for the string. It may omit zero terminator.
Deprecated: Unsafe, use StrNCopyS.
func StrNCopyS ¶
func StrNCopyS[T comparable](p []T, s []T)
StrNCopyS copies zero-terminated string s into a buffer p, including zero terminator.
If size of the buffer is too small, the string will be trimmed. It may omit zero terminator.
There is subtle difference between StrCopyS and StrNCopyS. Both accept slices and will stop writing if buffer is too small. However, StrNCopyS is allowed to omit zero terminator in this case, while StrCopyS will always add it. Thus, effective capacity of buffer for StrCopyS is len(p)-1, while for StrNCopyS it's len(p).
func StrNCopySlice
deprecated
func StrNCopySlice[T comparable](p *T, s []T, sz int)
StrNCopySlice copies zero-terminated string s into a buffer pointed by p, including zero terminator.
Size of buffer p must be large enough for the string. It may omit zero terminator.
Deprecated: Unsafe, use StrNCopyS.
func StrNCopyStr
deprecated
func StrNCopyStr16
deprecated
func StrNCopyStr16S ¶
StrNCopyStr16S copies string s into a UTF-16 buffer p, including zero terminator.
If size of the buffer is too small, the string will be trimmed. It may omit zero terminator.
For difference between StrCopyStr16S and StrNCopyStr16S see docs for StrCopyS or StrNCopyS.
func StrNCopyStrS ¶
StrNCopyStrS copies string s into a buffer p, including zero terminator.
If size of the buffer is too small, the string will be trimmed. It may omit zero terminator.
For difference between StrCopyStrS and StrNCopyStrS see docs for StrCopyS or StrNCopyS.
func StrNLen ¶
func StrNLen[T comparable](p *T, max int) int
StrNLen is a generic version of C strnlen. It's the same as StrLen, but accepts a length of the buffer.
It is safer than using StrLenS and unsafe.Slice directly, because it checks for a zero pointer before converting to a slice.
func StrNSlice ¶
func StrNSlice[T comparable](p *T, max int) []T
StrNSlice uses StrLen to determine the length of a zero-terminated string, and returns it as a slice.
It uses max as a capacity of the returned slice, making it suitable for writing to a string buffer.
func StrRChar
deprecated
func StrRChar[T comparable](p *T, v T) *T
StrRChar returns a pointer to the last occurrence of value in the C array/string p.
The terminating null-value is considered part of the C array. Therefore, it can also be located to retrieve a pointer to the end of array/string.
Deprecated: Unsafe, use StrRCharS.
func StrRCharS ¶
func StrRCharS[T comparable](p []T, v T) []T
StrRCharS is a safe version of StrRChar.
func StrSlice
deprecated
func StrSlice[T comparable](p *T) []T
StrSlice uses StrLen to determine the length of a zero-terminated string, and returns it as a slice.
This method is only suitable for reading from a string. For writing to a string buffer, see StrSliceS or StrNSlice.
Deprecated: Unsafe, use StrSliceS or StrNSlice.
func StrSliceS ¶
func StrSliceS[T comparable](p []T) []T
StrSliceS uses StrLenS to determine the length of a zero-terminated string in a slice, and returns a new slice.
func StrSliceStr ¶
StrSliceStr is similar to StrSlice, but accept a string.
func SubAssign ¶
func SubAssign[T number](p *T, v T) T
SubAssign is similar to x -= v expression in C, but accepts a pointer to a variable.
func XorAssign ¶
func XorAssign[T constraints.Integer](p *T, v T) T
XorAssign is similar to x ^= v expression in C, but accepts a pointer to a variable.
Types ¶
type Allocator ¶
type Allocator interface { // Realloc allocates a zero-initialized array of cnt element, each of size sz. // // If the old pointer is given, all data is copied to the new array (possibly trimming the content), // and the old array is automatically freed. Zero-initialization is best-effort in this case. Realloc(ptr unsafe.Pointer, cnt, sz uintptr) unsafe.Pointer // Free previously allocated memory. Free(ptr unsafe.Pointer) }
Allocator is an interface that provides C-like allocation mechanisms.
func SetAllocator ¶
SetAllocator sets the global allocator. By default, GoAllocator is used. It returns the old allocator.
type GoAllocator ¶
type GoAllocator struct {
// contains filtered or unexported fields
}
GoAllocator is a simple allocator that uses mechanism similar to make to allocate memory. It keeps pointers of all allocations, so that GC will never reclaim it until Free. Zero value is safe for use.
func (*GoAllocator) Free ¶
func (a *GoAllocator) Free(p unsafe.Pointer)