Re: Request help on allocator.

2023-12-02 Thread IGotD- via Digitalmars-d-learn

On Saturday, 2 December 2023 at 19:13:18 UTC, Vino B wrote:

Hi All,

  Request your help in understanding the below program, with 
the below program I can allocate 8589934592(8GB) it prints the 
length 8589934592(8GB) where as my laptop has only 4 GB so the 
confusion is that how can this program allocate 8GB RAM when I 
have only 4GB of RAM installed






From,
Vino


Welcome to the wonderful world of virtual memory.

Virtual memory size != physical memory size

This nothing to do with D programming language but how the OS 
manage the memory.




Re: Request help on allocator.

2023-12-02 Thread ryuukk_ via Digitalmars-d-learn

On Saturday, 2 December 2023 at 19:13:18 UTC, Vino B wrote:

Hi All,

  Request your help in understanding the below program, with 
the below program I can allocate 8589934592(8GB) it prints the 
length 8589934592(8GB) where as my laptop has only 4 GB so the 
confusion is that how can this program allocate 8GB RAM when I 
have only 4GB of RAM installed



```
import std.algorithm.comparison : max;
import 
std.experimental.allocator.building_blocks.allocator_list : 
AllocatorList;

import std.experimental.allocator.building_blocks.bucketizer;
import std.experimental.allocator.building_blocks.free_list : 
FreeList;
import std.experimental.allocator.building_blocks.region : 
Region;

import std.experimental.allocator.building_blocks.segregator;
import std.experimental.allocator.common : unbounded;
import std.experimental.allocator.mallocator : Mallocator;
import std.stdio: writeln;

void main () {
alias FList = FreeList!(Mallocator, 0, unbounded);
alias A = Segregator!(
  8, FreeList!(Mallocator, 0, 8),
  128, Bucketizer!(FList, 1, 128, 16),
  256, Bucketizer!(FList, 129, 256, 32),
  512, Bucketizer!(FList, 257, 512, 64),
  1024, Bucketizer!(FList, 513, 1024, 128),
  2048, Bucketizer!(FList, 1025, 2048, 256),
  3584, Bucketizer!(FList, 2049, 3584, 512),
  4097 * 1024, AllocatorList!(n => Region!Mallocator(max(n, 
1024 * 4096))),

  Mallocator
);
A tuMalloc;
auto c = tuMalloc.allocate(8589934592);  // 8GB
writeln(c.length);   // output: 8589934592
tuMalloc.deallocate(c);
}
```

From,
Vino


This is normal behavior on linux, it's called overcommit memory, 
i think it was meant to allow things like fork() to work properly


https://stackoverflow.com/a/7504354


Request help on allocator.

2023-12-02 Thread Vino B via Digitalmars-d-learn

Hi All,

  Request your help in understanding the below program, with the 
below program I can allocate 8589934592(8GB) it prints the length 
8589934592(8GB) where as my laptop has only 4 GB so the confusion 
is that how can this program allocate 8GB RAM when I have only 
4GB of RAM installed



```
import std.algorithm.comparison : max;
import std.experimental.allocator.building_blocks.allocator_list 
: AllocatorList;

import std.experimental.allocator.building_blocks.bucketizer;
import std.experimental.allocator.building_blocks.free_list : 
FreeList;

import std.experimental.allocator.building_blocks.region : Region;
import std.experimental.allocator.building_blocks.segregator;
import std.experimental.allocator.common : unbounded;
import std.experimental.allocator.mallocator : Mallocator;
import std.stdio: writeln;

void main () {
alias FList = FreeList!(Mallocator, 0, unbounded);
alias A = Segregator!(
  8, FreeList!(Mallocator, 0, 8),
  128, Bucketizer!(FList, 1, 128, 16),
  256, Bucketizer!(FList, 129, 256, 32),
  512, Bucketizer!(FList, 257, 512, 64),
  1024, Bucketizer!(FList, 513, 1024, 128),
  2048, Bucketizer!(FList, 1025, 2048, 256),
  3584, Bucketizer!(FList, 2049, 3584, 512),
  4097 * 1024, AllocatorList!(n => Region!Mallocator(max(n, 
1024 * 4096))),

  Mallocator
);
A tuMalloc;
auto c = tuMalloc.allocate(8589934592);  // 8GB
writeln(c.length);   // output: 8589934592
tuMalloc.deallocate(c);
}
```

From,
Vino


Re: How to hash SHA256 from string?

2023-12-02 Thread Sergey via Digitalmars-d-learn

On Saturday, 2 December 2023 at 15:30:39 UTC, zoujiaqing wrote:

SHA

Sorry for OT, but don’t know different place to reach you out.
What is the status of Archttp? Is it discontinued/abandoned?


Re: How to hash SHA256 from string?

2023-12-02 Thread An Pham via Digitalmars-d-learn

On Saturday, 2 December 2023 at 15:30:39 UTC, zoujiaqing wrote:

```D
string appKey = 
"1";

ubyte[1024] data = cast(ubyte[])(appKey.dup[0..$]);
sha256.put(data);


Your data has garbage at the end; try 
sha256.put(data[0..appKey.length])


Re: How to hash SHA256 from string?

2023-12-02 Thread user1234 via Digitalmars-d-learn

On Saturday, 2 December 2023 at 16:17:08 UTC, user1234 wrote:

On Saturday, 2 December 2023 at 15:30:39 UTC, zoujiaqing wrote:

[...]


sign is binary, you have to use the toHexString utility :

```d
import std.stdio;
import std.digest.sha;

void main()
{

SHA256 sha256;
sha256.start();
string appKey = 
"1";

sha256.put(cast(ubyte[])appKey);
ubyte[32] sign = sha256.finish();
writeln("sign: %s", toHexString(sign));
}
```

also you add a range error on data assignment.


and a last error I have not initially catch, use writefln:

```d
writefln("sign: %s", toHexString(sign));
```


Re: Inversion of conditional compilation statements

2023-12-02 Thread ryuukk_ via Digitalmars-d-learn
On Saturday, 2 December 2023 at 15:48:02 UTC, Nick Treleaven 
wrote:

On Saturday, 2 December 2023 at 15:03:25 UTC, ryuukk_ wrote:
I wish we could use ``version`` as expression, to void the 
repetition:


```D
import std.stdio;

enum HasTest = version (Test) ? true : false;


Tomek Sowiński wrote this template:
```d
enum bool isVersion(string ver) = !is(typeof({
  mixin("version(" ~ ver ~ ") static assert(0);");
}));

static assert(isVersion!"assert");
static assert(!isVersion!"Broken");
```


I have something similar that i found somewhere here

```D

struct Version
{
static bool opDispatch(string identifier)()
{
mixin("
version(", identifier, ")
return true;
else
return false;
");
}
}


static if (Version.something == false)
{}
```

But i don't even use it since it requires me to import a module, 
it not worth it, i'd rather have version as expression


Re: How to hash SHA256 from string?

2023-12-02 Thread user1234 via Digitalmars-d-learn

On Saturday, 2 December 2023 at 15:30:39 UTC, zoujiaqing wrote:

```D
import std.stdio;
import std.digest.sha;

void main()
{

SHA256 sha256;
sha256.start();
string appKey = 
"1";

ubyte[1024] data = cast(ubyte[])(appKey.dup[0..$]);
sha256.put(data);
ubyte[32] sign = sha256.finish();

string sign1 = cast(string) sign[0..$];
writeln("sign: %s", sign1);
}
```

The result varies when you run the code repeatedly and the 
display is garbled:

```
zoujiaqing@mac test % ./test
Getui access sign: %s>tM?a?j,???ߥm?8l~??uzU?|9?~ˡ
zoujiaqing@mac test % ./test
Getui access sign: %s1-??U?
?d<3^3??נ? ??P%u/Iv
zoujiaqing@mac test % ./test
Getui access sign: %s1?ϻN?ށ?`O?p!?O?4U
:8J~%ʬ
zoujiaqing@mac test % ./test
Getui access sign: %s??k#O?;?ڋ?5T?"=??;???e
```


sign is binary, you have to use the toHexString utility :

```d
import std.stdio;
import std.digest.sha;

void main()
{

SHA256 sha256;
sha256.start();
string appKey = 
"1";

sha256.put(cast(ubyte[])appKey);
ubyte[32] sign = sha256.finish();
writeln("sign: %s", toHexString(sign));
}
```

also you add a range error on data assignment.


Re: Inversion of conditional compilation statements

2023-12-02 Thread Nick Treleaven via Digitalmars-d-learn

On Saturday, 2 December 2023 at 15:03:25 UTC, ryuukk_ wrote:
I wish we could use ``version`` as expression, to void the 
repetition:


```D
import std.stdio;

enum HasTest = version (Test) ? true : false;


Tomek Sowiński wrote this template:
```d
enum bool isVersion(string ver) = !is(typeof({
  mixin("version(" ~ ver ~ ") static assert(0);");
}));

static assert(isVersion!"assert");
static assert(!isVersion!"Broken");
```


How to hash SHA256 from string?

2023-12-02 Thread zoujiaqing via Digitalmars-d-learn

```D
import std.stdio;
import std.digest.sha;

void main()
{

SHA256 sha256;
sha256.start();
string appKey = 
"1";

ubyte[1024] data = cast(ubyte[])(appKey.dup[0..$]);
sha256.put(data);
ubyte[32] sign = sha256.finish();

string sign1 = cast(string) sign[0..$];
writeln("sign: %s", sign1);
}
```

The result varies when you run the code repeatedly and the 
display is garbled:

```
zoujiaqing@mac test % ./test
Getui access sign: %s>tM?a?j,???ߥm?8l~??uzU?|9?~ˡ
zoujiaqing@mac test % ./test
Getui access sign: %s1-??U?
?d<3^3??נ? ??P%u/Iv
zoujiaqing@mac test % ./test
Getui access sign: %s1?ϻN?ށ?`O?p!?O?4U
:8J~%ʬ
zoujiaqing@mac test % ./test
Getui access sign: %s??k#O?;?ڋ?5T?"=??;???e
```


Re: Inversion of conditional compilation statements

2023-12-02 Thread ryuukk_ via Digitalmars-d-learn
On Saturday, 2 December 2023 at 13:16:26 UTC, Johannes 
Miesenhardt wrote:

Hello,
I am trying to learn D and I have stumbled upon an issue
Consider this code:
```d
import std.stdio;

//version = Test;

int main() {
version (Test) {
writeln("Hello, world!");
}
return 0;
}
```

This compiles, however what if we want to turn the version 
statement around?
I first expected `version (!Test)` to work, but it doesn't 
since the grammar says:

```
VersionCondition:
version ( Identifier )
version ( unittest )
version ( assert )
```

We are using the first way, the one with the Identifier.
The reason inverting works with if-statements is because they 
take an "Expression".


I see the way why it doesn't work, but I think it should. 
Considering that

`version (Test) {} else {`
works without any issue but looks very ugly.

Can somebody explain if this is an intended decision or what I 
should do instead of using my ugly replacement?


It depends what do you want to achieve, if you just want a bool 
at the top of your module to toggle features, then you can do 
this:


```D
import std.stdio;

enum Test = true;

int main() {
static if (Test == false) {
writeln("this will never be called, it won't be 
compiled");

} else {
writeln("hello!");
}
return 0;
}
```

``static if`` is evaluated at compile time, you can mix it with 
``version`` if you need to provide the flag during compilation


```D
import std.stdio;

version (Test)
enum HasTest = true;
else
enum HasTest = false;

int main() {
static if (HasTest == false) {
writeln("this will never be called, it won't be 
compiled");

} else {
writeln("hello!");
}
return 0;
}
```


I wish we could use ``version`` as expression, to void the 
repetition:


```D
import std.stdio;

enum HasTest = version (Test) ? true : false;

int main() {
static if (HasTest == false) {
writeln("this will never be called, it won't be 
compiled");

} else {
writeln("hello!");
}
return 0;
}
```

If someone able to do it, would be cool to see a PR


Re: Advent of Code 2023

2023-12-02 Thread Sergey via Digitalmars-d-learn
On Saturday, 2 December 2023 at 13:33:33 UTC, Johannes 
Miesenhardt wrote:


Day 1 solution here, since I swap them out based on a runtime 
argument.


In the Discord server we also have a topic about AoC2023. So feel 
free to join it as well.


Some other solutions that could be worth to check:

https://github.com/andrewlalis/AdventOfCode2023/blob/main/day_1/solution.d
https://github.com/schveiguy/adventofcode/blob/master/2023/day1/trebuchet.d


Re: Inversion of conditional compilation statements

2023-12-02 Thread user1234 via Digitalmars-d-learn
On Saturday, 2 December 2023 at 13:16:26 UTC, Johannes 
Miesenhardt wrote:

Hello,

[...]

I see the way why it doesn't work, but I think it should. 
Considering that

`version (Test) {} else {`
works without any issue but looks very ugly.

Can somebody explain if this is an intended decision or what I 
should do instead of using my ugly replacement?


It's an intended decision that's often debated, a.k.a "version 
algebra". The official position on version algebra is that 
complex conditions (think `&&` and `||`) would be a source of 
bugs.


Re: Advent of Code 2023

2023-12-02 Thread Johannes Miesenhardt via Digitalmars-d-learn
On Friday, 1 December 2023 at 01:01:31 UTC, Siarhei Siamashka 
wrote:
Advent of Code 2023 starts in a few hours from now. I suggest 
to discuss D language solutions here.
But to avoid spoilers, it's best to do this with a 24h delay 
after each puzzle is published.


Day 1 solution

```d
version = Part2;

import std.stdio;
import std.algorithm;
import std.array;
import std.format;
import std.conv;
import std.string;

int[string] numberMap;

static this() {
numberMap = [
"one": 1,
"two": 2,
"three": 3,
"four": 4,
"five": 5,
"six": 6,
"seven": 7,
"eight": 8,
"nine": 9
];
}

int findNum(T)(T /*char[] and string; since example and file read 
are different*/ str, bool reverse) {
	for(size_t i = reverse ? str.length - 1: 0; reverse ? i >= 0 : i 
< str.length; i += (reverse ? -1 : 1)) {

auto c = str[i];
if(c >= '0' && c <= '9')
return to!int(c - '0');
version(Part2) {
foreach(key, value; numberMap) {
if(key.length > str.length - i)
continue;
if(str[i..i+key.length] == key) {
return value;
}
}
}
}
writeln(str, " ", reverse);
assert(false);
}

int main() {
File("input")
.byLine
/*[
"two1nine",
"eightwothree",
"abcone2threexyz",
"xtwone3four",
"4nineeightseven2",
"zoneight234",
"7pqrstsixteen"
]*/
.map!((str) {
auto firstNum = findNum(str, false);
auto secNum = findNum(str, true);
auto code = firstNum * 10 + secNum;
return code;
})
.sum
.writeln;
return 0;
}
```

I am a bloody beginner so if there are any things that are very 
wrong with this please point them out.
The fact that I need a template for accepting both a string and a 
char[] is very weird but I went with it. I am also curious if 
there is a better way for the reversible for-loop to happen. I 
saw foreach and foreach_reverse but I don't think that helps me 
here, since I swap them out based on a runtime argument.


Inversion of conditional compilation statements

2023-12-02 Thread Johannes Miesenhardt via Digitalmars-d-learn

Hello,
I am trying to learn D and I have stumbled upon an issue
Consider this code:
```d
import std.stdio;

//version = Test;

int main() {
version (Test) {
writeln("Hello, world!");
}
return 0;
}
```

This compiles, however what if we want to turn the version 
statement around?
I first expected `version (!Test)` to work, but it doesn't since 
the grammar says:

```
VersionCondition:
version ( Identifier )
version ( unittest )
version ( assert )
```

We are using the first way, the one with the Identifier.
The reason inverting works with if-statements is because they 
take an "Expression".


I see the way why it doesn't work, but I think it should. 
Considering that

`version (Test) {} else {`
works without any issue but looks very ugly.

Can somebody explain if this is an intended decision or what I 
should do instead of using my ugly replacement?


Re: vibe-d REST API: POSTing a struct

2023-12-02 Thread bomat via Digitalmars-d-learn

On Friday, 1 December 2023 at 19:18:19 UTC, bomat wrote:
So my question is, is it possible to have vibe-d parse the 
request body into a struct "implicitly"?


I'm gonna answer my own question here, it's `@bodyParam`.
https://forum.rejectedsoftware.com/groups/rejectedsoftware.vibed/thread/40001/
https://github.com/vibe-d/vibe.d/pull/1676

I wasn't aware of the dedicated vibe-d forum before. As I've seen 
some vibe-d related questions in this forum as well, I thought 
this would be the place to ask...


Re: ImportC: Windows.h

2023-12-02 Thread name via Digitalmars-d-learn

On Saturday, 2 December 2023 at 08:36:01 UTC, Stefan Koch wrote:

On Saturday, 2 December 2023 at 05:16:43 UTC, name wrote:

Minimum thing to reproduce bug:
[...]


It doesn't show up since it's defined as an Identifier 
Expression which cannot be resolved.


But why can't it be resolved?


Re: ImportC: Windows.h

2023-12-02 Thread Stefan Koch via Digitalmars-d-learn

On Saturday, 2 December 2023 at 05:16:43 UTC, name wrote:

Minimum thing to reproduce bug:

main.d:
```d
import test;

void main() {
  auto a = FILE_MAP_READ;
}
```

test.c
```c
#define SECTION_MAP_READ  0x0004
#define FILE_MAP_READ SECTION_MAP_READ
```

build with 
```"D:\dmd.2.105.3.windows\dmd2\windows\bin64\dmd.exe" -c 
test.c -vcg-ast```.


test.c.cg (```FILE_MAP_READ``` doesn't show up):
```d
extern (C)
{
enum int __IMPORTC__ = 1;
enum int _M_X64 = 100;
enum int _MSC_EXTENSIONS = 1;
enum int _MSC_BUILD = 0;
enum int _WIN64 = 1;
// ...
enum int SECTION_MAP_READ = 4;
// ...
}
```


It doesn't show up since it's defined as an Identifier Expression 
which cannot be resolved.