Re: How to unpack template parameters,Or how to forward parameters?

2022-05-05 Thread vit via Digitalmars-d-learn

On Friday, 6 May 2022 at 00:41:18 UTC, zjh wrote:

Hello everyone,I have following function:
```d
import core.stdc.stdio;
void f(int i,int j){
printf("%i,%i",i,j);
}
```
I want to forward `int[N]` to `(int i,int j)` etc. How can I 
write a forwarding function?

```d
void ff(alias g,int...I)(int[2]k){
g(k[I]);
}//How to unpack template variable parameters?

void f(int[2]k){
ff!(f,0,1)(k);//how to forward?
}

```

main function:
```d
extern(C):void main()
{
int[2] a=[2,4];
f(a);//calling like this
}
```


Try this:

```d
import core.stdc.stdio;

void f(int i,int j){
printf("%i,%i",i,j);
}


import std.range :  iota;
import std.traits :  isStaticArray;
import std.meta :  AliasSeq;


template expand(alias arr)
if(isStaticArray!(typeof(arr))){
auto get(size_t I)(){
return arr[I];
}

alias Result = AliasSeq!();

static foreach(I; iota(0, arr.length))
Result = AliasSeq!(Result, get!I);

alias expand = Result;
}


extern(C):void main(){
int[2] a=[2,4];
f(expand!a);
}
```


Including C sources in a DUB project

2022-05-05 Thread Alexander Zhirov via Digitalmars-d-learn
I'm sure there is such a topic on the forum, but after scrolling 
through the search, I didn't find anything. The bottom line is 
that I want to enable compilation of C sources in DUB and then 
build the project with the connection of libraries. I would like 
to see an example of such a `dub.json` project.


Enable libraries (flags) `-lm`, `-lX11`, `-lXrandr`.


```sh
{
"name" : "test",
"description" : "Test project",
"dependencies" : {
}
}
```

It turns out to compile everything manually, but I would like to 
do it all through the dub project.



```sh
.
├── dub.json
└── src
├── app.d
└── main.c
```


Why isn't my dynamic array method doesn't work for this type?

2022-05-05 Thread rempas via Digitalmars-d-learn
I have created a structure that is a actually an array that 
allocates memory and growths. It is a template and it has worked 
with a couple of types that I have tried with. It doesn't work 
with one tho and I cannot understand why. I will list the 
smallest possible code I could. Keep in mind that it is a couple 
of code. For anyone that wants to read it, keep in mind that I 
have to make this code work with LDC and using "-betterC".


// Filename: "test_file"
```aneluhaein```

// Filename: "my_file.d"
```d
import vec;

struct My_File {
  char tok;
  const char* contents;
  ulong size, ln, cn, pos, identation;

  ref My_File opAssign(ref const My_File s) return { return this; 
}


  // Copy constructor
  this(ref return scope My_File rhs) {
this.tok = rhs.tok;
this.contents = rhs.contents;
this.ln = rhs.ln;
this.cn = rhs.cn;
this.pos = rhs.pos;
this.identation = rhs.identation;
  }

  debug {
void print() {
  printf("
  this.tok = %c
  this.contents = \n\n`\n%s`
  this.ln = %lu
  this.cn = %lu
  this.pos = %lu
  this.identation = %lu",
this.tok,
this.contents,
this.ln,
this.cn,
this.pos,
this.identation,
  );
}
  }
}
```
// Filename: "vec.d"
```d
public import core.stdc.stdlib;
public import core.stdc.stdio;
public import core.sys.posix.fcntl;
public import core.sys.linux.sys.mman;

public enum DEF_VEC_REALLOC_SIZE = 3;
public enum DEF_VEC_ALLOC_SIZE = 3;

void exit_prog(int code) {
  exit(0);
}

struct Vector(T) {
  T* ptr;  // The pointer to the data
  ulong capacity;  // Total amount of objects we can store
  ulong length;// The amount of the objects we have stored

  this(ulong capacity) { // Only used to create the vector that 
will hold the files

this.length = 0;
this.capacity = capacity;
this.ptr = cast(T*)malloc(T.sizeof * capacity);

if (!this.ptr) { // Allocation error check
  fprintf(stderr, "Error: Could not allocate memory for the 
*%s object! Exiting...\n",

  cast(char*)(T).stringof);
  exit_prog(1);
}
  }

  void add(T element) {
if (this.capacity > this.length) {
  this.ptr[this.length++] = element;
}

else {
  this.capacity += DEF_VEC_REALLOC_SIZE;
  realloc(this.ptr, T.sizeof * DEF_VEC_REALLOC_SIZE);

  if (!this.ptr) { // Re-allocation error check
fprintf(stderr, "Error: Could not allocate more memory 
for the *%s object! Exiting...",

cast(char*)(T).stringof);
exit_prog(1);
  }
}
  }

  void inc() {
this.ptr++;
this.length--;
  }
}
```

// Filename: "main.d"
```d
import core.stdc.stdlib;
import core.stdc.stdio;

import my_file;
import vec;

extern (C) void main() {
  auto modules = Vector!(My_File)(DEF_VEC_ALLOC_SIZE);
  const char* filename = "/tmp/test_file";
  int fd = open(filename, O_RDWR);

  ulong file_size = 24;
  const char* contents = cast(char*)mmap(null,
file_size, PROT_READ, MAP_PRIVATE, fd, 0
  );

  My_File file = {
'T',
contents,
file_size,
1, 2, 3, 4
  };

  debug printf("\n\nBEFORE ADDING IT TO THE MODULE\n");
  file.print();

  modules.add(file);

  debug printf("\n\n\nAFTER ADDING IT TO THE MODULE\n");
  modules.ptr.print();
  puts("");
  exit(0);
}
```


Re: Unittests being broken

2022-05-05 Thread René Zwanenburg via Digitalmars-d-learn

On Thursday, 5 May 2022 at 01:44:37 UTC, Ruby The Roobster wrote:

Some code I have:
(...)
What can I do to fix this?


I didn't try your code, but opList, funcList, and noTouch are 
thread-local variables, and you fill them in a shared static 
this. This means they will remain empty in any thread other than 
the main one. The test would fail in the way you describe if they 
are empty. So, I bet the tests run in another thread.


Of this is indeed the problem you can either:
- Make the lists immutable. This implicitly shares them among 
threads. But it looks like you want to modify them so immutable 
won't work in this case.


- If you don't want / need to share the lists over threads, fill 
them using a normal static this instead.


- If you do need to share them over threads, mark the lists as 
shared /
__gshared, but you'll be responsible for accessing them in a 
thread-safe manner.


Re: Why isn't my dynamic array method doesn't work for this type?

2022-05-05 Thread vit via Digitalmars-d-learn

On Thursday, 5 May 2022 at 11:49:29 UTC, vit wrote:

On Thursday, 5 May 2022 at 10:40:44 UTC, rempas wrote:
I have created a structure that is a actually an array that 
allocates memory and growths. It is a template and it has 
worked with a couple of types that I have tried with. It 
doesn't work with one tho and I cannot understand why. I will 
list the smallest possible code I could. Keep in mind that it 
is a couple of code. For anyone that wants to read it, keep in 
mind that I have to make this code work with LDC and using 
"-betterC".


[...]


```d
  this.capacity += DEF_VEC_REALLOC_SIZE;
  //realloc(this.ptr, T.sizeof * DEF_VEC_REALLOC_SIZE);
  this.ptr = realloc(this.ptr, T.sizeof * this.capacity); 
//<<--

```


```d
void add(T element) {
if (this.capacity == this.length) {
this.capacity += DEF_VEC_REALLOC_SIZE;
this.ptr = realloc(this.ptr, T.sizeof * this.capacity);
if (!this.ptr) { // Re-allocation error check
			fprintf(stderr, "Error: Could not allocate more memory for 
the *%s object! Exiting...",

  cast(char*)(T).stringof);
exit_prog(1);
}
}


this.ptr[this.length++] = element;

}
```


Re: Including C sources in a DUB project

2022-05-05 Thread Alexander Zhirov via Digitalmars-d-learn

On Thursday, 5 May 2022 at 16:23:18 UTC, H. S. Teoh wrote:

I don't know how to do it using dub, but you could use 
pragma(lib) in

one (or more) of your source files as a workaround:

pragma(lib, "m");
pragma(lib, "X11");
pragma(lib, "Xrandr");


I remember a long time ago, when I first started learning D, I 
set up a project in dub.json so that he compiled the sources 
written in C, linked the necessary libraries and then assembled 
the project in D along with the C object files. How to do it now 
- I have already forgotten.





Re: Including C sources in a DUB project

2022-05-05 Thread H. S. Teoh via Digitalmars-d-learn
On Thu, May 05, 2022 at 06:05:55AM +, Alexander Zhirov via 
Digitalmars-d-learn wrote:
> I'm sure there is such a topic on the forum, but after scrolling
> through the search, I didn't find anything. The bottom line is that I
> want to enable compilation of C sources in DUB and then build the
> project with the connection of libraries. I would like to see an
> example of such a `dub.json` project.
> 
> Enable libraries (flags) `-lm`, `-lX11`, `-lXrandr`.

I don't know how to do it using dub, but you could use pragma(lib) in
one (or more) of your source files as a workaround:

pragma(lib, "m");
pragma(lib, "X11");
pragma(lib, "Xrandr");


T

-- 
MACINTOSH: Most Applications Crash, If Not, The Operating System Hangs


Re: Using regular expressions when reading a file

2022-05-05 Thread Alexander Zhirov via Digitalmars-d-learn

On Thursday, 5 May 2022 at 18:58:41 UTC, H. S. Teoh wrote:
You don't have to. Just add a `$` to the end of your regex, and 
it should match the newline. If you put it outside the capture 
parentheses, it will not be included in the value.


In fact, it turned out to be much easier. It was just necessary 
to use the `m` flag instead of the `s` flag:


```d
auto p_property = regex(r"^(\w+) *= *(.+)", "m");
```



Re: Using regular expressions when reading a file

2022-05-05 Thread H. S. Teoh via Digitalmars-d-learn
On Thu, May 05, 2022 at 06:50:17PM +, Alexander Zhirov via 
Digitalmars-d-learn wrote:
> On Thursday, 5 May 2022 at 18:15:28 UTC, H. S. Teoh wrote:
> > auto m = matchFirst(line, p_property);
> 
> Yes, it looks more attractive. Thanks! I just don't quite understand how
> `matchFirst` works. I seem to have read the
> [description](https://dlang.org/phobos/std_regex.html#Captures), but I can't
> understand something.
> 
> And yet I have to manually remove the line break:
> ```sh
> ["host":"192.168.100.236\n", "dbname":"belpig\n", "user":"postgres",
> "port":"5432\n"]
> ```

You don't have to. Just add a `$` to the end of your regex, and it
should match the newline. If you put it outside the capture parentheses,
it will not be included in the value.


T

-- 
In a world without fences, who needs Windows and Gates? -- Christian Surchi


Re: Using regular expressions when reading a file

2022-05-05 Thread Ali Çehreli via Digitalmars-d-learn

On 5/5/22 12:05, Alexander Zhirov wrote:

On Thursday, 5 May 2022 at 18:58:41 UTC, H. S. Teoh wrote:
You don't have to. Just add a `$` to the end of your regex, and it 
should match the newline. If you put it outside the capture 
parentheses, it will not be included in the value.


In fact, it turned out to be much easier. It was just necessary to use 
the `m` flag instead of the `s` flag:


```d
auto p_property = regex(r"^(\w+) *= *(.+)", "m");
```



Couldn't help myself from improving. :) The following regex works in my 
Linux console. No issues with '\n'. (?) It also allows for leading and 
trailing spaces:


import std.regex;
import std.stdio;
import std.algorithm;
import std.array;
import std.typecons;
import std.functional;

void main() {
  auto p_property = regex(r"^ *(\w+) *= *(\w+) *$");
  const properties = File("settings.conf")
 .byLineCopy
 .map!(line => matchFirst(line, p_property))
 .filter!(not!empty) // OR: .filter!(m => !m.empty)
 .map!(m => tuple(m[1], m[2]))
 .assocArray;

  writeln(properties);
}

Ali


Re: Using regular expressions when reading a file

2022-05-05 Thread H. S. Teoh via Digitalmars-d-learn
On Thu, May 05, 2022 at 05:53:57PM +, Alexander Zhirov via 
Digitalmars-d-learn wrote:
> I want to use a configuration file with external settings. I'm trying
> to use regular expressions to read the `Property = Value` settings. I
> would like to do it all more beautifully. Is there any way to get rid
> of the line break character? How much does everything look "right"?
[...]
> ```d
> auto file = File("settings.conf", "r");
> string[string] properties;
> auto p_property = regex(r"^\w+ *= *.+", "s");
> while (!file.eof())
> {
>   string line = file.readln();
>   auto m = matchAll(line, p_property);
>   if (!m.empty())
>   {
> string property = matchAll(line, regex(r"^\w+", "m")).hit;
> string value = replaceAll(line, regex(r"^\w+ *= *", "m"), "");
> properties[property] = value;
>   }
> }

Your regex already matches the `Property = Value` pattern; why not just
use captures to extract the relevant parts of the match, insteead of
doing it all over again inside the if-statement?

// I added captures (parentheses) to extract the property name
// and value directly from the pattern.
auto p_property = regex(r"^(\w+) *= *(.+)", "s");

// I assume you only want one `Property = Value` pair per input
// line, so you really don't need matchAll; matchFirst will do
// the job.
auto m = matchFirst(line, p_property);

if (m) {
// No need to run a match again, just extract the
// captures
string property = m[1];
string value = m[2];
properties[property] = value;
}


T

-- 
"You are a very disagreeable person." "NO."


Re: Using regular expressions when reading a file

2022-05-05 Thread Alexander Zhirov via Digitalmars-d-learn

On Thursday, 5 May 2022 at 18:15:28 UTC, H. S. Teoh wrote:

auto m = matchFirst(line, p_property);


Yes, it looks more attractive. Thanks! I just don't quite 
understand how `matchFirst` works. I seem to have read the 
[description](https://dlang.org/phobos/std_regex.html#Captures), 
but I can't understand something.


And yet I have to manually remove the line break:
```sh
["host":"192.168.100.236\n", "dbname":"belpig\n", 
"user":"postgres", "port":"5432\n"]

```


Using regular expressions when reading a file

2022-05-05 Thread Alexander Zhirov via Digitalmars-d-learn
I want to use a configuration file with external settings. I'm 
trying to use regular expressions to read the `Property = Value` 
settings. I would like to do it all more beautifully. Is there 
any way to get rid of the line break character? How much does 
everything look "right"?


**settings.conf:**

```sh
host = 127.0.0.1
port = 5432
dbname = database
user = postgres
```

**code:**

```d
auto file = File("settings.conf", "r");
string[string] properties;
auto p_property = regex(r"^\w+ *= *.+", "s");
while (!file.eof())
{
  string line = file.readln();
  auto m = matchAll(line, p_property);
  if (!m.empty())
  {
string property = matchAll(line, regex(r"^\w+", "m")).hit;
string value = replaceAll(line, regex(r"^\w+ *= *", "m"), "");
properties[property] = value;
  }
}
file.close();
writeln(properties);
```

**output:**

```sh
["host":"127.0.0.1\n", "dbname":"mydb\n", "user":"postgres", 
"port":"5432\n"]

```


Re: Why isn't my dynamic array method doesn't work for this type?

2022-05-05 Thread vit via Digitalmars-d-learn

On Thursday, 5 May 2022 at 10:40:44 UTC, rempas wrote:
I have created a structure that is a actually an array that 
allocates memory and growths. It is a template and it has 
worked with a couple of types that I have tried with. It 
doesn't work with one tho and I cannot understand why. I will 
list the smallest possible code I could. Keep in mind that it 
is a couple of code. For anyone that wants to read it, keep in 
mind that I have to make this code work with LDC and using 
"-betterC".


[...]


```d
  this.capacity += DEF_VEC_REALLOC_SIZE;
  //realloc(this.ptr, T.sizeof * DEF_VEC_REALLOC_SIZE);
  this.ptr = realloc(this.ptr, T.sizeof * this.capacity); 
//<<--

```


Re: Why isn't my dynamic array method doesn't work for this type?

2022-05-05 Thread Steven Schveighoffer via Digitalmars-d-learn

On 5/5/22 6:40 AM, rempas wrote:

   ref My_File opAssign(ref const My_File s) return { return this; }


Your assignment operator does nothing.

-Steve


Re: Using regular expressions when reading a file

2022-05-05 Thread Alexander Zhirov via Digitalmars-d-learn

On Thursday, 5 May 2022 at 19:19:26 UTC, Ali Çehreli wrote:
Couldn't help myself from improving. :) The following regex 
works in my Linux console. No issues with '\n'. (?) It also 
allows for leading and trailing spaces:


import std.regex;
import std.stdio;
import std.algorithm;
import std.array;
import std.typecons;
import std.functional;

void main() {
  auto p_property = regex(r"^ *(\w+) *= *(\w+) *$");
  const properties = File("settings.conf")
 .byLineCopy
 .map!(line => matchFirst(line, p_property))
 .filter!(not!empty) // OR: .filter!(m => 
!m.empty)

 .map!(m => tuple(m[1], m[2]))
 .assocArray;

  writeln(properties);
}


It will need to be sorted out with a fresh head.  Thanks!



Re: Why isn't my dynamic array method doesn't work for this type?

2022-05-05 Thread rempas via Digitalmars-d-learn

On Thursday, 5 May 2022 at 11:49:29 UTC, vit wrote:


```d
  this.capacity += DEF_VEC_REALLOC_SIZE;
  //realloc(this.ptr, T.sizeof * DEF_VEC_REALLOC_SIZE);
  this.ptr = realloc(this.ptr, T.sizeof * this.capacity); 
//<<--

```


Oh, right! I forgot to say it. I'm using my own `realloc` and not 
the one from libc. So, the `realloc` function is not the problem 
here.


Re: Why isn't my dynamic array method doesn't work for this type?

2022-05-05 Thread rempas via Digitalmars-d-learn
On Thursday, 5 May 2022 at 14:20:49 UTC, Steven Schveighoffer 
wrote:

Your assignment operator does nothing.

-Steve


Thanks! That was indeed the problem! In the other data 
structures, it worked without needing explicitly provide one so I 
didn't thought about it. Thanks a lot, you're saving me!


Re: Why isn't my dynamic array method doesn't work for this type?

2022-05-05 Thread colleen camacho via Digitalmars-d-learn

On Thursday, 5 May 2022 at 10:40:44 UTC, rempas wrote:
I have created a structure that is a actually an array that 
allocates memory and growths. It is a template and it has 
worked with a couple of types that I have tried with. It 
doesn't work with one tho and I cannot understand why. I will 
list the smallest possible code I could. Keep in mind that it 
is a couple of code. For anyone that wants to read it, keep in 
mind that I have to make this code work with LDC and using 
"-betterC".


[...]


can i use method of Dynamic array in C using malloc library 
function. Program example will create an integer array of any 
length dynamically by asking the array size and array elements 
from user and display on the screen.how memory allocation in C 
programming is done at run time with examples. to integrate an 
api of tracking number like [intelcom courrier canada inc 
tracking](https://couriertrackingfinder.com/intelcom-express-tracking/)?


How to unpack template parameters,Or how to forward parameters?

2022-05-05 Thread zjh via Digitalmars-d-learn

Hello everyone,I have following function:
```d
import core.stdc.stdio;
void f(int i,int j){
printf("%i,%i",i,j);
}
```
I want to forward `int[N]` to `(int i,int j)` etc. How can I 
write a forwarding function?

```d
void ff(alias g,int...I)(int[2]k){
g(k[I]);
}//How to unpack template variable parameters?

void f(int[2]k){
ff!(f,0,1)(k);//how to forward?
}

```

main function:
```d
extern(C):void main()
{
int[2] a=[2,4];
f(a);//calling like this
}
```



Re: Google Code Jam 2022

2022-05-05 Thread Siarhei Siamashka via Digitalmars-d-learn
Now all three parts of the round 1 are over and finalized. 
There's a third-party website, which provides some stats: 
https://cpjsmith.uk/gcj/year?year=2022


It's interesting that D is one of the least popular programing 
languages with only a single digit number of users. It never 
managed to gain traction for some reason even in this niche. Yes, 
I know that there's a big discussion in another forum thread 
about why D is unpopular in general: 
https://forum.dlang.org/post/axslxubumvtrudpjf...@forum.dlang.org


In programming competitions the experimental nature of the 
programming language and never ending compatibility breakages are 
a non-issue, because there's no need to maintain the written code 
over the span of many years. So it may seem that D should be a 
very good choice for programming competitions, but there's still 
no success. And it's not just the usual domination of C++ and 
Python languages. Rust is happily eating D's lunch.


Re: Using regular expressions when reading a file

2022-05-05 Thread forkit via Digitalmars-d-learn

On Thursday, 5 May 2022 at 17:53:57 UTC, Alexander Zhirov wrote:
I want to use a configuration file with external settings. I'm 
trying to use regular expressions to read the `Property = 
Value` settings. I would like to do it all more beautifully. Is 
there any way to get rid of the line break character? How much 
does everything look "right"?


regex never looks right ;-)

try something else perhaps??

// 

module test;

import std;

void main()
{
auto file = File("d:\\settings.conf", "r");
string[string] aa;

// create an associate array of settings -> [key:value]
foreach (line; file.byLine().filter!(a => !a.empty))
{
auto myTuple = line.split(" = ");
aa[myTuple[0].to!string] = myTuple[1].to!string;
}

// write out all the settings.
foreach (key, value; aa.byPair)
writefln("%s:%s", key, value);

writeln;

// write just the host value
writeln(aa["host"]);

}


//