-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 335fe94
Showing
9 changed files
with
216 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
CC = gcc | ||
CCFLAGS = -O3 | ||
STRING_PATH = src/string | ||
|
||
all: $(STRING_PATH)/string.a | ||
$(CC) $(CCFLAGS) src/main.c -o main $^ | ||
|
||
.PHONY: string | ||
string: $(STRING_PATH)/string.a | ||
$(STRING_PATH)/string.a: $(STRING_PATH)/string.o | ||
ar rcs $(STRING_PATH)/string.a $^ | ||
$(STRING_PATH)/string.c: $(STRING_PATH)/string.h | ||
$(STRING_PATH)/string.o: $(STRING_PATH)/string.c $(STRING_PATH)/string.h | ||
$(CC) $(CCFLAGS) -c $< -o $(STRING_PATH)/string.o |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# Klib (Erik's C standard library) | ||
|
||
KLib is my own library to use with the C programming language | ||
Use are your own risk. Fully documented on the headers. |
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#include "string/string.h" | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
int main() | ||
{ | ||
// Create the string. | ||
String *string = string_create("Hello world"); | ||
printf("[Length: %d, Capacity: %d]:\n%s\n\n", string->length, string->capacity, string->text); | ||
|
||
// Add another text to the string. | ||
string_add(string, ", this is a nice example of dynamic strings"); | ||
printf("[Length: %d, Capacity: %d]:\n%s\n\n", string->length, string->capacity, string->text); | ||
|
||
// Pop the last value of the string. | ||
char pop = string_pop(string); | ||
printf("[Length: %d, Capacity: %d]:\n%s\n\n", string->length, string->capacity, string->text); | ||
|
||
// Push a new value at the end of the string. | ||
string_push(string, pop); | ||
printf("[Length: %d, Capacity: %d]:\n%s\n\n", string->length, string->capacity, string->text); | ||
|
||
// Add anothe string toghether. | ||
String *string2 = string_create(" yey!!!"); | ||
string_concat(string, string2); | ||
printf("[Length: %d, Capacity: %d]:\n%s\n\n", string->length, string->capacity, string->text); | ||
|
||
// Get a substring | ||
string_substring(string, 13, 34); | ||
printf("[Length: %d, Capacity: %d]:\n%s\n\n", string->length, string->capacity, string->text); | ||
|
||
// Delete the string memory. | ||
string_delete(string); | ||
|
||
return EXIT_SUCCESS; | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
#include "string.h" | ||
#include <string.h> | ||
#include <stdlib.h> | ||
|
||
static void reallocate_if_needed(String *string, size_t capacity) | ||
{ | ||
if (string->capacity < capacity) { | ||
// Increment the size of the capacity to a multiple of STRING_BLOCK_SIZE. | ||
do string->capacity += STRING_BLOCK_SIZE; | ||
while (string->capacity < capacity); | ||
// Reallocate the needed space. +1 for the null termination. | ||
string->text = (char *) realloc(string->text, sizeof(char) * string->capacity + 1); | ||
} else if (string->capacity > capacity) { | ||
// Decrement till it fits the minimun required space. | ||
while (string->capacity > capacity + STRING_BLOCK_SIZE) string->capacity -= STRING_BLOCK_SIZE; | ||
// Reallocate the needed space. +1 for the null termination. | ||
string->text = (char *) realloc(string->text, sizeof(char) * string->capacity + 1); | ||
} | ||
} | ||
|
||
String *string_create(char *text) | ||
{ | ||
// Allocate memory for the string. | ||
String *string = (String *) malloc(sizeof(String)); | ||
|
||
// Set the length of the string. | ||
string->length = strlen(text); | ||
|
||
// Set the capacity of the string. | ||
string->capacity = 0; | ||
while (string->capacity < string->length) string->capacity += STRING_BLOCK_SIZE; | ||
string->text = (char *) malloc(sizeof(char) * string->capacity + 1); | ||
|
||
// Set the text of the string. | ||
strcpy(string->text, text); | ||
|
||
return string; | ||
} | ||
|
||
void string_push(String *dest, char character) | ||
{ | ||
// Reallocate the capacity of the string if needed. | ||
reallocate_if_needed(dest, ++dest->length); | ||
|
||
// Push the extra character and add the null termination | ||
dest->text[dest->length - 1] = character; | ||
dest->text[dest->length] = '\0'; | ||
} | ||
|
||
char string_pop(String *dest) | ||
{ | ||
// Get the char to be poped. | ||
char pop = dest->text[--dest->length]; | ||
|
||
// Reallocate the capacity of the string if needed. | ||
reallocate_if_needed(dest, dest->length); | ||
|
||
// Set the null termination. | ||
dest->text[dest->length] = '\0'; | ||
|
||
return pop; | ||
} | ||
|
||
void string_add(String *dest, char *text) | ||
{ | ||
// Update the length of the string. | ||
dest->length += strlen(text); | ||
|
||
// Reallocate the capacity of the string if needed. | ||
reallocate_if_needed(dest, dest->length); | ||
|
||
// Append the given text to the string. | ||
strcat(dest->text, text); | ||
} | ||
|
||
void string_concat(String *dest, String *src) | ||
{ | ||
// Update the length of the string. | ||
dest->length += src->length; | ||
|
||
// Reallocate the capacity of the string if needed. | ||
reallocate_if_needed(dest, dest->length); | ||
|
||
// Append the given text to the string. | ||
strcat(dest->text, src->text); | ||
} | ||
|
||
void string_substring(String *dest, size_t start, size_t finish) | ||
{ | ||
// Calculate the result length of the substring. | ||
dest->length = finish - start + 1; | ||
|
||
// Allocate memory for the capacity +1 for null termination. | ||
char *substring = (char *) malloc(dest->length + 1); | ||
|
||
// Copy the string from the original to the substring. | ||
strncpy(substring, dest->text + start, dest->length); | ||
|
||
// Set the null termination. | ||
substring[dest->length] = '\0'; | ||
|
||
// Reallocate the capacity of the string if needed. | ||
reallocate_if_needed(dest, dest->length); | ||
|
||
// Set the result to the string | ||
strcpy(dest->text, substring); | ||
|
||
// Delete the substring memory | ||
free(substring); | ||
} | ||
|
||
void string_delete(String *dest) | ||
{ | ||
// Remove the text from the heap. | ||
free(dest->text); | ||
|
||
// Remove the string from the heap. | ||
free(dest); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#ifndef STRING_H | ||
#define STRING_H | ||
|
||
#define STRING_BLOCK_SIZE 20 | ||
|
||
#include <stdint.h> | ||
|
||
// This is the basic string structure | ||
// The methods are also documented. | ||
typedef struct { | ||
// It stores the basic text of the string. | ||
char *text; | ||
|
||
// It stores the length of the string. | ||
size_t length; | ||
|
||
// Stores the capacity of the string (the allocated char bytes of space). | ||
size_t capacity; | ||
} String; | ||
|
||
// Creates a new string given a text. | ||
String *string_create(char *text); | ||
|
||
// Pushes a character into the string. | ||
void string_push(String *dest, char character); | ||
|
||
// Pushes a character into the string. | ||
char string_pop(String *dest); | ||
|
||
// Adds a text to the string | ||
void string_add(String *dest, char *text); | ||
|
||
// Adds two strings together. | ||
void string_concat(String *dest, String *src); | ||
|
||
// Transforms the string into a substring. start and finish | ||
// are the indexes, both beeing inclusive. | ||
void string_substring(String *dest, size_t start, size_t finish); | ||
|
||
// Deletes the string and the text memory from the heap. | ||
void string_delete(String *dest); | ||
|
||
#endif |
Binary file not shown.