List of Phobos functions that allocate memory?

Jonathan M Davis jmdavisProg at gmx.com
Fri Feb 7 00:14:09 PST 2014


On Thursday, February 06, 2014 22:20:37 Dicebot wrote:
> On Thursday, 6 February 2014 at 22:18:10 UTC, Brad Anderson wrote:
> > You should probably validate utf from all foreign sources.
> > Catch a problem with it as it comes in rather than in some
> > arbitrary part of your program.
> > 
> > http://dlang.org/phobos/std_utf.html#.validate
> 
> pure @safe void validate(S)(in S str) if (isSomeString!S);
> 
> Throws:
> UTFException if str is not well-formed.
> 
> ;)

In general, I think that throwing on malformed Unicode is a good thing, 
because it results in code that's less error-prone (as the alternative is to 
not validate Unicode and try and continue somehow regardless of bad input when 
decoding Unicode, which would be very bad IMHO). That being said, validating 
strings when they enter the program is a good way to localize any failures - 
which is where validate would come in - and I have to agree that the fact that 
validate throws is horrific. It's a classic example of a function that should 
return a bool rather than throw. You're asking it whether the string is valid, 
not asking to report errors when your normal control flow encounters an error 
that prevents it from functioning normally (which is where exceptions should 
normally be used).

As such, I think that it's clear that we need a new function to replace it 
(e.g. isValidUnicode). I'll have to take a look at it. If I'm lucky, it won't 
even take all that long to implement.

- Jonathan M Davis


More information about the Digitalmars-d mailing list