Cthulhu  0.2.10
Cthulhu compiler collection
util.c File Reference
#include "base/util.h"
#include "base/panic.h"
#include <stdint.h>
#include <string.h>
Include dependency graph for util.c:

Go to the source code of this file.

Functions

STA_DECL bool is_path_special (const char *path)
 check if a path is special special paths are paths such as "." and ".." that are not valid for most operations More...
 
STA_DECL ctu_hash_t ctu_ptrhash (const void *ptr)
 hash a pointer value More...
 
STA_DECL ctu_hash_t str_hash (const char *str)
 hash a string More...
 
STA_DECL ctu_hash_t text_hash (text_view_t text)
 hash a string with a provided length More...
 
STA_DECL bool ctu_isalpha (int c)
 check if a character is a letter More...
 
STA_DECL bool ctu_isdigit (int c)
 check if a character is a digit More...
 
STA_DECL bool ctu_isalnum (int c)
 check if a character is alphanumeric More...
 
STA_DECL bool str_equal (const char *lhs, const char *rhs)
 compare strings equality More...
 
STA_DECL size_t ctu_strlen (const char *str)
 get the length of a string not including the null terminator equivalent to strlen but with safety checks More...
 
STA_DECL bool ctu_string_empty (const char *str)
 check if a string is empty equivalent to strlen(str) == 0 More...
 
STA_DECL int ctu_strncmp (const char *lhs, const char *rhs, size_t length)
 compare two strings equivalent to strncmp but with safety checks More...
 
STA_DECL int ctu_strcmp (const char *lhs, const char *rhs)
 compare two strings equivalent to strcmp but with safety checks More...
 
STA_DECL char * ctu_strcpy (char *dst, const char *src, size_t size)
 
STA_DECL CT_NOALIAS void * ctu_memcpy (void *restrict dst, const void *restrict src, size_t size)
 
STA_DECL void * ctu_memmove (void *dst, const void *src, size_t size)
 
STA_DECL CT_NOALIAS void ctu_memset (void *dst, int value, size_t size)
 
STA_DECL char * ctu_strstr (const char *haystack, const char *needle)
 find a substring in a string equivalent to strstr but with safety checks More...
 
STA_DECL text_t text_make (char *text, size_t length)
 
STA_DECL text_t text_from (char *text)
 create a new owning text array this is a shortcut for More...
 
STA_DECL text_view_t text_view_make (const char *text, size_t length)
 
STA_DECL text_view_t text_view_from (const char *text)
 create a new non-owning text array this is a shortcut for More...
 
bool text_equal (text_view_t lhs, text_view_t rhs)
 check if two text objects are equal More...
 

Function Documentation

◆ ctu_isalnum()

STA_DECL bool ctu_isalnum ( int  c)

check if a character is alphanumeric

Parameters
cthe character
Return values
trueif the character is alphanumeric
falseotherwise

Definition at line 70 of file util.c.

◆ ctu_isalpha()

STA_DECL bool ctu_isalpha ( int  c)

check if a character is a letter

Parameters
cthe character
Return values
trueif the character is a letter
falseotherwise

Definition at line 58 of file util.c.

◆ ctu_isdigit()

STA_DECL bool ctu_isdigit ( int  c)

check if a character is a digit

Parameters
cthe character
Return values
trueif the character is a digit
falseotherwise

Definition at line 64 of file util.c.

◆ ctu_memcpy()

STA_DECL CT_NOALIAS void* ctu_memcpy ( void *restrict  dst,
const void *restrict  src,
size_t  size 
)

Definition at line 130 of file util.c.

◆ ctu_memmove()

STA_DECL void* ctu_memmove ( void *  dst,
const void *  src,
size_t  size 
)

Definition at line 139 of file util.c.

◆ ctu_memset()

STA_DECL CT_NOALIAS void ctu_memset ( void *  dst,
int  value,
size_t  size 
)

Definition at line 148 of file util.c.

◆ ctu_ptrhash()

STA_DECL ctu_hash_t ctu_ptrhash ( const void *  ptr)

hash a pointer value

Parameters
ptrthe pointer to hash
Returns
the hash of the pointer

Definition at line 16 of file util.c.

◆ ctu_strcmp()

STA_DECL int ctu_strcmp ( const char *  lhs,
const char *  rhs 
)

compare two strings equivalent to strcmp but with safety checks

Precondition
lhs and rhs must not be null
Parameters
lhsthe left hand side
rhsthe right hand side
Returns
the comparison result

Definition at line 112 of file util.c.

◆ ctu_strcpy()

STA_DECL char* ctu_strcpy ( char *  dst,
const char *  src,
size_t  size 
)

Definition at line 121 of file util.c.

◆ ctu_string_empty()

STA_DECL bool ctu_string_empty ( const char *  str)

check if a string is empty equivalent to strlen(str) == 0

Precondition
str must not be null
Parameters
strthe string
Return values
trueif the string is empty
falseotherwise

Definition at line 95 of file util.c.

◆ ctu_strlen()

STA_DECL size_t ctu_strlen ( const char *  str)

get the length of a string not including the null terminator equivalent to strlen but with safety checks

Precondition
str must not be null
Parameters
strthe string
Returns
the length of the string

Definition at line 87 of file util.c.

◆ ctu_strncmp()

STA_DECL int ctu_strncmp ( const char *  lhs,
const char *  rhs,
size_t  length 
)

compare two strings equivalent to strncmp but with safety checks

Precondition
lhs and rhs must not be null
Parameters
lhsthe left hand side
rhsthe right hand side
lengththe length to compare
Returns
the comparison result

Definition at line 103 of file util.c.

◆ ctu_strstr()

STA_DECL char* ctu_strstr ( const char *  haystack,
const char *  needle 
)

find a substring in a string equivalent to strstr but with safety checks

Precondition
haystack and needle must not be null
Parameters
haystackthe string to search in
needlethe string to search for
Returns
the position of needle in haystack or null if not found

Definition at line 156 of file util.c.

◆ str_equal()

STA_DECL bool str_equal ( const char *  lhs,
const char *  rhs 
)

compare strings equality

check if 2 strings are equal

Parameters
lhsthe left hand side of the comparison
rhsthe right hand side of the comparison
Returns
if the strings are equal

Definition at line 76 of file util.c.

◆ str_hash()

STA_DECL ctu_hash_t str_hash ( const char *  str)

hash a string

Parameters
strthe string to hash
Returns
the hash

Definition at line 29 of file util.c.

◆ text_equal()

bool text_equal ( text_view_t  lhs,
text_view_t  rhs 
)

check if two text objects are equal

Parameters
lhsthe left hand side
rhsthe right hand side
Return values
trueif lhs and rhs are equal
falseotherwise

Definition at line 196 of file util.c.

◆ text_from()

STA_DECL text_t text_from ( char *  text)

create a new owning text array this is a shortcut for

text_make(text, ctu_strlen(text))
CT_CONSTFN CT_BASE_API text_t text_make(STA_READS(length) char *text, size_t length)
create a new owning text array text must be a valid string at least length bytes long
CT_NODISCARD CT_PUREFN CT_BASE_API size_t ctu_strlen(const char *str)
get the length of a string not including the null terminator equivalent to strlen but with safety che...
Definition: util.c:87
Parameters
textthe text
Returns
the text object

Definition at line 175 of file util.c.

◆ text_hash()

STA_DECL ctu_hash_t text_hash ( text_view_t  text)

hash a string with a provided length

Parameters
textthe string to hash
Returns
the hash

Definition at line 44 of file util.c.

◆ text_make()

STA_DECL text_t text_make ( char *  text,
size_t  length 
)

Definition at line 165 of file util.c.

◆ text_view_from()

STA_DECL text_view_t text_view_from ( const char *  text)

create a new non-owning text array this is a shortcut for

CT_CONSTFN CT_BASE_API text_view_t text_view_make(STA_READS(length) const char *text, size_t length)
create a new non-owning text array text must be at least length bytes long
Parameters
textthe text
Returns
the text object

Definition at line 191 of file util.c.

◆ text_view_make()

STA_DECL text_view_t text_view_make ( const char *  text,
size_t  length 
)

Definition at line 181 of file util.c.