handmade.network » Forums » Decent string lib for C?
Todd
113 posts

Code hacker/developer

#16533 Decent string lib for C?
1 month, 1 week ago Edited by Todd on Oct. 12, 2018, 6:14 a.m.

One of the things that C seems to be inherently weaker at is working with strings in a higher-level fashion. Python and similar languages have the .contains(), .split(), etc... functionality that C doesn't have by default and even if it's implemented, it tends to be more cumbersome to work with strings in C. Also the ability to do for s in my_strings { //do stuff} is very handy.

That all said, does anyone know of a good C string library or two? It's a bummer because I know C pretty well but I end up having to resort to other languages for small work projects just because it takes too long to get off the ground in C for these small utility projects that I often need to work on.

The downside to some of the other languages though is loss of power so I really would consider making some of these tools in C if I had access to better libraries that could help me get more done.

I'm the smartest man in Athens because I know that I know nothing. ~ Socrates
mmozeiko
Mārtiņš Možeiko
1826 posts / 1 project
#16534 Decent string lib for C?
1 month, 1 week ago Edited by Mārtiņš Možeiko on Oct. 12, 2018, 8:37 a.m.

From simpler, single/two file libraries:
Simple Dynamic Strings
Better String Library

A bit more header files, but more functionality:
µstr - Micro String API

For more universal strings (supports full unicode), but much more heavier:
ICU

Cranky
TM
13 posts
#16535 Decent string lib for C?
1 month, 1 week ago

As a general purpose string library rapidstring looks promising. It emulates the ease of std::string for C while being a lot faster at it. But it doesn't have contains/split etc (doesn't need to).

As to C not having contains/split, that is not necessarily true, it is just that the standard library of C doesn't have the most user friendly interface. You should look into the standard header string.h and its use case.

"Find" is strstr.
"Find a single char" is strchr.
"Find any of" is strpbrk.
"Find first not of" is kindof strspn, like this
1
str + strspn(str, " \t\v\f\r\n")
It would skip all whitespace.

"Contains" is just checking
1
strstr(haystack, needle) != NULL


Split is strtok. Not the best api for splitting, but it works. You can implement splitting easily with strspn/strpbrk too.

What the C libraries don't have is a way to reverse search most of the time. Like a way to find the last occurence of X in a str. The reason probably is, that there is no straightforward interface for such a function, since you would have to pass the length of the string into the function.
Telash
Mikael Johansson
94 posts / 1 project
#16536 Decent string lib for C?
1 month, 1 week ago

I have made my own string library, something I really recommend you to do.
Sure it takes a little while, but you will have it forever, and you just add the functionality you need for the current project. My string library follows me to every project I do. That way you will have a string library that does EXACTLY what you need it to.

The biggest obstacle to great software is lack of motivation. Motivate each other!
Instead of reinventing the wheel, we should put chariot wheels on jet planes!
godratio
Ray
53 posts
#16537 Decent string lib for C?
1 month, 1 week ago

Same here rolled my own never looked back. All to easy.