git.m455.casa

utils.h

clone url: git://git.m455.casa/utils.h


README.md

utils.c

a little, probably never-finished, c89 utility library.

backstory

originally, this started off as an exercise to learn string manipulation and manual memory management by writing a string replacement function, but then i added a string slice function, so i've decided to turn this into a repository for learning C, and hopefully build up some fun C functions that will hopefully be used to make a static website generator in the future.

i attempted to teach myself good practices in C by using random static code checkers i found from internet searches, and valgrind on the produced binary, to look for memory leaks.

i chose c89 because it has neat limitations, and (i think) it requires you to be extra safe when programming, due to its lack of safe functions (well, according to complaints on the internet about how easy it is to make unknown mistakes related to memory).

i didn't choose c89 because of portability, or because i think it's portable (it seems hard to write portable c code, so i just say fuck it, and don't worry about it), but more so because i think it's neat to program in a language that usually has a compiler available on most unix-like systems.

my makefile is probably terrible, because i use makefiles more as a command-line argument handler, where i can just use them to run single-argument commands, rather than software that checks if my source code changed or not before compiling lol.

i have no clue what i'm doing, but i'm definitely having fun.

compiling

use whatever c compiler you have and run make.

only make check requires you to have additional dependencies.

running code and binary checks

you'll need the following software installed to run checks:

on debian i've installed them with the following command:

sudo apt install gcc clang valgrind

then run make check to use all of the static and binary checks i've added in the makefile.

function reference

this section provides very lazy documentation of each of the functions in this library.

strrep

returns a string with all occurences of a string replaced with another string.

static char *strrep(char *str, char *from_str, char *to_str)

parameters

return values

example

the codeblock below provides an example of how you can replace the word "dog" with "person" in a string:

char *strrep_result;

strrep_result = strrep("hey i'm a dog. are you a dog too?", "dog", "person");
if (strrep_result == NULL) {
    perror("main() strrep() returned NULL");
    return 1;
}

printf("%s\n", strrep_result);

free(strrep_result);

strslice

takes a string and returns an identical string, only containing the characters between two indices of the original string.

static char *strslice(char *str, size_t start, size_t end)

parameters

return values

example

the codeblock below provides an example of how you can return a string that contains characters from the start of the original string to, and including, the second last character of the string:

char *strslice_result;

strslice_result = strslice("hello", 0, strlen("hello") - 1);
if (strslice_result == NULL) {
    (void) puts("main() strslice() returned NULL");
    return 1;
}

printf("%s\n", strslice_result);

free(strslice_result);