Re: Sanitizing forms in vibe.d. How?

2016-12-11 Thread Nicholas Wilson via Digitalmars-d-learn

On Sunday, 11 December 2016 at 18:30:54 UTC, aberba wrote:
In php, I use built-in functions like 
filter_var(FILTER_VALIDATE_EMAIL, $email). There are other 
constants for different data types.




You can enforce that the string that you receive is an email 
address with `isEmail` from `std.net.isemail`


Again, there is mysqli_real_escape_string() for escaping SQL 
injection/harmful characters.



What are my options in vibe.d or even D?


What sql library are you using? there is probably a function in 
that somewhere, that does sanitisation, or use prepared 
statements.


Re: Calling arbitrary functions at runtime?

2016-12-11 Thread Kevin Balbas via Digitalmars-d-learn

On Sunday, 11 December 2016 at 22:18:02 UTC, Adam D. Ruppe wrote:

On Sunday, 11 December 2016 at 22:00:27 UTC, Kevin Balbas wrote:

Basically, I need some way to turn an array of strings
into an argument list at runtime.  Is this possible?


Write (or generate) a helper function that loops over the 
Parameters!Func tuple and populates it from the strings. Call 
the helper function.



// declare your arguments tuple
Parameters!Func args;

// populate the arguments
foreach(idx, ref arg; args) {
   arg = to!(typeof(arg))(string_args[idx]);
}

Func(args); // call the function with that tuple



The free sample of my book: 
https://www.packtpub.com/application-development/d-cookbook has 
a more in-depth example near the end of it.


I see.  I was planning on doing a wrapper-based approach to the 
function calls if this didn't work out, but I didn't expect it'd 
be that simple.  Thanks for the tip.


Re: help on cartesianProduct()

2016-12-11 Thread Era Scarecrow via Digitalmars-d-learn

On Sunday, 11 December 2016 at 18:05:19 UTC, Era Scarecrow wrote:

On Sunday, 11 December 2016 at 16:34:38 UTC, Orut wrote:
I need to be able to vary the number of ranges to feed into 
cartesianProduct() at run time.


 Hmmm... what kind of ranges? Are they going to be arrays? Or 
something else?



Well, with the assumption of using arrays (since you kinda need a 
length for it to work) I've thrown together a quick struct that 
does the job. Not fully tested, but will do the same thing with a 
varying number of inputs.


[code]
struct MultiCart(T) {
T[][] data;
int iteration;
int max;

this(T[][] _d, int iter=0) {
data = _d;
iteration = iter;

max = 1;
foreach(a; _d) {
if (a.length)
max *= a.length;
}
}

T[] front() {
int i = iteration;
T[] val;

foreach(d; data) {
if (d.length) {
val ~= d[i % d.length];
i /= d.length;
}
}

return val;
}

void popFront() {
iteration++;
}

bool empty() {
return iteration >= max;
}
}

unittest {
import std.stdio;
alias CartInt = MultiCart!int;

int[] a=[1,2,3],b=[4,5],c=[6,7];
foreach(x; CartInt([a,b,c]))
writeln(x);

foreach(x; CartInt([a,b]))
writeln(x);
}
[/code]


Re: Calling arbitrary functions at runtime?

2016-12-11 Thread ketmar via Digitalmars-d-learn

import std.traits;
import std.stdio;


alias FDg = void delegate (string args);

FDg[string] cmdlist;

void register(DG) (string name, DG dg) if (isCallable!DG) {
  cmdlist[name] = delegate (string args) {
import std.array : split;
import std.conv : to;
alias Args = Parameters!DG;
auto spx = args.split(' ');
Args ara;
foreach (immutable idx, ref a; ara) {
  a = spx[idx].to!(typeof(a));
}
dg(ara);
  };
}


void main () {
  register("test", (int a, bool b) { writeln("a=", a, "; b=", b); 
});

  cmdlist["test"]("42 true");
}


Re: Calling arbitrary functions at runtime?

2016-12-11 Thread Adam D. Ruppe via Digitalmars-d-learn

On Sunday, 11 December 2016 at 22:00:27 UTC, Kevin Balbas wrote:

Basically, I need some way to turn an array of strings
into an argument list at runtime.  Is this possible?


Write (or generate) a helper function that loops over the 
Parameters!Func tuple and populates it from the strings. Call the 
helper function.



// declare your arguments tuple
Parameters!Func args;

// populate the arguments
foreach(idx, ref arg; args) {
   arg = to!(typeof(arg))(string_args[idx]);
}

Func(args); // call the function with that tuple



The free sample of my book: 
https://www.packtpub.com/application-development/d-cookbook has a 
more in-depth example near the end of it.


Re: arsd.dom appenChild method gives assertion message

2016-12-11 Thread Adam D. Ruppe via Digitalmars-d-learn

On Sunday, 11 December 2016 at 18:30:53 UTC, Erdem wrote:

   element.parentNode = null;
   content.appendChild(element);


That works too, but could lead to data corruption later because 
the other document thinks it still owns the element, but the 
element doesn't know that. So if you descend through children of 
either ones, you'll see it, but then if you go back up through 
the parents, you won't end up back where you started.


Re: arsd.dom appenChild method gives assertion message

2016-12-11 Thread Adam D. Ruppe via Digitalmars-d-learn

On Sunday, 11 December 2016 at 17:52:29 UTC, Erdem wrote:

   content.appendChild(firstElements[0]);
How should one use appendChild or similiar methods
of arsd.dom library?


You need to remove the element from the first document before 
trying to append it to another.


Try something like:

content.appendChild(firstElements[0].removeFromTree());


removeFromTree is a convenience method to do 
parentNode.removeChild(this).



Since an element can only exist in one place in a tree at a time, 
it will give the assert error if you try to add it to two places 
at once.





Calling arbitrary functions at runtime?

2016-12-11 Thread Kevin Balbas via Digitalmars-d-learn
I'm writing a system to register functions to be called at 
runtime.  With zero-argument functions, it works fine.  However, 
I run into a problem with functions that take arguments.


This is the relevant code I started with (zero-argument version):

mixin template CommandSystemRegister(string s = __MODULE__)
{
void CommandSystemRegisterCommands()
{
foreach(name; __traits(allMembers, mixin(s)))
{
static if (hasUDA!(mixin(name), RegisterCmd))
{
commandTable[name] = (name);
}
}
}
}

void CommandSystemExecuteCommand(string cmd)
{
auto result = cmd in commandTable;

if (result !is null)
{
(*result)();
}
else
{
writefln("command %s not found.", cmd);
}
}

The way to extend this seemed fairly straightforward.  I did the 
following things:


1.  Wrap function with a Variant, and put that Variant into a 
struct alongside an array of stringified parameter types (because 
Parameters!T can't be stored directly).


2.  On execution, parse the arguments to their correct types.

The problem is, I can't figure out how to actually *call* the 
function.  If it were python, I could construct a tuple with a 
comprehension and unpack that, but I can't figure out any way to 
dynamically construct tuples this way in D.


Basically, I need some way to turn an array of strings into an 
argument list at runtime.  Is this possible?


Re: How to get hash value of an object?

2016-12-11 Thread Seb via Digitalmars-d-learn

On Sunday, 4 December 2016 at 13:17:09 UTC, Era Scarecrow wrote:
On Tuesday, 29 November 2016 at 00:05:31 UTC, Steven 
Schveighoffer wrote:
hashOf is kind of this horrible hacky thing that nobody should 
be using. It literally takes whatever you pass it and hashes 
the local bytes.


 Ugg... Anything with pointers, classes or arrays will have 
huge problems with consistency; While anything with fixed 
static arrays or pure value-types will result in proper values.


For the record: it was a regression and has been fixed with 
https://github.com/dlang/druntime/pull/1707 and thus should be 
part of the next point release.


Re: Proper generic way to get the hash of something?

2016-12-11 Thread Seb via Digitalmars-d-learn

On Sunday, 4 December 2016 at 07:50:26 UTC, Tofu Ninja wrote:
Well for now I am going to revert back to 2.071.2, 2.072 seems 
broke as fuck.


For the record: it has been reverted: 
https://github.com/dlang/druntime/pull/1707 and thus should be 
part of the next point release.


Re: Strange memory corruption / codegen bug?

2016-12-11 Thread TheGag96 via Digitalmars-d-learn

On Sunday, 11 December 2016 at 11:58:39 UTC, ag0aep6g wrote:
Try putting an `assert(childCrossPoint !is otherCrossPoint);` 
before the assignment. If it fails, the variables refer to the 
same node. That would explain how otherCrossPoint.left gets set.


Ahh... This led me to it. I was about to say "That wouldn't be 
possible, childCrossPoint is part of a clone of the original 
caller!" But then I realized I did "this.getNodeList" towards the 
beginning instead of "child.getNodeList". I feel very silly right 
now, haha. Thanks for the help and your patience, guys.




Re: Strange memory corruption / codegen bug?

2016-12-11 Thread safety0ff via Digitalmars-d-learn

On Sunday, 11 December 2016 at 11:58:39 UTC, ag0aep6g wrote:


Try putting an `assert(childCrossPoint !is otherCrossPoint);` 
before the assignment. If it fails, the variables refer to the 
same node. That would explain how otherCrossPoint.left gets set.


Furthermore, I think he is calling breed on a Tree with itself.
i.e. assert(other !is this) would be a more reliable test since 
it won't be subject to randomness.




Sanitizing forms in vibe.d. How?

2016-12-11 Thread aberba via Digitalmars-d-learn
In php, I use built-in functions like 
filter_var(FILTER_VALIDATE_EMAIL, $email). There are other 
constants for different data types.


Again, there is mysqli_real_escape_string() for escaping SQL 
injection/harmful characters.



What are my options in vibe.d or even D?


Re: arsd.dom appenChild method gives assertion message

2016-12-11 Thread Erdem via Digitalmars-d-learn

Ok this seems to work as expected.

import arsd.dom;
import std.stdio;

void main()
{
   auto document = new Document();

   document.parseGarbage(`
 
 Test Document1
 
 
 This is the first paragraph of our href="test.html">test document.
 This second paragraph also has a href="test2.html">link.

 Old text
 
 `);

   auto document2 =  new Document();

   document2.parseGarbage(`
 
 Test Document
 
 
 
 
 
 
 Old text
 
 `);

   Element content = 
document2.getElementsBySelector(`div[id="content"]`)[0];


   writeln(content);
   writeln();

   Element[] firstElements = 
document.getElementsBySelector("body")[0].children;


   foreach (element; firstElements)
   {
   element.parentNode = null;
   content.appendChild(element);
   }

   writeln(document2.toString());
}


Re: Separate IP parts

2016-12-11 Thread Era Scarecrow via Digitalmars-d-learn

On Saturday, 10 December 2016 at 03:51:34 UTC, brocolis wrote:

How do I separate IP parts with dlang?

I found this very cool trick, with C++: 
http://stackoverflow.com/a/5328190


 Heh, I'd prefer to use sscanf vs using the streams.


Re: help on cartesianProduct()

2016-12-11 Thread Era Scarecrow via Digitalmars-d-learn

On Sunday, 11 December 2016 at 16:34:38 UTC, Orut wrote:
I need to be able to vary the number of ranges to feed into 
cartesianProduct() at run time. In Python, this is possible 
because I can dynamically construct a list of lists, then 
unpack this list using the unpacking operator when it is fed as 
argument to product()


 Hmmm... what kind of ranges? Are they going to be arrays? Or 
something else?


 Could make your own cartesian function range that works the same 
way, doesn't seem too complicated on that...


Re: Separate IP parts

2016-12-11 Thread notna via Digitalmars-d-learn
On Saturday, 10 December 2016 at 13:25:13 UTC, Nicholas Wilson 
wrote:

On Saturday, 10 December 2016 at 13:21:40 UTC, notna wrote:

Those statements need to be inside a function.


Feel free to post a working example or, even better, a pull 
request with one ;)


arsd.dom appenChild method gives assertion message

2016-12-11 Thread Erdem via Digitalmars-d-learn
I would like to add first documents content inside a div element 
like this.


import arsd.dom;
import std.stdio;

void main()
{

   auto document = new Document();

   document.parseGarbage(`
 
 Test Document1
 
 
 This is the first paragraph of our href="test.html">test document.
 This second paragraph also has a href="test2.html">link.

 Old text
 
 `);

   auto document2 =  new Document();

   document2.parseGarbage(`
 
 Test Document
 
 
 
 
 
 
 Old text
 
 `);

   Element content = 
document2.getElementsBySelector(`div[id="content"]`)[0];


   writeln(content);

   Element[] firstElements = 
document.getElementsBySelector("body");

   writeln (firstElements);

   content.appendChild(firstElements[0]);
}

The program compiles fine. But when I try to execute it gives an 
assertion error like this.


??:? _d_assert_msg [0x80cc048]
??:? 
_D4arsd3dom7Element11appendChildMFC4arsd3dom7ElementZ9__requireMFZv [0x80a6f62]
??:? arsd.dom.Element 
arsd.dom.Element.appendChild(arsd.dom.Element) [0x80a6df1]

??:? _Dmain [0x809f73c]
??:? 
_D2rt6dmain211_d_run_mainUiPPaPUAAaZiZ6runAllMFZ9__lambda1MFZv 
[0x80cd792]
??:? void rt.dmain2._d_run_main(int, char**, extern (C) int 
function(char[][])*).tryExec(scope void delegate()) [0x80cd6dc]
??:? void rt.dmain2._d_run_main(int, char**, extern (C) int 
function(char[][])*).runAll() [0x80cd74e]
??:? void rt.dmain2._d_run_main(int, char**, extern (C) int 
function(char[][])*).tryExec(scope void delegate()) [0x80cd6dc]

??:? _d_run_main [0x80cd66e]
??:? main [0x809fc93]
??:? __libc_start_main [0x129532]

How should one use appendChild or similiar methods of arsd.dom 
library?




Question about DUB

2016-12-11 Thread Xavier Bigand via Digitalmars-d-learn

Hi,

I am using DUB with the SDL language and I have two questions:
 1. How can I add some text file to my project? I want to add shaders 
in my Visual Project.
 2. How to make a compiler option depending on the platform and debug 
mode at the same time?


Thanks.


help on cartesianProduct()

2016-12-11 Thread Orut via Digitalmars-d-learn


Am trying to port some Python code to D and I got stumped on the 
use of cartesianProduct() from std.algorithm.setops. In Python, 
the same functionality is implemented by product() in the 
itertools module. I need to be able to vary the number of ranges 
to feed into cartesianProduct() at run time. In Python, this is 
possible because I can dynamically construct a list of lists, 
then unpack this list using the unpacking operator when it is fed 
as argument to product(). In D, as far as I can tell (D nub 
here), I can't unpack arrays (no expand property), and I can't 
dynamically change the content of tuples (fixed at compile time). 
A possible hack is to create different versions of the function 
call (each with a fixed number of arguments), say cases under a 
switch, but this gets ugly if I anticipate a large number of 
possible scenarios. Would appreciate any idea. Thanks.


Re: Strange memory corruption / codegen bug?

2016-12-11 Thread rikki cattermole via Digitalmars-d-learn

On 12/12/2016 12:43 AM, TheGag96 wrote:

On Sunday, 11 December 2016 at 11:17:50 UTC, rikki cattermole wrote:

Not public, please pastebin.


https://github.com/TheGag96/evo-pacman/blob/master/source/pacman/tree.d#L135


I just put it on GitHub. No idea why the repo wasn't public even after I
set it to be public...


Can you please create a function that will cause this error to occur?
E.g. generate the source code from the tree.

That way we can test against it.


Re: Strange memory corruption / codegen bug?

2016-12-11 Thread ag0aep6g via Digitalmars-d-learn

On 12/11/2016 12:43 PM, TheGag96 wrote:

On Sunday, 11 December 2016 at 11:17:50 UTC, rikki cattermole wrote:

Not public, please pastebin.


https://github.com/TheGag96/evo-pacman/blob/master/source/pacman/tree.d#L135


I just put it on GitHub. No idea why the repo wasn't public even after I
set it to be public...


Try putting an `assert(childCrossPoint !is otherCrossPoint);` before the 
assignment. If it fails, the variables refer to the same node. That 
would explain how otherCrossPoint.left gets set.


Also, what compiler are you using, what version of it, and on which 
platform?


Re: Strange memory corruption / codegen bug?

2016-12-11 Thread TheGag96 via Digitalmars-d-learn
On Sunday, 11 December 2016 at 11:17:50 UTC, rikki cattermole 
wrote:

Not public, please pastebin.


https://github.com/TheGag96/evo-pacman/blob/master/source/pacman/tree.d#L135

I just put it on GitHub. No idea why the repo wasn't public even 
after I set it to be public...


Re: Strange memory corruption / codegen bug?

2016-12-11 Thread rikki cattermole via Digitalmars-d-learn

On 12/12/2016 12:15 AM, TheGag96 wrote:

I was porting my Evolutionary Computing homework written in Python over
to D, and I've come across this bug I cannot for the life of me figure out.

https://gitlab.com/TheGag96/evo-pacman/blob/master/source/pacman/tree.d#L139


Not public, please pastebin.


I don't think I could cut this down to a smaller reproducible scenario
due to how bizarre and specific this problem is, so I apologize in
advance if this is hard to follow.

Basically, imagine that I have a binary tree class where, due to how my
program is set up, each node will have either children on both the left
and right side or no children at all (the latter represented by a value
of null for Tree's left and right member). I have a member function
called "dup" -- marked const, mind you -- that just returns a deep copy
of the tree and SHOULDN'T make any changes to the calling object.

I call this function a couple different places and it appears to
function okay, but at the spot I linked, if I call .dup here, it will
corrupt one of the tree's nodes and put something in its left member for
no apparent reason. This oddly doesn't happen on every Tree calling this
function. This could definitely be some stupid mistake on my part, but
the fact that calling a const-marked function changes the state of the
calling object makes me think something else is afoot here...

I would greatly appreciate anyone who would be willing to take a look at
this. This bug is driving me absolutely nuts.




Strange memory corruption / codegen bug?

2016-12-11 Thread TheGag96 via Digitalmars-d-learn
I was porting my Evolutionary Computing homework written in 
Python over to D, and I've come across this bug I cannot for the 
life of me figure out.


https://gitlab.com/TheGag96/evo-pacman/blob/master/source/pacman/tree.d#L139

I don't think I could cut this down to a smaller reproducible 
scenario due to how bizarre and specific this problem is, so I 
apologize in advance if this is hard to follow.


Basically, imagine that I have a binary tree class where, due to 
how my program is set up, each node will have either children on 
both the left and right side or no children at all (the latter 
represented by a value of null for Tree's left and right member). 
I have a member function called "dup" -- marked const, mind you 
-- that just returns a deep copy of the tree and SHOULDN'T make 
any changes to the calling object.


I call this function a couple different places and it appears to 
function okay, but at the spot I linked, if I call .dup here, it 
will corrupt one of the tree's nodes and put something in its 
left member for no apparent reason. This oddly doesn't happen on 
every Tree calling this function. This could definitely be some 
stupid mistake on my part, but the fact that calling a 
const-marked function changes the state of the calling object 
makes me think something else is afoot here...


I would greatly appreciate anyone who would be willing to take a 
look at this. This bug is driving me absolutely nuts.


Re: faster "stringification"

2016-12-11 Thread Nicholas Wilson via Digitalmars-d-learn

On Sunday, 11 December 2016 at 10:01:21 UTC, Orut wrote:
On Sunday, 11 December 2016 at 02:46:58 UTC, Nicholas Wilson 
wrote:


join performs allocations which is probably the reason for its 
slowness. There is joiner (in std.algorithm.iterations) that 
lazily performs the join, (though in the case of this 
"benchmark" will be cheating because you don't do anything 
with the result, print it to get a more fair comparison) 
avoiding allocation.


see also appender (in std.array) for fast concatenation.


Thanks, Stefan and Nicholas. I think joiner did the trick (for 
50M iterations, ~2s for D, ~17s for Python).


Excellent. that seem more like the numbers i would expect.


Re: faster "stringification"

2016-12-11 Thread Orut via Digitalmars-d-learn
On Sunday, 11 December 2016 at 02:46:58 UTC, Nicholas Wilson 
wrote:


join performs allocations which is probably the reason for its 
slowness. There is joiner (in std.algorithm.iterations) that 
lazily performs the join, (though in the case of this 
"benchmark" will be cheating because you don't do anything with 
the result, print it to get a more fair comparison) avoiding 
allocation.


see also appender (in std.array) for fast concatenation.


Thanks, Stefan and Nicholas. I think joiner did the trick (for 
50M iterations, ~2s for D, ~17s for Python).