How to reverse char[]?

Jonathan M Davis jmdavisProg at gmx.com
Wed Feb 8 08:32:32 PST 2012


On Wednesday, February 08, 2012 07:39:44 H. S. Teoh wrote:
> On Wed, Feb 08, 2012 at 09:56:17AM -0500, Steven Schveighoffer wrote:
> [...]
> 
> > D will continue to trip over itself and fall into newbies until it
> > makes a decision to make strings not also be arrays.
> 
> [...]
> 
> I disagree. D will continue to trip over itself until it treats all
> arrays equally, that is, if reverse() works on ubyte[], then it should
> also work on char[]. There's nothing wrong with treating a string as an
> array. After all, "string" means "string of characters", i.e., an array.

Except that char[] is _not_ an array of characters. It's an array of code 
units. There is a _big_ difference. Not even dchar[] is an array of characters. 
It's both an array of code units and an array of code points, but not even 
that quite gets you characters (though at this point, Phobos pretty much 
treats a code point as if it were a character). If you want a character, you 
need a grapheme (which could be multiple code points). _That_ is where the 
problem comes in.

You can definitely do array operations on strings. In fact, it can be very 
desirable to do so if you want to process strings efficiently. But if you treat 
them like you would ubyte[], you're in for a heap of trouble thanks to how 
unicode works.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list