comparison scripts/strings/validatestring.m @ 7658:1ce6460aebdf

nargoutchk.m, validatestring.m, addtodate.m: new functions
author bill@denney.ws
date Thu, 27 Mar 2008 15:14:10 -0400
parents
children e07e93c04080
comparison
equal deleted inserted replaced
7657:76e7548add3f 7658:1ce6460aebdf
1 ## Copyright (C) 2008 Bill Denney
2 ##
3 ## This file is part of Octave.
4 ##
5 ## Octave is free software; you can redistribute it and/or modify it
6 ## under the terms of the GNU General Public License as published by
7 ## the Free Software Foundation; either version 3 of the License, or (at
8 ## your option) any later version.
9 ##
10 ## Octave is distributed in the hope that it will be useful, but
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ## General Public License for more details.
14 ##
15 ## You should have received a copy of the GNU General Public License
16 ## along with Octave; see the file COPYING. If not, see
17 ## <http://www.gnu.org/licenses/>.
18
19 ## -*- texinfo -*-
20 ## @deftypefn {Function File} {@var{validstr} =} validatestring (@var{str}, @var{strarray})
21 ## @deftypefnx {Function File} {@var{validstr} =} validatestring (@var{str}, @var{strarray}, @var{funname})
22 ## @deftypefnx {Function File} {@var{validstr} =} validatestring (@var{str}, @var{strarray}, @var{funname}, @var{varname})
23 ## @deftypefnx {Function File} {@var{validstr} =} validatestring (@dots{}, @var{position})
24 ## Verify that @var{str} is a string or substring of an element of
25 ## @var{strarray}.
26 ##
27 ## @var{str} is a character string to be tested, and @var{strarray} is a
28 ## cellstr of valid values. @var{validstr} will be the validated form
29 ## of @var{str} where validation is defined as @var{str} being a member
30 ## or substring of @var{validstr}. If @var{str} is a substring of
31 ## @var{validstr} and there are multiple matches, the shortest match
32 ## will be returned if all matches are substrings of each other, and an
33 ## error will be raised if the matches are not substrings of each other.
34 ##
35 ## All comparisons are case insensitive.
36 ## @seealso{strcmp, strcmpi}
37 ## @end deftypefn
38
39 ## Author: Bill Denney <bill@denney.ws>
40
41 function str = validatestring (str, strarray, varargin)
42
43 if (nargin < 2 || nargin > 5)
44 print_usage ();
45 endif
46
47 ## set the defaults
48 funname = "";
49 varname = "";
50 position = 0;
51 ## set the actual values
52 if (! isempty (varargin))
53 if (isnumeric (varargin{end}))
54 position = varargin{end};
55 varargin(end) = [];
56 endif
57 endif
58 funnameset = false;
59 varnameset = false;
60 for i = 1:numel (varargin)
61 if (ischar (varargin{i}))
62 if (varnameset)
63 error ("validatestring: invalid number of character inputs: %d",
64 numel (varargin));
65 elseif (funnameset)
66 varname = varargin{i};
67 varnameset = true;
68 else
69 funname = varargin{i};
70 funnameset = true;
71 endif
72 endif
73 endfor
74
75 ## Check the inputs
76 if (! ischar (str))
77 error ("validatestring: str must be a character string")
78 elseif (rows (str) != 1)
79 error ("validatestring: str must have only one row")
80 elseif (! iscellstr (strarray))
81 error ("validatestring: strarray must be a cellstr")
82 elseif (! ischar (funname))
83 error ("validatestring: funname must be a character string")
84 elseif (! isempty (funname) && (rows (funname) != 1))
85 error ("validatestring: funname must be exactly one row")
86 elseif (! ischar (varname))
87 error ("validatestring: varname must be a character string")
88 elseif (! isempty (varname) && (rows (varname) != 1))
89 error ("validatestring: varname must be exactly one row")
90 elseif (position < 0)
91 error ("validatestring: position must be >= 0")
92 endif
93
94 ## make the part of the error that will use funname, varname, and
95 ## position
96 errstr = "";
97 if (! isempty (funname))
98 errstr = sprintf ("Function: %s ", funname);
99 endif
100 if (! isempty (varname))
101 errstr = sprintf ("%sVariable: %s ", errstr, varname);
102 endif
103 if (position > 0)
104 errstr = sprintf ("%sArgument position %d ", errstr, position);
105 endif
106 if (! isempty (errstr))
107 errstr(end:end+1) = ":\n";
108 endif
109
110 matches = strncmpi (str, strarray(:), numel (str));
111 nmatches = sum (matches);
112 if (nmatches == 1)
113 str = strarray{matches};
114 elseif (nmatches == 0)
115 error ("validatestring: %s%s does not match any of\n%s", errstr, str,
116 sprintf ("%s, ", strarray{:})(1:end-1));
117 else
118 ## are the matches a substring of each other, if so, choose the
119 ## shortest. If not, raise an error.
120 match_idx = find (matches);
121 match_l = cellfun (@length, strarray(match_idx));
122 longest_idx = find (match_l == max (match_l), 1);
123 shortest_idx = find (match_l == min (match_l), 1);
124 longest = strarray(match_idx)(longest_idx);
125 for i = 1:numel(match_idx)
126 currentmatch = strarray(match_idx(i));
127 if (! strncmpi (longest, currentmatch, length(currentmatch)))
128 error ("validatestring: %smultiple unique matches were found for %s:\n%s",
129 errstr, sprintf ("%s, ", strarray(match_idx))(1:end-2));
130 endif
131 endfor
132 str = strarray{shortest_idx};
133 endif
134
135 endfunction
136
137 ## Tests
138 %!shared strarray
139 %! strarray = {"octave" "Oct" "octopus" "octaves"};
140 %!assert (validatestring ("octave", strarray), "octave")
141 %!assert (validatestring ("oct", strarray), "Oct")
142 %!assert (validatestring ("octave", strarray), "octave")
143 %!assert (validatestring ("octav", strarray), "octave")