Thread overview | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
February 22, 2018 Negative index range violation | ||||
---|---|---|---|---|
| ||||
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 ? |
February 22, 2018 Re: Negative index range violation | ||||
---|---|---|---|---|
| ||||
Posted in reply to SrMordred | 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];
|
February 22, 2018 Re: Negative index range violation | ||||
---|---|---|---|---|
| ||||
Posted in reply to SrMordred | 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. |
February 22, 2018 Re: Negative index range violation | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nicholas Wilson | > 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 :) |
February 21, 2018 Re: Negative index range violation | ||||
---|---|---|---|---|
| ||||
Posted in reply to SrMordred | 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
|
February 22, 2018 Re: Negative index range violation | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | 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
:)
|
February 22, 2018 Re: Negative index range violation | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | 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.
|
February 22, 2018 Re: Negative index range violation | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | 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.
:)
|
Copyright © 1999-2021 by the D Language Foundation