comparison scripts/strings/validatestring.m @ 18714:d227178a0d40

validatestring.m: Change error message to follow Octave guidelines (bug #41484). * validatestring.m: Adapted the error messages to not mention validatestring when not needed. Modified %!tests to comply with the new output. * contributors.in: Add Eugenio Gianniti to list of contributors.
author Eugenio Gianniti <eugenio.gianniti@mail.polimi.it>
date Fri, 28 Feb 2014 19:24:10 +0100
parents d63878346099
children efd5cf93013b
comparison
equal deleted inserted replaced
18711:cd83730e5e38 18714:d227178a0d40
98 endif 98 endif
99 99
100 ## Make static part of error string that uses funcname, varname, and position 100 ## Make static part of error string that uses funcname, varname, and position
101 errstr = ""; 101 errstr = "";
102 if (! isempty (funcname)) 102 if (! isempty (funcname))
103 errstr = sprintf ("Function: %s ", funcname); 103 errstr = [funcname ": "];
104 endif 104 endif
105 if (! isempty (varname)) 105 if (! isempty (varname))
106 errstr = sprintf ("%sVariable: %s ", errstr, varname); 106 errstr = [errstr varname " "];
107 else
108 errstr = sprintf ("%s'%s' ", errstr, str);
107 endif 109 endif
108 if (position > 0) 110 if (position > 0)
109 errstr = sprintf ("%sArgument position %d ", errstr, position); 111 errstr = sprintf ("%s(argument #%i) ", errstr, position);
110 endif
111 if (! isempty (errstr))
112 errstr(end:end+1) = ":\n";
113 endif 112 endif
114 113
115 matches = strncmpi (str, strarray(:), length (str)); 114 matches = strncmpi (str, strarray(:), length (str));
116 nmatches = sum (matches); 115 nmatches = sum (matches);
117 if (nmatches == 0) 116 if (nmatches == 0)
118 error ("validatestring: %s'%s' does not match any of\n%s", errstr, str, 117 error ("%sdoes not match any of\n%s", errstr,
119 sprintf ("%s, ", strarray{:})(1:end-2)); 118 sprintf ("%s, ", strarray{:})(1:end-2));
120 elseif (nmatches == 1) 119 elseif (nmatches == 1)
121 str = strarray{matches}; 120 str = strarray{matches};
122 else 121 else
123 ## Are the matches substrings of each other? 122 ## Are the matches substrings of each other?
128 short_str = strarray{match_idx(min_idx)}; 127 short_str = strarray{match_idx(min_idx)};
129 submatch = strncmpi (short_str, strarray(match_idx), min_len); 128 submatch = strncmpi (short_str, strarray(match_idx), min_len);
130 if (all (submatch)) 129 if (all (submatch))
131 str = short_str; 130 str = short_str;
132 else 131 else
133 error ("validatestring: %smultiple unique matches were found for '%s':\n%s", 132 error ("%sallows multiple unique matches:\n%s",
134 errstr, str, sprintf ("%s, ", strarray{match_idx})(1:end-2)); 133 errstr, sprintf ("%s, ", strarray{match_idx})(1:end-2));
135 endif 134 endif
136 endif 135 endif
137 136
138 endfunction 137 endfunction
139 138
145 %!assert (validatestring ("octa", strarray), "octave") 144 %!assert (validatestring ("octa", strarray), "octave")
146 %! strarray = {"abc1" "def" "abc2"}; 145 %! strarray = {"abc1" "def" "abc2"};
147 %!assert (validatestring ("d", strarray), "def") 146 %!assert (validatestring ("d", strarray), "def")
148 147
149 %!error <'xyz' does not match any> validatestring ("xyz", strarray) 148 %!error <'xyz' does not match any> validatestring ("xyz", strarray)
150 %!error <Function: DUMMY_TEST> validatestring ("xyz", strarray, "DUMMY_TEST") 149 %!error <DUMMY_TEST: 'xyz' does not> validatestring ("xyz", strarray, "DUMMY_TEST")
151 %!error <Function: DUMMY_TEST Variable: DUMMY_VAR:> validatestring ("xyz", strarray, "DUMMY_TEST", "DUMMY_VAR") 150 %!error <DUMMY_TEST: DUMMY_VAR does> validatestring ("xyz", strarray, "DUMMY_TEST", "DUMMY_VAR")
152 %!error <Function: DUMMY_TEST Variable: DUMMY_VAR Argument position 5> validatestring ("xyz", strarray, "DUMMY_TEST", "DUMMY_VAR", 5) 151 %!error <DUMMY_TEST: DUMMY_VAR \(argument #5\) does> validatestring ("xyz", strarray, "DUMMY_TEST", "DUMMY_VAR", 5)
153 %!error <multiple unique matches were found for 'abc'> validatestring ("abc", strarray) 152 %!error <'abc' allows multiple unique matches> validatestring ("abc", strarray)
154 153
155 %% Test input validation 154 %% Test input validation
156 %!error validatestring ("xyz") 155 %!error validatestring ("xyz")
157 %!error validatestring ("xyz", {"xyz"}, "3", "4", 5, 6) 156 %!error validatestring ("xyz", {"xyz"}, "3", "4", 5, 6)
158 %!error <invalid number of character inputs> validatestring ("xyz", {"xyz"}, "3", "4", "5") 157 %!error <invalid number of character inputs> validatestring ("xyz", {"xyz"}, "3", "4", "5")