Mercurial > hg > octave-nkf
annotate scripts/general/genvarname.m @ 9037:4cb9f994dcec
Documentation cleanup of var.texi, expr.texi, eval.texi
Spellcheck
Style check (particularly for two spaces after period)
author | Rik <rdrider0-list@yahoo.com> |
---|---|
date | Sun, 22 Mar 2009 08:41:49 -0700 |
parents | eb63fbe60fab |
children | be55736a0783 |
rev | line source |
---|---|
8920 | 1 ## Copyright (C) 2008, 2009 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 -*- | |
7975
ed4ec7875f98
trival doc fix for genvarname
David Bateman <dbateman@free.fr>
parents:
7657
diff
changeset
|
20 ## @deftypefn {Function File} {@var{varname} =} genvarname (@var{str}) |
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 ()) | |
33 ## @result{} x1 | |
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"@}) | |
43 ## @result{} | |
44 ## @{ | |
45 ## [1,1] = foo | |
46 ## [1,2] = foo1 | |
47 ## @} | |
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"); | |
58 ## eval([name " = 42"]); | |
59 ## @result{} x = 42 | |
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 | |
71 ## @result{} | |
72 ## x = | |
73 ## @{ | |
74 ## a = 1 | |
75 ## a1 = 2 | |
76 ## a2 = 3 | |
77 ## @} | |
78 ## @end group | |
79 ## @end example | |
80 ## | |
81 ## Since variable names may only contain letters, digits and underscores, | |
82 ## 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
|
83 ## an underscore. Also, variables may not begin with a digit; in this |
7657 | 84 ## case an underscore is added before the variable name. |
85 ## | |
86 ## Variable names beginning and ending with two underscores "__" are valid but | |
87 ## they are used internally by octave and should generally be avoided, therefore | |
88 ## genvarname will not generate such names. | |
89 ## | |
90 ## genvarname will also make sure that returned names do not clash with | |
9037
4cb9f994dcec
Documentation cleanup of var.texi, expr.texi, eval.texi
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
91 ## keywords such as "for" and "if". A number will be appended if necessary. |
7657 | 92 ## Note, however, that this does @strong{not} include function names, |
9037
4cb9f994dcec
Documentation cleanup of var.texi, expr.texi, eval.texi
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
93 ## such as "sin". Such names should be included in @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)) | |
8664 | 112 error ("genvarname: exclusions must be a string or a cellstr"); |
7657 | 113 endif |
114 if (ischar (str)) | |
115 if (rows (str) != 1) | |
8664 | 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)) | |
8664 | 120 error ("genvarname: str must be a string or a cellstr"); |
7657 | 121 endif |
122 | |
123 validchars = cstrcat ("A":"Z", "a":"z", "0":"9", "_"); | |
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})) | |
134 str{i} = cstrcat ("_", str{i}); | |
135 endif | |
136 ## double underscores at the beginning and end are reserved variables | |
137 underscores = (str{i} == "_"); | |
138 if (any (underscores)) | |
139 firstnon = find (!underscores, 1); | |
140 lastnon = find (!underscores, 1, "last"); | |
141 str{i}([1:firstnon-2, lastnon+2:end]) = []; | |
142 endif | |
143 ## The variable cannot be empty | |
144 if (isempty (str{i})) | |
145 str{i} = "x"; | |
146 endif | |
147 ## it cannot start with a number | |
148 if (ismember (str{i}(1), "0":"9")) | |
149 str{i} = cstrcat ("_", str{i}); | |
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)); | |
155 if (excluded && ismember (str{i}(end), "0":"9")) | |
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") | |
158 str{i}(end+1) = "_"; | |
159 endif | |
160 varname(i) = str(i); | |
161 idx = 0; | |
162 while excluded | |
163 idx++; | |
164 varname{i} = sprintf("%s%d", str{i}, idx); | |
165 excluded = any (strcmp (varname{i}, exclusions)); | |
166 endwhile | |
167 exclusions(end+1) = varname(i); | |
168 endfor | |
169 | |
170 if strinput | |
171 varname = varname{1}; | |
172 endif | |
173 | |
174 endfunction | |
175 | |
176 ## Tests | |
177 ## a single argument | |
178 %!assert(genvarname("a"), "a") | |
179 ## a single argument with a non-conflicting exception | |
180 %!assert(genvarname("a", "b"), "a") | |
181 ## a single argument with a conflicting exception | |
182 %!assert(genvarname("a", "a"), "a1") | |
183 ## a single argument as a cell | |
184 %!assert(genvarname({"a"}), {"a"}) | |
185 %!assert(genvarname({"a"}, "b"), {"a"}) | |
186 %!assert(genvarname({"a"}, {"b"}), {"a"}) | |
187 %!assert(genvarname({"a"}, "a"), {"a1"}) | |
188 %!assert(genvarname({"a"}, {"a"}), {"a1"}) | |
189 ## Test different arguments | |
190 ## orientation | |
191 %!assert(genvarname({"a" "b"}), {"a" "b"}) | |
192 %!assert(genvarname({"a";"b"}), {"a";"b"}) | |
193 %!assert(genvarname({"a" "a"}), {"a" "a1"}) | |
194 %!assert(genvarname({"a" "b";"c" "d"}), {"a" "b";"c" "d"}) | |
195 %!assert(genvarname({"a" "a" "a";"a" "a" "a"}), {"a" "a2" "a4";"a1" "a3" "a5"}) | |
196 ## more than one repetition | |
197 %!assert(genvarname({"a" "a" "a"}), {"a" "a1" "a2"}) | |
198 %!assert(genvarname({"a" "a" "a"}, {"a" "a1" "a2"}), {"a3" "a4" "a5"}) | |
199 ## more than one repetition not in order | |
200 %!assert(genvarname({"a" "b" "a" "b" "a"}), {"a" "b" "a1" "b1" "a2"}) | |
201 ## Variable name munging | |
202 %!assert (genvarname ("__x__"), "_x_") | |
203 %!assert (genvarname ("123456789"), "_123456789") | |
204 %!assert (genvarname ("_$1__"), "_1_") | |
205 %!assert (genvarname ("__foo__", "_foo_"), "_foo_1") | |
206 %!assert (genvarname ("1million_and1", "_1million_and1"), "_1million_and1_1") | |
207 %!assert (genvarname ({"", "", ""}), {"x", "x1", "x2"}) | |
208 %!assert (genvarname ("if"), "_if") | |
209 %!assert (genvarname ({"if", "if", "if"}), {"_if", "_if1", "_if2"}) |