added documentation, almost done with code
This commit is contained in:
+82
-81
@@ -7,36 +7,35 @@
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
|
||||
typedef struct {
|
||||
typedef struct String {
|
||||
uint32_t size, capacity;
|
||||
char* data;
|
||||
} String_t;
|
||||
|
||||
String_t str_with_capacity(int capacity) {
|
||||
String_t s;
|
||||
|
||||
/// allocate memory for 'capacity' chars
|
||||
s.data = (char*)calloc(capacity, sizeof(char));
|
||||
|
||||
s.size = 0;
|
||||
s.capacity = capacity;
|
||||
|
||||
return s;
|
||||
int str_len(String_t* s) {
|
||||
return (int)s->size;
|
||||
}
|
||||
|
||||
String_t str_from_chars(const char* string) {
|
||||
String_t s;
|
||||
char* to_chars(String_t* s) {
|
||||
// allocate buffer with null terminator
|
||||
char* buffer = (char*)calloc(s->size + 1, sizeof(char));
|
||||
for (int i = 0; i < s->size; i++) {
|
||||
buffer[i] = s->data[i];
|
||||
}
|
||||
buffer[s->size] = '\0';
|
||||
return buffer;
|
||||
}
|
||||
|
||||
s.data = (char*)calloc(strlen(string), sizeof(char));
|
||||
strcpy(s.data, string);
|
||||
s.size = strlen(string);
|
||||
s.capacity = strlen(string);
|
||||
String_t str_with_capacity(int capacity) {
|
||||
String_t s;
|
||||
s.data = (char*)calloc(capacity, sizeof(char));
|
||||
s.size = 0;
|
||||
s.capacity = capacity;
|
||||
return s;
|
||||
}
|
||||
|
||||
String_t str_from_slice(const char* string, int len) {
|
||||
String_t s;
|
||||
|
||||
s.data = (char*)calloc(len, sizeof(char));
|
||||
strncpy(s.data, string, len);
|
||||
s.size = len;
|
||||
@@ -44,27 +43,26 @@ String_t str_from_slice(const char* string, int len) {
|
||||
return s;
|
||||
}
|
||||
|
||||
String_t str_from_chars(const char* string) {
|
||||
return str_from_slice(string, strlen(string));
|
||||
}
|
||||
|
||||
String_t str_new() {
|
||||
return str_with_capacity(1);
|
||||
}
|
||||
|
||||
|
||||
|
||||
int str_dealloc(String_t* self) {
|
||||
free(self->data);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int str_push(String_t* self, char c) {
|
||||
// check size < capacity
|
||||
if (self->size < self->capacity) {
|
||||
self->data[self->size] = c;
|
||||
self->size++;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// reallocate to add capacity for an extra char
|
||||
int newcap = self->capacity + sizeof(char);
|
||||
self->data = (char*)realloc(self->data, newcap);
|
||||
self->data[self->size] = c;
|
||||
@@ -75,41 +73,57 @@ int str_push(String_t* self, char c) {
|
||||
|
||||
int str_push_str(String_t* self, char* c) {
|
||||
if (self->size + strlen(c) >= self->capacity) {
|
||||
self->capacity = ( self->size + strlen(c) ) * sizeof(char);
|
||||
self->capacity = (self->size + strlen(c)) * sizeof(char);
|
||||
self->data = (char*)realloc(self->data, self->capacity);
|
||||
|
||||
for (int i = 0; i < strlen(c); i++) {
|
||||
self->data[self->size + i] = c[i];
|
||||
}
|
||||
self->size = self->capacity;
|
||||
}
|
||||
for (int i = 0; i < strlen(c); i++) {
|
||||
self->data[self->size + i] = c[i];
|
||||
}
|
||||
self->size = self->capacity;
|
||||
return 0;
|
||||
}
|
||||
|
||||
String_t str_from_file(FILE* file) {
|
||||
if (file == NULL) {
|
||||
return str_new();
|
||||
}
|
||||
|
||||
char buffer[64];
|
||||
|
||||
String_t string = str_new();
|
||||
|
||||
while (fgets(buffer, sizeof(buffer), file) != NULL) {
|
||||
str_push_str(&string, buffer);
|
||||
}
|
||||
|
||||
fclose(file);
|
||||
|
||||
return string;
|
||||
}
|
||||
|
||||
|
||||
int str_to_file(String_t* self, FILE* file) {
|
||||
char* data = to_chars(self);
|
||||
|
||||
if (fprintf(file, "%s\n", data) == -1) {
|
||||
free(data);
|
||||
return -1;
|
||||
}
|
||||
free(data);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
String_t str_clone(String_t* self) {
|
||||
String_t clone;
|
||||
clone.size = self->size;
|
||||
clone.capacity = self->capacity;
|
||||
clone.data = (char*)malloc(clone.capacity);
|
||||
memcpy(clone.data, self->data, clone.capacity);
|
||||
return clone;
|
||||
}
|
||||
|
||||
char str_pop(String_t* self) {
|
||||
if (self->size == 0) {
|
||||
return '\0';
|
||||
}
|
||||
self->size--;
|
||||
char c = self->data[self->size];
|
||||
return c;
|
||||
return self->data[self->size];
|
||||
}
|
||||
|
||||
int str_insert(String_t* self, int index, char c) {
|
||||
@@ -117,6 +131,10 @@ int str_insert(String_t* self, int index, char c) {
|
||||
return -1;
|
||||
}
|
||||
self->size++;
|
||||
if (self->size > self->capacity) {
|
||||
self->capacity = (self->size) * sizeof(char);
|
||||
self->data = (char*)realloc(self->data, self->capacity);
|
||||
}
|
||||
for (int i = self->size - 1; i > index; i--) {
|
||||
self->data[i] = self->data[i - 1];
|
||||
}
|
||||
@@ -135,22 +153,16 @@ int str_remove(String_t* self, int index) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
/// splits a string into an array of strings based on a delimiter
|
||||
String_t* str_split(String_t* self, int* res_len, char c) {
|
||||
|
||||
char* string = self->data;
|
||||
String_t* elements = NULL;
|
||||
|
||||
// find the number of lines in the file
|
||||
|
||||
bool flag = false;
|
||||
*res_len = 0;
|
||||
for (int i = 0; i < strlen(string); i++) {
|
||||
|
||||
// Count number of splits
|
||||
for (int i = 0; i < self->size; i++) {
|
||||
if (string[i] == c) {
|
||||
// if (flag) { // this condition was here so that empty strings would be ignore
|
||||
(*res_len)++;
|
||||
flag = false;
|
||||
// }
|
||||
} else {
|
||||
flag = true;
|
||||
}
|
||||
@@ -159,20 +171,17 @@ String_t* str_split(String_t* self, int* res_len, char c) {
|
||||
(*res_len)++;
|
||||
}
|
||||
|
||||
// allocate memory for an array of pointers to each line
|
||||
elements = (String_t*)malloc((*res_len) * sizeof(String_t));
|
||||
|
||||
String_t* elements = (String_t*)malloc((*res_len) * sizeof(String_t));
|
||||
int i = 0;
|
||||
flag = false;
|
||||
char* start = string;
|
||||
char* end = string;
|
||||
char* string_end = string + self->size;
|
||||
|
||||
while (*end != '\0') {
|
||||
while (end < string_end) {
|
||||
if (*end == c) {
|
||||
// if (flag) {
|
||||
elements[i] = str_from_slice(start, end - start);
|
||||
i++;
|
||||
// }
|
||||
end++;
|
||||
start = end;
|
||||
flag = false;
|
||||
@@ -180,12 +189,10 @@ String_t* str_split(String_t* self, int* res_len, char c) {
|
||||
end++;
|
||||
flag = true;
|
||||
}
|
||||
} if (flag) {
|
||||
}
|
||||
if (flag) {
|
||||
elements[i] = str_from_slice(start, end - start);
|
||||
}
|
||||
|
||||
|
||||
// returns an array of String_t
|
||||
return elements;
|
||||
}
|
||||
|
||||
@@ -209,38 +216,32 @@ String_t* str_lines(String_t* self, int* numlines) {
|
||||
|
||||
String_t str_slice(String_t* s, int start, int end) {
|
||||
String_t s2;
|
||||
|
||||
if (start < 0) start = 0;
|
||||
if (end > s->size) end = s->size;
|
||||
|
||||
// if the range is invalid, we return an empty string
|
||||
if (start >= end || start >= s->size) {
|
||||
s2.size = 0;
|
||||
s2.capacity = 1;
|
||||
s2.data = (char*)calloc(1, sizeof(char));
|
||||
return s2;
|
||||
}
|
||||
|
||||
s2.size = end - start;
|
||||
s2.capacity = s2.size;
|
||||
|
||||
s2.data = (char*)calloc(s2.size, sizeof(char));
|
||||
|
||||
// if the allocation fails we return a zero sized string
|
||||
if (s2.data == NULL) {
|
||||
s2.size = 0;
|
||||
s2.capacity = 0;
|
||||
return s2;
|
||||
}
|
||||
|
||||
for (int i = 0; i < s2.size; i++) {
|
||||
s2.data[i] = s->data[start + i];
|
||||
}
|
||||
return s2;
|
||||
}
|
||||
|
||||
int str_len(String_t* s) {
|
||||
return s->size;
|
||||
}
|
||||
|
||||
char* to_chars(String_t* s) {
|
||||
return s->data;
|
||||
}
|
||||
|
||||
// int main() {
|
||||
// String_t s = str_from_chars("hello\nworld\neeeee\notherline\n");
|
||||
|
||||
// str_remove(&s, 10);
|
||||
// str_insert(&s, 10, 'h');
|
||||
|
||||
// printf("%s\n", to_chars(&s));
|
||||
|
||||
// int linen = 0;
|
||||
// String_t* lines = str_lines(&s, &linen);
|
||||
// for (int i=0; i < linen; i++) {
|
||||
// printf("%s", to_chars(&lines[i]));
|
||||
// }
|
||||
|
||||
// str_dealloc(&s);
|
||||
|
||||
// return 0;
|
||||
// }
|
||||
|
||||
Reference in New Issue
Block a user