annotate scripts/strings/validatestring.m @ 27918:b442ec6dda5c

use centralized file for copyright info for individual contributors * COPYRIGHT.md: New file. * In most other files, use "Copyright (C) YYYY-YYYY The Octave Project Developers" instead of tracking individual names in separate source files. The motivation is to reduce the effort required to update the notices each year. Until now, the Octave source files contained copyright notices that list individual contributors. I adopted these file-scope copyright notices because that is what everyone was doing 30 years ago in the days before distributed version control systems. But now, with many contributors and modern version control systems, having these file-scope copyright notices causes trouble when we update copyright years or refactor code. Over time, the file-scope copyright notices may become outdated as new contributions are made or code is moved from one file to another. Sometimes people contribute significant patches but do not add a line claiming copyright. Other times, people add a copyright notice for their contribution but then a later refactoring moves part or all of their contribution to another file and the notice is not moved with the code. As a practical matter, moving such notices is difficult -- determining what parts are due to a particular contributor requires a time-consuming search through the project history. Even managing the yearly update of copyright years is problematic. We have some contributors who are no longer living. Should we update the copyright dates for their contributions when we release new versions? Probably not, but we do still want to claim copyright for the project as a whole. To minimize the difficulty of maintaining the copyright notices, I would like to change Octave's sources to use what is described here: https://softwarefreedom.org/resources/2012/ManagingCopyrightInformation.html in the section "Maintaining centralized copyright notices": The centralized notice approach consolidates all copyright notices in a single location, usually a top-level file. This file should contain all of the copyright notices provided project contributors, unless the contribution was clearly insignificant. It may also credit -- without a copyright notice -- anyone who helped with the project but did not contribute code or other copyrighted material. This approach captures less information about contributions within individual files, recognizing that the DVCS is better equipped to record those details. As we mentioned before, it does have one disadvantage as compared to the file-scope approach: if a single file is separated from the distribution, the recipient won't see the contributors' copyright notices. But this can be easily remedied by including a single copyright notice in each file's header, pointing to the top-level file: Copyright YYYY-YYYY The Octave Project Developers See the COPYRIGHT file at the top-level directory of this distribution or at https://octave.org/COPYRIGHT.html. followed by the usual GPL copyright statement. For more background, see the discussion here: https://lists.gnu.org/archive/html/octave-maintainers/2020-01/msg00009.html Most files in the following directories have been skipped intentinally in this changeset: doc libgui/qterminal liboctave/external m4
author John W. Eaton <jwe@octave.org>
date Mon, 06 Jan 2020 15:38:17 -0500
parents 00f796120a6d
children 1891570abac8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
27918
b442ec6dda5c use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
1 ## Copyright (C) 2008-2019 The Octave Project Developers
b442ec6dda5c use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
2 ##
b442ec6dda5c use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
3 ## See the file COPYRIGHT.md in the top-level directory of this distribution
b442ec6dda5c use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
4 ## or <https://octave.org/COPYRIGHT.html/>.
b442ec6dda5c use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
5 ##
7658
1ce6460aebdf nargoutchk.m, validatestring.m, addtodate.m: new functions
bill@denney.ws
parents:
diff changeset
6 ##
1ce6460aebdf nargoutchk.m, validatestring.m, addtodate.m: new functions
bill@denney.ws
parents:
diff changeset
7 ## This file is part of Octave.
1ce6460aebdf nargoutchk.m, validatestring.m, addtodate.m: new functions
bill@denney.ws
parents:
diff changeset
8 ##
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 23220
diff changeset
9 ## 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
10 ## 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
11 ## 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
12 ## (at your option) any later version.
7658
1ce6460aebdf nargoutchk.m, validatestring.m, addtodate.m: new functions
bill@denney.ws
parents:
diff changeset
13 ##
1ce6460aebdf nargoutchk.m, validatestring.m, addtodate.m: new functions
bill@denney.ws
parents:
diff changeset
14 ## 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
15 ## WITHOUT ANY WARRANTY; without even the implied warranty of
22755
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22323
diff changeset
16 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22323
diff changeset
17 ## GNU General Public License for more details.
7658
1ce6460aebdf nargoutchk.m, validatestring.m, addtodate.m: new functions
bill@denney.ws
parents:
diff changeset
18 ##
1ce6460aebdf nargoutchk.m, validatestring.m, addtodate.m: new functions
bill@denney.ws
parents:
diff changeset
19 ## 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
20 ## 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
21 ## <https://www.gnu.org/licenses/>.
7658
1ce6460aebdf nargoutchk.m, validatestring.m, addtodate.m: new functions
bill@denney.ws
parents:
diff changeset
22
1ce6460aebdf nargoutchk.m, validatestring.m, addtodate.m: new functions
bill@denney.ws
parents:
diff changeset
23 ## -*- texinfo -*-
20852
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 20726
diff changeset
24 ## @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
25 ## @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
26 ## @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
27 ## @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
28 ## 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
29 ## @var{strarray}.
1ce6460aebdf nargoutchk.m, validatestring.m, addtodate.m: new functions
bill@denney.ws
parents:
diff changeset
30 ##
13707
35f7cbf09c77 validatestring.m: Overhaul code for Matlab compatability and performance.
Rik <octave@nomad.inbox5.com>
parents: 12931
diff changeset
31 ## When @var{str} is a character string to be tested, and @var{strarray} is a
35f7cbf09c77 validatestring.m: Overhaul code for Matlab compatability and performance.
Rik <octave@nomad.inbox5.com>
parents: 12931
diff changeset
32 ## cellstr of valid values, then @var{validstr} will be the validated form
7658
1ce6460aebdf nargoutchk.m, validatestring.m, addtodate.m: new functions
bill@denney.ws
parents:
diff changeset
33 ## of @var{str} where validation is defined as @var{str} being a member
13707
35f7cbf09c77 validatestring.m: Overhaul code for Matlab compatability and performance.
Rik <octave@nomad.inbox5.com>
parents: 12931
diff changeset
34 ## or substring of @var{validstr}. This is useful for both verifying
17281
bc924baa2c4e doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents: 14363
diff changeset
35 ## and expanding short options, such as @qcode{"r"}, to their longer forms,
bc924baa2c4e doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents: 14363
diff changeset
36 ## such as @qcode{"red"}. If @var{str} is a substring of @var{validstr}, and
bc924baa2c4e doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents: 14363
diff changeset
37 ## there are multiple matches, the shortest match will be returned if all
bc924baa2c4e doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents: 14363
diff changeset
38 ## matches are substrings of each other. Otherwise, an error will be raised
bc924baa2c4e doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents: 14363
diff changeset
39 ## because the expansion of @var{str} is ambiguous. All comparisons are case
bc924baa2c4e doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents: 14363
diff changeset
40 ## insensitive.
13707
35f7cbf09c77 validatestring.m: Overhaul code for Matlab compatability and performance.
Rik <octave@nomad.inbox5.com>
parents: 12931
diff changeset
41 ##
35f7cbf09c77 validatestring.m: Overhaul code for Matlab compatability and performance.
Rik <octave@nomad.inbox5.com>
parents: 12931
diff changeset
42 ## 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
43 ## 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
44 ## specific.
35f7cbf09c77 validatestring.m: Overhaul code for Matlab compatability and performance.
Rik <octave@nomad.inbox5.com>
parents: 12931
diff changeset
45 ##
35f7cbf09c77 validatestring.m: Overhaul code for Matlab compatability and performance.
Rik <octave@nomad.inbox5.com>
parents: 12931
diff changeset
46 ## Examples:
14093
050bc580cb60 doc: Various docstring improvements before 3.6.0 release.
Rik <octave@nomad.inbox5.com>
parents: 13708
diff changeset
47 ## @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
48 ##
14093
050bc580cb60 doc: Various docstring improvements before 3.6.0 release.
Rik <octave@nomad.inbox5.com>
parents: 13708
diff changeset
49 ## @smallexample
13707
35f7cbf09c77 validatestring.m: Overhaul code for Matlab compatability and performance.
Rik <octave@nomad.inbox5.com>
parents: 12931
diff changeset
50 ## @group
13708
ee28bf4c3c29 doc: Use @{ macro for curly brace in validatestring Texinfo docstring
Rik <octave@nomad.inbox5.com>
parents: 13707
diff changeset
51 ## 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
52 ## @result{} "red"
13707
35f7cbf09c77 validatestring.m: Overhaul code for Matlab compatability and performance.
Rik <octave@nomad.inbox5.com>
parents: 12931
diff changeset
53 ##
13708
ee28bf4c3c29 doc: Use @{ macro for curly brace in validatestring Texinfo docstring
Rik <octave@nomad.inbox5.com>
parents: 13707
diff changeset
54 ## 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
55 ## @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
56 ## blue, black
13707
35f7cbf09c77 validatestring.m: Overhaul code for Matlab compatability and performance.
Rik <octave@nomad.inbox5.com>
parents: 12931
diff changeset
57 ## @end group
14093
050bc580cb60 doc: Various docstring improvements before 3.6.0 release.
Rik <octave@nomad.inbox5.com>
parents: 13708
diff changeset
58 ## @end smallexample
17405
3f0ed69d21c6 Replace unnecessary instances of strncmp with strcmp.
Rik <rik@octave.org>
parents: 17281
diff changeset
59 ##
19069
efd5cf93013b doc: Move validatestrings into function chapter of manual.
Rik <rik@octave.org>
parents: 18579
diff changeset
60 ## @seealso{strcmp, strcmpi, validateattributes, inputParser}
7658
1ce6460aebdf nargoutchk.m, validatestring.m, addtodate.m: new functions
bill@denney.ws
parents:
diff changeset
61 ## @end deftypefn
1ce6460aebdf nargoutchk.m, validatestring.m, addtodate.m: new functions
bill@denney.ws
parents:
diff changeset
62
1ce6460aebdf nargoutchk.m, validatestring.m, addtodate.m: new functions
bill@denney.ws
parents:
diff changeset
63 ## Author: Bill Denney <bill@denney.ws>
1ce6460aebdf nargoutchk.m, validatestring.m, addtodate.m: new functions
bill@denney.ws
parents:
diff changeset
64
1ce6460aebdf nargoutchk.m, validatestring.m, addtodate.m: new functions
bill@denney.ws
parents:
diff changeset
65 function str = validatestring (str, strarray, varargin)
1ce6460aebdf nargoutchk.m, validatestring.m, addtodate.m: new functions
bill@denney.ws
parents:
diff changeset
66
1ce6460aebdf nargoutchk.m, validatestring.m, addtodate.m: new functions
bill@denney.ws
parents:
diff changeset
67 if (nargin < 2 || nargin > 5)
1ce6460aebdf nargoutchk.m, validatestring.m, addtodate.m: new functions
bill@denney.ws
parents:
diff changeset
68 print_usage ();
1ce6460aebdf nargoutchk.m, validatestring.m, addtodate.m: new functions
bill@denney.ws
parents:
diff changeset
69 endif
1ce6460aebdf nargoutchk.m, validatestring.m, addtodate.m: new functions
bill@denney.ws
parents:
diff changeset
70
1ce6460aebdf nargoutchk.m, validatestring.m, addtodate.m: new functions
bill@denney.ws
parents:
diff changeset
71 position = 0;
13707
35f7cbf09c77 validatestring.m: Overhaul code for Matlab compatability and performance.
Rik <octave@nomad.inbox5.com>
parents: 12931
diff changeset
72 ## Process input arguments
35f7cbf09c77 validatestring.m: Overhaul code for Matlab compatability and performance.
Rik <octave@nomad.inbox5.com>
parents: 12931
diff changeset
73 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
74 position = fix (varargin{end});
13707
35f7cbf09c77 validatestring.m: Overhaul code for Matlab compatability and performance.
Rik <octave@nomad.inbox5.com>
parents: 12931
diff changeset
75 varargin(end) = [];
7658
1ce6460aebdf nargoutchk.m, validatestring.m, addtodate.m: new functions
bill@denney.ws
parents:
diff changeset
76 endif
17405
3f0ed69d21c6 Replace unnecessary instances of strncmp with strcmp.
Rik <rik@octave.org>
parents: 17281
diff changeset
77
13707
35f7cbf09c77 validatestring.m: Overhaul code for Matlab compatability and performance.
Rik <octave@nomad.inbox5.com>
parents: 12931
diff changeset
78 funcname = varname = "";
35f7cbf09c77 validatestring.m: Overhaul code for Matlab compatability and performance.
Rik <octave@nomad.inbox5.com>
parents: 12931
diff changeset
79 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
80 n_chararg = sum (char_idx);
35f7cbf09c77 validatestring.m: Overhaul code for Matlab compatability and performance.
Rik <octave@nomad.inbox5.com>
parents: 12931
diff changeset
81 if (n_chararg > 2)
35f7cbf09c77 validatestring.m: Overhaul code for Matlab compatability and performance.
Rik <octave@nomad.inbox5.com>
parents: 12931
diff changeset
82 error ("validatestring: invalid number of character inputs (3)");
35f7cbf09c77 validatestring.m: Overhaul code for Matlab compatability and performance.
Rik <octave@nomad.inbox5.com>
parents: 12931
diff changeset
83 elseif (n_chararg == 2)
35f7cbf09c77 validatestring.m: Overhaul code for Matlab compatability and performance.
Rik <octave@nomad.inbox5.com>
parents: 12931
diff changeset
84 [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
85 elseif (n_chararg == 1)
35f7cbf09c77 validatestring.m: Overhaul code for Matlab compatability and performance.
Rik <octave@nomad.inbox5.com>
parents: 12931
diff changeset
86 funcname = varargin{char_idx};
35f7cbf09c77 validatestring.m: Overhaul code for Matlab compatability and performance.
Rik <octave@nomad.inbox5.com>
parents: 12931
diff changeset
87 endif
7658
1ce6460aebdf nargoutchk.m, validatestring.m, addtodate.m: new functions
bill@denney.ws
parents:
diff changeset
88
1ce6460aebdf nargoutchk.m, validatestring.m, addtodate.m: new functions
bill@denney.ws
parents:
diff changeset
89 ## Check the inputs
1ce6460aebdf nargoutchk.m, validatestring.m, addtodate.m: new functions
bill@denney.ws
parents:
diff changeset
90 if (! ischar (str))
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
91 error ("validatestring: STR must be a character string");
13707
35f7cbf09c77 validatestring.m: Overhaul code for Matlab compatability and performance.
Rik <octave@nomad.inbox5.com>
parents: 12931
diff changeset
92 elseif (! isrow (str))
35f7cbf09c77 validatestring.m: Overhaul code for Matlab compatability and performance.
Rik <octave@nomad.inbox5.com>
parents: 12931
diff changeset
93 error ("validatestring: STR must be a single row vector");
7658
1ce6460aebdf nargoutchk.m, validatestring.m, addtodate.m: new functions
bill@denney.ws
parents:
diff changeset
94 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
95 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
96 elseif (! isempty (funcname) && ! isrow (funcname))
35f7cbf09c77 validatestring.m: Overhaul code for Matlab compatability and performance.
Rik <octave@nomad.inbox5.com>
parents: 12931
diff changeset
97 error ("validatestring: FUNCNAME must be a single row vector");
35f7cbf09c77 validatestring.m: Overhaul code for Matlab compatability and performance.
Rik <octave@nomad.inbox5.com>
parents: 12931
diff changeset
98 elseif (! isempty (varname) && ! isrow (varname))
35f7cbf09c77 validatestring.m: Overhaul code for Matlab compatability and performance.
Rik <octave@nomad.inbox5.com>
parents: 12931
diff changeset
99 error ("validatestring: VARNAME must be a single row vector");
7658
1ce6460aebdf nargoutchk.m, validatestring.m, addtodate.m: new functions
bill@denney.ws
parents:
diff changeset
100 elseif (position < 0)
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
101 error ("validatestring: POSITION must be >= 0");
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
7658
1ce6460aebdf nargoutchk.m, validatestring.m, addtodate.m: new functions
bill@denney.ws
parents:
diff changeset
105 errstr = "";
9209
923c7cb7f13f Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents: 8920
diff changeset
106 if (! isempty (funcname))
18579
d227178a0d40 validatestring.m: Change error message to follow Octave guidelines (bug #41484).
Eugenio Gianniti <eugenio.gianniti@mail.polimi.it>
parents: 17744
diff changeset
107 errstr = [funcname ": "];
7658
1ce6460aebdf nargoutchk.m, validatestring.m, addtodate.m: new functions
bill@denney.ws
parents:
diff changeset
108 endif
1ce6460aebdf nargoutchk.m, validatestring.m, addtodate.m: new functions
bill@denney.ws
parents:
diff changeset
109 if (! isempty (varname))
18579
d227178a0d40 validatestring.m: Change error message to follow Octave guidelines (bug #41484).
Eugenio Gianniti <eugenio.gianniti@mail.polimi.it>
parents: 17744
diff changeset
110 errstr = [errstr varname " "];
d227178a0d40 validatestring.m: Change error message to follow Octave guidelines (bug #41484).
Eugenio Gianniti <eugenio.gianniti@mail.polimi.it>
parents: 17744
diff changeset
111 else
d227178a0d40 validatestring.m: Change error message to follow Octave guidelines (bug #41484).
Eugenio Gianniti <eugenio.gianniti@mail.polimi.it>
parents: 17744
diff changeset
112 errstr = sprintf ("%s'%s' ", errstr, str);
7658
1ce6460aebdf nargoutchk.m, validatestring.m, addtodate.m: new functions
bill@denney.ws
parents:
diff changeset
113 endif
1ce6460aebdf nargoutchk.m, validatestring.m, addtodate.m: new functions
bill@denney.ws
parents:
diff changeset
114 if (position > 0)
18579
d227178a0d40 validatestring.m: Change error message to follow Octave guidelines (bug #41484).
Eugenio Gianniti <eugenio.gianniti@mail.polimi.it>
parents: 17744
diff changeset
115 errstr = sprintf ("%s(argument #%i) ", errstr, position);
7658
1ce6460aebdf nargoutchk.m, validatestring.m, addtodate.m: new functions
bill@denney.ws
parents:
diff changeset
116 endif
1ce6460aebdf nargoutchk.m, validatestring.m, addtodate.m: new functions
bill@denney.ws
parents:
diff changeset
117
17405
3f0ed69d21c6 Replace unnecessary instances of strncmp with strcmp.
Rik <rik@octave.org>
parents: 17281
diff changeset
118 matches = strncmpi (str, strarray(:), length (str));
7658
1ce6460aebdf nargoutchk.m, validatestring.m, addtodate.m: new functions
bill@denney.ws
parents:
diff changeset
119 nmatches = sum (matches);
13707
35f7cbf09c77 validatestring.m: Overhaul code for Matlab compatability and performance.
Rik <octave@nomad.inbox5.com>
parents: 12931
diff changeset
120 if (nmatches == 0)
20726
25d676f9619c Preface error() messages with name of function when possible.
Rik <rik@octave.org>
parents: 19833
diff changeset
121 error ("validatestring: %sdoes not match any of\n%s", errstr,
13707
35f7cbf09c77 validatestring.m: Overhaul code for Matlab compatability and performance.
Rik <octave@nomad.inbox5.com>
parents: 12931
diff changeset
122 sprintf ("%s, ", strarray{:})(1:end-2));
35f7cbf09c77 validatestring.m: Overhaul code for Matlab compatability and performance.
Rik <octave@nomad.inbox5.com>
parents: 12931
diff changeset
123 elseif (nmatches == 1)
7658
1ce6460aebdf nargoutchk.m, validatestring.m, addtodate.m: new functions
bill@denney.ws
parents:
diff changeset
124 str = strarray{matches};
1ce6460aebdf nargoutchk.m, validatestring.m, addtodate.m: new functions
bill@denney.ws
parents:
diff changeset
125 else
13707
35f7cbf09c77 validatestring.m: Overhaul code for Matlab compatability and performance.
Rik <octave@nomad.inbox5.com>
parents: 12931
diff changeset
126 ## 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
127 ## 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
128 match_idx = find (matches);
13707
35f7cbf09c77 validatestring.m: Overhaul code for Matlab compatability and performance.
Rik <octave@nomad.inbox5.com>
parents: 12931
diff changeset
129 match_len = cellfun ("length", strarray(match_idx));
17405
3f0ed69d21c6 Replace unnecessary instances of strncmp with strcmp.
Rik <rik@octave.org>
parents: 17281
diff changeset
130 [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
131 short_str = strarray{match_idx(min_idx)};
17405
3f0ed69d21c6 Replace unnecessary instances of strncmp with strcmp.
Rik <rik@octave.org>
parents: 17281
diff changeset
132 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
133 if (all (submatch))
35f7cbf09c77 validatestring.m: Overhaul code for Matlab compatability and performance.
Rik <octave@nomad.inbox5.com>
parents: 12931
diff changeset
134 str = short_str;
35f7cbf09c77 validatestring.m: Overhaul code for Matlab compatability and performance.
Rik <octave@nomad.inbox5.com>
parents: 12931
diff changeset
135 else
20726
25d676f9619c Preface error() messages with name of function when possible.
Rik <rik@octave.org>
parents: 19833
diff changeset
136 error ("validatestring: %sallows multiple unique matches:\n%s",
18579
d227178a0d40 validatestring.m: Change error message to follow Octave guidelines (bug #41484).
Eugenio Gianniti <eugenio.gianniti@mail.polimi.it>
parents: 17744
diff changeset
137 errstr, 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
138 endif
7658
1ce6460aebdf nargoutchk.m, validatestring.m, addtodate.m: new functions
bill@denney.ws
parents:
diff changeset
139 endif
1ce6460aebdf nargoutchk.m, validatestring.m, addtodate.m: new functions
bill@denney.ws
parents:
diff changeset
140
1ce6460aebdf nargoutchk.m, validatestring.m, addtodate.m: new functions
bill@denney.ws
parents:
diff changeset
141 endfunction
1ce6460aebdf nargoutchk.m, validatestring.m, addtodate.m: new functions
bill@denney.ws
parents:
diff changeset
142
13707
35f7cbf09c77 validatestring.m: Overhaul code for Matlab compatability and performance.
Rik <octave@nomad.inbox5.com>
parents: 12931
diff changeset
143
7658
1ce6460aebdf nargoutchk.m, validatestring.m, addtodate.m: new functions
bill@denney.ws
parents:
diff changeset
144 %!shared strarray
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
145 %! strarray = {"octave" "Oct" "octopus" "octaves"};
7658
1ce6460aebdf nargoutchk.m, validatestring.m, addtodate.m: new functions
bill@denney.ws
parents:
diff changeset
146 %!assert (validatestring ("octave", strarray), "octave")
1ce6460aebdf nargoutchk.m, validatestring.m, addtodate.m: new functions
bill@denney.ws
parents:
diff changeset
147 %!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
148 %!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
149 %! strarray = {"abc1" "def" "abc2"};
13707
35f7cbf09c77 validatestring.m: Overhaul code for Matlab compatability and performance.
Rik <octave@nomad.inbox5.com>
parents: 12931
diff changeset
150 %!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
151
13707
35f7cbf09c77 validatestring.m: Overhaul code for Matlab compatability and performance.
Rik <octave@nomad.inbox5.com>
parents: 12931
diff changeset
152 %!error <'xyz' does not match any> validatestring ("xyz", strarray)
18579
d227178a0d40 validatestring.m: Change error message to follow Octave guidelines (bug #41484).
Eugenio Gianniti <eugenio.gianniti@mail.polimi.it>
parents: 17744
diff changeset
153 %!error <DUMMY_TEST: 'xyz' does not> validatestring ("xyz", strarray, "DUMMY_TEST")
d227178a0d40 validatestring.m: Change error message to follow Octave guidelines (bug #41484).
Eugenio Gianniti <eugenio.gianniti@mail.polimi.it>
parents: 17744
diff changeset
154 %!error <DUMMY_TEST: DUMMY_VAR does> validatestring ("xyz", strarray, "DUMMY_TEST", "DUMMY_VAR")
d227178a0d40 validatestring.m: Change error message to follow Octave guidelines (bug #41484).
Eugenio Gianniti <eugenio.gianniti@mail.polimi.it>
parents: 17744
diff changeset
155 %!error <DUMMY_TEST: DUMMY_VAR \(argument #5\) does> validatestring ("xyz", strarray, "DUMMY_TEST", "DUMMY_VAR", 5)
d227178a0d40 validatestring.m: Change error message to follow Octave guidelines (bug #41484).
Eugenio Gianniti <eugenio.gianniti@mail.polimi.it>
parents: 17744
diff changeset
156 %!error <'abc' allows multiple unique matches> validatestring ("abc", strarray)
13707
35f7cbf09c77 validatestring.m: Overhaul code for Matlab compatability and performance.
Rik <octave@nomad.inbox5.com>
parents: 12931
diff changeset
157
19833
9fc020886ae9 maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19725
diff changeset
158 ## Test input validation
13707
35f7cbf09c77 validatestring.m: Overhaul code for Matlab compatability and performance.
Rik <octave@nomad.inbox5.com>
parents: 12931
diff changeset
159 %!error validatestring ("xyz")
35f7cbf09c77 validatestring.m: Overhaul code for Matlab compatability and performance.
Rik <octave@nomad.inbox5.com>
parents: 12931
diff changeset
160 %!error validatestring ("xyz", {"xyz"}, "3", "4", 5, 6)
35f7cbf09c77 validatestring.m: Overhaul code for Matlab compatability and performance.
Rik <octave@nomad.inbox5.com>
parents: 12931
diff changeset
161 %!error <invalid number of character inputs> validatestring ("xyz", {"xyz"}, "3", "4", "5")
35f7cbf09c77 validatestring.m: Overhaul code for Matlab compatability and performance.
Rik <octave@nomad.inbox5.com>
parents: 12931
diff changeset
162 %!error <STR must be a character string> validatestring (1, {"xyz"}, "3", "4", 5)
35f7cbf09c77 validatestring.m: Overhaul code for Matlab compatability and performance.
Rik <octave@nomad.inbox5.com>
parents: 12931
diff changeset
163 %!error <STR must be a single row vector> validatestring ("xyz".', {"xyz"}, "3", "4", 5)
35f7cbf09c77 validatestring.m: Overhaul code for Matlab compatability and performance.
Rik <octave@nomad.inbox5.com>
parents: 12931
diff changeset
164 %!error <STRARRAY must be a cellstr> validatestring ("xyz", "xyz", "3", "4", 5)
35f7cbf09c77 validatestring.m: Overhaul code for Matlab compatability and performance.
Rik <octave@nomad.inbox5.com>
parents: 12931
diff changeset
165 %!error <FUNCNAME must be a single row vector> validatestring ("xyz", {"xyz"}, "33".', "4", 5)
35f7cbf09c77 validatestring.m: Overhaul code for Matlab compatability and performance.
Rik <octave@nomad.inbox5.com>
parents: 12931
diff changeset
166 %!error <VARNAME must be a single row vector> validatestring ("xyz", {"xyz"}, "3", "44".', 5)
35f7cbf09c77 validatestring.m: Overhaul code for Matlab compatability and performance.
Rik <octave@nomad.inbox5.com>
parents: 12931
diff changeset
167 %!error <POSITION must be> validatestring ("xyz", {"xyz"}, "3", "4", -5)