dmd foreach loops throw exceptions on invalid UTF sequences, use replacementDchar instead

jfondren julian.fondren at gmail.com
Fri Nov 5 14:22:14 UTC 2021


On Friday, 5 November 2021 at 14:10:35 UTC, deadalnix wrote:
> On Friday, 5 November 2021 at 13:26:00 UTC, Andrei Alexandrescu 
> wrote:
>> "On demand" is not "auto".
>
> From the bug repport:
>
>> A simple foreach loop:
>> 
>>     void test(char[] a)
>>     {
>>         foreach (char c; a) { }
>>     }
>> 
>> will throw a UtfException if `a` is not a valid UTF string. 
>> Instead, it should replace the invalid sequence with 
>> replacementDchar.
>
> This shouldn't do anything related to unicode at all.

It doesn't. This does:

```d
unittest {
     enum invalid = "hello\247\205\257there";
     foreach (dchar c; invalid) { }
}
```

Looping over the dchar of a char[] requires one of

1. throwing an error on invalid UTF (current behavior)
2. doing something else in that case (proposed: replacementDchar; 
also possible: silently doing something invalid like iterating 
over three dchars between "hello" and "there")
3. a compile-time error (also proposed in the thread)


More information about the Digitalmars-d mailing list