Good C string library

I would suggest not using any library aside from malloc, free, strlen, memcpy, and snprintf. These functions give you all of the tools for powerful, safe, and efficient string processing in C. Just stay away from strcpy, strcat, strncpy, and strncat, all of which tend to lead to inefficiency and exploitable bugs.

Since you mentioned searching, whatever choice of library you make, strchr and strstr are almost certainly going to be what you want to use. strspn and strcspn can also be useful.


It's an old question, I hope you have already found a useful one. In case you didn't, please check out the Simple Dynamic String library on github. I copy&paste the author's description here:

SDS is a string library for C designed to augment the limited libc string handling functionalities by adding heap allocated strings that are:

  • Simpler to use.
  • Binary safe.
  • Computationally more efficient.
  • But yet... Compatible with normal C string functions.

This is achieved using an alternative design in which instead of using a C structure to represent a string, we use a binary prefix that is stored before the actual pointer to the string that is returned by SDS to the user.

+--------+-------------------------------+-----------+
| Header | Binary safe C alike string... | Null term |
+--------+-------------------------------+-----------+
         |
         `-> Pointer returned to the user.

Because of meta data stored before the actual returned pointer as a prefix, and because of every SDS string implicitly adding a null term at the end of the string regardless of the actual content of the string, SDS strings work well together with C strings and the user is free to use them interchangeably with real-only functions that access the string in read-only.


If you really want to get it right from the beginning, you should look at ICU, i.e. Unicode support, unless you are sure your strings will never hold anything but plain ASCII-7... Searching, regular expressions, tokenization is all in there.

Of course, going C++ would make things much easier, but even then my recommendation of ICU would stand.

Tags:

C

String

Io

Search