General utilities v0.1.0

Source
C's <stdlib.h>: allocation, number parsing, sorting, pseudo-random numbers and exit codes.

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

Source

Parameters

NameTypeDescription
nc_intthe 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

Source

Parameters

NameTypeDescription
str*char8a 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

Source

Parameters

NameTypeDescription
str*char8a 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

Source

Parameters

NameTypeDescription
str*char8a 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

Source

Parameters

NameTypeDescription
str*char8a 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

Source

Parameters

NameTypeDescription
numsize_thow many objects
sizesize_thow 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

Source

Parameters

NameTypeDescription
ptr*var opaquethe 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

Source

Parameters

NameTypeDescription
nint64the 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

Source

Parameters

NameTypeDescription
nint64the 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

Source

Parameters

NameTypeDescription
sizesize_ta 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

Source

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

Source

Parameters

NameTypeDescription
ptr*var opaquethe block to act on
sizesize_ta 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

Source

Parameters

NameTypeDescription
seedc_uintthe 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

Source

Parameters

NameTypeDescription
str*char8a null-terminated byte string
endptr*var *char8receives the address where parsing stopped, or null
basec_intthe 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

Source

Parameters

NameTypeDescription
str*char8a null-terminated byte string
endptr*var *char8receives the address where parsing stopped, or null
basec_intthe 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

Source

Parameters

NameTypeDescription
str*char8a null-terminated byte string
endptr*var *char8receives the address where parsing stopped, or null
basec_intthe 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

Source

Parameters

NameTypeDescription
str*char8a null-terminated byte string
endptr*var *char8receives the address where parsing stopped, or null
basec_intthe 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

Source

Parameters

NameTypeDescription
str*char8a null-terminated byte string
endptr*var *char8receives 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

Source

Parameters

NameTypeDescription
str*char8a null-terminated byte string
endptr*var *char8receives 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

Source

Parameters

NameTypeDescription
base*var opaquethe first element of the array
numsize_thow many elements it holds
sizesize_thow many bytes one element occupies
comparefunc(*opaque, *opaque) -> c_inthow 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

Source

Parameters

NameTypeDescription
key*opaquethe value to search for
base*opaquethe first element of the array
numsize_thow many elements it holds
sizesize_thow many bytes one element occupies
comparefunc(*opaque, *opaque) -> c_inthow 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.

Source

EXIT_FAILURE

pub const EXIT_FAILURE: int32 = 1;

EXIT_FAILURE: the status a program returns to mean it did not.

Source

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.

Source