← Back to context

Comment by FooBarWidget

12 years ago

Sigh, another string library. And this is the reason why I prefer C++ over C. You don't end up writing another string library for the 300th time. There aren't that many differences between string libraries anyway. Most of them are just structs with a pointer to a memory block, plus a length field.

> Sigh, another string library. And this is the reason why I prefer C++ over C. You don't end up writing another string library for the 300th time.

You say that, and yet every C++ project I've ever touched in my life has had its own string class with various levels of horror attached. My favorite was the one that stored everything internally as 32-bit characters to be Unicode safe, and was never used in a codebase that had to deal with Unicode.

Yep, but this one has not the usual layout of struct+pointer (that's the whole point), and I'm not sure it qualifies as "yet another" since it was written in 2006.

  • It appears to me to be "yet another" because the length before the string approach is usually referred to as a b-string.

    windows (for example) has had a comprehensive b-string library (type is called BSTR) for about 20 years - due to it's age and provenance it has the downside of thinking a character is a 16-bit value...

  • You're right, sorry. I only scanned the document, saw the struct, and closed the tab. The way you use a header before the data is similar to how Ruby represents its strings internally.

> And this is the reason why I prefer C++ over C. You don't end up writing another string library for the 300th time.

Ironically, in almost all of my uses of C++, I did end up writing or using a custom string library there too. (I was doing mostly console games or language interpreters.)

Yes, many people who have written much C have written some version of this library.

I'm not so happy with C++ string either. Main complaint is that I like the use of C strings as "semi-predicates"- you can test for NULL to indicate a failure. I wrote my own String class at one point to provide this feature:

  // These provide test if assigned/not assigned
  inline operator void*() const
    {
    return (void *)s;
    }

  inline bool operator!() const
    {
    if(s) return 0;
    else return 1;
    }

  • Better not to pun, and use an actual optional where you want it...

    • Eh, I like my C idioms. With C++ it's easy to make it safe so that accessing an unassigned string doesn't cause a crash (make NULL equivalent to empty strings except for tests).

      I have the same gripe with the way the STL is designed. Too tedious to test for empty first before reading an item.

      1 reply →

Not only that.

You also have x vector libraries for helping with bound checking in secure sensitive code and macro based generic data structures.

No thanks.