Re: enum to string

2009-03-13 Thread Ary Borenszweig

Lionello Lunesu wrote:


"Brad Roberts"  wrote in message 
news:alpine.deb.2.00.0903121755240.4...@bellevue.puremagic.com...
That said, I don't think this really helps the desired usecase much.  
It's

useful, don't get me wrong, but still requires code to build up the
bi-directional translations.  Or am I missing something?  Seems to be
happening to me a lot lately, so I'm very prepared to be wrong here too.
:)



You're not wrong :)
The problem is that the foreach variable is not evaluatable to a 
compile-time string. I don't know why, but I'll figure it out tonight.


I've also managed to convert an enum to an AssocArrayLiteralExp* (with 
the name/string as the key and the value/int as the value) but it seems 
that it cannot be foreached at compile time, even if it's a literal 
expression. But hell, I've spent about 1 hour browsing through dmd's 
code, so I'm pretty sure it's possible with a little more research.


Can you foreach at compile-time? I thought you could only do that in 
CTFE (or templates?). Maybe that's why it's not working. How do you do 
it to pragma msg the members of a struct?


I remember someone proposed "static foreach" some time ago...


Re: enum to string

2009-03-13 Thread Daniel Keep


Ary Borenszweig wrote:
> Lionello Lunesu wrote:
>>
>> "Brad Roberts"  wrote in message
>> news:alpine.deb.2.00.0903121755240.4...@bellevue.puremagic.com...
>>> That said, I don't think this really helps the desired usecase much. 
>>> It's
>>> useful, don't get me wrong, but still requires code to build up the
>>> bi-directional translations.  Or am I missing something?  Seems to be
>>> happening to me a lot lately, so I'm very prepared to be wrong here too.
>>> :)
>>
>>
>> You're not wrong :)
>> The problem is that the foreach variable is not evaluatable to a
>> compile-time string. I don't know why, but I'll figure it out tonight.
>>
>> I've also managed to convert an enum to an AssocArrayLiteralExp* (with
>> the name/string as the key and the value/int as the value) but it
>> seems that it cannot be foreached at compile time, even if it's a
>> literal expression. But hell, I've spent about 1 hour browsing through
>> dmd's code, so I'm pretty sure it's possible with a little more research.
> 
> Can you foreach at compile-time? I thought you could only do that in
> CTFE (or templates?). Maybe that's why it's not working. How do you do
> it to pragma msg the members of a struct?
> 
> I remember someone proposed "static foreach" some time ago...

We have a sort-of static foreach.  The trick is that the aggregate HAS
to be a tuple.  When in doubt, you can always fall back on the following
construct:

template Tuple(T...)
{
alias T Tuple;
}

template Range(int n)
{
static if( n <= 0 )
alias Tuple!() Range;
else
alias Tuple!(Range!(n-1), n-1) Range;
}

void blah()
{
// Note that static foreach ONLY works inside a function
foreach( i ; Range!(n) )
{
// do stuff with i, which should be a const int
}
}

There have been times when directly accessing some CT construct would
make the compiler choke, but going via an index worked fine.

-- Daniel


recognizing asciiz, utf ...

2009-03-13 Thread newbee
Hi all,

How does one check for asciiz, utf ...?
I do get a buffer with characters as parameter in a function, but i don’t know 
if it is asciiz or utf or wchar. Is it possible to find out in dmd1 and dmd2?

Any help is appreciated.




Re: recognizing asciiz, utf ...

2009-03-13 Thread Jarrett Billingsley
On Fri, Mar 13, 2009 at 3:04 PM, newbee  wrote:
> Hi all,
>
> How does one check for asciiz, utf ...?
> I do get a buffer with characters as parameter in a function, but i don’t 
> know if it is asciiz or utf or wchar. Is it possible to find out in dmd1 and 
> dmd2?
>
> Any help is appreciated.

How are you getting this buffer?  What type is it, char[]?  D strings
are supposed to be Unicode, always.  If you read the data in from a
file, there's little to no guarantee as to what encoding it is (unless
it started with a Unicode BOM).

If you have a zero-terminated char* that a C function gives you, you
can turn it into a D string with std.string.toString (Phobos) or
tango.stdc.stringz.fromStringz (Tango).


Re: recognizing asciiz, utf ...

2009-03-13 Thread newbee
Jarrett Billingsley Wrote:

> On Fri, Mar 13, 2009 at 3:04 PM, newbee  wrote:
> > Hi all,
> >
> > How does one check for asciiz, utf ...?
> > I do get a buffer with characters as parameter in a function, but i don’t 
> > know if it is asciiz or utf or wchar. Is it possible to find out in dmd1 
> > and dmd2?
> >
> > Any help is appreciated.
> 
> How are you getting this buffer?  What type is it, char[]?  D strings
> are supposed to be Unicode, always.  If you read the data in from a
> file, there's little to no guarantee as to what encoding it is (unless
> it started with a Unicode BOM).
> 
> If you have a zero-terminated char* that a C function gives you, you
> can turn it into a D string with std.string.toString (Phobos) or
> tango.stdc.stringz.fromStringz (Tango).


i get it from a tcp buffer and do not know in advace if it is char[], asciiz or 
wchar. is it possible to check for that?


Re: recognizing asciiz, utf ...

2009-03-13 Thread Daniel Keep


newbee wrote:
> Jarrett Billingsley Wrote:
> 
>> On Fri, Mar 13, 2009 at 3:04 PM, newbee  wrote:
>>> Hi all,
>>>
>>> How does one check for asciiz, utf ...?
>>> I do get a buffer with characters as parameter in a function, but i don�t 
>>> know if it is asciiz or utf or wchar. Is it possible to find out in dmd1 
>>> and dmd2?
>>>
>>> Any help is appreciated.
>> How are you getting this buffer?  What type is it, char[]?  D strings
>> are supposed to be Unicode, always.  If you read the data in from a
>> file, there's little to no guarantee as to what encoding it is (unless
>> it started with a Unicode BOM).
>>
>> If you have a zero-terminated char* that a C function gives you, you
>> can turn it into a D string with std.string.toString (Phobos) or
>> tango.stdc.stringz.fromStringz (Tango).
> 
> 
> i get it from a tcp buffer and do not know in advace if it is char[], asciiz 
> or wchar. is it possible to check for that?

If you're getting data from a network connection and you have no idea
what it is, then the language certainly isn't going to help you with that.

Perhaps reading the documentation for the network protocol is in order? :P

  -- Daniel


lvalue - opIndexAssign - Tango

2009-03-13 Thread The Anh Tran

Hi,

When porting from c++ to D, i encounter this strange discrimination:
1. Built-in AA:
int[int] arr;
arr[123] += 12345;
arr[321]++;

2. Tango HashMap:
auto hm = new HashMap!(int, int)();
hm[123] += 12345; // error not lvalue
hm[123]++;  // error

D document says current opIndexAssign does not work as lvalue. But why 
can builtin AA can that? How can i copy builtin AA behaviour?


--

Forgive my noob, where is the place to ask question, report bug for Tango?

1. I can't compile D code using tango hashmap in debug mode:

import tango.util.container.HashMap;
void main()
{
auto hm = new HashMap!(uint, uint)();
}

> dmd -w -g -debug  hello.d // error

2. Compile D code using Tango Regex by GDC emit lots of link errors.

3. Bug in Tango atomicIncrement, atomicDecrement:

int task_done = 0;
atomicIncrement(task_done);

That function is compiled in asm:
lock inc byte ptr[task_done];
Which is wrong. It'll wrap to 0 at 255.
It should be: lock inc dword ptr[task_done];

4. There is no atomicAdd(ref original, int newvalue) family. GCC 
equivalence is __syn_fetch_and_add ...


Re: lvalue - opIndexAssign - Tango

2009-03-13 Thread Daniel Keep


The Anh Tran wrote:
> Hi,
> 
> When porting from c++ to D, i encounter this strange discrimination:
> 1. Built-in AA:
> int[int] arr;
> arr[123] += 12345;
> arr[321]++;
> 
> 2. Tango HashMap:
> auto hm = new HashMap!(int, int)();
> hm[123] += 12345; // error not lvalue
> hm[123]++;// error
> 
> D document says current opIndexAssign does not work as lvalue. But why
> can builtin AA can that? How can i copy builtin AA behaviour?

You can't.  This is a hole in the language at the moment, hopefully
solved by the introduction of ref returns (but that's in D 2.0 which you
don't want to use at the moment.)

> Forgive my noob, where is the place to ask question, report bug for Tango?

You could try the Tango IRC channel:

irc://irc.freenode.org/#d.tango

That, or the Tango forums:

http://dsource.org/projects/tango/forums

You can report problems with Tango via the ticket system:

http://dsource.org/projects/tango/report ("New Ticket" is down the
bottom of the page.)

> 1. I can't compile D code using tango hashmap in debug mode:
> 
> import tango.util.container.HashMap;
> void main()
> {
> auto hm = new HashMap!(uint, uint)();
> }
> 
>> dmd -w -g -debug  hello.d // error

When posting problems with compiling something, it helps to mention the
version of the compiler you're using, your platform, the version of
Tango (in this case) and what the error actually is.

> 2. Compile D code using Tango Regex by GDC emit lots of link errors.
> 
> 3. Bug in Tango atomicIncrement, atomicDecrement:
> 
> int task_done = 0;
> atomicIncrement(task_done);
> 
> That function is compiled in asm:
> lock inc byte ptr[task_done];
> Which is wrong. It'll wrap to 0 at 255.
> It should be: lock inc dword ptr[task_done];
> 
> 4. There is no atomicAdd(ref original, int newvalue) family. GCC
> equivalence is __syn_fetch_and_add ...

  -- Daniel


Re: lvalue - opIndexAssign - Tango

2009-03-13 Thread Denis Koroskin

On Sat, 14 Mar 2009 06:41:52 +0300, The Anh Tran  wrote:


Hi,

When porting from c++ to D, i encounter this strange discrimination:
1. Built-in AA:
int[int] arr;
arr[123] += 12345;
arr[321]++;

2. Tango HashMap:
auto hm = new HashMap!(int, int)();
hm[123] += 12345; // error not lvalue
hm[123]++;  // error

D document says current opIndexAssign does not work as lvalue. But why  
can builtin AA can that? How can i copy builtin AA behaviour?


--

Forgive my noob, where is the place to ask question, report bug for  
Tango?


1. I can't compile D code using tango hashmap in debug mode:

import tango.util.container.HashMap;
void main()
{
auto hm = new HashMap!(uint, uint)();
}

 > dmd -w -g -debug  hello.d // error

2. Compile D code using Tango Regex by GDC emit lots of link errors.

3. Bug in Tango atomicIncrement, atomicDecrement:

int task_done = 0;
atomicIncrement(task_done);

That function is compiled in asm:
lock inc byte ptr[task_done];
Which is wrong. It'll wrap to 0 at 255.
It should be: lock inc dword ptr[task_done];



This one is fixed long ago, try updating your Tango installation.

4. There is no atomicAdd(ref original, int newvalue) family. GCC  
equivalence is __syn_fetch_and_add ...