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.
Answer from Steinway Wu on Stack OverflowIt'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.
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.
Str: Yet another string library for C language
Decent string lib for C? | Handmade Network
What are the most commonly used & useful string functions in C?
GitHub - pmkenned/pmk_string: A simple string library in C
Videos
As there are a lot of string manipulation functions, what are note-worthy?
This was my weekend project.
This was just to have fun and learn and also, play with C.
I am working in another personal project, and need to use string.
This was based from: https://github.com/antirez/sds and https://github.com/mauro-balades/ultimate-string-library (https://www.reddit.com/r/C_Programming/comments/16hy3zv/my_own_take_on_making_a_new_c_string_library_is/)
I took the idea of the string length from Base128 encoding techniques, actually my first take I implemented a encoder and decoder, however I dropped in favor of something simpler
Basically, the first byte its the options.
I have an bitfield structure, where the first 3 bits, represent the string length in bytes. Maximum its 8 bytes(a lot of bytes)
THe next bit its to inform if the string its updatable(I would like to implement some memory protection later)
The next two bits, represent the encoding(ascii, utf8, utf16 and utf-32), I havent implemented this yet.
2 bits are reserved, maybe I will use this to set the library version, So, it can be compatible with later version where I reorder the 2th byte for example to be the last.
The following bytes, are the string length. If the string size its less than 256, we are going to use only one byte.
Lets say that we requested memory 7 bytes for the OS, and it returned the **region** `0x4052a0` to us.Lets say that we requested memory 7 bytes for the OS, and it returned the **region** `0x4052a0` to us.
| Memory Region | Value |
|---|---|
| 0x4052A0 | 0x09 |
| 0x4052A1 | 0x01 |
| 0x4052A2 | 0x48('H') |
| 0x4052A3 | 0x65('e') |
| 0x4052A4 | 0x6C('l') |
| 0x4052A5 | 0x6C('l') |
| 0x4052A6 | 0x6F('o') |
Github: https://github.com/sgtcortez/SimpleCompactString
Thank you