home /
secrets /
src /
util /
stringu.c
stringu.c
//
// strings.c
// fornax2_core
//
// Created by Anders on 24/04/2019.
//
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include "stringu.h"
void stringu_copy(char *copy, const char *str, size_t buffer_size) {
// strcpy(copy, str);
strlcpy(copy, str, buffer_size);
}
char* stringu_copy_alloc(const char* str) {
size_t len = strlen(str);
char* copy = malloc(len + 1);
stringu_copy(copy, str, len + 1);
return copy;
}
uint32_t stringu_split(char* src, char** dest, const char* delim) {
uint32_t i = 0;
char *ptr = strtok(src, delim);
while(ptr != NULL) {
dest[i] = ptr;
i++;
ptr = strtok(NULL, delim);
}
return i;
}
bool stringu_equals(const char *str1, const char *str2) {
return !strcmp(str1, str2);
}
bool stringu_starts_with(const char *str, const char *prefix) {
if(strncmp(str, prefix, strlen(prefix)) == 0) return true;
return false;
}
long stringu_to_long(const char* str) {
return strtol(str, NULL, 10);
}
char *stringu_trim(char *str) {
char *end;
while(isspace((unsigned char)*str)) str++;
if(*str == 0)
return str;
end = str + strlen(str) - 1;
while(end > str && isspace((unsigned char)*end)) end--;
end[1] = '\0';
return str;
}