Re: Write UTF-8 bytes directly to stack buffer

2022-03-13 Thread Brian Callahan via Digitalmars-d-learn

On Sunday, 13 March 2022 at 07:55:01 UTC, Chris Piker wrote:


Hey thanks!  That does work with recent versions of dmd+phobos, 
but doesn't work in gdc-10.  For some reason it produces this 
error:


```d
error: static assert  "Cannot put a const(char)[] into a 
char[]."

```

Is there a work around involving `.representation` as alluded 
to in this 
[thread](https://forum.dlang.org/post/zmehmpithifbgfuef...@forum.dlang.org) ?


To get around the issue I built gdc-11.2 from source code at 
the GNU site but the old version of phobos is still included, 
so no dice.


Build the latest gdc snapshot: 
https://mirrors.concertpass.com/gcc/snapshots/12-20220306/


~Brian


Re: Detecting manifest contants

2022-03-13 Thread Basile B. via Digitalmars-d-learn

On Saturday, 12 March 2022 at 18:49:32 UTC, Anonymouse wrote:
I'm introspecting structs, and I ran into an issue where  
`__traits(derivedMembers)` includes manifest constant enums in 
the returned tuple.


What is the correct way to statically detect these? The 
immediate thing that springs to mind is `is(symbol == enum)`, 
but it's not it.


Currently I'm testing if a function that takes the address of 
the member compiles, and I think it works, but like with 
everything `__traits(compiles)` it strikes me as it might not 
be the right way to go about things.


```d
struct Foo
{
int i;
enum k = 42;
}

void main()
{
foreach (memberstring; __traits(derivedMembers, Foo))
{
static if (__traits(compiles, { Foo f; auto ptr = 
&__traits(getMember, f, memberstring); }))

{
// ...
}
}
}
```

What else can I try?


A way is to try declaring an enum with the value returned by the 
`getMember` trait.


```d
/**
 * Indicates wether something is a value known at compile time.
 *
 * Params:
 *  V = The value to test.
 *  T = Optional, the expected value type.
 */
template isCompileTimeValue(alias V, T...)
if (T.length == 0 || (T.length == 1 && is(T[0])))
{
enum isKnown = is(typeof((){enum v = V;}));
static if (!T.length)
enum isCompileTimeValue = isKnown;
else
enum isCompileTimeValue = isKnown && is(typeof(V) == 
T[0]);

}
///
unittest
{
string a;
enum b = "0";
enum c = 0;
static assert(!isCompileTimeValue!a);
static assert(isCompileTimeValue!b);
static assert(isCompileTimeValue!c);
static assert(isCompileTimeValue!(b,string));
static assert(isCompileTimeValue!(c,int));
static assert(!isCompileTimeValue!(c,char));
static assert(!isCompileTimeValue!(char));
}

/// ditto
template isCompileTimeValue(V, T...)
if (T.length == 0 || (T.length == 1 && is(T[0])))
{
enum isCompileTimeValue = false;
}
```



Re: Write UTF-8 bytes directly to stack buffer

2022-03-13 Thread Chris Piker via Digitalmars-d-learn

On Thursday, 10 March 2022 at 17:59:33 UTC, H. S. Teoh wrote:
Probably what you're looking for is std.format.formattedWrite. 
For example:


```d
import std;
void main() {
ubyte[65536] buf;
char[] usable_buf = cast(char[]) buf[];
	usable_buf.formattedWrite!"Blah %d blah %s"(123, "Это UTF-8 
строка.");

auto used = buf.length - usable_buf.length;
writefln("%(%02X %)", buf[0 .. used]);
}
```


Hey thanks!  That does work with recent versions of dmd+phobos, 
but doesn't work in gdc-10.  For some reason it produces this 
error:


```d
error: static assert  "Cannot put a const(char)[] into a char[]."
```

Is there a work around involving `.representation` as alluded to 
in this 
[thread](https://forum.dlang.org/post/zmehmpithifbgfuef...@forum.dlang.org) ?


To get around the issue I built gdc-11.2 from source code at the 
GNU site but the old version of phobos is still included, so no 
dice.