www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Taking a slice of a string: Correct behavior?

reply bachmeier <no spam.net> writes:
I'm trying to understand why this code returns an empty string 
rather than an out of bounds error:

```
writeln("this"[4..$]); // Empty string
```

But

```
writeln("this"[4]);
```

won't compile. I thought maybe it had something to do with the 
`$` or the `..` operator, but `"this"[4..4]` compiles and runs 
while `"this"[5..5]` does not. Therefore it's not the fact that 
there are no elements in the slice.

On the other hand, `"this"[3..3]` also returns an empty string. 
That still doesn't explain why there is no out of bounds error 
for `"this"[4..4]`.
Aug 11 2023
next sibling parent Steven Schveighoffer <schveiguy gmail.com> writes:
On 8/11/23 12:04 PM, bachmeier wrote:
 I'm trying to understand why this code returns an empty string rather 
 than an out of bounds error:
 
 ```
 writeln("this"[4..$]); // Empty string
 ```
 
 But
 
 ```
 writeln("this"[4]);
 ```
 
 won't compile. I thought maybe it had something to do with the `$` or 
 the `..` operator, but `"this"[4..4]` compiles and runs while 
 `"this"[5..5]` does not. Therefore it's not the fact that there are no 
 elements in the slice.
 
 On the other hand, `"this"[3..3]` also returns an empty string. That 
 still doesn't explain why there is no out of bounds error for 
 `"this"[4..4]`.
In a slicing operation, the second index is one index *beyond* the data you are looking to slice. In other words, the upper bound is *exclusive* of the data. The data looks like this: ``` data: [ t h i s ] index: 0 1 2 3 4 ``` So "this"[1 .. 3] will do it like this: ``` data: [ t h i s ] index: 0 1 2 3 4 slice: [h i ] ``` If you slice 4 .. 4, then you get: ``` data: [ t h i s ] index: 0 1 2 3 4 slice: [] ``` This is allowed, because the pointer is still within bounds of the array. If you slice 5 .. 5, that is out of bounds of the entire array, and even though you aren't getting any data, the pointer will point beyond the scope of the array, and isn't allowed. -Steve
Aug 11 2023
prev sibling parent Salih Dincer <salihdb hotmail.com> writes:
On Friday, 11 August 2023 at 16:04:21 UTC, bachmeier wrote:
 I'm trying to understand why this code returns an empty string 
 rather than an out of bounds error:

 ```d
 writeln("this"[4..$]); // Empty string
 ```
Something goes wrong is indeed a 2nd degree problem. The important thing is that slicing a string might be wrong if it contains UTF. SDB 79
Aug 11 2023