summaryrefslogtreecommitdiff
path: root/src/string.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/string.c')
-rw-r--r--src/string.c58
1 files changed, 36 insertions, 22 deletions
diff --git a/src/string.c b/src/string.c
index 74fe2bd..e8c7dbc 100644
--- a/src/string.c
+++ b/src/string.c
@@ -1,17 +1,14 @@
-#include "config.h"
+#include <s.h>
#include <assert.h>
#include <errno.h>
#include <stdbool.h>
-#include <stddef.h>
-#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "logerr.h"
#include "math.h"
-#include "util.h"
#include "string.h"
@@ -19,7 +16,7 @@
struct String {
const size_t length;
- const uint8_t *const bytes;
+ const u8 *const bytes;
};
@@ -33,7 +30,7 @@ string_new_with(
int rc = -1;
const struct String *ret = NULL;
- const uint8_t *bytes = NULL;
+ const u8 *bytes = NULL;
ret = malloc(sizeof(*ret));
if (ret == NULL) {
@@ -47,9 +44,9 @@ string_new_with(
goto out;
}
- memcpy((void *)bytes, string, length);
- memcpy((void *)(bytes + length), "", NULL_TERMINATOR);
- memcpy((void *)ret, &(struct String) {
+ memcpy((u8 *)bytes, string, length);
+ memcpy((u8 *)(bytes + length), "", NULL_TERMINATOR);
+ memcpy((u8 *)ret, &(struct String) {
.length = length,
.bytes = bytes,
}, sizeof(*ret));
@@ -58,10 +55,12 @@ string_new_with(
out:
if (rc) {
if (bytes != NULL) {
- freeit((void *)&bytes);
+ free((u8 *)bytes);
+ bytes = NULL;
}
if (ret != NULL) {
- freeit((void *)&ret);
+ free((struct String *)ret);
+ ret = NULL;
}
}
return rc;
@@ -72,11 +71,21 @@ string_new(const char *const string, const struct String **const out) {
return string_new_with(string, strlen(string), out);
}
+struct String
+S(const char *const string) {
+ return (struct String) {
+ .length = strlen(string) + NULL_TERMINATOR,
+ .bytes = (const u8 *)string,
+ };
+}
+
void
string_free(const struct String **const s) {
- const uint8_t *bytes = (*s)->bytes;
- freeit((void *)&bytes);
- freeit((void *)s);
+ const u8 *bytes = (*s)->bytes;
+ free((u8 *)bytes);
+ bytes = NULL;
+ free((struct String *)*s);
+ *s = NULL;
}
const char *
@@ -91,7 +100,10 @@ string_compare(const struct String *const s1, const struct String *const s2) {
} else if (s1->length > s2->length) {
return Comparison_GT;
} else {
- const int ret = strcmp((const char *)s1->bytes, (const char *)s2->bytes);
+ const int ret = strcmp(
+ (const char *)s1->bytes,
+ (const char *)s2->bytes
+ );
if (ret < 0) {
return Comparison_LT;
} else if (ret > 0) {
@@ -116,7 +128,7 @@ string_append(
int rc = -1;
const struct String *ret = NULL;
- const uint8_t *bytes = NULL;
+ const u8 *bytes = NULL;
size_t length;
if (add_size(s1->length, s2->length, &length)) {
@@ -136,10 +148,10 @@ string_append(
goto out;
}
- memcpy((void *)bytes, s1->bytes, s1->length);
- memcpy((void *)(bytes + s1->length), s2->bytes, s2->length);
- memcpy((void *)(bytes + length), "", NULL_TERMINATOR);
- memcpy((void *)ret, &(struct String) {
+ memcpy((u8 *)bytes, s1->bytes, s1->length);
+ memcpy((u8 *)(bytes + s1->length), s2->bytes, s2->length);
+ memcpy((u8 *)(bytes + length), "", NULL_TERMINATOR);
+ memcpy((u8 *)ret, &(struct String) {
.length = length,
.bytes = bytes,
}, sizeof(*ret));
@@ -148,10 +160,12 @@ string_append(
out:
if (rc) {
if (bytes != NULL) {
- freeit((void *)&bytes);
+ free((u8 *)bytes);
+ bytes = NULL;
}
if (ret != NULL) {
- freeit((void *)&ret);
+ free((struct String *)ret);
+ ret = NULL;
}
}
return rc;