Mercurial > octave-dspies
comparison scripts/miscellaneous/genvarname.m @ 18594: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 |
comparison
equal
deleted
inserted
replaced
18592:75467145096f | 18594:56f3c564baaf |
---|---|
78 ## @end example | 78 ## @end example |
79 ## | 79 ## |
80 ## Since variable names may only contain letters, digits and underscores, | 80 ## Since variable names may only contain letters, digits and underscores, |
81 ## genvarname replaces any sequence of disallowed characters with | 81 ## genvarname replaces any sequence of disallowed characters with |
82 ## an underscore. Also, variables may not begin with a digit; in this | 82 ## an underscore. Also, variables may not begin with a digit; in this |
83 ## case an underscore is added before the variable name. | 83 ## case an x is added before the variable name. |
84 ## | 84 ## |
85 ## Variable names beginning and ending with two underscores @qcode{"__"} are | 85 ## Variable names beginning and ending with two underscores @qcode{"__"} are |
86 ## valid but they are used internally by octave and should generally be | 86 ## valid but they are used internally by octave and should generally be |
87 ## avoided, therefore genvarname will not generate such names. | 87 ## avoided, therefore genvarname will not generate such names. |
88 ## | 88 ## |
129 | 129 |
130 ## remove invalid characters | 130 ## remove invalid characters |
131 str{i}(! ismember (str{i}, validchars)) = "_"; | 131 str{i}(! ismember (str{i}, validchars)) = "_"; |
132 ## do not use keywords | 132 ## do not use keywords |
133 if (iskeyword (str{i})) | 133 if (iskeyword (str{i})) |
134 str{i} = ["_" str{i}]; | 134 firstcharacter = toupper (str{i}(1)); |
135 endif | 135 str{i} = ["x", firstcharacter, str{i}(2:end)]; |
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 | 136 endif |
143 ## The variable cannot be empty | 137 ## The variable cannot be empty |
144 if (isempty (str{i})) | 138 if (isempty (str{i})) |
145 str{i} = "x"; | 139 str{i} = "x"; |
146 endif | 140 endif |
141 ## Leading underscores are not Matlab compatible | |
142 if (str{i}(1) == "_") | |
143 str{i} = ["x", str{i}]; | |
144 endif | |
147 ## it cannot start with a number | 145 ## it cannot start with a number |
148 if (ismember (str{i}(1), "0":"9")) | 146 if (ismember (str{i}(1), "0":"9")) |
149 str{i} = ["_" str{i}]; | 147 str{i} = ["x", str{i}]; |
150 endif | 148 endif |
151 | 149 |
152 ## make sure that the variable is unique relative to other variables | 150 ## make sure that the variable is unique relative to other variables |
153 ## and the exclusions list | 151 ## and the exclusions list |
154 excluded = any (strcmp (str{i}, exclusions)); | 152 excluded = any (strcmp (str{i}, exclusions)); |
197 %!assert (genvarname ({"a" "a" "a"}), {"a" "a1" "a2"}) | 195 %!assert (genvarname ({"a" "a" "a"}), {"a" "a1" "a2"}) |
198 %!assert (genvarname ({"a" "a" "a"}, {"a" "a1" "a2"}), {"a3" "a4" "a5"}) | 196 %!assert (genvarname ({"a" "a" "a"}, {"a" "a1" "a2"}), {"a3" "a4" "a5"}) |
199 ## more than one repetition not in order | 197 ## more than one repetition not in order |
200 %!assert (genvarname ({"a" "b" "a" "b" "a"}), {"a" "b" "a1" "b1" "a2"}) | 198 %!assert (genvarname ({"a" "b" "a" "b" "a"}), {"a" "b" "a1" "b1" "a2"}) |
201 ## Variable name munging | 199 ## Variable name munging |
202 %!assert (genvarname ("__x__"), "_x_") | 200 %!assert (genvarname ("__x__"), "x__x__") |
203 %!assert (genvarname ("123456789"), "_123456789") | 201 %!assert (genvarname ("123456789"), "x123456789") |
204 %!assert (genvarname ("_$1__"), "_1_") | 202 %!assert (genvarname ("_$1__"), "x__1__") |
205 %!assert (genvarname ("__foo__", "_foo_"), "_foo_1") | 203 %!assert (genvarname ("__foo__", "x__foo__"), "x__foo__1") |
206 %!assert (genvarname ("1million_and1", "_1million_and1"), "_1million_and1_1") | 204 %!assert (genvarname ("1million_and1", "x1million_and1"), "x1million_and1_1") |
207 %!assert (genvarname ({"", "", ""}), {"x", "x1", "x2"}) | 205 %!assert (genvarname ({"", "", ""}), {"x", "x1", "x2"}) |
208 %!assert (genvarname ("if"), "_if") | 206 %!assert (genvarname ("if"), "xIf") |
209 %!assert (genvarname ({"if", "if", "if"}), {"_if", "_if1", "_if2"}) | 207 %!assert (genvarname ({"if", "if", "if"}), {"xIf", "xIf1", "xIf2"}) |
210 | 208 |