2313
|
1 ## Copyright (C) 1995, 1996 Kurt Hornik |
2325
|
2 ## |
2313
|
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 2, or (at your option) |
|
8 ## 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, write to the Free |
5307
|
17 ## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
18 ## 02110-1301, USA. |
2288
|
19 |
3361
|
20 ## -*- texinfo -*- |
|
21 ## @deftypefn {Function File} {} strrep (@var{s}, @var{x}, @var{y}) |
|
22 ## Replaces all occurrences of the substring @var{x} of the string @var{s} |
|
23 ## with the string @var{y}. For example, |
3426
|
24 ## |
3361
|
25 ## @example |
|
26 ## strrep ("This is a test string", "is", "&%$") |
|
27 ## @result{} "Th&%$ &%$ a test string" |
|
28 ## @end example |
|
29 ## @end deftypefn |
2311
|
30 |
5428
|
31 ## Author: Kurt Hornik <Kurt.Hornik@wu-wien.ac.at> |
2312
|
32 ## Created: 11 November 1994 |
|
33 ## Adapted-By: jwe |
|
34 |
2288
|
35 function t = strrep (s, x, y) |
2325
|
36 |
3666
|
37 if (nargin != 3) |
2288
|
38 usage ("strrep (s, x, y)"); |
|
39 endif |
2325
|
40 |
2288
|
41 if (! (isstr (s) && isstr (x) && isstr (y))) |
|
42 error ("strrep: all arguments must be strings"); |
|
43 endif |
2325
|
44 |
2288
|
45 if (length (x) > length (s) || isempty (x)) |
|
46 t = s; |
|
47 return; |
|
48 endif |
2325
|
49 |
2288
|
50 ind = findstr (s, x, 0); |
3666
|
51 |
|
52 if (length(ind) == 0) |
2288
|
53 t = s; |
3666
|
54 elseif (length(y) > 0) # replacement |
|
55 ## Copy the parts of s that aren't being replaced. This is done |
|
56 ## with an index vector, with jumps where each search string |
|
57 ## is found. For a jump of 0 (target length == replacement length) |
|
58 ## the index is just cumsum ( ones (length (s))). For non-zero |
|
59 ## jumps, add the jump size to the ones vector at each found position. |
|
60 jump = length(y) - length(x); |
|
61 if (jump > 0) # s expands |
|
62 di = ones(size(s)); |
3674
|
63 di(ind) = 1 + jump * ones (length (ind), 1); |
|
64 t(cumsum (di)) = s; |
3666
|
65 elseif (jump < 0) # s contracts |
|
66 di = ones (jump * length (ind) + length (s), 1); |
|
67 di (ind + jump * [0:length(ind)-1]) = 1 - jump * ones(length(ind), 1); |
|
68 t = s (cumsum (di)); |
|
69 else # s stays the same length |
|
70 t = s; |
|
71 endif |
|
72 ## Now, substitute a copy of the replacement string whereever the |
|
73 ## search string was found. Note that we must first update the |
|
74 ## target positions to account for any expansion or contraction |
|
75 ## of s that may have occurred. |
3667
|
76 ind = ind + jump * [0:length(ind)-1]; |
3670
|
77 repeat = [1:length(y)]' * ones (1, length (ind)); |
3666
|
78 dest = ones (length (y), 1) * ind + repeat - 1; |
3674
|
79 t(dest) = y(repeat); |
3666
|
80 else # deletion |
|
81 ## Build an index vector of all locations where the target was found |
|
82 ## in the search string, and zap them. |
3674
|
83 t = toascii (s); |
3666
|
84 repeat = [1:length(x)]' * ones (1, length (ind)); |
|
85 delete = ones (length (x), 1) * ind + repeat - 1; |
3674
|
86 t(delete) = []; |
|
87 t = setstr (t); |
2288
|
88 endif |
2325
|
89 |
2288
|
90 endfunction |