2847
|
1 ## Copyright (C) 1996, 1997 John W. Eaton |
2313
|
2 ## |
|
3 ## This file is part of Octave. |
|
4 ## |
|
5 ## Octave is free software; you can redistribute it and/or modify it |
|
6 ## under the terms of the GNU General Public License as published by |
|
7 ## the Free Software Foundation; either version 2, or (at your option) |
|
8 ## any later version. |
|
9 ## |
|
10 ## Octave is distributed in the hope that it will be useful, but |
|
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
13 ## General Public License for more details. |
|
14 ## |
|
15 ## You should have received a copy of the GNU General Public License |
|
16 ## along with Octave; see the file COPYING. If not, write to the Free |
|
17 ## Software Foundation, 59 Temple Place - Suite 330, Boston, MA |
|
18 ## 02111-1307, USA. |
245
|
19 |
3361
|
20 ## -*- texinfo -*- |
|
21 ## @deftypefn {Function File} {} strcmp (@var{s1}, @var{s2}) |
|
22 ## Compares two strings, returning 1 if they are the same, and 0 otherwise. |
3426
|
23 ## |
3361
|
24 ## @strong{Note:} For compatibility with @sc{Matlab}, Octave's strcmp |
|
25 ## function returns 1 if the strings are equal, and 0 otherwise. This is |
|
26 ## just the opposite of the corresponding C library function. |
|
27 ## @end deftypefn |
4
|
28 |
2314
|
29 ## Author: jwe |
|
30 |
4257
|
31 function retval = strcmp (s1, s2) |
4
|
32 |
|
33 if (nargin != 2) |
904
|
34 usage ("strcmp (s, t)"); |
4
|
35 endif |
|
36 |
4257
|
37 retval = 0; |
|
38 |
|
39 if (isstr (s1)) |
|
40 [r1, c1] = size (s1); |
|
41 if (isstr (s2)) |
|
42 [r2, c2] = size (s2); |
|
43 if (r1 == r2 && c1 == c2) |
|
44 if (c1 == 0) |
|
45 retval = 1; |
|
46 else |
|
47 retval = all (all (s1 == s2)); |
|
48 endif |
|
49 endif |
|
50 elseif (iscell (s2)) |
|
51 [r2, c2] = size (s2); |
|
52 if (r1 == 1) |
|
53 t2 = s2(:); |
|
54 n = length (t2); |
|
55 retval = zeros (n, 1); |
|
56 for i = 1:n |
|
57 retval(i) = strcmp (s1, t2{i}); |
|
58 endfor |
|
59 retval = reshape (retval, r2, c2); |
|
60 elseif (r1 > 1) |
|
61 if (r2 == 1 && c2 == 1) |
|
62 t2 = s2{1}; |
|
63 retval = zeros (r1, 1); |
|
64 for i = 1:r1 |
|
65 retval(i) = strcmp (deblank (s1(i,:)), t2); |
|
66 endfor |
|
67 else |
|
68 t2 = s2(:); |
|
69 n = length (t2); |
|
70 if (n == r1) |
|
71 retval = zeros (n, 1); |
|
72 for i = 1:n |
|
73 retval(i) = strcmp (deblank (s1(i,:)), t2{i}); |
|
74 endfor |
|
75 retval = reshape (retval, r2, c2); |
|
76 endif |
|
77 endif |
|
78 endif |
|
79 endif |
|
80 elseif (iscell (s1)) |
3695
|
81 [r1, c1] = size (s1); |
4257
|
82 if (isstr (s2)) |
|
83 [r2, c2] = size (s2); |
|
84 if (r2 == 1) |
|
85 t1 = s1(:); |
|
86 n = length (t1); |
|
87 retval = zeros (n, 1); |
|
88 for i = 1:n |
|
89 retval(i) = strcmp (t1{i}, s2); |
|
90 endfor |
|
91 retval = reshape (retval, r1, c1); |
|
92 elseif (r2 > 1) |
|
93 if (r1 == 1 && c1 == 1) |
|
94 t1 = s1{1}; |
|
95 retval = zeros (r2, 1); |
|
96 for i = 1:r2 |
|
97 retval(i) = strcmp (t1, deblank (s2(i,:))); |
|
98 endfor |
|
99 else |
|
100 t1 = s1(:); |
|
101 n = length (t1); |
|
102 if (n == r2) |
|
103 retval = zeros (n, 1); |
|
104 for i = 1:n |
|
105 retval(i) = strcmp (t2{i}, deblank (s2(i,:))); |
|
106 endfor |
|
107 retval = reshape (retval, r1, c1); |
|
108 endif |
|
109 endif |
|
110 endif |
|
111 elseif (iscell (s2)) |
|
112 [r2, c2] = size (s2); |
|
113 if (r1 == 1 && c1 == 1) |
|
114 t1 = s1{:}; |
|
115 t2 = s2(:); |
|
116 n = length (t2); |
|
117 retval = zeros (n, 1); |
|
118 for i = 1:n |
|
119 retval(i) = strcmp (t1, t2{i}); |
|
120 endfor |
|
121 retval = reshape (retval, r2, c2); |
|
122 elseif (r2 == 1 && c2 == 1) |
|
123 t1 = s1(:); |
|
124 t2 = s2{:}; |
|
125 n = length (t1); |
|
126 retval = zeros (n, 1); |
|
127 for i = 1:n |
|
128 retval(i) = strcmp (t1{i}, t2); |
|
129 endfor |
|
130 retval = reshape (retval, r1, c1); |
|
131 elseif (r1 == r2 && c1 == c2) |
|
132 t1 = s1(:); |
|
133 t2 = s2(:); |
|
134 n = length (t1); |
|
135 for i = 1:n |
|
136 retval(i) = strcmp (t1{i}, t2{i}); |
|
137 endfor |
|
138 retval = reshape (retval, r1, c1); |
321
|
139 else |
4257
|
140 error ("strcmp: nonconformant cell arrays"); |
321
|
141 endif |
|
142 endif |
4
|
143 endif |
|
144 |
|
145 endfunction |