General utilities v0.1.0
abs
pub extern func abs(n: c_int) -> c_int
Computes the absolute value of an integer value.
See also: https://en.cppreference.com/c/numeric/math/abs
Parameters
| Name | Type | Description |
|---|---|---|
n | c_int | the value to act on |
Returns: the magnitude; the most negative value has none and the result is undefined
atof
pub extern func atof(str: *char8) -> c_double
Converts a byte string to a floating point value. Cannot report failure: a string that is not a number gives zero, and a value out of range is undefined rather than reported. Use strtod, which reports both.
See also: https://en.cppreference.com/c/string/byte/atof
Parameters
| Name | Type | Description |
|---|---|---|
str | *char8 | a null-terminated byte string |
Returns: the value, or zero when nothing was read; there is no way to tell the two apart
atoi
pub extern func atoi(str: *char8) -> c_int
Converts a byte string to an integer value. Cannot report failure, and a value out of range is undefined rather than reported. Use strtol.
See also: https://en.cppreference.com/c/string/byte/atoi
Parameters
| Name | Type | Description |
|---|---|---|
str | *char8 | a null-terminated byte string |
Returns: the value, or zero when nothing was read; there is no way to tell the two apart
atol
pub extern func atol(str: *char8) -> int64
Converts a byte string to a long integer value. Cannot report failure. Use strtol.
See also: https://en.cppreference.com/c/string/byte/atoi
Parameters
| Name | Type | Description |
|---|---|---|
str | *char8 | a null-terminated byte string |
Returns: the value, or zero when nothing was read; there is no way to tell the two apart
atoll
pub extern func atoll(str: *char8) -> int64
Converts a byte string to a long long integer value. Cannot report failure. Use strtoll.
See also: https://en.cppreference.com/c/string/byte/atoi
Parameters
| Name | Type | Description |
|---|---|---|
str | *char8 | a null-terminated byte string |
Returns: the value, or zero when nothing was read; there is no way to tell the two apart
calloc
pub extern func calloc(
num: size_t,
size: size_t
) -> *var opaque
Allocates memory for an array of num objects of size and zero-initializes it.
See also: https://en.cppreference.com/c/memory/calloc
Parameters
| Name | Type | Description |
|---|---|---|
num | size_t | how many objects |
size | size_t | how many bytes one object occupies |
Returns: the address of the zeroed block, or null
free
pub extern func free(ptr: *var opaque)
Deallocates the memory previously allocated by malloc, calloc or realloc.
See also: https://en.cppreference.com/c/memory/free
Parameters
| Name | Type | Description |
|---|---|---|
ptr | *var opaque | the block to act on |
labs
pub extern func labs(n: int64) -> int64
Computes the absolute value of a long integer value.
See also: https://en.cppreference.com/c/numeric/math/abs
Parameters
| Name | Type | Description |
|---|---|---|
n | int64 | the value to act on |
Returns: the magnitude; the most negative value has none and the result is undefined
llabs
pub extern func llabs(n: int64) -> int64
Computes the absolute value of a long long integer value.
See also: https://en.cppreference.com/c/numeric/math/abs
Parameters
| Name | Type | Description |
|---|---|---|
n | int64 | the value to act on |
Returns: the magnitude; the most negative value has none and the result is undefined
malloc
pub extern func malloc(size: size_t) -> *var opaque
Allocates size bytes of uninitialized memory.
See also: https://en.cppreference.com/c/memory/malloc
Parameters
| Name | Type | Description |
|---|---|---|
size | size_t | a size in bytes |
Returns: the address of the block, or null
rand
pub extern func rand() -> c_int
Returns a pseudo-random integer value between 0 and RAND_MAX. Nothing about the sequence is guaranteed, and it must never produce a key, a token, or anything an adversary would like to predict. Rux/Random names its generators; Rux/Entropy reaches the operating system.
See also: https://en.cppreference.com/c/numeric/random/rand
Returns: a value in [0, RAND_MAX], from a sequence that guarantees nothing
realloc
pub extern func realloc(
ptr: *var opaque,
size: size_t
) -> *var opaque
Changes the size of the memory block pointed to by ptr to size bytes.
See also: https://en.cppreference.com/c/memory/realloc
Parameters
| Name | Type | Description |
|---|---|---|
ptr | *var opaque | the block to act on |
size | size_t | a size in bytes |
Returns: the address of the resized block, or null with the original still valid
srand
pub extern func srand(seed: c_uint)
Seeds the pseudo-random number generator used by rand.
See also: https://en.cppreference.com/c/numeric/random/srand
Parameters
| Name | Type | Description |
|---|---|---|
seed | c_uint | the value to seed the generator with |
strtol
pub extern func strtol(
str: *char8,
endptr: *var *char8,
base: c_int
) -> c_long
Converts a byte string to a long integer, reporting where it stopped.
See also: https://en.cppreference.com/c/string/byte/strtol
Parameters
| Name | Type | Description |
|---|---|---|
str | *char8 | a null-terminated byte string |
endptr | *var *char8 | receives the address where parsing stopped, or null |
base | c_int | the radix, 2 through 36, or zero to read a C integer prefix |
Returns: the value, zero when nothing was read, or a saturated limit with errno set to ERANGE
strtoul
pub extern func strtoul(
str: *char8,
endptr: *var *char8,
base: c_int
) -> c_ulong
Converts a byte string to an unsigned long integer, reporting where it stopped.
See also: https://en.cppreference.com/c/string/byte/strtoul
Parameters
| Name | Type | Description |
|---|---|---|
str | *char8 | a null-terminated byte string |
endptr | *var *char8 | receives the address where parsing stopped, or null |
base | c_int | the radix, 2 through 36, or zero to read a C integer prefix |
Returns: the value, zero when nothing was read, or a saturated limit with errno set to ERANGE
strtoll
pub extern func strtoll(
str: *char8,
endptr: *var *char8,
base: c_int
) -> c_longlong
Converts a byte string to a long long integer, reporting where it stopped.
See also: https://en.cppreference.com/c/string/byte/strtol
Parameters
| Name | Type | Description |
|---|---|---|
str | *char8 | a null-terminated byte string |
endptr | *var *char8 | receives the address where parsing stopped, or null |
base | c_int | the radix, 2 through 36, or zero to read a C integer prefix |
Returns: the value, zero when nothing was read, or a saturated limit with errno set to ERANGE
strtoull
pub extern func strtoull(
str: *char8,
endptr: *var *char8,
base: c_int
) -> c_ulonglong
Converts a byte string to an unsigned long long integer, reporting where it stopped.
See also: https://en.cppreference.com/c/string/byte/strtoul
Parameters
| Name | Type | Description |
|---|---|---|
str | *char8 | a null-terminated byte string |
endptr | *var *char8 | receives the address where parsing stopped, or null |
base | c_int | the radix, 2 through 36, or zero to read a C integer prefix |
Returns: the value, zero when nothing was read, or a saturated limit with errno set to ERANGE
strtod
pub extern func strtod(
str: *char8,
endptr: *var *char8
) -> c_double
Converts a byte string to a double, reporting where it stopped.
See also: https://en.cppreference.com/c/string/byte/strtof
Parameters
| Name | Type | Description |
|---|---|---|
str | *char8 | a null-terminated byte string |
endptr | *var *char8 | receives the address where parsing stopped, or null |
Returns: the value, zero when nothing was read, or a saturated value with errno set to ERANGE
strtof
pub extern func strtof(
str: *char8,
endptr: *var *char8
) -> c_float
Converts a byte string to a float, reporting where it stopped.
See also: https://en.cppreference.com/c/string/byte/strtof
Parameters
| Name | Type | Description |
|---|---|---|
str | *char8 | a null-terminated byte string |
endptr | *var *char8 | receives the address where parsing stopped, or null |
Returns: the value, zero when nothing was read, or a saturated value with errno set to ERANGE
qsort
pub extern func qsort(
base: *var opaque,
num: size_t,
size: size_t,
compare: func(*opaque, *opaque) -> c_int
)
Sorts an array in place; the comparison must be a strict weak ordering and the sort is not stable.
See also: https://en.cppreference.com/c/algorithm/qsort
Parameters
| Name | Type | Description |
|---|---|---|
base | *var opaque | the first element of the array |
num | size_t | how many elements it holds |
size | size_t | how many bytes one element occupies |
compare | func(*opaque, *opaque) -> c_int | how two elements order against each other |
bsearch
pub extern func bsearch(
key: *opaque,
base: *opaque,
num: size_t,
size: size_t,
compare: func(*opaque, *opaque) -> c_int
) -> *opaque
Finds an element of a sorted array; the array must already be ordered by the same comparison.
See also: https://en.cppreference.com/c/algorithm/bsearch
Parameters
| Name | Type | Description |
|---|---|---|
key | *opaque | the value to search for |
base | *opaque | the first element of the array |
num | size_t | how many elements it holds |
size | size_t | how many bytes one element occupies |
compare | func(*opaque, *opaque) -> c_int | how two elements order against each other |
Returns: the address of a matching element, or null
EXIT_SUCCESS
pub const EXIT_SUCCESS: int32 = 0;
EXIT_SUCCESS: the status a program returns to mean it did what it was asked.
EXIT_FAILURE
pub const EXIT_FAILURE: int32 = 1;
EXIT_FAILURE: the status a program returns to mean it did not.
RAND_MAX
// Windows
pub const RAND_MAX: int32 = 32767;
// Linux · macOS · FreeBSD
pub const RAND_MAX: int32 = 2147483647;
RAND_MAX: the largest value rand can return on this target.