Jump to page: 1 2
Thread overview
Avoid subtracting form .length
Nov 08
Dom DiSc
Nov 09
monkyyy
Nov 14
Dom DiSc
6 days ago
Manfred Nowak
Nov 14
user1234
Nov 14
matheus
6 days ago
user1234
6 days ago
Dom DiSc
November 08

I very often use this pattern:

fun(ref int[] a)
{
   assert(a.length && a.length<=100);
   int[100] b;
   b[0 .. a.length-1] = a[];
   b[a.length .. 100] = 5;
}

I consider this perfectly safe, but DScanner gives warnings for this, no matter if a check is present or not.
What's the recommended fix for this? I have no idea what else to use.

November 09

On Friday, 8 November 2024 at 23:27:40 UTC, Dom DiSc wrote:

>

I very often use this pattern:

fun(ref int[] a)
{
   assert(a.length && a.length<=100);
   int[100] b;
   b[0 .. a.length-1] = a[];
   b[a.length .. 100] = 5;
}

I consider this perfectly safe, but DScanner gives warnings for this, no matter if a check is present or not.
What's the recommended fix for this? I have no idea what else to use.

write a utility function? after turning off dscanner

November 09

On Friday, 8 November 2024 at 23:27:40 UTC, Dom DiSc wrote:

>

I very often use this pattern:

fun(ref int[] a)
{
   assert(a.length && a.length<=100);
   int[100] b;
   b[0 .. a.length-1] = a[];
   b[a.length .. 100] = 5;
}

I use a.length +(-1). It's not pretty but at least dscanner is happy.

November 08
On Friday, November 8, 2024 4:27:40 PM MST Dom DiSc via Digitalmars-d-learn wrote:
> I _very_ often use this pattern:
>
> ```
> fun(ref int[] a)
> {
>     assert(a.length && a.length<=100);
>     int[100] b;
>     b[0 .. a.length-1] = a[];
>     b[a.length .. 100] = 5;
> }
> ```
>
> I consider this perfectly safe, but DScanner gives warnings for
> this, no matter if a check is present or not.
> What's the recommended fix for this? I have no idea what else to
> use.

Well, I don't know what it's warning about, but the code is wrong, because the lengths of the slices don't match in

    b[0 .. a.length-1] = a[];

You'll get a RangeError being thrown when you run the code.

- Jonathan M Davis



November 14

On Friday, 8 November 2024 at 23:27:40 UTC, Dom DiSc wrote:

>

I very often use this pattern:

fun(ref int[] a)
{
   assert(a.length && a.length<=100);
   int[100] b;
   b[0 .. a.length-1] = a[];
   b[a.length .. 100] = 5;
}

I consider this perfectly safe, but DScanner gives warnings for this, no matter if a check is present or not.
What's the recommended fix for this? I have no idea what else to use.

Why not use foreach in your code? Is there a specific reason for this? If I'm not mistaken, this is what you want to do:

import std.array, std.range, std.stdio;

void fun(size_t size, int padding = 42)(ref int[] arr)
{
  assert(arr.length <= size, "Size Error!");
  int[size] sarr = padding;

  foreach(i, ref e; arr) sarr[i] = e;/*
  sarr[0 .. arr.length+ 1] = arr[];
  sarr[arr.length .. size] = padding;//*/

  sarr.writefln!"   %s";
}

void main()
{
  int[] arr = iota(5).array;
  arr.length.writeln(": ", arr);
  arr.fun!10;
} /*
  5: [0, 1, 2, 3, 4]
     [0, 1, 2, 3, 4, 42, 42, 42, 42, 42]
//*/

SDB79

November 14
On Friday, 8 November 2024 at 23:27:40 UTC, Dom DiSc wrote:
> ...

My version:

import std.stdio, std.array, std.range, std.algorithm;

void foo(ref int[] a, int filler){
   int[5] b;
   auto len = min(b.length,a.length);
   b[0..len] = a[0..len];
   b[len..5] = filler;
   writeln("a = ", a);
   writeln("b = ", b);
}

void main(){
   int[] a;

   a = iota(2).array;
   foo(a, 77);

   writeln("");

   a = iota(11).array;
   foo(a, 77);
}

output:

a = [0, 1]
b = [0, 1, 77, 77, 77]

a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
b = [0, 1, 2, 3, 4]

Matheus.
November 14
On Saturday, 9 November 2024 at 04:02:46 UTC, Jonathan M Davis wrote:
[...]
>     b[0 .. a.length-1] = a[];
>
> You'll get a RangeError being thrown when you run the code.
[...]

Therefore a fix might be to not deminish the length:
    b[ 0 .. a.length]= a[];

-manfred

November 14
No, no. My problem is: I want to access the last element of an array and assign it to the element of same index in another array (so I can't use $), and I checked that the source array has at least one element.

The questions are:
- Why is it considered dangerous to use the expression a.length-1 ?
- What else is proposed to implement the desired effect without warning?
November 14

On Thursday, 14 November 2024 at 06:53:01 UTC, Salih Dincer wrote:

>

On Friday, 8 November 2024 at 23:27:40 UTC, Dom DiSc wrote:

>

I very often use this pattern:

fun(ref int[] a)
{
   assert(a.length && a.length<=100);
   int[100] b;
   b[0 .. a.length-1] = a[];
   b[a.length .. 100] = 5;
}

I consider this perfectly safe, but DScanner gives warnings for this, no matter if a check is present or not.
What's the recommended fix for this? I have no idea what else to use.

Why not use foreach in your code? Is there a specific reason for this? If I'm not mistaken, this is what you want to do:

[...]

SDB79

I dont know if that's the reason but slice assignments are likely faster, e.g for builtin types that certainly lead to generate a stdc memoves. (that must be indirectly done in DRT function _d_array_slice_copy).

6 days ago

On Friday, 8 November 2024 at 23:27:40 UTC, Dom DiSc wrote:

>

I very often use this pattern:

fun(ref int[] a)
{
   assert(a.length && a.length<=100);
   int[100] b;
   b[0 .. a.length-1] = a[];
   b[a.length .. 100] = 5;
}

I consider this perfectly safe, but DScanner gives warnings for this, no matter if a check is present or not.
What's the recommended fix for this? I have no idea what else to use.

You define a contract that Dscanner does not understand. The more simple solution, as you seem to be careful about bounds, is to disable the specific check that worries you. You can do that in an ".ini" file that must stand in the project root directory IIRC.

« First   ‹ Prev
1 2