Overview
I have been working on my own C library as a tangent before moving back to my own side-projects in C.
C as a whole as a lack of lots of little features that would make your life easy and your code more readable as a whole. Avoiding things like malloc/free, and pointers tends to be a good idea in general. Many people will look at the lack of certain features in C: dynamic arrays, generics, slices, optional arguments, etc. It's a bug or lacking feature of the language to them. In my personal opinion, This is what makes C to be C. It's an intentionally small language with little syntactical depth and a whole host of footguns. With the potential to do a lot of things with the power of manual memory management and pointer usage.
In general, I like to look at people with much more experience than me, so I look at the coding style of the Linux Kernel, Tigerbeetle, NASA, etc.
To see what could work and to find what knowledge they have
Here is a quote from Linus Torvalds after the release of Git 1.0 and what he thinks made it successful.
"Bad programmers worry about the code. Good programmers worry about data structures and their relationships." - Linus Torvalds
Different String Types
There has been a whole host of different blog-posts, ideas, and discussion around just using length based strings in C.
It can be easily done with
typedef struct {
u8 * data;
size_t len;
} str8;
str8 str = str("Hello, World");
printf("%*s", str.len, str.data);Notice that because we aren't using a null-terminated string, we just instead use the '*' character before the 's' to tell printf to use a length for the string.
After that you could then make a linked-list or array of these structs to have a collection of strings.
My Implementation
I have decided to instead use
typedef struct {
u8 data[CAP_SIZE];
size_t len, cap;
} strbuf_8;Then I can use the len and cap to par down on the needed calculations for when manipulating the string.
It's also easy to do (data + offset, end) to find a slice of that buffer using a strview type like
typedef struct {
const u8 * data;
size_t len;
} strview;strbufs are nice because I can essentially treat them as ring-buffers or gap-buffers, or any kind of buffer type data-structure without having to modify the data-structure itself. Also notice that I tend to avoid using dynamic allocation when possible to avoid un-needed complexity.
If you want a good look at a well-developed string library, I would check out the RAD-Debuggers source code, specifically in thier base folder
CSV Parsing
I am working on a new CSV/delimiter parser that is backed by a strbuf that reads 4kb chunks of a file at a time, then classifies the data according to a given schema and checks them. I was inspired by Jansson for JSON-Parsing so doing something like
csv_parse(&buf, "s-s-f-b")Would be the goal in general, where 's-s-f-b' denotes that I want return types (string-string-float-boolean).
My main idea for returning information currently is that I will have an array of node objects that has a union of the types I want.
then just walk the array or list and do something like f32 interest = csv_return_from_float() to get the values for the specific platform
The Tokens would be string-slices into the buffer that we would read and then convert rather than copying data in the first place.
Ini Parsing
I also noticed that I could potentially want a key-value like config file-format for denoting options in programs I write in the future. I have a couple of ideas for .ini parsing, but its currently blocked until I fully work out my associative-data structure.
One restriction or invariant that I could write is limiting the amount of sections or keys for every section, then just loading the entire file in one or two 4kb chunks.
Str-Map
I'm also trying to avoid having to hand-roll my own hash-table, why?
- Hashing can be slow
- Most of my use-cases will be a small pool of different strings
I have decided to go with a basic format where we have buckets delimited by the first char of the key being input
so something like
strmap_get(&map, "Jonathan");
will first get 'J' and see then walk all strings that start with 'J'. This is better than brute-forcing all the strings as we immediately jump down to a subset of the strings.
It also comes with the assumption that keys will tend to have different first characters more often than not. I'm fine with making that assumption.
I needed this data structure for work on my .ini parser, as my idea for the front-facing api is that we would do
IniGet(&map, SECTION, "ip-address");
Which would then return the Key that is stored in the section of the that part of the .ini file. This assumes that we would have to make our own enum for the sections that we want, also a fine trade-off for me.
Last Thoughts
As an addition, most of my C code tends to be using typedefs like u8 data, u16 data and others so I know the exact of amount of bits
and bytes that I am using for my variables. This makes explicitly reasoning about the size and structure of data much easier.
In general my two goals with the standard-library project for C is that I want basic handling of data on disk, and an easy way to have a simple api for retrieving data from csv or key-value formats without dealing with pointers or traditional C functions or parsing paradigms.
fgetc, puts, etc. Where the pattern is that you ingest one at a time.
I have a bucket-list of other data-structures and wrappers I want to make in the future as well
- GUI Framework wrapper for making cross-platform applications
- getopt wrapper for better arg-parsing
- radix tree, or tries
- graph structure
- Thanks for Reading!
zaigiaz