string need to be robust

spir denis.spir at gmail.com
Sun Mar 13 05:32:34 PDT 2011


On 03/13/2011 12:34 PM, ZY Zhou wrote:
> std.utf throw exception instead of crash the program. but you still need to add
> try/catch everywhere.
>
> My point is: this simple code should work, instead of crash, it is supposed to
> leave all invalid codes untouched and just process the valid parts.
>
> Stream file = new BufferedFile("sample.txt");
> foreach(char[] line; file) {
>     string s = line.idup.tolower;
> }

The line reader /must/ crash or throw if the source is invalid. If you do not 
want to pre-process the source (and convert to surrogates as you do, which IMO 
is not a very good idea) /before/ running the process loop, then read the 
source as plain byte[], check and pre-process invalid text in this form, and 
then only convert to string to be able to use string functions.

There is no builtin type in D that takes random input as supposed string (text) 
/without/ checking it, and still allows to use string operations on it as if it 
were checked to be valid. This would be foolish, don't you think?
The D way to check the source of strings at construction time allows checking 
only once, then safely apply any number of operations. The opposite would 
require to check at each oeration. And would still throw or crash, which is not 
what you expect, I guess: you would like IIUC invalid input to be ignored.

Denis
-- 
_________________
vita es estrany
spir.wikidot.com



More information about the Digitalmars-d mailing list