5 ms·
I unexpectedly did some cpp few days ago and I was surprised that cpp standard library doesn't have string trim function! Everybody is rolling their own. What i
by dvh 2y ago
I unexpectedly did some cpp few days ago and I was surprised that cpp standard library doesn't have string trim function! Everybody is rolling their own. What is the reason behind that?
- account42 2y agoWhat do you want to trim off? ASCII 0x20? Any ASCII white-space? Any Unicode white-space? Well the latter requires defined string encodings and depends on the Unicode version and you can't just use the latest without introducing subtle compatibility issues.
- deleted 2y ago[deleted]
- criddell 2y agoASCII white space (in any encoding) by default with an optional user defined set of trim characters (like Python) would probably solve the needs of 90% of people rolling their own.
- usrnm 2y agoNot all unicode whitespace characters take up exactly one byte when encoded in utf8. Not even talking about other possible encodings, just good old utf8. Let that sink in a bit, and you'll realize what a can of worms it is in a language where strings are just byte sequences.
- criddell 2y agoBecause it's tricky is exactly why it should be in the standard library. The C++ standard library should just incorporate ICU by reference IMHO.
- account42 2y agoICU is an unreasonably large dependency for something that many users won't need. Its behavior also changes with new Unicode versions which makes it incompatible with something that cares as much as backward compatibility as the C++ standard library.
- criddell 2y agoThat’s the nature of Unicode: it’s complicated and a moving target. As far as it being a large dependency, the beauty of C++ is that if you don’t use it, it won’t affect your build. If ICU is too large, complex, and unstable for the C++ committee, then regular users don’t stand a chance.
- account42 2y ago> As far as it being a large dependency, the beauty of C++ is that if you don’t use it, it won’t affect your build. That's the theory. In practice, you have things like iostreams pulling in tons of locale machinery (which is really significant for static builds) even if you never use a locale other than "C". That locale machinery will include gigantic functions for formatting monetary amounts even if you never do any formatting. > If ICU is too large, complex, and unstable for the C++ committee, then regular users don’t stand a chance. Regular users have more specific requirements and can handle binary compatibility breaks better if those aren't coupled with other unrelated functionality.
- tialaramex 2y agoI mean, you're a big grown-up language with generic programming, why can't you: https://doc.rust-lang.org/std/primitive.str.html#method.trim_end_matches https://doc.rust-lang.org/std/primitive.str.html#method.trim... C++ can't manage to do this because it doesn't give its primitive types methods, it doesn't have a sensible way to talk about methods on types, and it always coerces to function pointers... assert_eq!("123foo1bar123".trim_end_matches(char::is_numeric), "123foo1bar"); But it's pretty easy to at least do this: assert_eq!("11foo1bar11".trim_end_matches('1'), "11foo1bar"); (Yes Rust does provide one that always trims off trailing whitespace, but that requires that you know, as Rust does, what the encoding is)
- madduci 2y agoExactly, same as for base64 encoding, sha256/512 hashes and many more.
- eru 2y agoThe problem here isn't so much that it's not in the standard library (not everything needs to be in the standard library), but that everyone is rolling their own instead of using third party libraries.
- tialaramex 2y ago"Everybody" is the problem, there are two proper use cases for a standard library† 1. Vocabulary. Things Everybody will want to talk about. It's easier to communicate if we all agree this is a List<Goose> than if we first have to negotiate do we mean MyLinkedList or ArrayList or HybridStorage::List, and if we can't agree do we need an adaptor layer. Vocab is a reason the stdlib should provide a string type (if the language itself does not), a growable array, a hash table, etc. With generic programming you likely want some algorithms in here too, all & any, sum, that sort of thing. 2. Shared features Everybody will find they need and might otherwise screw up. Trimming trailing whitespace, turning numbers into strings and vice versa, sorting, basic arithmetic, familiar constants. This should be in category (2). "Everybody" will need this once in a while. † In C++ instead the standard library functions as a way to not bother with package management, this does have amusing effects like how FreeBSD will end up with a linear algebra library required to build the OS.
- eru 2y agoYes. How much you'll want to stick in the standard library and how much into other 3rd party libraries also depends on how easy it is to work with these 3rd party library. Two examples: Rust 3rd party library support is so strong, that its standard library itself is actually partially built on top of 3rd party libraries. Python, especially in the bad old days eg 20 years, didn't have much support for (sane) package management and dependency management, so it was really convenient to have a standard library with 'batteries included'.
- pjmlp 2y agoThing is, while C and C++ might be a late comers to the whole cross platform package management, vcpkg and conan are already quite established, what we are having is folks stuck in the old ways.
- jeroenhd 2y agoIn a similar vein, I found out that Go doesn't have a string reverse function either. Everyone online pretends reversing strings is easy (just iterate through the array backwards! The world is US ASCII only, right?). Trimming strings isn't hard in most real world applications, on the other hand, and not putting it in the standard library means people won't confuse the way the trim method works (i.e. the user must make a choice between copying memory or reusing memory and risking memory lifetime/consistency issues). And that doesn't even include problems like "what if the string isn't utf8". I'm more disappointed in Go, which takes a ton of questionable assumptions in the standard library to pretend difficult problems are easy. C++ wants to be correct and knowing what is or isn't whitespace is hard when you don't know the length of a single grapheme.
- zabzonk 2y agoSlightly OT: Interview question(s): "Write a function to reverse a string/linked list" Me, as interviewee: "You spend a lot of time reversing things, do you?" I don't understand why people are so obsessed with this kind of thing. In my entire career, I don't think I ever felt the need to reverse anything - iterate backwards, perhaps.
- pjmlp 2y agoIt is the closest to do a programmer casting. I would rather have that question, instead of how many golf balls fit into a plane. At least the former has something to do with programming.
- billforsternz 2y agoUsing my fingers I'm guessing a golf ball is about 3cm in diameter. A 737 or an A320 cabin is, again my guess/estimate, 30m long, 4m wide 2m high. So approx 30cm^3 into 200m^3. One million cm^3 in a m^3. I'm going with 5 million golf balls and hoping I'm right within an order of magnitude or so. I miss those kind of questions which have died out sadly.
- 2y ago
- pjmlp 2y agoIn C++ frameworks it exists for ages. Why not in ISO C++? Welcome to the ways of ISO and committee driven development, apparently no one cared enough to submit a paper, and do the work to win the paper voting into the standard.
- deleted 2y ago[deleted]