Rune strings. Like in Go.

H. S. Teoh hsteoh at quickfur.ath.cx
Thu Sep 30 22:57:42 UTC 2021


On Thu, Sep 30, 2021 at 10:47:25PM +0000, Alexey via Digitalmars-d wrote:
> On Thursday, 30 September 2021 at 20:57:34 UTC, Paul Backus wrote:
> > On Thursday, 30 September 2021 at 20:44:54 UTC, Alexey wrote:
> > > Can we have them in D? :)
> > 
> > We have them already. :) What Go calls a "rune" is called a `dchar`
> > in D, and a string of them is a `dchar[]`.
> 
> Go's runes and D's dchars - have different behavior.
> Go's rune string, being converted to byte array like so
> `[]byte(string_var)` and saved to file - results in UTF-8, while dchar
> is UTF-32.
> 
> this means - the frequent conversions between string and dstring in D
> is required for confortable work with unicode.

This is not true. D strings are autodecoded with Phobos range functions,
i.e., if you iterate over a string with Phobos, you will get a stream of
dchars without having to convert the encoding.

(Ironically enough, autodecoding is regarded as a bad thing!)

Also, IIRC, writing a stream of dchars to a string sink, e.g.,
appender!string, will automatically encode into UTF-8, so no explicit
conversion is needed afterwards.


T

-- 
If you look at a thing nine hundred and ninety-nine times, you are perfectly safe; if you look at it the thousandth time, you are in frightful danger of seeing it for the first time. -- G. K. Chesterton


More information about the Digitalmars-d mailing list