3789
|
1 *** strrep.m-2.1.28 Tue May 2 01:39:24 2000 |
|
2 --- strrep.m Fri May 12 08:21:15 2000 |
|
3 *************** |
|
4 *** 45,70 **** |
|
5 t = s; |
|
6 return; |
|
7 endif |
|
8 ind = findstr (s, x, 0); |
|
9 ! len = length (ind); |
|
10 ! if (len == 0) |
|
11 t = s; |
|
12 ! else |
|
13 ! save_empty_list_elements_ok = empty_list_elements_ok; |
|
14 ! unwind_protect |
|
15 ! empty_list_elements_ok = 1; |
|
16 ! l_x = length (x); |
|
17 ! tmp = s (1 : ind (1) - 1); |
|
18 ! t = strcat (tmp, y); |
|
19 ! for k = 1 : len - 1 |
|
20 ! tmp = s (ind (k) + l_x : ind (k+1) - 1); |
|
21 ! t = strcat (t, tmp, y); |
|
22 ! endfor |
|
23 ! tmp = s (ind(len) + l_x : length (s)); |
|
24 ! t = [t, tmp]; |
|
25 ! unwind_protect_cleanup |
|
26 ! empty_list_elements_ok = save_empty_list_elements_ok; |
|
27 ! end_unwind_protect |
|
28 endif |
|
29 |
|
30 endfunction |
|
31 --- 45,94 ---- |
|
32 t = s; |
|
33 return; |
|
34 endif |
|
35 + |
|
36 ind = findstr (s, x, 0); |
|
37 ! if (length(ind) == 0) |
|
38 t = s; |
|
39 ! |
|
40 ! elseif (length(y) > 0) # replacement |
|
41 ! ## Copy the parts of s that aren't being replaced. This is done |
|
42 ! ## with an index vector, with jumps where each search string |
|
43 ! ## is found. For a jump of 0 (target length == replacement length) |
|
44 ! ## the index is just cumsum ( ones (length (s))). For non-zero |
|
45 ! ## jumps, add the jump size to the ones vector at each found position. |
|
46 ! jump = length(y) - length(x); |
|
47 ! if (jump > 0) # s expands |
|
48 ! di = ones(size(s)); |
|
49 ! di (ind) = 1 + jump * ones (length (ind), 1); |
|
50 ! t (cumsum (di)) = s; |
|
51 ! if (size(s,1) == 1) |
|
52 ! t = t'; |
|
53 ! endif |
|
54 ! elseif (jump < 0) # s contracts |
|
55 ! di = ones (jump * length (ind) + length (s), 1); |
|
56 ! di (ind + jump * [0:length(ind)-1]) = 1 - jump * ones(length(ind), 1); |
|
57 ! t = s (cumsum (di)); |
|
58 ! else # s stays the same length |
|
59 ! t = s; |
|
60 ! endif |
|
61 ! |
|
62 ! ## Now, substitute a copy of the replacement string whereever the |
|
63 ! ## search string was found. Note that we must first update the |
|
64 ! ## target positions to account for any expansion or contraction |
|
65 ! ## of s that may have occurred. |
|
66 ! ind = ind + jump * [ 0 : length (ind) - 1 ]; |
|
67 ! repeat = [1 : length (y)]' * ones (1, length (ind)); |
|
68 ! dest = ones (length (y), 1) * ind + repeat - 1; |
|
69 ! t (dest) = y (repeat); |
|
70 ! |
|
71 ! else # deletion |
|
72 ! ## Build an index vector of all locations where the target was found |
|
73 ! ## in the search string, and zap them. |
|
74 ! t = toascii(s); |
|
75 ! repeat = [1 : length (x)]' * ones (1, length (ind)); |
|
76 ! delete = ones (length (x), 1) * ind + repeat - 1; |
|
77 ! t (delete) = []; |
|
78 ! t = setstr(t); |
|
79 endif |
|
80 |
|
81 endfunction |