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

Implementation of the C standard library replacements. More...

#include "x-stdlib.h"

Functions

int x_lib_abs (int i)
 
void * x_lib_memcpy (void *p_dest, const void *p_src, size_t size)
 
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

Implementation of the C standard library replacements.

The C standard defines the behaviour here, not us (see x-stdlib.h for the charter). Comparison functions compare bytes as unsigned char (C99 7.24.4) regardless of the platform's plain-char signedness, and strchr converts c to char before comparing (C99 7.24.5.2) – both exactly as the standard specifies, so the freestanding path and an X_USE_STDLIB build agree byte-for-byte.

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.

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.

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.

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.

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.

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.

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 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.

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.