November 03, 2021

On Monday, 1 November 2021 at 21:32:21 UTC, Ali Çehreli wrote:

>

Joking aside, I liked the nested struct and its opAssign to mimic internal arr.length = 42 syntax. (I know it involves a potentially expensive delegate but still...)

The nested struct is not needed. UFCS works for setters, too:

void assumedLength(S)(ref S slice, size_t length) {
    if(slice.length >= length)
        slice.length = length;
    else
        assert(false, "Let's not corrupt memory today.");
}

void main() {
  auto arr = [ 1, 2, 3 ];
  arr.assumedLength = 2;
  writeln(arr);
}
November 03, 2021
On 11/2/21 9:06 PM, tsbockman wrote:

> UFCS works for setters, too:

Oh yeah! Pretty cool. :)

Ali
November 03, 2021

On Wednesday, 3 November 2021 at 02:38:56 UTC, Steven Schveighoffer wrote:

>

On 11/1/21 9:03 PM, pascal111 wrote:

>

On Monday, 1 November 2021 at 21:32:21 UTC, Ali Çehreli wrote:

> >

Joking aside
...

This function seems smart and flexible and higher than my current level, I'll study it.

Please please, do NOT study this code. It is bad all around. Ali should know better ;)

He is joking with the code posted, it should not be used in any real code ever. Not only is it corrupting memory, it may not work as expected in all cases (and will randomly fail).

Please learn first how memory works before writing the low-level bits of arrays!

As a beginner, I suggest studying what a buffer overrun is, and why it is bad.

-Steve

I started to change my mind about doing things in low level of classic C, I started to think get benefits of new features of D.

November 03, 2021

On Wednesday, 3 November 2021 at 10:38:45 UTC, pascal111 wrote:

>

On Wednesday, 3 November 2021 at 02:38:56 UTC, Steven Schveighoffer wrote:

>

On 11/1/21 9:03 PM, pascal111 wrote:

>

[...]

> >

[...]
...
[...]

Please please, do NOT study this code. It is bad all around. Ali should know better ;)

He is joking with the code posted, it should not be used in any real code ever. Not only is it corrupting memory, it may not work as expected in all cases (and will randomly fail).

Please learn first how memory works before writing the low-level bits of arrays!

As a beginner, I suggest studying what a buffer overrun is, and why it is bad.

-Steve

I started to change my mind about doing things in low level of classic C, I started to think get benefits of new features of D.

Good, you will be much happier ☀️

November 04, 2021

On Monday, 1 November 2021 at 19:56:13 UTC, pascal111 wrote:

>

But what if I want to use "strcpy" function to assign that new value to the array that the problem is that the array won't take more than its first initializing value length:

{

char[] s="xyz".dup;

strcpy(&s[0], "Hello World!");

writeln(s);

}

Result:

Hel

Easy win: use normal operations with static arrays instead of strcpy:

unittest {
    char[3] s = "xyz";
    s = "Hello World"; // Error: index [11] exceeds array of length 3
}

Now you'll get errors (in range-checked builds) instead of silent bad behavior.

With a larger buffer, short assigns (only with string literals) zero the rest of the buffer:

unittest {
    char[15] s = "xyz";
    s = "Hello World"; // this is OK
    assert(s == "Hello World\0\0\0\0");
}

unittest {
    char[15] s = "Hello World";
    s = "xyz"; // so is this
    assert(s[0 .. 5] == "xyz\0\0");
}

But more realistically you'll be getting strings from somewhere and can assign a specific region using the length of those strings:

unittest {
    // pretend these are user inputs
    string ex1 = "Hello World";
    string ex2 = "xyz";

    char[15] s;

    s[0 .. ex1.length] = ex1;
    assert(s == "Hello World\xFF\xFF\xFF\xFF");
    s[0 .. ex2.length] = ex2;
    assert(s[0 .. ex1.length] == "xyzlo World");
}

This is all obviously much more of a hassle than normal D string-handling, but if you're going to apply C levels of care with memory and string handling, you can do that in D while still avoiding C levels of bugs.

unittest {
    import std.algorithm : min;
    import core.stdc.string : strlen;

    char[6] buf;
    string output;

    foreach (input; ["ok", "Way too long!"]) {
        auto len = min(buf.length-1, input.length);
        buf[0 .. len] = input[0 .. len];
        buf[len] = '\0';
        output ~= buf[0 .. len+1];
    }
    assert(output == "ok\0Way t\0");
    assert(output[0 .. strlen(&output[0])] == "ok");
}
November 05, 2021

On Thursday, 4 November 2021 at 01:29:57 UTC, jfondren wrote:

>

On Monday, 1 November 2021 at 19:56:13 UTC, pascal111 wrote:

>

But what if I want to use "strcpy" function to assign that new value to the array that the problem is that the array won't take more than its first initializing value length:

{

.
.
.

>
assert(output == "ok\0Way t\0");
assert(output[0 .. strlen(&output[0])] == "ok");

}

I appreciate your vary explanation.

1 2
Next ›   Last »