[REBOL] Re: [load vs load/all] [how to//handle untrusted data] load v loa...
From: SunandaDH::aol::com at: 18-Nov-2004 10:52
Peter:
> 4. It is advisable to wrap the to-block or load of untrusted data in an
> error/try block as some strings will give problems. For example:
That's a good summary. Thanks for doing that.
Another technique that has it's uses is simply trying to convert the
untrusted string directly into the type you want:
internal-item: to date! "a string"
This needs error trapping as it can fail on bad strings. And you need to test
the end result to be none, meaning the conversion failed.
Here's a reconstructed function based on something half-remembered on the ML
for converting a data item into an expected datatype:
to-type: func [dt [datatype! block!] item [string!]
/local temp
][
if not block? dt
[dt: copy reduce [dt]]
trim/lines item
foreach dte reduce dt
[
error? try
[
temp: to dte item
if not none? temp [return temp]
]
]
return none
]
Uses:
>>print to-type date! " 4-6-04 "
4-Jun-2004 ;; it's a date
>>print to-type date! " 404 "
none ;; it's not a date
>> print to-type [pair! date!] " 4-6-04"
4-Jun-2004 ;; it's a date
>> print to-type [pair! date!] " 66x66 "
66x66 ;; it's a pair
>> print to-type [pair! date!] " 3.14159 "
none ;; not a date or a pair
Sunanda