UTFException when reading a file

Dennis dkorpel at gmail.com
Fri Jan 11 20:10:52 UTC 2019


On Friday, 11 January 2019 at 19:45:05 UTC, Head Scratcher wrote:
> How can I read the file and convert the string into proper 
> UTF-8 in memory without an exception?

You have multiple options:

```
import std.file: read;
import std.encoding: transcode, Windows1252String;
auto ansiStr = cast(Windows1252String) read(filename);
string utf8string;
transcode(ansiStr, utf8string);
```

If it's ANSI.

```
import std.encoding: sanitize;
auto sanitized =  (cast(string) read(filename)).sanitize;
```

If it's incorrect UTF8, eager

```
import std.exception: handle;
import std.range;
auto handled = str.handle!(UTFException, RangePrimitive.access,
         (e, r) => ' '); // Replace invalid code points with spaces
```

If it's incorrect UTF8, lazy

See:
https://dlang.org/phobos/std_encoding.html#transcode
https://dlang.org/phobos/std_exception.html#handle


More information about the Digitalmars-d-learn mailing list