5372
|
1 ## Copyright (C) 1996, 1997 John W. Eaton |
|
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, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
18 ## 02110-1301, USA. |
|
19 |
|
20 ## -*- texinfo -*- |
|
21 ## @deftypefn {Function File} {} strncmp (@var{s1}, @var{s2}, @var{n}) |
|
22 ## Compares the first @var{n} characters (columns) of two character |
|
23 ## strings, returning true if they are the same, and false otherwise. |
|
24 ## |
|
25 ## @strong{Caution:} For compatibility with @sc{Matlab}, Octave's strncmp |
|
26 ## function returns true if the character strings are equal, and false |
|
27 ## otherwise. This is just the opposite of the corresponding C library |
|
28 ## function. |
|
29 ## @end deftypefn |
|
30 |
|
31 ## Author: jwe |
|
32 ## Adapted from strcmp.m by Tom Holroyd <tomh@kurage.nimh.nih.gov> |
|
33 |
|
34 function retval = strncmp (s1, s2, n) |
|
35 |
|
36 if (nargin != 3) |
|
37 usage ("strncmp (s, t, n)"); |
|
38 endif |
|
39 |
|
40 retval = false; |
|
41 |
5443
|
42 if (ischar (s1)) |
5372
|
43 [r1, c1] = size (s1); |
5443
|
44 if (ischar (s2)) |
5372
|
45 [r2, c2] = size (s2); |
|
46 if (r1 == r2 && c1 == c2) |
|
47 if (c1 == 0) |
|
48 retval = true; |
|
49 else |
|
50 if (c1 > n) |
|
51 t1 = s1(:, 1:n); |
|
52 t2 = s2(:, 1:n); |
|
53 retval = all (all (t1 == t2)); |
|
54 else |
|
55 retval = all (all (s1 == s2)); |
|
56 endif |
|
57 endif |
|
58 elseif (r1 == r2) |
|
59 if (r1 == 0) |
|
60 retval = true; |
|
61 else |
|
62 l1 = min(n, c1); |
|
63 l2 = min(n, c2); |
|
64 if (l1 == l2) |
|
65 t1 = s1(:, 1:l1); |
|
66 t2 = s2(:, 1:l2); |
|
67 retval = all (all (t1 == t2)); |
|
68 endif |
|
69 endif |
|
70 endif |
|
71 elseif (iscellstr (s2)) |
|
72 [r2, c2] = size (s2); |
|
73 if (r1 == 1) |
|
74 t2 = s2(:); |
|
75 m = length (t2); |
|
76 retval = zeros (m, 1, "logical"); |
|
77 for i = 1:m |
|
78 retval(i) = strncmp (s1, t2{i}, n); |
|
79 endfor |
|
80 retval = reshape (retval, r2, c2); |
|
81 elseif (r1 > 1) |
|
82 if (r2 == 1 && c2 == 1) |
|
83 t2 = s2{1}; |
|
84 retval = zeros (r1, 1, "logical"); |
|
85 for i = 1:r1 |
|
86 retval(i) = strncmp (deblank (s1(i,:)), t2, n); |
|
87 endfor |
|
88 else |
|
89 t2 = s2(:); |
|
90 m = length (t2); |
|
91 if (m == r1) |
|
92 retval = zeros (m, 1, "logical"); |
|
93 for i = 1:m |
|
94 retval(i) = strncmp (deblank (s1(i,:)), t2{i}, n); |
|
95 endfor |
|
96 retval = reshape (retval, r2, c2); |
|
97 else |
|
98 error ("strncmp: nonconformant arrays"); |
|
99 endif |
|
100 endif |
|
101 endif |
|
102 endif |
|
103 elseif (iscellstr (s1)) |
|
104 [r1, c1] = size (s1); |
5443
|
105 if (ischar (s2)) |
5372
|
106 retval = strncmp (s2, s1, n, "logical"); |
|
107 elseif (iscellstr (s2)) |
|
108 [r2, c2] = size (s2); |
|
109 if (r1 == 1 && c1 == 1) |
|
110 t1 = s1{:}; |
|
111 t2 = s2(:); |
|
112 m = length (t2); |
|
113 retval = zeros (m, 1); |
|
114 for i = 1:m |
|
115 retval(i) = strncmp (t1, t2{i}, n); |
|
116 endfor |
|
117 retval = reshape (retval, r2, c2); |
|
118 elseif (r2 == 1 && c2 == 1) |
|
119 ## retval = strncmp (s2, s1, n); |
|
120 t1 = s1(:); |
|
121 t2 = s2{:}; |
|
122 m = length (t1); |
|
123 retval = zeros (m, 1, "logical"); |
|
124 for i = 1:m |
|
125 retval(i) = strncmp (t1{i}, t2, n); |
|
126 endfor |
|
127 retval = reshape (retval, r1, c1); |
|
128 elseif (r1 == r2 && c1 == c2) |
|
129 t1 = s1(:); |
|
130 t2 = s2(:); |
|
131 m = length (t1); |
|
132 for i = 1:m |
|
133 retval(i) = strncmp (t1{i}, t2{i}, n); |
|
134 endfor |
|
135 retval = reshape (retval, r1, c1); |
|
136 else |
|
137 error ("strncmp: nonconformant cell arrays"); |
|
138 endif |
|
139 endif |
|
140 endif |
|
141 |
|
142 if (n < 1) |
|
143 retval = zeros (size (retval), "logical"); |
|
144 endif |
|
145 |
|
146 endfunction |