News for hackers. Stuff that matters.
Re:Str: Yet another string library for C language by azhenley on Wednesday November 25, 2020 @03:55PM (#25214193)
Every time I think to use C for something, I re-realize how terrible it is to do anything involving strings. Although this library looks nice, I’ll still have to compose and manage them myself , which is a major headache.
Re:Str: Yet another string library for C language by WalterBright on Wednesday November 25, 2020 @04:12PM (#25214348)
When I review C code, I look for strncpy, etc., and give them special attention. There's always a bug or two in it.

0-terminated strings not only have proven to be a rich source of bugs, they're remarkably inefficient as well [1]. Doing better was a major focus of the initial design of D.

[1] This is because of constantly scanning to get the length (which also necessitates reloading the string contents into the memory cache), and having to make copies of strings instead of just slicing them.

Re:Str: Yet another string library for C language by aidenn0 on Wednesday November 25, 2020 @04:33PM (#25214546)
Well you don't have to make copies of strings to slice them, just ask strtok! </s>
Re:Str: Yet another string library for C language by abqexpert on Wednesday November 25, 2020 @05:29PM (#25215002)
If you wanted to slice at an arbitrary point, then you would either have to lose some data in the original string, move or copy the original string to make space for the extra delimiter/null character, or have set up the string ahead of time to contain the delimiter in the desired position. If you are using strtok.
Re:Str: Yet another string library for C language by souprock on Thursday November 26, 2020 @12:28AM (#25217144)
There is also the mangle-use-repair choice. I've done that with pathnames for creating nested directories.

C programmers are expected to make the best choice based on the situation. The various choices trade off memory usage, CPU usage, source code readability, and program correctness.

Re:Str: Yet another string library for C language by account42 on Thursday November 26, 2020 @07:45AM (#25219522)
> There is also the mangle-use-repair choice.

Which is problematic for thread safety and depending on the source of the string (constant) may not be possible.

Re:Str: Yet another string library for C language by souprock on Friday November 27, 2020 @06:31PM (#25233539)
It's not problematic. C programmers are expected to avoid screwing that up. C is a full-power language.

If available, strdupa() would be a fine way to get a suitable local copy of the string. Commonly though, the programmer knows that there will not be threads and can make the string non-constant.

Re:Str: Yet another string library for C language by codezero on Wednesday November 25, 2020 @09:08PM (#25216244)
I encountered Hollerith constants in an ancient Fortran codebase I worked on and was thrilled to see folks were doing clever stuff with strings in the 60s.

I wonder how much time was wasted in early computing (maybe not wasted really) because of the fear of incompatibility that is getting smaller and smaller as computing platforms coalesce into standardized-ish things.

Watching the M1 roll out and how it doesn't seem to care much that x86 is a thing and gets along with its life has been fascinating.

Re:Str: Yet another string library for C language by pansa2 on Wednesday November 25, 2020 @05:15PM (#25214883)
How is the support for strings in C++? Presumably better than in C, but is it good enough when compared to other compiled languages - Go, Rust etc?
Re:Str: Yet another string library for C language by WalterBright on Wednesday November 25, 2020 @05:36PM (#25215064)
D uses "phat pointers" for strings, aka a length/pointer pair. Over the years, this has proven to be simple, efficient, and resistant to errors. It means array bounds checking can be automatically done. It enables efficient slicing.

String literals also have an extra 0 appended, making it transparently easy to still pass strings to C functions like printf.

Re:Str: Yet another string library for C language by abqexpert on Wednesday November 25, 2020 @05:40PM (#25215094)
>"phat pointers"

I don't know if that is a typo given you normally call them "fat pointers", but they are "pretty hot and tempting".

Re:Str: Yet another string library for C language by __d on Wednesday November 25, 2020 @05:35PM (#25215038)
C++ has string support in the standard library.

It doesn't have the same breadth of features as, say, Python's string class, but it's ok.

See, eg. https://en.cppreference.com/w/cpp/string

Re:Str: Yet another string library for C language by emmanueloga_ on Thursday November 26, 2020 @02:40AM (#25217803)
In my opinion, still a bit hairy, the reason something like nowide exists [1].

1: https://www.boost.org/doc/libs/develop/libs/nowide/doc/html/...

Re:Str: Yet another string library for C language by kenniskrag on Wednesday November 25, 2020 @06:29PM (#25215464)
How is the support of unicode in c++?
Re:Str: Yet another string library for C language by PaulDavisThe1st on Wednesday November 25, 2020 @11:15PM (#25216747)
C++ itself doesn't support it. There are libraries that provide unicode-aware handling of strings/vectors of bytes. It's not always clear that you want unicode-aware code when dealing with unicode, but there are times when it is nice to have.
Re:Str: Yet another string library for C language by ansgri on Wednesday November 25, 2020 @05:37PM (#25215068)
It's exceedingly verbose, but decent, if you have a recent language version and/or Boost.
Re:Str: Yet another string library for C language by herodoturtle on Wednesday November 25, 2020 @06:12PM (#25215348)
Gosh this evoked a keen sense of nostalgia. I don't miss C strings at all. Along with segmentation fault, core dumped. Ruined many a night!
Re:Str: Yet another string library for C language by pcdoodle on Wednesday November 25, 2020 @04:08PM (#25214322)
I know. This and the fact that compliers never bitch about a single = in if() statements really take time out of my life...
Re:Str: Yet another string library for C language by WalterBright on Wednesday November 25, 2020 @05:04PM (#25214800)
Most C compilers will give a warning for that. D makes it an error in the grammar. `a < b < c` is also an error in the grammar.
Re:Str: Yet another string library for C language by thechao on Wednesday November 25, 2020 @09:58PM (#25216448)
I gave up trying to teach C after 2 years teaching it at university. You’ve been at it, what? 25+ years? Mad props. Thanks for great C/++ compilers, and double-thanks for D!