Mercurial > hg > octave-nkf
annotate scripts/miscellaneous/genvarname.m @ 20038:9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Try to trim long lines to < 80 chars.
Use '##' for single line comments.
Use '(...)' around tests for if/elseif/switch/while.
Abut cell indexing operator '{' next to variable.
Abut array indexing operator '(' next to variable.
Use space between negation operator '!' and following expression.
Use two newlines between endfunction and start of %!test or %!demo code.
Remove unnecessary parens grouping between short-circuit operators.
Remove stray extra spaces (typos) between variables and assignment operators.
Remove stray extra spaces from ends of lines.
author | Rik <rik@octave.org> |
---|---|
date | Mon, 23 Feb 2015 14:54:39 -0800 |
parents | 4197fc428c7d |
children | df437a52bcaf |
rev | line source |
---|---|
19898
4197fc428c7d
maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents:
19470
diff
changeset
|
1 ## Copyright (C) 2008-2015 Bill Denney, Robert Platt |
7657 | 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 -*- | |
10793
be55736a0783
Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents:
9037
diff
changeset
|
20 ## @deftypefn {Function File} {@var{varname} =} genvarname (@var{str}) |
7975
ed4ec7875f98
trival doc fix for genvarname
David Bateman <dbateman@free.fr>
parents:
7657
diff
changeset
|
21 ## @deftypefnx {Function File} {@var{varname} =} genvarname (@var{str}, @var{exclusions}) |
19386 | 22 ## Create valid unique variable name(s) from @var{str}. |
7657 | 23 ## |
24 ## If @var{str} is a cellstr, then a unique variable is created for each | |
25 ## cell in @var{str}. | |
26 ## | |
27 ## @example | |
28 ## @group | |
29 ## genvarname (@{"foo", "foo"@}) | |
14327
4d917a6a858b
doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
30 ## @result{} |
4d917a6a858b
doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
31 ## @{ |
4d917a6a858b
doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
32 ## [1,1] = foo |
4d917a6a858b
doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
33 ## [1,2] = foo1 |
4d917a6a858b
doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
34 ## @} |
7657 | 35 ## @end group |
36 ## @end example | |
37 ## | |
19386 | 38 ## If @var{exclusions} is given, then the variable(s) will be unique to each |
39 ## other and to @var{exclusions} (@var{exclusions} may be either a string or | |
40 ## a cellstr). | |
41 ## | |
42 ## @example | |
43 ## @group | |
44 ## x = 3.141; | |
45 ## genvarname ("x", who ()) | |
46 ## @result{} x1 | |
47 ## @end group | |
48 ## @end example | |
49 ## | |
50 ## Note that the result is a char array or cell array of strings, not the | |
9037
4cb9f994dcec
Documentation cleanup of var.texi, expr.texi, eval.texi
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
51 ## variables themselves. To define a variable, @code{eval()} can be |
4cb9f994dcec
Documentation cleanup of var.texi, expr.texi, eval.texi
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
52 ## used. The following trivial example sets @code{x} to @code{42}. |
7657 | 53 ## |
54 ## @example | |
55 ## @group | |
56 ## name = genvarname ("x"); | |
14327
4d917a6a858b
doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
57 ## eval ([name " = 42"]); |
4d917a6a858b
doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
58 ## @result{} x = 42 |
7657 | 59 ## @end group |
60 ## @end example | |
61 ## | |
19386 | 62 ## This can be useful for creating unique struct field names. |
7657 | 63 ## |
64 ## @example | |
65 ## @group | |
66 ## x = struct (); | |
67 ## for i = 1:3 | |
68 ## x.(genvarname ("a", fieldnames (x))) = i; | |
69 ## endfor | |
14327
4d917a6a858b
doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
70 ## @result{} x = |
4d917a6a858b
doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
71 ## @{ |
4d917a6a858b
doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
72 ## a = 1 |
4d917a6a858b
doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
73 ## a1 = 2 |
4d917a6a858b
doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
74 ## a2 = 3 |
4d917a6a858b
doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
75 ## @} |
7657 | 76 ## @end group |
77 ## @end example | |
78 ## | |
19386 | 79 ## Since variable names may only contain letters, digits, and underscores, |
80 ## @code{genvarname} will replace any sequence of disallowed characters with | |
9037
4cb9f994dcec
Documentation cleanup of var.texi, expr.texi, eval.texi
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
81 ## an underscore. Also, variables may not begin with a digit; in this |
19386 | 82 ## case an @samp{x} is added before the variable name. |
7657 | 83 ## |
17281
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
16994
diff
changeset
|
84 ## Variable names beginning and ending with two underscores @qcode{"__"} are |
19386 | 85 ## valid, but they are used internally by Octave and should generally be |
86 ## avoided; therefore, @code{genvarname} will not generate such names. | |
7657 | 87 ## |
19386 | 88 ## @code{genvarname} will also ensure that returned names do not clash with |
17281
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
16994
diff
changeset
|
89 ## keywords such as @qcode{"for"} and @qcode{"if"}. A number will be |
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
16994
diff
changeset
|
90 ## appended if necessary. Note, however, that this does @strong{not} include |
19386 | 91 ## function names such as @qcode{"sin"}. Such names should be included in |
92 ## @var{exclusions} if necessary. | |
19470
6ca096827123
Use tempname() rather than tmpnam() in core Octave.
Rik <rik@octave.org>
parents:
19386
diff
changeset
|
93 ## @seealso{isvarname, iskeyword, exist, who, tempname, eval} |
7657 | 94 ## @end deftypefn |
95 | |
96 ## Authors: Rob Platt <robert.platt@postgrad.manchester.ac.uk> | |
97 ## Bill Denney <bill@denney.ws> | |
98 | |
19386 | 99 function varname = genvarname (str, exclusions = {}) |
100 | |
101 if (nargin < 1 || nargin > 2) | |
102 print_usage (); | |
103 endif | |
7657 | 104 |
105 strinput = ischar (str); | |
106 ## Process the inputs | |
19386 | 107 if (strinput) |
108 if (rows (str) != 1) | |
109 error ("genvarname: if more than one STR is given, it must be a cellstr"); | |
110 endif | |
111 str = {str}; | |
112 elseif (! iscellstr (str)) | |
113 error ("genvarname: STR must be a string or cellstr"); | |
114 endif | |
115 | |
116 if (ischar (exclusions)) | |
7657 | 117 if (rows (exclusions) != 1) |
8664 | 118 error ("genvarname: if more than one exclusion is given, it must be a cellstr"); |
7657 | 119 endif |
120 exclusions = {exclusions}; | |
121 elseif (! iscellstr (exclusions)) | |
19386 | 122 error ("genvarname: EXCLUSIONS must be a string or cellstr"); |
123 else | |
124 exclusions = exclusions(:); | |
7657 | 125 endif |
126 | |
127 varname = cell (size (str)); | |
128 for i = 1:numel (str) | |
129 ## Perform any modifications to the varname to make sure that it is | |
130 ## a valid variable name. | |
131 | |
132 ## remove invalid characters | |
19386 | 133 str{i}(! (isalnum (str{i}) | str{i} == "_")) = "_"; |
7657 | 134 ## do not use keywords |
135 if (iskeyword (str{i})) | |
18742
56f3c564baaf
genvarname: don't produce names with leading underscored (bug #41923)
Eugenio Gianniti <eugenio.gianniti@mail.polimi.it>
parents:
17745
diff
changeset
|
136 firstcharacter = toupper (str{i}(1)); |
56f3c564baaf
genvarname: don't produce names with leading underscored (bug #41923)
Eugenio Gianniti <eugenio.gianniti@mail.polimi.it>
parents:
17745
diff
changeset
|
137 str{i} = ["x", firstcharacter, str{i}(2:end)]; |
7657 | 138 endif |
139 ## The variable cannot be empty | |
140 if (isempty (str{i})) | |
141 str{i} = "x"; | |
142 endif | |
18742
56f3c564baaf
genvarname: don't produce names with leading underscored (bug #41923)
Eugenio Gianniti <eugenio.gianniti@mail.polimi.it>
parents:
17745
diff
changeset
|
143 ## Leading underscores are not Matlab compatible |
56f3c564baaf
genvarname: don't produce names with leading underscored (bug #41923)
Eugenio Gianniti <eugenio.gianniti@mail.polimi.it>
parents:
17745
diff
changeset
|
144 if (str{i}(1) == "_") |
56f3c564baaf
genvarname: don't produce names with leading underscored (bug #41923)
Eugenio Gianniti <eugenio.gianniti@mail.polimi.it>
parents:
17745
diff
changeset
|
145 str{i} = ["x", str{i}]; |
56f3c564baaf
genvarname: don't produce names with leading underscored (bug #41923)
Eugenio Gianniti <eugenio.gianniti@mail.polimi.it>
parents:
17745
diff
changeset
|
146 endif |
7657 | 147 ## it cannot start with a number |
19386 | 148 if (isdigit (str{i}(1))) |
18742
56f3c564baaf
genvarname: don't produce names with leading underscored (bug #41923)
Eugenio Gianniti <eugenio.gianniti@mail.polimi.it>
parents:
17745
diff
changeset
|
149 str{i} = ["x", str{i}]; |
7657 | 150 endif |
151 | |
152 ## make sure that the variable is unique relative to other variables | |
153 ## and the exclusions list | |
154 excluded = any (strcmp (str{i}, exclusions)); | |
19386 | 155 if (excluded && isdigit (str{i}(end))) |
7657 | 156 ## if it is not unique and ends with a digit, add an underscore to |
157 ## make the variable name more readable ("x1_1" instead of "x11") | |
19386 | 158 str{i} = [str{i}, "_"]; |
7657 | 159 endif |
160 varname(i) = str(i); | |
161 idx = 0; | |
14552
86854d032a37
maint: miscellaneous style fixes for .m files
John W. Eaton <jwe@octave.org>
parents:
14363
diff
changeset
|
162 while (excluded) |
7657 | 163 idx++; |
14868
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14552
diff
changeset
|
164 varname{i} = sprintf ("%s%d", str{i}, idx); |
7657 | 165 excluded = any (strcmp (varname{i}, exclusions)); |
166 endwhile | |
167 exclusions(end+1) = varname(i); | |
168 endfor | |
169 | |
14552
86854d032a37
maint: miscellaneous style fixes for .m files
John W. Eaton <jwe@octave.org>
parents:
14363
diff
changeset
|
170 if (strinput) |
7657 | 171 varname = varname{1}; |
172 endif | |
173 | |
174 endfunction | |
175 | |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
176 |
7657 | 177 ## a single argument |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
178 %!assert (genvarname ("a"), "a") |
7657 | 179 ## a single argument with a non-conflicting exception |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
180 %!assert (genvarname ("a", "b"), "a") |
7657 | 181 ## a single argument with a conflicting exception |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
182 %!assert (genvarname ("a", "a"), "a1") |
7657 | 183 ## a single argument as a cell |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
184 %!assert (genvarname ({"a"}), {"a"}) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
185 %!assert (genvarname ({"a"}, "b"), {"a"}) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
186 %!assert (genvarname ({"a"}, {"b"}), {"a"}) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
187 %!assert (genvarname ({"a"}, "a"), {"a1"}) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
188 %!assert (genvarname ({"a"}, {"a"}), {"a1"}) |
7657 | 189 ## Test different arguments |
190 ## orientation | |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
191 %!assert (genvarname ({"a" "b"}), {"a" "b"}) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
192 %!assert (genvarname ({"a";"b"}), {"a";"b"}) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
193 %!assert (genvarname ({"a" "a"}), {"a" "a1"}) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
194 %!assert (genvarname ({"a" "b";"c" "d"}), {"a" "b";"c" "d"}) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
195 %!assert (genvarname ({"a" "a" "a";"a" "a" "a"}), {"a" "a2" "a4";"a1" "a3" "a5"}) |
7657 | 196 ## more than one repetition |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
197 %!assert (genvarname ({"a" "a" "a"}), {"a" "a1" "a2"}) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
198 %!assert (genvarname ({"a" "a" "a"}, {"a" "a1" "a2"}), {"a3" "a4" "a5"}) |
7657 | 199 ## more than one repetition not in order |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
200 %!assert (genvarname ({"a" "b" "a" "b" "a"}), {"a" "b" "a1" "b1" "a2"}) |
7657 | 201 ## Variable name munging |
18742
56f3c564baaf
genvarname: don't produce names with leading underscored (bug #41923)
Eugenio Gianniti <eugenio.gianniti@mail.polimi.it>
parents:
17745
diff
changeset
|
202 %!assert (genvarname ("__x__"), "x__x__") |
56f3c564baaf
genvarname: don't produce names with leading underscored (bug #41923)
Eugenio Gianniti <eugenio.gianniti@mail.polimi.it>
parents:
17745
diff
changeset
|
203 %!assert (genvarname ("123456789"), "x123456789") |
56f3c564baaf
genvarname: don't produce names with leading underscored (bug #41923)
Eugenio Gianniti <eugenio.gianniti@mail.polimi.it>
parents:
17745
diff
changeset
|
204 %!assert (genvarname ("_$1__"), "x__1__") |
56f3c564baaf
genvarname: don't produce names with leading underscored (bug #41923)
Eugenio Gianniti <eugenio.gianniti@mail.polimi.it>
parents:
17745
diff
changeset
|
205 %!assert (genvarname ("__foo__", "x__foo__"), "x__foo__1") |
56f3c564baaf
genvarname: don't produce names with leading underscored (bug #41923)
Eugenio Gianniti <eugenio.gianniti@mail.polimi.it>
parents:
17745
diff
changeset
|
206 %!assert (genvarname ("1million_and1", "x1million_and1"), "x1million_and1_1") |
7657 | 207 %!assert (genvarname ({"", "", ""}), {"x", "x1", "x2"}) |
18742
56f3c564baaf
genvarname: don't produce names with leading underscored (bug #41923)
Eugenio Gianniti <eugenio.gianniti@mail.polimi.it>
parents:
17745
diff
changeset
|
208 %!assert (genvarname ("if"), "xIf") |
56f3c564baaf
genvarname: don't produce names with leading underscored (bug #41923)
Eugenio Gianniti <eugenio.gianniti@mail.polimi.it>
parents:
17745
diff
changeset
|
209 %!assert (genvarname ({"if", "if", "if"}), {"xIf", "xIf1", "xIf2"}) |
19386 | 210 ## Exclusions in odd format |
211 %!assert (genvarname ("x", {"a", "b"; "x", "d"}), "x1") | |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
212 |
20038
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19898
diff
changeset
|
213 ## Test input validation |
19386 | 214 %!error genvarname () |
215 %!error genvarname (1,2,3) | |
216 %!error <more than one STR is given, it must be a cellstr> genvarname (char ("a", "b", "c")) | |
217 %!error <STR must be a string or cellstr> genvarname (1) | |
218 %!error <more than one exclusion is given, it must be a cellstr> genvarname ("x", char ("a", "b", "c")) | |
219 %!error <EXCLUSIONS must be a string or cellstr> genvarname ("x", 1) | |
220 |