Skip to content

Commit

Permalink
Create a simple custom string library.
Browse files Browse the repository at this point in the history
  • Loading branch information
Nufflee committed Apr 23, 2020
1 parent 7561196 commit 657a1bf
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 0 deletions.
105 changes: 105 additions & 0 deletions src/libk/string.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
#include <stdbool.h>
#include <libc/string.h>
#include <libk/assert.h>
#include <kernel/kmalloc.h>
#include "string.h"

string *string_split(const_string str, const_string delimiter)
{
size_t length = strlen(str);
string *parts = kmalloc(length + 5);
string substr;
int count = 0;

for (size_t i = 0; i < strlen(str); i++)
{
substr = string_substring_range(str, i, length);

int pos = string_find(substr, delimiter);
kfree(substr);

// TODO: This a hackish and buggy in some cases. (For example: ',,,,' with ',')
if (pos == -1)
{
pos = length;
}

parts[count++] = string_substring(str, i, pos);

i += pos;
}

parts[count] = NULL;

return parts;
}

string string_normalize_spaces(const_string str)
{
string result = kmalloc(string_length(str));
char c = 0;
int r = 0;

for (size_t i = 0; i < string_length(result); i++)
{
while (c == ' ') //replaces two or more spaces by a single space
{
c = str[i++];
if (c != ' ')
result[r++] = c;
}
}

return result;
}

// TODO: Should use a better algorithm here.
int string_find(const_string str, const_string needle)
{
for (size_t i = 0; i < strlen(str); i++)
{
bool found = true;

for (size_t j = 0; j < strlen(needle); j++)
{
if (str[i] != needle[j])
{
found = false;

break;
}
}

if (found)
{
return i;
}
}

return -1;
}

string string_substring(const_string str, int start, int length)
{
ASSERT(length > 0);

string substr = kmalloc(length + 1);

memcpy(substr, &str[start], length);

substr[length] = '\0';

return substr;
}

string string_substring_range(const_string str, int start, int end)
{
ASSERT(start < end);

return string_substring(str, start, end - start);
}

size_t string_length(const_string str)
{
return strlen(str);
}
13 changes: 13 additions & 0 deletions src/libk/string.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#pragma once

#include <stddef.h>

typedef char *string;
typedef const char *const_string;

string *string_split(const_string str, const_string delimiter);
string string_normalize_spaces(const_string str);
int string_find(const_string str, const_string needle);
string string_substring(const_string str, int start, int length);
string string_substring_range(const_string str, int start, int end);
size_t string_length(const_string str);

0 comments on commit 657a1bf

Please sign in to comment.