3 ms·
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, an
by o11c 1y ago
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.
- remexre 1y agoHow would you recommend doing that sort of "subtyping"? _Generic and macros?
- o11c 1y agoYup. 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.
- improgrammer007 1y agoThey 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!
- zahlman 1y agoTrolling 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.
- vlovich123 1y agoFollowing that argument, c++ is also a colossal waste of time and energy and bugs when compared with Rust :D.
- pjmlp 1y agoEventually, 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.
- vlovich123 1y agoYou can bind to C and c++ libraries just fine. There’s plenty of Rust bindings for Vulkan and CUDA, the canonical implementation of WebGPU is in rust.
- pjmlp 1y agoBinding just fine isn't the same as taking part in the conversation of industry standards, and being shipped in vendor SDKs. Requires manual work from people willing to put into the effort, lesser development experience reading documents written for other languages, no out of the box plugins for IDEs, or graphical debuggers support, e.g. CUDA as one such example.
- uecker 1y ago"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".
- deleted 1y ago[deleted]
- pjmlp 1y agoC's simplicity is oversold, when taking into account that in practice it isn't the second edition of the K&R C book, adapted for ISO, rather a plethora of extensions, several standard revisions since 1989, zero care for security. Dealing with security issues is one of my occupations, when having a DevOps role in project delivery, and I rather use that time elsewhere.
- uecker 1y agoAfter wasting many hours of my life debugging C++ code, before switching to C, I disagree. The simplicity and explicitness of C is underappreciated.
- pjmlp 1y agoIf only it came along with a security mindset, unfortunately 50 years aren't enough to change language culture.
- uecker 1y agoAs if C++ had a security mindset: void foo(std::vector<int> &x) { x[4] = 1; } int main() { std::vector<int> x{ 0, 1, 3, }; foo(x); std::cout << x[4]; }
- pjmlp 1y agoThe difference being that all C++ compilers have an option to enable bounds checking on operator[](). Additionally, until C++26, the way the standard is written, the legalese doesn't forbid the implementation to do bounds checking. Now can you please enlighten us how to do the same with C arrays and strings, with bounds checking enabled, and why in 50 years WG14 has completely ignored the problem, including Dennis Ritchie proposal for fat pointers, until the government and industry pressure, and even now, I am quite curious if C2y will really bring any actual improvement.
- amelius 1y agoI wonder how an LLM would rate this code.
- lelanthran 1y ago> 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_a_stringbuffer_in_c_and_questioning_my/n3w8cwp/ https://www.reddit.com/r/programming/comments/1m3dg0l/making... gets used for future training, future LLMs might make good suggestions for cleaning it up.
- zahlman 1y ago> 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?
- o11c 1y ago"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.
- deleted 1y ago[deleted]