annotate scripts/strings/regexptranslate.m @ 20164:df437a52bcaf stable

doc: Update more docstrings to have one sentence summary as first line. Reviewed miscellaneous, sparse, strings in scripts directory. * scripts/miscellaneous/bzip2.m, scripts/miscellaneous/citation.m, scripts/miscellaneous/compare_versions.m, scripts/miscellaneous/computer.m, scripts/miscellaneous/debug.m, scripts/miscellaneous/dir.m, scripts/miscellaneous/edit.m, scripts/miscellaneous/error_ids.m, scripts/miscellaneous/fileattrib.m, scripts/miscellaneous/fullfile.m, scripts/miscellaneous/genvarname.m, scripts/miscellaneous/gzip.m, scripts/miscellaneous/mkoctfile.m, scripts/miscellaneous/news.m, scripts/miscellaneous/open.m, scripts/miscellaneous/parseparams.m, scripts/miscellaneous/recycle.m, scripts/miscellaneous/run.m, scripts/miscellaneous/swapbytes.m, scripts/miscellaneous/tar.m, scripts/miscellaneous/tmpnam.m, scripts/miscellaneous/unpack.m, scripts/miscellaneous/what.m, scripts/sparse/bicg.m, scripts/sparse/bicgstab.m, scripts/sparse/cgs.m, scripts/sparse/colperm.m, scripts/sparse/eigs.m, scripts/sparse/etreeplot.m, scripts/sparse/gmres.m, scripts/sparse/gplot.m, scripts/sparse/ichol.m, scripts/sparse/ilu.m, scripts/sparse/pcg.m, scripts/sparse/pcr.m, scripts/sparse/qmr.m, scripts/sparse/spaugment.m, scripts/sparse/spconvert.m, scripts/sparse/spdiags.m, scripts/sparse/spfun.m, scripts/sparse/spones.m, scripts/sparse/sprandsym.m, scripts/sparse/spstats.m, scripts/sparse/spy.m, scripts/sparse/svds.m, scripts/sparse/treelayout.m, scripts/sparse/treeplot.m, scripts/strings/base2dec.m, scripts/strings/bin2dec.m, scripts/strings/blanks.m, scripts/strings/cstrcat.m, scripts/strings/deblank.m, scripts/strings/dec2base.m, scripts/strings/dec2bin.m, scripts/strings/dec2hex.m, scripts/strings/findstr.m, scripts/strings/hex2dec.m, scripts/strings/index.m, scripts/strings/isletter.m, scripts/strings/isstrprop.m, scripts/strings/mat2str.m, scripts/strings/ostrsplit.m, scripts/strings/regexptranslate.m, scripts/strings/rindex.m, scripts/strings/str2num.m, scripts/strings/strcat.m, scripts/strings/strchr.m, scripts/strings/strjoin.m, scripts/strings/strjust.m, scripts/strings/strmatch.m, scripts/strings/strsplit.m, scripts/strings/strtok.m, scripts/strings/strtrim.m, scripts/strings/strtrunc.m, scripts/strings/substr.m, scripts/strings/untabify.m, scripts/time/datenum.m: Update more docstrings to have one sentence summary as first line.
author Rik <rik@octave.org>
date Mon, 04 May 2015 14:22:02 -0700
parents 9fc020886ae9
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
19697
4197fc428c7d maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents: 19593
diff changeset
1 ## Copyright (C) 2008-2015 David Bateman
7629
cc31c5002c96 Add the regexptranslate function
David Bateman <dbateman@free.fr>
parents:
diff changeset
2 ##
cc31c5002c96 Add the regexptranslate function
David Bateman <dbateman@free.fr>
parents:
diff changeset
3 ## This file is part of Octave.
cc31c5002c96 Add the regexptranslate function
David Bateman <dbateman@free.fr>
parents:
diff changeset
4 ##
cc31c5002c96 Add the regexptranslate function
David Bateman <dbateman@free.fr>
parents:
diff changeset
5 ## Octave is free software; you can redistribute it and/or modify it
cc31c5002c96 Add the regexptranslate function
David Bateman <dbateman@free.fr>
parents:
diff changeset
6 ## under the terms of the GNU General Public License as published by
cc31c5002c96 Add the regexptranslate function
David Bateman <dbateman@free.fr>
parents:
diff changeset
7 ## the Free Software Foundation; either version 3 of the License, or (at
cc31c5002c96 Add the regexptranslate function
David Bateman <dbateman@free.fr>
parents:
diff changeset
8 ## your option) any later version.
cc31c5002c96 Add the regexptranslate function
David Bateman <dbateman@free.fr>
parents:
diff changeset
9 ##
cc31c5002c96 Add the regexptranslate function
David Bateman <dbateman@free.fr>
parents:
diff changeset
10 ## Octave is distributed in the hope that it will be useful, but
cc31c5002c96 Add the regexptranslate function
David Bateman <dbateman@free.fr>
parents:
diff changeset
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
cc31c5002c96 Add the regexptranslate function
David Bateman <dbateman@free.fr>
parents:
diff changeset
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
cc31c5002c96 Add the regexptranslate function
David Bateman <dbateman@free.fr>
parents:
diff changeset
13 ## General Public License for more details.
cc31c5002c96 Add the regexptranslate function
David Bateman <dbateman@free.fr>
parents:
diff changeset
14 ##
cc31c5002c96 Add the regexptranslate function
David Bateman <dbateman@free.fr>
parents:
diff changeset
15 ## You should have received a copy of the GNU General Public License
cc31c5002c96 Add the regexptranslate function
David Bateman <dbateman@free.fr>
parents:
diff changeset
16 ## along with Octave; see the file COPYING. If not, see
cc31c5002c96 Add the regexptranslate function
David Bateman <dbateman@free.fr>
parents:
diff changeset
17 ## <http://www.gnu.org/licenses/>.
cc31c5002c96 Add the regexptranslate function
David Bateman <dbateman@free.fr>
parents:
diff changeset
18
cc31c5002c96 Add the regexptranslate function
David Bateman <dbateman@free.fr>
parents:
diff changeset
19 ## -*- texinfo -*-
7983
91d020444da7 Document regexptranslate
David Bateman <dbateman@free.fr>
parents: 7629
diff changeset
20 ## @deftypefn {Function File} {} regexptranslate (@var{op}, @var{s})
20164
df437a52bcaf doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
21 ## Translate a string for use in a regular expression.
df437a52bcaf doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
22 ##
df437a52bcaf doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
23 ## This may include either wildcard replacement or special character escaping.
df437a52bcaf doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
24 ##
13184
8a124aeb7ded regexptranslate.m: Correctly escape *all* regular expression special characters.
Rik <octave@nomad.inbox5.com>
parents: 12469
diff changeset
25 ## The behavior is controlled by @var{op} which can take the following
7983
91d020444da7 Document regexptranslate
David Bateman <dbateman@free.fr>
parents: 7629
diff changeset
26 ## values
91d020444da7 Document regexptranslate
David Bateman <dbateman@free.fr>
parents: 7629
diff changeset
27 ##
91d020444da7 Document regexptranslate
David Bateman <dbateman@free.fr>
parents: 7629
diff changeset
28 ## @table @asis
17281
bc924baa2c4e doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents: 14560
diff changeset
29 ## @item @qcode{"wildcard"}
20164
df437a52bcaf doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
30 ## The wildcard characters @code{.}, @code{*}, and @code{?} are replaced with
df437a52bcaf doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
31 ## wildcards that are appropriate for a regular expression. For example:
10821
693e22af08ae Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents: 10549
diff changeset
32 ##
8442
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7983
diff changeset
33 ## @example
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7983
diff changeset
34 ## @group
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7983
diff changeset
35 ## regexptranslate ("wildcard", "*.m")
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7983
diff changeset
36 ## @result{} ".*\.m"
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7983
diff changeset
37 ## @end group
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7983
diff changeset
38 ## @end example
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
39 ##
17281
bc924baa2c4e doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents: 14560
diff changeset
40 ## @item @qcode{"escape"}
7983
91d020444da7 Document regexptranslate
David Bateman <dbateman@free.fr>
parents: 7629
diff changeset
41 ## The characters @code{$.?[]}, that have special meaning for regular
9036
58604c45ca74 Cleanup of data types related documentation
Rik <rdrider0-list@yahoo.com>
parents: 8920
diff changeset
42 ## expressions are escaped so that they are treated literally. For example:
10821
693e22af08ae Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents: 10549
diff changeset
43 ##
8442
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7983
diff changeset
44 ## @example
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7983
diff changeset
45 ## @group
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7983
diff changeset
46 ## regexptranslate ("escape", "12.5")
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7983
diff changeset
47 ## @result{} "12\.5"
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7983
diff changeset
48 ## @end group
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7983
diff changeset
49 ## @end example
10821
693e22af08ae Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents: 10549
diff changeset
50 ##
7983
91d020444da7 Document regexptranslate
David Bateman <dbateman@free.fr>
parents: 7629
diff changeset
51 ## @end table
8442
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7983
diff changeset
52 ## @seealso{regexp, regexpi, regexprep}
7629
cc31c5002c96 Add the regexptranslate function
David Bateman <dbateman@free.fr>
parents:
diff changeset
53 ## @end deftypefn
cc31c5002c96 Add the regexptranslate function
David Bateman <dbateman@free.fr>
parents:
diff changeset
54
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10821
diff changeset
55 function y = regexptranslate (op, s)
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
56
14552
86854d032a37 maint: miscellaneous style fixes for .m files
John W. Eaton <jwe@octave.org>
parents: 14138
diff changeset
57 if (nargin != 2)
8442
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7983
diff changeset
58 print_usage ();
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
59 endif
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
60
13184
8a124aeb7ded regexptranslate.m: Correctly escape *all* regular expression special characters.
Rik <octave@nomad.inbox5.com>
parents: 12469
diff changeset
61 if (! ischar (op))
8a124aeb7ded regexptranslate.m: Correctly escape *all* regular expression special characters.
Rik <octave@nomad.inbox5.com>
parents: 12469
diff changeset
62 error ("regexptranslate: operation OP must be a string");
8a124aeb7ded regexptranslate.m: Correctly escape *all* regular expression special characters.
Rik <octave@nomad.inbox5.com>
parents: 12469
diff changeset
63 endif
8a124aeb7ded regexptranslate.m: Correctly escape *all* regular expression special characters.
Rik <octave@nomad.inbox5.com>
parents: 12469
diff changeset
64
8a124aeb7ded regexptranslate.m: Correctly escape *all* regular expression special characters.
Rik <octave@nomad.inbox5.com>
parents: 12469
diff changeset
65 op = tolower (op);
8a124aeb7ded regexptranslate.m: Correctly escape *all* regular expression special characters.
Rik <octave@nomad.inbox5.com>
parents: 12469
diff changeset
66 if (strcmp ("wildcard", op))
19593
446c46af4b42 strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 17744
diff changeset
67 y = regexprep (regexprep (regexprep (s, '\.', '\.'),
446c46af4b42 strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 17744
diff changeset
68 '\*', '.*'),
13184
8a124aeb7ded regexptranslate.m: Correctly escape *all* regular expression special characters.
Rik <octave@nomad.inbox5.com>
parents: 12469
diff changeset
69 '\?', '.');
8a124aeb7ded regexptranslate.m: Correctly escape *all* regular expression special characters.
Rik <octave@nomad.inbox5.com>
parents: 12469
diff changeset
70 elseif (strcmp ("escape", op))
14560
638390eacec4 regexptranslate.m: Fix failing %!test due to new regexp single quote rules.
Rik <octave@nomad.inbox5.com>
parents: 14552
diff changeset
71 y = regexprep (s, '([^\w])', '\\$1');
7629
cc31c5002c96 Add the regexptranslate function
David Bateman <dbateman@free.fr>
parents:
diff changeset
72 else
13184
8a124aeb7ded regexptranslate.m: Correctly escape *all* regular expression special characters.
Rik <octave@nomad.inbox5.com>
parents: 12469
diff changeset
73 error ("regexptranslate: invalid operation OP");
7629
cc31c5002c96 Add the regexptranslate function
David Bateman <dbateman@free.fr>
parents:
diff changeset
74 endif
13184
8a124aeb7ded regexptranslate.m: Correctly escape *all* regular expression special characters.
Rik <octave@nomad.inbox5.com>
parents: 12469
diff changeset
75
7629
cc31c5002c96 Add the regexptranslate function
David Bateman <dbateman@free.fr>
parents:
diff changeset
76 endfunction
cc31c5002c96 Add the regexptranslate function
David Bateman <dbateman@free.fr>
parents:
diff changeset
77
13184
8a124aeb7ded regexptranslate.m: Correctly escape *all* regular expression special characters.
Rik <octave@nomad.inbox5.com>
parents: 12469
diff changeset
78
7629
cc31c5002c96 Add the regexptranslate function
David Bateman <dbateman@free.fr>
parents:
diff changeset
79 %!assert (regexptranslate ("wildcard", "/a*b?c."), "/a.*b.c\\.")
13184
8a124aeb7ded regexptranslate.m: Correctly escape *all* regular expression special characters.
Rik <octave@nomad.inbox5.com>
parents: 12469
diff changeset
80 %!assert (regexptranslate ("escape", '$.?[abc]'), '\$\.\?\[abc\]')
8a124aeb7ded regexptranslate.m: Correctly escape *all* regular expression special characters.
Rik <octave@nomad.inbox5.com>
parents: 12469
diff changeset
81
19833
9fc020886ae9 maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19697
diff changeset
82 ## Test input validation
13184
8a124aeb7ded regexptranslate.m: Correctly escape *all* regular expression special characters.
Rik <octave@nomad.inbox5.com>
parents: 12469
diff changeset
83 %!error <Invalid call to regexptranslate> regexptranslate ()
8a124aeb7ded regexptranslate.m: Correctly escape *all* regular expression special characters.
Rik <octave@nomad.inbox5.com>
parents: 12469
diff changeset
84 %!error <Invalid call to regexptranslate> regexptranslate ("wildcard")
8a124aeb7ded regexptranslate.m: Correctly escape *all* regular expression special characters.
Rik <octave@nomad.inbox5.com>
parents: 12469
diff changeset
85 %!error <Invalid call to regexptranslate> regexptranslate ("a", "b", "c")
8a124aeb7ded regexptranslate.m: Correctly escape *all* regular expression special characters.
Rik <octave@nomad.inbox5.com>
parents: 12469
diff changeset
86 %!error <invalid operation> regexptranslate ("foo", "abc")
8a124aeb7ded regexptranslate.m: Correctly escape *all* regular expression special characters.
Rik <octave@nomad.inbox5.com>
parents: 12469
diff changeset
87 %!error <operation OP must be a string> regexptranslate (10, "abc")
8a124aeb7ded regexptranslate.m: Correctly escape *all* regular expression special characters.
Rik <octave@nomad.inbox5.com>
parents: 12469
diff changeset
88