# HG changeset patch # User Rik # Date 1318293081 25200 # Node ID e84d512b2438dcaa42fe67e993bc2596b7ee2159 # Parent d590d9df5596caef2b1d74f7bcc3f52ecc98cadd isstrprop.m: Put input validation first and add a few more tests. * isstrprop.m: Put input validation first and add a few more tests. diff -r d590d9df5596 -r e84d512b2438 scripts/strings/isstrprop.m --- a/scripts/strings/isstrprop.m Mon Oct 10 15:08:48 2011 -0400 +++ b/scripts/strings/isstrprop.m Mon Oct 10 17:31:21 2011 -0700 @@ -84,47 +84,52 @@ function retval = isstrprop (str, prop) - if (nargin == 2) - switch (prop) - case "alpha" - retval = isalpha (str); - case {"alnum", "alphanum"} - retval = isalnum (str); - case "ascii" - retval = isascii (str); - case "cntrl" - retval = iscntrl (str); - case "digit" - retval = isdigit (str); - case {"graph", "graphic"} - retval = isgraph (str); - case "lower" - retval = islower (str); - case "print" - retval = isprint (str); - case "punct" - retval = ispunct (str); - case {"space", "wspace"} - retval = isspace (str); - case "upper" - retval = isupper (str); - case "xdigit" - retval = isxdigit (str); - otherwise - error ("isstrprop: invalid string property"); - endswitch - else + if (nargin != 2) print_usage (); endif + switch (prop) + case "alpha" + retval = isalpha (str); + case {"alnum", "alphanum"} + retval = isalnum (str); + case "ascii" + retval = isascii (str); + case "cntrl" + retval = iscntrl (str); + case "digit" + retval = isdigit (str); + case {"graph", "graphic"} + retval = isgraph (str); + case "lower" + retval = islower (str); + case "print" + retval = isprint (str); + case "punct" + retval = ispunct (str); + case {"space", "wspace"} + retval = isspace (str); + case "upper" + retval = isupper (str); + case "xdigit" + retval = isxdigit (str); + otherwise + error ("isstrprop: invalid string property"); + endswitch + endfunction -%!error isstrprop ("abc123", "foo") + %!assert (isstrprop ("abc123", "alpha"), logical ([1, 1, 1, 0, 0, 0])) +%!assert (isstrprop ("abc123", "digit"), logical ([0, 0, 0, 1, 1, 1])) %!assert (isstrprop ("Hello World", "wspace"), isspace ("Hello World")) %!assert (isstrprop ("Hello World", "graphic"), isgraph ("Hello World")) +%!assert (isstrprop (char ("AbC", "123"), "upper"), logical ([1 0 1; 0 0 0])) +%!assert (isstrprop ({"AbC", "123"}, "lower"), {logical([0 1 0]), logical([0 0 0])}) %%Input Validation %!error isstrprop () %!error isstrprop ("abc123") %!error isstrprop ("abc123", "alpha", "alpha") +%!error isstrprop ("abc123", "foo") +