Mercurial > octave
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 |
rev | line source |
---|---|
7017 | 1 ## Copyright (C) 1995, 1996, 1998, 1999, 2000, 2005, 2006, 2007 |
2 ## Kurt Hornik | |
2325 | 3 ## |
2313 | 4 ## This file is part of Octave. |
5 ## | |
6 ## Octave is free software; you can redistribute it and/or modify it | |
7 ## under the terms of the GNU General Public License as published by | |
7016 | 8 ## the Free Software Foundation; either version 3 of the License, or (at |
9 ## your option) any later version. | |
2313 | 10 ## |
11 ## Octave is distributed in the hope that it will be useful, but | |
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 ## General Public License for more details. | |
15 ## | |
16 ## You should have received a copy of the GNU General Public License | |
7016 | 17 ## along with Octave; see the file COPYING. If not, see |
18 ## <http://www.gnu.org/licenses/>. | |
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} | |
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 | 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) |
6046 | 38 print_usage (); |
2288 | 39 endif |
2325 | 40 |
5443 | 41 if (! (ischar (s) && ischar (x) && ischar (y))) |
2288 | 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) = []; |
5443 | 87 t = char (t); |
2288 | 88 endif |
2325 | 89 |
2288 | 90 endfunction |