x-engine-c v0.2.13
The C engine for x-lang
Loading...
Searching...
No Matches
x-lib.h File Reference

General-purpose library helpers. More...

#include "x-stdlib.h"

Go to the source code of this file.

Functions

Library Functions
x_char_tx_lib_inttostr (x_int_t num, x_char_t *p_str, unsigned short base)
 
void * x_lib_memdup (const void *p_src, size_t size)
 
x_int_t x_lib_strtoint (const x_char_t *p_str, x_char_t **pp_end, unsigned short base)
 

Detailed Description

General-purpose library helpers.

Helpers with x-expr's own contracts, declared in the x layer's types. The C standard library replacements – stdlib names, stdlib types, stdlib semantics – live in x-stdlib.h; this header pulls them in so the library layer is one include.

Author
Jon Ruttan (jonru.nosp@m.ttan.nosp@m.@gmai.nosp@m.l.co.nosp@m.m)

Function Documentation

◆ x_lib_inttostr()

x_char_t * x_lib_inttostr ( x_int_t  num,
x_char_t p_str,
unsigned short  base 
)

Convert an integer to a string representation.

Convert an integer to a string representation.

Converts num into an ASCII string stored in p_str using the given numeric base (2–36). If base is greater than 10, digits after '9' use lowercase letters starting with 'a'. If num is negative, a minus sign is prepended.

Note
The caller must provide sufficient storage in p_str. The minimum buffer size depends on the base. For base 2 (binary), the buffer needs at least 8 * sizeof(x_int_t) + 1 bytes.
Parameters
numThe integer to convert.
p_strDestination buffer for the resulting string.
baseNumeric base (2–36).
Returns
Pointer to p_str, or NULL if arguments are invalid.

◆ x_lib_memdup()

void * x_lib_memdup ( const void *  p_src,
size_t  size 
)

Duplicate a memory region.

Duplicate a memory region.

Allocates a new buffer via x_sys_malloc() and copies size bytes from p_src into it. If p_src is NULL, the buffer is allocated but not copied (and optionally zeroed if X_OPT_MEMZERO is defined).

Parameters
p_srcPointer to the source memory, or NULL.
sizeNumber of bytes to duplicate.
Returns
Pointer to the new buffer, or NULL on allocation failure.

◆ x_lib_strtoint()

x_int_t x_lib_strtoint ( const x_char_t p_str,
x_char_t **  pp_end,
unsigned short  base 
)

Convert a string to an integer.

Convert a string to an integer.

Parses p_str as a signed integer in the given base (2–36). If base is 0, the base is auto-detected from the prefix: 0x for hexadecimal, 0 for octal, otherwise decimal.

Leading whitespace is skipped. An optional + or - sign is accepted. If pp_end is non-NULL, it is set to point past the last parsed character.

Parameters
p_strThe string to parse.
pp_endOutput: pointer past the last parsed character, or NULL.
baseNumeric base (0 for auto-detect, or 2–36).
Returns
The parsed integer value.