annotate scripts/strings/validatestring.m @ 33625:d213a148b3f1 default tip

ensure exp. terminal widget has focus at startup * main-window.cc (main_window): call focus_command_window only if event loop is idle by using a single shot timer * main-window.h: make focus_command_window a public slot
author Torsten Lilge <ttl-octave@mailbox.org>
date Sun, 26 May 2024 02:29:44 +0200
parents 2e484f9f1f18
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
1 ########################################################################
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
2 ##
32632
2e484f9f1f18 maint: update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents: 31706
diff changeset
3 ## Copyright (C) 2008-2024 The Octave Project Developers
27918
b442ec6dda5c use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
4 ##
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
5 ## See the file COPYRIGHT.md in the top-level directory of this
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
6 ## distribution or <https://octave.org/copyright/>.
7658
1ce6460aebdf nargoutchk.m, validatestring.m, addtodate.m: new functions
bill@denney.ws
parents:
diff changeset
7 ##
1ce6460aebdf nargoutchk.m, validatestring.m, addtodate.m: new functions
bill@denney.ws
parents:
diff changeset
8 ## This file is part of Octave.
1ce6460aebdf nargoutchk.m, validatestring.m, addtodate.m: new functions
bill@denney.ws
parents:
diff changeset
9 ##
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 23220
diff changeset
10 ## Octave is free software: you can redistribute it and/or modify it
7658
1ce6460aebdf nargoutchk.m, validatestring.m, addtodate.m: new functions
bill@denney.ws
parents:
diff changeset
11 ## under the terms of the GNU General Public License as published by
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 23220
diff changeset
12 ## the Free Software Foundation, either version 3 of the License, or
22755
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22323
diff changeset
13 ## (at your option) any later version.
7658
1ce6460aebdf nargoutchk.m, validatestring.m, addtodate.m: new functions
bill@denney.ws
parents:
diff changeset
14 ##
1ce6460aebdf nargoutchk.m, validatestring.m, addtodate.m: new functions
bill@denney.ws
parents:
diff changeset
15 ## Octave is distributed in the hope that it will be useful, but
1ce6460aebdf nargoutchk.m, validatestring.m, addtodate.m: new functions
bill@denney.ws
parents:
diff changeset
16 ## WITHOUT ANY WARRANTY; without even the implied warranty of
22755
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22323
diff changeset
17 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22323
diff changeset
18 ## GNU General Public License for more details.
7658
1ce6460aebdf nargoutchk.m, validatestring.m, addtodate.m: new functions
bill@denney.ws
parents:
diff changeset
19 ##
1ce6460aebdf nargoutchk.m, validatestring.m, addtodate.m: new functions
bill@denney.ws
parents:
diff changeset
20 ## You should have received a copy of the GNU General Public License
1ce6460aebdf nargoutchk.m, validatestring.m, addtodate.m: new functions
bill@denney.ws
parents:
diff changeset
21 ## along with Octave; see the file COPYING. If not, see
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 23220
diff changeset
22 ## <https://www.gnu.org/licenses/>.
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
23 ##
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
24 ########################################################################
7658
1ce6460aebdf nargoutchk.m, validatestring.m, addtodate.m: new functions
bill@denney.ws
parents:
diff changeset
25
1ce6460aebdf nargoutchk.m, validatestring.m, addtodate.m: new functions
bill@denney.ws
parents:
diff changeset
26 ## -*- texinfo -*-
20852
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 20726
diff changeset
27 ## @deftypefn {} {@var{validstr} =} validatestring (@var{str}, @var{strarray})
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 20726
diff changeset
28 ## @deftypefnx {} {@var{validstr} =} validatestring (@var{str}, @var{strarray}, @var{funcname})
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 20726
diff changeset
29 ## @deftypefnx {} {@var{validstr} =} validatestring (@var{str}, @var{strarray}, @var{funcname}, @var{varname})
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 20726
diff changeset
30 ## @deftypefnx {} {@var{validstr} =} validatestring (@dots{}, @var{position})
13707
35f7cbf09c77 validatestring.m: Overhaul code for Matlab compatability and performance.
Rik <octave@nomad.inbox5.com>
parents: 12931
diff changeset
31 ## Verify that @var{str} is an element, or substring of an element, in
7658
1ce6460aebdf nargoutchk.m, validatestring.m, addtodate.m: new functions
bill@denney.ws
parents:
diff changeset
32 ## @var{strarray}.
1ce6460aebdf nargoutchk.m, validatestring.m, addtodate.m: new functions
bill@denney.ws
parents:
diff changeset
33 ##
13707
35f7cbf09c77 validatestring.m: Overhaul code for Matlab compatability and performance.
Rik <octave@nomad.inbox5.com>
parents: 12931
diff changeset
34 ## When @var{str} is a character string to be tested, and @var{strarray} is a
29290
d3c1e6f5ac85 validatestring.m: Overhaul function.
Rik <rik@octave.org>
parents: 27978
diff changeset
35 ## cell array of strings of valid values, then @var{validstr} will be the
d3c1e6f5ac85 validatestring.m: Overhaul function.
Rik <rik@octave.org>
parents: 27978
diff changeset
36 ## validated form of @var{str} where validation is defined as @var{str} being
d3c1e6f5ac85 validatestring.m: Overhaul function.
Rik <rik@octave.org>
parents: 27978
diff changeset
37 ## a member or substring of @var{validstr}. This is useful for both verifying
30353
212461a26e9c doc: grammarcheck m-files in scripts/ directory ahead of 7.1 release.
Rik <rik@octave.org>
parents: 29359
diff changeset
38 ## and expanding short options, such as @qcode{"r"}, to their longer forms,
212461a26e9c doc: grammarcheck m-files in scripts/ directory ahead of 7.1 release.
Rik <rik@octave.org>
parents: 29359
diff changeset
39 ## such as @qcode{"red"}. If @var{str} is a substring of @var{validstr},
212461a26e9c doc: grammarcheck m-files in scripts/ directory ahead of 7.1 release.
Rik <rik@octave.org>
parents: 29359
diff changeset
40 ## and there are multiple matches, the shortest match will be returned if
212461a26e9c doc: grammarcheck m-files in scripts/ directory ahead of 7.1 release.
Rik <rik@octave.org>
parents: 29359
diff changeset
41 ## all matches are substrings of each other. Otherwise, an error will be
212461a26e9c doc: grammarcheck m-files in scripts/ directory ahead of 7.1 release.
Rik <rik@octave.org>
parents: 29359
diff changeset
42 ## raised because the expansion of @var{str} is ambiguous. All comparisons
212461a26e9c doc: grammarcheck m-files in scripts/ directory ahead of 7.1 release.
Rik <rik@octave.org>
parents: 29359
diff changeset
43 ## are case insensitive.
13707
35f7cbf09c77 validatestring.m: Overhaul code for Matlab compatability and performance.
Rik <octave@nomad.inbox5.com>
parents: 12931
diff changeset
44 ##
35f7cbf09c77 validatestring.m: Overhaul code for Matlab compatability and performance.
Rik <octave@nomad.inbox5.com>
parents: 12931
diff changeset
45 ## The additional inputs @var{funcname}, @var{varname}, and @var{position}
35f7cbf09c77 validatestring.m: Overhaul code for Matlab compatability and performance.
Rik <octave@nomad.inbox5.com>
parents: 12931
diff changeset
46 ## are optional and will make any generated validation error message more
35f7cbf09c77 validatestring.m: Overhaul code for Matlab compatability and performance.
Rik <octave@nomad.inbox5.com>
parents: 12931
diff changeset
47 ## specific.
35f7cbf09c77 validatestring.m: Overhaul code for Matlab compatability and performance.
Rik <octave@nomad.inbox5.com>
parents: 12931
diff changeset
48 ##
35f7cbf09c77 validatestring.m: Overhaul code for Matlab compatability and performance.
Rik <octave@nomad.inbox5.com>
parents: 12931
diff changeset
49 ## Examples:
14093
050bc580cb60 doc: Various docstring improvements before 3.6.0 release.
Rik <octave@nomad.inbox5.com>
parents: 13708
diff changeset
50 ## @c Set example in small font to prevent overfull line
7658
1ce6460aebdf nargoutchk.m, validatestring.m, addtodate.m: new functions
bill@denney.ws
parents:
diff changeset
51 ##
14093
050bc580cb60 doc: Various docstring improvements before 3.6.0 release.
Rik <octave@nomad.inbox5.com>
parents: 13708
diff changeset
52 ## @smallexample
13707
35f7cbf09c77 validatestring.m: Overhaul code for Matlab compatability and performance.
Rik <octave@nomad.inbox5.com>
parents: 12931
diff changeset
53 ## @group
13708
ee28bf4c3c29 doc: Use @{ macro for curly brace in validatestring Texinfo docstring
Rik <octave@nomad.inbox5.com>
parents: 13707
diff changeset
54 ## validatestring ("r", @{"red", "green", "blue"@})
14093
050bc580cb60 doc: Various docstring improvements before 3.6.0 release.
Rik <octave@nomad.inbox5.com>
parents: 13708
diff changeset
55 ## @result{} "red"
13707
35f7cbf09c77 validatestring.m: Overhaul code for Matlab compatability and performance.
Rik <octave@nomad.inbox5.com>
parents: 12931
diff changeset
56 ##
13708
ee28bf4c3c29 doc: Use @{ macro for curly brace in validatestring Texinfo docstring
Rik <octave@nomad.inbox5.com>
parents: 13707
diff changeset
57 ## validatestring ("b", @{"red", "green", "blue", "black"@})
14093
050bc580cb60 doc: Various docstring improvements before 3.6.0 release.
Rik <octave@nomad.inbox5.com>
parents: 13708
diff changeset
58 ## @result{} error: validatestring: multiple unique matches were found for 'b':
050bc580cb60 doc: Various docstring improvements before 3.6.0 release.
Rik <octave@nomad.inbox5.com>
parents: 13708
diff changeset
59 ## blue, black
13707
35f7cbf09c77 validatestring.m: Overhaul code for Matlab compatability and performance.
Rik <octave@nomad.inbox5.com>
parents: 12931
diff changeset
60 ## @end group
14093
050bc580cb60 doc: Various docstring improvements before 3.6.0 release.
Rik <octave@nomad.inbox5.com>
parents: 13708
diff changeset
61 ## @end smallexample
17405
3f0ed69d21c6 Replace unnecessary instances of strncmp with strcmp.
Rik <rik@octave.org>
parents: 17281
diff changeset
62 ##
19069
efd5cf93013b doc: Move validatestrings into function chapter of manual.
Rik <rik@octave.org>
parents: 18579
diff changeset
63 ## @seealso{strcmp, strcmpi, validateattributes, inputParser}
7658
1ce6460aebdf nargoutchk.m, validatestring.m, addtodate.m: new functions
bill@denney.ws
parents:
diff changeset
64 ## @end deftypefn
1ce6460aebdf nargoutchk.m, validatestring.m, addtodate.m: new functions
bill@denney.ws
parents:
diff changeset
65
1ce6460aebdf nargoutchk.m, validatestring.m, addtodate.m: new functions
bill@denney.ws
parents:
diff changeset
66 function str = validatestring (str, strarray, varargin)
1ce6460aebdf nargoutchk.m, validatestring.m, addtodate.m: new functions
bill@denney.ws
parents:
diff changeset
67
1ce6460aebdf nargoutchk.m, validatestring.m, addtodate.m: new functions
bill@denney.ws
parents:
diff changeset
68 if (nargin < 2 || nargin > 5)
1ce6460aebdf nargoutchk.m, validatestring.m, addtodate.m: new functions
bill@denney.ws
parents:
diff changeset
69 print_usage ();
1ce6460aebdf nargoutchk.m, validatestring.m, addtodate.m: new functions
bill@denney.ws
parents:
diff changeset
70 endif
1ce6460aebdf nargoutchk.m, validatestring.m, addtodate.m: new functions
bill@denney.ws
parents:
diff changeset
71
29290
d3c1e6f5ac85 validatestring.m: Overhaul function.
Rik <rik@octave.org>
parents: 27978
diff changeset
72 ## Process input arguments
7658
1ce6460aebdf nargoutchk.m, validatestring.m, addtodate.m: new functions
bill@denney.ws
parents:
diff changeset
73 position = 0;
13707
35f7cbf09c77 validatestring.m: Overhaul code for Matlab compatability and performance.
Rik <octave@nomad.inbox5.com>
parents: 12931
diff changeset
74 if (! isempty (varargin) && isnumeric (varargin{end}))
19725
5f2c0ca0ef51 Ensure that numbers passed to integer *printf format codes are integers (bug #44245).
Rik <rik@octave.org>
parents: 19697
diff changeset
75 position = fix (varargin{end});
13707
35f7cbf09c77 validatestring.m: Overhaul code for Matlab compatability and performance.
Rik <octave@nomad.inbox5.com>
parents: 12931
diff changeset
76 varargin(end) = [];
29290
d3c1e6f5ac85 validatestring.m: Overhaul function.
Rik <rik@octave.org>
parents: 27978
diff changeset
77 if (position < 1)
d3c1e6f5ac85 validatestring.m: Overhaul function.
Rik <rik@octave.org>
parents: 27978
diff changeset
78 error ("validatestring: POSITION must be >= 1");
d3c1e6f5ac85 validatestring.m: Overhaul function.
Rik <rik@octave.org>
parents: 27978
diff changeset
79 endif
7658
1ce6460aebdf nargoutchk.m, validatestring.m, addtodate.m: new functions
bill@denney.ws
parents:
diff changeset
80 endif
17405
3f0ed69d21c6 Replace unnecessary instances of strncmp with strcmp.
Rik <rik@octave.org>
parents: 17281
diff changeset
81
13707
35f7cbf09c77 validatestring.m: Overhaul code for Matlab compatability and performance.
Rik <octave@nomad.inbox5.com>
parents: 12931
diff changeset
82 funcname = varname = "";
35f7cbf09c77 validatestring.m: Overhaul code for Matlab compatability and performance.
Rik <octave@nomad.inbox5.com>
parents: 12931
diff changeset
83 char_idx = cellfun ("isclass", varargin, "char");
35f7cbf09c77 validatestring.m: Overhaul code for Matlab compatability and performance.
Rik <octave@nomad.inbox5.com>
parents: 12931
diff changeset
84 n_chararg = sum (char_idx);
35f7cbf09c77 validatestring.m: Overhaul code for Matlab compatability and performance.
Rik <octave@nomad.inbox5.com>
parents: 12931
diff changeset
85 if (n_chararg > 2)
29290
d3c1e6f5ac85 validatestring.m: Overhaul function.
Rik <rik@octave.org>
parents: 27978
diff changeset
86 error ("validatestring: invalid number of character inputs (> 3)");
13707
35f7cbf09c77 validatestring.m: Overhaul code for Matlab compatability and performance.
Rik <octave@nomad.inbox5.com>
parents: 12931
diff changeset
87 elseif (n_chararg == 2)
35f7cbf09c77 validatestring.m: Overhaul code for Matlab compatability and performance.
Rik <octave@nomad.inbox5.com>
parents: 12931
diff changeset
88 [funcname, varname] = deal (varargin{char_idx});
35f7cbf09c77 validatestring.m: Overhaul code for Matlab compatability and performance.
Rik <octave@nomad.inbox5.com>
parents: 12931
diff changeset
89 elseif (n_chararg == 1)
35f7cbf09c77 validatestring.m: Overhaul code for Matlab compatability and performance.
Rik <octave@nomad.inbox5.com>
parents: 12931
diff changeset
90 funcname = varargin{char_idx};
35f7cbf09c77 validatestring.m: Overhaul code for Matlab compatability and performance.
Rik <octave@nomad.inbox5.com>
parents: 12931
diff changeset
91 endif
7658
1ce6460aebdf nargoutchk.m, validatestring.m, addtodate.m: new functions
bill@denney.ws
parents:
diff changeset
92
29290
d3c1e6f5ac85 validatestring.m: Overhaul function.
Rik <rik@octave.org>
parents: 27978
diff changeset
93 ## Validate the inputs
d3c1e6f5ac85 validatestring.m: Overhaul function.
Rik <rik@octave.org>
parents: 27978
diff changeset
94 if (! (ischar (str) && isrow (str)))
d3c1e6f5ac85 validatestring.m: Overhaul function.
Rik <rik@octave.org>
parents: 27978
diff changeset
95 error ("validatestring: STR must be a single character string");
7658
1ce6460aebdf nargoutchk.m, validatestring.m, addtodate.m: new functions
bill@denney.ws
parents:
diff changeset
96 elseif (! iscellstr (strarray))
11472
1740012184f9 Use uppercase for variable names in error() strings to match Info documentation. Only m-files done.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
97 error ("validatestring: STRARRAY must be a cellstr");
13707
35f7cbf09c77 validatestring.m: Overhaul code for Matlab compatability and performance.
Rik <octave@nomad.inbox5.com>
parents: 12931
diff changeset
98 elseif (! isempty (funcname) && ! isrow (funcname))
29290
d3c1e6f5ac85 validatestring.m: Overhaul function.
Rik <rik@octave.org>
parents: 27978
diff changeset
99 error ("validatestring: FUNCNAME must be a single character string");
13707
35f7cbf09c77 validatestring.m: Overhaul code for Matlab compatability and performance.
Rik <octave@nomad.inbox5.com>
parents: 12931
diff changeset
100 elseif (! isempty (varname) && ! isrow (varname))
29290
d3c1e6f5ac85 validatestring.m: Overhaul function.
Rik <rik@octave.org>
parents: 27978
diff changeset
101 error ("validatestring: VARNAME must be a single character string");
7658
1ce6460aebdf nargoutchk.m, validatestring.m, addtodate.m: new functions
bill@denney.ws
parents:
diff changeset
102 endif
1ce6460aebdf nargoutchk.m, validatestring.m, addtodate.m: new functions
bill@denney.ws
parents:
diff changeset
103
13707
35f7cbf09c77 validatestring.m: Overhaul code for Matlab compatability and performance.
Rik <octave@nomad.inbox5.com>
parents: 12931
diff changeset
104 ## Make static part of error string that uses funcname, varname, and position
29290
d3c1e6f5ac85 validatestring.m: Overhaul function.
Rik <rik@octave.org>
parents: 27978
diff changeset
105 if (isempty (funcname))
d3c1e6f5ac85 validatestring.m: Overhaul function.
Rik <rik@octave.org>
parents: 27978
diff changeset
106 funcname = "validatestring";
7658
1ce6460aebdf nargoutchk.m, validatestring.m, addtodate.m: new functions
bill@denney.ws
parents:
diff changeset
107 endif
29290
d3c1e6f5ac85 validatestring.m: Overhaul function.
Rik <rik@octave.org>
parents: 27978
diff changeset
108 errstr = [funcname ": '" str "' "];
7658
1ce6460aebdf nargoutchk.m, validatestring.m, addtodate.m: new functions
bill@denney.ws
parents:
diff changeset
109 if (! isempty (varname))
29290
d3c1e6f5ac85 validatestring.m: Overhaul function.
Rik <rik@octave.org>
parents: 27978
diff changeset
110 if (position > 0)
d3c1e6f5ac85 validatestring.m: Overhaul function.
Rik <rik@octave.org>
parents: 27978
diff changeset
111 errstr = sprintf ("%s(variable %s, argument #%i) ", ...
d3c1e6f5ac85 validatestring.m: Overhaul function.
Rik <rik@octave.org>
parents: 27978
diff changeset
112 errstr, varname, position);
d3c1e6f5ac85 validatestring.m: Overhaul function.
Rik <rik@octave.org>
parents: 27978
diff changeset
113 else
d3c1e6f5ac85 validatestring.m: Overhaul function.
Rik <rik@octave.org>
parents: 27978
diff changeset
114 errstr = sprintf ("%s(variable %s) ", errstr, varname);
d3c1e6f5ac85 validatestring.m: Overhaul function.
Rik <rik@octave.org>
parents: 27978
diff changeset
115 endif
18579
d227178a0d40 validatestring.m: Change error message to follow Octave guidelines (bug #41484).
Eugenio Gianniti <eugenio.gianniti@mail.polimi.it>
parents: 17744
diff changeset
116 else
29290
d3c1e6f5ac85 validatestring.m: Overhaul function.
Rik <rik@octave.org>
parents: 27978
diff changeset
117 if (position > 0)
d3c1e6f5ac85 validatestring.m: Overhaul function.
Rik <rik@octave.org>
parents: 27978
diff changeset
118 errstr = sprintf ("%s(argument #%i) ", errstr, position);
d3c1e6f5ac85 validatestring.m: Overhaul function.
Rik <rik@octave.org>
parents: 27978
diff changeset
119 endif
7658
1ce6460aebdf nargoutchk.m, validatestring.m, addtodate.m: new functions
bill@denney.ws
parents:
diff changeset
120 endif
1ce6460aebdf nargoutchk.m, validatestring.m, addtodate.m: new functions
bill@denney.ws
parents:
diff changeset
121
29290
d3c1e6f5ac85 validatestring.m: Overhaul function.
Rik <rik@octave.org>
parents: 27978
diff changeset
122 matches = strncmpi (str, strarray(:), numel (str));
7658
1ce6460aebdf nargoutchk.m, validatestring.m, addtodate.m: new functions
bill@denney.ws
parents:
diff changeset
123 nmatches = sum (matches);
13707
35f7cbf09c77 validatestring.m: Overhaul code for Matlab compatability and performance.
Rik <octave@nomad.inbox5.com>
parents: 12931
diff changeset
124 if (nmatches == 0)
29290
d3c1e6f5ac85 validatestring.m: Overhaul function.
Rik <rik@octave.org>
parents: 27978
diff changeset
125 error ("%sdoes not match any of\n%s", errstr,
d3c1e6f5ac85 validatestring.m: Overhaul function.
Rik <rik@octave.org>
parents: 27978
diff changeset
126 sprintf ("'%s', ", strarray{:})(1:end-2));
13707
35f7cbf09c77 validatestring.m: Overhaul code for Matlab compatability and performance.
Rik <octave@nomad.inbox5.com>
parents: 12931
diff changeset
127 elseif (nmatches == 1)
7658
1ce6460aebdf nargoutchk.m, validatestring.m, addtodate.m: new functions
bill@denney.ws
parents:
diff changeset
128 str = strarray{matches};
1ce6460aebdf nargoutchk.m, validatestring.m, addtodate.m: new functions
bill@denney.ws
parents:
diff changeset
129 else
13707
35f7cbf09c77 validatestring.m: Overhaul code for Matlab compatability and performance.
Rik <octave@nomad.inbox5.com>
parents: 12931
diff changeset
130 ## Are the matches substrings of each other?
35f7cbf09c77 validatestring.m: Overhaul code for Matlab compatability and performance.
Rik <octave@nomad.inbox5.com>
parents: 12931
diff changeset
131 ## If true, choose the shortest. If not, raise an error.
7658
1ce6460aebdf nargoutchk.m, validatestring.m, addtodate.m: new functions
bill@denney.ws
parents:
diff changeset
132 match_idx = find (matches);
13707
35f7cbf09c77 validatestring.m: Overhaul code for Matlab compatability and performance.
Rik <octave@nomad.inbox5.com>
parents: 12931
diff changeset
133 match_len = cellfun ("length", strarray(match_idx));
17405
3f0ed69d21c6 Replace unnecessary instances of strncmp with strcmp.
Rik <rik@octave.org>
parents: 17281
diff changeset
134 [min_len, min_idx] = min (match_len);
13707
35f7cbf09c77 validatestring.m: Overhaul code for Matlab compatability and performance.
Rik <octave@nomad.inbox5.com>
parents: 12931
diff changeset
135 short_str = strarray{match_idx(min_idx)};
17405
3f0ed69d21c6 Replace unnecessary instances of strncmp with strcmp.
Rik <rik@octave.org>
parents: 17281
diff changeset
136 submatch = strncmpi (short_str, strarray(match_idx), min_len);
13707
35f7cbf09c77 validatestring.m: Overhaul code for Matlab compatability and performance.
Rik <octave@nomad.inbox5.com>
parents: 12931
diff changeset
137 if (all (submatch))
35f7cbf09c77 validatestring.m: Overhaul code for Matlab compatability and performance.
Rik <octave@nomad.inbox5.com>
parents: 12931
diff changeset
138 str = short_str;
35f7cbf09c77 validatestring.m: Overhaul code for Matlab compatability and performance.
Rik <octave@nomad.inbox5.com>
parents: 12931
diff changeset
139 else
29290
d3c1e6f5ac85 validatestring.m: Overhaul function.
Rik <rik@octave.org>
parents: 27978
diff changeset
140 error ("%smatches multiple possible values:\n%s", errstr,
d3c1e6f5ac85 validatestring.m: Overhaul function.
Rik <rik@octave.org>
parents: 27978
diff changeset
141 sprintf ("'%s', ", strarray{match_idx})(1:end-2));
13707
35f7cbf09c77 validatestring.m: Overhaul code for Matlab compatability and performance.
Rik <octave@nomad.inbox5.com>
parents: 12931
diff changeset
142 endif
7658
1ce6460aebdf nargoutchk.m, validatestring.m, addtodate.m: new functions
bill@denney.ws
parents:
diff changeset
143 endif
1ce6460aebdf nargoutchk.m, validatestring.m, addtodate.m: new functions
bill@denney.ws
parents:
diff changeset
144
1ce6460aebdf nargoutchk.m, validatestring.m, addtodate.m: new functions
bill@denney.ws
parents:
diff changeset
145 endfunction
1ce6460aebdf nargoutchk.m, validatestring.m, addtodate.m: new functions
bill@denney.ws
parents:
diff changeset
146
13707
35f7cbf09c77 validatestring.m: Overhaul code for Matlab compatability and performance.
Rik <octave@nomad.inbox5.com>
parents: 12931
diff changeset
147
7658
1ce6460aebdf nargoutchk.m, validatestring.m, addtodate.m: new functions
bill@denney.ws
parents:
diff changeset
148 %!shared strarray
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
149 %! strarray = {"octave" "Oct" "octopus" "octaves"};
7658
1ce6460aebdf nargoutchk.m, validatestring.m, addtodate.m: new functions
bill@denney.ws
parents:
diff changeset
150 %!assert (validatestring ("octave", strarray), "octave")
1ce6460aebdf nargoutchk.m, validatestring.m, addtodate.m: new functions
bill@denney.ws
parents:
diff changeset
151 %!assert (validatestring ("oct", strarray), "Oct")
13707
35f7cbf09c77 validatestring.m: Overhaul code for Matlab compatability and performance.
Rik <octave@nomad.inbox5.com>
parents: 12931
diff changeset
152 %!assert (validatestring ("octa", strarray), "octave")
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
153 %! strarray = {"abc1" "def" "abc2"};
13707
35f7cbf09c77 validatestring.m: Overhaul code for Matlab compatability and performance.
Rik <octave@nomad.inbox5.com>
parents: 12931
diff changeset
154 %!assert (validatestring ("d", strarray), "def")
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
155
13707
35f7cbf09c77 validatestring.m: Overhaul code for Matlab compatability and performance.
Rik <octave@nomad.inbox5.com>
parents: 12931
diff changeset
156 %!error <'xyz' does not match any> validatestring ("xyz", strarray)
29290
d3c1e6f5ac85 validatestring.m: Overhaul function.
Rik <rik@octave.org>
parents: 27978
diff changeset
157 %!error <DUMMY_TEST: 'xyz' does not match any>
d3c1e6f5ac85 validatestring.m: Overhaul function.
Rik <rik@octave.org>
parents: 27978
diff changeset
158 %! validatestring ("xyz", strarray, "DUMMY_TEST")
d3c1e6f5ac85 validatestring.m: Overhaul function.
Rik <rik@octave.org>
parents: 27978
diff changeset
159 %!error <DUMMY_TEST: 'xyz' \(variable DUMMY_VAR\) does not match>
d3c1e6f5ac85 validatestring.m: Overhaul function.
Rik <rik@octave.org>
parents: 27978
diff changeset
160 %! validatestring ("xyz", strarray, "DUMMY_TEST", "DUMMY_VAR")
d3c1e6f5ac85 validatestring.m: Overhaul function.
Rik <rik@octave.org>
parents: 27978
diff changeset
161 %!error <DUMMY_TEST: 'xyz' \(variable DUMMY_VAR, argument #5\) does>
d3c1e6f5ac85 validatestring.m: Overhaul function.
Rik <rik@octave.org>
parents: 27978
diff changeset
162 %! validatestring ("xyz", strarray, "DUMMY_TEST", "DUMMY_VAR", 5)
d3c1e6f5ac85 validatestring.m: Overhaul function.
Rik <rik@octave.org>
parents: 27978
diff changeset
163 %!error <'abc' matches multiple possible values:> validatestring ("abc", strarray)
13707
35f7cbf09c77 validatestring.m: Overhaul code for Matlab compatability and performance.
Rik <octave@nomad.inbox5.com>
parents: 12931
diff changeset
164
19833
9fc020886ae9 maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19725
diff changeset
165 ## Test input validation
29290
d3c1e6f5ac85 validatestring.m: Overhaul function.
Rik <rik@octave.org>
parents: 27978
diff changeset
166 %!error <Invalid call> validatestring ("xyz")
d3c1e6f5ac85 validatestring.m: Overhaul function.
Rik <rik@octave.org>
parents: 27978
diff changeset
167 %!error <Invalid call> validatestring ("xyz", {"xyz"}, "3", "4", 5, 6)
d3c1e6f5ac85 validatestring.m: Overhaul function.
Rik <rik@octave.org>
parents: 27978
diff changeset
168 %!error <POSITION must be .= 1> validatestring ("xyz", {"xyz"}, "3", "4", -5)
d3c1e6f5ac85 validatestring.m: Overhaul function.
Rik <rik@octave.org>
parents: 27978
diff changeset
169 %!error <invalid number of character inputs>
d3c1e6f5ac85 validatestring.m: Overhaul function.
Rik <rik@octave.org>
parents: 27978
diff changeset
170 %! validatestring ("xyz", {"xyz"}, "3", "4", "5")
d3c1e6f5ac85 validatestring.m: Overhaul function.
Rik <rik@octave.org>
parents: 27978
diff changeset
171 %!error <STR must be a single character string>
d3c1e6f5ac85 validatestring.m: Overhaul function.
Rik <rik@octave.org>
parents: 27978
diff changeset
172 %! validatestring (1, {"xyz"}, "3", "4", 5)
d3c1e6f5ac85 validatestring.m: Overhaul function.
Rik <rik@octave.org>
parents: 27978
diff changeset
173 %!error <STR must be a single character string>
d3c1e6f5ac85 validatestring.m: Overhaul function.
Rik <rik@octave.org>
parents: 27978
diff changeset
174 %! validatestring (['xyz';'def'], {"xyz"}, "3", "4", 5)
13707
35f7cbf09c77 validatestring.m: Overhaul code for Matlab compatability and performance.
Rik <octave@nomad.inbox5.com>
parents: 12931
diff changeset
175 %!error <STRARRAY must be a cellstr> validatestring ("xyz", "xyz", "3", "4", 5)
29290
d3c1e6f5ac85 validatestring.m: Overhaul function.
Rik <rik@octave.org>
parents: 27978
diff changeset
176 %!error <FUNCNAME must be a single character string>
d3c1e6f5ac85 validatestring.m: Overhaul function.
Rik <rik@octave.org>
parents: 27978
diff changeset
177 %! validatestring ("xyz", {"xyz"}, ["3";"3"], "4", 5)
d3c1e6f5ac85 validatestring.m: Overhaul function.
Rik <rik@octave.org>
parents: 27978
diff changeset
178 %!error <VARNAME must be a single character string>
d3c1e6f5ac85 validatestring.m: Overhaul function.
Rik <rik@octave.org>
parents: 27978
diff changeset
179 %! validatestring ("xyz", {"xyz"}, "3", ["4";"4"], 5)