annotate scripts/strings/strrep.m @ 3426:f8dde1807dee

[project @ 2000-01-13 08:40:00 by jwe]
author jwe
date Thu, 13 Jan 2000 08:40:53 +0000
parents 4f40efa995c1
children 301e0da5b455
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2313
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
1 ## Copyright (C) 1995, 1996 Kurt Hornik
2325
b5568c31ee2c [project @ 1996-07-15 22:20:21 by jwe]
jwe
parents: 2313
diff changeset
2 ##
2313
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
3 ## This file is part of Octave.
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
4 ##
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
5 ## 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
6 ## under the terms of the GNU General Public License as published by
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
7 ## the Free Software Foundation; either version 2, or (at your option)
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
8 ## any later version.
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
9 ##
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
10 ## 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
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
12 ## 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
13 ## General Public License for more details.
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
14 ##
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
15 ## You should have received a copy of the GNU General Public License
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
16 ## along with Octave; see the file COPYING. If not, write to the Free
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
17 ## Software Foundation, 59 Temple Place - Suite 330, Boston, MA
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
18 ## 02111-1307, USA.
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}
4f40efa995c1 [project @ 1999-11-19 21:19:37 by jwe]
jwe
parents: 3180
diff changeset
23 ## with the string @var{y}. 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
2312
204cc7db6f4a [project @ 1996-07-11 21:20:36 by jwe]
jwe
parents: 2311
diff changeset
31 ## Author: Kurt Hornik <Kurt.Hornik@ci.tuwien.ac.at>
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
2288
039cda4dc95a [project @ 1996-06-05 23:21:52 by jwe]
jwe
parents:
diff changeset
37 if (nargin <> 3)
039cda4dc95a [project @ 1996-06-05 23:21:52 by jwe]
jwe
parents:
diff changeset
38 usage ("strrep (s, x, y)");
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
2288
039cda4dc95a [project @ 1996-06-05 23:21:52 by jwe]
jwe
parents:
diff changeset
41 if (! (isstr (s) && isstr (x) && isstr (y)))
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);
039cda4dc95a [project @ 1996-06-05 23:21:52 by jwe]
jwe
parents:
diff changeset
51 len = length (ind);
039cda4dc95a [project @ 1996-06-05 23:21:52 by jwe]
jwe
parents:
diff changeset
52 if (len == 0)
039cda4dc95a [project @ 1996-06-05 23:21:52 by jwe]
jwe
parents:
diff changeset
53 t = s;
039cda4dc95a [project @ 1996-06-05 23:21:52 by jwe]
jwe
parents:
diff changeset
54 else
3180
c17387059fd3 [project @ 1998-09-24 18:59:11 by jwe]
jwe
parents: 2325
diff changeset
55 save_empty_list_elements_ok = empty_list_elements_ok;
c17387059fd3 [project @ 1998-09-24 18:59:11 by jwe]
jwe
parents: 2325
diff changeset
56 unwind_protect
c17387059fd3 [project @ 1998-09-24 18:59:11 by jwe]
jwe
parents: 2325
diff changeset
57 empty_list_elements_ok = 1;
c17387059fd3 [project @ 1998-09-24 18:59:11 by jwe]
jwe
parents: 2325
diff changeset
58 l_x = length (x);
c17387059fd3 [project @ 1998-09-24 18:59:11 by jwe]
jwe
parents: 2325
diff changeset
59 tmp = s (1 : ind (1) - 1);
c17387059fd3 [project @ 1998-09-24 18:59:11 by jwe]
jwe
parents: 2325
diff changeset
60 t = strcat (tmp, y);
c17387059fd3 [project @ 1998-09-24 18:59:11 by jwe]
jwe
parents: 2325
diff changeset
61 for k = 1 : len - 1
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3361
diff changeset
62 tmp = s (ind (k) + l_x : ind (k+1) - 1);
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3361
diff changeset
63 t = strcat (t, tmp, y);
3180
c17387059fd3 [project @ 1998-09-24 18:59:11 by jwe]
jwe
parents: 2325
diff changeset
64 endfor
c17387059fd3 [project @ 1998-09-24 18:59:11 by jwe]
jwe
parents: 2325
diff changeset
65 tmp = s (ind(len) + l_x : length (s));
c17387059fd3 [project @ 1998-09-24 18:59:11 by jwe]
jwe
parents: 2325
diff changeset
66 t = [t, tmp];
c17387059fd3 [project @ 1998-09-24 18:59:11 by jwe]
jwe
parents: 2325
diff changeset
67 unwind_protect_cleanup
c17387059fd3 [project @ 1998-09-24 18:59:11 by jwe]
jwe
parents: 2325
diff changeset
68 empty_list_elements_ok = save_empty_list_elements_ok;
c17387059fd3 [project @ 1998-09-24 18:59:11 by jwe]
jwe
parents: 2325
diff changeset
69 end_unwind_protect
2288
039cda4dc95a [project @ 1996-06-05 23:21:52 by jwe]
jwe
parents:
diff changeset
70 endif
2325
b5568c31ee2c [project @ 1996-07-15 22:20:21 by jwe]
jwe
parents: 2313
diff changeset
71
2288
039cda4dc95a [project @ 1996-06-05 23:21:52 by jwe]
jwe
parents:
diff changeset
72 endfunction