]> Creatis software - CreaPhase.git/blob - octave_packages/m/strings/regexptranslate.m
update packages
[CreaPhase.git] / octave_packages / m / strings / regexptranslate.m
1 ## Copyright (C) 2008-2012 David Bateman
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 -*-
20 ## @deftypefn {Function File} {} regexptranslate (@var{op}, @var{s})
21 ## Translate a string for use in a regular expression.  This may
22 ## include either wildcard replacement or special character escaping.
23 ## The behavior is controlled by @var{op} which can take the following
24 ## values
25 ##
26 ## @table @asis
27 ## @item "wildcard"
28 ## The wildcard characters @code{.}, @code{*}, and @code{?} are replaced
29 ## with wildcards that are appropriate for a regular expression.
30 ## For example:
31 ##
32 ## @example
33 ## @group
34 ## regexptranslate ("wildcard", "*.m")
35 ##      @result{} ".*\.m"
36 ## @end group
37 ## @end example
38 ##
39 ## @item "escape"
40 ## The characters @code{$.?[]}, that have special meaning for regular
41 ## expressions are escaped so that they are treated literally.  For example:
42 ##
43 ## @example
44 ## @group
45 ## regexptranslate ("escape", "12.5")
46 ##      @result{} "12\.5"
47 ## @end group
48 ## @end example
49 ##
50 ## @end table
51 ## @seealso{regexp, regexpi, regexprep}
52 ## @end deftypefn
53
54 function y = regexptranslate (op, s)
55
56   if nargin != 2
57     print_usage ();
58   endif
59
60   if (! ischar (op))
61     error ("regexptranslate: operation OP must be a string");
62   endif
63
64   op = tolower (op);
65   if (strcmp ("wildcard", op))
66     y = regexprep (regexprep (regexprep (s, '\.', '\.'), 
67                                             '\*', '.*'), 
68                                             '\?', '.');
69   elseif (strcmp ("escape", op))
70     y = regexprep (s, '([^\w])', '\$1');
71   else
72     error ("regexptranslate: invalid operation OP");
73   endif
74
75 endfunction
76
77
78 %!assert (regexptranslate ("wildcard", "/a*b?c."), "/a.*b.c\\.")
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