Is there a profiler for D2?

2011-09-20 Thread Cheng Wei
Is there any usable profiler for D2?


Re: Is there a profiler for D2?

2011-09-20 Thread Mirko Pilger

Is there any usable profiler for D2?


dmd -profile


Re: Is there a profiler for D2?

2011-09-20 Thread Jonathan M Davis
On Tuesday, September 20, 2011 08:35:52 Mirko Pilger wrote:
  Is there any usable profiler for D2?
 
 dmd -profile

I don't believe that it doesn't currently work with 64-bit binaries though, so 
if you want to run the profiler, you're going to need to do it with a 32-bit 
binary (which as all you can have if you're on Windows anyway, so whether 
that's an issue or not depends on your OS).

- Jonathan M Davis


Calling D code from C

2011-09-20 Thread Jonathan M Davis
Someone who has actually done a C or C++ application or two which used D code 
should answer this question. I know that there are at least a few folks around 
here who have done that, but I've never done it myself.

http://stackoverflow.com/questions/7480046/implementing-a-c-api-in-d


Re: A little puzzle

2011-09-20 Thread Regan Heath
On Mon, 19 Sep 2011 23:09:45 +0100, Simen Kjaeraas  
simen.kja...@gmail.com wrote:


On Mon, 19 Sep 2011 23:20:47 +0200, bearophile  
bearophileh...@lycos.com wrote:


A tiny puzzle I've shown on IRC. This is supposed to create an inverted  
array of cards, but what does it print instead?


import std.stdio, std.algorithm, std.range;
void main() {
int[52] cards;
copy(iota(cards.length - 1, -1, -1), cards[]);
writeln(cards);
}


Gawds, that's an ugly bug. For those who can't spot it,
typeof(cards.length) == uint, hence -1 is converted to a uint
(4_294_967_295, to be exact). iota then says 'that's fine, I'll just
return an empty range for you.'
Solution: knock some sense into integral promotion rules.
Workaround: cast(int)cards.length.

What was the rationale for having unsigned array lengths, again?


Well.. logically it makes sense as arrays cannot have negative lengths,  
but practically, this bug is what happens as a result.  This is the reason  
I typically use signed int for lengths, unless I expect the length to  
exceed max signed int.  It's also a good idea if you ever do any  
subtraction/calculation with them, as you don't want to underflow to max  
signed int ever.  It means that in my daily work I have to cast the return  
value of strlen() everywhere (cos my compiler complains, but only in 64  
bit mode..).. I mean, really, when are you going to have a string which is  
longer than max signed int?!  It's just nonsensical.


--
Using Opera's revolutionary email client: http://www.opera.com/mail/


const-immutable array argument?

2011-09-20 Thread bearophile
In this bug report I have asked for better error messages:
http://d.puremagic.com/issues/show_bug.cgi?id=6696

But beside the error message, do you know why an immutable ref can't be given 
to a function with a const ref argument? foo() can't change the contents of the 
array a any way, so what's wrong in this code?


void foo(const ref int[5] a) {}
void main() {
immutable int[5] arr;
foo(arr); // Error?
}

Bye,
bearophile


Re: const-immutable array argument?

2011-09-20 Thread Christophe
bearophile , dans le message (digitalmars.D.learn:29609), a écrit :
 what's wrong in this code?
 
 
 void foo(const ref int[5] a) {}
 void main() {
 immutable int[5] arr;
 foo(arr); // Error?
 }

I don't think it is wrong. Did you try changind the order of const and 
ref, or adding parenthesis ?


Re: const-immutable array argument?

2011-09-20 Thread bearophile
Christophe:

 I don't think it is wrong. Did you try changind the order of const and 
 ref, or adding parenthesis ?

I am trying now, and it seems the situation doesn't change.

Bye,
bearophile


Re: const-immutable array argument?

2011-09-20 Thread Kagamin
bearophile Wrote:

 In this bug report I have asked for better error messages:
 http://d.puremagic.com/issues/show_bug.cgi?id=6696
 
 But beside the error message, do you know why an immutable ref can't be given 
 to a function with a const ref argument? foo() can't change the contents of 
 the array a any way, so what's wrong in this code?
 
 
 void foo(const ref int[5] a) {}
 void main() {
 immutable int[5] arr;
 foo(arr); // Error?
 }

Looks like a bug. Seems like the compiler treats casted argument as an rvalue 
and you can't pass rvalue byref, can you?


Re: const-immutable array argument?

2011-09-20 Thread Steven Schveighoffer
On Tue, 20 Sep 2011 07:33:20 -0400, bearophile bearophileh...@lycos.com  
wrote:



In this bug report I have asked for better error messages:
http://d.puremagic.com/issues/show_bug.cgi?id=6696

But beside the error message, do you know why an immutable ref can't be  
given to a function with a const ref argument? foo() can't change the  
contents of the array a any way, so what's wrong in this code?



void foo(const ref int[5] a) {}
void main() {
immutable int[5] arr;
foo(arr); // Error?
}


The complaint from the compiler is that cast(const(int[5u])) arr is not an  
lvalue.


So apparently, the compiler wants to *copy* arr (via cast), then ref that.

This smells like a bug, but could potentially be intended behavior.

There is a workaround, but it's UGLY and dangerous:

foo(*(cast(const(int[5])*)arr));

I think the rules for implicit casting need to take into account whether  
it can use this kind of rewrite.


I'd file a bug, but note that it could possibly be an enhancement.

BTW, there are some funky things that happen with an immutable storage  
class.  For example:


void foo(ref const(int) a) {}
void bar(immutable int a) { foo(a); }
void main()
{
   immutable int a = 5;
   foo(a); // error, see below
   bar(a); // ok
}

The error for the foo(a) line is:
Error: constant 5 is not an lvalue

So the compiler is apparently replacing a with an enum.  But if it's a  
parameter, it gets storage on the stack.  That doesn't seem right.  I  
thought immutable data was supposed to live where you declare it, and  
that's why you use enum to force it to be a compile-time constant.


The same trick does not work with a fixed-size array, so I think  
definitely the original issue is a bug (behavior should be consistent with  
int).


-Steve


Re: const-immutable array argument?

2011-09-20 Thread Steven Schveighoffer
On Tue, 20 Sep 2011 07:33:20 -0400, bearophile bearophileh...@lycos.com  
wrote:



In this bug report I have asked for better error messages:
http://d.puremagic.com/issues/show_bug.cgi?id=6696

But beside the error message, do you know why an immutable ref can't be  
given to a function with a const ref argument? foo() can't change the  
contents of the array a any way, so what's wrong in this code?



void foo(const ref int[5] a) {}
void main() {
immutable int[5] arr;
foo(arr); // Error?
}



BTW, when posting questions like this, it is *immensely* helpful to give  
exact error messages, so I don't have to try it out to see what you are  
talking about.  I spent a while writing a response until I saw the error  
message (which changed completely how I viewed the issue), and erased it  
before writing my eventual reply.


-Steve


Re: const-immutable array argument?

2011-09-20 Thread Daniel Murphy
Steven Schveighoffer schvei...@yahoo.com wrote in message 
news:op.v13w8td2eav7ka@localhost.localdomain...
 On Tue, 20 Sep 2011 07:33:20 -0400, bearophile bearophileh...@lycos.com 
 wrote:

 void foo(const ref int[5] a) {}
 void main() {
 immutable int[5] arr;
 foo(arr); // Error?
 }

 The complaint from the compiler is that cast(const(int[5u])) arr is not an 
 lvalue.

It's a bug, the compiler shouldn't be inserting a cast when the value 
implicitly converts.  It's also a bug when the compiler tries to optimise 
away the variable to a literal when passing by reference, I've got a patch 
for this I haven't written up yet. 




toUTFz and WinAPI GetTextExtentPoint32W

2011-09-20 Thread Andre
Hi,

I want something like:

bool test(HDC dc, string str, int len, SIZE* s)
{
wchar[] wstr = toUTFz!(wchar*)str;
GetTextExtentPoint32W(dc wstr.ptr, wstr.length, s);
...

I get the wchar[] stuff not working. I am struggling
with pointer to array. Could you give some advice?

Kind regards
Andre


Re: toUTFz and WinAPI GetTextExtentPoint32W

2011-09-20 Thread Trass3r

bool test(HDC dc, string str, int len, SIZE* s)
{
wchar[] wstr = toUTFz!(wchar*)str;
GetTextExtentPoint32W(dc wstr.ptr, wstr.length, s);


toUTFz returns a wchar*, not a wchar[].


Re: toUTFz and WinAPI GetTextExtentPoint32W

2011-09-20 Thread Andre
Am Tue, 20 Sep 2011 19:27:03 +0200 schrieb Trass3r:

 bool test(HDC dc, string str, int len, SIZE* s)
 {
 wchar[] wstr = toUTFz!(wchar*)str;
 GetTextExtentPoint32W(dc wstr.ptr, wstr.length, s);
 
 toUTFz returns a wchar*, not a wchar[].

I am not familiar with pointers. I know I have to
call toUTFz! and fill pointer value and length value
of the WinAPI from the result.
Do you have any suggestions how to achieve this API call?

Kind regards
Andre


Dynamic Array Question

2011-09-20 Thread Dax
Hi!
I'm working on a library written in D.
After some tests I have discovered that my library leaks memory, those leaks 
are caused by dynamics array that I use in my library.

My question is:
Should dynamics array be deallocated automatically when a procedure returns? 
There is another way to acomplish this?

Maybe I'm doing something wrong, so, I post the function that causes the leak:

public string getWindowText(HWND hWnd)
{
  int len = GetWindowTextLengthW(hWnd);
  wchar[] t = new wchar[len + 1]; // How to deallocate this?

  GetWindowTextW(hWnd, t.ptr, len);

  /*
   * I'm converting the wchar[] to char[],
   * the variable 't' should be deallocated
   * because I not need it anymore.
   */
  return to!(string)(t[0..len]);
}

Thanks,
Dax


Re: toUTFz and WinAPI GetTextExtentPoint32W

2011-09-20 Thread Timon Gehr

On 09/20/2011 08:07 PM, Andre wrote:

Am Tue, 20 Sep 2011 19:27:03 +0200 schrieb Trass3r:


bool test(HDC dc, string str, int len, SIZE* s)
{
wchar[] wstr = toUTFz!(wchar*)str;
GetTextExtentPoint32W(dc wstr.ptr, wstr.length, s);


toUTFz returns a wchar*, not a wchar[].


I am not familiar with pointers. I know I have to
call toUTFz! and fill pointer value and length value
of the WinAPI from the result.
Do you have any suggestions how to achieve this API call?

Kind regards
Andre


Are you sure that the call requires the string to be null terminated? I 
do not know that winapi function, but this might work:


bool test(HDC dc, string str, SIZE* s)
{
auto wstr = to!(wchar[])str;
GetTextExtentPoint32W(dc, wstr.ptr, wstr.length, s);
...


Re: Dynamic Array Question

2011-09-20 Thread Steven Schveighoffer

On Tue, 20 Sep 2011 14:06:34 -0400, Dax d...@mailinator.com wrote:


Hi!
I'm working on a library written in D.
After some tests I have discovered that my library leaks memory, those  
leaks are caused by dynamics array that I use in my library.


My question is:
Should dynamics array be deallocated automatically when a procedure  
returns?


No, in a GC-enabled language, the GC is responsible for cleaning up the  
memory.



There is another way to acomplish this?


Yes, you can manually free the memory at your own risk.  Note that in any  
GC-enabled language, more memory is consumed than in a manually-managed  
language.  This is because there is a time period where a memory block is  
unused, but still allocated (i.e. the GC hasn't collected it yet).


D's garbage collector is conservative, which means it may keep some blocks  
in memory even though they are no longer in use.  Also, depending on your  
measurement tools, you may be counting freed memory towards memory usage.   
For example, if a block of memory is deallocated, it's not given back to  
the OS, it simply goes back into a pool to be reallocated again later.


Maybe I'm doing something wrong, so, I post the function that causes the  
leak:


public string getWindowText(HWND hWnd)
{
  int len = GetWindowTextLengthW(hWnd);
  wchar[] t = new wchar[len + 1]; // How to deallocate this?

  GetWindowTextW(hWnd, t.ptr, len);

  /*
   * I'm converting the wchar[] to char[],
   * the variable 't' should be deallocated
   * because I not need it anymore.
   */
  return to!(string)(t[0..len]);
}


You can deallocate the original array.  The soon-to-be-deprecated method  
(but easiest) is:


delete t;

To avoid having to change your other code, I'd do this:

wchar[] t = ...;
scope(exit) delete t; // add this line to the end of the function (after  
returning)


There is another way, but it's not as easy:

// put this at the top of file
import core.memory;

...

scope(exit) GC.free(t.ptr);

However, this is what will be required when delete is deprecated.

-Steve


Re: toUTFz and WinAPI GetTextExtentPoint32W

2011-09-20 Thread Trass3r
Are you sure that the call requires the string to be null terminated? I  
do not know that winapi function, but this might work:


bool test(HDC dc, string str, SIZE* s)
{
auto wstr = to!(wchar[])str;
GetTextExtentPoint32W(dc, wstr.ptr, wstr.length, s);
...


It doesn't need to be null-terminated for that function.
Shouldn't you use to!wstring though?!


Re: Dynamic Array Question

2011-09-20 Thread Timon Gehr

On 09/20/2011 08:06 PM, Dax wrote:

Hi!
I'm working on a library written in D.
After some tests I have discovered that my library leaks memory, those leaks 
are caused by dynamics array that I use in my library.



Does it 'leak'? What is your exact setup, why isn't the GC collecting 
that memory?



My question is:
Should dynamics array be deallocated automatically when a procedure returns? 
There is another way to acomplish this?

Maybe I'm doing something wrong, so, I post the function that causes the leak:

public string getWindowText(HWND hWnd)
{
   int len = GetWindowTextLengthW(hWnd);
   wchar[] t = new wchar[len + 1]; // How to deallocate this?

   GetWindowTextW(hWnd, t.ptr, len);

   /*
* I'm converting the wchar[] to char[],
* the variable 't' should be deallocated
* because I not need it anymore.
*/
   return to!(string)(t[0..len]);
}



Unless the string gets extremely long, you could allocate it on the stack:

wchar[] t=(cast(wchar*)alloca(wchar.sizeof*(len+1)))[0..len+1];





Re: toUTFz and WinAPI GetTextExtentPoint32W

2011-09-20 Thread Timon Gehr

On 09/20/2011 08:34 PM, Trass3r wrote:

Are you sure that the call requires the string to be null terminated?
I do not know that winapi function, but this might work:

bool test(HDC dc, string str, SIZE* s)
{
auto wstr = to!(wchar[])str;
GetTextExtentPoint32W(dc, wstr.ptr, wstr.length, s);
...


It doesn't need to be null-terminated for that function.
Shouldn't you use to!wstring though?!


It has to be copied anyway, so there is no real difference. I just did 
not know the signature of that function, and if it had been missing the 
const, wstring would not have worked. But if there is a const, wstring 
is indeed superior because shorter and clearer.


Re: toUTFz and WinAPI GetTextExtentPoint32W

2011-09-20 Thread Timon Gehr

On 09/20/2011 08:24 PM, Timon Gehr wrote:

On 09/20/2011 08:07 PM, Andre wrote:

Am Tue, 20 Sep 2011 19:27:03 +0200 schrieb Trass3r:


bool test(HDC dc, string str, int len, SIZE* s)
{
wchar[] wstr = toUTFz!(wchar*)str;
GetTextExtentPoint32W(dc wstr.ptr, wstr.length, s);


toUTFz returns a wchar*, not a wchar[].


I am not familiar with pointers. I know I have to
call toUTFz! and fill pointer value and length value
of the WinAPI from the result.
Do you have any suggestions how to achieve this API call?

Kind regards
Andre


Are you sure that the call requires the string to be null terminated? I
do not know that winapi function, but this might work:

bool test(HDC dc, string str, SIZE* s)
{
auto wstr = to!(wchar[])str;
GetTextExtentPoint32W(dc, wstr.ptr, wstr.length, s);
...


sry, should have read:

bool test(HDC dc, string str, SIZE* s)
{
auto wstr = to!(wchar[])(str);
GetTextExtentPoint32W(dc, wstr.ptr, wstr.length, s);
...












Re: toUTFz and WinAPI GetTextExtentPoint32W

2011-09-20 Thread Andre
Am Tue, 20 Sep 2011 20:44:40 +0200 schrieb Timon Gehr:

 On 09/20/2011 08:24 PM, Timon Gehr wrote:
 On 09/20/2011 08:07 PM, Andre wrote:
 Am Tue, 20 Sep 2011 19:27:03 +0200 schrieb Trass3r:

 bool test(HDC dc, string str, int len, SIZE* s)
 {
 wchar[] wstr = toUTFz!(wchar*)str;
 GetTextExtentPoint32W(dc wstr.ptr, wstr.length, s);

 toUTFz returns a wchar*, not a wchar[].

 I am not familiar with pointers. I know I have to
 call toUTFz! and fill pointer value and length value
 of the WinAPI from the result.
 Do you have any suggestions how to achieve this API call?

 Kind regards
 Andre

 Are you sure that the call requires the string to be null terminated? I
 do not know that winapi function, but this might work:

 bool test(HDC dc, string str, SIZE* s)
 {
 auto wstr = to!(wchar[])str;
 GetTextExtentPoint32W(dc, wstr.ptr, wstr.length, s);
 ...
 
 sry, should have read:
 
 bool test(HDC dc, string str, SIZE* s)
 {
 auto wstr = to!(wchar[])(str);
 GetTextExtentPoint32W(dc, wstr.ptr, wstr.length, s);
 ...


thanks a lot for your help. 

Kind regards
Andre


Re: Dynamic Array Question

2011-09-20 Thread Dax


Does it 'leak'? What is your exact setup, why isn't the GC collecting
that memory?



I have a Label class with a text() property that calls the procedure 
that I have written in my first post and returns the result.



I have posted here because I was looking the memory usage (more 
precisely the process' private working set) with Process Explorer (from 
Sysinternals) and I saw the private working set increase and increase 
every time text() property il called, so I trought it was a memory leak.


But, if the Garbage Collector handles the memory allocation of dynamic 
arrays, the problem is solved.



Thanks to all for help me with this problem! :)


Re: const-immutable array argument?

2011-09-20 Thread bearophile
Steven Schveighoffer:

 BTW, when posting questions like this, it is *immensely* helpful to give  
 exact error messages,

I have forgotten to do that by mistake. I am sorry.

Bye,
bearophile


Re: toUTFz and WinAPI GetTextExtentPoint32W

2011-09-20 Thread Andrej Mitrovic
Don't use length, use std.utf.count, ala:

import std.utf;
alias toUTFz!(const(wchar)*, string)  toUTF16z;
GetTextExtentPoint32W(str.toUTF16z, std.utf.count(str), s);

I like to keep that alias for my code since I was already using it beforehand.

I'm pretty sure (ok maybe 80% sure) that GetTextExtentPoint32W asks
for the count of characters and not code units. The WinAPI docs are a
bit fuzzy when it comes to these things, some functions take the
character count, others code-unit count. I've used this function in a
D port of a Neatpad project a while ago.


Re: toUTFz and WinAPI GetTextExtentPoint32W

2011-09-20 Thread Andrej Mitrovic
On 9/20/11, Jonathan M Davis jmdavisp...@gmx.com wrote:
 Or std.range.walkLength. I don't know why we really have std.utf.count. I
 just
 calls walkLength anyway. I suspect that it's a function that predates
 walkLength and was made to use walkLength after walkLength was introduced.
 But
 it's kind of pointless now.

 - Jonathan M Davis


I don't think having better-named aliases is a bad thing. Although now
I'm seeing it's not just an alias but a function.

What exactly is the static if (E.sizeof  4) in there for btw? When
would the element type exceed 4 bytes while still passing the
isSomeChar contract, and then why not stop compilation at that point
instead of return s.length?


Re: toUTFz and WinAPI GetTextExtentPoint32W

2011-09-20 Thread Andrej Mitrovic
One other thing, count can only take an array which seems too
restrictive since walkLength can take any range at all. So maybe count
should be just an alias to walkLength or it should possibly be removed
(I'm against fully removing it because I already use it in code and I
think the name does make sense).


Re: Dynamic Array Question

2011-09-20 Thread Christophe
 To avoid having to change your other code, I'd do this:
 
 wchar[] t = ...;
 scope(exit) delete t; // add this line to the end of the function (after  
 returning)
 
 There is another way, but it's not as easy:
 
 // put this at the top of file
 import core.memory;
 
 ...
 
 scope(exit) GC.free(t.ptr);

does scope wchar[] t = ...; work too ?


Re: toUTFz and WinAPI GetTextExtentPoint32W

2011-09-20 Thread Jonathan M Davis
On Tuesday, September 20, 2011 14:43 Andrej Mitrovic wrote:
 On 9/20/11, Jonathan M Davis jmdavisp...@gmx.com wrote:
  Or std.range.walkLength. I don't know why we really have std.utf.count. I
  just
  calls walkLength anyway. I suspect that it's a function that predates
  walkLength and was made to use walkLength after walkLength was
  introduced. But
  it's kind of pointless now.
  
  - Jonathan M Davis
 
 I don't think having better-named aliases is a bad thing. Although now
 I'm seeing it's not just an alias but a function.

We specifically avoid having aliases in Phobos simply for having alternate 
function names. Aliases need to actually be useful, or they shouldn't be 
there.

 What exactly is the static if (E.sizeof  4) in there for btw? When
 would the element type exceed 4 bytes while still passing the
 isSomeChar contract, and then why not stop compilation at that point
 instead of return s.length?

The static if is there to special-case narrow strings. It's unnecessary 
(though it does eliminate a function call when -inline isn't used). It would 
have been necessary prior to count just forwarding to walkLength, but it isn't 
now.

 One other thing, count can only take an array which seems too
 restrictive since walkLength can take any range at all. So maybe count
 should be just an alias to walkLength or it should possibly be removed
 (I'm against fully removing it because I already use it in code and I
 think the name does make sense).

I don't know if we're going to remove std.utf.count or not, but it _is_ the 
kind of thing that we've been removing. It doesn't add any real value. It's 
just another function which does exactly the same thing as walkLength except 
that it's restricted to strings, and we don't generally like having pointless 
aliases around (or pointless function wrappers, which amounts to pretty much 
the same thing). So, it wouldn't surprise me at all if it goes away, but 
if/when it does, it'll go through the proper deprecation cycle rather than 
just being removed, so if/when we do that, it's not like your code would
immediately break.

- Jonathan M Davis


Re: toUTFz and WinAPI GetTextExtentPoint32W

2011-09-20 Thread Andrej Mitrovic
On 9/20/11, Jonathan M Davis jmdavisp...@gmx.com wrote:
 We specifically avoid having aliases in Phobos simply for having alternate
 function names. Aliases need to actually be useful, or they shouldn't be
 there.

And function names have to be useful to library users. walkLength is
an awful name for something that returns the character count.

If you ask a GUI developer to look for a function that creates a
rectangle path, you can be sure he'll start looking for Rectangle or
DrawRect or something similar, and not ClosedShapePointN!4 or
something that generic.


Re: toUTFz and WinAPI GetTextExtentPoint32W

2011-09-20 Thread Christophe
Jonathan M Davis , dans le message (digitalmars.D.learn:29637), a
 écrit :
 On Tuesday, September 20, 2011 14:43 Andrej Mitrovic wrote:
 On 9/20/11, Jonathan M Davis jmdavisp...@gmx.com wrote:
  Or std.range.walkLength. I don't know why we really have std.utf.count. I
  just
  calls walkLength anyway. I suspect that it's a function that predates
  walkLength and was made to use walkLength after walkLength was
  introduced. But
  it's kind of pointless now.
  
  - Jonathan M Davis
 
 I don't think having better-named aliases is a bad thing. Although now
 I'm seeing it's not just an alias but a function.
 

std.utf.count has on advantage: someone looking for the function will 
find it. The programmer might not look in std.range to find a function 
about UFT strings, and even if he did, it is not indicated in walkLength 
that it works with (narrow) strings the way it does. To know you can use 
walklength, you must know that:
-popFront works differently in string.
-hasLength is not true for strings.
-what is walkLength.

So yes, you experienced programmer don't need std.utf.count, but newbies 
do.

Last point: WalkLength is not optimized for strings.
std.utf.count should be.

This short implementation of count was 3 to 8 times faster than 
walkLength is a simple benchmark:

size_t myCount(string text)
{
  size_t n = text.length;
  for (uint i=0; itext.length; ++i)
{
  auto s = text[i]6;
  n -= (s1) - ((s+1)2);
}
  return n;
}

(compiled with gdc on 64 bits, the sample text was the introduction of 
french wikipedia UTF-8 article down to the sommaire - 
http://fr.wikipedia.org/wiki/UTF-8 ).

The reason is that the loop can be unrolled by the compiler.


Issuing compile-time warnings with line numbers?

2011-09-20 Thread Andrej Mitrovic
Won't compile for obvious reasons:

struct Bar { }
struct Foo
{
this(Bar bar, int line = __LINE__)
{
pragma(msg, Warning: Constructing Foo with Bar incurs
precision loss. Line:  ~ line);
}
}

void main()
{
auto foo = Foo(Bar());
}

That's just an example, but I want to issue a compile-time warning
when a specific ctor is called because it might incur precision loss.
I want to give the user the line number of the call, but I can't call
to!string since it's not ctfe-able, so do I have any other options?


Re: toUTFz and WinAPI GetTextExtentPoint32W

2011-09-20 Thread Christophe
Timon Gehr , dans le message (digitalmars.D.learn:29641), a écrit :
 Last point: WalkLength is not optimized for strings.
 std.utf.count should be.

 This short implementation of count was 3 to 8 times faster than
 walkLength is a simple benchmark:

 size_t myCount(string text)
 {
size_t n = text.length;
for (uint i=0; itext.length; ++i)
  {
auto s = text[i]6;
n -= (s1) - ((s+1)2);
  }
return n;
 }

 (compiled with gdc on 64 bits, the sample text was the introduction of
 french wikipedia UTF-8 article down to the sommaire -
 http://fr.wikipedia.org/wiki/UTF-8 ).

 The reason is that the loop can be unrolled by the compiler.
 
 Very good point, you might want to file an enhancement request. It would 
 make the functionality different enough to prevent count from being 
 removed: walkLength throws on an invalid UTF sequence.

I would be glad to do so, but I am quite new here, so I don't know how 
to. A little pointer could help.

-- 
Christophe


Re: Issuing compile-time warnings with line numbers?

2011-09-20 Thread Jonathan M Davis
On Tuesday, September 20, 2011 17:18 Andrej Mitrovic wrote:
 Won't compile for obvious reasons:
 
 struct Bar { }
 struct Foo
 {
 this(Bar bar, int line = __LINE__)
 {
 pragma(msg, Warning: Constructing Foo with Bar incurs
 precision loss. Line:  ~ line);
 }
 }
 
 void main()
 {
 auto foo = Foo(Bar());
 }
 
 That's just an example, but I want to issue a compile-time warning
 when a specific ctor is called because it might incur precision loss.
 I want to give the user the line number of the call, but I can't call
 to!string since it's not ctfe-able, so do I have any other options?

Not really. That's why the scheduled for deprecation messages don't ever use 
line numbers. They can only give the line that pragma is on. If the function 
were templated and took the line number as a template (resulting in a new 
instantiation each time that the function was called), then you could use the 
line number in the template, but that would be the closest that you could get 
to having a message per call.

- Jonathan M Davis


Re: Issuing compile-time warnings with line numbers?

2011-09-20 Thread Andrej Mitrovic
Ah, you're right. It should have been a compile-time argument, conv.to
actually works in CTFE:

import std.conv;

struct Bar { }
struct Foo
{
   this(int line = __LINE__)(Bar bar)
   {
   pragma(msg, Warning: Constructing Foo with Bar incurs
precision loss. Line:  ~ to!string(line));
   }
}

void main()
{
   auto foo = Foo(Bar());
}

Unfortunately this is nearly impossible to use this way without making
*every* ctor templated. This is due to that bug where templated
functions can't overload against non-templated functions.

I guess I'll have to do without line numbers for now. But thanks for the tip.


Re: Dynamic Array Question

2011-09-20 Thread Jonathan M Davis
On Tuesday, September 20, 2011 21:48:10 Christophe wrote:
  To avoid having to change your other code, I'd do this:
  
  wchar[] t = ...;
  scope(exit) delete t; // add this line to the end of the function (after
  returning)
  
  There is another way, but it's not as easy:
  
  // put this at the top of file
  import core.memory;
  
  ...
  
  scope(exit) GC.free(t.ptr);
 
 does scope wchar[] t = ...; work too ?

I'm not sure, but scope as a modifier on local variables like that is going to 
be removed from the language. std.typecons.Scoped is replacing it.

However, unless you're having major memory issues, I really wouldn't worry 
about it. Just let the garbage collector do its thing. It's not the best, but 
it works fine in most cases.

- Jonathan M Davis


Re: Issuing compile-time warnings with line numbers?

2011-09-20 Thread Adam D. Ruppe
I'd suggest having a version(warnings_suck) { static assert(0); }
so people who want more info can just stick a -version= on the build
and get the compiler's help.

static assert(0) gives a kind of compile time stack trace.



Re: Issuing compile-time warnings with line numbers?

2011-09-20 Thread Andrej Mitrovic
On 9/21/11, Adam D. Ruppe destructiona...@gmail.com wrote:
 I'd suggest having a version(warnings_suck) { static assert(0); }
 so people who want more info can just stick a -version= on the build
 and get the compiler's help.

Yeah that was already planned, no worries. :)


How to read output of a script

2011-09-20 Thread Cheng Wei
In D2, how can we get the output of running a script.
We can use 'system' to run the script, but how we cannot assign the
standard output of the running script. Is there any way to do it, or
whether it will be included into future version of std.process? Thanks a
lot.


Re: How to read output of a script

2011-09-20 Thread Jonathan M Davis
On Wednesday, September 21, 2011 03:31:11 Cheng Wei wrote:
 In D2, how can we get the output of running a script.
 We can use 'system' to run the script, but how we cannot assign the
 standard output of the running script. Is there any way to do it, or
 whether it will be included into future version of std.process? Thanks a
 lot.

std.process.shell

- Jonathan M Davis


Re: Dynamic Array Question

2011-09-20 Thread Jesse Phillips
On Tue, 20 Sep 2011 14:28:54 -0400, Steven Schveighoffer wrote:

 You can deallocate the original array.  The soon-to-be-deprecated method
 (but easiest) is:
 
 delete t;
 
 To avoid having to change your other code, I'd do this:
 
 wchar[] t = ...;
 scope(exit) delete t; // add this line to the end of the function (after
 returning)

Unless I missed something, delete is being removed from the language.


Is this a bug in execvp of std.process

2011-09-20 Thread Cheng Wei
#import std.process
void main() {
execvp(ip, route);
}

result:
Object ute is unknown, try ip help.

That is the first two bytes are lost

Adding two spaces works:
#import std.process
void main() {
execvp(ip,   route);
}

Version 2.055, linux, 32bit

Thanks.


Is it a bug in execvb (std.process)?

2011-09-20 Thread Cheng Wei
import std.process;
void main() {
execvp(ip, [route]);
}

result:
Object ute is unknown, try ip help.

So the first two bytes are lost.
After adding two spaces in the first argument, it works.
import std.process;
void main() {
execvp(ip, [  route]);
}

dmd 2.055 linux 32bit.
Is this a bug? Can I issue it? Thanks a lot.


Re: How to read output of a script

2011-09-20 Thread Cheng Wei
Thanks a lot.
Weird. It is not in the library reference in http://www.d-programming-
language.org/, but it is in the library reference in digitalmars.com. I
throught the previous one was the official web site now. It seems it
still is not synced well.