annotate scripts/strings/strrep.m @ 11910:9d46d257f6ef release-3-0-x

strrep.m: specify what is the return value
author Francesco Potortì <pot@gnu.org>
date Mon, 12 Jan 2009 10:56:19 +0100
parents a1dbe9d80eee
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7017
a1dbe9d80eee [project @ 2007-10-12 21:27:11 by jwe]
jwe
parents: 7016
diff changeset
1 ## Copyright (C) 1995, 1996, 1998, 1999, 2000, 2005, 2006, 2007
a1dbe9d80eee [project @ 2007-10-12 21:27:11 by jwe]
jwe
parents: 7016
diff changeset
2 ## 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})
4f40efa995c1 [project @ 1999-11-19 21:19:37 by jwe]
jwe
parents: 3180
diff changeset
22 ## Replaces all occurrences of the substring @var{x} of the string @var{s}
11910
9d46d257f6ef strrep.m: specify what is the return value
Francesco Potortì <pot@gnu.org>
parents: 7017
diff changeset
23 ## with the string @var{y} and returns 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
4f40efa995c1 [project @ 1999-11-19 21:19:37 by jwe]
jwe
parents: 3180
diff changeset
26 ## strrep ("This is a test string", "is", "&%$")
4f40efa995c1 [project @ 1999-11-19 21:19:37 by jwe]
jwe
parents: 3180
diff changeset
27 ## @result{} "Th&%$ &%$ a test string"
4f40efa995c1 [project @ 1999-11-19 21:19:37 by jwe]
jwe
parents: 3180
diff changeset
28 ## @end example
4f40efa995c1 [project @ 1999-11-19 21:19:37 by jwe]
jwe
parents: 3180
diff changeset
29 ## @end deftypefn
2311
2b5788792cad [project @ 1996-07-11 20:18:38 by jwe]
jwe
parents: 2303
diff changeset
30
5428
2a16423e4aa0 [project @ 2005-08-23 18:38:27 by jwe]
jwe
parents: 5307
diff changeset
31 ## 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
32 ## Created: 11 November 1994
204cc7db6f4a [project @ 1996-07-11 21:20:36 by jwe]
jwe
parents: 2311
diff changeset
33 ## Adapted-By: jwe
204cc7db6f4a [project @ 1996-07-11 21:20:36 by jwe]
jwe
parents: 2311
diff changeset
34
2288
039cda4dc95a [project @ 1996-06-05 23:21:52 by jwe]
jwe
parents:
diff changeset
35 function t = strrep (s, x, y)
2325
b5568c31ee2c [project @ 1996-07-15 22:20:21 by jwe]
jwe
parents: 2313
diff changeset
36
3666
301e0da5b455 [project @ 2000-05-13 06:02:36 by jwe]
jwe
parents: 3426
diff changeset
37 if (nargin != 3)
6046
34f96dd5441b [project @ 2006-10-10 16:10:25 by jwe]
jwe
parents: 5443
diff changeset
38 print_usage ();
2288
039cda4dc95a [project @ 1996-06-05 23:21:52 by jwe]
jwe
parents:
diff changeset
39 endif
2325
b5568c31ee2c [project @ 1996-07-15 22:20:21 by jwe]
jwe
parents: 2313
diff changeset
40
5443
ec8c33dcd1bf [project @ 2005-09-08 01:40:57 by jwe]
jwe
parents: 5428
diff changeset
41 if (! (ischar (s) && ischar (x) && ischar (y)))
2288
039cda4dc95a [project @ 1996-06-05 23:21:52 by jwe]
jwe
parents:
diff changeset
42 error ("strrep: all arguments must be strings");
039cda4dc95a [project @ 1996-06-05 23:21:52 by jwe]
jwe
parents:
diff changeset
43 endif
2325
b5568c31ee2c [project @ 1996-07-15 22:20:21 by jwe]
jwe
parents: 2313
diff changeset
44
2288
039cda4dc95a [project @ 1996-06-05 23:21:52 by jwe]
jwe
parents:
diff changeset
45 if (length (x) > length (s) || isempty (x))
039cda4dc95a [project @ 1996-06-05 23:21:52 by jwe]
jwe
parents:
diff changeset
46 t = s;
039cda4dc95a [project @ 1996-06-05 23:21:52 by jwe]
jwe
parents:
diff changeset
47 return;
039cda4dc95a [project @ 1996-06-05 23:21:52 by jwe]
jwe
parents:
diff changeset
48 endif
2325
b5568c31ee2c [project @ 1996-07-15 22:20:21 by jwe]
jwe
parents: 2313
diff changeset
49
2288
039cda4dc95a [project @ 1996-06-05 23:21:52 by jwe]
jwe
parents:
diff changeset
50 ind = findstr (s, x, 0);
3666
301e0da5b455 [project @ 2000-05-13 06:02:36 by jwe]
jwe
parents: 3426
diff changeset
51
301e0da5b455 [project @ 2000-05-13 06:02:36 by jwe]
jwe
parents: 3426
diff changeset
52 if (length(ind) == 0)
2288
039cda4dc95a [project @ 1996-06-05 23:21:52 by jwe]
jwe
parents:
diff changeset
53 t = s;
3666
301e0da5b455 [project @ 2000-05-13 06:02:36 by jwe]
jwe
parents: 3426
diff changeset
54 elseif (length(y) > 0) # replacement
301e0da5b455 [project @ 2000-05-13 06:02:36 by jwe]
jwe
parents: 3426
diff changeset
55 ## 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
56 ## 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
57 ## is found. For a jump of 0 (target length == replacement length)
301e0da5b455 [project @ 2000-05-13 06:02:36 by jwe]
jwe
parents: 3426
diff changeset
58 ## the index is just cumsum ( ones (length (s))). For non-zero
301e0da5b455 [project @ 2000-05-13 06:02:36 by jwe]
jwe
parents: 3426
diff changeset
59 ## 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
60 jump = length(y) - length(x);
301e0da5b455 [project @ 2000-05-13 06:02:36 by jwe]
jwe
parents: 3426
diff changeset
61 if (jump > 0) # s expands
301e0da5b455 [project @ 2000-05-13 06:02:36 by jwe]
jwe
parents: 3426
diff changeset
62 di = ones(size(s));
3674
ef883684e58e [project @ 2000-06-07 17:11:33 by jwe]
jwe
parents: 3670
diff changeset
63 di(ind) = 1 + jump * ones (length (ind), 1);
ef883684e58e [project @ 2000-06-07 17:11:33 by jwe]
jwe
parents: 3670
diff changeset
64 t(cumsum (di)) = s;
3666
301e0da5b455 [project @ 2000-05-13 06:02:36 by jwe]
jwe
parents: 3426
diff changeset
65 elseif (jump < 0) # s contracts
301e0da5b455 [project @ 2000-05-13 06:02:36 by jwe]
jwe
parents: 3426
diff changeset
66 di = ones (jump * length (ind) + length (s), 1);
301e0da5b455 [project @ 2000-05-13 06:02:36 by jwe]
jwe
parents: 3426
diff changeset
67 di (ind + jump * [0:length(ind)-1]) = 1 - jump * ones(length(ind), 1);
301e0da5b455 [project @ 2000-05-13 06:02:36 by jwe]
jwe
parents: 3426
diff changeset
68 t = s (cumsum (di));
301e0da5b455 [project @ 2000-05-13 06:02:36 by jwe]
jwe
parents: 3426
diff changeset
69 else # s stays the same length
301e0da5b455 [project @ 2000-05-13 06:02:36 by jwe]
jwe
parents: 3426
diff changeset
70 t = s;
301e0da5b455 [project @ 2000-05-13 06:02:36 by jwe]
jwe
parents: 3426
diff changeset
71 endif
301e0da5b455 [project @ 2000-05-13 06:02:36 by jwe]
jwe
parents: 3426
diff changeset
72 ## 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
73 ## 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
74 ## target positions to account for any expansion or contraction
301e0da5b455 [project @ 2000-05-13 06:02:36 by jwe]
jwe
parents: 3426
diff changeset
75 ## of s that may have occurred.
3667
f7f94396dd81 [project @ 2000-05-13 06:31:28 by jwe]
jwe
parents: 3666
diff changeset
76 ind = ind + jump * [0:length(ind)-1];
3670
0287dfeb1f34 [project @ 2000-05-24 18:41:37 by jwe]
jwe
parents: 3667
diff changeset
77 repeat = [1:length(y)]' * ones (1, length (ind));
3666
301e0da5b455 [project @ 2000-05-13 06:02:36 by jwe]
jwe
parents: 3426
diff changeset
78 dest = ones (length (y), 1) * ind + repeat - 1;
3674
ef883684e58e [project @ 2000-06-07 17:11:33 by jwe]
jwe
parents: 3670
diff changeset
79 t(dest) = y(repeat);
3666
301e0da5b455 [project @ 2000-05-13 06:02:36 by jwe]
jwe
parents: 3426
diff changeset
80 else # deletion
301e0da5b455 [project @ 2000-05-13 06:02:36 by jwe]
jwe
parents: 3426
diff changeset
81 ## 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
82 ## in the search string, and zap them.
3674
ef883684e58e [project @ 2000-06-07 17:11:33 by jwe]
jwe
parents: 3670
diff changeset
83 t = toascii (s);
3666
301e0da5b455 [project @ 2000-05-13 06:02:36 by jwe]
jwe
parents: 3426
diff changeset
84 repeat = [1:length(x)]' * ones (1, length (ind));
301e0da5b455 [project @ 2000-05-13 06:02:36 by jwe]
jwe
parents: 3426
diff changeset
85 delete = ones (length (x), 1) * ind + repeat - 1;
3674
ef883684e58e [project @ 2000-06-07 17:11:33 by jwe]
jwe
parents: 3670
diff changeset
86 t(delete) = [];
5443
ec8c33dcd1bf [project @ 2005-09-08 01:40:57 by jwe]
jwe
parents: 5428
diff changeset
87 t = char (t);
2288
039cda4dc95a [project @ 1996-06-05 23:21:52 by jwe]
jwe
parents:
diff changeset
88 endif
2325
b5568c31ee2c [project @ 1996-07-15 22:20:21 by jwe]
jwe
parents: 2313
diff changeset
89
2288
039cda4dc95a [project @ 1996-06-05 23:21:52 by jwe]
jwe
parents:
diff changeset
90 endfunction