D-ish way to work with strings?
Steven Schveighoffer
schveiguy at gmail.com
Sun Dec 22 18:45:52 UTC 2019
On 12/22/19 9:15 AM, Robert M. Münch wrote:
> I want to do all the basics mutating things with strings: append,
> insert, replace
>
> What is the D-ish way to do that since string is aliased
> to immutable(char)[]?
switch to using char[].
Unfortunately, there's a lot of code out there that accepts string
instead of const(char)[], which is more usable.
I think many people don't realize the purpose of the string type. It's
meant to be something that is heap-allocated (or as a global), and NEVER
goes out of scope. Many things are shoehorned into string which
shouldn't be.
> Using arrays, using ~ operator, always copying, changing, combining my
> strings into a new one? Does it make sense to think about reducing GC
> pressure?
It really depends on your use cases. strings are great precisely because
they don't change. slicing makes huge sense there.
> I'm a bit lost in the possibilities and don't find any "that's the way
> to do it".
Again, use char[] if you are going to be rearranging strings. And you
have to take care not to cheat and cast to string. Always use idup if
you need one.
If you find Phobos functions that unnecessarily take string instead of
const(char)[] please post to bugzilla.
-Steve
More information about the Digitalmars-d-learn
mailing list