Re: Problem with creating a new account on wiki.dlang.org

2015-02-03 Thread Zach the Mystic via Digitalmars-d-learn

On Tuesday, 3 February 2015 at 23:52:18 UTC, Piotrek wrote:

Hi,

I wanted to create my account at wiki.dlang.org:

Went to:
http://wiki.dlang.org/?title=Special:UserLoginreturnto=Wish+listtype=signup

And got:
No questions found; set some in LocalSettings.php using the 
format from QuestyCaptcha.php.


Anyone familiar with the issue?

Piotrek


https://issues.dlang.org/show_bug.cgi?id=14121


Re: Constructing a tuple dynamically

2015-02-03 Thread thedeemon via Digitalmars-d-learn

On Tuesday, 3 February 2015 at 10:32:47 UTC, Pena wrote:

How can I use this code or something similar to dynamically
construct a tuple containing types of fields marked as 
@Cloneable?


import std.traits, std.typetuple;

template CloneableTypes(S) {
  template IsCloneable(string M) {
enum IsCloneable = staticIndexOf!(Cloneable, 
__traits(getAttributes, __traits(getMember, S, M))) = 0;

  }

  template FieldType(string M) {
alias FieldType = typeof(__traits(getMember, S, M));
  }

  alias CloneableTypes = staticMap!(FieldType, 
Filter!(IsCloneable, __traits(allMembers, S)));

}

Then CloneableTypes!Foo is a TypeTuple (int, string).


Re: Problem with creating a new account on wiki.dlang.org

2015-02-03 Thread Zach the Mystic via Digitalmars-d-learn

On Tuesday, 3 February 2015 at 23:52:18 UTC, Piotrek wrote:

Went to:
http://wiki.dlang.org/?title=Special:UserLoginreturnto=Wish+listtype=signup

And got:
No questions found; set some in LocalSettings.php using the 
format from QuestyCaptcha.php.


Anyone familiar with the issue?

Piotrek


Vladimir fixed it. Yay!


Re: Conway's game of life

2015-02-03 Thread Paul via Digitalmars-d-learn

On Monday, 2 February 2015 at 16:58:43 UTC, bearophile wrote:


The quality of the D GC is not important for a simple Life 
implementation, you just need two arrays.


Here's my 30 minute sandwich-break version, sorry it's not very 
arractive 'D'...


import std.stdio;
import std.random;


void main(){

enum WORLDSIZE = 20;
enum INITIALPOP = 70;   //experimental
enum DEAD = 0;
enum ALIVE = 1;

int world[WORLDSIZE][WORLDSIZE];
int buffer[WORLDSIZE][WORLDSIZE];

//sprinkle life
foreach(i; 0..INITIALPOP){
//find an empty cell
int rX, rY;
do{
rX = uniform(0, WORLDSIZE);
rY = uniform(0, WORLDSIZE);
} while(world[rX][rY] == ALIVE);
world[rX][rY] = ALIVE;
}

//loop forever
while (true){

//work on the buffer
buffer = world;

foreach(x; 0..WORLDSIZE){
foreach(y; 0..WORLDSIZE){

int neighbourCount;

//get index to left, right, above and below current cell, 
wrapping if necessary

int left = (x == 0) ? WORLDSIZE-1 : x-1;
int right = (x == (WORLDSIZE-1) ) ? 0 : x+1;
int top = (y == 0) ? WORLDSIZE-1 : y-1;
int bottom = (y == (WORLDSIZE-1) ) ? 0 : y+1;

//add up surrounding cells
neighbourCount += world[left][y];
neighbourCount += world[left][top];
neighbourCount += world[left][bottom];
neighbourCount += world[x][top];
neighbourCount += world[x][bottom];
neighbourCount += world[right][top];
neighbourCount += world[right][y];
neighbourCount += world[right][bottom];


//if this cell is alive
if( world[x][y] == ALIVE){
//decide what to do
switch(neighbourCount){
case 2:
buffer[x][y] = ALIVE;
break;
case 3:
buffer[x][y] = ALIVE;
break;
default:
buffer[x][y] = DEAD;
}
}
else{
//just like today's news, newborn has 
three parents!
if(neighbourCount == 3) buffer[x][y] = 
ALIVE;
}
}   
}

//update world with contents of buffer
world = buffer;

//show current state of world with fancy graphics :P
foreach(x; 0..WORLDSIZE){
foreach(y; 0..WORLDSIZE){
write( world[x][y] == ALIVE ? X : . );
}
writeln();
}

readln();

}//end loop
}




Re: Conway's game of life

2015-02-03 Thread Paul via Digitalmars-d-learn

On Tuesday, 3 February 2015 at 13:35:37 UTC, Paul wrote:

On Monday, 2 February 2015 at 16:58:43 UTC, bearophile wrote:


The quality of the D GC is not important for a simple Life 
implementation, you just need two arrays.


Here's my 30 minute sandwich-break version, sorry it's not very 
arractive 'D'...


Meant to say - hold down return to avoid tedium and Ctrl+C to 
quit (of course!).


Re: Conway's game of life

2015-02-03 Thread Paul via Digitalmars-d-learn

On Tuesday, 3 February 2015 at 14:01:51 UTC, bearophile wrote:

Paul:


enum WORLDSIZE = 20;
enum INITIALPOP = 70;   //experimental
enum DEAD = 0;
enum ALIVE = 1;


D enums don't need to be ALL UPPERCASE :-)



int world[WORLDSIZE][WORLDSIZE];


Don't forget to compile with warnings active (it's a design 
error of the D compiler to have them disabled by default).




foreach(i; 0..INITIALPOP){


It's less bug-prone to make that index immutable:

foreach(immutable i; 0 .. initialPop) {

Bye,
bearophile


Thanks for the comments. The all-caps enums is just habit. I 
don't get any warnings using the dmd -w switch, I take it you 
mean use int[size][size] var instead?


Regarding the immutable loop variable, I've conditioned myself 
never to interfere with loop control values - it gives me the 
same bad feeling as goto/continue (and to a lesser extent 
'break').





Re: Constructing a tuple dynamically

2015-02-03 Thread Kagamin via Digitalmars-d-learn

Try variadic templates with recursion.
For example see http://dpaste.dzfl.pl/f49a97e35974


Re: Conway's game of life

2015-02-03 Thread bearophile via Digitalmars-d-learn

Paul:

Regarding the immutable loop variable, I've conditioned myself 
never to interfere with loop control values


But adding immutable you don't risk modifying the variable by 
mistake.


It's another design mistake of D. Variables (like foreach loop 
indexes) must be immutable by default because otherwise 
programmers often don't bother making them immutable. It's a lost 
war.


Bye,
bearophile


Allegro 5 in C - installing on OSX

2015-02-03 Thread Joel via Digitalmars-d-learn

How do you install Allegro 5 (OSX)? Like, using 'Home Brew'.


Creating a JSON file

2015-02-03 Thread solidstate1991 via Digitalmars-d-learn
I wrote my graphics engine with CodeBlocks, and I only used dub 
when I complied the derelict SDL2 libs.


https://github.com/ZILtoid1991/VDP-engine

You can find the sources here.


Re: Allegro 5 in C - installing on OSX

2015-02-03 Thread Mike Parker via Digitalmars-d-learn

On 2/3/2015 5:37 PM, Joel wrote:

How do you install Allegro 5 (OSX)? Like, using 'Home Brew'.


This really isn't the place for that. At allegro.cc there are 
Allegro-specific forums [1] and an Allegro wiki info on 
building/installing, where you can find [2].


[1] https://www.allegro.cc/forums/
[2] https://wiki.allegro.cc/index.php?title=Getting_Started#Mac_OS


Re: Conway's game of life

2015-02-03 Thread bearophile via Digitalmars-d-learn

Paul:


enum WORLDSIZE = 20;
enum INITIALPOP = 70;   //experimental
enum DEAD = 0;
enum ALIVE = 1;


D enums don't need to be ALL UPPERCASE :-)



int world[WORLDSIZE][WORLDSIZE];


Don't forget to compile with warnings active (it's a design error 
of the D compiler to have them disabled by default).




foreach(i; 0..INITIALPOP){


It's less bug-prone to make that index immutable:

foreach(immutable i; 0 .. initialPop) {

Bye,
bearophile


Code coverage during CTFE

2015-02-03 Thread Stefan Frijters via Digitalmars-d-learn
Recently I've hooked my code up to coveralls.io using the 
convenient doveralls[1]. At first, I just did a dub test command 
before sending the data to coveralls, but my simulation code also 
has runnable tests in addition to unittests, which reaches many 
more lines.


Today I've added an option to my testing script to merge the .lst 
files from various runs, basically adding up the number of times 
each line is hit. This seems to work just fine.


However, my code also heavily uses string mixins in a couple of 
places, and I use functions that return the relevant strings to 
generate them. These functions are never called at runtime, and 
as such are shown with the nasty red bars for no coverage. Is it 
possible to get a -cov dump for code run at compile time?


[1] https://github.com/ColdenCullen/doveralls


Want to read a whole file as utf-8

2015-02-03 Thread Foo via Digitalmars-d-learn
How can I do that without any GC allocation? Nothing in std.file 
seems to be marked with @nogc


I'm asking since it seems very complicated to do that with C++, 
maybe D is a better choice, then we would probably move our whole 
project from C++ to D.


Symbol References Pass in DMD

2015-02-03 Thread Per Nordlöw via Digitalmars-d-learn
Is there any part of Dmd that keeps track of references to 
symbols? If not do you have any advice on where and how to add 
them?


Re: Want to read a whole file as utf-8

2015-02-03 Thread FG via Digitalmars-d-learn

On 2015-02-03 at 19:53, Foo wrote:

How can I do that without any GC allocation? Nothing in std.file seems to be 
marked with @nogc

I'm asking since it seems very complicated to do that with C++, maybe D is a 
better choice, then we would probably move our whole project from C++ to D.


Looks like std.stdio isn't marked with @nogc all the way either.

So for now the temporary solution would be to use std.c.stdio.
Get the file size, malloc a buffer large enough for it[1],
use std.c.stdio.read to fill it, assign it to a char[] slice
and std.utf.decode to consume the text...

Oh wait, decode isn't @nogc either. FFS, what now?


[1] I assume the file is small, otherwise there would be an extra step
involved where after nearing the end of the buffer you move the rest
of the data to the front, read new data after it, and continue decoding.


Re: Want to read a whole file as utf-8

2015-02-03 Thread Tobias Pankrath via Digitalmars-d-learn

On Tuesday, 3 February 2015 at 19:44:49 UTC, FG wrote:

On 2015-02-03 at 19:53, Foo wrote:
How can I do that without any GC allocation? Nothing in 
std.file seems to be marked with @nogc


I'm asking since it seems very complicated to do that with 
C++, maybe D is a better choice, then we would probably move 
our whole project from C++ to D.


Looks like std.stdio isn't marked with @nogc all the way either.

So for now the temporary solution would be to use std.c.stdio.
Get the file size, malloc a buffer large enough for it[1],
use std.c.stdio.read to fill it, assign it to a char[] slice
and std.utf.decode to consume the text...

Oh wait, decode isn't @nogc either. FFS, what now?


[1] I assume the file is small, otherwise there would be an 
extra step
involved where after nearing the end of the buffer you move the 
rest
of the data to the front, read new data after it, and continue 
decoding.


Use std.utf.validate instead of decode. It will only allocate one 
exception if necessary.


Re: Want to read a whole file as utf-8

2015-02-03 Thread FG via Digitalmars-d-learn

On 2015-02-03 at 20:50, Tobias Pankrath wrote:

Use std.utf.validate instead of decode. It will only allocate one exception if 
necessary.


Looks to me like it uses decode internally...

But Foo, do you have to use @nogc? It still looks like it's work in progress,
and lack of it doesn't mean that the GC is actually involved in the function.
It will probably take several months for the obvious nogc parts of the std lib
to get annotated, and much longer to get rid of unnecessary use of the GC.
So maybe the solution for now is to verify the source code of the function in
question with ones own set of eyeballs and decide if it's good enough for use,
ie. doesn't leak too much?


Re: Want to read a whole file as utf-8

2015-02-03 Thread Foo via Digitalmars-d-learn

On Tuesday, 3 February 2015 at 19:56:37 UTC, FG wrote:

On 2015-02-03 at 20:50, Tobias Pankrath wrote:
Use std.utf.validate instead of decode. It will only allocate 
one exception if necessary.


Looks to me like it uses decode internally...

But Foo, do you have to use @nogc? It still looks like it's 
work in progress,
and lack of it doesn't mean that the GC is actually involved in 
the function.
It will probably take several months for the obvious nogc parts 
of the std lib
to get annotated, and much longer to get rid of unnecessary use 
of the GC.
So maybe the solution for now is to verify the source code of 
the function in
question with ones own set of eyeballs and decide if it's good 
enough for use,

ie. doesn't leak too much?


Yes, we don't want to use a GC. We want determinsitic life times. 
I'm not the boss, but I support the idea.


@Nordlöw Neither of them can be marked with @nogc. :/


Re: Want to read a whole file as utf-8

2015-02-03 Thread Foo via Digitalmars-d-learn

On Tuesday, 3 February 2015 at 19:44:49 UTC, FG wrote:

On 2015-02-03 at 19:53, Foo wrote:
How can I do that without any GC allocation? Nothing in 
std.file seems to be marked with @nogc


I'm asking since it seems very complicated to do that with 
C++, maybe D is a better choice, then we would probably move 
our whole project from C++ to D.


Looks like std.stdio isn't marked with @nogc all the way either.

So for now the temporary solution would be to use std.c.stdio.
Get the file size, malloc a buffer large enough for it[1],
use std.c.stdio.read to fill it, assign it to a char[] slice
and std.utf.decode to consume the text...

Oh wait, decode isn't @nogc either. FFS, what now?


[1] I assume the file is small, otherwise there would be an 
extra step
involved where after nearing the end of the buffer you move the 
rest
of the data to the front, read new data after it, and continue 
decoding.


How would I use decoding for that? Isn't there a way to read the 
file as utf8 or event better, as unicode?


Re: Want to read a whole file as utf-8

2015-02-03 Thread Nordlöw

On Tuesday, 3 February 2015 at 18:53:28 UTC, Foo wrote:
How can I do that without any GC allocation? Nothing in 
std.file seems to be marked with @nogc


I'm asking since it seems very complicated to do that with C++, 
maybe D is a better choice, then we would probably move our 
whole project from C++ to D.


My module

https://github.com/nordlow/justd/blob/master/mmfile_ex.d

together with

https://github.com/nordlow/justd/blob/master/bylines.d

is about as low-level as you can get in D.


Constructing a tuple dynamically

2015-02-03 Thread Pena via Digitalmars-d-learn

I want to create a simple clone operator based on UDAs. First
step is to create a tuple of the fields with desired UDA value
and construct a clone method which uses that tuple.

import std.traits;

enum Cloneable;

struct Foo {
   @Cloneable int cloneableInt;
   @Cloneable string cloneableStr;
   float notCloneable;
}


I came up with this to iterate through fields and print out ones
with @Cloneable set:

foreach(member; __traits(allMembers, Foo)) {
   foreach(attr; __traits(getAttributes, mixin(member))) {
 static if(is(attr == Cloneable)) {
   pragma(msg, member);
 }
   }
}

How can I use this code or something similar to dynamically
construct a tuple containing types of fields marked as @Cloneable?


Re: Want to read a whole file as utf-8

2015-02-03 Thread FG via Digitalmars-d-learn

On 2015-02-04 at 00:07, Foo wrote:

How would I use decoding for that? Isn't there a way to read the file as utf8 
or event better, as unicode?


Well, apparently the utf-8-aware foreach loop still works just fine.
This program shows the file size and the number of unicode glyps, or whatever 
they are called:

import core.stdc.stdio;
int main() @nogc
{
const int bufSize = 64000;
char[bufSize] buffer;
size_t bytesRead, count;
FILE* f = core.stdc.stdio.fopen(test.d, r);
if (!f)
return 1;
bytesRead = fread(cast(void*)buffer, 1, bufSize, f);
if (bytesRead  bufSize - 1) {
printf(File is too big);
return 1;
}
if (!bytesRead)
return 2;
foreach (dchar d; buffer[0..bytesRead])
count++;
printf(read %d bytes, %d unicode characters\n, bytesRead, count);
fclose(f);
return 0;
}

Outputs for example this: read 838 bytes, 829 unicode characters

(It would be more complicated if it had to process bigger files.)


Re: Want to read a whole file as utf-8

2015-02-03 Thread Tobias Pankrath via Digitalmars-d-learn

On Tuesday, 3 February 2015 at 23:07:03 UTC, Foo wrote:

On Tuesday, 3 February 2015 at 19:44:49 UTC, FG wrote:

On 2015-02-03 at 19:53, Foo wrote:
How can I do that without any GC allocation? Nothing in 
std.file seems to be marked with @nogc


I'm asking since it seems very complicated to do that with 
C++, maybe D is a better choice, then we would probably move 
our whole project from C++ to D.


Looks like std.stdio isn't marked with @nogc all the way 
either.


So for now the temporary solution would be to use std.c.stdio.
Get the file size, malloc a buffer large enough for it[1],
use std.c.stdio.read to fill it, assign it to a char[] slice
and std.utf.decode to consume the text...

Oh wait, decode isn't @nogc either. FFS, what now?


[1] I assume the file is small, otherwise there would be an 
extra step
involved where after nearing the end of the buffer you move 
the rest
of the data to the front, read new data after it, and continue 
decoding.


How would I use decoding for that? Isn't there a way to read 
the file as utf8 or event better, as unicode?


Arrays of char, wchar and dchar are supposed to be UTF strings 
and of course you can just read them using a c function from a 
file. You'd just need to make sure they are valid UTF before 
passing them on to other parts of phobos.


What do you mean with as unicode?


Re: Allegro 5 in C - installing on OSX

2015-02-03 Thread Joel via Digitalmars-d-learn

Ok, thanks Mike.


Problem with creating a new account on wiki.dlang.org

2015-02-03 Thread Piotrek via Digitalmars-d-learn

Hi,

I wanted to create my account at wiki.dlang.org:

Went to:
http://wiki.dlang.org/?title=Special:UserLoginreturnto=Wish+listtype=signup

And got:
No questions found; set some in LocalSettings.php using the 
format from QuestyCaptcha.php.


Anyone familiar with the issue?

Piotrek


Re: Want to read a whole file as utf-8

2015-02-03 Thread Namespace via Digitalmars-d-learn

On Tuesday, 3 February 2015 at 23:55:19 UTC, FG wrote:

On 2015-02-04 at 00:07, Foo wrote:
How would I use decoding for that? Isn't there a way to read 
the file as utf8 or event better, as unicode?


Well, apparently the utf-8-aware foreach loop still works just 
fine.
This program shows the file size and the number of unicode 
glyps, or whatever they are called:


import core.stdc.stdio;
int main() @nogc
{
const int bufSize = 64000;
char[bufSize] buffer;
size_t bytesRead, count;
FILE* f = core.stdc.stdio.fopen(test.d, r);
if (!f)
return 1;
bytesRead = fread(cast(void*)buffer, 1, bufSize, f);
if (bytesRead  bufSize - 1) {
printf(File is too big);
return 1;
}
if (!bytesRead)
return 2;
foreach (dchar d; buffer[0..bytesRead])
count++;
printf(read %d bytes, %d unicode characters\n, 
bytesRead, count);

fclose(f);
return 0;
}

Outputs for example this: read 838 bytes, 829 unicode characters

(It would be more complicated if it had to process bigger 
files.)


To use a foreach loop is such a nice idea! tank you very much. :)

That's my code now:

private:

static import m3.m3;
static import core.stdc.stdio;
alias printf = core.stdc.stdio.printf;

public:

@trusted
@nogc
auto readFile(in string filename) nothrow {
	import std.c.stdio : FILE, SEEK_END, SEEK_SET, fopen, fclose, 
fseek, ftell, fread;


FILE* f = fopen(filename.ptr, rb);
fseek(f, 0, SEEK_END);
immutable size_t fsize = ftell(f);
fseek(f, 0, SEEK_SET);

char[] str = m3.m3.make!(char[])(fsize);
fread(str.ptr, fsize, 1, f);
fclose(f);

return str;
}

@trusted
@nogc
@property
dstring toUTF32(in char[] s) {
dchar[] r = m3.m3.make!(dchar[])(s.length); // r will never 
be longer than s

foreach (immutable size_t i, dchar c; s) {
r[i] = c;
}

return cast(dstring) r;
}

@nogc
void main() {
auto str = readFile(test_file.txt);
scope(exit) m3.m3.destruct(str);

auto str2 = str.toUTF32;
printf(%d : %d\n, cast(int) str[0], cast(int) str2[0]);
}


m3 is my own module and means manual memory management, three 
m's so m3. If we will use D (what is now much more likely) that 
is our core module for memory management.


Re: Want to read a whole file as utf-8

2015-02-03 Thread FG via Digitalmars-d-learn

On 2015-02-04 at 01:56, Namespace wrote:


 FILE* f = fopen(filename.ptr, rb);
 fseek(f, 0, SEEK_END);
 immutable size_t fsize = ftell(f);
 fseek(f, 0, SEEK_SET);



That's quite a smart way to get the size of the file.

I started with std.file.getSize (which obviously isn't marked as @nogc) and 
ended up with the monstrosity below (which I have only compiled on Windows), so 
I decided not to mention it in my previous post. Wouldn't be the point anyway, 
since I have only shown an example with a single-fill fixed buffer. But here it 
is, rendered useless by your code:

long getFileSize(const char* cName) @nogc
{
version(Windows)
{
import core.sys.windows.windows;
WIN32_FILE_ATTRIBUTE_DATA fad;
if (!GetFileAttributesExA(cName, 
GET_FILEEX_INFO_LEVELS.GetFileExInfoStandard, fad))
return -1;
ULARGE_INTEGER li;
li.LowPart = fad.nFileSizeLow;
li.HighPart = fad.nFileSizeHigh;
return li.QuadPart;
}
else version(Posix)
{
import core.sys.posix.sys.stat;
stat_t statbuf = void;
if (stat(cName, statbuf))
return -1;
return statbuf.st_size;
}
}