comparison scripts/strings/validatestring.m @ 17405:3f0ed69d21c6

Replace unnecessary instances of strncmp with strcmp. * scripts/optimization/optimget.m, scripts/plot/__gnuplot_drawnow__.m, scripts/plot/fill.m, scripts/plot/private/__go_draw_axes__.m, scripts/plot/private/__pie__.m, scripts/plot/private/__print_parse_opts__.m, scripts/plot/private/__quiver__.m, scripts/plot/private/__tight_eps_bbox__.m, scripts/plot/stairs.m, scripts/plot/uigetfile.m, scripts/strings/strsplit.m, scripts/strings/validatestring.m: Replace unnecessary instances of strncmp with strcmp.
author Rik <rik@octave.org>
date Tue, 10 Sep 2013 18:31:39 -0700
parents bc924baa2c4e
children d63878346099
comparison
equal deleted inserted replaced
17404:5e552cd9315a 17405:3f0ed69d21c6
50 ## validatestring ("b", @{"red", "green", "blue", "black"@}) 50 ## validatestring ("b", @{"red", "green", "blue", "black"@})
51 ## @result{} error: validatestring: multiple unique matches were found for 'b': 51 ## @result{} error: validatestring: multiple unique matches were found for 'b':
52 ## blue, black 52 ## blue, black
53 ## @end group 53 ## @end group
54 ## @end smallexample 54 ## @end smallexample
55 ## 55 ##
56 ## @seealso{strcmp, strcmpi} 56 ## @seealso{strcmp, strcmpi}
57 ## @end deftypefn 57 ## @end deftypefn
58 58
59 ## Author: Bill Denney <bill@denney.ws> 59 ## Author: Bill Denney <bill@denney.ws>
60 60
68 ## Process input arguments 68 ## Process input arguments
69 if (! isempty (varargin) && isnumeric (varargin{end})) 69 if (! isempty (varargin) && isnumeric (varargin{end}))
70 position = varargin{end}; 70 position = varargin{end};
71 varargin(end) = []; 71 varargin(end) = [];
72 endif 72 endif
73 73
74 funcname = varname = ""; 74 funcname = varname = "";
75 char_idx = cellfun ("isclass", varargin, "char"); 75 char_idx = cellfun ("isclass", varargin, "char");
76 n_chararg = sum (char_idx); 76 n_chararg = sum (char_idx);
77 if (n_chararg > 2) 77 if (n_chararg > 2)
78 error ("validatestring: invalid number of character inputs (3)"); 78 error ("validatestring: invalid number of character inputs (3)");
110 endif 110 endif
111 if (! isempty (errstr)) 111 if (! isempty (errstr))
112 errstr(end:end+1) = ":\n"; 112 errstr(end:end+1) = ":\n";
113 endif 113 endif
114 114
115 matches = strncmpi (str, strarray(:), numel (str)); 115 matches = strncmpi (str, strarray(:), length (str));
116 nmatches = sum (matches); 116 nmatches = sum (matches);
117 if (nmatches == 0) 117 if (nmatches == 0)
118 error ("validatestring: %s'%s' does not match any of\n%s", errstr, str, 118 error ("validatestring: %s'%s' does not match any of\n%s", errstr, str,
119 sprintf ("%s, ", strarray{:})(1:end-2)); 119 sprintf ("%s, ", strarray{:})(1:end-2));
120 elseif (nmatches == 1) 120 elseif (nmatches == 1)
122 else 122 else
123 ## Are the matches substrings of each other? 123 ## Are the matches substrings of each other?
124 ## If true, choose the shortest. If not, raise an error. 124 ## If true, choose the shortest. If not, raise an error.
125 match_idx = find (matches); 125 match_idx = find (matches);
126 match_len = cellfun ("length", strarray(match_idx)); 126 match_len = cellfun ("length", strarray(match_idx));
127 [min_len, min_idx] = min (match_len); 127 [min_len, min_idx] = min (match_len);
128 short_str = strarray{match_idx(min_idx)}; 128 short_str = strarray{match_idx(min_idx)};
129 submatch = strncmpi (short_str, strarray(match_idx), min_len); 129 submatch = strncmpi (short_str, strarray(match_idx), min_len);
130 if (all (submatch)) 130 if (all (submatch))
131 str = short_str; 131 str = short_str;
132 else 132 else
133 error ("validatestring: %smultiple unique matches were found for '%s':\n%s", 133 error ("validatestring: %smultiple unique matches were found for '%s':\n%s",
134 errstr, str, sprintf ("%s, ", strarray{match_idx})(1:end-2)); 134 errstr, str, sprintf ("%s, ", strarray{match_idx})(1:end-2));