a wrapper around strings
This commit is contained in:
+123
@@ -0,0 +1,123 @@
|
||||
#include <odweta/string.h>
|
||||
#include <odweta/util.h>
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
size_t chars_length(char *chars) {
|
||||
int i = 0;
|
||||
while (*(chars + (i++)) != '\0');
|
||||
|
||||
return i;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
string_t *string_new() {
|
||||
string_t *s = (string_t *)malloc(sizeof(string_t));
|
||||
|
||||
s->content = (char *)malloc(sizeof(char));
|
||||
if (s->content == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
s->content = &(char){'\0'};
|
||||
s->length = 0;
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
string_t *string_from(char *chars) {
|
||||
string_t *s = (string_t *)malloc(sizeof(string_t));
|
||||
s->content = (char *)malloc(sizeof(chars));
|
||||
if (s->content == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int i = 0;
|
||||
while (*(chars + i) != '\0') {
|
||||
char *tmp = realloc(s->content, sizeof(s->content) + sizeof(char));
|
||||
if (tmp == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
s->content = tmp;
|
||||
*(s->content + i) = *(chars + i);
|
||||
i++;
|
||||
}
|
||||
char *tmp = realloc(s->content, sizeof(s->content) + sizeof(char));
|
||||
if (tmp == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
s->content = tmp;
|
||||
*(s->content + (++i)) = '\0';
|
||||
|
||||
s->length = (int)chars_length(s->content)-1; // -1 because \0
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
bool string_push_chars(string_t *this, char *chars) {
|
||||
int i = this->length;
|
||||
int j = 0;
|
||||
while (*(chars + j) != '\0') {
|
||||
char *tmp = realloc(this->content, sizeof(this->content) + sizeof(char));
|
||||
if (tmp == NULL) {
|
||||
return false;
|
||||
}
|
||||
this->content = tmp;
|
||||
*(this->content + i) = *(chars + j);
|
||||
i++;
|
||||
j++;
|
||||
}
|
||||
|
||||
this->length = (int)chars_length(this->content)-1;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool string_push(string_t *this, string_t* to_be_pushed) {
|
||||
return string_push_chars(this, to_be_pushed->content);
|
||||
}
|
||||
|
||||
compare_e string_compare(string_t* left, string_t *right) {
|
||||
int i = 0;
|
||||
while (
|
||||
*((left->content) + i) != '\0' ||
|
||||
*((right->content) + i) != '\0'
|
||||
) {
|
||||
if (*((left->content) + i) == *((right->content) + i)) {
|
||||
i++;
|
||||
continue;
|
||||
} else {
|
||||
return NOT_EQUAL;
|
||||
}
|
||||
}
|
||||
|
||||
return EQUAL;
|
||||
}
|
||||
|
||||
void string_print(string_t *this) {
|
||||
printf("%s", this->content);
|
||||
}
|
||||
|
||||
void string_println(string_t *this) {
|
||||
printf("%s\n", this->content);
|
||||
}
|
||||
|
||||
bool string_is_empty(string_t *this) {
|
||||
compare_e cmp = string_compare(this, string_from(""));
|
||||
switch (cmp) {
|
||||
case EQUAL:
|
||||
return true;
|
||||
case NOT_EQUAL:
|
||||
return false;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
void string_cleanup(string_t *this) {
|
||||
free(this->content);
|
||||
free(this);
|
||||
}
|
||||
Reference in New Issue
Block a user