Mercurial > hg > octave-nkf
annotate scripts/strings/strrep.m @ 8507:cadc73247d65
style fixes
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Tue, 13 Jan 2009 14:08:36 -0500 |
parents | bc982528de11 |
children | bf6befcfa70a |
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} | |
8431
99b6066fccb6
strrep.m: specify what is the return value
Francesco Potortì <pot@gnu.org>
parents:
7411
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 | |
8442
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8431
diff
changeset
|
29 ## @seealso{regexprep, strfind, findstr} |
3361 | 30 ## @end deftypefn |
2311 | 31 |
5428 | 32 ## Author: Kurt Hornik <Kurt.Hornik@wu-wien.ac.at> |
2312 | 33 ## Created: 11 November 1994 |
34 ## Adapted-By: jwe | |
35 | |
2288 | 36 function t = strrep (s, x, y) |
2325 | 37 |
3666 | 38 if (nargin != 3) |
6046 | 39 print_usage (); |
2288 | 40 endif |
2325 | 41 |
5443 | 42 if (! (ischar (s) && ischar (x) && ischar (y))) |
2288 | 43 error ("strrep: all arguments must be strings"); |
44 endif | |
2325 | 45 |
2288 | 46 if (length (x) > length (s) || isempty (x)) |
47 t = s; | |
48 return; | |
49 endif | |
2325 | 50 |
2288 | 51 ind = findstr (s, x, 0); |
3666 | 52 |
53 if (length(ind) == 0) | |
2288 | 54 t = s; |
8506 | 55 elseif (length(y) > 0) |
56 ## Replacement. | |
57 ## | |
3666 | 58 ## Copy the parts of s that aren't being replaced. This is done |
59 ## with an index vector, with jumps where each search string | |
60 ## is found. For a jump of 0 (target length == replacement length) | |
8507 | 61 ## the index is just cumsum (ones (length (s))). For non-zero |
3666 | 62 ## jumps, add the jump size to the ones vector at each found position. |
63 jump = length(y) - length(x); | |
8506 | 64 if (jump > 0) |
65 ## S expands. | |
8507 | 66 di = ones (size (s)); |
3674 | 67 di(ind) = 1 + jump * ones (length (ind), 1); |
68 t(cumsum (di)) = s; | |
8507 | 69 elseif (jump < 0) |
70 ## S contracts. | |
3666 | 71 di = ones (jump * length (ind) + length (s), 1); |
8507 | 72 di (ind + jump * [0:length(ind)-1]) = 1 - jump * ones (length (ind), 1); |
3666 | 73 t = s (cumsum (di)); |
8506 | 74 else |
75 ## S stays the same length. | |
3666 | 76 t = s; |
77 endif | |
78 ## Now, substitute a copy of the replacement string whereever the | |
79 ## search string was found. Note that we must first update the | |
80 ## target positions to account for any expansion or contraction | |
81 ## of s that may have occurred. | |
3667 | 82 ind = ind + jump * [0:length(ind)-1]; |
3670 | 83 repeat = [1:length(y)]' * ones (1, length (ind)); |
3666 | 84 dest = ones (length (y), 1) * ind + repeat - 1; |
3674 | 85 t(dest) = y(repeat); |
8506 | 86 else |
87 ## Deletion. | |
88 ## | |
3666 | 89 ## Build an index vector of all locations where the target was found |
90 ## in the search string, and zap them. | |
3674 | 91 t = toascii (s); |
3666 | 92 repeat = [1:length(x)]' * ones (1, length (ind)); |
93 delete = ones (length (x), 1) * ind + repeat - 1; | |
3674 | 94 t(delete) = []; |
5443 | 95 t = char (t); |
2288 | 96 endif |
2325 | 97 |
2288 | 98 endfunction |
7411 | 99 |
100 %!assert(strcmp (strrep ("This is a test string", "is", "&%$"), | |
101 %! "Th&%$ &%$ a test string")); | |
102 | |
103 %!error strrep (); | |
104 | |
105 %!error strrep ("foo", "bar", 3, 4); |