Skip to content

Commit

Permalink
Strings
Browse files Browse the repository at this point in the history
  • Loading branch information
ConsoleTVs committed Dec 30, 2018
0 parents commit 335fe94
Show file tree
Hide file tree
Showing 9 changed files with 216 additions and 0 deletions.
14 changes: 14 additions & 0 deletions Makefile
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
4 changes: 4 additions & 0 deletions Readme.md
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 added main.exe
Binary file not shown.
Binary file added ptime.exe
Binary file not shown.
36 changes: 36 additions & 0 deletions src/main.c
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 added src/string/string.a
Binary file not shown.
119 changes: 119 additions & 0 deletions src/string/string.c
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);
}
43 changes: 43 additions & 0 deletions src/string/string.h
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 added src/string/string.o
Binary file not shown.

0 comments on commit 335fe94

Please sign in to comment.