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
-- 
To unsubscribe from the list, just send an email to rebol-request
at rebol.com with unsubscribe as the subject.

Reply via email to