Re: ImportC: Windows.h

2023-11-30 Thread Kagamin via Digitalmars-d-learn

You can declare them
```
extern(C) void _InterlockedExchangeAdd(){ assert(false); }
```


Re: ImportC: Windows.h

2023-11-30 Thread name via Digitalmars-d-learn

On Thursday, 30 November 2023 at 08:54:40 UTC, Kagamin wrote:

You can declare them
```
extern(C) void _InterlockedExchangeAdd(){ assert(false); }
```


That works, thanks.


Re: struct initializer

2023-11-30 Thread Dennis via Digitalmars-d-learn

On Thursday, 30 November 2023 at 07:21:29 UTC, Dom DiSc wrote:
So, why supporting the (somewhat strange looking) version with 
curly backets at all?
It only works in one special place, so is simply overhead to 
remember.
Again a superfluous way to do the same - but only under 
specific circumstances.


I think a syntax should work either always or never.


The syntax was inherited from C. The 'special place' is called 
initialization, and it's special because the target type of the 
initializer is known in advance, while normal expression 
assignments are analyzed bottom up. Since there is no 
`typeof({10, 10})`, struct initializers don't work as expressions.


C99 added Compound literals `(S){.a = 10, .b = 20}`, and with 
named arguments you can do the same in D: `S(a: 10, b:20)`, and 
since the type name is included, they do work as standalone 
expressions.


Walter tried to deprecate the old struct initializer syntax:
https://github.com/dlang/DIPs/blob/master/DIPs/other/DIP1031.md

But it got some resistance, since {} initializers still have an 
advantage when you want to define an array of structs, and don't 
want to repeat the (potentially long) struct name for every entry.


Also note that even when {} is removed, there are still other 
special cases with initialization, for example with arrays:


```D
void main()
{
short[] a = [3: 10]; // ok
a = [3: 10]; // cannot implicitly convert expression `[3:10]` 
of type `int[int]` to `short[]`

}
```


Re: struct initializer

2023-11-30 Thread zjh via Digitalmars-d-learn

On Wednesday, 29 November 2023 at 16:38:36 UTC, Dom DiSc wrote:

```d

struct S { int a; int b; }
S2 fun3() { return S2( 5, 2 ); }

```

Here,`S2( 5, 2 );` violeit `DRY` principle.



Re: ImportC: Windows.h

2023-11-30 Thread name via Digitalmars-d-learn

Weird... building with ```dmd -m64 -i main.d``` works fine.

Then, I tried calling 
[CreateFileW](https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-createfilew), passing ```GENERIC_READ``` for ```[in] dwDesiredAccess```; I get this:


```main.d(7): Error: undefined identifier GENERIC_READ```.


ImportC seems very brittle. :/


Re: D: Convert/parse uint integer to string. (@nogc)

2023-11-30 Thread kdevel via Digitalmars-d-learn
On Friday, 24 November 2023 at 13:05:30 UTC, Ferhat Kurtulmuş 
wrote:

[...]
```
import core.stdc.stdio : sprintf;
import core.stdc.math : log10;

import std.exception : assumeUnique;
import std.stdio : writeln;

size_t nod(int num){
  return cast(size_t)((num==0)?1:log10(num)+1);
}

void main()
{
   int myint = 23;

   char[80] str;

   sprintf(str.ptr, "%d", myint);

   string _dstring = str[0..nod(myint)].assumeUnique;

   writeln(_dstring);

}
```


What happend to the indentation? Anyway

```
$ ./inttostr -1

$ ./inttostr -2147483648

```

I like `snprintf`:

```d
import core.stdc.stdio : snprintf;
import std.exception : assumeUnique, enforce;
import std.stdio;
import std.conv;
import core.stdc.locale;
import std.format;

void main (string [] args)
{
   setlocale (LC_NUMERIC, "");
   auto myint = args[1].to!long;
   char[27] str;
   auto n = snprintf(str.ptr, str.sizeof, "%'ld", myint);
   enforce (n < str.sizeof, "buffer too small");
   string _dstring = str[0..n].assumeUnique;
   writeln(_dstring);
}
```

```
$ ./l2s -9223372036854775808
-9.223.372.036.854.775.808
```



Re: D: Convert/parse uint integer to string. (@nogc)

2023-11-30 Thread kdevel via Digitalmars-d-learn

On Tuesday, 28 November 2023 at 09:43:47 UTC, Dom DiSc wrote:

On Tuesday, 28 November 2023 at 08:51:21 UTC, Mark Davies wrote:

On Friday, 24 November 2023 at 09:35:00 UTC, BoQsc wrote:
```
import std.stdio;

char[10] longToString(long n) @nogc
```


For a 'long' 10 characters is likely to be not enough (long max 
is 9223372036854775808 [...]


`long.max` is 9223372036854775807, `long.min` is 
-9223372036854775808. Besides from the too short buffer the 
`longToString` function neither converts 0 (zero) nor `long.min` 
correctly.





Re: D: Convert/parse uint integer to string. (@nogc)

2023-11-30 Thread Siarhei Siamashka via Digitalmars-d-learn

On Friday, 24 November 2023 at 09:35:00 UTC, BoQsc wrote:

I tried to look into https://dlang.org/phobos/std_conv.html

Most of the functions inside `std.conv` seem to be dependant on 
[Garbage Collection](https://dlang.org/spec/garbage.html).


And I couldn't find a straightforward way to produce a `string` 
value out of `uint` value.


How to convert or parse `uint` value to a `string` in `@nogc` 
way?


There are various tricks to do such conversion very fast. For 
example, the following article is essential for implementing it: 
https://lemire.me/blog/2021/06/03/computing-the-number-of-digits-of-an-integer-even-faster/


Rather than doing conversion one digit at a time, it's much more 
efficient to process multiple digits per loop iteration. Consider 
an illustrative pseudocode like this:


```D
while (x >= 100) {
  write(twodigits_lookup_table[x % 100]); // we can handle 
two digits at once

  x /= 100;
}
```

I have a @nogc compatible DUB module, which implements fast 
formatted output to stdout: https://github.com/ssvb/speedy-stdio 
(cutting some corners allows to squeeze some extra performance 
out of it).


The same code can be adapted to do formatted output to a memory 
buffer as well.