Cthulhu  0.2.10
Cthulhu compiler collection
util.h
Go to the documentation of this file.
1 // SPDX-License-Identifier: LGPL-3.0-only
2 
3 #pragma once
4 
5 #include <ctu_base_api.h>
6 
7 #include "core/analyze.h"
8 #include "core/compiler.h"
9 #include "core/text.h"
10 #include "core/types.h"
11 
12 #include <stddef.h>
13 #include <stdbool.h>
14 
16 
19 
27 CT_BASE_API bool is_path_special(IN_STRING const char *path);
28 
35 CT_BASE_API ctu_hash_t ctu_ptrhash(const void *ptr);
36 
43 CT_BASE_API ctu_hash_t str_hash(IN_STRING const char *str);
44 
51 CT_BASE_API ctu_hash_t text_hash(text_view_t text);
52 
53 // stdlib wrappers
54 
62 CT_BASE_API bool ctu_isalpha(int c);
63 
71 CT_BASE_API bool ctu_isdigit(int c);
72 
80 CT_BASE_API bool ctu_isalnum(int c);
81 
91 CT_BASE_API bool str_equal(IN_STRING const char *lhs, IN_STRING const char *rhs);
92 
103 CT_BASE_API char *ctu_strcpy(STA_WRITES(size) char *dst, IN_STRING const char *src, size_t size);
104 
114 CT_BASE_API size_t ctu_strlen(IN_STRING const char *str);
115 
126 CT_BASE_API bool ctu_string_empty(IN_STRING const char *str);
127 
139 CT_BASE_API int ctu_strncmp(IN_STRING const char *lhs, IN_STRING const char *rhs, size_t length);
140 
151 CT_BASE_API int ctu_strcmp(IN_STRING const char *lhs, IN_STRING const char *rhs);
152 
164 CT_BASE_API void *ctu_memcpy(STA_WRITES(size) void *CT_RESTRICT dst, STA_READS(size) const void *CT_RESTRICT src, size_t size);
165 
176 CT_BASE_API void *ctu_memmove(STA_WRITES(size) void *dst, STA_READS(size) const void *src, size_t size);
177 
187 CT_BASE_API void ctu_memset(STA_WRITES(size) void *dst, int value, size_t size);
188 
199 CT_BASE_API char *ctu_strstr(IN_STRING const char *haystack, IN_STRING const char *needle);
200 
201 // text api
202 
211 CT_BASE_API text_t text_make(STA_READS(length) char *text, size_t length);
212 
222 CT_PUREFN
223 CT_BASE_API text_t text_from(IN_STRING char *text);
224 
233 CT_BASE_API text_view_t text_view_make(STA_READS(length) const char *text, size_t length);
234 
244 CT_PUREFN
245 CT_BASE_API text_view_t text_view_from(IN_STRING const char *text);
246 
254 CT_PUREFN
255 CT_BASE_API bool text_equal(text_view_t lhs, text_view_t rhs);
256 
258 
CT_NODISCARD STA_WRITES(size) void *dst
CT_NODISCARD size_t size
Definition: scan.h:128
#define CT_RESTRICT
Definition: analyze.h:260
#define CT_PUREFN
mark a function as pure, always returns the same value for the same arguments
Definition: analyze.h:228
#define CT_NODISCARD
mark a function as returning a value that must be used
#define STA_READS(size)
annotate a parameter as reading expr elements
#define CT_CONSTFN
mark a function as const, has no side effects and always returns the same value for the same argument...
Definition: analyze.h:227
#define IN_STRING
annotate a parameter as being a null terminated string
#define CT_NOALIAS
mark a function as only modifying pointers passed to it the same as CT_CONSTFN but allowed to modify/...
Definition: analyze.h:226
CT_NODISCARD CT_PUREFN CT_BASE_API int ctu_strncmp(const char *lhs, const char *rhs, size_t length)
compare two strings equivalent to strncmp but with safety checks
Definition: util.c:103
CT_NODISCARD CT_CONSTFN CT_BASE_API ctu_hash_t ctu_ptrhash(const void *ptr)
hash a pointer value
Definition: util.c:16
CT_NODISCARD CT_PUREFN CT_BASE_API int ctu_strcmp(const char *lhs, const char *rhs)
compare two strings equivalent to strcmp but with safety checks
Definition: util.c:112
CT_BASE_API char * ctu_strcpy(STA_WRITES(size) char *dst, const char *src, size_t size)
copy a string equivalent to strcpy but with safety checks
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_BASE_API void * ctu_memmove(STA_WRITES(size) void *dst, STA_READS(size) const void *src, size_t size)
move memory from one location to another equivalent to memmove but with safety checks
CT_NODISCARD CT_PUREFN CT_BASE_API ctu_hash_t text_hash(text_view_t text)
hash a string with a provided length
Definition: util.c:44
CT_NOALIAS CT_BASE_API void * ctu_memcpy(STA_WRITES(size) void *CT_RESTRICT dst, STA_READS(size) const void *CT_RESTRICT src, size_t size)
copy memory from one location to another equivalent to memcpy but with safety checks
CT_NODISCARD CT_PUREFN CT_BASE_API bool ctu_string_empty(const char *str)
check if a string is empty equivalent to strlen(str) == 0
Definition: util.c:95
CT_BEGIN_API CT_NODISCARD CT_PUREFN CT_BASE_API 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 o...
Definition: util.c:10
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
CT_NODISCARD CT_CONSTFN CT_BASE_API bool ctu_isalpha(int c)
check if a character is a letter
Definition: util.c:58
CT_NODISCARD CT_CONSTFN CT_BASE_API bool ctu_isdigit(int c)
check if a character is a digit
Definition: util.c:64
CT_NODISCARD CT_PUREFN CT_BASE_API char * ctu_strstr(const char *haystack, const char *needle)
find a substring in a string equivalent to strstr but with safety checks
Definition: util.c:156
CT_NODISCARD CT_CONSTFN CT_BASE_API bool ctu_isalnum(int c)
check if a character is alphanumeric
Definition: util.c:70
CT_NODISCARD CT_PUREFN CT_BASE_API bool str_equal(const char *lhs, const char *rhs)
compare strings equality
Definition: util.c:76
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
CT_PUREFN CT_BASE_API bool text_equal(text_view_t lhs, text_view_t rhs)
check if two text objects are equal
Definition: util.c:196
CT_NODISCARD CT_PUREFN CT_BASE_API ctu_hash_t str_hash(const char *str)
hash a string
Definition: util.c:29
CT_PUREFN CT_BASE_API text_view_t text_view_from(const char *text)
create a new non-owning text array this is a shortcut for
Definition: util.c:191
CT_NOALIAS CT_BASE_API void ctu_memset(STA_WRITES(size) void *dst, int value, size_t size)
set memory to a value equivalent to memset but with safety checks
CT_PUREFN CT_BASE_API text_t text_from(char *text)
create a new owning text array this is a shortcut for
Definition: util.c:175
#define CT_BEGIN_API
Definition: compiler.h:129
#define CT_END_API
Definition: compiler.h:130
size_t ctu_hash_t
Definition: types.h:8
a range of text
Definition: text.h:14
a non-owning view of text
Definition: text.h:24