Optimized Type Check
- Posted by doug edmunds <dae at WORLDACCESSNET.COM> Jul 18, 1997
- 562 views
I am looking for comments on the following code: I am trying to optimize this for speed, so reducing the number of loops, etc. is key. The type will be used with very long decimals, so floating number or integers type checks and conversions can't be used. For example, this would be valid input: "11238997598982379827340982309849509860345.23508972345234872347" Thanks. Doug Edmunds ----------code starts here---------- --objective: create a TYPE with these characteristics: -- a sequence containing: -- only the digits 1-9, optional minus sign, optional decimal point -- no sequences-within-sequences --rules for the string: --1. minus sign, if any, must be in front of digits --2. there must be at least 1 digit --3. there must be at most 1 decimal point --4. there can be no spaces in the string --Examples --valid: "123." "-23.45" "666" ".00004" --not valid: 123.55 not a string --not valid: "123-" minus sign in wrong place --not valid: "123a45" contains a character which is not allowed --not valid: "123.45.67" more than one decimal --not valid: "- 123.45" contains a space --not valid: "" empty string --not valid: "-" no digits --not valid: {"234", "567"} more than one string type number_as_string (sequence input) integer decimal_points sequence valid_elements valid_elements = "-.0123456789" if length (input) = 0 then --cannot be empty return 0 end if -- test for "." and "-" only if length (input) = 1 and find(input[1], "-.") then return 0 end if --do one pass over the string, if possible, for speed decimal_points = 0 for x = 1 to length(input) do if not find(input[x], valid_elements) then return 0 end if if input[x] = '.' then -- count the optional decimal decimal_points = decimal_points +1 if decimal_points > 1 then return 0 end if end if if input[x] = '-' and x > 1 then --position of optional hyphen return 0 end if end for return 1 end type -------------------------- --test-zone procedure test_run(number_as_string test_value) printf(1, "\n%s passed.\n", {test_value}) end procedure sequence input_pattern input_pattern = "-123.456" --<< change this value mercilessly! --input_pattern = {52,53} --this also works. test_run(input_pattern)