[REBOL] Re: Load of trouble?
From: sanghabum:aol at: 1-Dec-2001 15:04
Hi Gabrielle,
> When handling dates, I'd suggest to use TO-DATE instead: it handles
> much more formats than LOAD and just gives an error if the input
> is not a date.
< snip>
> Romano's suggestion is useful if the user could input values of
> different datatypes (imagine accepting a file or an url), but if
> you know the type in advance, TO is the best IMHO. (Of course,
> you'll need to do your own checks for certain types, such as all
> of the any-string!s...)
Thanks for the analysis. Romano abd Brett's responses were pretty close to
what I wanted.....
That's a generic validation process for raw strings that does three things:
1. Convert string to its internal Rebol type
2. Ensure that type is the right type for the data field
3. Apply field-specific rules to ensure the content is right for the field
and the
data type. (So, for example, a date field may be blank; have a date; or have
any of the strings "Today" "Tomorrow" "Soonest".
Using everything I've learn on the list in the last few days, I can do the
first two with this little function (please suggest improvements!)
==============
is-this?: func [ "Checks and cleans a string"
Types [block!] "List of acceptable Rebol"
Raw-data [string!] "Item to be checked"
/local Block-data Clean-Data
]
[
;; Returns: either Raw-data converted to its Rebol datatype, if possible
;; or False - if it is not one of the types on the list
Block-data: copy []
error? try [block-data: to-block Raw-Data]
either (length? Block-data) = 1
[Clean-data: first Block-data]
[Clean-data: Raw-data]
foreach RebolType Types
[if (type? Clean-data) = get to-word RebolType
[Return Clean-data]
] ; for
Return False
] ; func
============
Examples of use:
is-this? [date!] " 24/6/44 "
is-this? [date! tuple!] " 10.08.1976"
is-this? [url! email!] " ftp://[dorothy--somewhere--otr]"
is-this? [integer!] " 45.5"
is-this? [integer! decimal!] " 45.5"
Thanks again,
--Colin.