69 lines
1.4 KiB
C
69 lines
1.4 KiB
C
#include "helper.h"
|
|
#include "types.h"
|
|
|
|
int noom_startswith(const char* str, const char* compare) {
|
|
#ifdef __has_builtin
|
|
#if __has_builtin(__builtin_strncmp)
|
|
return __builtin_strncmp(compare, str, noom_strlen(compare)) == 0;
|
|
#endif
|
|
#endif
|
|
noom_uint_t i = 0;
|
|
while (1) {
|
|
if (compare[i] == '\0') return 1; // we did it
|
|
if (str[i] != compare[i]) return 0;
|
|
i++;
|
|
}
|
|
return 0; // unreachable but whatevs
|
|
}
|
|
|
|
int noom_streql(const char* stra, noom_uint_t lena, const char* strb, noom_uint_t lenb) {
|
|
if (lena != lenb) return 0;
|
|
|
|
for (noom_uint_t i = 0; i < lena; i++) {
|
|
if (stra[i] != strb[i]) return 0;
|
|
}
|
|
|
|
return 1;
|
|
}
|
|
|
|
noom_uint_t noom_strlen(const char *s) {
|
|
#ifdef __has_builtin
|
|
#if __has_builtin(__builtin_strlen)
|
|
return __builtin_strlen(s);
|
|
#endif
|
|
#endif
|
|
const char *a = s;
|
|
while (*s) s++;
|
|
return s - a;
|
|
}
|
|
|
|
int noom_strcmp(const char *a, const char *b) {
|
|
#ifdef __has_builtin
|
|
#if __has_builtin(__builtin_strcmp)
|
|
return __builtin_strcmp(a, b);
|
|
#endif
|
|
#endif
|
|
for (; *a && *a == *b; a++, b++);
|
|
return *(const unsigned char*)a - *(const unsigned char*)b;
|
|
}
|
|
|
|
void noom_safe_strcpy(char* buffer, noom_uint_t* pos, noom_uint_t buffer_size, const char* src) {
|
|
while (*src && *pos < buffer_size - 1) {
|
|
buffer[(*pos)++] = *src++;
|
|
}
|
|
}
|
|
|
|
#include <stdlib.h> // TODO: remove
|
|
|
|
void* noom_alloc(noom_uint_t size) {
|
|
return malloc(size);
|
|
}
|
|
|
|
void noom_free(void* ptr) {
|
|
free(ptr);
|
|
}
|
|
|
|
void* noom_realloc(void* ptr, noom_uint_t size) {
|
|
return realloc(ptr, size);
|
|
}
|