[Issue 15710] Replacement for std.utf.validate which does not throw

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sat Mar 31 18:33:05 UTC 2018


https://issues.dlang.org/show_bug.cgi?id=15710

Seb <greensunny12 at gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |bootcamp
                 CC|                            |greensunny12 at gmail.com

--- Comment #1 from Seb <greensunny12 at gmail.com> ---
Yeah, I would be really cool if I don't have to do such ugly hacks when I just
want to handle invalid UTF.

---
private string toValidUTF(string s)
{
        import std.algorithm.iteration : map;
        import std.range : iota;
        import std.utf;
        return s.representation.length
                .iota
                .map!(i => s.decode!(UseReplacementDchar.yes)(i))
                .toUTF8;
}

try {
        outStream.validate;
} catch (UTFException) {
        outStream = outStream.toValidUTF;
}
---

--


More information about the Digitalmars-d-bugs mailing list