string need to be robust

Jacob Carlborg doob at me.com
Mon Mar 14 06:56:28 PDT 2011


On 2011-03-13 23:36, KennyTM~ wrote:
> On Mar 14, 11 02:55, Jacob Carlborg wrote:
>> I would say that the functions should NOT crash but instead throw an
>> exception. Then the developer can choose what to do when there's an
>> invalid unicode character.
>
> It is already throwing an exception called
> core.exception.UnicodeException. This even provides you the index where
> decoding failed.
>
> (However Phobos is not using it, AFAIK.)
>
> -----------
> import core.exception, std.stdio, std.conv;
>
> void main() {
> char[] s = [0x0f, 0x12,0x34,0x56,0x78,0x9a,0xbc];
> try {
> foreach (dchar d; s){}
> } catch (UnicodeException e) {
> writefln("error at index %s (%x)", e.idx, to!ubyte(s[e.idx]));
> // error at index 5 (9a)
> }
> }
> -----------

Good, then we can call it a day :) I assumed it crashed since he said it 
did.

-- 
/Jacob Carlborg


More information about the Digitalmars-d mailing list