feat(util): add str_dup()

This commit is contained in:
decafcode 2023-12-08 12:35:46 -05:00 committed by decafcode
parent c3da699f84
commit 1bb847c204
2 changed files with 25 additions and 1 deletions

View File

@ -1,10 +1,34 @@
#include <assert.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include "util/str.h"
int str_dup(char **out, const char *src) {
size_t len;
char *dest;
assert(out != NULL);
assert(src != NULL);
*out = NULL;
len = strlen(src) + 1;
dest = malloc(len);
if (dest == NULL) {
return -ENOMEM;
}
memcpy(dest, src, len);
*out = dest;
return 0;
}
bool str_eq(const char *lhs, const char *rhs) {
if (lhs == NULL || rhs == NULL) {
return lhs == rhs;

View File

@ -12,8 +12,8 @@ struct strbuf {
size_t pos;
};
int str_dup(char **dest, const char *src);
bool str_eq(const char *lhs, const char *rhs);
int str_printf(char **out, const char *fmt, ...)
gcc_attribute((format(printf, 2, 3)));
int str_vprintf(char **out, const char *fmt, va_list ap);