www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Negative index range violation

reply SrMordred <patric.dexheimer gmail.com> writes:
string x = "123";
auto c = x.ptr;
c++;
writeln(c[-1]); // 1
writeln(c[-1..0]); //BOOM Range violation


Can I do this / Bug / some mistake ?
Feb 21 2018
next sibling parent reply Nicholas Wilson <iamthewilsonator hotmail.com> writes:
On Thursday, 22 February 2018 at 00:13:43 UTC, SrMordred wrote:
 string x = "123";
 auto c = x.ptr;
 c++;
 writeln(c[-1]); // 1
 writeln(c[-1..0]); //BOOM Range violation


 Can I do this / Bug / some mistake ?
youd have to do (c-1)[0 .. 1];
Feb 21 2018
parent reply SrMordred <patric.dexheimer gmail.com> writes:
 But with a slice negative indexes are never allowed, even on a 
 pointer.
 youd have to do
 (c-1)[0 .. 1];
Nice! Thank you both! In D Slice article it says "You can even use negative indexes!" so I thought that the [-1..x] should work too :)
Feb 21 2018
parent reply Steven Schveighoffer <schveiguy yahoo.com> writes:
On 2/21/18 7:30 PM, SrMordred wrote:
 But with a slice negative indexes are never allowed, even on a pointer.
 youd have to do
 (c-1)[0 .. 1];
Nice! Thank you both! In D Slice article it says "You can even use negative indexes!" so I thought that the [-1..x] should work too :)
Hah! I never thought of doing a slice with negative indexes ;) Note that the statement is about C pointers, so since C doesn't have slicing, it stands to reason that slicing with negative indexes isn't supported. -Steve
Feb 21 2018
next sibling parent joe <joe example.com> writes:
On Thursday, 22 February 2018 at 02:41:30 UTC, Steven 
Schveighoffer wrote:
 On 2/21/18 7:30 PM, SrMordred wrote:
 But with a slice negative indexes are never allowed, even on 
 a pointer.
 youd have to do
 (c-1)[0 .. 1];
Nice! Thank you both! In D Slice article it says "You can even use negative indexes!" so I thought that the [-1..x] should work too :)
Hah! I never thought of doing a slice with negative indexes ;) /SNIP -Steve
At night I dream about doing something like this auto pos = haystack.find(needle); auto something = haystack[pos..+3]; // meaning [pos..pos+3] auto somethingElse = haystack[pos..-3]; // and [pos..pos-3] respectively :)
Feb 22 2018
prev sibling parent SrMordred <patric.dexheimer gmail.com> writes:
On Thursday, 22 February 2018 at 02:41:30 UTC, Steven 
Schveighoffer wrote:
 Hah! I never thought of doing a slice with negative indexes ;)
Maybe is my past of python: arr[-3:] to get the last 3 elements for eg. :)
Feb 22 2018
prev sibling parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Thursday, 22 February 2018 at 00:13:43 UTC, SrMordred wrote:
 string x = "123";
 auto c = x.ptr;
 c++;
 writeln(c[-1]); // 1
That's only happening because pointers bypass range checks.
 writeln(c[-1..0]); //BOOM Range violation
But with a slice negative indexes are never allowed, even on a pointer.
Feb 21 2018
parent Timon Gehr <timon.gehr gmx.ch> writes:
On 22.02.2018 01:26, Adam D. Ruppe wrote:
 On Thursday, 22 February 2018 at 00:13:43 UTC, SrMordred wrote:
 string x = "123";
 auto c = x.ptr;
 c++;
 writeln(c[-1]); // 1
That's only happening because pointers bypass range checks.
 writeln(c[-1..0]); //BOOM Range violation
But with a slice negative indexes are never allowed, even on a pointer.
Actually, it's slightly more complicated than that. E.g. c[-2..-1] does not trigger the range check and will behave as expected. For slices c[l..r] there is a check whether cast(size_t)l<=cast(size_t)r. The range violation happens because -1 is larger than 0 as an unsigned integer.
Feb 22 2018