Mercurial > hg > octave-lyh
comparison scripts/strings/regexptranslate.m @ 13184:8a124aeb7ded
regexptranslate.m: Correctly escape *all* regular expression special characters.
* regexptranslate.m: Correctly escape *all* regular expression special characters.
Simplify code and move input validation to start of function. Add new tests.
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Tue, 20 Sep 2011 22:01:00 -0700 |
parents | 0ee8d7d60c82 |
children | 72c96de7a403 |
comparison
equal
deleted
inserted
replaced
13183:cc1fd6a58151 | 13184:8a124aeb7ded |
---|---|
16 ## along with Octave; see the file COPYING. If not, see | 16 ## along with Octave; see the file COPYING. If not, see |
17 ## <http://www.gnu.org/licenses/>. | 17 ## <http://www.gnu.org/licenses/>. |
18 | 18 |
19 ## -*- texinfo -*- | 19 ## -*- texinfo -*- |
20 ## @deftypefn {Function File} {} regexptranslate (@var{op}, @var{s}) | 20 ## @deftypefn {Function File} {} regexptranslate (@var{op}, @var{s}) |
21 ## Translate a string for use in a regular expression. This might | 21 ## Translate a string for use in a regular expression. This may |
22 ## include either wildcard replacement or special character escaping. | 22 ## include either wildcard replacement or special character escaping. |
23 ## The behavior can be controlled by the @var{op} that can have the | 23 ## The behavior is controlled by @var{op} which can take the following |
24 ## values | 24 ## values |
25 ## | 25 ## |
26 ## @table @asis | 26 ## @table @asis |
27 ## @item "wildcard" | 27 ## @item "wildcard" |
28 ## The wildcard characters @code{.}, @code{*} and @code{?} are replaced | 28 ## The wildcard characters @code{.}, @code{*}, and @code{?} are replaced |
29 ## with wildcards that are appropriate for a regular expression. | 29 ## with wildcards that are appropriate for a regular expression. |
30 ## For example: | 30 ## For example: |
31 ## | 31 ## |
32 ## @example | 32 ## @example |
33 ## @group | 33 ## @group |
55 | 55 |
56 if nargin != 2 | 56 if nargin != 2 |
57 print_usage (); | 57 print_usage (); |
58 endif | 58 endif |
59 | 59 |
60 if (ischar (op)) | 60 if (! ischar (op)) |
61 op = tolower (op); | 61 error ("regexptranslate: operation OP must be a string"); |
62 if (strcmp ("wildcard", op)) | 62 endif |
63 y = regexprep (regexprep (regexprep (s, '\.', '\.'), '\*', | 63 |
64 '.*'), '\?', '.'); | 64 op = tolower (op); |
65 elseif (strcmp ("escape", op)) | 65 if (strcmp ("wildcard", op)) |
66 ch = {'\$', '\.', '\?', '\[', '\]'}; | 66 y = regexprep (regexprep (regexprep (s, '\.', '\.'), |
67 y = s; | 67 '\*', '.*'), |
68 for i = 1 : length (ch) | 68 '\?', '.'); |
69 y = regexprep (y, ch{i}, ch{i}); | 69 elseif (strcmp ("escape", op)) |
70 endfor | 70 y = regexprep (s, '([^\w])', '\$1'); |
71 else | |
72 error ("regexptranslate: unexpected operation"); | |
73 endif | |
74 else | 71 else |
75 error ("regexptranslate: expecting operation to be a string"); | 72 error ("regexptranslate: invalid operation OP"); |
76 endif | 73 endif |
74 | |
77 endfunction | 75 endfunction |
78 | 76 |
79 %!error <Invalid call to regexptranslate> regexptranslate (); | 77 |
80 %!error <Invalid call to regexptranslate> regexptranslate ("wildcard"); | |
81 %!error <Invalid call to regexptranslate> regexptranslate ("a", "b", "c"); | |
82 %!error <unexpected operation> regexptranslate ("foo", "abc"); | |
83 %!error <expecting operation to be a string> regexptranslate (10, "abc"); | |
84 %!assert (regexptranslate ("wildcard", "/a*b?c."), "/a.*b.c\\.") | 78 %!assert (regexptranslate ("wildcard", "/a*b?c."), "/a.*b.c\\.") |
85 %!assert (regexptranslate ("escape", '$.?[]'), '\$\.\?\[\]') | 79 %!assert (regexptranslate ("escape", '$.?[abc]'), '\$\.\?\[abc\]') |
80 | |
81 %% Test input validation | |
82 %!error <Invalid call to regexptranslate> regexptranslate () | |
83 %!error <Invalid call to regexptranslate> regexptranslate ("wildcard") | |
84 %!error <Invalid call to regexptranslate> regexptranslate ("a", "b", "c") | |
85 %!error <invalid operation> regexptranslate ("foo", "abc") | |
86 %!error <operation OP must be a string> regexptranslate (10, "abc") | |
87 |