annotate scripts/strings/regexptranslate.m @ 31242:96ad887ae4f8 stable

dec2bin.m and dec2hex.m: graft bugfixes from dd6b37f67db2 to stable (bug #63089) Fix behavior of dec2bin and dec2hex for negative integer-type inputs
author Arun Giridhar <arungiridhar@gmail.com>
date Wed, 28 Sep 2022 17:00:43 -0400
parents 796f54d4ddbf
children 5d3faba0342e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
1 ########################################################################
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
2 ##
30564
796f54d4ddbf update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents: 29359
diff changeset
3 ## Copyright (C) 2008-2022 The Octave Project Developers
27918
b442ec6dda5c use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
4 ##
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
5 ## See the file COPYRIGHT.md in the top-level directory of this
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
6 ## distribution or <https://octave.org/copyright/>.
7629
cc31c5002c96 Add the regexptranslate function
David Bateman <dbateman@free.fr>
parents:
diff changeset
7 ##
cc31c5002c96 Add the regexptranslate function
David Bateman <dbateman@free.fr>
parents:
diff changeset
8 ## This file is part of Octave.
cc31c5002c96 Add the regexptranslate function
David Bateman <dbateman@free.fr>
parents:
diff changeset
9 ##
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 23220
diff changeset
10 ## Octave is free software: you can redistribute it and/or modify it
7629
cc31c5002c96 Add the regexptranslate function
David Bateman <dbateman@free.fr>
parents:
diff changeset
11 ## under the terms of the GNU General Public License as published by
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 23220
diff changeset
12 ## the Free Software Foundation, either version 3 of the License, or
22755
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22323
diff changeset
13 ## (at your option) any later version.
7629
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 ## 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
16 ## WITHOUT ANY WARRANTY; without even the implied warranty of
22755
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22323
diff changeset
17 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22323
diff changeset
18 ## GNU General Public License for more details.
7629
cc31c5002c96 Add the regexptranslate function
David Bateman <dbateman@free.fr>
parents:
diff changeset
19 ##
cc31c5002c96 Add the regexptranslate function
David Bateman <dbateman@free.fr>
parents:
diff changeset
20 ## 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
21 ## along with Octave; see the file COPYING. If not, see
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 23220
diff changeset
22 ## <https://www.gnu.org/licenses/>.
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
23 ##
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
24 ########################################################################
7629
cc31c5002c96 Add the regexptranslate function
David Bateman <dbateman@free.fr>
parents:
diff changeset
25
cc31c5002c96 Add the regexptranslate function
David Bateman <dbateman@free.fr>
parents:
diff changeset
26 ## -*- texinfo -*-
20852
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 20180
diff changeset
27 ## @deftypefn {} {} 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
28 ## 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
29 ##
df437a52bcaf doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
30 ## 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
31 ##
13184
8a124aeb7ded regexptranslate.m: Correctly escape *all* regular expression special characters.
Rik <octave@nomad.inbox5.com>
parents: 12469
diff changeset
32 ## 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
33 ## values
91d020444da7 Document regexptranslate
David Bateman <dbateman@free.fr>
parents: 7629
diff changeset
34 ##
91d020444da7 Document regexptranslate
David Bateman <dbateman@free.fr>
parents: 7629
diff changeset
35 ## @table @asis
17281
bc924baa2c4e doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents: 14560
diff changeset
36 ## @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
37 ## 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
38 ## 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
39 ##
8442
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7983
diff changeset
40 ## @example
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7983
diff changeset
41 ## @group
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7983
diff changeset
42 ## regexptranslate ("wildcard", "*.m")
24595
5ed2e9248ee9 doc: Tweaks to use single quotes instead of double quotes (bug #52870).
Rik <rik@octave.org>
parents: 23219
diff changeset
43 ## @result{} '.*\.m'
8442
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7983
diff changeset
44 ## @end group
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7983
diff changeset
45 ## @end example
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
46 ##
17281
bc924baa2c4e doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents: 14560
diff changeset
47 ## @item @qcode{"escape"}
7983
91d020444da7 Document regexptranslate
David Bateman <dbateman@free.fr>
parents: 7629
diff changeset
48 ## 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
49 ## 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
50 ##
8442
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7983
diff changeset
51 ## @example
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7983
diff changeset
52 ## @group
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7983
diff changeset
53 ## regexptranslate ("escape", "12.5")
24595
5ed2e9248ee9 doc: Tweaks to use single quotes instead of double quotes (bug #52870).
Rik <rik@octave.org>
parents: 23219
diff changeset
54 ## @result{} '12\.5'
8442
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7983
diff changeset
55 ## @end group
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7983
diff changeset
56 ## @end example
10821
693e22af08ae Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents: 10549
diff changeset
57 ##
7983
91d020444da7 Document regexptranslate
David Bateman <dbateman@free.fr>
parents: 7629
diff changeset
58 ## @end table
8442
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7983
diff changeset
59 ## @seealso{regexp, regexpi, regexprep}
7629
cc31c5002c96 Add the regexptranslate function
David Bateman <dbateman@free.fr>
parents:
diff changeset
60 ## @end deftypefn
cc31c5002c96 Add the regexptranslate function
David Bateman <dbateman@free.fr>
parents:
diff changeset
61
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10821
diff changeset
62 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
63
14552
86854d032a37 maint: miscellaneous style fixes for .m files
John W. Eaton <jwe@octave.org>
parents: 14138
diff changeset
64 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
65 print_usage ();
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
66 endif
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
67
13184
8a124aeb7ded regexptranslate.m: Correctly escape *all* regular expression special characters.
Rik <octave@nomad.inbox5.com>
parents: 12469
diff changeset
68 if (! ischar (op))
8a124aeb7ded regexptranslate.m: Correctly escape *all* regular expression special characters.
Rik <octave@nomad.inbox5.com>
parents: 12469
diff changeset
69 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
70 endif
8a124aeb7ded regexptranslate.m: Correctly escape *all* regular expression special characters.
Rik <octave@nomad.inbox5.com>
parents: 12469
diff changeset
71
8a124aeb7ded regexptranslate.m: Correctly escape *all* regular expression special characters.
Rik <octave@nomad.inbox5.com>
parents: 12469
diff changeset
72 op = tolower (op);
8a124aeb7ded regexptranslate.m: Correctly escape *all* regular expression special characters.
Rik <octave@nomad.inbox5.com>
parents: 12469
diff changeset
73 if (strcmp ("wildcard", op))
20180
89616a98b02e regexptranslate.m: Fix 'escape' option list of special chars (bug #45084).
Rik <rik@octave.org>
parents: 20164
diff changeset
74 y = strrep (strrep (strrep (s, '.', '\.'),
89616a98b02e regexptranslate.m: Fix 'escape' option list of special chars (bug #45084).
Rik <rik@octave.org>
parents: 20164
diff changeset
75 '*', '.*'),
89616a98b02e regexptranslate.m: Fix 'escape' option list of special chars (bug #45084).
Rik <rik@octave.org>
parents: 20164
diff changeset
76 '?', '.');
13184
8a124aeb7ded regexptranslate.m: Correctly escape *all* regular expression special characters.
Rik <octave@nomad.inbox5.com>
parents: 12469
diff changeset
77 elseif (strcmp ("escape", op))
20180
89616a98b02e regexptranslate.m: Fix 'escape' option list of special chars (bug #45084).
Rik <rik@octave.org>
parents: 20164
diff changeset
78 y = regexprep (s, '([][(){}.*+?^$|\\])', '\\$1');
7629
cc31c5002c96 Add the regexptranslate function
David Bateman <dbateman@free.fr>
parents:
diff changeset
79 else
13184
8a124aeb7ded regexptranslate.m: Correctly escape *all* regular expression special characters.
Rik <octave@nomad.inbox5.com>
parents: 12469
diff changeset
80 error ("regexptranslate: invalid operation OP");
7629
cc31c5002c96 Add the regexptranslate function
David Bateman <dbateman@free.fr>
parents:
diff changeset
81 endif
13184
8a124aeb7ded regexptranslate.m: Correctly escape *all* regular expression special characters.
Rik <octave@nomad.inbox5.com>
parents: 12469
diff changeset
82
7629
cc31c5002c96 Add the regexptranslate function
David Bateman <dbateman@free.fr>
parents:
diff changeset
83 endfunction
cc31c5002c96 Add the regexptranslate function
David Bateman <dbateman@free.fr>
parents:
diff changeset
84
13184
8a124aeb7ded regexptranslate.m: Correctly escape *all* regular expression special characters.
Rik <octave@nomad.inbox5.com>
parents: 12469
diff changeset
85
7629
cc31c5002c96 Add the regexptranslate function
David Bateman <dbateman@free.fr>
parents:
diff changeset
86 %!assert (regexptranslate ("wildcard", "/a*b?c."), "/a.*b.c\\.")
20180
89616a98b02e regexptranslate.m: Fix 'escape' option list of special chars (bug #45084).
Rik <rik@octave.org>
parents: 20164
diff changeset
87 %!assert (regexptranslate ("escape", '^.?[abc]$'), '\^\.\?\[abc\]\$')
13184
8a124aeb7ded regexptranslate.m: Correctly escape *all* regular expression special characters.
Rik <octave@nomad.inbox5.com>
parents: 12469
diff changeset
88
19833
9fc020886ae9 maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19697
diff changeset
89 ## Test input validation
13184
8a124aeb7ded regexptranslate.m: Correctly escape *all* regular expression special characters.
Rik <octave@nomad.inbox5.com>
parents: 12469
diff changeset
90 %!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
91 %!error <Invalid call to regexptranslate> regexptranslate ("wildcard")
28564
1dd765e54265 fix tests for new argument number mismatch error message
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
92 %!error <called with too many inputs> regexptranslate ("a", "b", "c")
13184
8a124aeb7ded regexptranslate.m: Correctly escape *all* regular expression special characters.
Rik <octave@nomad.inbox5.com>
parents: 12469
diff changeset
93 %!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
94 %!error <operation OP must be a string> regexptranslate (10, "abc")