strfmarstime progress
I think it is warranted, otherwise I will forget later. This is part 1 of many on the journey to the strfmarstime function - which returns a formatted string of a broken down mars time structure.
This was my first time ever recreating a string formatting function - especially in C, so I learned some things along the way.
Basing off of musl
After some attempts, it became pretty clear that, although string formatting functions may seem easy, when you need to support width and padding specifiers, things get complicated.
Since I had to learn this, I decided to save time by basically recreating musl’s strftime implementation side by side. I would comment and give details on what is happening so I learned how it worked while also getting the result I need to, without just blatantly copying and pasting without giving credit.
This took about an hour since some of the variable names in musl’s implementation are like, ‘l’, ‘f’, and I had to keep it straight. I actually am writing this devlog after I spent 30+ minutes debugging some stack smashing that was happening, as a result of me putting in the wrong variable for string length into memcpy.
How it works
Basically, strftime/strfmarstime will first collect information needed for each format specifier, like if there is a character requesting padding. Then it will do each item at a time, with the given padding and width.
Here’s a fun thing I learned: There is a lot of “format++”. But format is a char*. How does that make sense?
Well, format++ actually is basically the same as doing .substring(1, format.length()) in other languages. I guess it makes sense if you think about a list of characters, and you are progressing to the next character. (Like a “cdr” of a “cons” box in functional programming).
So that helped.
Locales, locales
I wanted to come back to this later, especially because I will eventually need translations. But the %c specifier actually refers to the system locale’s date and time format. This required some hacking that came around the time I was stuck with that stack smashing. So seriously, I was stuck.
musl has it easy, it can tell by its own thread what the locale is. Here, we need to initialize locales (setlocale), and then make a locale_t object we can use with newlocale. Then we free it at the end.
Where I am now
Since the locale’s DT formatting is really just a shorthand for other specifiers, it returns “-” in its place. But as I continue forward, it will work.
Comments 1
Here are the commits that make up this devlog:
https://github.com/JPeisach/libspacetime/commit/c1ae4606564ff5493a2e849f329d23844932c95e
https://github.com/JPeisach/libspacetime/commit/c9cef377df9e71218e88abc705cc1631c1003d09
https://github.com/JPeisach/libspacetime/commit/eb71c19775a94fdb473f847d78e24a06fff53982
https://github.com/JPeisach/libspacetime/commit/4366cb0172bd44c40722448193b4fb1ab7fa2fad
https://github.com/JPeisach/libspacetime/commit/466c9f3ec122e0821862d7af92121a798c53e375
Sign in to join the conversation.