Mercurial > octave-nkf
annotate scripts/miscellaneous/genvarname.m @ 18628:56f3c564baaf
genvarname: don't produce names with leading underscored (bug #41923)
* genvarname.m: Use 'x' instead of '_'.
author | Eugenio Gianniti <eugenio.gianniti@mail.polimi.it> |
---|---|
date | Sun, 23 Mar 2014 20:37:28 +0100 |
parents | 93b3d03b05e7 |
children | 5b64aee2257b |
rev | line source |
---|---|
17744
d63878346099
maint: Update copyright notices for release.
John W. Eaton <jwe@octave.org>
parents:
17702
diff
changeset
|
1 ## Copyright (C) 2008-2013 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}) |
7657 | 22 ## Create unique variable(s) from @var{str}. If @var{exclusions} is |
23 ## given, then the variable(s) will be unique to each other and to | |
24 ## @var{exclusions} (@var{exclusions} may be either a string or a cellstr). | |
25 ## | |
26 ## If @var{str} is a cellstr, then a unique variable is created for each | |
27 ## cell in @var{str}. | |
28 ## | |
29 ## @example | |
30 ## @group | |
31 ## x = 3.141; | |
32 ## genvarname ("x", who ()) | |
14327
4d917a6a858b
doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
33 ## @result{} x1 |
7657 | 34 ## @end group |
35 ## @end example | |
36 ## | |
37 ## If @var{wanted} is a cell array, genvarname will make sure the returned | |
38 ## strings are distinct: | |
39 ## | |
40 ## @example | |
41 ## @group | |
42 ## genvarname (@{"foo", "foo"@}) | |
14327
4d917a6a858b
doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
43 ## @result{} |
4d917a6a858b
doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
44 ## @{ |
4d917a6a858b
doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
45 ## [1,1] = foo |
4d917a6a858b
doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
46 ## [1,2] = foo1 |
4d917a6a858b
doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
47 ## @} |
7657 | 48 ## @end group |
49 ## @end example | |
50 ## | |
51 ## Note that the result is a char array/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
|
52 ## 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
|
53 ## used. The following trivial example sets @code{x} to @code{42}. |
7657 | 54 ## |
55 ## @example | |
56 ## @group | |
57 ## name = genvarname ("x"); | |
14327
4d917a6a858b
doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
58 ## eval ([name " = 42"]); |
4d917a6a858b
doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
59 ## @result{} x = 42 |
7657 | 60 ## @end group |
61 ## @end example | |
62 ## | |
63 ## Also, this can be useful for creating unique struct field names. | |
64 ## | |
65 ## @example | |
66 ## @group | |
67 ## x = struct (); | |
68 ## for i = 1:3 | |
69 ## x.(genvarname ("a", fieldnames (x))) = i; | |
70 ## endfor | |
14327
4d917a6a858b
doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
71 ## @result{} x = |
4d917a6a858b
doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
72 ## @{ |
4d917a6a858b
doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
73 ## a = 1 |
4d917a6a858b
doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
74 ## a1 = 2 |
4d917a6a858b
doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
75 ## a2 = 3 |
4d917a6a858b
doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
76 ## @} |
7657 | 77 ## @end group |
78 ## @end example | |
79 ## | |
80 ## Since variable names may only contain letters, digits and underscores, | |
81 ## genvarname replaces 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
|
82 ## an underscore. Also, variables may not begin with a digit; in this |
18628
56f3c564baaf
genvarname: don't produce names with leading underscored (bug #41923)
Eugenio Gianniti <eugenio.gianniti@mail.polimi.it>
parents:
17745
diff
changeset
|
83 ## case an x is added before the variable name. |
7657 | 84 ## |
17281
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
16994
diff
changeset
|
85 ## Variable names beginning and ending with two underscores @qcode{"__"} are |
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
16994
diff
changeset
|
86 ## valid but they are used internally by octave and should generally be |
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
16994
diff
changeset
|
87 ## avoided, therefore genvarname will not generate such names. |
7657 | 88 ## |
89 ## genvarname will also make sure 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
|
90 ## 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
|
91 ## appended if necessary. Note, however, that this does @strong{not} include |
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
16994
diff
changeset
|
92 ## function names, such as @qcode{"sin"}. Such names should be included in |
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
16994
diff
changeset
|
93 ## @var{avoid} if necessary. |
7657 | 94 ## @seealso{isvarname, exist, tmpnam, eval} |
95 ## @end deftypefn | |
96 | |
97 ## Authors: Rob Platt <robert.platt@postgrad.manchester.ac.uk> | |
98 ## Bill Denney <bill@denney.ws> | |
99 | |
100 function varname = genvarname (str, exclusions) | |
101 | |
102 strinput = ischar (str); | |
103 ## Process the inputs | |
104 if (nargin < 2) | |
105 exclusions = {}; | |
106 elseif (ischar (exclusions)) | |
107 if (rows (exclusions) != 1) | |
8664 | 108 error ("genvarname: if more than one exclusion is given, it must be a cellstr"); |
7657 | 109 endif |
110 exclusions = {exclusions}; | |
111 elseif (! iscellstr (exclusions)) | |
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
|
112 error ("genvarname: EXCLUSIONS must be a string or a cellstr"); |
7657 | 113 endif |
114 if (ischar (str)) | |
115 if (rows (str) != 1) | |
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
|
116 error ("genvarname: if more than one STR is given, it must be a cellstr"); |
7657 | 117 endif |
118 str = {str}; | |
119 elseif (! iscellstr (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
|
120 error ("genvarname: STR must be a string or a cellstr"); |
7657 | 121 endif |
122 | |
16994
333243133364
Use matrix concatenation for strings, rather than cstrcat(), for clarity and performance.
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
123 validchars = ["A":"Z", "a":"z", "0":"9", "_"]; |
7657 | 124 |
125 varname = cell (size (str)); | |
126 for i = 1:numel (str) | |
127 ## Perform any modifications to the varname to make sure that it is | |
128 ## a valid variable name. | |
129 | |
130 ## remove invalid characters | |
131 str{i}(! ismember (str{i}, validchars)) = "_"; | |
132 ## do not use keywords | |
133 if (iskeyword (str{i})) | |
18628
56f3c564baaf
genvarname: don't produce names with leading underscored (bug #41923)
Eugenio Gianniti <eugenio.gianniti@mail.polimi.it>
parents:
17745
diff
changeset
|
134 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
|
135 str{i} = ["x", firstcharacter, str{i}(2:end)]; |
7657 | 136 endif |
137 ## The variable cannot be empty | |
138 if (isempty (str{i})) | |
139 str{i} = "x"; | |
140 endif | |
18628
56f3c564baaf
genvarname: don't produce names with leading underscored (bug #41923)
Eugenio Gianniti <eugenio.gianniti@mail.polimi.it>
parents:
17745
diff
changeset
|
141 ## 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
|
142 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
|
143 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
|
144 endif |
7657 | 145 ## it cannot start with a number |
146 if (ismember (str{i}(1), "0":"9")) | |
18628
56f3c564baaf
genvarname: don't produce names with leading underscored (bug #41923)
Eugenio Gianniti <eugenio.gianniti@mail.polimi.it>
parents:
17745
diff
changeset
|
147 str{i} = ["x", str{i}]; |
7657 | 148 endif |
149 | |
150 ## make sure that the variable is unique relative to other variables | |
151 ## and the exclusions list | |
152 excluded = any (strcmp (str{i}, exclusions)); | |
153 if (excluded && ismember (str{i}(end), "0":"9")) | |
154 ## if it is not unique and ends with a digit, add an underscore to | |
155 ## make the variable name more readable ("x1_1" instead of "x11") | |
156 str{i}(end+1) = "_"; | |
157 endif | |
158 varname(i) = str(i); | |
159 idx = 0; | |
14552
86854d032a37
maint: miscellaneous style fixes for .m files
John W. Eaton <jwe@octave.org>
parents:
14363
diff
changeset
|
160 while (excluded) |
7657 | 161 idx++; |
14868
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14552
diff
changeset
|
162 varname{i} = sprintf ("%s%d", str{i}, idx); |
7657 | 163 excluded = any (strcmp (varname{i}, exclusions)); |
164 endwhile | |
165 exclusions(end+1) = varname(i); | |
166 endfor | |
167 | |
14552
86854d032a37
maint: miscellaneous style fixes for .m files
John W. Eaton <jwe@octave.org>
parents:
14363
diff
changeset
|
168 if (strinput) |
7657 | 169 varname = varname{1}; |
170 endif | |
171 | |
172 endfunction | |
173 | |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
174 |
7657 | 175 ## a single argument |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
176 %!assert (genvarname ("a"), "a") |
7657 | 177 ## 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
|
178 %!assert (genvarname ("a", "b"), "a") |
7657 | 179 ## 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
|
180 %!assert (genvarname ("a", "a"), "a1") |
7657 | 181 ## 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
|
182 %!assert (genvarname ({"a"}), {"a"}) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
183 %!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
|
184 %!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
|
185 %!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
|
186 %!assert (genvarname ({"a"}, {"a"}), {"a1"}) |
7657 | 187 ## Test different arguments |
188 ## orientation | |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
189 %!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
|
190 %!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
|
191 %!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
|
192 %!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
|
193 %!assert (genvarname ({"a" "a" "a";"a" "a" "a"}), {"a" "a2" "a4";"a1" "a3" "a5"}) |
7657 | 194 ## 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
|
195 %!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
|
196 %!assert (genvarname ({"a" "a" "a"}, {"a" "a1" "a2"}), {"a3" "a4" "a5"}) |
7657 | 197 ## 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
|
198 %!assert (genvarname ({"a" "b" "a" "b" "a"}), {"a" "b" "a1" "b1" "a2"}) |
7657 | 199 ## Variable name munging |
18628
56f3c564baaf
genvarname: don't produce names with leading underscored (bug #41923)
Eugenio Gianniti <eugenio.gianniti@mail.polimi.it>
parents:
17745
diff
changeset
|
200 %!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
|
201 %!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
|
202 %!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
|
203 %!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
|
204 %!assert (genvarname ("1million_and1", "x1million_and1"), "x1million_and1_1") |
7657 | 205 %!assert (genvarname ({"", "", ""}), {"x", "x1", "x2"}) |
18628
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 ("if"), "xIf") |
56f3c564baaf
genvarname: don't produce names with leading underscored (bug #41923)
Eugenio Gianniti <eugenio.gianniti@mail.polimi.it>
parents:
17745
diff
changeset
|
207 %!assert (genvarname ({"if", "if", "if"}), {"xIf", "xIf1", "xIf2"}) |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
208 |