Re: FTP LS

2022-05-15 Thread ikod via Digitalmars-d-learn

On Saturday, 14 May 2022 at 08:42:46 UTC, Anonymouse wrote:
Before I go on duplicating effort, does anyone have anything 
that can access FTP beyond PUT and GET?


I need to read file information from a NAS so I know if I 
should upload a file or not. `dlang-requests` has 
`Request.post` and `Request.get`, but seemingly no way to LS.


Added LIST command in v2.0.8

```
ᐅ cat source/app.d
import std.stdio;
import requests;

void main()
{
auto rq = Request();
auto rs = rq.execute("LIST", 
"ftp://ftp.iij.ad.jp/pub/FreeBSD/;);

writeln(rs.responseBody);

}
/tmp/ttt ᐅ dub run
Running ttt
-rw-rw-r--1 ftp  ftp  4259 May 07  2015 README.TXT
-rw-rw-r--1 ftp  ftp35 May 12 09:00 TIMESTAMP
drwxrwxr-x9 ftp  ftp   169 Oct 05  2015 
development

-rw-r--r--1 ftp  ftp  2871 May 11 10:00 dir.sizes
drwxrwxr-x   22 ftp  ftp  8192 May 09 23:00 doc
drwxrwxr-x6 ftp  ftp92 Jan 10 21:38 ports
drwxrwxr-x   12 ftp  ftp   237 Feb 06  2021 releases
drwxrwxr-x   12 ftp  ftp   237 May 05 18:00 snapshots

```

--
Support !


Re: FTP LS

2022-05-14 Thread ikod via Digitalmars-d-learn

On Saturday, 14 May 2022 at 08:42:46 UTC, Anonymouse wrote:
Before I go on duplicating effort, does anyone have anything 
that can access FTP beyond PUT and GET?


I need to read file information from a NAS so I know if I 
should upload a file or not. `dlang-requests` has 
`Request.post` and `Request.get`, but seemingly no way to LS.


or I can implement this with http method HEAD. If this is ok - 
please post issue on github repo.


Thanks!

--
Support !


Re: FTP LS

2022-05-14 Thread ikod via Digitalmars-d-learn

On Saturday, 14 May 2022 at 10:17:14 UTC, ikod wrote:

On Saturday, 14 May 2022 at 08:42:46 UTC, Anonymouse wrote:
Before I go on duplicating effort, does anyone have anything 
that can access FTP beyond PUT and GET?


I need to read file information from a NAS so I know if I 
should upload a file or not. `dlang-requests` has 
`Request.post` and `Request.get`, but seemingly no way to LS.


It should. Maybe get ftp://path/ . Will check a bit later.


Yes, it should. If you get("ftp://hostname/path/;) it will try to 
download directory as file and fail. After that it will try to 
make NLST and will return directory listing.


Please note - AFAIK you will receive only names, no size or other 
file attributes.


--
Support !


Re: FTP LS

2022-05-14 Thread ikod via Digitalmars-d-learn

On Saturday, 14 May 2022 at 08:42:46 UTC, Anonymouse wrote:
Before I go on duplicating effort, does anyone have anything 
that can access FTP beyond PUT and GET?


I need to read file information from a NAS so I know if I 
should upload a file or not. `dlang-requests` has 
`Request.post` and `Request.get`, but seemingly no way to LS.


It should. Maybe get ftp://path/ . Will check a bit later.


Re: How to work with hashmap from memutils properly?

2022-02-16 Thread ikod via Digitalmars-d-learn
On Wednesday, 16 February 2022 at 10:31:38 UTC, Siarhei Siamashka 
wrote:

On Friday, 11 February 2022 at 19:04:41 UTC, Sergey wrote:
Is this an attempt to implement a high performance solution 
for the Benchmarks Game's LRU problem in D language?


Yes. There is no D version there. And I'm just curious how 
fast is D in those problems.


Sorry for late question - I can't find page with benchmark 
results, can you please share url?


Thanks


Re: Payload Details with std.net.curl:post

2021-11-29 Thread ikod via Digitalmars-d-learn

On Monday, 29 November 2021 at 01:49:37 UTC, Kyle Ingraham wrote:

On Sunday, 28 November 2021 at 07:27:35 UTC, ikod wrote:
On Sunday, 28 November 2021 at 01:06:45 UTC, Kyle Ingraham 
wrote:

On Saturday, 27 November 2021 at 22:18:48 UTC, ikod wrote:


Hi Kyle!

I found https://pki.goog/repository/ which has links to all 
certificates relevant to Google URLs. I worked my way down the 
certificate chain and the call that worked was:


```D
auto rq = Request();
rq.sslSetCaCert(r"gtsr1.pem");
```



Nice to hear that you solved this problem! Openssl under windows 
sometimes require
extra efforts. I tried to add support for native windows 
SecureChannel API in `requests`, but got stuck somewhere in the 
middle of the process.





Re: Payload Details with std.net.curl:post

2021-11-27 Thread ikod via Digitalmars-d-learn

On Sunday, 28 November 2021 at 01:06:45 UTC, Kyle Ingraham wrote:

On Saturday, 27 November 2021 at 22:18:48 UTC, ikod wrote:

On Saturday, 27 November 2021 at 20:31:16 UTC, Kyle Ingraham



Hi Kyle,



```
object.Exception@C:\Users\Kyle 
Ingraham\AppData\Local\dub\packages\requests-2.0.2\requests\source\requests\streams.d(890): ssl connect failed: certificate verify failed

```

I checked the issues page for `hunt-http` and saw that there 
were similar reports there from Windows users (I have not 
verified them myself). I did not try `vibe-http` because the 
callback interface in the docs wasn't one that grabbed me.



Do you have tips on why I might be getting that exception? I'm 
guessing I need to point `requests` at a trust store.



Yes, trust store location can be the problem. I hope you checked 
this readme sections: 
[windows-ssl-notes](https://github.com/ikod/dlang-requests#windows-ssl-notes) and [ssl-settings](https://github.com/ikod/dlang-requests#ssl-settings).


It is difficult to answer without having more details, but I 
recently fixed a bug that could lead to a similar problem on 
32-bit systems (regardless of OS). If this is your case, then you 
can try to use the latest commit from the github. Anyway, it 
would be nice if you create an issue on github and post more 
details there, so that I can try to reproduce the problem.




Re: Payload Details with std.net.curl:post

2021-11-27 Thread ikod via Digitalmars-d-learn
On Saturday, 27 November 2021 at 20:31:16 UTC, Kyle Ingraham 
wrote:

On Saturday, 27 November 2021 at 19:21:28 UTC, frame wrote:
On Saturday, 27 November 2021 at 17:41:55 UTC, Kyle Ingraham 
wrote:


I was afraid of that. Thank you for clarifying. I’ve tried 
other clients but they all seem to have trouble verifying HTTPS 
connections.


Which clients did you try, and what the troubles they have?



Re: Best way to make a template function conditionally @trusted

2021-04-02 Thread ikod via Digitalmars-d-learn

On Thursday, 1 April 2021 at 22:35:01 UTC, tsbockman wrote:
Suppose I have a templated struct member function for which I 
can compute at compile-time when the function is memory safe, 
and when it is not. But, the compiler cannot correctly 
determine this automatically.


Compiler should be able to derive safety of templated functions. 
You may just omit @safe/@trusted. But there are some rules and 
restrictions: 
https://dlang.org/spec/function.html#function-attribute-inference


Re: dub fetching dependencies for wrong configuration(s)

2020-10-29 Thread ikod via Digitalmars-d-learn

On Wednesday, 28 October 2020 at 21:56:46 UTC, Anonymouse wrote:

On Wednesday, 28 October 2020 at 19:34:43 UTC, ikod wrote:
To make this transition as painless as possible I'll create 
new package with major version "2" if you confirm that 
everything works as expected.

...

No vibe-d downloaded!


Thanks! I bumped version to 2.0.0
And thanks Andre for idea with packages.


Re: dub fetching dependencies for wrong configuration(s)

2020-10-28 Thread ikod via Digitalmars-d-learn

On Tuesday, 27 October 2020 at 22:15:34 UTC, Anonymouse wrote:

On Tuesday, 27 October 2020 at 21:15:35 UTC, ikod wrote:

On Tuesday, 27 October 2020 at 17:01:23 UTC, Anonymouse wrote:

On Sunday, 4 October 2020 at 21:00:49 UTC, ikod wrote:

Thanks, Andre!
I'll split requests as you suggested (increasing minor 
version, so that this change will not hurt anybody who build 
current requests package for vibe-d).


Will you make a requests-core?


I plan to clean requests package dependency from vibe, and 
make separate requests-vibe with vibe enabled config.


Is it urgent?


Not very, just following with interest!


Well, I pushed fix to github. Change consist from moving vibe-d 
support in separate subpackage:


"configurations": [
{
"name": "std"
}
],
"subPackages": [
"./vibed/"
],

So if you don't use requests on top of vibe-d sockets you can 
leave everything as is. And you should not fetch all vibe-d 
dependencies(!).


If you use requests with vibe-d sockets you have to use 
'requests:vibed' instead of 'requests' as dependency (and not 
subConfiguration)


To make this transition as painless as possible I'll create new 
package with major version "2" if you confirm that everything 
works as expected.


Thanks,
Igor


Re: dub fetching dependencies for wrong configuration(s)

2020-10-27 Thread ikod via Digitalmars-d-learn

On Tuesday, 27 October 2020 at 17:01:23 UTC, Anonymouse wrote:

On Sunday, 4 October 2020 at 21:00:49 UTC, ikod wrote:

Thanks, Andre!
I'll split requests as you suggested (increasing minor 
version, so that this change will not hurt anybody who build 
current requests package for vibe-d).


Will you make a requests-core?


I plan to clean requests package dependency from vibe, and make 
separate requests-vibe with vibe enabled config.


Is it urgent?


Re: Help on asdf json module

2020-10-25 Thread ikod via Digitalmars-d-learn

On Sunday, 25 October 2020 at 06:05:27 UTC, Vino wrote:

Hi All,

   Currently we are testing various json module such as 
"std.json, std_data_json, vibe.data.json and asdf", the below 
code works perfectely while use  "std_data_json or 
vibe.data.json" but not working as expected when we use "asdf" 
and throwing the below error, hence request your help on the 
same.


Error:

source/app.d(12,2): Error: invalid foreach aggregate 
jv.opIndex(["items"][]), define opApply(), range primitives, or 
use .tupleof


Maybe library author didn't implement opApply, so you have to 
iterate using range primitives empty, front, popFront, or handle 
data using algorithms.


You can check examples on the project page 
https://github.com/libmir/asdf


Re: std.net.curl get json_encode

2020-10-08 Thread ikod via Digitalmars-d-learn

On Friday, 9 October 2020 at 01:45:37 UTC, Vino wrote:

On Friday, 2 October 2020 at 23:20:48 UTC, Imperatorn wrote:

On Friday, 2 October 2020 at 21:12:09 UTC, Vino wrote:

Hi All,

...

auto content = https.perform();
https.shutdown;
JSONValue jv = parseJSONValue(content);


Maybe
JSONValue jv = toJSONValue(content);



writeln(jv["Name"]);

}

From,
Vino





Re: It is possible to substract 5 from 3 unsigned integer

2020-10-06 Thread ikod via Digitalmars-d-learn

On Tuesday, 6 October 2020 at 18:24:14 UTC, Alaindevos wrote:

There are two subtractions possible.
A machine-one which can be architecture dependent, does not 
have the same results on all computers, and behaves like a 
modulus in mathematics.

A logical one. For the last one higher classes might be needed.


Hello,

You may try 
https://dlang.org/phobos/std_experimental_checkedint.html in this 
case.


Regards,


Re: dub fetching dependencies for wrong configuration(s)

2020-10-04 Thread ikod via Digitalmars-d-learn

On Sunday, 4 October 2020 at 20:08:31 UTC, Andre Pany wrote:

On Sunday, 4 October 2020 at 14:08:24 UTC, Anonymouse wrote:


At the moment the only chance to avoid these additional 
dependencies would be to split requests into 2 dub packages. 
The core package which uses std, and an additional package 
which is based on the core package and is using vibe-d.


Kind regards
Andre


Thanks, Andre!
I'll split requests as you suggested (increasing minor version, 
so that this change will not hurt anybody who build current 
requests package for vibe-d).


Re: dub fetching dependencies for wrong configuration(s)

2020-10-04 Thread ikod via Digitalmars-d-learn

On Sunday, 4 October 2020 at 14:08:24 UTC, Anonymouse wrote:
My project depends on the requests dub package, which has two 
build configurations; one with an extra vibe-d dependency, one 
without (default).


"configurations": [
{
"name": "std"
},
{
"name": "vibed",
"versions": ["vibeD"],
"dependencies": {
"vibe-d": ">=0.8.0"
}
}
],

I only ever use the default "std" configuration, but dub always 
pulls the additional multiple vibe-d dependencies, regardless 
of what I pick. Adding a subConfiguration restriction does not 
seem to help.


$ dub upgrade
Upgrading project in /home/zorael/src/kameloso
Fetching vibe-core 1.10.2 (getting selected version)...





Is there any way to stop this?


AFAIK this is dub feature, which you cant't avoid easily. Naybe 
there are some reasons behind this feature, idk. But I'd like to 
avoid it too.




Re: Fiber based HTTP client for load testing

2020-10-01 Thread ikod via Digitalmars-d-learn

On Thursday, 1 October 2020 at 03:10:32 UTC, ikod wrote:

On Thursday, 1 October 2020 at 00:35:49 UTC, Arun wrote:

I have a REST client that can do load test of my REST



void main()
{
auto urls = ["http://httpbin.org/;, 
"http://httpbin.org/image;];

foreach(url; urls)
{
runWorkerTask(, url);


Or maybe just `runTask(, url)` - I'm not sure which 
function implements fiber and not worker thread.




Re: Fiber based HTTP client for load testing

2020-09-30 Thread ikod via Digitalmars-d-learn

On Thursday, 1 October 2020 at 00:35:49 UTC, Arun wrote:
I have a REST client that can do load test of my REST services. 
It uses ikod's dlang-requests under the hood. 
https://github.com/ikod/dlang-requests


At the moment I use a thread pool to dispatch the runtime 
operation and send the requests to the server.


As seen, the downside is that the more threads we use the more 
memory the app consumes. Is there a way to replace the threads 
with fibers in this particular case so that instead of spawning 
1000 threads, we spawn 1000 fibers with just 1 thread?


Hello, you can use requests with vibe.d sockets. You'll get the 
idea from example:


---
dub.json:
{
"authors": [
"me"
],
"copyright": "Copyright © 2020, me",
"dependencies": {
"requests": "~>1.1.7",
"vibe-d": "~>0.9.2"
},
"description": "A minimal D application.",
"license": "proprietary",
"name": "t",
"subConfigurations": {
"requests": "vibed"
}
}

---
import vibe.core.core;
import vibe.core.log;
import requests;

static void workerFunc(string url)
{
logInfo("Param: %s", url);
Request rq;
rq.verbosity = 1;
auto rs=rq.get(url);
logInfo("response code: %s", rs.code);
}

void main()
{
auto urls = ["http://httpbin.org/;, 
"http://httpbin.org/image;];

foreach(url; urls)
{
runWorkerTask(, url);
}
runApplication();
}
---

GET / HTTP/1.1
User-Agent: dlang-requests
Host: httpbin.org
Connection: Keep-Alive
Accept-Encoding: gzip,deflate

GET /image HTTP/1.1
User-Agent: dlang-requests
Host: httpbin.org
Connection: Keep-Alive
Accept-Encoding: gzip,deflate


< HTTP/1.1 200 OK
< date: Thu, 01 Oct 2020 03:07:33 GMT
< content-type: image/png
< content-length: 8090
< connection: keep-alive
< server: gunicorn/19.9.0
< access-control-allow-origin: *
< access-control-allow-credentials: true
< HTTP/1.1 200 OK
< date: Thu, 01 Oct 2020 03:07:33 GMT
< content-type: text/html; charset=utf-8
< content-length: 9593
< connection: keep-alive
< server: gunicorn/19.9.0

Connect time: 210 ms, 805 μs, and 9 hnsecs
Request send time: 293 μs and 2 hnsecs
Response recv time: 205 ms, 378 μs, and 9 hnsecs

< access-control-allow-origin: *
[vibe-6(ZLjs) INF] < access-control-allow-credentials: true
response code: 200

Connect time: 210 ms, 749 μs, and 6 hnsecs
Request send time: 307 μs and 6 hnsecs
Response recv time: 205 ms and 922 μs

[vibe-3(cfzw) INF] response code: 200



Re: Safe to remove AA elements while iterating over it via .byKeyValue?

2020-09-29 Thread ikod via Digitalmars-d-learn

Hello,

Sorry if I unintentionally hurt anybody in this thread.
I'll try to implement sane and correct iteration behavior for AA 
without noticeable performance loss, and propose it if I succeed.


Igor


Re: Safe to remove AA elements while iterating over it via .byKeyValue?

2020-09-28 Thread ikod via Digitalmars-d-learn

On Monday, 28 September 2020 at 19:18:20 UTC, H. S. Teoh wrote:
On Mon, Sep 28, 2020 at 05:18:41PM +, ikod via 
Digitalmars-d-learn wrote:
On Monday, 28 September 2020 at 14:58:15 UTC, Steven 
Schveighoffer wrote:

[...]

> One could write a specific function to iterate and remove. I

This looks like dead end to me, as you may not only remove 
items from arbitrary positions while iterating over 
collection, but also insert items.  Also this can happens not 
only inside foreach body, but in other kinds of iteration. And 
also there can be nested iterations over same collection.


The problem with arbitrary, unrestricted modification of a 
container while iterating over it, is that it inevitably leads 
to counterintuitive results.


Thanks for your reply, and sorry if I was not clear, but I meant 
AA. AA is unordered container, so for me intuitive behavior for 
mutations visibility during iteration is next:


1) you must not see removed keys. Let our keys are (unordered) A 
D C E F, you stay on D and remove E. Then E must not be seen on 
any future iteration steps.


2) you may or may not see any inserted keys. As AA is unordered 
container there is no reason to expect anything about position of 
key you just inserted - it can fall before or after current 
iteration position, so implementation is free to show inserted 
keys or not. I prefer not to see new keys.


3) you expect to visit all not removed keys exactly once.

Is it sane?



For example, suppose you have a container containing the 
elements A, B, C, D, E, and you're iterating over it in that 
order.


1) Suppose you're at element C, and you decide that you need to 
insert F. Then there's the question: should F be included in

...


2) Suppose you're at element C, and you decide to delete D. 
"Obviously", the current iteration should not include D. Or

...

Sure, everything you said is correct for ordered collection.


Also, what if the act of deleting D requires an internal 
reorganization of the container?  If this reorg changes the 
iteration order, how should the current iteration proceed?


Intuitive behavior should follow mentioned three rules regardless 
of internal implementation.





Re: Safe to remove AA elements while iterating over it via .byKeyValue?

2020-09-28 Thread ikod via Digitalmars-d-learn
On Monday, 28 September 2020 at 14:58:15 UTC, Steven 
Schveighoffer wrote:

On 9/27/20 4:43 PM, Per Nordlöw wrote:

On Sunday, 27 September 2020 at 14:23:11 UTC, H. S. Teoh wrote:
No.  Modifying a container while iterating over it is, in 
general, a bad idea (unless the container is designed to be


So the major problem of removing keys while iterating is that 
the AA can decide to rehash and shrink. If it does this, your 
iterator is invalidated -- you might skip elements that are

...


How do you "detect" this at compile time?


I think idea was to have some attributes that collection 
programmer may attach to implementation, like enums: 
SafeToRemoveOnIteration, SafeToAddOnIteration and so on, which 
may be checked at compile time when foreach is handled by 
compiler. Then in the foreach body compiler can refuse to call 
some unsafe methods. I do not know if it is worth of 
implementation, just this this was an idea.




One could write a specific function to iterate and remove. I


This looks like dead end to me, as you may not only remove items 
from arbitrary positions while iterating over collection, but 
also insert items. Also this can happens not only inside foreach 
body, but in other kinds of iteration. And also there can be 
nested iterations over same collection.


did it for dcollections (this was actually a very important 
functionality that I missed from C++ std::map, which is why I 
created dcollections in the first place).


I don't think it's worth worrying about this at compile time, 
or even at runtime. Just identify that if you do it, you are 
subject to peculiarities.


Yes, compile-time check is just nice option.



Then, we can introduce a specific mechanism to do this (total 
strawman, have no idea what the best thing looks like):




I have an idea on how to implement this, trading up memory for 
performance in some rare cases. Just need some time to estimate 
how it works.


Igor


Re: Safe to remove AA elements while iterating over it via .byKeyValue?

2020-09-28 Thread ikod via Digitalmars-d-learn

On Monday, 28 September 2020 at 10:10:10 UTC, Per Nordlöw wrote:

On Monday, 28 September 2020 at 10:01:23 UTC, ikod wrote:
Is it specific to some types? What if collection supports 
stable "foreach"?


Yes it depends on how collection members (such as insert, find, 
replace, erase, etc) are implemented.


I presume we need attributes on mutating collection members 
that describes if the operation invalidates ranges or not.





Agree, nice idea!



Re: Safe to remove AA elements while iterating over it via .byKeyValue?

2020-09-28 Thread ikod via Digitalmars-d-learn

On Monday, 28 September 2020 at 09:41:02 UTC, Per Nordlöw wrote:

On Monday, 28 September 2020 at 07:15:27 UTC, Imperatorn wrote:

Yes, this should be a compile-time error


Spec here: 
https://dlang.org/spec/statement.html#foreach_restrictions


Is it specific to some types? What if collection supports stable 
"foreach"?




Re: Safe to remove AA elements while iterating over it via .byKeyValue?

2020-09-28 Thread ikod via Digitalmars-d-learn

On Sunday, 27 September 2020 at 14:23:11 UTC, H. S. Teoh wrote:
On Sun, Sep 27, 2020 at 01:02:04PM +, Per Nordlöw via 
Digitalmars-d-learn wrote:
Is it safe to remove AA-elements from an `aa` I'm iterating 
over via aa.byKeyValue?


No.  Modifying a container while iterating over it is, in 
general, a bad idea (unless the container is designed to be 
used that way, but even then, such removal is generally 
restricted), because it often leads to highly counterintuitive 
results.  In the case of AA's, removing an element may lead to 
a rehashing, which reorders elements, and your iteration may 
miss some elements or repeat some elements or terminate 
prematurely. Even without a rehashing, you may encounter 
inconsistent behaviours, like some elements going "missing".


I think sane rule for AA iterator cane be like this:

1. you must not visit keys removed during iteration regardless of 
their position.
2. you must be able to visit all keys available when iteration 
started.
3. you may not visit keys added during iteration (as AA is 
unordered container)


Looks like this can be implemented for open addressing hash map 
without performance loss (in terms of speed). My has hmap 
implementation support rules 2 and 3, I'll try to add rule 1 to 
the list (right now it iterate over immutable copy of keys).




Re: vibe.d: How to get the conent of a file upload ?

2020-09-19 Thread ikod via Digitalmars-d-learn
On Saturday, 19 September 2020 at 18:56:20 UTC, Steven 
Schveighoffer wrote:

On 9/19/20 7:15 AM, ikod wrote:

On Saturday, 19 September 2020 at 11:11:21 UTC, ikod wrote:

On Friday, 18 September 2020 at 13:13:16 UTC, wjoe wrote:


My preferable way to handle such thing is: convert incoming 
data into input range of immutable(ubyte)[] and let user to 
consume this range (and handle it as it wish - transform, 
store in memory as is, or dump to disk)


sorry, this looks exactly like it described in proposal (and 
this is how requests works).




You mean for each file, right? Yeah, that is the proposal,


If user uploads several files through single form then yes, for 
each file. Maybe handler should receive range of input ranges and 
handle each as necessary.



though it uses Vibe's io type (necessary I think).

-Steve





Re: vibe.d: How to get the conent of a file upload ?

2020-09-19 Thread ikod via Digitalmars-d-learn

On Saturday, 19 September 2020 at 11:11:21 UTC, ikod wrote:

On Friday, 18 September 2020 at 13:13:16 UTC, wjoe wrote:
On Friday, 18 September 2020 at 12:58:29 UTC, Steven 
Schveighoffer wrote:

On 9/18/20 8:39 AM, Steven Schveighoffer wrote:
But again, solved with an enhancement that allows you to 
process the data in your code. I'll file the enhancement 
request for you, as I think it's a nice addition.


https://github.com/vibe-d/vibe.d/issues/2478

-Steve


Awesome! Thanks a ton :)


My preferable way to handle such thing is: convert incoming 
data into input range of immutable(ubyte)[] and let user to 
consume this range (and handle it as it wish - transform, store 
in memory as is, or dump to disk)


sorry, this looks exactly like it described in proposal (and this 
is how requests works).




Re: vibe.d: How to get the conent of a file upload ?

2020-09-19 Thread ikod via Digitalmars-d-learn

On Friday, 18 September 2020 at 13:13:16 UTC, wjoe wrote:
On Friday, 18 September 2020 at 12:58:29 UTC, Steven 
Schveighoffer wrote:

On 9/18/20 8:39 AM, Steven Schveighoffer wrote:
But again, solved with an enhancement that allows you to 
process the data in your code. I'll file the enhancement 
request for you, as I think it's a nice addition.


https://github.com/vibe-d/vibe.d/issues/2478

-Steve


Awesome! Thanks a ton :)


My preferable way to handle such thing is: convert incoming data 
into input range of immutable(ubyte)[] and let user to consume 
this range (and handle it as it wish - transform, store in memory 
as is, or dump to disk)


Re: Why is dtor called for lazy parameter?

2020-09-18 Thread ikod via Digitalmars-d-learn
On Friday, 18 September 2020 at 10:43:47 UTC, Andrey Zherikov 
wrote:

I have this code:
==
class S

...



auto create()
{
writeln("-> ",__PRETTY_FUNCTION__);
scope(exit) writeln("<- ",__PRETTY_FUNCTION__);

return scoped!S(1);
}


If you replace this line with "return new S(1)" it should work as 
you expect.




Re: Construct an used-defined hash table container type from an AA-literal expression

2020-07-05 Thread ikod via Digitalmars-d-learn

On Sunday, 5 July 2020 at 21:06:32 UTC, Per Nordlöw wrote:
Is there a way to construct a custom written hash-table 
container (struct) from an AA-literal expression?


can opAssign help?

struct AA(K,V)
{
void opAssign(V[K] aa)
{
}
}

void main()
{
AA!(string, int) custom_aa;
custom_aa = ["one":1, "two":2];
}


Re: Downloading files over TLS

2020-06-26 Thread ikod via Digitalmars-d-learn

On Friday, 26 June 2020 at 11:41:27 UTC, Jacob Carlborg wrote:

On Friday, 26 June 2020 at 11:10:27 UTC, ikod wrote:


[...]


Oh, it's that bad. That's disappointing.


[...]


I'm using a script (written in D) in my tool, DStep [1][2], to 
identify which versions of libclang is available and to select 
between dynamic link (not using `dlopen`) and static linking of 
libclang. It works pretty well.






[1] 
https://github.com/jacob-carlborg/dstep/blob/master/configure.d
[2] 
https://github.com/jacob-carlborg/dstep/blob/master/dub.json#L34-L41


Thanks, will try to improve this (at least to add static openssl 
linking, as this is not first time somebody asked about it)




--
/Jacob Carlborg




Re: Downloading files over TLS

2020-06-26 Thread ikod via Digitalmars-d-learn

On Friday, 26 June 2020 at 10:12:09 UTC, Jacob Carlborg wrote:
Downloading files over TLS. This seems that it's something that 
should be quite simple to do. My high level goals are 
cross-platform and easy distribution. I don't need anything 
fancy just a simple API like this:


download("https://url.com;, "/local/file");




Is there anything obvious I'm missing or why does this seem so 
difficult? Do I have too high expectations and requirements?


[1] https://code.dlang.org/packages/requests
[2] https://code.dlang.org/packages/vibe-d
[3] https://code.dlang.org/packages/hunt
[4] https://code.dlang.org/packages/arsd-official%3Ahttp
[5] 
https://developer.apple.com/documentation/security/secure_transport

[6] https://developer.apple.com/documentation/network
[7] 
https://developer.apple.com/documentation/foundation/nsurlsession

[8] https://code.dlang.org/packages/botan

--
/Jacob Carlborg


Hello,

re `requests` - it uses dlopen (and variants for OSX and Windows, 
see 
https://github.com/ikod/dlang-requests/blob/master/source/requests/ssl_adapter.d#L50). The reason for dlopen is simple - compatibility with both openssl ver 1.0 and 1.1 (which have incompatible interfaces). To solve this problem I expose common interface for Requests internal needs, and detect and use different underlying openssl interfaces depending on library version.


I'm sure it is possible to detect library version at build time, 
and then use static linking. It just require some time investment 
to find most acceptable solution on how to do this.


Re Windows - I'd be happy to use native SecureChannel but I have 
zero windows programming experience. I'm completely open for any 
help in this field.


Bottom line - problem with SSL/TLS libraries lies in 
incompatibility between platforms and even inside the single 
library.




Re: Use classes as keys in associative array

2020-06-06 Thread ikod via Digitalmars-d-learn

On Saturday, 6 June 2020 at 20:31:36 UTC, ikod wrote:

On Saturday, 6 June 2020 at 16:49:29 UTC, JN wrote:
Is it possible to use different class instances as keys for 
associative array, but compare them by the contents? I tried 
to override opEquals but it doesn't seem to work. Basically I'd


May be wrong, but probably you have to override opEquals to 
Object, not to another class instance.


Oops, sorry, it was answered already


Re: Use classes as keys in associative array

2020-06-06 Thread ikod via Digitalmars-d-learn

On Saturday, 6 June 2020 at 16:49:29 UTC, JN wrote:
Is it possible to use different class instances as keys for 
associative array, but compare them by the contents? I tried to 
override opEquals but it doesn't seem to work. Basically I'd


May be wrong, but probably you have to override opEquals to 
Object, not to another class instance.




Re: Fibers and std.socket

2020-05-22 Thread ikod via Digitalmars-d-learn

On Friday, 22 May 2020 at 06:10:38 UTC, Atwork wrote:

Is it possible to mix fibers with sockets from phobos?

If so, how would I do it?

Like just a simple example of async sockets using fibers in D.

I will say that I'd prefer to not use any packages ex. vibe.d


Yes you can mix std sockets with fibers, but then you have two 
options - either sockets are in blocking mode (and every socket 
can block current thread and other fibers) or you use sockets in 
non-blocking mode and then you have to manage socket events in 
some hand-made event loop. AFAIK there is no other options.


Re: undefined reference to 'deflateEnd'

2020-05-18 Thread ikod via Digitalmars-d-learn

On Monday, 18 May 2020 at 07:33:29 UTC, Andre Pany wrote:

On Monday, 18 May 2020 at 06:02:03 UTC, ikod wrote:

NAME="Ubuntu"
VERSION="18.04.4 LTS (Bionic Beaver)"

[...]


Thanks a lot.

Kind regards
André


Just discovered that we can import etc.c.zlib and use low level 
zlib directly


Re: undefined reference to 'deflateEnd'

2020-05-18 Thread ikod via Digitalmars-d-learn

On Monday, 18 May 2020 at 05:01:45 UTC, Andre Pany wrote:

Hi,

I have some issues, the get this program working on ubuntu:

``` Dockerfile
FROM ubuntu:focal

RUN apt-get update && apt-get upgrade -y \
&& apt-get install --no-install-recommends -y 
build-essential ldc dub zlib1g-dev


COPY app.d /tmp/
RUN dub build --single /tmp/app.d -v
```

``` app.d
/+ dub.sdl:
name "app"
lflags "-lz" "-ldl"
dflags "-static"
+/
import std;
void main(){}
```

Linker fails with error messages like these:
/usr/lib/gcc/x86_64-linux-gnu/9/../../../x86_64-linux-gnu/libphobos2-ldc.a(zlib.o):function
 _D3std4zlib8Compress5errorMFiZv: error: undefined reference to 'deflateEnd'
/usr/lib/gcc/x86_64-linux-gnu/9/../../../x86_64-linux-gnu/libphobos2-ldc.a(zlib.o):function
 _D3std4zlib8Compress6__dtorMFZv: error: undefined reference to 'deflateEnd'

The actual link command looks right:
/usr/bin/ldc2 
-oftmp/.dub/build/application-debug-linux.posix-x86_64-ldc_2090-ED4668B88284A40FA4668246697CB2B5/app tmp/.dub/build/application-debug-linux.posix-x86_64-ldc_2090-ED4668B88284A40FA4668246697CB2B5/app.o -L--no-as-needed -L-lz -L-ldl -static -g


Do you have an idea?

Kind regards
André


NAME="Ubuntu"
VERSION="18.04.4 LTS (Bionic Beaver)"

Works for me on ubuntu when changed dflags to lflags:

/+ dub.sdl:
name "app"
lflags "-lz" "-ldl"
lflags "-static"
+/
import std;
void main(){}

/usr/bin/dmd -c 
-of.dub/build/application-debug-linux.posix-x86_64-dmd_2089-1D31FAC5109A6D2373EAB3139BEB0877/app.o -debug -g -w -version=Have_app t.d -vcolumns

Linking...
/usr/bin/dmd 
-of.dub/build/application-debug-linux.posix-x86_64-dmd_2089-1D31FAC5109A6D2373EAB3139BEB0877/app .dub/build/application-debug-linux.posix-x86_64-dmd_2089-1D31FAC5109A6D2373EAB3139BEB0877/app.o -L--no-as-needed -L-lz -L-ldl -L-static -g
Copying target from 
/tmp/test/.dub/build/application-debug-linux.posix-x86_64-dmd_2089-1D31FAC5109A6D2373EAB3139BEB0877/app to /tmp/test

Running ./app





Re: D and Async I/O

2020-05-11 Thread ikod via Digitalmars-d-learn
On Monday, 11 May 2020 at 21:15:28 UTC, Steven Schveighoffer 
wrote:

On 5/11/20 3:46 PM, ikod wrote:

On Monday, 11 May 2020 at 17:34:41 UTC, Jacob Carlborg wrote:

On 2020-05-11 16:44, Russel Winder wrote:


Crickey, a third option. This wil increase my dithering! ;-)


Forth: Mecca [1] :)

[1] https://github.com/weka-io/mecca


And probably more. At least I also have my async library for 
network IO.


It would be nice to have well defined interface for async io. 
That will allow to choose and test different implementations.


Part of the problem is the different APIs that async libraries 
use. Some use callbacks, some use fibers, maybe some 
async/await forms.




callbacks are basic building blocks, which should be supported 
anyway.


std.io aims to provide a common interface for async and sync 
i/o, where you choose the driver at the beginning of your 
program. If written properly, it could be a nice way to test


My library also can work in this modes. There are low-level 
"sockets" which can be used only with callbacks, and also 
high-level sockets which can be used in "sync" mode in fibers or 
even without fibers as a replacement for std.socket's. Here is 
simple example where callback-based Timer used to build "sync" 
sleep:


void Sleep(Duration d) {
if ( d <= 0.seconds) {
return;
}
auto f = Fiber.getThis();
if (f is null)
{
// called not from fiber/task, there is no concurrency,
// use plain old sleep
Thread.sleep(d);
return;
}
// otherwise we yield and return to current fiber
// later, when timer expires (using event loop)
auto callback = delegate void (AppEvent e)
{
assert(e == TimeoutExpired);
f.call();
};
auto t = new Timer(d, callback);
getDefaultLoop().startTimer(t);
Fiber.yield();
}


code with various drivers without having to change code. It 
would require a Fiber-based approach, however.


I have not added an async driver (yet), but it's in my somewhat 
immediate plans to do so, as I want to start using this more 
(along with iopipe). Contributions would be welcome.


initially very few interfaces required - like start/stop for 
Timer, start/stop for SignalHandling, start/stop for file/socket 
poll.




https://github.com/MartinNowak/io

-Steve





Re: D and Async I/O

2020-05-11 Thread ikod via Digitalmars-d-learn

On Monday, 11 May 2020 at 17:34:41 UTC, Jacob Carlborg wrote:

On 2020-05-11 16:44, Russel Winder wrote:


Crickey, a third option. This wil increase my dithering! ;-)


Forth: Mecca [1] :)

[1] https://github.com/weka-io/mecca


And probably more. At least I also have my async library for 
network IO.


It would be nice to have well defined interface for async io. 
That will allow to choose and test different implementations.


Re: How to get to body of HTTP 500 error with std.net.curl.get()?

2020-02-16 Thread ikod via Digitalmars-d-learn

On Saturday, 15 February 2020 at 20:38:51 UTC, Anonymouse wrote:

On Saturday, 15 February 2020 at 16:25:42 UTC, Gregor Mückl


When testing to confirm I ran into a bug[2] where the body is 
sometimes empty, but outside of fringe cases it should work.


[1]: https://code.dlang.org/packages/requests
[2]: https://github.com/ikod/dlang-requests/issues/115


Just a note - this is not a bug, server really send empty body:

< HTTP/1.1 500 Internal Server Error
< cache-control: private
< server: Microsoft-IIS/10.0
< x-aspnetmvc-version: 5.1
< access-control-allow-origin: *
< x-aspnet-version: 4.0.30319
< request-context: 
appId=cid-v1:7585021b-2db7-4da6-abff-2cf23005f0a9

< access-control-expose-headers: Request-Context
< x-powered-by: ASP.NET
< date: Sat, 15 Feb 2020 20:44:03 GMT
< content-length: 0
< 0 bytes of body received



Re: Intersection of two sets

2019-12-03 Thread ikod via Digitalmars-d-learn

On Tuesday, 3 December 2019 at 13:43:26 UTC, Jan Hönig wrote:

It seems i don't google the right keywords.

What i want to do: I have two sets. (I didn't find how to do 
sets, so i have two associative boolean arrays 
`bool[]`). And i want to join them, via an 
intersection.


I know how to code this. Loop over one AA, if the key is also 
in the other one, we add that to the third array which is going 
to be returned.


pseudocode:
alias set = bool[]
set foo = ...
set bar = ...
set result;

foreach(key; foo)
{
  if (key in bar)
  {
result[key] = true;
  }
}
return result;


1) Is there a better way for creating a set, other then `alias 
set = bool[keyClass]`?

2) Are there some build-in function for handling such sets?


Never tried, but depending of the nature of your "something" you 
can try bit sets. There are efficient algorithms for large bit 
arrays (see "roaring" for example).


Re: dmd memory usage

2019-11-17 Thread ikod via Digitalmars-d-learn
On Monday, 18 November 2019 at 00:20:12 UTC, Steven Schveighoffer 
wrote:
I'm fighting some out of memory problems using DMD and some 
super-template heavy code.


I have ideas on how to improve the situation, but it involves 
redesigning a large portion of the design. I want to do it 
incrementally, but I need to see things improving.


Is there a straightforward way to figure out how much memory 
the compiler uses during compilation? I though maybe 
/usr/bin/time, but I feel like I don't trust the output to be 
the true max resident size to be what I'm looking for (or that 
it's 100% accurate). Is there a sure-fire way to have DMD print 
it's footprint?


-Steve


Hello,
You can look at
http://man7.org/linux/man-pages/man2/getrusage.2.html
But, probably /usr/bin/time already use it.



Re: (SOLVED) requests module generates " Can't parse uri '' "

2019-11-10 Thread ikod via Digitalmars-d-learn

On Sunday, 10 November 2019 at 01:28:13 UTC, cartland wrote:

On Saturday, 9 November 2019 at 01:02:57 UTC, cartland wrote:

On Saturday, 9 November 2019 at 00:53:13 UTC, cartland wrote:

On Friday, 8 November 2019 at 17:01:07 UTC, ikod wrote:
*snip*
Even this does it.

import requests;
void main() {
}


https://github.com/ikod/dlang-requests/issues/109


Figured it out. See github issue. I had http_proxy env var set 
to blank. Worked when I unset it.


Thanks for report, fixed in latest release.


Re: requests module generates " Can't parse uri '' "

2019-11-08 Thread ikod via Digitalmars-d-learn

On Friday, 8 November 2019 at 03:29:41 UTC, cartland wrote:

First time D use :)

After dub init, I used the example from 
https://code.dlang.org/packages/requests and ran "dub add 
requests"


[...]


If you still stuck with this problem, you can post new issue on 
github project page, I'll try to solve it.


https://github.com/ikod/dlang-requests


Re: How should I sort a doubly linked list the D way?

2019-08-13 Thread ikod via Digitalmars-d-learn
On Tuesday, 13 August 2019 at 09:48:52 UTC, Mirjam Akkersdijk 
wrote:

Hello there,
If I had a DLL, how would I sort it judging by the node 
contents, the D way?


In C if I were to sort a piece of malloc'd memory pointing to 
node pointers, I would write my compare function and let qsort 
sort it out. In D, I tried to use std.algorithm's sort 
functionality to no avail, because my guess would be it only 
operates on arrays. This opens up another can of worms as I am 
not fond of VLAs and D desperately wants to know what the size 
is at compile time.


You can check std.container.rbtree which will build sorted "list" 
for you on every Node insert. Again, not sure how this can be 
done at compile time.


Re: Error: @nogc function run cannot call non-@nogc destructor TcpServer.~this

2019-07-17 Thread ikod via Digitalmars-d-learn

On Wednesday, 17 July 2019 at 10:51:53 UTC, Newbie2019 wrote:
this is very hard to made a minimal example, and the projects 
only build with ldc.


There is a struct TcpServer has no destructor, and all method 
is @nogc nothrow.


Then just add destructor with @nogc attribute.




Re: How to debug long-lived D program memory usage?

2019-04-22 Thread ikod via Digitalmars-d-learn

On Sunday, 21 April 2019 at 21:04:52 UTC, Patrick Schluter wrote:

On Thursday, 18 April 2019 at 12:00:10 UTC, ikod wrote:
On Wednesday, 17 April 2019 at 16:27:02 UTC, Adam D. Ruppe 
wrote:
D programs are a vital part of my home computer 
infrastructure. I run some 60 D processes at almost any 
time and have recently been running out of memory.


I usually run program under valgrind in this case. Though it 
will not help you to debug GC problems, but will cut off 
memory leaked malloc-s.


Even valgrind tool=massif ?


I rarely use massif. It is heap profiler, and my primary target 
usually are memory leaks.


Does your question mean that massif can help to debug GC?


Q: docs and guides for hunt

2019-04-21 Thread ikod via Digitalmars-d-learn

Hello,

I'l like to try 'hunt' project to solve one of my problems, but I 
can't find docs and code samples or tutorials, etc. I even can't 
find if hunt (or hunt-framework) can be used for my task.


Please, help!

Here is short descriptions what I need - long running 
application, periodically polling hundreds services over http and 
plain TCP, and exposing results as single html page. My needs are 
- single or several threads,  something like async/await in 
fiber, no callback hell.


Thanks!



Re: How to debug long-lived D program memory usage?

2019-04-18 Thread ikod via Digitalmars-d-learn

On Wednesday, 17 April 2019 at 16:27:02 UTC, Adam D. Ruppe wrote:
D programs are a vital part of my home computer infrastructure. 
I run some 60 D processes at almost any time and have 
recently been running out of memory.


I usually run program under valgrind in this case. Though it will 
not help you to debug GC problems, but will cut off memory leaked 
malloc-s.


Re: ddox and exempting dependencies

2018-12-05 Thread ikod via Digitalmars-d-learn

On Wednesday, 5 December 2018 at 17:12:03 UTC, Anonymouse wrote:

On Wednesday, 5 December 2018 at 16:56:12 UTC, Andre Pany wrote:
The compiler (DMD) is triggered to generate a JSON file 
docs.json which contains technical information used by ddox in 
a later step. The error message you see is thrown by dmd.

At this point of time, ddoxFilterArgs isn't evaluated at all.

I assume this line causes the issue
https://github.com/ikod/cachetools/blob/master/source/cachetools/hash.d#L7
You can contact the author (via github issue) to solve the 
DDOC warning.


Kind regards
André

The -ddoxFilterArgs is evaluated in a later step by DDOX.


Will do, many thanks.


Hello,

I replaced dashes with equal signs in comment. Didn't know that 
it breaks ddoc.


You can try v0.0.7 - should be fixed.


Re: what are the rules for @nogc and @safe attributes inference?

2018-11-30 Thread ikod via Digitalmars-d-learn

On Friday, 30 November 2018 at 21:03:06 UTC, Neia Neutuladh wrote:

On Fri, 30 Nov 2018 20:41:03 +, ikod wrote:
I can't find the reason why nogc/nothrow can't be inferred in 
this case:


class S(K,V)
{
 auto get/*()*/(K a) {
 return 0;
 }
}
void main() @nogc nothrow {
 S!(int, string) sia;
 auto v = sia.get(1);
}


class Nefarious : S!(int, string)
{
  override int get(int a)
  {
// Whoops, I used the GC
return new char[a].length;
  }
}

The compiler can't prove that a variable of type S!(int, 
string) will not be of type Nefarious, which uses the GC, so it 
can't infer @nogc for S.get.


However, if you make the function final, then the compiler can 
infer it to be pure nothrow @nogc @safe.


Or if you use a struct instead of a class, structs don't do 
inheritance, so the compiler can infer attributes without 
worrying about nefarious inheritance.



But everything is ok if you uncomment parentheses after get.


Templated functions are implicitly final.


Thanks for explanation, got it.

My case is actually

interface I(K,V)
{
int get()(K);
}
class S(K,V) : I!(K, V)
{
int v;
int get()(K a)
{
return v;
}
}
void main() nothrow
{
S!(int, string) s = new S!(int, string);
s.get(1);
}

My goal is to allow compiler to infer all properties of s.get 
without adding nothrow/nogc anywhere.  And these templated 
functions is only way it works for me. Is it ok? Or there is 
better solution?


Thanks!


Re: what are the rules for @nogc and @safe attributes inference?

2018-11-30 Thread ikod via Digitalmars-d-learn
On Thursday, 15 November 2018 at 21:55:18 UTC, Steven 
Schveighoffer wrote:

On 11/15/18 4:09 PM, Adam D. Ruppe wrote:

On Thursday, 15 November 2018 at 21:00:48 UTC, ikod wrote:

what are the rules for @nogc inference?


It attempts it if and only if it is a template.


Well, the general "rule" is, if it's code that must be 
available to the compiler when it's called, then it will be 
inferred.


Examples of code that must be processed every time it's used:

1. Template functions
2. auto-returning functions
3. functions inside templates (like member functions of a 
templated struct)

4. Inner functions

There may be others I didn't think of.

Everything else must be manually attributed. The reasoning is 
that the function may be stubbed in a .di file, and in that 
case, attribute inference wouldn't be possible.


-Steve


Hello, Steve!

I can't find the reason why nogc/nothrow can't be inferred in 
this case:


class S(K,V)
{
auto get/*()*/(K a)
{
return 0;
}
}
void main() @nogc nothrow
{
S!(int, string) sia;
auto v = sia.get(1);
}

But everything is ok if you uncomment parentheses after get. get 
is already a member of templated class, what can be wrong with 
this code?


Thanks!


Re: what are the rules for @nogc and @safe attributes inference?

2018-11-16 Thread ikod via Digitalmars-d-learn

On Friday, 16 November 2018 at 12:12:12 UTC, Alex wrote:

=
This code compiles as long as `lazy` is commented out. But I'd 
like to have
both lazy parameter and @nogc inferrence for `library_func` so 
that user is not locked to code @nogc or not.


Aha, aha... Interesting!
I had a similar problem with passing a delegate. And it was 
solved:

https://forum.dlang.org/thread/erznqknpyxzxqivaw...@forum.dlang.org

For your problem, there is a bug report:
https://forum.dlang.org/post/wedwfooqdxbwxttpm...@forum.dlang.org
https://issues.dlang.org/show_bug.cgi?id=12664
https://issues.dlang.org/show_bug.cgi?id=12647


Thanks for these links.
And yes, I have workaround with handmade delegate. It compiles 
and it is lazy, but also ugly.


import std.traits;

T library_func(T)(/*not lazy*/ T i) if (!isCallable!T)
{
return i;
}
ReturnType!T library_func(T)(/*lazy*/ T i) if (isCallable!T)
{
return i();
}
void user_function_nogc() @nogc
{
int x = 1;
library_func(x+1);
}
void user_function_gc()
{
library_func(()=>[1]);
}
void main()
{
}


Re: what are the rules for @nogc and @safe attributes inference?

2018-11-16 Thread ikod via Digitalmars-d-learn
On Thursday, 15 November 2018 at 21:55:18 UTC, Steven 
Schveighoffer wrote:

On 11/15/18 4:09 PM, Adam D. Ruppe wrote:

On Thursday, 15 November 2018 at 21:00:48 UTC, ikod wrote:

what are the rules for @nogc inference?


It attempts it if and only if it is a template.


Well, the general "rule" is, if it's code that must be 
available to the compiler when it's called, then it will be 
inferred.


Examples of code that must be processed every time it's used:

1. Template functions
2. auto-returning functions
3. functions inside templates (like member functions of a 
templated struct)

4. Inner functions

There may be others I didn't think of.

Everything else must be manually attributed. The reasoning is 
that the function may be stubbed in a .di file, and in that 
case, attribute inference wouldn't be possible.


-Steve


Thanks for clarifications, Adam and Steven!

My problem is next code:
=
import std.traits;

T library_func(T)(/*lazy*/ T i)
{
// this fuction can be @nogc or not depending on T properties
static if (isArray!T) {
return i ~ i;
}
else
{
return i;
}
}
void user_function_nogc() @nogc
{
int x = 1;
library_func(x+1);
}
void user_function_gc()
{
library_func([1]);
}
void main()
{
}
=
This code compiles as long as `lazy` is commented out. But I'd 
like to have
both lazy parameter and @nogc inferrence for `library_func` so 
that user is not locked to code @nogc or not.




Re: how to make '==' safe for classes?

2018-10-28 Thread ikod via Digitalmars-d-learn

Thanks for excellent explanation!

On Sunday, 28 October 2018 at 19:00:52 UTC, Jonathan M Davis 
wrote:


Because Object predats @safe (and most attributes), it really 
isn't compatible with them. In fact, it predates const (since 
it's basically the same as it was in D1) and it's only possible 
to compare const class objects because of a hack in the free 
function opEquals (which == lowers to) which casts away const 
(meaning that if you're not careful, you can actually violate 
the type system with == by mutating an object in a class'

...
So, unfortunately, for now, you're going to have to use 
@trusted with == on classes, as stupid as that is, though as a 
workaround, you could always create an @trusted wrapper 
function that just called ==. It would still be annoying, but 
it would be cleanly restricted the @trusted bits.


- Jonathan M Davis





Re: how to make '==' safe for classes?

2018-10-28 Thread ikod via Digitalmars-d-learn
On Sunday, 28 October 2018 at 18:00:06 UTC, Stanislav Blinov 
wrote:

On Sunday, 28 October 2018 at 12:38:12 UTC, ikod wrote:

and object.opEquals(a,b) do not inherits safety from class C 
properties, and also I can't override it.


Yep. Since Object is the base class and it defines opEquals as:


object.opEquals(a,b) even is not a Object member function, it's 
free function.


It looks a bit unnatural for me, but thanks for confirmation!



```
bool opEquals(Object);
```

the compiler rewrites `a == b` as 
`(cast(Object)a).opEquals(cast(Object)ob)`, i.e. it inserts a 
@system call into your code.


Is there clean way to use '==' here, or I have to convert this 
to a.opEquals(b) for classes, leaving '==' for structs?


Pretty much, yes. "Implicit" value comparison in general is 
somewhat alien for classes, since they're reference types.





how to make '==' safe for classes?

2018-10-28 Thread ikod via Digitalmars-d-learn

Hello

How to make this code to compile? My goal is safe(not @trusted) 
longFunction().



---
class C
{
override bool opEquals(Object o) const @safe
{
return true;
}
}
bool longFunction(C a, C b) @safe
{
return a==b;
}
void main()
{
}
---


As far as I understand the problem is that AST for this code 
looks like


---
import object;
class C : Object
{
override const @safe bool opEquals(Object o)
{
return true;
}
}
bool longFunction(C a, C b)
{
return opEquals(a, b);
}
void main()
{
return 0;
}
RTInfo!(C)
{
enum typeof(null) RTInfo = null;

}
---

and object.opEquals(a,b) do not inherits safety from class C 
properties, and also I can't override it.


Is there clean way to use '==' here, or I have to convert this to 
a.opEquals(b) for classes, leaving '==' for structs?


Thanks!


Re: How to curl!

2018-05-02 Thread ikod via Digitalmars-d-learn
On Wednesday, 2 May 2018 at 16:58:35 UTC, IntegratedDimensions 
wrote:

On Wednesday, 2 May 2018 at 03:03:19 UTC, ikod wrote:
On Wednesday, 2 May 2018 at 00:04:49 UTC, IntegratedDimensions 
wrote:
On Tuesday, 1 May 2018 at 23:35:42 UTC, Arun Chandrasekaran 
wrote:

[...]


Ok, first try:

Unhandled exception: object.Exception can't complete call to 
TLS_method at requests\ssl_adapter.d(248)


which says it can't find the lib. I renamed ssleay32.dll to 
libssl32.dll and it then passed. I am getting http 404 error 
though ;/


I turned off cert though, which may be the reason, I have no 
idea. Seems to be working through.


Did you try to use full path to cacert.pem?


That seemed to work. Why would it not look in the current 
directory by default? Maybe something to add?


Not sure about curl, but dlang-requests just pass filename as-is 
to libssl. I think that if you have cert file in current 
directiry, and use no path in call to sslSetCertFile it should 
work. If not - please post bugreport to 
github.com/ikod/dlang-requests


PS. I'll check and add ssleay32 to search names for dynamic ssl 
library load.






Re: How to curl!

2018-05-01 Thread ikod via Digitalmars-d-learn
On Wednesday, 2 May 2018 at 00:04:49 UTC, IntegratedDimensions 
wrote:
On Tuesday, 1 May 2018 at 23:35:42 UTC, Arun Chandrasekaran 
wrote:
On Tuesday, 1 May 2018 at 21:57:22 UTC, IntegratedDimensions 
wrote:

[...]


std.net.curl is not quite up to date when compared to the curl 
command. It is not useful any non-trivial stuff. Right now I'm 
using ikod's dlang-requests[1] and I'm quite happy with it. I 
would recommend you to try it.


[1] https://github.com/ikod/dlang-requests


Ok, first try:

Unhandled exception: object.Exception can't complete call to 
TLS_method at requests\ssl_adapter.d(248)


which says it can't find the lib. I renamed ssleay32.dll to 
libssl32.dll and it then passed. I am getting http 404 error 
though ;/


I turned off cert though, which may be the reason, I have no 
idea. Seems to be working through.


Did you try to use full path to cacert.pem?


Re: HTTP-methods and encoding

2018-04-08 Thread ikod via Digitalmars-d-learn

On Saturday, 7 April 2018 at 23:54:21 UTC, Vindex wrote:

On Saturday, 7 April 2018 at 15:58:14 UTC, Seb wrote:

On Saturday, 7 April 2018 at 13:02:39 UTC, Vindex wrote:
There is an error on some sites when using HTTP-methods 
(std.net.curl.get, std.net.curl.post):
std.encoding.EncodingException@std/encoding.d(2505): 
Unrecognized Encoding: utf8


Is there a beautiful way around it?
For the GET-method I use the download() and readText(). But 
for the POST-method I can not come up with an alternative 
solution.


That's weird. std.net.curl should be able to handle UTF-8.
What content are you trying to download/post?
Maybe you can open a bug report for it?

In any case, you might want to checkout requests:

https://github.com/ikod/dlang-requests

It's by far more convenient to use than std.net.curl


The problem is that "utf-8" (or "UTF-8") is required instead of 
"utf8".


I tried to get HTML and JSON. For example, this GET-query 
returns error:

"https://yobit.net/api/3/ticker/btc_usd;

Thank you for the advice


Hello,

"utf-8" (or "UTF-8") is required instead of "utf8" - explain, 
please.


Anyway this code works as expected:

import requests;
import std.stdio;
import std.format;

void main() {
auto rq = Request();
auto rs = rq.get("https://yobit.net/api/3/ticker/btc_usd;);
writeln(rs.responseBody);
}

output:

{"btc_usd":{"high":7216.09463851,"low":6950,"avg":7083.04731925,"vol":753989.73116823,"vol_cur":105.94453165,"last":7114,"buy":7114.,"sell":7135.1911,"updated":1523170067}}



can be (){yield();} mark as @trusted?

2018-03-28 Thread ikod via Digitalmars-d-learn

Hello,

I have very short function with only Fiber.yield() inside it, 
like:


void f1() {
yield();
}

This function is called inside @safe function f2:

void f2() @safe {
f1();
}

Can f1 be marked as @trusted so that I can keep @safe for the 
whole hierarchy of calls?


Thanks!


inline @trusted code

2018-01-30 Thread ikod via Digitalmars-d-learn

Hello,

Several times I faced with next problem: I have "@safe" routine 
with few calls to @system functions inside: OS calls (recv, send, 
kqueue) os some phobos unsafe functions like assumeUnique. I'd 
like not to wrap these @system functions in @trusted wrappers, 
but somehow mark these calls @trusted inline.


In ideal case this should look like

import std.exception;

void main() @safe
{
ubyte[] a;
@trusted {
auto b = assumeUnique(a);
};
}

But the closest variant is

import std.exception;

void main() @safe
{
ubyte[] a;
delegate void() @trusted {
auto b = assumeUnique(a);
}();
}

Which looks a bit verbose.

How do you solve this problem?

Thanks!


Re: libcurl acting differently to curl.exe

2017-12-11 Thread ikod via Digitalmars-d-learn

On Monday, 11 December 2017 at 03:53:25 UTC, Josh wrote:

The POST C code was:

/* Sample code generated by the curl command line tool 
**

 * All curl_easy_setopt() options are documented at:
 * https://curl.haxx.se/libcurl/c/curl_easy_setopt.html


Maybe off-topic but you can try 
http://code.dlang.org/packages/requests.


dub optional dependency

2017-10-28 Thread ikod via Digitalmars-d-learn

Hello,

I have dub.json with two configurations:

   "configurations": [
{
"name": "std",
"targetType": "library"
},
{
"name": "vibed",
"dependencies": {
"vibe-d": {"version": "~>0.7"}
},
"targetType": "library",
"versions": [
"vibeD"
]
}
],

So default version is "std", I can build it w/o vibe-d, but dub 
anyway fetch  vibe-d.

I'd like dub fetch vibe-d only when I build with --config vibed.

I know about "optional": true, it prevent dub to fetch vibe-d for 
"std" config, but it also prevent it from fetching for --config 
vibed.


Thanks for any help.



Re: Skynet 1M Fiber microbenchmark in D

2017-10-18 Thread ikod via Digitalmars-d-learn

On Wednesday, 18 October 2017 at 11:52:08 UTC, Biotronic wrote:

On Wednesday, 18 October 2017 at 11:34:57 UTC, Nordlöw wrote:
Another thing...how should the synchronization between the 
fibers figure out when the total number of fibers have reached 
one million?...via an atomic counter fed by reference to the 
constructor...or are there better ways? Because I do need an 
atomic reference counter here, right?


This is how I did it:
import core.thread : Fiber;

class MyFiber : Fiber {
int _depth;
ulong _index;
ulong _value;

this(int depth, ulong index) {
super();
_depth = depth;
_index = index;
}

void run() {
if (_depth == 6) { // 10^6 == 1 million, so stop here.
_value = _index;
return;
}

_value = 0;
foreach (i; 0..10) { // Line 23
auto e = new MyFiber(_depth+1, _index * 10 + i);
e.call();
_value += e._value;
}
}
}

unittest {
import std.stdio : writeln;
import std.datetime.stopwatch : StopWatch, AutoStart;
auto sw = StopWatch(AutoStart.yes);
auto a = new MyFiber(0, 0);
a.call();
sw.stop();
assert(a._value == 4950);
writeln(a._value, " after ", sw.peek);
}


And how do I parallelize this over multiple worker threads? 
AFAICT fibers are by default all spawned in the same main 
thread, right?


True. Well, they're not really spawned on any thread - they're 
allocated on the heap, have their own stack, and are run on 
whichever thread happens to invoke their call() method.


I experimented a little bit with parallelism, and the easiest 
definitely is to replace line 23 with this:


foreach (i; taskPool.parallel(10.iota, 1)) {

It seems to make very little difference in terms of run time, 
though. I tried using a mix of these approaches - parallel at 
low depth, basically just to fill up the cores, and serial 
closer to the leaves. The difference is still negligible, so I 
assume the losses are elsewhere.


--
  Biotronic


I ran this under linux perf, and here is top from 'perf report'

# Overhead  Command  Shared Object   Symbol
#   ...  ..  
...

#
 7.34%  t[kernel.kallsyms]   [k] clear_page
 6.80%  t[kernel.kallsyms]   [k] __do_page_fault
 5.39%  t[kernel.kallsyms]   [k] __lock_text_start
 3.90%  tt   [.] nothrow 
core.thread.Fiber core.thread.Fiber.__ctor(void delegate(), ulong)

 3.73%  t[kernel.kallsyms]   [k] unmap_page_range
 3.32%  t[kernel.kallsyms]   [k] flush_tlb_mm_range
 2.70%  t[kernel.kallsyms]   [k] _raw_spin_lock
 2.57%  tlibpthread-2.23.so  [.] pthread_mutex_unlock
 2.53%  tt   [.] nothrow void 
core.thread.Fiber.__dtor()


So looks like memory management, even not GC, take most of the 
time (if I interpret these numbers correctly)


Re: What is the best way to use requests and iopipe on gzipped JSON file

2017-10-17 Thread ikod via Digitalmars-d-learn

Hello, Steve

On Friday, 13 October 2017 at 22:22:54 UTC, Steven Schveighoffer 
wrote:

On 10/13/17 6:18 PM, ikod wrote:
On Friday, 13 October 2017 at 19:17:54 UTC, Steven 
Schveighoffer wrote:


Eventually, something like this will be possible with 
jsoniopipe (I need to update and release this too, it's 
probably broken with some of the changes I just put into 
iopipe). Hopefully combined with some sort of networking 
library you could process a JSON stream without reading the 
whole thing into memory.


This can be done with requests. You can ask not to load whole 
content in memory, but instead produce input range, which will 
continue to load data from server when you will  be ready to 
consume:


     auto rq = Request();
     rq.useStreaming = true;
     auto rs = rq.get("http://httpbin.org/image/jpeg;);
     auto stream = rs.receiveAsRange();
     while(!stream.empty) {
     // stream.front contain next data portion
     writefln("Received %d bytes, total received %d from 
document legth %d", stream.front.length, rq.contentReceived, 
rq.contentLength);

     stream.popFront; // continue to load from server
     }


Very nice, I will add a component to iopipe that converts a 
"chunk-like" range like this into an iopipe source, as this is 
going to be needed to interface with existing libraries. I 
still will want to skip the middle man buffer at some point 
though :)


Thanks!

-Steve


Just in order to have complete picture here - getContent returns 
not just ubyte[], but more rich structure (which can be convered 
to ubyte[] if needed). Basically it is an 
immutable(immutable(ubyte)[]) and almost all data there are just 
data received from network without any data copy.


There are more details and docs on 
https://github.com/ikod/nbuff/blob/master/source/nbuff/buffer.d. 
Main goal behind Buffer is to minimize data movement, but it also 
support many range properties, as long as some internal optimized 
methods.


Thanks,

Igor


Re: What is the best way to use requests and iopipe on gzipped JSON file

2017-10-13 Thread ikod via Digitalmars-d-learn
On Friday, 13 October 2017 at 19:17:54 UTC, Steven Schveighoffer 
wrote:

On 10/13/17 2:47 PM, Andrew Edwards wrote:
A bit of advice, please. I'm trying to parse a gzipped JSON 
file retrieved from the internet. The following naive 
implementation accomplishes the task:


 auto url = 
"http://api.syosetu.com/novelapi/api/?out=json=500=5;;

 getContent(url)
     .data
     .unzip
     .runEncoded!((input) {
     ubyte[] content;
     foreach (line; input.byLineRange!true) {
     content ~= cast(ubyte[])line;
     }
     auto json = (cast(string)content).parseJSON;


input is an iopipe of char, wchar, or dchar. There is no need 
to cast it around.


Also, there is no need to split it by line, json doesn't care.

Note also that getContent returns a complete body, but unzip 
may not be so forgiving. But there definitely isn't a reason to 
create your own buffer here.


this should work (something like this really should be in 
iopipe):


while(input.extend(0) != 0) {} // get data until EOF

And then:
auto json = input.window.parseJSON;


     foreach (size_t ndx, record; json) {
     if (ndx == 0) continue;
     auto title = json[ndx]["title"].str;
     auto author = json[ndx]["writer"].str;
     writefln("title: %s", title);
     writefln("author: %s\n", author);
     }
     });

However, I'm sure there is a much better way to accomplish 
this. Is there any way to accomplish something akin to:


 auto url = 
"http://api.syosetu.com/novelapi/api/?out=json=500=5;;

 getContent(url)
     .data
     .unzip
     .runEncoded!((input) {
     foreach (record; input.data.parseJSON[1 .. $]) {
     // use or update record as desired
     }
     });


Eventually, something like this will be possible with 
jsoniopipe (I need to update and release this too, it's 
probably broken with some of the changes I just put into 
iopipe). Hopefully combined with some sort of networking 
library you could process a JSON stream without reading the 
whole thing into memory.


This can be done with requests. You can ask not to load whole 
content in memory, but instead produce input range, which will 
continue to load data from server when you will  be ready to 
consume:


auto rq = Request();
rq.useStreaming = true;
auto rs = rq.get("http://httpbin.org/image/jpeg;);
auto stream = rs.receiveAsRange();
while(!stream.empty) {
// stream.front contain next data portion
writefln("Received %d bytes, total received %d from 
document legth %d", stream.front.length, rq.contentReceived, 
rq.contentLength);

stream.popFront; // continue to load from server
}




Right now, it works just like std.json.parseJSON: it parses an 
entire JSON message into a DOM form.


-Steve





Re: Different Output after each execution

2017-08-18 Thread ikod via Digitalmars-d-learn

On Friday, 18 August 2017 at 08:00:26 UTC, Vino.B wrote:

Hi All,

 I have written a small program to just list the directories, 
but when i run the program each time i am getting different 
output, hence request you help, below is the code


[...]


Do you expect some strict execution order when you run 'parallel' 
foreach?


Re: D outperformed by C++, what am I doing wrong?

2017-08-13 Thread ikod via Digitalmars-d-learn

On Sunday, 13 August 2017 at 08:32:50 UTC, amfvcg wrote:

Gives me

5 μs and 2 hnsecs
5000
3 secs, 228 ms, 837 μs, and 4 hnsecs
5000


And you've compiled it with?

Btw. clang for c++ version works worse than gcc (for this case 
[112ms vs 180ms]).


DMD64 D Compiler v2.074.1


Re: D outperformed by C++, what am I doing wrong?

2017-08-13 Thread ikod via Digitalmars-d-learn

On Sunday, 13 August 2017 at 08:13:56 UTC, amfvcg wrote:

On Sunday, 13 August 2017 at 08:00:53 UTC, Daniel Kozak wrote:
my second version on ldc takes 380ms and c++ version on same 
compiler (clang), takes 350ms, so it seems to be almost same




Ok, on ideone (ldc 1.1.0) it timeouts, on dpaste (ldc 0.12.0) 
it gets killed.

What version are you using?

Either way, if that'd be the case - that's slick. (and ldc 
would be the compiler of choice for real use cases).


import std.stdio : writeln;
import std.algorithm.comparison: min;
import std.algorithm.iteration: sum;
import core.time: MonoTime, Duration;
import std.range;
import std.algorithm;

auto s1(T)(T input, uint r) {
return input.chunks(r).map!sum;
}

T[] sum_subranges(T)(T[] input, uint range)
{
T[] result;
if (range == 0)
{
return result;
}
for (uint i; i < input.length; i=min(i+range, input.length))
{
result ~= sum(input[i..min(i+range, input.length)]);
}
return result;
}

unittest
{
assert(sum_subranges([1,1,1], 2) == [2, 1]);
assert(sum_subranges([1,1,1,2,3,3], 2) == [2, 3, 6]);
assert(sum_subranges([], 2) == []);
assert(sum_subranges([1], 2) == [1]);
assert(sum_subranges([1], 0) == []);
assert(s1([1,1,1], 2).array == [2, 1]);
assert(s1([1,1,1,2,3,3], 2).array == [2, 3, 6]);
}
int main()
{
int sum;
MonoTime beg0 = MonoTime.currTime;
for (int i=0; i < 100; i++)
sum += s1(iota(100),2).length;
MonoTime end0 = MonoTime.currTime;
writeln(end0-beg0);
writeln(sum);

sum = 0;
int[100] v;
for (int i=0; i < 100; ++i)
v[i] = i;
MonoTime beg = MonoTime.currTime;
for (int i=0; i < 100; i++)
sum += cast(int)sum_subranges(v,2).length;
MonoTime end = MonoTime.currTime;
writeln(end-beg);
writeln(sum);
return sum;
}

Gives me

5 μs and 2 hnsecs
5000
3 secs, 228 ms, 837 μs, and 4 hnsecs
5000



Re: lambda function with "capture by value"

2017-08-05 Thread ikod via Digitalmars-d-learn

On Saturday, 5 August 2017 at 18:45:34 UTC, Simon Bürger wrote:

On Saturday, 5 August 2017 at 18:22:38 UTC, Stefan Koch wrote:

[...]


No, sometimes I want i to be the value it has at the time the 
delegate was defined. My actual usecase was more like this:


void delegate()[3] dgs;
for(int i = 0; i < 3; ++i)
dgs[i] = (){writefln("%s", i); };


And I want three different delegates, not three times the same. 
I tried the following:


void delegate()[3] dgs;
for(int i = 0; i < 3; ++i)
{
int j = i;
dgs[i] = (){writefln("%s", j); };
}

I thought that 'j' should be considered a new variable each 
time around, but sadly it doesn't work.


Maybe std.functional.partial can help you.


Re: Having a strange issue with std.net.curl.HTTP as a struct dependency

2017-07-12 Thread ikod via Digitalmars-d-learn

On Wednesday, 12 July 2017 at 08:24:12 UTC, NoBigDeal256 wrote:

On Monday, 10 July 2017 at 20:55:19 UTC, ketmar wrote:

NoBigDeal256 wrote:

Do you happen to have a link to the bug report for this 
specific issue that I could look at? I looked at the known 
bugs list and couldn't find anything related to this.


nope. it is a kind of "known bug nobody bothered to report". 
;-)


Okay, so I decided to ditch curl and switch over to 
dlang-requests, which doesn't depend on curl, and I'm running 
into this same issue again... I don't think this is a curl 
issue.


looks like circular dependency between thingA and thingB lead to 
problems on exit. this code fails on osx too.


Re: How to add authentificaion method to request?

2017-07-11 Thread ikod via Digitalmars-d-learn

On Tuesday, 11 July 2017 at 12:15:39 UTC, Suliman wrote:
I am using dlang-requests. I need authentificate on 
https://scihub.copernicus.eu/dhus/login and than do some 
data-parsing.


MultipartForm form;
form.add(formData("login_username", "Suliman"));
form.add(formData("login_password", "123")); // changed
auto content = 
postContent("https://scihub.copernicus.eu/dhus/login;, form);

writeln("Output:");
writeln(content);

Return error about login pass. So it's seems that I need to add 
BasicAuthentication method type. But how to add it?



Browser console show next: https://snag.gy/VXaq2R.jpg


Hello,
Yo have to use Request object if you need to configure basic 
authentication.


MultipartForm form;
Request request;
request.authenticator = new BasicAuthentication("user", 
"passwd");

form.add(formData("login_username", "Suliman"));
form.add(formData("login_password", "123")); // changed
auto response = 
request.post("https://scihub.copernicus.eu/dhus/login;, form);

auto content = response.responseBody;
writeln("Output:");
writeln(content);



Re: 'Access Violation Error' with parallel-foreach loop

2017-03-19 Thread ikod via Digitalmars-d-learn

On Sunday, 19 March 2017 at 00:46:29 UTC, ooyu wrote:
On Saturday, 18 March 2017 at 22:27:27 UTC, Vladimir Panteleev 
wrote:

[...]


Aha! Thank you.
I try again using std.file.write and input ubyte[] data.


auto rq = Request();
auto d = rq.get( std.uri.encode(web_url) );
std.file.write(path_hash[web_url], d.responseBody.data);

working pretty :-)


It would be nice to know what actually solved problem.


Re: 'Access Violation Error' with parallel-foreach loop

2017-03-18 Thread ikod via Digitalmars-d-learn

On Saturday, 18 March 2017 at 16:15:30 UTC, ooyu wrote:

When i was uing parallel-foreach loop, got this error:


object.Error@(0): Access Violation

0x0056CCB8 in _flushbu
0x005663C7 in fwrite
0x0041D39F in nothrow @nogc @trusted uint 
std.stdio.trustedFwrite!(char).trustedFwrite(shared(core.stdc.stdio._iobuf)*, const(char[]))

at C:\D\dmd2\windows\bin\..\..\src\phobos\std\stdio.d(3836)
0x004611AA in D3std6format99__T14formattedWri at 
C:\D\dmd2\windows\bin\..\..\src\phobos\std\format.d(556)



code is here:

foreach( web_url; parallel(urls) ){
try{
auto rq = Request(); // using dlang-requests library
auto d = rq.get( std.uri.encode(web_url) ); // file url

File f = File(path_hash[web_url], "wb");
f.write( d.responseBody );
f.close();
}
catch(std.exception.ErrnoException e){}
}


I don't understand why got Access Violation error. :-(



Hello,
Also don't understand the reason and I can't reproduce this 
error. Can you try to pass ubyte[] to f.write() explicitly 
converting responseBody to array? Like


f.write(d.responseBody.data());




Re: Date formatting in D

2017-03-07 Thread ikod via Digitalmars-d-learn

On Tuesday, 7 March 2017 at 20:29:07 UTC, aberba wrote:
I've been trying to figure out an inbuilt functionality in 
phobos for formatting date. In my use case, I've been trying to 
format current Unix timestamp to something like "Thu, 08 Mar 
2017 12:00:00 GMT".


How do I go by this easily (Currently, long concatenation of 
strings is what I'm thinking)?


I saw this thread[1] from 2011 on similar issue. Has it been 
resolved? What is the state of toCustomString?


[1] 
http://forum.dlang.org/post/mailman.673.1320367887.24802.digitalmars-d-le...@puremagic.com


Straightforward solution:

https://dlang.org/library/core/stdc/time/strftime.html
http://man7.org/linux/man-pages/man3/strftime.3.html

Maybe there is something better.


Re: foreach for string[string]AA

2017-02-28 Thread ikod via Digitalmars-d-learn
On Tuesday, 28 February 2017 at 15:15:00 UTC, Anton Pastukhov 
wrote:

I can't see the logic in AA foreach order. Consider this code:

```
void main() {
string[string] test = [
"one": "1",
"two": "2",
"three": "3",
"four": "4"
];

import std.stdio:writeln;

foreach(k, v; test) {
writeln(k);
}
}

Output:
three
two
one
four

I was sure output should be
one
two
three
four


AA implemented as hash table, so it doesn't preserve insertion 
order. You have to sort keys when you need:


import std.algorithm;
import std.stdio;
void main() {
auto aa = ["one":1,
   "two":2
   ];
foreach(k; sort(aa.keys)) {
writeln(k);
}
}


Re: trick to make throwing method @nogc

2017-02-25 Thread ikod via Digitalmars-d-learn
On Saturday, 25 February 2017 at 20:49:51 UTC, Adam D. Ruppe 
wrote:
A wrapper that unifies these 4 steps like enforce is pretty 
easy to implement.


yeah easy to use exception in @nogc as long as the catch knows 
to free it too.


Alas, not my case. Exception can be catched not in my code.


Re: trick to make throwing method @nogc

2017-02-25 Thread ikod via Digitalmars-d-learn

On Saturday, 25 February 2017 at 19:59:29 UTC, ikod wrote:

Hello,

I have a method for range:

struct Range {
immutable(ubyte[]) _buffer;
size_t _pos;

@property void popFront() pure @safe {
enforce(_pos < _buffer.length, "popFront from empty 
buffer");

_pos++;
}
}

I'd like to have @nogc here, but I can't because enforce() is 
non-@nogc.
I have a trick but not sure if it is valid, especially I don't 
know if optimization will preserve code, used for throwing:


import std.string;

struct Range {
immutable(ubyte[]) _buffer;
size_t  _pos;

this(immutable(ubyte[]) s) {
_buffer = s;
}
@property void popFront() pure @safe @nogc {
if (_pos >= _buffer.length ) {
auto _ = _buffer[$]; // throws RangeError
}
_pos++;
}
}

void main() {
auto r = Range("1".representation);
r.popFront();
r.popFront(); // throws
}

Is it ok to use it? Is there any better solution?

Thanks!


Found that I can use

@property void popFront() pure @safe @nogc {
if (_pos >= _buffer.length ) {
assert(0, "popFront for empty range");
}
_pos++;
}

which is both descriptive and can't be optimized out.



trick to make throwing method @nogc

2017-02-25 Thread ikod via Digitalmars-d-learn

Hello,

I have a method for range:

struct Range {
immutable(ubyte[]) _buffer;
size_t _pos;

@property void popFront() pure @safe {
enforce(_pos < _buffer.length, "popFront from empty 
buffer");

_pos++;
}
}

I'd like to have @nogc here, but I can't because enforce() is 
non-@nogc.
I have a trick but not sure if it is valid, especially I don't 
know if optimization will preserve code, used for throwing:


import std.string;

struct Range {
immutable(ubyte[]) _buffer;
size_t  _pos;

this(immutable(ubyte[]) s) {
_buffer = s;
}
@property void popFront() pure @safe @nogc {
if (_pos >= _buffer.length ) {
auto _ = _buffer[$]; // throws RangeError
}
_pos++;
}
}

void main() {
auto r = Range("1".representation);
r.popFront();
r.popFront(); // throws
}

Is it ok to use it? Is there any better solution?

Thanks!



vibed ssl stream and SSL_CTX_set_default_verify_paths

2016-11-15 Thread ikod via Digitalmars-d-learn

Hello,

Sorry if this is FAQ, or any other way stupid question, e.t.c.

I have to configure vibe.d tlsstream to verify remote certificate.

Please correct me if I'm wrong -- here is part of my code to 
request certificate verification:


auto sslctx = createTLSContext(TLSContextKind.client);

sslctx.useTrustedCertificateFile("/opt/local/etc/openssl/cert.pem");

sslctx.peerValidationMode = TLSPeerValidationMode.trustedCert;
auto _stream = createTLSStream(_conn, sslctx, host);

the problem here is call to useTrustedCertificateFile. At compile 
time I do not know place of cert authority file, and this 
location can also be unknown for program user even if there is a 
way to configure it during program start.


libopenssl provide call SSL_CTX_set_default_verify_paths(ctx) - 
which configure default (already known to library code) location 
of ca certs distributed with openssl.


Is there any way for vibed sslctx to configure CA cert path "by 
default value"?


Thanks for your responce!



Re: Classes and Structs, Memory management questions

2016-09-02 Thread ikod via Digitalmars-d-learn

On Friday, 2 September 2016 at 08:43:45 UTC, dom wrote:


Since garbage collection is a very nice feature that I wouldn't 
wanna miss for certain scenarios I think D should give us the 
opportunity to determine how an object is allocated. In the 
example above putting it on the stack is probably a good idea. 
Having a self managed reference to the heap can be good too if 
manual memory management is wanted. Or of course let the GC 
manage it ( i love it for prototyping code and also as a D 
beginner it is beneficial that i just dont need to care about 
memory management).


Could somebody explain to me if this is seen as a problem 
why/whynot and how I should address that kind of issues in my 
code?


You can allocate class instance on stack:

https://dlang.org/phobos/std_typecons.html#.scoped



Re: new XML and JSON libs and replacement of std.net.curl

2016-08-16 Thread ikod via Digitalmars-d-learn

On Tuesday, 16 August 2016 at 09:16:40 UTC, yawniek wrote:

imo things should be modularized.

so there should be (fast) protocol parsers first, something like

https://github.com/h2o/picohttpparser or
https://github.com/seanmonstar/httparse

then a very simple eventloop that has abstractions and range 
based interfaces for reading/writing data into different types 
of sockets.


There is common http message parser that used in nginx and 
nodejs. I think it can be ported from C to D. Both sync and async 
code can be built around this parser. But async library accepted 
as standard need before this.




Re: new XML and JSON libs and replacement of std.net.curl

2016-08-15 Thread ikod via Digitalmars-d-learn

On Monday, 15 August 2016 at 15:01:13 UTC, Oleg B wrote:

Hello.

As replacement of std.net.curl I found 
https://github.com/ikod/dlang-requests. Who know's about this 
lib? Is this good replacement of std.net.curl?


Maybe if I need json and http requests I must fully use vibe 
for these things? Vibe not resolve xml question =(

I want to use minimal count of dependencies...


Hello,

Can you use dlang-requests instead of curl, or not, depends on 
your requirements. If you find any bugs, you can post bug report 
to project, usually I react quickly. dlang-requests depend only 
on libssl/libcrypt.


Regards,

Igor


Re: std.range pipelike interface, inverting OutputStreams?

2016-08-13 Thread ikod via Digitalmars-d-learn

On Saturday, 13 August 2016 at 02:15:55 UTC, cy wrote:
I was trying to use std.regex, and it takes an output stream to 
pump the result to, which is great, but I wanted to perform TWO 
replacements, one with the output of the previous, with data 
that might be a tricky size to cache redundantly.


So, ideally when you do something like regexp replacing, you'd 
have a function like:
InputRange replaceThingy(InputRange source, someregex, 
somereplacer);


It doesn't do that, unfortunately, so I can't easily chain 
replacements. But couldn't I do a thing like...


InputRange pipe(void delegate(ref OutputRange)) { ? }

and then just...
  auto result = pipe((sink) => replaceAllInto(sink, data, 
firstmatch, firstreplacer));
  auto result2 = pipe((sink) => replaceAllInto(sink, result, 
secondmatch, secondreplacer));

  copy(result2,stdout);

... or something like that? Has that been done before? Can it 
be done?


I'm pretty sure any algorithm that takes an output stream can 
be transformed into an algorithm that provides an input stream, 
but I'm not sure if I'd be reinventing the wheel here, or if my 
assumptions are seriously off.


Hoping to avoid weird stuff like context switches... eh.


Looks like you can't just because std.regex replace* functions 
accept only someString as input.


But you can write something like this (sorry for dirty code):
import std.regex;
import std.stdio;

class ReplacePipe {
private {
Regex!char   _re;
string   _format;
string   _input;
ReplacePipe  _next;
}
this(string input, string r, string f) {
_re = regex(r);
_format = f;
_input = input;
}
this(ReplacePipe next, string r, string f) {
_re = regex(r);
_format = f;
_next = next;
}
string go(string i) {
return replaceAll(i, _re, _format);
}
string go() {
if ( _next ) {
auto s = _next.go();
return replaceAll(s, _re, _format);;
}
return go(_input);
}
}
ReplacePipe replacePipe(string data, string r, string c) {
return new ReplacePipe(data, r, c);
}
ReplacePipe replacePipe(ReplacePipe output, string r, string c) {
return new ReplacePipe(output, r, c);
}
void main() {
string input = "1 2, 3 4";
auto a = input.replacePipe(r" ", "+").replacePipe(r"\+", 
".").go();

assert(a=="1.2,.3.4");
}



docs generation for mixins

2016-05-15 Thread ikod via Digitalmars-d-learn

Hello,

sorry if this question was asked before...

How can I get docs generated for int z?

-
string V(string var) {
return "int " ~ var ~ ";";
}
///
struct X {
/// comment for y
int y;
/// comment for z
mixin(V("z"));
}

void main()
{
}
-

"dmd -D -Dddocs" generate docs for "y" only.

Thanks


Re: What reasons are known a thread stops suddenly?

2016-02-04 Thread ikod via Digitalmars-d-learn

On Thursday, 4 February 2016 at 20:25:27 UTC, tcak wrote:

Is there any known reason for a thread to suddenly stop like 
this?


Your listener object can be GC-ed. Check if you have any live ref 
to it.


attributes and properties lookup order

2016-01-26 Thread ikod via Digitalmars-d-learn

Hello

$ cat t.d

import std.net.curl: get;

void main() {
string[string] x;
string y = x.get("","");
}

$ dmd t.d
t.d(6): Error: template std.net.curl.get cannot deduce function 
from argument types !()(string[string], string, string), 
candidates are:
/usr/include/dmd/phobos/std/net/curl.d(527):
std.net.curl.get(Conn = AutoProtocol, T = char)(const(char)[] 
url, Conn conn = Conn()) if (isCurlConn!Conn && (is(T == char) || 
is(T == ubyte)))


It is strange that AA property 'get' is not considered first.