4 ms·
I was thinking I really need to research how to efficiently deal with strings in a certain language that doesn't allow them to be accessed as arrays of characte
by perl4ever 7y ago
I was thinking I really need to research how to efficiently deal with strings in a certain language that doesn't allow them to be accessed as arrays of characters. Because then I could get started on porting some C code to it.
- swsieber 7y agoHa ha. Rust? I suppose it depends on the context of what you're trying to do. Edit: if it is Rust most of the time you'll want to use character iterators/map/filter/etc. Then benchmark.
- trevyn 7y agoThe Rust docs have good sections on this: https://doc.rust-lang.org/book/ch08-02-strings.html https://doc.rust-lang.org/book/ch08-02-strings.html https://doc.rust-lang.org/std/primitive.char.html https://doc.rust-lang.org/std/primitive.char.html TLDR you can straightforwardly access as bytes, or iterate as "Unicode scalar values" as stored in the 4-byte "char" type. (But a "Unicode scalar value" is not exactly equal to the colloquial meaning of "character".) However: If you care about valid UTF-8 inputs not breaking the function or intent of your code, it's probably time to understand the problems Unicode is actually trying to address, and apply those considerations to what you're trying to do. :)
- steveklabnik 7y agoIf you’re talking about Rust, https://doc.rust-lang.org/stable/std/string/struct.String.html#method.as_bytes https://doc.rust-lang.org/stable/std/string/struct.String.ht... and related methods will give you byte slices. If not Rust, I’m sure the language has some sort of API!