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

C standard library replacements. More...

#include "x-sys.h"

Go to the source code of this file.

Functions

Standard Library Replacements
int x_lib_abs (int i)
 
void * x_lib_memcpy (void *p_dest, const void *p_src, size_t n)
 
void * x_lib_memset (void *p_dest, int byte, size_t size)
 
char * x_lib_strchr (const char *p_str, int c)
 
int x_lib_strcmp (const char *p_str1, const char *p_str2)
 
size_t x_lib_strlen (const char *p_str)
 
int x_lib_strncmp (const char *p_str1, const char *p_str2, size_t n)
 
char * x_lib_strndup (const char *p_str, size_t size)
 

Detailed Description

C standard library replacements.

Stand-ins for standard C library functions, for builds where the stdlib is not available (freestanding targets, embedders that supply their own runtime). When X_USE_STDLIB is defined each function is a light wrapper delegating to the real one; otherwise the self-contained implementation is used.

The C standard owns everything about these functions: the semantics, and the types in the prototypes – plain char/void/int/ size_t, never the x layer's typedefs. The x_lib_ prefix is the one deliberate difference: it lets the replacements coexist with a real libc in the same link. General-purpose helpers with their own contracts live in x-lib.h, not here.

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

Function Documentation

◆ x_lib_abs()

int x_lib_abs ( int  i)

Compute the absolute value of an integer.

Compute the absolute value of an integer.

Parameters
iThe integer value.
Returns
The absolute value of i.

◆ x_lib_memcpy()

void * x_lib_memcpy ( void *  p_dest,
const void *  p_src,
size_t  size 
)

Copy bytes from a source buffer to a destination buffer.

Copy bytes from a source buffer to a destination buffer.

Parameters
p_destPointer to the destination buffer.
p_srcPointer to the source buffer.
sizeNumber of bytes to copy.
Returns
Pointer to p_dest.

◆ x_lib_memset()

void * x_lib_memset ( void *  p_dest,
int  byte,
size_t  size 
)

Fill a memory region with a constant byte.

Fill a memory region with a constant byte.

Parameters
p_destPointer to the destination buffer.
byteThe byte value to fill with.
sizeNumber of bytes to set.
Returns
Pointer to p_dest.

◆ x_lib_strchr()

char * x_lib_strchr ( const char *  p_str,
int  c 
)

Locate the first occurrence of a character in a string.

Locate the first occurrence of a character in a string.

c is converted to char before comparing, and the terminating NUL is part of the string (C99 7.24.5.2): searching for '\0' returns the terminator.

Parameters
p_strThe string to search.
cThe character to find (as an int).
Returns
Pointer to the first occurrence, or NULL if not found.

◆ x_lib_strcmp()

int x_lib_strcmp ( const char *  p_str1,
const char *  p_str2 
)

Compare two null-terminated strings.

Compare two null-terminated strings.

Bytes compare as unsigned char (C99 7.24.4), independent of the platform's plain-char signedness.

Parameters
p_str1First string.
p_str2Second string.
Returns
Negative, zero, or positive value indicating ordering.

◆ x_lib_strlen()

size_t x_lib_strlen ( const char *  p_str)

Calculate the length of a null-terminated string.

Calculate the length of a null-terminated string.

Note
This function does not handle wide characters.
Parameters
p_strThe string to measure.
Returns
The length in bytes, excluding the null terminator.

◆ x_lib_strncmp()

int x_lib_strncmp ( const char *  p_str1,
const char *  p_str2,
size_t  n 
)

Compare two strings up to a maximum number of characters.

Compare two strings up to a maximum of n characters.

Bytes compare as unsigned char (C99 7.24.4); n == 0 compares nothing and returns 0.

Parameters
p_str1First string.
p_str2Second string.
nMaximum number of characters to compare.
Returns
Negative, zero, or positive value indicating ordering.

◆ x_lib_strndup()

char * x_lib_strndup ( const char *  p_str,
size_t  size 
)

Duplicate at most size characters of a string (POSIX.1-2008).

Duplicate at most size characters of a string.

The copy stops at the first NUL or after size characters, whichever comes first, and is always NUL-terminated; at most size source bytes are read.

Parameters
p_strThe source string.
sizeMaximum number of characters to copy.
Returns
Pointer to the new string, or NULL on allocation failure.