Re: Stack overflow

2012-06-23 Thread Namespace

On Friday, 22 June 2012 at 23:01:47 UTC, David wrote:

Am 22.06.2012 23:11, schrieb Namespace:
debugger), but my guess is that Namespace just doesn't know 
how to do

that or
that it's even possible - hence his frustration with not 
getting any

information.


Exactly.

And I use VisualD and the standard debugger of it.



Sorry but I would learn how to use the debugger instead (it 
helps a lot!), instead of implementing features which emulate 
C++ behaviour (according to your blog) and will confuse people, 
e.g. if you documentate that, people will be like *huh this 
cannot be null, is this a feature I don't know?* and end up 
confused.


I would prefer NullPointer Exceptions and / or not null types 
rather than playing around with the debugger.

That's the first step.


Re: Writing .di files

2012-06-23 Thread 1100110
On Fri, 22 Jun 2012 13:13:52 -0500, Minas Mina  
minas_mina1...@hotmail.co.uk wrote:


I'm sorry, what I meant was how to interface to C code. Sorry for  
writing it in a bad way.


Do I just declare the C code as extern and then link together with the C  
.lib/.o/.so file? (I'm in Ubuntu)


What about the stuff that is in header files?


Take a look at the Deimos Repos on Github.   
https://github.com/D-Programming-Deimos


Take a look at ncurses there.  There are two folders, C and deimos.
It needs to be cleaned up, but you'll notice that the .d files are really  
nothing more than a direct

translation of the C header files.

Include the translated header and link with the C library.

If you have a specific library that you want to interface to, it's pretty  
easy once you realize what needs to be done.


Global variables in C are truly global, but in D they are Thread Local.
So certain variables will need to be immutable, shared, __gshared,  
whatever.


Everything will need to be wrapped with 'extern (C)'

The biggest issues your likely to face are the Thread Local Storage issues  
and

the preprocessor directives.

dmd -vtls will print out the problem vars...

You can translate partial header files, just make sure you get all the  
relevant bits.


If you wanna play around, then add this to a file and compile it.

import std.string: toStringz;

extern (C) int system(in char* command);

auto newTerm(S:string)(S command)
{   return system(command.toStringz);   }

Yeah, I hate dealing with toStringz directly

Unlike the functions in std.process, this one will allow interactive  
programs to be run from inside a D file.

try newTerm(vim ~/.vimrc);

Yeah i just killed my terminal making sure std.process wouldn't work with  
that.






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


Re: Stack overflow

2012-06-23 Thread David

Am 23.06.2012 07:27, schrieb Namespace:

I would prefer NullPointer Exceptions and / or not null types rather
than playing around with the debugger.
That's the first step.


Yeah but not implemented by a library.




Re: Stack overflow

2012-06-23 Thread Namespace

On Saturday, 23 June 2012 at 09:53:35 UTC, David wrote:

Am 23.06.2012 07:27, schrieb Namespace:
I would prefer NullPointer Exceptions and / or not null types 
rather

than playing around with the debugger.
That's the first step.


Yeah but not implemented by a library.


Not yet. ;)


phobos and splitting things... but not with whitespace.

2012-06-23 Thread Chad J

http://dlang.org/phobos/std_array.html#splitter

The first thing I don't understand is why splitter is in /std.array/ and 
yet only works on /strings/.  It is defined in terms of whitespace, and 
I don't understand how whitespace is well-defined for things besides 
text.  Why wouldn't it be in std.string?


That said, I'd like to split on something that isn't whitespace.  So 
where's auto splitter(C)(C[] s, C[] delim)??  Is there a hole in 
functionality?


The next thing I want to do is split on whitespace, but only once, and 
recover the tail.  I want to write this function:


string snip(string text)
{
string head, tail;
head = getHead(text, // -- snip --, tail);
return tail;
}

I would expect these functions to exist:
auto getHead(C)(C[] s, C[] delim, ref C[] tail);
auto getHead(C)(C[] s, C[] delim);
auto getTail(C)(C[] s, C[] delim);

Maybe even this, though it could be a bit redundant:
auto getTail(C)(C[] s, C[] delim, ref C[] head);

Do these exist in phobos?  Otherwise, is it a hole in the functionality 
or some kind of intentional design minimalism?


Re: phobos and splitting things... but not with whitespace.

2012-06-23 Thread simendsjo
On Sat, 23 Jun 2012 17:19:59 +0200, Chad J  
chadjoan@__spam.is.bad__gmail.com wrote:

http://dlang.org/phobos/std_array.html#splitter


The first thing I don't understand is why splitter is in /std.array/ and  
yet only works on /strings/.  It is defined  in terms of whitespace,  
and I don't understand how whitespace is well-defined for things besides  
text.  Why wouldn't  it be in std.string?


See http://dlang.org/phobos/std_algorithm.html#splitter


I would expect these functions to exist:
auto getHead(C)(C[] s, C[] delim, ref C[] tail);
auto getHead(C)(C[] s, C[] delim);
auto getTail(C)(C[] s, C[] delim);


As head is simply splitter(..)[0] and tail splitter(...)[1..$], extra  
functions could be implemented much like this


@property T head(T[] arr) { return arr.front; }
@property T[] tail(T[] arr) { return arr[1..$]; }

..and UFCS takes care of the rest:
auto fields = splitter(...);
auto head = fields.head;
auto tail = fields.tail;


Re: phobos and splitting things... but not with whitespace.

2012-06-23 Thread Chad J
I'm realizing that if I want to remove exactly one line from a string of 
text and make no assumptions about the type of newline (\n or \r\n 
or \r) and without scanning the rest of the text then I'm not sure how 
to do this with a single call to phobos functions.  I'd have to use 
indexOf and do a bunch of twiddling and maybe look ahead a character. 
It seems unusually complicated for such a simple operation.


Re: phobos and splitting things... but not with whitespace.

2012-06-23 Thread Chad J

On 06/23/2012 11:31 AM, simendsjo wrote:

On Sat, 23 Jun 2012 17:19:59 +0200, Chad J
chadjoan@__spam.is.bad__gmail.com wrote:

http://dlang.org/phobos/std_array.html#splitter



The first thing I don't understand is why splitter is in /std.array/
and yet only works on /strings/. It is defined  in terms of
whitespace, and I don't understand how whitespace is well-defined for
things besides text. Why wouldn't  it be in std.string?


See http://dlang.org/phobos/std_algorithm.html#splitter


I would expect these functions to exist:
auto getHead(C)(C[] s, C[] delim, ref C[] tail);
auto getHead(C)(C[] s, C[] delim);
auto getTail(C)(C[] s, C[] delim);


As head is simply splitter(..)[0] and tail splitter(...)[1..$], extra
functions could be implemented much like this

@property T head(T[] arr) { return arr.front; }
@property T[] tail(T[] arr) { return arr[1..$]; }

..and UFCS takes care of the rest:
auto fields = splitter(...);
auto head = fields.head;
auto tail = fields.tail;


But I don't want tail as an array.  Assume that arr is HUGE and scanning 
the rest of it is a bad idea.  join(arr[1..$]) then becomes a slow 
operation: O(n) when I could have O(1).


Re: phobos and splitting things... but not with whitespace.

2012-06-23 Thread simendsjo
On Sat, 23 Jun 2012 17:39:55 +0200, Chad J  
chadjoan@__spam.is.bad__gmail.com wrote:



On 06/23/2012 11:31 AM, simendsjo wrote:

On Sat, 23 Jun 2012 17:19:59 +0200, Chad J
chadjoan@__spam.is.bad__gmail.com wrote:

http://dlang.org/phobos/std_array.html#splitter



The first thing I don't understand is why splitter is in /std.array/
and yet only works on /strings/. It is defined  in terms of
whitespace, and I don't understand how whitespace is well-defined for
things besides text. Why wouldn't  it be in std.string?


See http://dlang.org/phobos/std_algorithm.html#splitter


I would expect these functions to exist:
auto getHead(C)(C[] s, C[] delim, ref C[] tail);
auto getHead(C)(C[] s, C[] delim);
auto getTail(C)(C[] s, C[] delim);


As head is simply splitter(..)[0] and tail splitter(...)[1..$], extra
functions could be implemented much like this

@property T head(T[] arr) { return arr.front; }
@property T[] tail(T[] arr) { return arr[1..$]; }

..and UFCS takes care of the rest:
auto fields = splitter(...);
auto head = fields.head;
auto tail = fields.tail;


But I don't want tail as an array.  Assume that arr is HUGE and scanning  
the rest of it is a bad idea.  join(arr[1..$]) then becomes a slow  
operation: O(n) when I could have O(1).



Looking for findSplit? http://dlang.org/phobos/std_algorithm.html#findSplit


Re: Strange exception using threads

2012-06-23 Thread simendsjo
On Sat, 23 Jun 2012 18:05:47 +0200, Minas Mina  
minas_mina1...@hotmail.co.uk wrote:


I am using a secondary thread to send messages to it so it can print  
those messages.


import std.stdio;
import std.concurrency;

void main()
{
auto low = 0, high = 10;

auto tid = spawn(writer);

foreach(i; low..high)
{
writeln(Main thread: , i);
tid.send(tid, i);
}
}

void writer()
{
while( true )
{
receive(
(Tid id, int i)
{
writeln(Secondary thread: , i);
}
);
}
}

This is the result:
Main thread: 0
Main thread: 1
Main thread: 2
Main thread: 3
Main thread: 4
Main thread: 5
Main thread: 6
Main thread: 7
Main thread: 8
Main thread: 9
Secondary thread: 0
Secondary thread: 1
Secondary thread: 2
Secondary thread: 3
Secondary thread: 4
Secondary thread: 5
Secondary thread: 6
Secondary thread: 7
Secondary thread: 8
Secondary thread: 9
std.concurrency.OwnerTerminated@std/concurrency.d(248): Owner terminated

/home/minas/Projects/D/D_Test/D_Test/bin/Debug/D_Test(bool  
std.concurrency.MessageBox.get!(void function(std.concurrency.Tid,  
int)*).get(scope void function(std.concurrency.Tid, int)*).bool  
onControlMsg(ref std.concurrency.Message)+0x2a) [0x437016]
/home/minas/Projects/D/D_Test/D_Test/bin/Debug/D_Test(bool  
std.concurrency.MessageBox.get!(void function(std.concurrency.Tid,  
int)*).get(scope void function(std.concurrency.Tid, int)*).bool scan(ref  
std.concurrency.List!(std.concurrency.Message).List)+0x68) [0x437084]
/home/minas/Projects/D/D_Test/D_Test/bin/Debug/D_Test(bool  
std.concurrency.MessageBox.get!(void function(std.concurrency.Tid,  
int)*).get(scope void function(std.concurrency.Tid, int)*)+0x88)  
[0x436c20]
/home/minas/Projects/D/D_Test/D_Test/bin/Debug/D_Test(void  
std.concurrency.receive!(void function(std.concurrency.Tid,  
int)*).receive(void function(std.concurrency.Tid, int)*)+0x32) [0x436b86]
/home/minas/Projects/D/D_Test/D_Test/bin/Debug/D_Test(void  
main.writer()+0x13) [0x43066b]
/home/minas/Projects/D/D_Test/D_Test/bin/Debug/D_Test(_D3std11concurrency11__T6_spawnZ6_spawnFbPFZvZS3std11concurrency3Tid4execMFZv+0x45)  
[0x430941]
/home/minas/Projects/D/D_Test/D_Test/bin/Debug/D_Test(void  
core.thread.Thread.run()+0x2a) [0x4470fe]
/home/minas/Projects/D/D_Test/D_Test/bin/Debug/D_Test(thread_entryPoint+0xf3)  
[0x446e97]


The program runs correctly but then boom! Does anyone know why?


You can notify child threads that their owner terminates so they can  
finish up


import std.stdio;
import std.concurrency;

void main()
{
auto low = 0, high = 10;

auto tid = spawnLinked(writer);

foreach(i; low..high)
{
writeln(Main thread: , i);
tid.send(tid, i);
}
writeln(need to gracefully terminate child threads);
tid.send(tid, Term());
writeln(term sent);
}

struct Term {}
void writer()
{
bool done;
while( !done )
{
receive(
(Tid id, int i)
{
writeln(Secondary thread: , i);
},
(Tid id, Term term)
{
writeln(Owner terminated, so do we);
done = true;
}
);
}
}


Main thread: 0
Main thread: 1
Main thread: 2
Main thread: 3
Main thread: 4
Secondary thread: 0
Secondary thread: 1
Secondary thread: 2
Secondary thread: 3
Secondary thread: 4
Main thread: 5
Main thread: 6
Main thread: 7
Main thread: 8
Main thread: 9
Secondary thread: 5
Secondary thread: 6
need to gracefully terminate child threads
term sent
Secondary thread: 7
Secondary thread: 8
Secondary thread: 9
Owner terminated, so do we


Re: Strange exception using threads

2012-06-23 Thread Ali Çehreli

On 06/23/2012 09:05 AM, Minas Mina wrote:
 I am using a secondary thread to send messages to it so it can print
 those messages.

 std.concurrency.OwnerTerminated@std/concurrency.d(248): Owner terminated

The OwnerTerminated exception is thrown when a worker attempts to 
receive a message to notify it about the fact that its owner has been 
terminated.


There are ways to deal with the situation:

- The worker can catch this particular exception

- The worker can catch this exception as a message

- The owner can send a special YouAreDone :) message to the worker so it 
no longer attempts to receive messages and exits gracefully


- More?

Here is the second method as described in TDPL's concurrency chapter, 
which is available online:


  http://www.informit.com/articles/article.aspx?p=1609144

void writer()
{
bool done = false;

while( !done )
{
receive(
(Tid id, int i)
{
writeln(Secondary thread: , i);
},

(OwnerTerminated exc) // - as a message
{
done = true;
}
);
}
}

Ali

--
D Programming Language Tutorial: http://ddili.org/ders/d.en/index.html



Re: Strange exception using threads

2012-06-23 Thread simendsjo

On Sat, 23 Jun 2012 18:29:37 +0200, simendsjo simend...@gmail.com wrote:


auto tid = spawnLinked(writer);


auto tid = spawn(writer); of course


Re: Strange exception using threads

2012-06-23 Thread simendsjo

On Sat, 23 Jun 2012 18:29:50 +0200, Ali Çehreli acehr...@yahoo.com wrote:


receive(
(Tid id, int i)
{
writeln(Secondary thread: , i);
},
 (OwnerTerminated exc) // - as a message
{
done = true;
}
);


Nice. I thought you had to send an explicit message.


Re: phobos and splitting things... but not with whitespace.

2012-06-23 Thread Chad J

On 06/23/2012 11:44 AM, simendsjo wrote:

On Sat, 23 Jun 2012 17:39:55 +0200, Chad J
chadjoan@__spam.is.bad__gmail.com wrote:


On 06/23/2012 11:31 AM, simendsjo wrote:

On Sat, 23 Jun 2012 17:19:59 +0200, Chad J
chadjoan@__spam.is.bad__gmail.com wrote:

http://dlang.org/phobos/std_array.html#splitter



The first thing I don't understand is why splitter is in /std.array/
and yet only works on /strings/. It is defined  in terms of
whitespace, and I don't understand how whitespace is well-defined for
things besides text. Why wouldn't  it be in std.string?


See http://dlang.org/phobos/std_algorithm.html#splitter


I would expect these functions to exist:
auto getHead(C)(C[] s, C[] delim, ref C[] tail);
auto getHead(C)(C[] s, C[] delim);
auto getTail(C)(C[] s, C[] delim);


As head is simply splitter(..)[0] and tail splitter(...)[1..$], extra
functions could be implemented much like this

@property T head(T[] arr) { return arr.front; }
@property T[] tail(T[] arr) { return arr[1..$]; }

..and UFCS takes care of the rest:
auto fields = splitter(...);
auto head = fields.head;
auto tail = fields.tail;


But I don't want tail as an array. Assume that arr is HUGE and
scanning the rest of it is a bad idea. join(arr[1..$]) then becomes a
slow operation: O(n) when I could have O(1).



Looking for findSplit? http://dlang.org/phobos/std_algorithm.html#findSplit


Cool, that's what I want!

Now if I could find the elegant way to remove exactly one line from the 
text without scanning the text after it...


Re: Strange exception using threads

2012-06-23 Thread Minas Mina

Thank you very much :)

I like the you are done :) approach!


Re: phobos and splitting things... but not with whitespace.

2012-06-23 Thread simendsjo
On Sat, 23 Jun 2012 18:50:05 +0200, Chad J  
chadjoan@__spam.is.bad__gmail.com wrote:


Looking for findSplit?  
http://dlang.org/phobos/std_algorithm.html#findSplit

 Cool, that's what I want!
 Now if I could find the elegant way to remove exactly one line from the  
text without scanning the text after it...


Isn't that exactly what findSplit does? It doesn't have to search the rest  
of the string after the match, it just returns a slice of the rest of the  
array (I guess - haven't read the code)


Re: phobos and splitting things... but not with whitespace.

2012-06-23 Thread simendsjo

On Sat, 23 Jun 2012 18:56:24 +0200, simendsjo simend...@gmail.com wrote:

On Sat, 23 Jun 2012 18:50:05 +0200, Chad J  
chadjoan@__spam.is.bad__gmail.com wrote:


Looking for findSplit?  
http://dlang.org/phobos/std_algorithm.html#findSplit

 Cool, that's what I want!
 Now if I could find the elegant way to remove exactly one line from  
the text without scanning the text after it...


Isn't that exactly what findSplit does? It doesn't have to search the  
rest of the string after the match, it just returns a slice of the rest  
of the array (I guess - haven't read the code)



import std.stdio, std.algorithm;

void main() {
auto text = 1\n2\n3\n4;
auto res = text.findSplit(\n);

auto pre = res[0];
assert(pre.ptr == text.ptr); // no copy for pre match

auto match = res[1];
assert(match.ptr == text[1]); // no copy for needle

auto post = res[2];
assert(post.ptr == text[2]); // no copy for post match
assert(post.length == 5);
}


Re: phobos and splitting things... but not with whitespace.

2012-06-23 Thread Chad J

On 06/23/2012 01:02 PM, simendsjo wrote:

On Sat, 23 Jun 2012 18:56:24 +0200, simendsjo simend...@gmail.com wrote:


On Sat, 23 Jun 2012 18:50:05 +0200, Chad J
chadjoan@__spam.is.bad__gmail.com wrote:


Looking for findSplit?
http://dlang.org/phobos/std_algorithm.html#findSplit
Cool, that's what I want!
Now if I could find the elegant way to remove exactly one line from
the text without scanning the text after it...


Isn't that exactly what findSplit does? It doesn't have to search the
rest of the string after the match, it just returns a slice of the
rest of the array (I guess - haven't read the code)



import std.stdio, std.algorithm;

void main() {
auto text = 1\n2\n3\n4;
auto res = text.findSplit(\n);

auto pre = res[0];
assert(pre.ptr == text.ptr); // no copy for pre match

auto match = res[1];
assert(match.ptr == text[1]); // no copy for needle

auto post = res[2];
assert(post.ptr == text[2]); // no copy for post match
assert(post.length == 5);
}


Close... the reason findSplit doesn't work is because a new line could 
be \n or it could be \r\n or it could be \r.


Re: phobos and splitting things... but not with whitespace.

2012-06-23 Thread Chad J

On 06/23/2012 01:24 PM, Chad J wrote:

On 06/23/2012 01:02 PM, simendsjo wrote:

On Sat, 23 Jun 2012 18:56:24 +0200, simendsjo simend...@gmail.com
wrote:


On Sat, 23 Jun 2012 18:50:05 +0200, Chad J
chadjoan@__spam.is.bad__gmail.com wrote:


Looking for findSplit?
http://dlang.org/phobos/std_algorithm.html#findSplit
Cool, that's what I want!
Now if I could find the elegant way to remove exactly one line from
the text without scanning the text after it...


Isn't that exactly what findSplit does? It doesn't have to search the
rest of the string after the match, it just returns a slice of the
rest of the array (I guess - haven't read the code)



import std.stdio, std.algorithm;

void main() {
auto text = 1\n2\n3\n4;
auto res = text.findSplit(\n);

auto pre = res[0];
assert(pre.ptr == text.ptr); // no copy for pre match

auto match = res[1];
assert(match.ptr == text[1]); // no copy for needle

auto post = res[2];
assert(post.ptr == text[2]); // no copy for post match
assert(post.length == 5);
}


Close... the reason findSplit doesn't work is because a new line could
be \n or it could be \r\n or it could be \r.


As an additional note: I could probably do this easily if I had a 
function like findSplit where the predicate is used /instead/ of a 
delimiter.  So like this:


auto findSplit(alias pred = a, R)(R haystack);
...
auto tuple = findSplit!(`a == \n || a == \r\n || a == \r`)(text);
return tuple[2];


Re: phobos and splitting things... but not with whitespace.

2012-06-23 Thread simendsjo
On Sat, 23 Jun 2012 19:52:32 +0200, Chad J  
chadjoan@__spam.is.bad__gmail.com wrote:




As an additional note: I could probably do this easily if I had a  
function like findSplit where the predicate is used /instead/ of a  
delimiter.  So like this:

 auto findSplit(alias pred = a, R)(R haystack);
...
auto tuple = findSplit!(`a == \n || a == \r\n || a == \r`)(text);
return tuple[2];


I don't think it can match on ranges, but it's pretty trivial to implement  
something that would work for your case


import std.array, std.algorithm, std.typecons;

auto newlineSplit(string data) {
auto rest = data.findAmong(\r\n);
if(!rest.empty) { // found
auto pre = data[0..data.length-rest.length];
string match;
if(rest.front == '\r'  (rest.length  1  rest[1] == '\n')) {  
// \r\n

match = rest[0..2];
rest = rest[2..$];
} else { // \r or \n
match = rest[0..1];
rest = rest[1..$];
}
return tuple(pre, match, rest);
} else {
return tuple(data, , );
}
}
unittest {
auto text = 1\n2\r\n3\r4;
auto res = text.newlineSplit();
assert(res[0] == 1);
assert(res[1] == \n);
assert(res[2] == 2\r\n3\r4);

res = res[2].newlineSplit();
assert(res[0] == 2);
assert(res[1] == \r\n);
assert(res[2] == 3\r4);

res = res[2].newlineSplit();
assert(res[0] == 3);
assert(res[1] == \r);
assert(res[2] == 4);

res = res[2].newlineSplit();
assert(res[0] == 4);
assert(res[1] == );
assert(res[2] == );
}


Passing a ubyte[] to a function taking a 'ref ubyte[16]'

2012-06-23 Thread Johannes Pfau
I'm working on the new design for std.hash and I hit an interesting
problem:

The OOP interface has to take buffers as slices with unknown length, as
the length differs between hashes and I have to put a common function
declaration in a interface. So I have this:

interface Digest
{
ubyte[] finish(ubyte[] buf);
}


Now the template API can and should use the correct type, so there
finish is defined like this:

struct MD5
{
void finish(ref ubyte[16] data);
}


And the interface implementation for MD5 has to call the MD5 structs
finish function:

class MD5Digest : Digest
{
private MD5 _digest;

ubyte[] finish(ubyte[] buf)
{
enforce(buf.length = 16);
_digest.finish(buf); //How to do this?
}
}


So how to pass a ubyte[] to a function expecting a ref ubyte[16]
without allocating/using any extra memory (Not even stack)?

This seems to work, but it's very ugly:

_digest.finish(*cast(ubyte[16]*)buf.ptr);


I thought this might create a temporary, but it passes all unittests,
so it seems to work?


Re: Passing a ubyte[] to a function taking a 'ref ubyte[16]'

2012-06-23 Thread simendsjo
On Sat, 23 Jun 2012 20:23:26 +0200, Johannes Pfau nos...@example.com  
wrote:



So how to pass a ubyte[] to a function expecting a ref ubyte[16]
without allocating/using any extra memory (Not even stack)?
This seems to work, but it's very ugly:

_digest.finish(*cast(ubyte[16]*)buf.ptr);

I thought this might create a temporary, but it passes all unittests,
so it seems to work?


I don't have a clue, but the following seems to work as well :)

void sarr(ref ubyte[1] a) {
darr(a.ptr[0..a.length]);
}

void darr(ubyte[] a) {
a[0] = 2;
}

void main() {
ubyte[1] a = [1];
sarr(a);
assert(a[0] == 2);
}


Re: Passing a ubyte[] to a function taking a 'ref ubyte[16]'

2012-06-23 Thread simendsjo

On Sat, 23 Jun 2012 20:33:09 +0200, simendsjo simend...@gmail.com wrote:

On Sat, 23 Jun 2012 20:23:26 +0200, Johannes Pfau nos...@example.com  
wrote:



So how to pass a ubyte[] to a function expecting a ref ubyte[16]
without allocating/using any extra memory (Not even stack)?
This seems to work, but it's very ugly:

_digest.finish(*cast(ubyte[16]*)buf.ptr);

I thought this might create a temporary, but it passes all unittests,
so it seems to work?


I don't have a clue, but the following seems to work as well :)

void sarr(ref ubyte[1] a) {
 darr(a.ptr[0..a.length]);
}

void darr(ubyte[] a) {
 a[0] = 2;
}

void main() {
 ubyte[1] a = [1];
 sarr(a);
 assert(a[0] == 2);
}


Oops.. Just saw it should have been the other way around. Never mind me


Re: phobos and splitting things... but not with whitespace.

2012-06-23 Thread Chad J

On 06/23/2012 02:17 PM, simendsjo wrote:

On Sat, 23 Jun 2012 19:52:32 +0200, Chad J
chadjoan@__spam.is.bad__gmail.com wrote:



As an additional note: I could probably do this easily if I had a
function like findSplit where the predicate is used /instead/ of a
delimiter. So like this:
auto findSplit(alias pred = a, R)(R haystack);
...
auto tuple = findSplit!(`a == \n || a == \r\n || a == \r`)(text);
return tuple[2];


I don't think it can match on ranges, but it's pretty trivial to
implement something that would work for your case

import std.array, std.algorithm, std.typecons;

auto newlineSplit(string data) {
auto rest = data.findAmong(\r\n);
if(!rest.empty) { // found
auto pre = data[0..data.length-rest.length];
string match;
if(rest.front == '\r'  (rest.length  1  rest[1] == '\n')) { // \r\n
match = rest[0..2];
rest = rest[2..$];
} else { // \r or \n
match = rest[0..1];
rest = rest[1..$];
}
return tuple(pre, match, rest);
} else {
return tuple(data, , );
}
}
unittest {
auto text = 1\n2\r\n3\r4;
auto res = text.newlineSplit();
assert(res[0] == 1);
assert(res[1] == \n);
assert(res[2] == 2\r\n3\r4);

res = res[2].newlineSplit();
assert(res[0] == 2);
assert(res[1] == \r\n);
assert(res[2] == 3\r4);

res = res[2].newlineSplit();
assert(res[0] == 3);
assert(res[1] == \r);
assert(res[2] == 4);

res = res[2].newlineSplit();
assert(res[0] == 4);
assert(res[1] == );
assert(res[2] == );
}


Hey, thanks for doing all of that.  I didn't expect you to write all of 
that.


Once I've established that the issue isn't just a lack of learning on my 
part, my subsequent objective is filling any missing functionality in 
phobos.  IMO the take away a single line thing should be 
accomplishable with a single concise expression.  Then there should be a 
function in std.string that contains that single expression and wraps it 
in easy-to-find documentation.  This kind of thing is a fairly common 
operation.  Otherwise, I find it odd that there is a function to split 
up an arbitrary number of lines but no function to split off only one!


Also, any function that works with whitespace should have 
versions/variants that work with arbitrary delimiters.  Not unless it is 
impossible to generalize it that way for some reason.  If the variants 
are found in a separate module, then the documentation should reference 
them.


Re: phobos and splitting things... but not with whitespace.

2012-06-23 Thread simendsjo
On Sat, 23 Jun 2012 20:41:29 +0200, Chad J  
chadjoan@__spam.is.bad__gmail.com wrote:




Hey, thanks for doing all of that.  I didn't expect you to write all of  
that.

np

 Once I've established that the issue isn't just a lack of learning on  
my part, my subsequent objective is filling any missing functionality in  
phobos.  IMO the take away a single line thing should be  
accomplishable with a single concise expression.  Then there should be a  
function in std.string that contains that single expression and wraps it  
in easy-to-find documentation.  This kind of thing is a fairly common  
operation.  Otherwise, I find it odd that there is a function to split  
up an arbitrary number of lines but no function to split off only one!
 Also, any function that works with whitespace should have  
versions/variants that work with arbitrary delimiters.  Not unless it is  
impossible to generalize it that way for some reason.  If the variants  
are found in a separate module, then the documentation should reference  
them.



The problem here is there isn't a version of findSplit only taking a  
predicate and not a needle.
If it had an overload just taking a function, you could have solved it by  
writing:

auto res = myText.findSplit!(a = a.startsWith(\r\n, \n, \r));


Learning asynchronous sockets in D (well actually C...)

2012-06-23 Thread Jarl André
The learning curve has been from like zero to something. I am 
still grasping for some fundamental knowledge that I need to 
fully get whats going on. Had to read documentation for sockets 
in C to understand anything at all. That says a lot. Coming from 
BufferedReader hell in Java and did never get Java nio.


Looking at the Splat library, it was crude, and I didn't like the 
fact that it was D1. So I converted it to D2. Happy now, it 
works. My plan is to implement splat in the background inside of 
my simple socket server library. At this moment I have made a 
simple parrot server that currently replaces my old server in 
echoserver.d. So a bit of testing going on there atm.


I have learned the pattern one person that runs back and forth 
with a bucket and fills it up and dumps it accordingly for each 
socket is actually very effective. This is not very different 
from how i have implemented my simple socket server, except in my 
version i spawned threads that communicated and waited directly 
on the sockets. The parent thread only accepts sockets, I had 
Socket.select(sset,null,null); that in effect allows my spawned 
threads to do anything they wish for. Think I'll replace the 
inner contents of my old simple server with splat code.


Any pointers to what I should do next? Vibed is not an option. I 
do not like the approach. To much hassle for same result. I thnk 
the main reason for not using Vibed is that it is tightly 
connected to a toolchain. I like to have software that is 
independent.


https://github.com/jarlah/d2-simple-socket-server


Re: phobos and splitting things... but not with whitespace.

2012-06-23 Thread simendsjo
On Sat, 23 Jun 2012 20:41:29 +0200, Chad J  
chadjoan@__spam.is.bad__gmail.com wrote:


IMO the take away a single line thing should be accomplishable with a  
single concise expression


This takes a range to match against, so much like startsWith:

auto findSplitAny(Range, Ranges...)(Range data, Ranges matches) {
auto rest = data;
for(; !rest.empty; rest.popFront()) {
foreach(match; matches) {
if(rest.startsWith(match)) {
auto restStart = data.length-rest.length;
auto pre = data[0..restStart];
// we'll fetch it from the data instead of using the  
supplied

// match to be consistent with findSplit
auto dataMatch = data[restStart..restStart+match.length];
auto post = rest[match.length..$];
return tuple(pre, dataMatch, post);
}
}
}
return tuple(data, Range.init, Range.init);
}
unittest {
auto text = 1\n2\r\n3\r4;

auto res = text.findSplitAny(\r\n, \n, \r);
assert(res[0] == 1);
assert(res[1] == \n);
assert(res[2] == 2\r\n3\r4);

res = res[2].findSplitAny(\r\n, \n, \r);
assert(res[0] == 2);
assert(res[1] == \r\n);
assert(res[2] == 3\r4);

res = res[2].findSplitAny(\r\n, \n, \r);
assert(res[0] == 3);
assert(res[1] == \r);
assert(res[2] == 4);

res = res[2].findSplitAny(\r\n, \n, \r);
assert(res[0] == 4);
assert(res[1] == );
assert(res[2] == );
}


Re: phobos and splitting things... but not with whitespace.

2012-06-23 Thread Chad J

On 06/23/2012 02:53 PM, simendsjo wrote:

On Sat, 23 Jun 2012 20:41:29 +0200, Chad J
chadjoan@__spam.is.bad__gmail.com wrote:



Hey, thanks for doing all of that. I didn't expect you to write all of
that.

np


Once I've established that the issue isn't just a lack of learning on
my part, my subsequent objective is filling any missing functionality
in phobos. IMO the take away a single line thing should be
accomplishable with a single concise expression. Then there should be
a function in std.string that contains that single expression and
wraps it in easy-to-find documentation. This kind of thing is a fairly
common operation. Otherwise, I find it odd that there is a function to
split up an arbitrary number of lines but no function to split off
only one!
Also, any function that works with whitespace should have
versions/variants that work with arbitrary delimiters. Not unless it
is impossible to generalize it that way for some reason. If the
variants are found in a separate module, then the documentation should
reference them.



The problem here is there isn't a version of findSplit only taking a
predicate and not a needle.
If it had an overload just taking a function, you could have solved it
by writing:
auto res = myText.findSplit!(a = a.startsWith(\r\n, \n, \r));


True, although I'm a bigger fan of the compile-time alias predicate 
because of it's superior inline-ability. ;)


Address of TLS variables

2012-06-23 Thread Alex Rønne Petersen

Hi,

Does taking the address of a TLS variable and passing it to other 
threads have defined semantics? I would assume it results in a pointer 
to the thread's instance of the TLS variable (which makes sense), but is 
passing it to other threads OK? Does the language guarantee this?


--
Alex Rønne Petersen
a...@lycus.org
http://lycus.org



Re: phobos and splitting things... but not with whitespace.

2012-06-23 Thread Chad J

On 06/23/2012 03:41 PM, simendsjo wrote:

On Sat, 23 Jun 2012 20:41:29 +0200, Chad J
chadjoan@__spam.is.bad__gmail.com wrote:


IMO the take away a single line thing should be accomplishable with
a single concise expression


This takes a range to match against, so much like startsWith:

auto findSplitAny(Range, Ranges...)(Range data, Ranges matches) {
auto rest = data;
for(; !rest.empty; rest.popFront()) {
foreach(match; matches) {
if(rest.startsWith(match)) {
auto restStart = data.length-rest.length;
auto pre = data[0..restStart];
// we'll fetch it from the data instead of using the supplied
// match to be consistent with findSplit
auto dataMatch = data[restStart..restStart+match.length];
auto post = rest[match.length..$];
return tuple(pre, dataMatch, post);
}
}
}
return tuple(data, Range.init, Range.init);
}
unittest {
auto text = 1\n2\r\n3\r4;

auto res = text.findSplitAny(\r\n, \n, \r);
assert(res[0] == 1);
assert(res[1] == \n);
assert(res[2] == 2\r\n3\r4);

res = res[2].findSplitAny(\r\n, \n, \r);
assert(res[0] == 2);
assert(res[1] == \r\n);
assert(res[2] == 3\r4);

res = res[2].findSplitAny(\r\n, \n, \r);
assert(res[0] == 3);
assert(res[1] == \r);
assert(res[2] == 4);

res = res[2].findSplitAny(\r\n, \n, \r);
assert(res[0] == 4);
assert(res[1] == );
assert(res[2] == );
}


I, for one, would like to see that in phobos...
Although it should probably be called findSplitAmong to be consistent 
with findAmong ;)


Re: Address of TLS variables

2012-06-23 Thread Timon Gehr

On 06/23/2012 09:51 PM, Alex Rønne Petersen wrote:

Hi,

Does taking the address of a TLS variable and passing it to other
threads have defined semantics? I would assume it results in a pointer
to the thread's instance of the TLS variable (which makes sense),



I'd assume that it results in a pointer to the original thread's TLS 
variable, because threads usually share the physical address space.



but is passing it to other threads OK?


I think you could pass a T* function() thunk that takes the respective 
TLS variable's address instead, if that makes sense.



Does the language guarantee this?



No, it is probably implementation/architecture dependent.


Re: Stack overflow

2012-06-23 Thread Namespace

So here i proudly present my Ref struct (2.0 ;))
http://dpaste.dzfl.pl/905e1d3d

So, any suggestions, further ideas or criticism?


Re: Stack overflow

2012-06-23 Thread Namespace

BTW: Any tries to declare
@disable
this(typeof(null)); end with the same message Stack overflow...


Re: Stack overflow

2012-06-23 Thread Jonathan M Davis
On Saturday, June 23, 2012 07:27:56 Namespace wrote:
 I would prefer NullPointer Exceptions and / or not null types
 rather than playing around with the debugger.
 That's the first step.

NullPointerExceptions (well, they'd end up being NullPointersErrors) will 
_never_ happen. Walter is completely against them. From his point of view, the 
OS is _already_ checking this for you (hence the segfault or access 
violation), so there's no point in having the language check.

We might end up with something in druntime which prints out a stacktrace when 
a segfault occurs, but that's the closest that you'll ever get to a null 
pointer exception in D.

A non-nullable type _will_ be added to Phobos at some point. The issue is a 
matter of implementation. At least one has been submitted, but it wasn't good 
enough to be included as it stood. So, it hasn't been merged in. As soon as 
there is one which is good enough and has been appropriately reviewed by the 
Phobos devs, we'll have one. But until then, we don't.

- Jonathan M Davis


Re: Passing a ubyte[] to a function taking a 'ref ubyte[16]'

2012-06-23 Thread Jonathan M Davis
On Saturday, June 23, 2012 20:23:26 Johannes Pfau wrote:
 I'm working on the new design for std.hash and I hit an interesting
 problem:
 
 The OOP interface has to take buffers as slices with unknown length, as
 the length differs between hashes and I have to put a common function
 declaration in a interface. So I have this:
 
 interface Digest
 {
 ubyte[] finish(ubyte[] buf);
 }
 

I confess that I'm baffled as to why you'd even be using interfaces for this, 
given that Phobos always uses structs and templates for this sort of thing.

 Now the template API can and should use the correct type, so there
 finish is defined like this:
 
 struct MD5
 {
 void finish(ref ubyte[16] data);
 }
 
 
 And the interface implementation for MD5 has to call the MD5 structs
 finish function:
 
 class MD5Digest : Digest
 {
 private MD5 _digest;
 
 ubyte[] finish(ubyte[] buf)
 {
 enforce(buf.length = 16);
 _digest.finish(buf); //How to do this?
 }
 }
 
 
 So how to pass a ubyte[] to a function expecting a ref ubyte[16]
 without allocating/using any extra memory (Not even stack)?
 
 This seems to work, but it's very ugly:
 
 _digest.finish(*cast(ubyte[16]*)buf.ptr);
 
 
 I thought this might create a temporary, but it passes all unittests,
 so it seems to work?

I believe that that will work, but it's definitely ugly. However, if you do 
that, you _need_ to put an assertion about the length of buf in there, 
otherwise, you could be using memory from past the end of buf.

- Jonathan M Davis


aa.remove in a destructor

2012-06-23 Thread Ellery Newcomer

this code:

class X{
string[string] s;
this() {
s[s] = S;
}
~this() {
s.remove(s);

}
}

void main() {
X x = new X();
}

produces this:

core.exception.InvalidMemoryOperationError

because the aa is calling gc_free during a collection, apparently.

Should I be expecting the above code to run without error or not?

thanks


Re: aa.remove in a destructor

2012-06-23 Thread BLM768
I've been having the same problem as well, but I never figured 
out the link to the remove() call in the destructor. The only 
solution I've found is to use GC.removeRoot() on the table, but 
it's an untested and potentially dangerous solution.