digitalmars.D.learn - Negative index range violation
- SrMordred (6/6) Feb 21 2018 string x = "123";
- Nicholas Wilson (3/9) Feb 21 2018 youd have to do
- SrMordred (5/9) Feb 21 2018 Nice!
- Steven Schveighoffer (6/19) Feb 21 2018 Hah! I never thought of doing a slice with negative indexes ;)
- Adam D. Ruppe (4/9) Feb 21 2018 But with a slice negative indexes are never allowed, even on a
- Timon Gehr (5/16) Feb 22 2018 Actually, it's slightly more complicated than that. E.g. c[-2..-1] does
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
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
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
On 2/21/18 7:30 PM, SrMordred wrote: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. -SteveBut 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
On Thursday, 22 February 2018 at 02:41:30 UTC, Steven Schveighoffer wrote:On 2/21/18 7:30 PM, SrMordred wrote: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 :)Hah! I never thought of doing a slice with negative indexes ;) /SNIP -SteveBut 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 22 2018
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
On Thursday, 22 February 2018 at 00:13:43 UTC, SrMordred wrote:string x = "123"; auto c = x.ptr; c++; writeln(c[-1]); // 1That's only happening because pointers bypass range checks.writeln(c[-1..0]); //BOOM Range violationBut with a slice negative indexes are never allowed, even on a pointer.
Feb 21 2018
On 22.02.2018 01:26, Adam D. Ruppe wrote:On Thursday, 22 February 2018 at 00:13:43 UTC, SrMordred wrote: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.string x = "123"; auto c = x.ptr; c++; writeln(c[-1]); // 1That's only happening because pointers bypass range checks.writeln(c[-1..0]); //BOOM Range violationBut with a slice negative indexes are never allowed, even on a pointer.
Feb 22 2018