www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Avoid subtracting form .length

reply Dom DiSc <dominikus scherkl.de> writes:
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.
Nov 08
next sibling parent monkyyy <crazymonkyyy gmail.com> writes:
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~~
Nov 08
prev sibling next sibling parent Anonymouse <zorael gmail.com> writes:
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.
Nov 08
prev sibling next sibling parent reply Jonathan M Davis <newsgroup.d jmdavisprog.com> writes:
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
Nov 08
parent reply Manfred Nowak <svv1999 hotmail.com> writes:
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
Nov 14
next sibling parent reply Dom DiSc <dominikus scherkl.de> writes:
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?
Nov 14
parent Manfred Nowak <svv1999 hotmail.com> writes:
On Thursday, 14 November 2024 at 13:24:47 UTC, Dom DiSc wrote:
 No, no. My problem is:  [...]
So it seems, that the intended problem was transposed into some example, thereby oblittering that problem with some coding errors.
 I checked that the source array has at least one element.
Yes, but only for the purpose of the development of the code, because the used `assert' will probably not be compiled into a release binary. [...]
 - What else is proposed to implement the desired effect
 without warning?
Maybe using an `if`-statement, which is propper for release too, instead of an `assert`-expression, which is propper for develeopment only? Please reconsider the docs at 11.19.10.9.1: https://dlang.org/spec/expression.html#assert_expressions
Nov 15
prev sibling parent Paolo Invernizzi <paolo.invernizzi gmail.com> writes:
On Thursday, 14 November 2024 at 13:08:36 UTC, Manfred Nowak 
wrote:
 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
Good Lord! Manfred Nowak? Is that really you?! Everyone, we have the history of D in its full essence, dating back to 2004! :-P It's great to see you here again, even if only from time to time!
Nov 18
prev sibling next sibling parent reply Salih Dincer <salihdb hotmail.com> writes:
On Friday, 8 November 2024 at 23:27:40 UTC, Dom DiSc wrote:
 I _very_ often use this pattern:

 ```d
 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: ```d 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
Nov 13
parent user1234 <user1234 12.de> writes:
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:

 ```d
 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 `memove`s. (that must be indirectly done in DRT function `_d_array_slice_copy`).
Nov 14
prev sibling next sibling parent matheus <matheus gmail.com> writes:
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.
Nov 14
prev sibling parent reply user1234 <user1234 12.de> writes:
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.
Nov 14
parent Dom DiSc <dominikus scherkl.de> writes:
On Thursday, 14 November 2024 at 17:27:54 UTC, user1234 wrote:
 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.
Ok. Somewhat sad, but at least it works. Thanks for all the replies.
Nov 14