Mercurial > hg > octave-nkf
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 |
rev | line source |
---|---|
8920 | 1 ## Copyright (C) 1995, 1996, 1998, 1999, 2000, 2005, 2006, 2007, 2008, |
2 ## 2009 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}) | |
8525 | 22 ## Replace all occurrences of the substring @var{x} of the string @var{s} |
23 ## with the string @var{y} and return the result. For example, | |
3426 | 24 ## |
3361 | 25 ## @example |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
26 ## @group |
3361 | 27 ## strrep ("This is a test string", "is", "&%$") |
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 | 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 | 32 ## @end deftypefn |
2311 | 33 |
5428 | 34 ## Author: Kurt Hornik <Kurt.Hornik@wu-wien.ac.at> |
2312 | 35 ## Created: 11 November 1994 |
36 ## Adapted-By: jwe | |
37 | |
2288 | 38 function t = strrep (s, x, y) |
2325 | 39 |
3666 | 40 if (nargin != 3) |
6046 | 41 print_usage (); |
2288 | 42 endif |
2325 | 43 |
5443 | 44 if (! (ischar (s) && ischar (x) && ischar (y))) |
2288 | 45 error ("strrep: all arguments must be strings"); |
46 endif | |
2325 | 47 |
2288 | 48 if (length (x) > length (s) || isempty (x)) |
49 t = s; | |
50 return; | |
51 endif | |
2325 | 52 |
2288 | 53 ind = findstr (s, x, 0); |
3666 | 54 |
55 if (length(ind) == 0) | |
2288 | 56 t = s; |
8506 | 57 elseif (length(y) > 0) |
58 ## Replacement. | |
59 ## | |
3666 | 60 ## Copy the parts of s that aren't being replaced. This is done |
61 ## with an index vector, with jumps where each search string | |
62 ## is found. For a jump of 0 (target length == replacement length) | |
8507 | 63 ## the index is just cumsum (ones (length (s))). For non-zero |
3666 | 64 ## jumps, add the jump size to the ones vector at each found position. |
65 jump = length(y) - length(x); | |
8506 | 66 if (jump > 0) |
67 ## S expands. | |
8507 | 68 di = ones (size (s)); |
3674 | 69 di(ind) = 1 + jump * ones (length (ind), 1); |
70 t(cumsum (di)) = s; | |
8507 | 71 elseif (jump < 0) |
72 ## S contracts. | |
3666 | 73 di = ones (jump * length (ind) + length (s), 1); |
8507 | 74 di (ind + jump * [0:length(ind)-1]) = 1 - jump * ones (length (ind), 1); |
3666 | 75 t = s (cumsum (di)); |
8506 | 76 else |
77 ## S stays the same length. | |
3666 | 78 t = s; |
79 endif | |
80 ## Now, substitute a copy of the replacement string whereever the | |
81 ## search string was found. Note that we must first update the | |
82 ## target positions to account for any expansion or contraction | |
83 ## of s that may have occurred. | |
3667 | 84 ind = ind + jump * [0:length(ind)-1]; |
3670 | 85 repeat = [1:length(y)]' * ones (1, length (ind)); |
3666 | 86 dest = ones (length (y), 1) * ind + repeat - 1; |
3674 | 87 t(dest) = y(repeat); |
8506 | 88 else |
89 ## Deletion. | |
90 ## | |
3666 | 91 ## Build an index vector of all locations where the target was found |
92 ## in the search string, and zap them. | |
3674 | 93 t = toascii (s); |
3666 | 94 repeat = [1:length(x)]' * ones (1, length (ind)); |
95 delete = ones (length (x), 1) * ind + repeat - 1; | |
3674 | 96 t(delete) = []; |
5443 | 97 t = char (t); |
2288 | 98 endif |
2325 | 99 |
2288 | 100 endfunction |
7411 | 101 |
102 %!assert(strcmp (strrep ("This is a test string", "is", "&%$"), | |
103 %! "Th&%$ &%$ a test string")); | |
104 | |
105 %!error strrep (); | |
106 | |
107 %!error strrep ("foo", "bar", 3, 4); |