Most active commenters
  • o11c(3)

←back to thread

40 points coneonthefloor | 14 comments | | HN request time: 2.176s | source | bottom
1. o11c ◴[] No.44609974[source]
Hm, this implementation seems allergic to passing types by value, which eliminates half of the allocations. It also makes the mistake of being mutable-first, and provides some fundamentally-inefficient operations.

The main mistake that this makes in common with most string implementations make is to only provide a single type, rather than a series of mostly-compatible types that can be used generically in common contexts, but which differ in ways that sometimes matter. Ownership, lifetime, representation, etc.

replies(4): >>44610524 #>>44610702 #>>44611230 #>>44611895 #
2. remexre ◴[] No.44610524[source]
How would you recommend doing that sort of "subtyping"? _Generic and macros?
replies(1): >>44611063 #
3. amelius ◴[] No.44610702[source]
I wonder how an LLM would rate this code.
replies(1): >>44613250 #
4. o11c ◴[] No.44611063[source]
Yup. It's a lot saner in C++, but people who refuse to use C++ for political reasons can do it the ugly way using C11 or GNU C.
replies(2): >>44611135 #>>44613588 #
5. improgrammer007 ◴[] No.44611135{3}[source]
They even downvote people who suggest C++ :-). Doing this in C is such a colossal waste of time and energy, not to mention the bugs it'll introduce. Sigh!
replies(2): >>44611260 #>>44611802 #
6. zahlman ◴[] No.44611230[source]
> It also makes the mistake of being mutable-first

Is mutability not part of the point of having a string buffer? Wouldn't the corresponding immutable type just be a string?

replies(1): >>44611638 #
7. zahlman ◴[] No.44611260{4}[source]
Trolling about the choice of implementation language from a throwaway account is worth downvotes, yes. Doing a given task in a given language, simply for the sake of having it done in that language, is a legitimate endeavour, and having someone document (from personal experience) why it's difficult in that language is real content worth discussion. Choosing a better language is very much not a goal here.

> Please don't post shallow dismissals, especially of other people's work. A good critical comment teaches us something.

8. o11c ◴[] No.44611638[source]
"Buffer" just means it is used between input and output. It does not imply mutability, and many buffers indeed only take their state at construction time and are not mutable.

In my experience, the only functions a mutable string buffer needs to provide are "append string (or to-string-able)" and "undo that append" (which mostly comes up in list-like contexts, e.g. to remove a final comma); for everything else you can convert to an immutable string first.

(theoretically there might be a "split and clobber" function like `strtok`, but in my experience it isn't that useful once your APIs actually take a buffer class).

Considering the functions from this implementation, they can be divided as follows:

Lifetime methods:

  init
  free
  clear
Immutable methods:

  print
  index_of
  match_all
  split
Mutable methods:

  append
  prepend (inefficient!)
  remove
  replace
I've already mentioned `append`, and I suppose I can grant `prepend` for symmetry (though note that immutable strings do provide some sort of `concatenate`, though beware efficiency concerns). Immutable strings ubiquitously provide `replace` (and `remove` is just `replace` with an empty string), which are much safer/easier to use.

There are also a lot of common operations not provided here. And the ones that are provided fail to work with `StringBuffer` input.

9. vlovich123 ◴[] No.44611802{4}[source]
Following that argument, c++ is also a colossal waste of time and energy and bugs when compared with Rust :D.
replies(1): >>44613569 #
10. ◴[] No.44611895[source]
11. lelanthran ◴[] No.44613250[source]
> I wonder how an LLM would rate this code.

I dunno how it rates it now, but if this link: https://www.reddit.com/r/programming/comments/1m3dg0l/making... gets used for future training, future LLMs might make good suggestions for cleaning it up.

12. pjmlp ◴[] No.44613569{5}[source]
Eventually, when Rust finally catches up with C++ ecosystem, including being used in industry standards like Khronos APIs, CUDA, console devkits, HPC and HFT standards.

Until then, the choice is pretty much between C and C++, and the latter provides a much saner and safer alternative, than a language that keeps pretending to be portable macro assembler.

13. uecker ◴[] No.44613588{3}[source]
"political reasons"?

I switched from C++ to C because C++ is too complex and dealing with this complexity was stealing my time. I would not call this a "political reason".

replies(1): >>44615694 #
14. ◴[] No.44615694{4}[source]