String Type Usage. String vs DString vs WString

Jonathan M Davis newsgroup.d at jmdavisprog.com
Mon Jan 15 02:36:02 UTC 2018


On Monday, January 15, 2018 02:22:09 Chris P via Digitalmars-d-learn wrote:
> On Monday, 15 January 2018 at 02:15:55 UTC, Nicholas Wilson wrote:
> > On Monday, 15 January 2018 at 02:05:32 UTC, Chris P wrote:
> >> [...]
> >>
> >  string == immutable( char)[], char == utf8
> >
> > wstring == immutable(wchar)[], char == utf16
> > dstring == immutable(dchar)[], char == utf32
> >
> > Unless you are dealing with windows, in which case you way need
> > to consider using wstring, there is very little reason to use
> > anything but string.
> >
> > N.B. when you iterate over a string there are a number of
> > different "flavours" (for want of a better term) you can
> > iterate over, bytes, unicode codepoints and graphemes ( I'm
> > possible forgetting some). have a look in std.uni and related
> > modules. Iteration in Phobos defaults to coepoints I think.
> >
> > TLDR use string.
>
> Thank you (and rikki) for replying. Actually, I am using Windows
> (Doh!) but I now understand. Cheers!

Even with Windows, there usually isn't any reason to use wstring. The only
reason that wstring might be more desirable on Windows is that you need
UTF-16 when dealing with the Windows API calls, and that's normally only
going to come up if you're not writing platform-independent code. The common
stuff such as file access is already wrap by Phobos (e.g. in std.file and
std.stdio), so most programs, don't need to worry about the Windows API
calls. And even if you do, the best practice generally is to use string
everywhere in your code and then only convert to a zero-terminated wchar*
when making the Windows API calls (either by actually allocating a
zero-terminated wchar* or using a static array with the appropriate wchar
set to 0, depending on the context).

If you have to do a ton with Windows API calls, at some point, it arguably
becomes better to just keep them as wstrings to avoid the conversions, but
even then, because strings in D aren't zero-terminated, and the C API calls
usually require them to be, you're often forced to copy the string to pass
it to a Windows API call anyway, in which case, you lose most of the benefit
of keeping stuff around in wstrings instead of just using strings
everywhere.

If you do need to worry about call a Windows API call, then check out toUTFz
in std.utf, since it will allow you to easily convert to zero-terminated
strings of any character type (std.string.toStringz handles zero-terminated
strings as well, but just for string).

- Jonathan M Davis



More information about the Digitalmars-d-learn mailing list