string need to be robust

KennyTM~ kennytm at gmail.com
Sun Mar 13 15:36:31 PDT 2011


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)
     }
}
-----------


More information about the Digitalmars-d mailing list