Path.GetDirectoryName for D?

2018-10-18 Thread Dr.No via Digitalmars-d-learn
Are there a function like C#'s Path.GetDirectoryName() 
(https://docs.microsoft.com/en-us/dotnet/api/system.io.path.getdirectoryname?redirectedfrom=MSDN=netframework-4.7.2#System_IO_Path_GetDirectoryName_System_String_) in D standard library or some dub package?
just checking if there's one, so that I don't reinvente the 
wheel...


Re: Process in parallel and output result to stdout theread-safely

2018-09-11 Thread Dr.No via Digitalmars-d-learn

On Monday, 10 September 2018 at 20:30:52 UTC, Dr.No wrote:

On Saturday, 8 September 2018 at 14:26:45 UTC, ag0aep6g wrote:

[...]


Yes, it does only happens at line breaks. I hadn't realized 
that until you mentioned. It does gets in place of \r and \n, 
that's why there's all in one line when this happens. Thankfor 
for the notice.


[...]


I noticied this happens with everything I print in the loop, even 
using C's printf:




__gshared Mutex m;
m = new Mutex();
foreach(string fn; parallel(files))
{
try
{
auto res = f(fn);
synchronized(m)
{
import std.c.stdio :
printf;
printf("hello!\n");
}

[...]



Output:

https://imgur.com/a/Mq9X4c3


Re: Process in parallel and output result to stdout theread-safely

2018-09-10 Thread Dr.No via Digitalmars-d-learn

On Saturday, 8 September 2018 at 14:26:45 UTC, ag0aep6g wrote:

On 09/03/2018 08:13 PM, Dr.No wrote:

But it in the middle of output, I got output like this:

outjson = {"barCode":"20","ade":"20"}♪◙outjson = 
{"barCode":"X21","ade":"21"}


also there's that extra ♪◙ character. Thos sounds memory 
violation somewhere.
This only happens when using parallel. Any guess what's 
possibily happeing?


If that only ever happens at line breaks, then that doesn't 
necessarily look like memory corruption to me.


Yes, it does only happens at line breaks. I hadn't realized that 
until you mentioned. It does gets in place of \r and \n, that's 
why there's all in one line when this happens. Thankfor for the 
notice.


I looked around a bit and found code page 437 [1]. It has those 
characters at 0xD and 0xA which is where ASCII has \r and \n. 
So it might be that code page 437 is used when displaying that 
particular entry. Why that would happen, I have no idea.


So I guessed that something changed the console code page. I've 
tried to call call chcp 65001 at program's start up. Not worked. 
Tried inside the main loop. Also not worked.


To get better help, you should post a complete test case that 
people can just copy/paste. That includes imports, a `main` 
function, and the command you use to compile. Also add 
information about your environment (OS, compiler version).




I made a reduced version where you can do just dmd -run hello.d. 
The application does convert alot of PDF files to text then do 
some parsing (I've removed this not needed part). In my code, at 
iteration 28 I got those character (♪◙), which is the error. I 
hope you can reproduce this error on your machine as well. I got 
some PDF files and duplicated them just for testing purpose. If 
for some reason you didn't get the same error immediately, try 
duplicate the PDFs and try again. I believe this can reproduce 
the erro.



Here's the resources to compile the application:

source code: https://pastebin.com/RwrUikQS
PDF files + pdf to text application: 
https://drive.google.com/file/d/1uKjJX4pQIEWVK4vujUsm0ln2yHS7z5ZZ/view?usp=sharing

OS: Windows 10 64-bit
compiler: DMD32 D Compiler v2.080.0 (same issue happens on ldc)
command line: dmd -run hello.d

Thank you very much for your time.



Re: Process in parallel and output result to stdout theread-safely

2018-09-08 Thread Dr.No via Digitalmars-d-learn
Does anyone have some tips to try trace the error with debug or 
so?

I haven't fixed this issue yet... any help is very appreciated


Re: std.process: spawnProcess

2018-09-07 Thread Dr.No via Digitalmars-d-learn

On Friday, 7 September 2018 at 14:36:42 UTC, Russel Winder wrote:
From what I can see, processes created with std.process: 
spawnProcess are not terminated when the creating process 
terminates, i.e. it seems Config.detached is the default for 
these process.


Is there a way of all spawned processes being terminated on 
main termination?


You also can use WINAPI's job object. It will close the registred 
process, even if the application exit abruptly. I have, by now, 
only a link to a C# example how do that but I believe you can 
convert to D easily.


https://stackoverflow.com/questions/6266820/working-example-of-createjobobject-setinformationjobobject-pinvoke-in-net


Process in parallel and output result to stdout theread-safely

2018-09-03 Thread Dr.No via Digitalmars-d-learn
so I'im doing an expansive operation with a file, to try speed 
up, i switch to using parallel but keeping in the otuput printing 
thread-safe. But for some reason, even using synchonized, it 
doesn't work as expected, for example, it output multiples 
results on same time, as in the example below.


the code:


stdout.flush();
foreach(string fn; parallel(files))
{
auto res = doSomething(fn);
synchronized
{
   stdout.writefln("outjson = %s", res.serializeToJson);
   stdout.flush();
}

}



the expeced output is like that (one per line):


outjson = {"barCode":"1","ade":"1"}
outjson = {"barCode":"2","ade":"2"}
outjson = {"barCode":"3","ade":"3"}

// and so on...

But it in the middle of output, I got output like this:

outjson = {"barCode":"20","ade":"20"}♪◙outjson = 
{"barCode":"X21","ade":"21"}


also there's that extra ♪◙ character. Thos sounds memory 
violation somewhere.
This only happens when using parallel. Any guess what's possibily 
happeing?


Re: Is this a good idea?

2018-09-01 Thread Dr.No via Digitalmars-d-learn
On Saturday, 1 September 2018 at 17:08:25 UTC, Peter Alexander 
wrote:

On Saturday, 1 September 2018 at 16:20:11 UTC, Dr.No wrote:

why move flush to outside the synchronized block?


flush should be thread safe. In general, yiu want as little 
code as possible to run under the lock. Not that important 
though.


trying out this approach I found to be ok except in some 
cases, the output look like that:


...

also there's that extra ♪◙ character. Thos sounds memory 
violation somewhere.
This only happens when using parallel. Any guess what's 
possibily happeing?


Hard to say without seeing code. Agree it looks like a race.


I'll try to make a reduced version of the program so that your 
guys can help me find out what's wrong.


One guess:

In the code:


foreach(x; parallel(arr)) {
  auto a = f(x);
  auto res = g(a);
  synchronized {
   stdout.writeln(res);
   stdout.flush();

}


assuming res is a class type, is res's adress unique due whole 
loop execution or it can be overritten by another thread? for 
example:


the writeln() call block is locked until thread3 finish printing,
thread2 has just finished and now is waiting for thread3 free the 
resource but before that happens, thread2 just finish. Can 
thread2 overrite the res adress in any way?
I need clarifation on that to try find out what's wrong with this 
code.


for more info: g() does have the last statement as return new 
myClass()


Re: Is this a good idea?

2018-09-01 Thread Dr.No via Digitalmars-d-learn
On Thursday, 30 August 2018 at 21:09:35 UTC, Peter Alexander 
wrote:

On Thursday, 30 August 2018 at 19:59:17 UTC, Dr.No wrote:
I would to process the current block in parallel but priting 
need to be theread-safe so I'm using



foreach(x; parallel(arr)) {
   auto a = f(x);
   auto res = g(a);
   synchronized {
stdout.writeln(res);
stdout.flush();
}
}



Since f() and g() are some heavy functions, I'd like to 
process in parallel but the printing (doesn't need to respect 
order but must be thread-safe) hence I'm using synchronized. 
Is this counter-productive in any way?


I don't see any problem with that assuming f and g are 
significantly more expensive than writeln. The flush can be 
moved outside the synchronized block.


why move flush to outside the synchronized block?
trying out this approach I found to be ok except in some cases, 
the output look like that:



outjson = {"barCode":"1","ade":"1"}
outjson = {"barCode":"2","ade":"2"}
outjson = {"barCode":"3","ade":"3"}
outjson = {"barCode":"4","ade":"4"}
outjson = {"barCode":"5","ade":"5"}

// and so on...

then there is this output:

outjson = {"barCode":"20","ade":"20"}♪◙outjson = 
{"barCode":"X21","ade":"21"}


within the synchronized block.
This is supposed to be:


outjson = {"barCode":"20","ade":"20"}
outjson = {"barCode":"X21","ade":"21"}


also there's that extra ♪◙ character. Thos sounds memory 
violation somewhere.
This only happens when using parallel. Any guess what's possibily 
happeing?




Is this a good idea?

2018-08-30 Thread Dr.No via Digitalmars-d-learn
I would to process the current block in parallel but priting need 
to be theread-safe so I'm using



foreach(x; parallel(arr)) {
   auto a = f(x);
   auto res = g(a);
   synchronized {
stdout.writeln(res);
stdout.flush();
}
}



Since f() and g() are some heavy functions, I'd like to process 
in parallel but the printing (doesn't need to respect order but 
must be thread-safe) hence I'm using synchronized. Is this 
counter-productive in any way?


QWebView requesting QtE5WebEng32.so on windows

2018-07-21 Thread Dr.No via Digitalmars-d-learn

So I went to try out QWebView on Windows from this wrapper:

https://github.com/MGWL/QtE5

all the examples went fine until I tried QWebView:

https://github.com/MGWL/QtE5/blob/master/examples/webview.d

I compile using this command line:


dmd -m32 webview.d qte5.d -oflol


but when I run I get this error:


Error load: QtE5WebEng32.so


I thought this was just a typo and what he really meant was 
QtE5WebEng32.dll but even so, I can't find such dll even using

windeployqt --webkit2 --release


Is the library's author around so that we can fix this?



Re: Convert path to file system path on windows

2018-06-30 Thread Dr.No via Digitalmars-d-learn

Thank you very much u all guys.


Can I parse this kind of HTML with arsd.dom module?

2018-06-23 Thread Dr.No via Digitalmars-d-learn
This is the module I'm speaking about: 
https://arsd-official.dpldocs.info/arsd.dom.html


So I have this HTML that not even parseGarbae() can del with:

https://hostname.com/?file=foo.png=baa;>G!

There is this spaces between  "href" and "=" and "https..." which 
makes below code fails:



string html = get(page, client).text;
auto document = new Document();
document.parseGarbage(html);
Element attEle = document.querySelector("span[id=link2]");
Element aEle = attEle.querySelector("a");
string link = aEle.href; // <-- if the href contains space, it 
return "href" rather the link




let's say the page HTML look like this:




Hello, dear world!

https://hostname.com/?file=foo.png=baa;>G!




I know the library author post on this forum often, I hope he see 
this help somehow


to make it work. But if anyone else know how to fix this, will be 
very welcome too!


Re: How do I filter out this type?

2018-06-23 Thread Dr.No via Digitalmars-d-learn
On Friday, 22 June 2018 at 17:20:03 UTC, Steven Schveighoffer 
wrote:

On 6/22/18 1:07 PM, Dr.No wrote:
    static if(is(typeof(__traits(getMember, B, field) == 
A[])))
  static if(is(typeof(__traits(getMember, B, field)) == 
A[]))


Note the slight change in parentheses.

-Steve



oh, I'm a bit embarrassed. haha Thanks anyway!


How do I filter out this type?

2018-06-22 Thread Dr.No via Digitalmars-d-learn
In the below code, "A[] found" is never printed. What's the 
proper way to check for this type?


import std.stdio;
import std.traits : FieldNameTuple;

class A { }
class B
{
string foo;
string baa;
A[] list;
}

void main()
{
static foreach(field; FieldNameTuple!B)
{
   static if(is(typeof(__traits(getMember, B, field) == A[])))
   {
   writeln("A[] found");
   }
}
writeln("done");
}





Convert path to file system path on windows

2018-06-21 Thread Dr.No via Digitalmars-d-learn

How can I do that with D?

In C# you can do that:

var filename = @"C:\path\to\my\file.txt";
var file = new Uri(filename).AbsoluteUri;
// file is "file:///C:/path/to/my/file.txt"

How can I do that in D?



How can I enforce an parameter to be constant know at compile time?

2018-06-12 Thread Dr.No via Digitalmars-d-learn

I'd like help of compiler to check this:

consider this:

int f(int n) {
  m_n = n;
}

f(10); // ok
f(myTampleteFunction!(compileTimeParameter)); // ok
enum n = 10;
f(n); // I won't use this, but should also be ok
int x = 10;
f(x); // error
int g() { return 20; }
f(g); // error


How can I enforce that?



run "thread" with memory space similar a new process

2018-06-01 Thread Dr.No via Digitalmars-d-learn
Thread is "limited" to local storage, so static variables 
(including the ones marked as __gshared in D) which are globals 
are shared between the threads. So, calling not pure functions 
which depend upon global variables prevent parallization for that 
global-dependence.

(please tell me I got anything wrong on this).

My question is:

Is there some mechanism to make a thread or whatnot run entirely 
in a new memory space context, including create their own globals 
rather share exising ones, exactly as a new process does?

I hope my question is clear. Thanks in advance.



Re: How do I make this function thread safe?

2018-06-01 Thread Dr.No via Digitalmars-d-learn

On Friday, 1 June 2018 at 02:30:34 UTC, Paul Backus wrote:

On Thursday, 31 May 2018 at 19:26:12 UTC, Dr.No wrote:
My application create some HTML which is then converted to PDF 
by wkhtmltopdf library. I'm trying to figure out how make the 
PDF generation run parallel, currently, it's running linearly.


It looks like wkhtmltopdf does not support multithreaded use; 
see here:


https://github.com/wkhtmltopdf/wkhtmltopdf/issues/1711

So, if you want to run the conversions in parallel, you will 
have to use separate processes.


Can I run that function in another memory space within my 
application, making even __gshared be allocated in that memory 
space, so that it seems it is another process? it's quite similar 
to thread but everything is allocated in that new memory space 
including __gshared.
I started calling the wkhtmltopdf executable by I wanted to see 
if there's a signficative gain in performance calling the library 
directly. My application do generate a lot of PDFs so I'd like to 
optimiza as possible.


How do I make this function thread safe?

2018-05-31 Thread Dr.No via Digitalmars-d-learn
My application create some HTML which is then converted to PDF by 
wkhtmltopdf library. I'm trying to figure out how make the PDF 
generation run parallel, currently, it's running linearly. My 
guess is wkhtmltopdf internal variables is preventing 
parallelization. But I'm new to parallization and I don't know 
how to solve that by now. I guesses that that function from an 
external thread would make each wkhtmltopdf initilization run on 
its memory space, similiar to a process. But since it is not 
working, I guess this isn't how it's working.
I'm not asking to just give me the code ready (if it's somehow 
complex) brather some directions, how I may archive that.



Here's my current code:

void genPDFImplt(string htmlFilename, string outputpdfFilename)
{
import pdf;
import std.string : toStringz;

/* Init wkhtmltopdf in graphics less mode */
wkhtmltopdf_init(0);
	wkhtmltopdf_global_settings *gs = 
wkhtmltopdf_create_global_settings();
	/* We want the result to be storred in the file called test.pdf 
*/
	wkhtmltopdf_set_global_setting(gs, "out", 
outputpdfFilename.toStringz);
	wkhtmltopdf_object_settings *os = 
wkhtmltopdf_create_object_settings();
	/* We want to convert to convert the qstring documentation page 
*/
	wkhtmltopdf_set_object_setting(os, "page", 
htmlFilename.toStringz);
	/* Create the actual converter object used to convert the pages 
*/

wkhtmltopdf_converter * c = wkhtmltopdf_create_converter(gs);
static if(0) {
/* Call the progress_changed function when progress changes */
		wkhtmltopdf_set_progress_changed_callback(c, 
_progress_changed);

/* Call the phase _changed function when the phase changes */
wkhtmltopdf_set_phase_changed_callback(c, _phase_changed);
/* Call the error function when an error occures */
wkhtmltopdf_set_error_callback(c, _error);
/* Call the waring function when a warning is issued */
wkhtmltopdf_set_warning_callback(c, _warning);
}
scope(exit) {
/* Destroy the converter object since we are done with it */
wkhtmltopdf_destroy_converter(c);
/* We will no longer be needing wkhtmltopdf funcionality */
wkhtmltopdf_deinit();
}
wkhtmltopdf_add_object(c, os, null);
/* Perform the actual convertion */
wkhtmltopdf_convert(c);
}


called from a loop like this:

foreach(string file; parallel(files)) {
   auto res = doSomething(file);
   auto htmlfile = genHtmlFile(res);
   auto pdffile = genTmpPDFFilename();
   genPDFImplt(htmlfiel, pdffile);
}



Running in a dual core CPU, with 4 threads. It's genrating
one PDF per iteration rather 4. I've made sure already it's the 
genPDFImplt() which is doing that.




Re: question about keeeping reference to toStringz()

2018-05-30 Thread Dr.No via Digitalmars-d-learn

On Wednesday, 30 May 2018 at 20:43:48 UTC, Ali Çehreli wrote:

On 05/30/2018 01:09 PM, Dr.No wrote:

> consider a C function with this prototype:
>> void foo(const char *baa);
>
> Does it means I should do:
>
>> string s = ...;
>> auto cstring = s.toStringz;
>> foo(cstring);
>
> rather just:
>
>> foo(s.toStringz);
>
> ?

It depends. cstring method above is not sufficient if cstring's 
life is shorter than the C library's use:


void bar() {
string s = ...;
auto cstring = s.toStringz;
foo(cstring);

} // <- cstring is gone

What if the library saved that pointer while performing foo()?

If cstring is in module-scope or in a container (e.g. an array) 
that's in module-scope then it's fine. But then, you would have 
to remove it from that container when the C library does not 
need that pointer anymore.


Ali


is foo() is being called from a thread, how I am supposed to keep 
cstring "alive"?


question about keeeping reference to toStringz()

2018-05-30 Thread Dr.No via Digitalmars-d-learn

The documentation says:

Important Note: When passing a char* to a C function, and the C 
function keeps it around for any reason, make sure that you keep 
a reference to it in your D code. Otherwise, it may become 
invalid during a garbage collection cycle and cause a nasty bug 
when the C code tries to use it.


(from https://dlang.org/library/std/string/to_stringz.html)

consider a C function with this prototype:

void foo(const char *baa);


Does it means I should do:


string s = ...;
auto cstring = s.toStringz;
foo(cstring);


rather just:


foo(s.toStringz);


?


How do I break from loop when using parallel()?

2018-05-28 Thread Dr.No via Digitalmars-d-learn

import std.parallelism : parallel;
foreach(t; parallel(arr))
{
if(!doSomething(t)) {
return false;
}
}

It reuturns the run time error:

std.parallelism.ParallelForeachError@(0): Cannot break from a 
parallel foreach loop using break, return, labeled 
break/continue or goto statements.


What's the proper way to break from loop?



Re: using wkhtmltopdf with D

2018-05-28 Thread Dr.No via Digitalmars-d-learn

On Monday, 28 May 2018 at 02:10:48 UTC, sarn wrote:

On Monday, 28 May 2018 at 01:28:10 UTC, Dr.No wrote:
What's likely the reason of the crash? mismatch between D and 
C memory alignment?


From an ABI point of view, the raw pointers won't care about 
the memory structure they point to.  The function call is the 
only thing that depends on the binary interface.


If you translate the code into C, does it work?


Yep, the C version work just fine.


using wkhtmltopdf with D

2018-05-27 Thread Dr.No via Digitalmars-d-learn
I'm trying to use wkhtmltopdf[1] with D. I converted this 
header[2] with little modification using htod tool which resulted 
in this[3].

The libray is passed to link using:

pragma(lib, "wkhtmltox.lib");
(that file is in wkhtmltopdf\lib folder)


and the module imported with:
import pdf;

but it crashes right upon the start with a SEGFAULT:

void main()
{

wkhtmltopdf_global_settings * gs;
wkhtmltopdf_object_settings * os;
wkhtmltopdf_converter * c;

/* Init wkhtmltopdf in graphics less mode */
wkhtmltopdf_init(0);
}

toolset I'm using:

DUB version 1.8.1, built on Apr 29 2018
LDC - the LLVM D compiler (1.9.0):
  based on DMD v2.079.1 and LLVM 5.0.1
  built with LDC - the LLVM D compiler (1.9.0)
  Default target: i686-pc-windows-msvc
  Host CPU: skylake
  http://dlang.org - http://wiki.dlang.org/LDC

What's likely the reason of the crash? mismatch between D and C 
memory alignment?


[1]: https://wkhtmltopdf.org/index.html
[2]: 
https://github.com/clowder/wkhtmltopdf/blob/master/src/lib/pdf.h

[3]: https://pastebin.com/SrtDUhPf


convert string to wchar[]

2018-05-26 Thread Dr.No via Digitalmars-d-learn
What's D's way to do that? I need it to be mutable array of wchar 
because a Windows function requires that.


Alternative to go down to using pointers, which would be 
something like:


wchar[] w = new wchar[s.length];
memcpy(w.ptr, s.ptr, s.length);




Re: How to convert ubyte[] to uint?

2018-05-23 Thread Dr.No via Digitalmars-d-learn

On Wednesday, 23 May 2018 at 19:49:27 UTC, Jonathan M Davis wrote:
On Wednesday, May 23, 2018 19:36:07 Dr.No via 
Digitalmars-d-learn wrote:

  [...]


As the template constraint in the error message says, read 
requires an input range. Static arrays are not input ranges. 
You need to give it a dynamic array - and since read takes its 
argument by reference, you can't simply slice the static array 
and pass it. You need a variable that's a dynamic array.


- Jonathan M Davis


sorry, the error message wasn't clear to me. When I use dynamic 
arrays I get:


slice of static array temporary returned by fnv64.finish() 
assigned to longer lived variable arr


What should use instead of?


How to convert ubyte[] to uint?

2018-05-23 Thread Dr.No via Digitalmars-d-learn

read fails with both uint and ulong on 64bit platform:

 Error: template std.bitmanip.read cannot deduce function from 
argument types !(ulong)(ubyte[8]), candidates are:
C:\ldc2-1.9.0-windows-x64\bin\..\import\std\bitmanip.d(3213,3):   
 std.bitmanip.read(T, Endian endianness = Endian.bigEndian, 
R)(ref R range) if (canSwapEndianness!T && isInputRange!R && 
is(ElementType!R : const(ubyte)))


code:

import digestx.fnv;
import std.bitmanip : read;
FNV64 fnv64;
fnv64.start();
fnv64.put(cast(ubyte[])word);
ubyte[8] arr = fnv64.finish();
auto h = arr.read!ulong;
return cast(uint)h;


Can I work out error: reinterpreting cast from const(ubyte)* to const(uint)* is not supported in CTFE?

2018-05-21 Thread Dr.No via Digitalmars-d-learn
I'm trying to do some hashing at compile time with xxhash 
algorithm but I get this error:


..\..\..\AppData\Local\dub\packages\xxhash-master\xxhash\src\xxhash.d(39,37): 
Error: reinterpreting cast from const(ubyte)* to const(uint)* is not supported 
in CTFE

this is line 39 
(https://github.com/repeatedly/xxhash-d/blob/master/src/xxhash.d#L39):



auto srcPtr = cast(const(uint)*)source.ptr;


I'm on Windows 10 64-bit machine.


How do I see the flags passed from dub to the compiler?

2018-05-21 Thread Dr.No via Digitalmars-d-learn

where's this stored?


Re: Temporary file creation for unittests

2018-05-21 Thread Dr.No via Digitalmars-d-learn

On Monday, 21 May 2018 at 15:16:11 UTC, Atila Neves wrote:

On Friday, 18 May 2018 at 15:16:52 UTC, Russel Winder wrote:

Hi,

What's the current official position on how to create 
temporary files for use during a unittest. I found


Not official, but...

import unit_threaded;

with(const Sandbox()) {
writeFile("myfile.txt", "contents");
shouldExist("myfile.txt");
shouldEqualContent("myfile.txt", "contents");
fileShouldContain("myfile.txt", "cont");
}

Atila


I've never seen "should" being in used in function names before...


Re: Can I infer the type from this?

2018-05-19 Thread Dr.No via Digitalmars-d-learn

On Sunday, 20 May 2018 at 02:01:20 UTC, Alex wrote:

On Sunday, 20 May 2018 at 01:41:03 UTC, Dr.No wrote:
I'd like to pass a symbol as paramater (class static member0 
and at same time get the type of this, something like this:


template myTemp(alias s)
{
enum myTemp = templateFunction!(??)(s.stringof);
}

the templateFunction has this signature:

int templateFunction(T)(string targetMembername)

but I don't how to get the type name from the give symbol name 
in myTemp. Can I make this work?


Something like this?

´´´
import std.stdio;

void main()
{
size_t s;
auto res = myTemp!s;
}


template myTemp(alias s)
{
enum myTemp = templateFunction!(typeof(s))(s.stringof);
}


int templateFunction(T)(string targetMembername)
{
static assert(is(T == size_t));
assert(targetMembername == "s");
return 42;
}
´´´


Oh, my bad: I totally forgot a crucial thing on question: I want 
this to work with a static member, for example, call myTemp like 
this myTemp!(C.a) I don't mind if I to pass the type as parameter 
somehow, like myTemp!(C, C.a) or myTemp!(C)(C.a) but I do need to 
pass a symbol as parameter, hence I'm using alias template 
parameter.




Can I infer the type from this?

2018-05-19 Thread Dr.No via Digitalmars-d-learn
I'd like to pass a symbol as paramater (class static member0 and 
at same time get the type of this, something like this:


template myTemp(alias s)
{
enum myTemp = templateFunction!(??)(s.stringof);
}

the templateFunction has this signature:

int templateFunction(T)(string targetMembername)

but I don't how to get the type name from the give symbol name in 
myTemp. Can I make this work?


Re: How is == operator implemented for string type?

2018-05-19 Thread Dr.No via Digitalmars-d-learn
On Wednesday, 16 May 2018 at 18:56:26 UTC, Steven Schveighoffer 
wrote:

On 5/16/18 2:45 PM, Dr.No wrote:

where is the actual source code implementation?


https://github.com/dlang/druntime/blob/7e3b4086fee8f2e2a6882942c677acc28df527ee/src/object.d#L3479

-Steve


thanks


Re: Temporary file creation for unittests

2018-05-18 Thread Dr.No via Digitalmars-d-learn

On Friday, 18 May 2018 at 15:30:05 UTC, Uknown wrote:

On Friday, 18 May 2018 at 15:16:52 UTC, Russel Winder wrote:

Hi,

What's the current official position on how to create 
temporary files for use during a unittest. I found


https://github.com/dlang/phobos/pull/5788

but it seems to be languishing in the "we have discussed all 
the issues that no-one will ever have a problem with" phase.


What to do between now and when there is an LDC release that 
has the result of

the merge?


You could use libc's tmpfile with std.stdio.File until a D 
alternative pops up.


http://en.cppreference.com/w/c/io/tmpfile


I've had no idea C++'s got this in the standard library lol a 
while ago I ended up doing this (on Windows):


/// Creates a uniquely named, zero-byte temporary file on disk 
and returns the full path of that file.

/// Returns: the full path of the temp file.
string tempFilename()
{
import  core.sys.windows.windows : GetTempFileNameW, MAX_PATH;
import std.file : tempDir;
import core.stdc.wchar_ : wcslen;
import std.windows.syserror : wenforce;
import std.conv : text, wtext;
wchar[] path = new wchar[MAX_PATH+1];
string dir = tempDir;
wenforce(GetTempFileNameW(dir.wtext.ptr, // temp path
  ("tmp"w).ptr, // dir 
prefix
  0, // id generated 
internally
  path.ptr // path 
buffer
),
"GetTempFileName()");
return path[0 .. wcslen(path.ptr)].text;
}

It's windows-only and call GetTempFileNameW actually so just a 
function wrapper to work with D. There's a way to MAX_PATH but I 
didn't care to implement at time...




How is == operator implemented for string type?

2018-05-16 Thread Dr.No via Digitalmars-d-learn

where is the actual source code implementation?


there's no gdc for windows?

2018-05-15 Thread Dr.No via Digitalmars-d-learn
Has gdc been supported for Windows? if so, where can I find it? 
I've only find Linux versions so far...


Re: How to curl!

2018-05-01 Thread Dr.No via Digitalmars-d-learn
On Tuesday, 1 May 2018 at 22:51:01 UTC, IntegratedDimensions 
wrote:

On Tuesday, 1 May 2018 at 22:08:50 UTC, Dr.No wrote:
On Tuesday, 1 May 2018 at 21:57:22 UTC, IntegratedDimensions 
wrote:

Trying to curl basic stuff but std.net.curl isn't cooperating:

curl "https://www.googleapis.com/youtube/v3/channels; -G -d 
part=contentDetails -d forUsername=test -d key=somekey


[...]


Just a wild guess, do you have the SSL dlls in the same folder 
as your executable or (less likely) in your PATH?



What SSL dlls?

those dlls:
libeay32.dll libssl32.dll ssleay32.dll

I'm sure they would be installed with whatever

installation uses them.


I don't think so. IIRC, when I used D+curl I needed to download 
them manutally. This is always the case with Qt. Even using 
windeployqt with   --webkit2 flag I still need to copy those dlls 
to application binary folder.


ssleay32.dll comes with dmd2 and it is in the path, if that is 
what you are talking about. If a dll was missing though and 
that was the problem then it needs to state that it is a 
missing dll rather than some other issue.



It isn't always the case if the dll is dynamic loaded, it might 
fail quietly, for example, if you deploy an Qt application with 
QWebKit missing those Open SSL dlls, an page with https protocol 
just doesn't open and didn't show any error at all. To find out 
whether some referenced dll by your executable is missing, people 
use Dependence Walker: http://www.dependencywalker.com/







Is build a 64 bit version worth if I'm looking for better perfomance?

2018-05-01 Thread Dr.No via Digitalmars-d-learn
Looking for make application run fast as possible, aside 
optimization in the source code, is using 64 bit over 32 really 
worth?


Re: How to curl!

2018-05-01 Thread Dr.No via Digitalmars-d-learn
On Tuesday, 1 May 2018 at 21:57:22 UTC, IntegratedDimensions 
wrote:

Trying to curl basic stuff but std.net.curl isn't cooperating:

curl "https://www.googleapis.com/youtube/v3/channels; -G -d 
part=contentDetails -d forUsername=test -d key=somekey


[...]


Just a wild guess, do you have the SSL dlls in the same folder as 
your executable or (less likely) in your PATH?


Can I convert the Range returned by asUpperCase to ubyte[]?

2018-05-01 Thread Dr.No via Digitalmars-d-learn
I'm trying to do an optimization here: a hash function which 
expect a ubye[] array as argument, would just work if I cast 
string to ubyte[] but I need to convert it to upper case, so I'd 
like to do that lazily, so that the byte is converted to its 
upper case version soon as it's requested. I'm not sure if this 
possible because I think the function should also work with Range 
and not ubyte[] to work.


So, adding some code example, can I convert the string to upper 
case then convert it to ubyte[] without memory allocation? 
something like this:



import xxhash;
import std.uni : asUpperCase;
uint hash = xxhashOf(cast(ubyte[])(word.asUpperCase));


Re: Get files from directory sorted by name

2018-04-27 Thread Dr.No via Digitalmars-d-learn

On Friday, 27 April 2018 at 14:48:00 UTC, Jesse Phillips wrote:

On Thursday, 26 April 2018 at 16:59:45 UTC, Dr.No wrote:
On Wednesday, 25 April 2018 at 19:25:11 UTC, Jesse Phillips 
wrote:

On Wednesday, 25 April 2018 at 17:34:41 UTC, Dr.No wrote:
Is there something implemented already to get the files from 
directory by name using D or I'm on my own and I have to 
write it myself? I didn't find how do that with dirEntries()


I want to add that sorting can be done, if you just call 
std.algorithm.sort you'll find that file names with numbers 
in them will be sorted as a well strings.


Newfile1.txt
Newfile10.txt
Newfile2.txt


I've had realized that then implemented natural sort


Thats what it was called. Looks like Rosetta Code has an 
implementation.


https://rosettacode.org/wiki/Natural_sorting#D


I've tried here and that implmentation doesn't yield same result 
as Windows Explorer...


readonly member (but assignable at constructor time)

2018-04-26 Thread Dr.No via Digitalmars-d-learn
In C# you can have a readonly member assignable either at 
declaration or constructor time, like this:


class C
{
  readonly myClass mc;

  this()
  {
mc = new myClass();
  }


  void doSomething()
  {
mc = new myClass(); // wrong! result in compiler error, mc is 
readonly

  }
}

Does D have something like this natively or there's a way to do 
so with traits/CTFE at runtime?


What's wrong with this alias?

2018-04-26 Thread Dr.No via Digitalmars-d-learn

consider this:

module report;

// output an error message on stderr
void error(A...)(string fmt, A args)
{
import colorize : fg, color, cwriteln, cwritefln, cwrite;
stderr.cwrite("error: ".color(fg.yellow));
cwritefln(fmt.color(fg.yellow), args);
}

void warning(A...)(string fmt, A args)
{
import colorize : fg, color, cwriteln, cwritefln, cwrite;
cwrite("warning: ".color(fg.blue));
cwritefln(fmt.color(fg.blue), args);
}

then

class C
{

void error(A...)(string fmt, A args)
{
import report : error;
reportedAnyError = true;
error(fmt, args);
}
alias warning = report.warning;
}


I got this:

Error: undefined identifier report.warning

but this works:

void warning(A...)(string fmt, A args)
{
import report : warning;
warning(fmt, args);
}

why alias cannot find my symbol there?



Re: Get files from directory sorted by name

2018-04-26 Thread Dr.No via Digitalmars-d-learn

On Wednesday, 25 April 2018 at 19:25:11 UTC, Jesse Phillips wrote:

On Wednesday, 25 April 2018 at 17:34:41 UTC, Dr.No wrote:
Is there something implemented already to get the files from 
directory by name using D or I'm on my own and I have to write 
it myself? I didn't find how do that with dirEntries()


I want to add that sorting can be done, if you just call 
std.algorithm.sort you'll find that file names with numbers in 
them will be sorted as a well strings.


Newfile1.txt
Newfile10.txt
Newfile2.txt


I've had realized that then implemented natural sort


Re: Get files from directory sorted by name

2018-04-26 Thread Dr.No via Digitalmars-d-learn
On Wednesday, 25 April 2018 at 18:06:07 UTC, Jonathan M Davis 
wrote:
On Wednesday, April 25, 2018 17:34:41 Dr.No via 
Digitalmars-d-learn wrote:
Is there something implemented already to get the files from 
directory by name using D or I'm on my own and I have to write 
it myself? I didn't find how do that with dirEntries()


There is nothing in the standard library for doing it, though 
maybe someone has something on code.dlang.org. However, the 
underlying OS API doesn't exactly conform well to that 
particular use case. AFAIK, given how the C APIs work, the only 
option is to get the list of files and then sort it, which 
could be done easily enough with dirEntries. Something as 
simple as


auto files = dirEntries(dir, SpanMode.shallow).array();
sort!((a, b) => a.name < b.name)(files);

would give you a sorted DirEntry[] of all of the directories 
and files directly in the directory. SpanMode.depth or 
SpanMode.breadth could be used instead if you want 
sub-directories, and std.algorithm.iteration.filter could be 
used if you want to do something like filter out directories. 
std.algorithm.iteration.map could be used if you just want the 
file names. So, if you wanted just the names, you could do


auto files = dirEntries(dir, SpanMode.shallow).map!(a => 
a.name)().array();

sort(files);

though you'd need to use std.path.baseName if you didn't want 
the full path
- e.g. map!(a => a.name.baseName)(). If you wanted just files, 
you could do

something like

auto files = dirEntries(dir, SpanMode.shallow).
 filter!(a => a.isFile()).array();
sort!((a, b) => a.name < b.name)(files);

or

auto files = dirEntries(dir, SpanMode.shallow).
 filter!(a => a.isFile()).map!(a => a.name).array();
sort(files);

Exactly which combination of functions you use depends on what 
you want for the end result. But the key thing is that you use 
std.array.array to convert the forward range into a dynamic 
array so that std.algorithm.sorting.sort can sort it (since it 
requires a random-access range). I really don't think that 
you're going to find any other way to do this other than 
someone who has written a function that just ends up doing the 
same thing by wrapping a call to dirEntries or the underlying C 
API.


- Jonathan M Davis


I have had just called sort, to later realize the sort wasn't in 
the way humans expect (which is the way Windows Explorer does) so 
I eventually reached 
https://blog.codinghorror.com/sorting-for-humans-natural-sort-order/ but I failed to call StrCmpLogicalW() from core.sys.windows.shlwapi or link on my own:


pragma(lib, "Shlwapi.lib")
extern(Windows) int StrCmpLogicalW(
  PCWSTR psz1,
  PCWSTR psz2
);

but I got links error so I went to implement StrCmpLogicalW() on 
my own and sorted like this:


auto files = dirEntries(inputDir, SpanMode.shallow).array();
// natural sort
sort!((a, b) => StrCmpLogical(a, b) < 0)(files);

which resulted in the files in same way as Windows 
Explorer/Natural sort.






Get files from directory sorted by name

2018-04-25 Thread Dr.No via Digitalmars-d-learn
Is there something implemented already to get the files from 
directory by name using D or I'm on my own and I have to write it 
myself? I didn't find how do that with dirEntries()


Re: How to use std.meta.Filter?

2018-04-21 Thread Dr.No via Digitalmars-d-learn
On Saturday, 21 April 2018 at 17:15:47 UTC, Jonathan M Davis 
wrote:
On Saturday, April 21, 2018 16:05:22 Dr.No via 
Digitalmars-d-learn wrote:

import std.meta : Filter;
enum isNotReservedSymbol(string name) = name != "none" && name 
!=

"lastToken";
enum string[] members = staticMembers!Token;
static foreach(member; Filter!(isNotReservedSymbol, members))
{{


This return the error:

  Error: template instance `pred!(["none", "word", "n", 
"digits",

"name", /* my whole array here */ ])  does not match template
declaration isNotReservedSymbol(string name)

how should have isNotReservedSymbol be defined?


std.meta.Filter operates on an AliasSeq, not a dynamic array. 
If you have an array, then you can just use 
std.algorithm.iteration.filter with a normal lambda.


- Jonathan M Davis


I've tried use normal filter - albeit i'm willing to do all that 
at runtin, but I got f cannot be read at compile time.


static foreach(member; staticMembers!Token.filter!(f => 
isNotReservedSymbol!(member))


How to use std.meta.Filter?

2018-04-21 Thread Dr.No via Digitalmars-d-learn

import std.meta : Filter;
enum isNotReservedSymbol(string name) = name != "none" && name != 
"lastToken";

enum string[] members = staticMembers!Token;
static foreach(member; Filter!(isNotReservedSymbol, members))
{{


This return the error:

 Error: template instance `pred!(["none", "word", "n", "digits", 
"name", /* my whole array here */ ])  does not match template 
declaration isNotReservedSymbol(string name)


how should have isNotReservedSymbol be defined?


get literal symbol name without base class/struct as string

2018-04-17 Thread Dr.No via Digitalmars-d-learn

give structs like this:

struct A
{
int a = 10;
string s = "haha";
}

struct B
{
A aDetails;
}

I'd like to do this and store that symbol name as string (my goal 
is store the member name);


string memberName = magic(B.aDetails.s);
writeln(memberName); // otuput "aDetails.s"

how can I do that?

closet I got was:

template nameof(alias S) {
import std.array : split, join;
import std.traits;
pragma(msg, fullyQualifiedName!S);
pragma(msg, "stringof = " ~ S.stringof);
enum parts = fullyQualifiedName!S.split(".");
enum nameof = parts[1 .. $].join(".");
}


but neither fullyQualifiedName nor stringof give the symbol in 
the way I need.


How do you check for nonempty string?

2018-04-13 Thread Dr.No via Digitalmars-d-learn

s.length > 0 or s !is null?
used to C++/C#'s world I cautch myself doing something like this:


if(s !is null && s.length > 0 && s[0] == '/)


then a I remembered the D's way, which length is probably a 
function not a property (in the C++/C#'s sense, which needs this 
as first parameter, hence s.length with s being null would result 
in error).


But it seems it's just enough:


string s = null;
if(s !is null && s[0] == '/')


is this correct?


Proper way to fix core.exception.UnicodeException@src\rt\util\utf.d(292): invalid UTF-8 sequence by File.readln()

2018-04-06 Thread Dr.No via Digitalmars-d-learn
I'm reading line by line the lines from a CSV file provided by 
the user which is assumed to be UTF8. But an user has provided an 
ANSI file which resulted in the error:


core.exception.UnicodeException@src\rt\util\utf.d(292): invalid 
UTF-8 sequence


(it happend when the user took the originally UTF8 encoded file 
generated by another application, made some edit using an editor 
(which I don't know the name) then saved not aware it was 
changing the encoding to ANSI.


My question is: what's the proper way to solve that? using toUTF8 
didn't solve:



while((line = csvFile.readln().toUTF8) !is null) {


I didn't find a way to set explicitly the encoding with 
std.stdio.File to set to UTF8 regardless it's an ANSI or already 
UTF8.
I don't want to conver the whole file to UTF8, the CSV file can 
be large and might take quite while. And if I do so to a 
temporary copy the file (which will make things even more slow) 
to avoid touch user's original file.


I thought in writing my own readLine() with 
std.stdio.File.byChunk to take as many bytes as possible until 
'\n' byte is seen, treat it as UTF8 and return.


But I'd like to not reinvent the wheel and use something native, 
if possible. Any ideas?