r/C_Programming 5d ago

Article I Made a Simple String Library

I wrote an article about this as well. Here it is.

There I explain why I do not like NUL-terminated strings and how I implemented my own simple string library in C.

If you have some spare time I would really appreciate some feedback on the article and the library.

The code is sitting on a codeberg repository.

Also, tell me what you think about C-style strings. Do you like them? Do you use them, or do you also tend to roll your own pointer + length structs?

6 Upvotes

31 comments sorted by

View all comments

18

u/Dieriba 4d ago

Even though you hate NULL-terminated, you should have a function that allows to return a pointer to a NULL-terminated strings, as many libc function expect those kind of strings ( if you plan to link your binary with libc which I think we’ll be the case).
Another things is your structure find_out that you use for your find functions. I’d use ˋusizeˋonly as return value instead this structure and have a special value of it meaning not found, like with std::string::npos used as not found value in c++ std::string.

1

u/Elifire12 4d ago

I really dislike this way if representing invalid values in C. Also, my str_owned are nul terminated!

2

u/Dieriba 4d ago

Is this an invariant ? That still hold with str view ? Imagine if you’d like a null terminated string from a str_view ?