Jump to page: 1 2
Thread overview
How to reverse char[]?
Feb 08, 2012
H. S. Teoh
Feb 08, 2012
Timon Gehr
Feb 08, 2012
James Miller
Feb 08, 2012
Jonathan M Davis
Feb 08, 2012
Jos van Uden
Feb 08, 2012
Timon Gehr
Feb 08, 2012
H. S. Teoh
Feb 08, 2012
Jonathan M Davis
Feb 08, 2012
Manfred Nowak
Feb 08, 2012
Jonathan M Davis
Feb 08, 2012
H. S. Teoh
Feb 08, 2012
Jonathan M Davis
February 08, 2012
Hi all,

I'm trying to reverse a character array. Why doesn't the following work?

	import std.algorithm;
	void main() {
		char[] array = ['a', 'b', 'c'];
		reverse(array);
	}

I get:

Error: template std.algorithm.reverse(Range) if (isBidirectionalRange!(Range) && hasSwappableElements!(Range)) does not match any function template declaration
Error: template std.algorithm.reverse(Range) if (isBidirectionalRange!(Range) && hasSwappableElements!(Range)) cannot deduce template function from argument types !()(char[])


T

-- 
Three out of two people have difficulties with fractions. -- Dirk Eddelbuettel
February 08, 2012
On 02/08/2012 02:29 AM, H. S. Teoh wrote:
> Hi all,
>
> I'm trying to reverse a character array. Why doesn't the following work?
>
> 	import std.algorithm;
> 	void main() {
> 		char[] array = ['a', 'b', 'c'];
> 		reverse(array);
> 	}
>
> I get:
>
> Error: template std.algorithm.reverse(Range) if (isBidirectionalRange!(Range)&&  hasSwappableElements!(Range)) does not match any function template declaration
> Error: template std.algorithm.reverse(Range) if (isBidirectionalRange!(Range)&&  hasSwappableElements!(Range)) cannot deduce template function from argument types !()(char[])
>
>
> T
>

char[] is handled by Phobos as a range of dchar, ergo it does not have swappable elements. Apparently there is no template specialisation of 'reverse' that handles narrow strings, you might want to file an enhancement request.
February 08, 2012
> On 02/08/2012 02:29 AM, H. S. Teoh wrote:
>>
>> Hi all,
>>
>> I'm trying to reverse a character array. Why doesn't the following work?
>>
>>        import std.algorithm;
>>        void main() {
>>                char[] array = ['a', 'b', 'c'];
>>                reverse(array);
>>        }
>>
>> I get:
>>
>> Error: template std.algorithm.reverse(Range) if
>> (isBidirectionalRange!(Range)&&  hasSwappableElements!(Range)) does not
>> match any function template declaration
>> Error: template std.algorithm.reverse(Range) if
>> (isBidirectionalRange!(Range)&&  hasSwappableElements!(Range)) cannot deduce
>> template function from argument types !()(char[])
>>
>>
>> T
>>
>
> char[] is handled by Phobos as a range of dchar, ergo it does not have swappable elements. Apparently there is no template specialisation of 'reverse' that handles narrow strings, you might want to file an enhancement request.

That seems correct, the `reverse' function tests for `isBidirectionalRange' and `hasSwappableElements'

The following code shows the results

import std.range;
import std.stdio;
import std.conv;

void main() {
    char[] char_array = ['a','b','c'];
    ubyte[] ubyte_array = ['a','b','c'];

    writefln("isBidirectonalRange char_array:\t%s",
        to!string(isBidirectionalRange!(typeof(char_array))));
    writefln("isBidirectonalRange ubyte_array:\t%s",
        to!string(isBidirectionalRange!(typeof(ubyte_array))));

    writefln("hasSwappableElements char_array:\t%s",
        to!string(hasSwappableElements!(typeof(char_array))));
    writefln("hasSwappableElements ubyte_array:\t%s",
        to!string(hasSwappableElements!(typeof(ubyte_array))));

}

The output is


isBidirectonalRange char_array:	true
isBidirectonalRange ubyte_array:	true
hasSwappableElements char_array:	false
hasSwappableElements ubyte_array:	true

So if just just need an array of bytes and the `char' semantics are unimportant, then you can just use a ubyte instead. However Timon is correct that there should probably be a narrow string version of `reverse'.

James Miller
February 08, 2012
On Wednesday, February 08, 2012 02:36:23 Timon Gehr wrote:
> char[] is handled by Phobos as a range of dchar, ergo it does not have swappable elements. Apparently there is no template specialisation of 'reverse' that handles narrow strings, you might want to file an enhancement request.

There already is such an overload in HEAD.

- Jonathan M Davis
February 08, 2012
On 8-2-2012 2:36, Timon Gehr wrote:

> char[] is handled by Phobos as a range of dchar, ergo it does not have
> swappable elements.

I'm surprised that array.reverse does work (using 2.057)

February 08, 2012
On Wed, 08 Feb 2012 04:30:04 -0500, Jos van Uden <user@domain.invalid> wrote:

> On 8-2-2012 2:36, Timon Gehr wrote:
>
>> char[] is handled by Phobos as a range of dchar, ergo it does not have
>> swappable elements.
>
> I'm surprised that array.reverse does work (using 2.057)

array.reverse is *not* the same as reverse(array).  The former is a compiler-builtin property for all arrays (the compiler believes that anything of the form T[] is an array, even if it's a narrow-width string type), and the latter is a range function.

D will continue to trip over itself and fall into newbies until it makes a decision to make strings not also be arrays.

-Steve
February 08, 2012
On 02/08/2012 03:56 PM, Steven Schveighoffer wrote:
> On Wed, 08 Feb 2012 04:30:04 -0500, Jos van Uden <user@domain.invalid>
> wrote:
>
>> On 8-2-2012 2:36, Timon Gehr wrote:
>>
>>> char[] is handled by Phobos as a range of dchar, ergo it does not have
>>> swappable elements.
>>
>> I'm surprised that array.reverse does work (using 2.057)
>
> array.reverse is *not* the same as reverse(array). The former is a
> compiler-builtin property for all arrays (the compiler believes that
> anything of the form T[] is an array,

That is obviously the case. It is just that functions taking eg. char[] often have an in-contract that the array contains a valid utf-8 string.

> even if it's a narrow-width string
> type),  and the latter is a range function.
>

Luckily, array.reverse is going away. Anyway, note that char[].reverse reverses unicode code points, not code units.

> D will continue to trip over itself and fall into newbies until it makes
> a decision to make strings not also be arrays.
>
> -Steve

When I was a newbie, I liked the design of D strings. Anyway it is not the case that strings are _also_ arrays. D strings are arrays.
February 08, 2012
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.


T

-- 
Change is inevitable, except from a vending machine.
February 08, 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
February 08, 2012
On Wed, Feb 08, 2012 at 08:32:32AM -0800, Jonathan M Davis wrote: [...]
> 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.
[...]

Except that the point of my code was to fix byte-order so that they can be correctly interpreted. I suppose I really should be using ubyte[] for that instead, and perhaps use a union to translate it to char[] when I call decode().


T

-- 
Ph.D. = Permanent head Damage
« First   ‹ Prev
1 2