Re: Get indexes of character in array

2012-03-28 Thread Dmitry Olshansky

On 28.03.2012 10:42, Andrej Mitrovic wrote:

3/28/12, Andrej Mitrovic  wrote:
I'd like to get a list of indexes into an array that matches a character.
E.g.:

"a foo a bar a".indexes("a") == [0, 6, 12]


Hah I even managed to screw up that "bar" has an 'a' there. Anywho
this works just fine:

size_t[] indexes(string input, dchar target)
{
 size_t[] result;
 foreach (index, dchar ch; input)
 {
 if (ch == target)
 result ~= index;
 }
 return result;
}

The cool thing about the foreach loop is that 'index' will point to
the exact code point of the target (it won't just be incremented by +1
on each loop).


Piece of cake indeed.
Just use Appender + put for building arrays in future,  it's becoming 
military grade soon:

 https://github.com/D-Programming-Language/phobos/pull/502

--
Dmitry Olshansky


Re: Problem about lambda expressions

2012-03-28 Thread Tongzhou Li

On Tuesday, 27 March 2012 at 14:12:38 UTC, dennis luehring wrote:

Am 27.03.2012 15:52, schrieb Tongzhou Li:

Oh, I also tried:
 void seq_apply(Params..., Args...)(void delegate(Params)
func, Args args)
But I got a error:
 variadic template parameter must be last
Does it mean that there can only be one variadic template
parameter? How to fix it?
Thanks


just a question:

how on earth should the compiler seperate your 2 variadic 
parameters???


t( a,b,c,x,y,z ) -> where Params Start/End, where Args 
magic?


Well...
It's a question...


Re: Problem about lambda expressions

2012-03-28 Thread Tongzhou Li

On Tuesday, 27 March 2012 at 14:54:11 UTC, Artur Skawina wrote:

On 03/27/12 15:52, Tongzhou Li wrote:

Oh, I also tried:
void seq_apply(Params..., Args...)(void delegate(Params) 
func, Args args)

But I got a error:
variadic template parameter must be last
Does it mean that there can only be one variadic template 
parameter? How to fix it?


I'm not sure what exactly you're trying to do, but maybe this 
will help:


void seq_apply(Func, Args...)(Func func, Args args) {
import std.traits;
alias ParameterTypeTuple!Func Params;
enum ArgNum = Params.length-1;
func(args[0], args[1 .. ArgNum + 1]);
static if (args.length > ArgNum + 1) {
seq_apply(func, args[ArgNum + 1 .. 
args.length]);

}
}

artur


Yes, this is what I want to do. Thanks!


Re: Problem about lambda expressions

2012-03-28 Thread Tongzhou Li

On Tuesday, 27 March 2012 at 15:21:57 UTC, Kenji Hara wrote:

On Tuesday, 27 March 2012 at 13:42:30 UTC, Tongzhou Li wrote:

Hello again! I'm learning D, and I encountered a problem.
I tried this code:
http://ideone.com/hkpT6
It works well. (Have no idea why codepad.org failed to compile 
it)
I tried to write a lambda instead of function f, but I got 
nothing printed.

Did I make something wrong?
Compiler used: DMD32 D Compiler v2.058 (Win7 SP1 x64)
Sorry for my poor English :)


(obj x, int a0, int a1) => { x.setxxx(a0); x.setyyy(a1); }

This lambda expression returns *a delegate has no parameter*.
Instead:

(obj x, int a0, int a1) => (x.setxxx(a0), x.setyyy(a1))

or:

(obj x, int a0, int a1){ x.setxxx(a0); x.setyyy(a1); }


I understood. But why it compiles instead of giving an error? 
What does the complier do when I write the wrong code?


Re: Problem about lambda expressions

2012-03-28 Thread Tongzhou Li

On Wednesday, 28 March 2012 at 08:22:25 UTC, Tongzhou Li wrote:
I understood. But why it compiles instead of giving an error? 
What does the complier do when I write the wrong code?


Oh, I mean if I write the wrong code, what objectcode does the 
compiler generate?

My English is not good, sorry.


Regex question

2012-03-28 Thread James Blewitt

I'm having a problem with regexes.
The following code gives a compilation error.  If I comment out 
the regex outside of main and comment in the regex inside of main 
then it does compile.


I'm using DMD v2.058

Any ideas what is going on?

--

import std.regex;

Regex!(char) testRegex = regex("\\b(A(Z)?|B(Z)?|C(Z)?)\\b", "i");

void main() {
//	Regex!(char) testRegex = regex("\\b(A(Z)?|B(Z)?|C(Z)?)\\b", 
"i");

}


Re: Regex question

2012-03-28 Thread simendsjo

On Wed, 28 Mar 2012 11:40:21 +0200, James Blewitt  wrote:


I'm having a problem with regexes.
The following code gives a compilation error.  If I comment out the  
regex outside of main and comment in the regex inside of main then it  
does compile.


I'm using DMD v2.058

Any ideas what is going on?

--

import std.regex;

Regex!(char) testRegex = regex("\\b(A(Z)?|B(Z)?|C(Z)?)\\b", "i");

void main() {
//  Regex!(char) testRegex = regex("\\b(A(Z)?|B(Z)?|C(Z)?)\\b", "i");
}


Sounds like a bug. The following works:
import std.regex;

Regex!(char) testRegex = ctRegex!(r"\b[ABC]Z?\b", "i");

void main() {
// matches
assert(match(" A ", testRegex));
assert(match(" B ", testRegex));
assert(match(" C ", testRegex));
assert(match(" AZ ", testRegex));
assert(match(" BZ ", testRegex));
assert(match(" CZ ", testRegex));
// case insensitive
assert(match(" a ", testRegex));
assert(match(" az ", testRegex));
// needs match at word boundary
assert(!match("A", testRegex));
// doesn't match other characters
assert(!match(" D ", testRegex));
assert(!match(" DZ ", testRegex));
assert(!match(" DZ ", testRegex));
}


[Ubuntu] Linking to static libraries

2012-03-28 Thread Minas
There's a small C graphics library called GP142, which I am 
trying to port to D on Linux (Ubuntu).


So I compiled the library in gcc, and that gave me the file 
gp142.o which I must link together with my D source file.


My code is very simple. I declare the two simplest functions in 
GP142, and try to call them. If it works, I will do the same for 
all the other functions :)


However, GP142 must be linked with the Xlib library in order to 
work.
When compiling as a C program, I would say something like: [gcc 
test.c gp142.o -o test -lX11] and it would work.


How can do this with dmd? (Note that X11 is somewhere in the 
system, not in my project folder - gcc manages to find though).



This my code:

import std.stdio;

extern (C) int GP142_open();
extern (C) void GP142_close();

void main()
{
// initialize GP142
GP142_open();   

// close GP142
GP142_close();

writeln("Works");
}


Re: Problem about lambda expressions

2012-03-28 Thread Timon Gehr

On 03/28/2012 10:28 AM, Tongzhou Li wrote:

On Wednesday, 28 March 2012 at 08:22:25 UTC, Tongzhou Li wrote:

I understood. But why it compiles instead of giving an error? What
does the complier do when I write the wrong code?


Oh, I mean if I write the wrong code, what objectcode does the compiler
generate?
My English is not good, sorry.


(int a) => {return a;}

Is the same thing as

(int a){return ()=>a;}


Re: Regex question

2012-03-28 Thread Dmitry Olshansky

On 28.03.2012 13:40, James Blewitt wrote:

I'm having a problem with regexes.
The following code gives a compilation error. If I comment out the regex
outside of main and comment in the regex inside of main then it does
compile.


Please include compilation errors in future, it helps folks to figure 
the cause even without compiling your code.


In this case I get:

C:\dmd2\windows\bin\..\..\src\phobos\std\regex.d(1220): Error: 
assert(cast(int)

his.ir[orStart].code() == 129) failed
C:\dmd2\windows\bin\..\..\src\phobos\std\regex.d(946):called 
from here:

this.parseRegex()
C:\dmd2\windows\bin\..\..\src\phobos\std\regex.d(6522):called 
from here

 parser.this(pattern,flags)
newww.d(4):called from here: regex("\\b(A(Z)?|B(Z)?|C(Z)?)\\b","i")
Failed: "dmd" "-v" "-o-" "newww.d" "-I."

>
> I'm using DMD v2.058
>
> Any ideas what is going on?
>

It does look like a bug C-T parser, this one I'm aware of, but I have no 
cure for it yet.
Apparently global variables requires const initializers thus invoking 
CTFE to process regex(...).


Try something like this for a workaround:

Regex!(char) testRegex;

static this(){
testRegex = regex("\\b(A(Z)?|B(Z)?|C(Z)?)\\b", "i");
//init all global
}

P.S. Note that you have raw literals in D, it is advised to use them for 
regex: `\b(A(Z)?|B(Z)?|C(Z)?)\b` or r"\b(A(Z)?|B(Z)?|C(Z)?)\b"


--
Dmitry Olshansky


Re: [Ubuntu] Linking to static libraries

2012-03-28 Thread simendsjo
On Wed, 28 Mar 2012 12:05:59 +0200, Minas   
wrote:


There's a small C graphics library called GP142, which I am trying to  
port to D on Linux (Ubuntu).


So I compiled the library in gcc, and that gave me the file gp142.o  
which I must link together with my D source file.


My code is very simple. I declare the two simplest functions in GP142,  
and try to call them. If it works, I will do the same for all the other  
functions :)


However, GP142 must be linked with the Xlib library in order to work.
When compiling as a C program, I would say something like: [gcc test.c  
gp142.o -o test -lX11] and it would work.


How can do this with dmd? (Note that X11 is somewhere in the system, not  
in my project folder - gcc manages to find though).



This my code:

import std.stdio;

extern (C) int GP142_open();
extern (C) void GP142_close();

void main()
{
// initialize GP142
GP142_open();   

// close GP142
GP142_close();

writeln("Works");
}


To pass -lX11 to the linker, dmd has the -L flag.
"dmd -L-lX11" will pass "-lX11" to the linker.


Re: D Dll injection problem

2012-03-28 Thread maarten van damme
I wrote my own injector and this makes the target exe call loadlibrary.
this works on every dll I try to inject apart from dll's written in D
(starting with dmd version 2,054 or something like that).
I'll try with D calling loadlibrary on D dll's this evening.


Re: D Dll injection problem

2012-03-28 Thread Trass3r

this works on every dll I try to inject apart from dll's written in D
(starting with dmd version 2,054 or something like that).


If this is a regression, please narrow it down to the exact version.


Re: Problem about lambda expressions

2012-03-28 Thread Tongzhou Li

On Wednesday, 28 March 2012 at 10:17:15 UTC, Timon Gehr wrote:

On 03/28/2012 10:28 AM, Tongzhou Li wrote:

On Wednesday, 28 March 2012 at 08:22:25 UTC, Tongzhou Li wrote:
I understood. But why it compiles instead of giving an error? 
What

does the complier do when I write the wrong code?


Oh, I mean if I write the wrong code, what objectcode does the 
compiler

generate?
My English is not good, sorry.


(int a) => {return a;}

Is the same thing as

(int a){return ()=>a;}


I understand.
  { return a; }
is a function which requires no params right?


Re: std.stream.File help required (and classes)

2012-03-28 Thread akaz

> I am a bit lost between pointers
> (s->x or (*s).x) and values (s.x). For structures there are
pointers,
> for classes there are no pointers?

Yes.

Ali


And migrating from std.stream.File (which was a class) to 
std.stdio.File (which is a structure) lost me completely.


Why, in fact, std.stream.File is a structure and not a class?

Citing: http://dlang.org/phobos/std_stdio.html

struct File;
Encapsulates a FILE*. Generally D does not attempt to provide 
thin wrappers over equivalent functions in the C standard 
library, but manipulating FILE* values directly is unsafe and 
error-prone in many ways. The File type ensures safe 
manipulation, automatic file closing, and a lot of convenience.


 The underlying FILE* handle is maintained in a reference-counted 
manner, such that as soon as the last File variable bound to a 
given FILE* goes out of scope, the underlying FILE* is 
automatically closed.


May I disable that reference counting?

I do not get how to use GC.addRange(). Can you, please, direct me 
towards an example?


You seem to be right, the f and f.data variables are allocated 
into my C code, so they are invisible to the garbage collector.


BUT!!! Is there any way to disable that garbage collector 
straight from the beginning? Maybe a compiler flag?


I should also add that I allocated my structure with:

(init)
s.filedesc = cast(File*)GC.calloc(1,File.sizeof);

(open)
(*(s.filedesc)).open(*(cast(string*)arg),"w+b");

but without success, since the MSF_State* 
s=ms_new!(MSF_State)(1); line allocating the memory for s is 
esentially a C library function (a wrapper around it):


extern(C){
//...
void* ortp_malloc(size_t sz);
T* ortp_new(T)(int count){
return cast(T*)ortp_malloc(T.sizeof*count);
}
alias ortp_new ms_new;
//...
}

so that allocation is not visible to the garbage collector.

My code has so many pointers because I took it from C, directly. 
Many of those pointers are also imposed by the underlying (C) 
library. I had no intention to extensively re-write my code, just 
to port it.




Re: std.stream.File help required (and classes)

2012-03-28 Thread akaz

I should also add that I allocated my structure with:

(init)
s.filedesc = cast(File*)GC.calloc(1,File.sizeof);

(open)
(*(s.filedesc)).open(*(cast(string*)arg),"w+b");



OMG! That solution works! Except that I was making a silly 
mistake and thought that GC.alloc() prototype is similar to C's 
calloc (since it has two parameters).


I fact, after writing:

(init)
s.filedesc = cast(File*)GC.calloc(File.sizeof*1);

(open)
(*(s.filedesc)).open(*(cast(string*)arg),"w+b");

now, my file remains open and I can write inside!

(The data inside is not really what it should be, but that, at 
least, is a debugging problem, not a segfault one).


Thank you once again!




Re: std.stream.File help required (and classes)

2012-03-28 Thread akaz
(The data inside is not really what it should be, but that, at 
least, is a debugging problem, not a segfault one).


Just to let you know that I did it. I was writing the good data 
(as double), but it was my verification test that assumed it to 
be int...


Now I corrected it and it works!

Any way to mark this thread as solved?





Re: Problem about lambda expressions

2012-03-28 Thread Timon Gehr

On 03/28/2012 01:13 PM, Tongzhou Li wrote:

On Wednesday, 28 March 2012 at 10:17:15 UTC, Timon Gehr wrote:

On 03/28/2012 10:28 AM, Tongzhou Li wrote:

On Wednesday, 28 March 2012 at 08:22:25 UTC, Tongzhou Li wrote:

I understood. But why it compiles instead of giving an error? What
does the complier do when I write the wrong code?


Oh, I mean if I write the wrong code, what objectcode does the compiler
generate?
My English is not good, sorry.


(int a) => {return a;}

Is the same thing as

(int a){return ()=>a;}


I understand.
{ return a; }
is a function which requires no params right?


Yes.


Re: std.stream.File help required (and classes)

2012-03-28 Thread Ali Çehreli

On 03/28/2012 05:09 AM, akaz wrote:

(The data inside is not really what it should be, but that, at least,
is a debugging problem, not a segfault one).


Just to let you know that I did it. I was writing the good data (as
double), but it was my verification test that assumed it to be int...

Now I corrected it and it works!

Any way to mark this thread as solved?


Glad to hear it! :) I don't think there is a way of marking an NNTP 
thread as solved.


Ali


Installing Modules

2012-03-28 Thread TJB

All,

I'm very new to D.  I am wanting to install the SciD module 
(David Simcha's fork), but I don't know how to go about it.  Can 
you guide me?


Where does the code go?  How do I import it?

Thanks,

TJB


Re: Installing Modules

2012-03-28 Thread Jesse Phillips

On Wednesday, 28 March 2012 at 23:55:38 UTC, TJB wrote:

All,

I'm very new to D.  I am wanting to install the SciD module 
(David Simcha's fork), but I don't know how to go about it.  
Can you guide me?


Where does the code go?  How do I import it?

Thanks,

TJB


There are many ways to go about using modules, I will go over 
installing a module for Linux. Usually the simplest way to get 
started is to pass all files to the compiler (you use an import 
statement to import the modules).


Build SciD as a library.

$ dmd -lib -oflibscid.a all.d files.d for.d SciD.d

Copy lib to /usr/local/lib

Copy sciD source files in the same direcectory structure 
(propably starting with scid/) to /usr/local/src


Installed.

Create a main.d file somewhere:

$ cat main.d
import scid.something;
void main() {}

$ dmd -I/usr/local/src main.d

Windows would be something different, however optlink doesn't 
have a standard set of locations to look for library files. you 
can also pass the library to dmd


$ dmd -I/usr/local/src main.d libscid.a -- If it is in the same 
directory as main.d


Re: Installing Modules

2012-03-28 Thread TJB

On Thursday, 29 March 2012 at 02:07:24 UTC, Jesse Phillips wrote:

On Wednesday, 28 March 2012 at 23:55:38 UTC, TJB wrote:

All,

I'm very new to D.  I am wanting to install the SciD module 
(David Simcha's fork), but I don't know how to go about it.  
Can you guide me?


Where does the code go?  How do I import it?

Thanks,

TJB


There are many ways to go about using modules, I will go over 
installing a module for Linux. Usually the simplest way to get 
started is to pass all files to the compiler (you use an import 
statement to import the modules).


Build SciD as a library.

$ dmd -lib -oflibscid.a all.d files.d for.d SciD.d

Copy lib to /usr/local/lib

Copy sciD source files in the same direcectory structure 
(propably starting with scid/) to /usr/local/src


Installed.

Create a main.d file somewhere:

$ cat main.d
import scid.something;
void main() {}

$ dmd -I/usr/local/src main.d

Windows would be something different, however optlink doesn't 
have a standard set of locations to look for library files. you 
can also pass the library to dmd


$ dmd -I/usr/local/src main.d libscid.a -- If it is in the same 
directory as main.d


Okay.  I tried this.  I think I am close.  I followed the 
instructions that you gave (thanks btw)!

But, I get this error message:

$ dmd -I/usr/local/src main.d
main.d(1): Error: module scid is in file 'scid.d' which cannot be 
read

import path[0] = /usr/local/src
import path[1] = /Users/name/dmd2/src/phobos
import path[2] = /Users/name/dmd2/src/druntime/import

Thoughts?

Thanks!

TJB


Re: std.conv length=0

2012-03-28 Thread cc

On Tuesday, 10 April 2007 at 07:46:35 UTC, Bill Baxter wrote:

Derek Parnell wrote:
Currently, the functions in std.conv throw an exception if the 
input string

is empty. What is the rationale for this?


I too thought this behavior was silly, so I wrote a little 
wrapper for it (replying 5 years after the fact because 
someone'll probably stumble onto this question through Google 
like I did):


import std.conv : toOrig = to;
T toSafe(T, S)(S arg) {
static if ((is(T == int) || (is(T == real))) && is(S == string))
if (!arg.length)
return 0;
return toOrig!T(arg);
}
alias toSafe to;


and then just continue to call 'to' normally.



Re: std.conv length=0

2012-03-28 Thread James Miller
On 29 March 2012 16:03, cc  wrote:
> On Tuesday, 10 April 2007 at 07:46:35 UTC, Bill Baxter wrote:
> I too thought this behavior was silly, so I wrote a little wrapper for it
> (replying 5 years after the fact because someone'll probably stumble onto
> this question through Google like I did):

I award thee the "Necromancer" badge, for reviving a long-dead thread.

--
James Miller


Re: Installing Modules

2012-03-28 Thread Jesse Phillips

On Thursday, 29 March 2012 at 03:02:27 UTC, TJB wrote:
Okay.  I tried this.  I think I am close.  I followed the 
instructions that you gave (thanks btw)!

But, I get this error message:

$ dmd -I/usr/local/src main.d
main.d(1): Error: module scid is in file 'scid.d' which cannot 
be read

import path[0] = /usr/local/src
import path[1] = /Users/name/dmd2/src/phobos
import path[2] = /Users/name/dmd2/src/druntime/import

Thoughts?

Thanks!

TJB


You don't import scid; as that is just a package, you need a 
module such as:


import scid.matrix;

but it will truely depend on which modules you need for the code 
you are writing.


Re: std.conv length=0

2012-03-28 Thread Jesse Phillips

On Thursday, 29 March 2012 at 03:40:55 UTC, James Miller wrote:

I award thee the "Necromancer" badge, for reviving a long-dead 
thread.


--
James Miller


I find the distaste of reviving a thread strange. It would be 
like removing the "reopened" feature of bug tracking software 
(who wants to transpose all that information).


Re: Installing Modules

2012-03-28 Thread TJB

On Thursday, 29 March 2012 at 04:01:49 UTC, Jesse Phillips wrote:

On Thursday, 29 March 2012 at 03:02:27 UTC, TJB wrote:
Okay.  I tried this.  I think I am close.  I followed the 
instructions that you gave (thanks btw)!

But, I get this error message:

$ dmd -I/usr/local/src main.d
main.d(1): Error: module scid is in file 'scid.d' which cannot 
be read

import path[0] = /usr/local/src
import path[1] = /Users/name/dmd2/src/phobos
import path[2] = /Users/name/dmd2/src/druntime/import

Thoughts?

Thanks!

TJB


You don't import scid; as that is just a package, you need a 
module such as:


import scid.matrix;

but it will truely depend on which modules you need for the 
code you are writing.


Thank you for your patience with me.  I now have the following 
simple test code:


import scid.matrix;

void main() {}

Then I run and get the following error:

$ dmd -I/usr/local/src main.d
Undefined symbols:
  "_D4scid6matrix12__ModuleInfoZ", referenced from:
  _D4main12__ModuleInfoZ in main.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
--- errorlevel 1

Thank you for your help!

TJB



Re: std.conv length=0

2012-03-28 Thread James Miller
On 29 March 2012 17:05, Jesse Phillips  wrote:
> On Thursday, 29 March 2012 at 03:40:55 UTC, James Miller wrote:
>
>> I award thee the "Necromancer" badge, for reviving a long-dead thread.
>>
>> --
>> James Miller
>
>
> I find the distaste of reviving a thread strange. It would be like removing
> the "reopened" feature of bug tracking software (who wants to transpose all
> that information).

I have no problem with it, if I did, I would have said so.

--
James Miller