annotate scripts/strings/strrep.m @ 9209:923c7cb7f13f

Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction. spellchecked all .txi and .texi files.
author Rik <rdrider0-list@yahoo.com>
date Sun, 17 May 2009 12:18:06 -0700
parents 1bf0ce0930be
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
8920
eb63fbe60fab update copyright notices
John W. Eaton <jwe@octave.org>
parents: 8525
diff changeset
1 ## Copyright (C) 1995, 1996, 1998, 1999, 2000, 2005, 2006, 2007, 2008,
eb63fbe60fab update copyright notices
John W. Eaton <jwe@octave.org>
parents: 8525
diff changeset
2 ## 2009 Kurt Hornik
2325
b5568c31ee2c [project @ 1996-07-15 22:20:21 by jwe]
jwe
parents: 2313
diff changeset
3 ##
2313
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
4 ## This file is part of Octave.
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
5 ##
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
6 ## Octave is free software; you can redistribute it and/or modify it
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
7 ## under the terms of the GNU General Public License as published by
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
8 ## the Free Software Foundation; either version 3 of the License, or (at
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
9 ## your option) any later version.
2313
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
10 ##
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
11 ## Octave is distributed in the hope that it will be useful, but
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
14 ## General Public License for more details.
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
15 ##
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
16 ## You should have received a copy of the GNU General Public License
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
17 ## along with Octave; see the file COPYING. If not, see
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
18 ## <http://www.gnu.org/licenses/>.
2288
039cda4dc95a [project @ 1996-06-05 23:21:52 by jwe]
jwe
parents:
diff changeset
19
3361
4f40efa995c1 [project @ 1999-11-19 21:19:37 by jwe]
jwe
parents: 3180
diff changeset
20 ## -*- texinfo -*-
4f40efa995c1 [project @ 1999-11-19 21:19:37 by jwe]
jwe
parents: 3180
diff changeset
21 ## @deftypefn {Function File} {} strrep (@var{s}, @var{x}, @var{y})
8525
bf6befcfa70a strrep.m: doc fix
John W. Eaton <jwe@octave.org>
parents: 8507
diff changeset
22 ## Replace all occurrences of the substring @var{x} of the string @var{s}
bf6befcfa70a strrep.m: doc fix
John W. Eaton <jwe@octave.org>
parents: 8507
diff changeset
23 ## with the string @var{y} and return the result. For example,
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3361
diff changeset
24 ##
3361
4f40efa995c1 [project @ 1999-11-19 21:19:37 by jwe]
jwe
parents: 3180
diff changeset
25 ## @example
9051
1bf0ce0930be Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents: 8920
diff changeset
26 ## @group
3361
4f40efa995c1 [project @ 1999-11-19 21:19:37 by jwe]
jwe
parents: 3180
diff changeset
27 ## strrep ("This is a test string", "is", "&%$")
4f40efa995c1 [project @ 1999-11-19 21:19:37 by jwe]
jwe
parents: 3180
diff changeset
28 ## @result{} "Th&%$ &%$ a test string"
9051
1bf0ce0930be Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents: 8920
diff changeset
29 ## @end group
3361
4f40efa995c1 [project @ 1999-11-19 21:19:37 by jwe]
jwe
parents: 3180
diff changeset
30 ## @end example
8442
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8431
diff changeset
31 ## @seealso{regexprep, strfind, findstr}
3361
4f40efa995c1 [project @ 1999-11-19 21:19:37 by jwe]
jwe
parents: 3180
diff changeset
32 ## @end deftypefn
2311
2b5788792cad [project @ 1996-07-11 20:18:38 by jwe]
jwe
parents: 2303
diff changeset
33
5428
2a16423e4aa0 [project @ 2005-08-23 18:38:27 by jwe]
jwe
parents: 5307
diff changeset
34 ## Author: Kurt Hornik <Kurt.Hornik@wu-wien.ac.at>
2312
204cc7db6f4a [project @ 1996-07-11 21:20:36 by jwe]
jwe
parents: 2311
diff changeset
35 ## Created: 11 November 1994
204cc7db6f4a [project @ 1996-07-11 21:20:36 by jwe]
jwe
parents: 2311
diff changeset
36 ## Adapted-By: jwe
204cc7db6f4a [project @ 1996-07-11 21:20:36 by jwe]
jwe
parents: 2311
diff changeset
37
2288
039cda4dc95a [project @ 1996-06-05 23:21:52 by jwe]
jwe
parents:
diff changeset
38 function t = strrep (s, x, y)
2325
b5568c31ee2c [project @ 1996-07-15 22:20:21 by jwe]
jwe
parents: 2313
diff changeset
39
3666
301e0da5b455 [project @ 2000-05-13 06:02:36 by jwe]
jwe
parents: 3426
diff changeset
40 if (nargin != 3)
6046
34f96dd5441b [project @ 2006-10-10 16:10:25 by jwe]
jwe
parents: 5443
diff changeset
41 print_usage ();
2288
039cda4dc95a [project @ 1996-06-05 23:21:52 by jwe]
jwe
parents:
diff changeset
42 endif
2325
b5568c31ee2c [project @ 1996-07-15 22:20:21 by jwe]
jwe
parents: 2313
diff changeset
43
5443
ec8c33dcd1bf [project @ 2005-09-08 01:40:57 by jwe]
jwe
parents: 5428
diff changeset
44 if (! (ischar (s) && ischar (x) && ischar (y)))
2288
039cda4dc95a [project @ 1996-06-05 23:21:52 by jwe]
jwe
parents:
diff changeset
45 error ("strrep: all arguments must be strings");
039cda4dc95a [project @ 1996-06-05 23:21:52 by jwe]
jwe
parents:
diff changeset
46 endif
2325
b5568c31ee2c [project @ 1996-07-15 22:20:21 by jwe]
jwe
parents: 2313
diff changeset
47
2288
039cda4dc95a [project @ 1996-06-05 23:21:52 by jwe]
jwe
parents:
diff changeset
48 if (length (x) > length (s) || isempty (x))
039cda4dc95a [project @ 1996-06-05 23:21:52 by jwe]
jwe
parents:
diff changeset
49 t = s;
039cda4dc95a [project @ 1996-06-05 23:21:52 by jwe]
jwe
parents:
diff changeset
50 return;
039cda4dc95a [project @ 1996-06-05 23:21:52 by jwe]
jwe
parents:
diff changeset
51 endif
2325
b5568c31ee2c [project @ 1996-07-15 22:20:21 by jwe]
jwe
parents: 2313
diff changeset
52
2288
039cda4dc95a [project @ 1996-06-05 23:21:52 by jwe]
jwe
parents:
diff changeset
53 ind = findstr (s, x, 0);
3666
301e0da5b455 [project @ 2000-05-13 06:02:36 by jwe]
jwe
parents: 3426
diff changeset
54
301e0da5b455 [project @ 2000-05-13 06:02:36 by jwe]
jwe
parents: 3426
diff changeset
55 if (length(ind) == 0)
2288
039cda4dc95a [project @ 1996-06-05 23:21:52 by jwe]
jwe
parents:
diff changeset
56 t = s;
8506
bc982528de11 comment style fixes
John W. Eaton <jwe@octave.org>
parents: 8442
diff changeset
57 elseif (length(y) > 0)
bc982528de11 comment style fixes
John W. Eaton <jwe@octave.org>
parents: 8442
diff changeset
58 ## Replacement.
bc982528de11 comment style fixes
John W. Eaton <jwe@octave.org>
parents: 8442
diff changeset
59 ##
3666
301e0da5b455 [project @ 2000-05-13 06:02:36 by jwe]
jwe
parents: 3426
diff changeset
60 ## Copy the parts of s that aren't being replaced. This is done
301e0da5b455 [project @ 2000-05-13 06:02:36 by jwe]
jwe
parents: 3426
diff changeset
61 ## with an index vector, with jumps where each search string
301e0da5b455 [project @ 2000-05-13 06:02:36 by jwe]
jwe
parents: 3426
diff changeset
62 ## is found. For a jump of 0 (target length == replacement length)
8507
cadc73247d65 style fixes
John W. Eaton <jwe@octave.org>
parents: 8506
diff changeset
63 ## the index is just cumsum (ones (length (s))). For non-zero
3666
301e0da5b455 [project @ 2000-05-13 06:02:36 by jwe]
jwe
parents: 3426
diff changeset
64 ## jumps, add the jump size to the ones vector at each found position.
301e0da5b455 [project @ 2000-05-13 06:02:36 by jwe]
jwe
parents: 3426
diff changeset
65 jump = length(y) - length(x);
8506
bc982528de11 comment style fixes
John W. Eaton <jwe@octave.org>
parents: 8442
diff changeset
66 if (jump > 0)
bc982528de11 comment style fixes
John W. Eaton <jwe@octave.org>
parents: 8442
diff changeset
67 ## S expands.
8507
cadc73247d65 style fixes
John W. Eaton <jwe@octave.org>
parents: 8506
diff changeset
68 di = ones (size (s));
3674
ef883684e58e [project @ 2000-06-07 17:11:33 by jwe]
jwe
parents: 3670
diff changeset
69 di(ind) = 1 + jump * ones (length (ind), 1);
ef883684e58e [project @ 2000-06-07 17:11:33 by jwe]
jwe
parents: 3670
diff changeset
70 t(cumsum (di)) = s;
8507
cadc73247d65 style fixes
John W. Eaton <jwe@octave.org>
parents: 8506
diff changeset
71 elseif (jump < 0)
cadc73247d65 style fixes
John W. Eaton <jwe@octave.org>
parents: 8506
diff changeset
72 ## S contracts.
3666
301e0da5b455 [project @ 2000-05-13 06:02:36 by jwe]
jwe
parents: 3426
diff changeset
73 di = ones (jump * length (ind) + length (s), 1);
8507
cadc73247d65 style fixes
John W. Eaton <jwe@octave.org>
parents: 8506
diff changeset
74 di (ind + jump * [0:length(ind)-1]) = 1 - jump * ones (length (ind), 1);
3666
301e0da5b455 [project @ 2000-05-13 06:02:36 by jwe]
jwe
parents: 3426
diff changeset
75 t = s (cumsum (di));
8506
bc982528de11 comment style fixes
John W. Eaton <jwe@octave.org>
parents: 8442
diff changeset
76 else
bc982528de11 comment style fixes
John W. Eaton <jwe@octave.org>
parents: 8442
diff changeset
77 ## S stays the same length.
3666
301e0da5b455 [project @ 2000-05-13 06:02:36 by jwe]
jwe
parents: 3426
diff changeset
78 t = s;
301e0da5b455 [project @ 2000-05-13 06:02:36 by jwe]
jwe
parents: 3426
diff changeset
79 endif
301e0da5b455 [project @ 2000-05-13 06:02:36 by jwe]
jwe
parents: 3426
diff changeset
80 ## Now, substitute a copy of the replacement string whereever the
301e0da5b455 [project @ 2000-05-13 06:02:36 by jwe]
jwe
parents: 3426
diff changeset
81 ## search string was found. Note that we must first update the
301e0da5b455 [project @ 2000-05-13 06:02:36 by jwe]
jwe
parents: 3426
diff changeset
82 ## target positions to account for any expansion or contraction
301e0da5b455 [project @ 2000-05-13 06:02:36 by jwe]
jwe
parents: 3426
diff changeset
83 ## of s that may have occurred.
3667
f7f94396dd81 [project @ 2000-05-13 06:31:28 by jwe]
jwe
parents: 3666
diff changeset
84 ind = ind + jump * [0:length(ind)-1];
3670
0287dfeb1f34 [project @ 2000-05-24 18:41:37 by jwe]
jwe
parents: 3667
diff changeset
85 repeat = [1:length(y)]' * ones (1, length (ind));
3666
301e0da5b455 [project @ 2000-05-13 06:02:36 by jwe]
jwe
parents: 3426
diff changeset
86 dest = ones (length (y), 1) * ind + repeat - 1;
3674
ef883684e58e [project @ 2000-06-07 17:11:33 by jwe]
jwe
parents: 3670
diff changeset
87 t(dest) = y(repeat);
8506
bc982528de11 comment style fixes
John W. Eaton <jwe@octave.org>
parents: 8442
diff changeset
88 else
bc982528de11 comment style fixes
John W. Eaton <jwe@octave.org>
parents: 8442
diff changeset
89 ## Deletion.
bc982528de11 comment style fixes
John W. Eaton <jwe@octave.org>
parents: 8442
diff changeset
90 ##
3666
301e0da5b455 [project @ 2000-05-13 06:02:36 by jwe]
jwe
parents: 3426
diff changeset
91 ## Build an index vector of all locations where the target was found
301e0da5b455 [project @ 2000-05-13 06:02:36 by jwe]
jwe
parents: 3426
diff changeset
92 ## in the search string, and zap them.
3674
ef883684e58e [project @ 2000-06-07 17:11:33 by jwe]
jwe
parents: 3670
diff changeset
93 t = toascii (s);
3666
301e0da5b455 [project @ 2000-05-13 06:02:36 by jwe]
jwe
parents: 3426
diff changeset
94 repeat = [1:length(x)]' * ones (1, length (ind));
301e0da5b455 [project @ 2000-05-13 06:02:36 by jwe]
jwe
parents: 3426
diff changeset
95 delete = ones (length (x), 1) * ind + repeat - 1;
3674
ef883684e58e [project @ 2000-06-07 17:11:33 by jwe]
jwe
parents: 3670
diff changeset
96 t(delete) = [];
5443
ec8c33dcd1bf [project @ 2005-09-08 01:40:57 by jwe]
jwe
parents: 5428
diff changeset
97 t = char (t);
2288
039cda4dc95a [project @ 1996-06-05 23:21:52 by jwe]
jwe
parents:
diff changeset
98 endif
2325
b5568c31ee2c [project @ 1996-07-15 22:20:21 by jwe]
jwe
parents: 2313
diff changeset
99
2288
039cda4dc95a [project @ 1996-06-05 23:21:52 by jwe]
jwe
parents:
diff changeset
100 endfunction
7411
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7017
diff changeset
101
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7017
diff changeset
102 %!assert(strcmp (strrep ("This is a test string", "is", "&%$"),
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7017
diff changeset
103 %! "Th&%$ &%$ a test string"));
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7017
diff changeset
104
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7017
diff changeset
105 %!error strrep ();
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7017
diff changeset
106
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7017
diff changeset
107 %!error strrep ("foo", "bar", 3, 4);