annotate src/strfns.cc @ 10840:89f4d7e294cc

Grammarcheck .cc files
author Rik <octave@nomad.inbox5.com>
date Sat, 31 Jul 2010 11:18:11 -0700
parents 12884915a8e4
children a4f482e66b65
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
807
c03b179482fc [project @ 1994-10-14 14:21:58 by jwe]
jwe
parents:
diff changeset
1 /*
c03b179482fc [project @ 1994-10-14 14:21:58 by jwe]
jwe
parents:
diff changeset
2
7017
a1dbe9d80eee [project @ 2007-10-12 21:27:11 by jwe]
jwe
parents: 7016
diff changeset
3 Copyright (C) 1994, 1995, 1996, 1997, 1999, 2002, 2003, 2004, 2005,
8920
eb63fbe60fab update copyright notices
John W. Eaton <jwe@octave.org>
parents: 8825
diff changeset
4 2006, 2007, 2008, 2009 John W. Eaton
807
c03b179482fc [project @ 1994-10-14 14:21:58 by jwe]
jwe
parents:
diff changeset
5
c03b179482fc [project @ 1994-10-14 14:21:58 by jwe]
jwe
parents:
diff changeset
6 This file is part of Octave.
c03b179482fc [project @ 1994-10-14 14:21:58 by jwe]
jwe
parents:
diff changeset
7
c03b179482fc [project @ 1994-10-14 14:21:58 by jwe]
jwe
parents:
diff changeset
8 Octave is free software; you can redistribute it and/or modify it
c03b179482fc [project @ 1994-10-14 14:21:58 by jwe]
jwe
parents:
diff changeset
9 under the terms of the GNU General Public License as published by the
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6678
diff changeset
10 Free Software Foundation; either version 3 of the License, or (at your
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6678
diff changeset
11 option) any later version.
807
c03b179482fc [project @ 1994-10-14 14:21:58 by jwe]
jwe
parents:
diff changeset
12
c03b179482fc [project @ 1994-10-14 14:21:58 by jwe]
jwe
parents:
diff changeset
13 Octave is distributed in the hope that it will be useful, but WITHOUT
c03b179482fc [project @ 1994-10-14 14:21:58 by jwe]
jwe
parents:
diff changeset
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
c03b179482fc [project @ 1994-10-14 14:21:58 by jwe]
jwe
parents:
diff changeset
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
c03b179482fc [project @ 1994-10-14 14:21:58 by jwe]
jwe
parents:
diff changeset
16 for more details.
c03b179482fc [project @ 1994-10-14 14:21:58 by jwe]
jwe
parents:
diff changeset
17
c03b179482fc [project @ 1994-10-14 14:21:58 by jwe]
jwe
parents:
diff changeset
18 You should have received a copy of the GNU General Public License
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6678
diff changeset
19 along with Octave; see the file COPYING. If not, see
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6678
diff changeset
20 <http://www.gnu.org/licenses/>.
807
c03b179482fc [project @ 1994-10-14 14:21:58 by jwe]
jwe
parents:
diff changeset
21
c03b179482fc [project @ 1994-10-14 14:21:58 by jwe]
jwe
parents:
diff changeset
22 */
c03b179482fc [project @ 1994-10-14 14:21:58 by jwe]
jwe
parents:
diff changeset
23
c03b179482fc [project @ 1994-10-14 14:21:58 by jwe]
jwe
parents:
diff changeset
24 #ifdef HAVE_CONFIG_H
1192
b6360f2d4fa6 [project @ 1995-03-30 21:38:35 by jwe]
jwe
parents: 1009
diff changeset
25 #include <config.h>
807
c03b179482fc [project @ 1994-10-14 14:21:58 by jwe]
jwe
parents:
diff changeset
26 #endif
c03b179482fc [project @ 1994-10-14 14:21:58 by jwe]
jwe
parents:
diff changeset
27
1355
94697d007075 [project @ 1995-09-05 20:04:15 by jwe]
jwe
parents: 1315
diff changeset
28 #include <cctype>
7528
26d8a92644de try to avoid ctype macro problems
John W. Eaton <jwe@octave.org>
parents: 7017
diff changeset
29
8463
a74871446af7 Fchar, Fstrvcat: use queue to avoid duplicate calls to all_strings
John W. Eaton <jwe@octave.org>
parents: 8462
diff changeset
30 #include <queue>
5765
7ba9ad1fec11 [project @ 2006-04-17 05:05:15 by jwe]
jwe
parents: 5707
diff changeset
31 #include <sstream>
807
c03b179482fc [project @ 1994-10-14 14:21:58 by jwe]
jwe
parents:
diff changeset
32
c03b179482fc [project @ 1994-10-14 14:21:58 by jwe]
jwe
parents:
diff changeset
33 #include "dMatrix.h"
c03b179482fc [project @ 1994-10-14 14:21:58 by jwe]
jwe
parents:
diff changeset
34
5416
9433f94acd29 [project @ 2005-07-18 18:36:29 by jwe]
jwe
parents: 5415
diff changeset
35 #include "Cell.h"
1355
94697d007075 [project @ 1995-09-05 20:04:15 by jwe]
jwe
parents: 1315
diff changeset
36 #include "defun.h"
807
c03b179482fc [project @ 1994-10-14 14:21:58 by jwe]
jwe
parents:
diff changeset
37 #include "error.h"
c03b179482fc [project @ 1994-10-14 14:21:58 by jwe]
jwe
parents:
diff changeset
38 #include "gripes.h"
2366
c90105b99479 [project @ 1996-10-11 23:22:27 by jwe]
jwe
parents: 2267
diff changeset
39 #include "ov.h"
1355
94697d007075 [project @ 1995-09-05 20:04:15 by jwe]
jwe
parents: 1315
diff changeset
40 #include "oct-obj.h"
4457
d7d9ca19960a [project @ 2003-07-11 03:05:38 by jwe]
jwe
parents: 4358
diff changeset
41 #include "unwind-prot.h"
807
c03b179482fc [project @ 1994-10-14 14:21:58 by jwe]
jwe
parents:
diff changeset
42 #include "utils.h"
c03b179482fc [project @ 1994-10-14 14:21:58 by jwe]
jwe
parents:
diff changeset
43
4358
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4233
diff changeset
44 DEFUN (char, args, ,
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4233
diff changeset
45 "-*- texinfo -*-\n\
9307
c2923c27c877 Various documentation improvements
Rik <rdrider0-list@yahoo.com>
parents: 9064
diff changeset
46 @deftypefn {Built-in Function} {} char (@var{x})\n\
c2923c27c877 Various documentation improvements
Rik <rdrider0-list@yahoo.com>
parents: 9064
diff changeset
47 @deftypefnx {Built-in Function} {} char (@var{x}, @dots{})\n\
c2923c27c877 Various documentation improvements
Rik <rdrider0-list@yahoo.com>
parents: 9064
diff changeset
48 @deftypefnx {Built-in Function} {} char (@var{s1}, @var{s2}, @dots{})\n\
4358
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4233
diff changeset
49 @deftypefnx {Built-in Function} {} char (@var{cell_array})\n\
8461
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
50 Create a string array from one or more numeric matrices, character\n\
9307
c2923c27c877 Various documentation improvements
Rik <rdrider0-list@yahoo.com>
parents: 9064
diff changeset
51 matrices, or cell arrays. Arguments are concatenated vertically.\n\
8461
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
52 The returned values are padded with blanks as needed to make each row\n\
9307
c2923c27c877 Various documentation improvements
Rik <rdrider0-list@yahoo.com>
parents: 9064
diff changeset
53 of the string array have the same length. Empty input strings are\n\
c2923c27c877 Various documentation improvements
Rik <rdrider0-list@yahoo.com>
parents: 9064
diff changeset
54 significant and will concatenated in the output.\n\
c2923c27c877 Various documentation improvements
Rik <rdrider0-list@yahoo.com>
parents: 9064
diff changeset
55 \n\
c2923c27c877 Various documentation improvements
Rik <rdrider0-list@yahoo.com>
parents: 9064
diff changeset
56 For numerical input, each element is converted\n\
c2923c27c877 Various documentation improvements
Rik <rdrider0-list@yahoo.com>
parents: 9064
diff changeset
57 to the corresponding ASCII character. A range error results if an input\n\
c2923c27c877 Various documentation improvements
Rik <rdrider0-list@yahoo.com>
parents: 9064
diff changeset
58 is outside the ASCII range (0-255).\n\
c2923c27c877 Various documentation improvements
Rik <rdrider0-list@yahoo.com>
parents: 9064
diff changeset
59 \n\
10840
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10350
diff changeset
60 For cell arrays, each element is concatenated separately. Cell arrays converted\n\
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10350
diff changeset
61 through\n\
9307
c2923c27c877 Various documentation improvements
Rik <rdrider0-list@yahoo.com>
parents: 9064
diff changeset
62 @code{char} can mostly be converted back with @code{cellstr}.\n\
8461
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
63 For example,\n\
4358
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4233
diff changeset
64 \n\
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4233
diff changeset
65 @example\n\
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4233
diff changeset
66 @group\n\
9307
c2923c27c877 Various documentation improvements
Rik <rdrider0-list@yahoo.com>
parents: 9064
diff changeset
67 char ([97, 98, 99], \"\", @{\"98\", \"99\", 100@}, \"str1\", [\"ha\", \"lf\"])\n\
8461
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
68 @result{} [\"abc \"\n\
9307
c2923c27c877 Various documentation improvements
Rik <rdrider0-list@yahoo.com>
parents: 9064
diff changeset
69 \" \"\n\
c2923c27c877 Various documentation improvements
Rik <rdrider0-list@yahoo.com>
parents: 9064
diff changeset
70 \"98 \"\n\
c2923c27c877 Various documentation improvements
Rik <rdrider0-list@yahoo.com>
parents: 9064
diff changeset
71 \"99 \"\n\
c2923c27c877 Various documentation improvements
Rik <rdrider0-list@yahoo.com>
parents: 9064
diff changeset
72 \"d \"\n\
c2923c27c877 Various documentation improvements
Rik <rdrider0-list@yahoo.com>
parents: 9064
diff changeset
73 \"str1 \"\n\
c2923c27c877 Various documentation improvements
Rik <rdrider0-list@yahoo.com>
parents: 9064
diff changeset
74 \"half \"]\n\
4358
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4233
diff changeset
75 @end group\n\
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4233
diff changeset
76 @end example\n\
9307
c2923c27c877 Various documentation improvements
Rik <rdrider0-list@yahoo.com>
parents: 9064
diff changeset
77 @seealso{strvcat, cellstr}\n\
4358
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4233
diff changeset
78 @end deftypefn")
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4233
diff changeset
79 {
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4233
diff changeset
80 octave_value retval;
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4233
diff changeset
81
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4233
diff changeset
82 int nargin = args.length ();
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4233
diff changeset
83
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4233
diff changeset
84 if (nargin == 1)
5281
f3266e7dbb99 [project @ 2005-04-14 19:35:20 by jwe]
jwe
parents: 5280
diff changeset
85 retval = args(0).convert_to_str (true, true,
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10234
diff changeset
86 args(0).is_dq_string () ? '"' : '\'');
4358
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4233
diff changeset
87 else if (nargin > 1)
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4233
diff changeset
88 {
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4233
diff changeset
89 int n_elts = 0;
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4233
diff changeset
90
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4233
diff changeset
91 int max_len = 0;
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4233
diff changeset
92
8463
a74871446af7 Fchar, Fstrvcat: use queue to avoid duplicate calls to all_strings
John W. Eaton <jwe@octave.org>
parents: 8462
diff changeset
93 std::queue<string_vector> args_as_strings;
a74871446af7 Fchar, Fstrvcat: use queue to avoid duplicate calls to all_strings
John W. Eaton <jwe@octave.org>
parents: 8462
diff changeset
94
4358
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4233
diff changeset
95 for (int i = 0; i < nargin; i++)
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10234
diff changeset
96 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10234
diff changeset
97 string_vector s = args(i).all_strings ();
4358
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4233
diff changeset
98
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10234
diff changeset
99 if (error_state)
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10234
diff changeset
100 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10234
diff changeset
101 error ("char: unable to convert some args to strings");
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10234
diff changeset
102 return retval;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10234
diff changeset
103 }
4358
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4233
diff changeset
104
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10234
diff changeset
105 if (s.length () > 0)
8353
349a555729a9 keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7528
diff changeset
106 n_elts += s.length ();
349a555729a9 keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7528
diff changeset
107 else
349a555729a9 keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7528
diff changeset
108 n_elts += 1;
4358
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4233
diff changeset
109
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10234
diff changeset
110 int s_max_len = s.max_length ();
4358
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4233
diff changeset
111
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10234
diff changeset
112 if (s_max_len > max_len)
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10234
diff changeset
113 max_len = s_max_len;
8463
a74871446af7 Fchar, Fstrvcat: use queue to avoid duplicate calls to all_strings
John W. Eaton <jwe@octave.org>
parents: 8462
diff changeset
114
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10234
diff changeset
115 args_as_strings.push (s);
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10234
diff changeset
116 }
4358
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4233
diff changeset
117
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4233
diff changeset
118 string_vector result (n_elts);
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4233
diff changeset
119
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4233
diff changeset
120 int k = 0;
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4233
diff changeset
121
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4233
diff changeset
122 for (int i = 0; i < nargin; i++)
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10234
diff changeset
123 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10234
diff changeset
124 string_vector s = args_as_strings.front ();
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10234
diff changeset
125 args_as_strings.pop ();
4358
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4233
diff changeset
126
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10234
diff changeset
127 int n = s.length ();
4358
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4233
diff changeset
128
8353
349a555729a9 keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7528
diff changeset
129 if (n > 0)
349a555729a9 keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7528
diff changeset
130 {
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10234
diff changeset
131 for (int j = 0; j < n; j++)
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10234
diff changeset
132 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10234
diff changeset
133 std::string t = s[j];
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10234
diff changeset
134 int t_len = t.length ();
4358
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4233
diff changeset
135
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10234
diff changeset
136 if (max_len > t_len)
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10234
diff changeset
137 t += std::string (max_len - t_len, ' ');
4358
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4233
diff changeset
138
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10234
diff changeset
139 result[k++] = t;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10234
diff changeset
140 }
8353
349a555729a9 keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7528
diff changeset
141 }
349a555729a9 keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7528
diff changeset
142 else
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10234
diff changeset
143 result[k++] = std::string (max_len, ' ');
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10234
diff changeset
144 }
4358
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4233
diff changeset
145
5280
25c12c80ccc3 [project @ 2005-04-14 19:19:45 by jwe]
jwe
parents: 4734
diff changeset
146 retval = octave_value (result, '\'');
4358
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4233
diff changeset
147 }
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4233
diff changeset
148 else
5823
080c08b192d8 [project @ 2006-05-19 05:32:17 by jwe]
jwe
parents: 5765
diff changeset
149 print_usage ();
4358
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4233
diff changeset
150
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4233
diff changeset
151 return retval;
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4233
diff changeset
152 }
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4233
diff changeset
153
8353
349a555729a9 keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7528
diff changeset
154 /*
349a555729a9 keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7528
diff changeset
155 %!error <Invalid call to char> char()
349a555729a9 keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7528
diff changeset
156 %!assert (char (100) == "d");
349a555729a9 keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7528
diff changeset
157 %!assert (all(char (100,100) == ["d";"d"]));
349a555729a9 keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7528
diff changeset
158 %!assert (all(char ({100,100}) == ["d";"d"]));
349a555729a9 keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7528
diff changeset
159 %!assert (all(char ([100,100]) == ["dd"]));
349a555729a9 keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7528
diff changeset
160 %!assert (all(char ({100,{100}}) == ["d";"d"]));
349a555729a9 keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7528
diff changeset
161 %!assert (all(char (100, [], 100) == ["d";" ";"d"]))
349a555729a9 keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7528
diff changeset
162 %!assert (all(char ({100, [], 100}) == ["d";" ";"d"]))
349a555729a9 keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7528
diff changeset
163 %!assert (all(char ({100,{100, {""}}}) == ["d";"d";" "]))
349a555729a9 keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7528
diff changeset
164 %!assert (all(char (["a";"be"], {"c", 100}) == ["a";"be";"c";"d"]))
8372
8dff9cba15fe move str2mat to deprecated and make it a simple wrapper around char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8353
diff changeset
165 %!assert(strcmp (char ("a", "bb", "ccc"), ["a "; "bb "; "ccc"]));
8353
349a555729a9 keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7528
diff changeset
166 */
349a555729a9 keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7528
diff changeset
167
8462
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
168 DEFUN (strvcat, args, ,
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
169 "-*- texinfo -*-\n\
9307
c2923c27c877 Various documentation improvements
Rik <rdrider0-list@yahoo.com>
parents: 9064
diff changeset
170 @deftypefn {Built-in Function} {} strvcat (@var{x})\n\
c2923c27c877 Various documentation improvements
Rik <rdrider0-list@yahoo.com>
parents: 9064
diff changeset
171 @deftypefnx {Built-in Function} {} strvcat (@var{x}, @dots{})\n\
c2923c27c877 Various documentation improvements
Rik <rdrider0-list@yahoo.com>
parents: 9064
diff changeset
172 @deftypefnx {Built-in Function} {} strvcat (@var{s1}, @var{s2}, @dots{})\n\
8462
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
173 @deftypefnx {Built-in Function} {} strvcat (@var{cell_array})\n\
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
174 Create a character array from one or more numeric matrices, character\n\
9307
c2923c27c877 Various documentation improvements
Rik <rdrider0-list@yahoo.com>
parents: 9064
diff changeset
175 matrices, or cell arrays. Arguments are concatenated vertically.\n\
8462
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
176 The returned values are padded with blanks as needed to make each row\n\
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
177 of the string array have the same length. Unlike @code{char}, empty\n\
9307
c2923c27c877 Various documentation improvements
Rik <rdrider0-list@yahoo.com>
parents: 9064
diff changeset
178 strings are removed and will not appear in the output.\n\
c2923c27c877 Various documentation improvements
Rik <rdrider0-list@yahoo.com>
parents: 9064
diff changeset
179 \n\
c2923c27c877 Various documentation improvements
Rik <rdrider0-list@yahoo.com>
parents: 9064
diff changeset
180 For numerical input, each element is converted\n\
c2923c27c877 Various documentation improvements
Rik <rdrider0-list@yahoo.com>
parents: 9064
diff changeset
181 to the corresponding ASCII character. A range error results if an input\n\
c2923c27c877 Various documentation improvements
Rik <rdrider0-list@yahoo.com>
parents: 9064
diff changeset
182 is outside the ASCII range (0-255).\n\
c2923c27c877 Various documentation improvements
Rik <rdrider0-list@yahoo.com>
parents: 9064
diff changeset
183 \n\
10840
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10350
diff changeset
184 For cell arrays, each element is concatenated separately. Cell arrays converted\n\
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10350
diff changeset
185 through\n\
9307
c2923c27c877 Various documentation improvements
Rik <rdrider0-list@yahoo.com>
parents: 9064
diff changeset
186 @code{strvcat} can mostly be converted back with @code{cellstr}.\n\
8462
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
187 For example,\n\
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
188 \n\
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
189 @example\n\
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
190 @group\n\
9307
c2923c27c877 Various documentation improvements
Rik <rdrider0-list@yahoo.com>
parents: 9064
diff changeset
191 strvcat ([97, 98, 99], \"\", @{\"98\", \"99\", 100@}, \"str1\", [\"ha\", \"lf\"])\n\
8462
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
192 @result{} [\"abc \"\n\
9307
c2923c27c877 Various documentation improvements
Rik <rdrider0-list@yahoo.com>
parents: 9064
diff changeset
193 \"98 \"\n\
c2923c27c877 Various documentation improvements
Rik <rdrider0-list@yahoo.com>
parents: 9064
diff changeset
194 \"99 \"\n\
c2923c27c877 Various documentation improvements
Rik <rdrider0-list@yahoo.com>
parents: 9064
diff changeset
195 \"d \"\n\
c2923c27c877 Various documentation improvements
Rik <rdrider0-list@yahoo.com>
parents: 9064
diff changeset
196 \"str1 \"\n\
c2923c27c877 Various documentation improvements
Rik <rdrider0-list@yahoo.com>
parents: 9064
diff changeset
197 \"half \"]\n\
8462
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
198 @end group\n\
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
199 @end example\n\
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
200 @seealso{char, strcat, cstrcat}\n\
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
201 @end deftypefn")
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
202 {
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
203 octave_value retval;
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
204
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
205 int nargin = args.length ();
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
206
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
207 if (nargin > 0)
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
208 {
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
209 int n_elts = 0;
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
210
8809
732cb0236488 strfns.cc: use size_t or octave_idx_type instead of int as needed
John W. Eaton <jwe@octave.org>
parents: 8524
diff changeset
211 size_t max_len = 0;
8462
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
212
8463
a74871446af7 Fchar, Fstrvcat: use queue to avoid duplicate calls to all_strings
John W. Eaton <jwe@octave.org>
parents: 8462
diff changeset
213 std::queue<string_vector> args_as_strings;
a74871446af7 Fchar, Fstrvcat: use queue to avoid duplicate calls to all_strings
John W. Eaton <jwe@octave.org>
parents: 8462
diff changeset
214
8462
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
215 for (int i = 0; i < nargin; i++)
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10234
diff changeset
216 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10234
diff changeset
217 string_vector s = args(i).all_strings ();
8462
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
218
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10234
diff changeset
219 if (error_state)
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10234
diff changeset
220 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10234
diff changeset
221 error ("strvcat: unable to convert some args to strings");
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10234
diff changeset
222 return retval;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10234
diff changeset
223 }
8462
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
224
8809
732cb0236488 strfns.cc: use size_t or octave_idx_type instead of int as needed
John W. Eaton <jwe@octave.org>
parents: 8524
diff changeset
225 size_t n = s.length ();
8462
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
226
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
227 // do not count empty strings in calculation of number of elements
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
228 if (n > 0)
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
229 {
8809
732cb0236488 strfns.cc: use size_t or octave_idx_type instead of int as needed
John W. Eaton <jwe@octave.org>
parents: 8524
diff changeset
230 for (size_t j = 0; j < n; j++)
8462
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
231 {
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
232 if (s[j].length () > 0)
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
233 n_elts++;
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
234 }
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
235 }
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
236
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10234
diff changeset
237 size_t s_max_len = s.max_length ();
8462
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
238
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10234
diff changeset
239 if (s_max_len > max_len)
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10234
diff changeset
240 max_len = s_max_len;
8463
a74871446af7 Fchar, Fstrvcat: use queue to avoid duplicate calls to all_strings
John W. Eaton <jwe@octave.org>
parents: 8462
diff changeset
241
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10234
diff changeset
242 args_as_strings.push (s);
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10234
diff changeset
243 }
8462
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
244
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
245 string_vector result (n_elts);
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
246
8809
732cb0236488 strfns.cc: use size_t or octave_idx_type instead of int as needed
John W. Eaton <jwe@octave.org>
parents: 8524
diff changeset
247 octave_idx_type k = 0;
8462
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
248
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
249 for (int i = 0; i < nargin; i++)
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10234
diff changeset
250 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10234
diff changeset
251 string_vector s = args_as_strings.front ();
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10234
diff changeset
252 args_as_strings.pop ();
8462
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
253
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10234
diff changeset
254 size_t n = s.length ();
8462
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
255
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
256 if (n > 0)
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
257 {
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10234
diff changeset
258 for (size_t j = 0; j < n; j++)
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10234
diff changeset
259 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10234
diff changeset
260 std::string t = s[j];
8462
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
261 if (t.length () > 0)
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
262 {
8809
732cb0236488 strfns.cc: use size_t or octave_idx_type instead of int as needed
John W. Eaton <jwe@octave.org>
parents: 8524
diff changeset
263 size_t t_len = t.length ();
8462
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
264
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
265 if (max_len > t_len)
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
266 t += std::string (max_len - t_len, ' ');
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
267
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
268 result[k++] = t;
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
269 }
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10234
diff changeset
270 }
8462
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
271 }
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10234
diff changeset
272 }
8462
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
273
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
274 retval = octave_value (result, '\'');
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
275 }
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
276 else
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
277 print_usage ();
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
278
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
279 return retval;
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
280 }
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
281
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
282 /*
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
283 %!error <Invalid call to strvcat> strvcat()
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
284 %!assert (strvcat (""), "");
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
285 %!assert (strvcat (100) == "d");
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
286 %!assert (all(strvcat (100,100) == ["d";"d"]));
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
287 %!assert (all(strvcat ({100,100}) == ["d";"d"]));
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
288 %!assert (all(strvcat ([100,100]) == ["dd"]));
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
289 %!assert (all(strvcat ({100,{100}}) == ["d";"d"]));
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
290 %!assert (all(strvcat (100, [], 100) == ["d";"d"]))
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
291 %!assert (all(strvcat ({100, [], 100}) == ["d";"d"]))
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
292 %!assert (all(strvcat ({100,{100, {""}}}) == ["d";"d"]))
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
293 %!assert (all(strvcat (["a";"be"], {"c", 100}) == ["a";"be";"c";"d"]))
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
294 %!assert(strcmp (strvcat ("a", "bb", "ccc"), ["a "; "bb "; "ccc"]));
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
295 */
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
296
8353
349a555729a9 keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7528
diff changeset
297
4535
a5f23212a3d8 [project @ 2003-10-16 02:39:15 by jwe]
jwe
parents: 4457
diff changeset
298 DEFUN (ischar, args, ,
3361
4f40efa995c1 [project @ 1999-11-19 21:19:37 by jwe]
jwe
parents: 3014
diff changeset
299 "-*- texinfo -*-\n\
4535
a5f23212a3d8 [project @ 2003-10-16 02:39:15 by jwe]
jwe
parents: 4457
diff changeset
300 @deftypefn {Built-in Function} {} ischar (@var{a})\n\
8461
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
301 Return 1 if @var{a} is a character array. Otherwise, return 0.\n\
3361
4f40efa995c1 [project @ 1999-11-19 21:19:37 by jwe]
jwe
parents: 3014
diff changeset
302 @end deftypefn")
807
c03b179482fc [project @ 1994-10-14 14:21:58 by jwe]
jwe
parents:
diff changeset
303 {
4233
ccfdb55c8156 [project @ 2002-12-20 22:43:54 by jwe]
jwe
parents: 3363
diff changeset
304 octave_value retval;
807
c03b179482fc [project @ 1994-10-14 14:21:58 by jwe]
jwe
parents:
diff changeset
305
c03b179482fc [project @ 1994-10-14 14:21:58 by jwe]
jwe
parents:
diff changeset
306 int nargin = args.length ();
c03b179482fc [project @ 1994-10-14 14:21:58 by jwe]
jwe
parents:
diff changeset
307
c03b179482fc [project @ 1994-10-14 14:21:58 by jwe]
jwe
parents:
diff changeset
308 if (nargin == 1 && args(0).is_defined ())
4233
ccfdb55c8156 [project @ 2002-12-20 22:43:54 by jwe]
jwe
parents: 3363
diff changeset
309 retval = args(0).is_string ();
807
c03b179482fc [project @ 1994-10-14 14:21:58 by jwe]
jwe
parents:
diff changeset
310 else
5823
080c08b192d8 [project @ 2006-05-19 05:32:17 by jwe]
jwe
parents: 5765
diff changeset
311 print_usage ();
807
c03b179482fc [project @ 1994-10-14 14:21:58 by jwe]
jwe
parents:
diff changeset
312
c03b179482fc [project @ 1994-10-14 14:21:58 by jwe]
jwe
parents:
diff changeset
313 return retval;
c03b179482fc [project @ 1994-10-14 14:21:58 by jwe]
jwe
parents:
diff changeset
314 }
c03b179482fc [project @ 1994-10-14 14:21:58 by jwe]
jwe
parents:
diff changeset
315
8461
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
316 /*
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
317
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
318 %!assert (ischar ("a"), logical (1));
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
319 %!assert (ischar (["ab";"cd"]), logical (1));
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
320 %!assert (ischar ({"ab"}), logical (0));
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
321 %!assert (ischar (1), logical (0));
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
322 %!error <Invalid call to ischar.*> ischar ();
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
323
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
324 */
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
325
10108
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
326 static octave_value
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
327 do_strcmp_fun (const octave_value& arg0, const octave_value& arg1,
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
328 octave_idx_type n, const char *fcn_name,
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
329 bool (*array_op) (const charNDArray&, const charNDArray&, octave_idx_type),
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
330 bool (*str_op) (const std::string&, const std::string&, octave_idx_type))
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
331
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
332 {
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
333 octave_value retval;
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
334
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
335 bool s1_string = arg0.is_string ();
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
336 bool s1_cell = arg0.is_cell ();
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
337 bool s2_string = arg1.is_string ();
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
338 bool s2_cell = arg1.is_cell ();
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
339
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
340 if (s1_string && s2_string)
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
341 retval = array_op (arg0.char_array_value (), arg1.char_array_value (), n);
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
342 else if ((s1_string && s2_cell) || (s1_cell && s2_string))
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
343 {
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
344 octave_value str_val, cell_val;
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
345
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
346 if (s1_string)
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
347 {
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
348 str_val = arg0;
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
349 cell_val = arg1;
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
350 }
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
351 else
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
352 {
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
353 str_val = arg1;
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
354 cell_val = arg0;
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
355 }
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
356
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
357 const Cell cell = cell_val.cell_value ();
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
358 const string_vector str = str_val.all_strings ();
10350
12884915a8e4 merge MArray classes & improve Array interface
Jaroslav Hajek <highegg@gmail.com>
parents: 10318
diff changeset
359 octave_idx_type r = str.length ();
10108
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
360
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
361 if (r == 0 || r == 1)
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
362 {
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
363 // Broadcast the string.
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
364
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
365 boolNDArray output (cell_val.dims (), false);
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
366
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
367 std::string s = r == 0 ? std::string () : str[0];
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
368
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
369 if (cell_val.is_cellstr ())
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
370 {
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
371 const Array<std::string> cellstr = cell_val.cellstr_value ();
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
372 for (octave_idx_type i = 0; i < cellstr.length (); i++)
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
373 output(i) = str_op (cellstr(i), s, n);
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
374 }
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
375 else
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
376 {
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
377 // FIXME: should we warn here?
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
378 for (octave_idx_type i = 0; i < cell.length (); i++)
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
379 {
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
380 if (cell(i).is_string ())
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
381 output(i) = str_op (cell(i).string_value (), s, n);
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
382 }
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
383 }
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
384
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
385 retval = output;
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
386 }
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
387 else if (r > 1)
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
388 {
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
389 if (cell.length () == 1)
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
390 {
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
391 // Broadcast the cell.
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
392
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
393 const dim_vector dv (r, 1);
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
394 boolNDArray output (dv, false);
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
395
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
396 if (cell(0).is_string ())
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
397 {
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
398 const std::string str2 = cell(0).string_value ();
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
399
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
400 for (octave_idx_type i = 0; i < r; i++)
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
401 output(i) = str_op (str[i], str2, n);
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
402 }
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
403
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
404 retval = output;
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
405 }
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
406 else
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
407 {
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
408 // Must match in all dimensions.
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
409
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
410 boolNDArray output (cell.dims (), false);
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
411
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
412 if (cell.length () == r)
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
413 {
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
414 if (cell_val.is_cellstr ())
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
415 {
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
416 const Array<std::string> cellstr = cell_val.cellstr_value ();
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
417 for (octave_idx_type i = 0; i < cellstr.length (); i++)
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
418 output(i) = str_op (str[i], cellstr(i), n);
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
419 }
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
420 else
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
421 {
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
422 // FIXME: should we warn here?
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
423 for (octave_idx_type i = 0; i < r; i++)
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
424 {
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
425 if (cell(i).is_string ())
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
426 output(i) = str_op (str[i], cell(i).string_value (), n);
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
427 }
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
428 }
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
429
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
430 retval = output;
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
431 }
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
432 else
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
433 retval = false;
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
434 }
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
435 }
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
436 }
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
437 else if (s1_cell && s2_cell)
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
438 {
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
439 octave_value cell1_val, cell2_val;
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
440 octave_idx_type r1 = arg0.numel (), r2;
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
441
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
442 if (r1 == 1)
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
443 {
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
444 // Make the singleton cell2.
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
445
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
446 cell1_val = arg1;
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
447 cell2_val = arg0;
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
448 }
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
449 else
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
450 {
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
451 cell1_val = arg0;
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
452 cell2_val = arg1;
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
453 }
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
454
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
455 const Cell cell1 = cell1_val.cell_value ();
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
456 const Cell cell2 = cell2_val.cell_value ();
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
457 r1 = cell1.numel ();
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
458 r2 = cell2.numel ();
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
459
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
460 const dim_vector size1 = cell1.dims ();
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
461 const dim_vector size2 = cell2.dims ();
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
462
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
463 boolNDArray output (size1, false);
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
464
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
465 if (r2 == 1)
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
466 {
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
467 // Broadcast cell2.
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
468
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
469 if (cell2(0).is_string ())
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
470 {
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
471 const std::string str2 = cell2(0).string_value ();
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
472
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
473 if (cell1_val.is_cellstr ())
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
474 {
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
475 const Array<std::string> cellstr = cell1_val.cellstr_value ();
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
476 for (octave_idx_type i = 0; i < cellstr.length (); i++)
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
477 output(i) = str_op (cellstr(i), str2, n);
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
478 }
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
479 else
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
480 {
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
481 // FIXME: should we warn here?
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
482 for (octave_idx_type i = 0; i < r1; i++)
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
483 {
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
484 if (cell1(i).is_string ())
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
485 {
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
486 const std::string str1 = cell1(i).string_value ();
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
487 output(i) = str_op (str1, str2, n);
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
488 }
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
489 }
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
490 }
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
491 }
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
492 }
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
493 else
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
494 {
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
495 if (size1 != size2)
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
496 {
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
497 error ("%s: nonconformant cell arrays", fcn_name);
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
498 return retval;
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
499 }
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
500
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
501 if (cell1.is_cellstr () && cell2.is_cellstr ())
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
502 {
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
503 const Array<std::string> cellstr1 = cell1_val.cellstr_value ();
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
504 const Array<std::string> cellstr2 = cell2_val.cellstr_value ();
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
505 for (octave_idx_type i = 0; i < r1; i++)
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
506 output (i) = str_op (cellstr1(i), cellstr2(i), n);
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
507 }
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
508 else
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
509 {
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
510 // FIXME: should we warn here?
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
511 for (octave_idx_type i = 0; i < r1; i++)
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
512 {
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
513 if (cell1(i).is_string () && cell2(i).is_string ())
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
514 {
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
515 const std::string str1 = cell1(i).string_value ();
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
516 const std::string str2 = cell2(i).string_value ();
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
517 output(i) = str_op (str1, str2, n);
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
518 }
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
519 }
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
520 }
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
521 }
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
522
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
523 retval = output;
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
524 }
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
525 else
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
526 retval = false;
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
527
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
528 return retval;
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
529 }
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
530
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
531 // If both args are arrays, dimensions may be significant.
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
532 static bool
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
533 strcmp_array_op (const charNDArray& s1, const charNDArray& s2, octave_idx_type)
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
534 {
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
535 return (s1.dims () == s2.dims ()
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
536 && std::equal (s1.data (), s1.data () + s1.numel (), s2.data ()));
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
537 }
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
538
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
539 // Otherwise, just use strings.
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
540 static bool
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
541 strcmp_str_op (const std::string& s1, const std::string& s2,
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
542 octave_idx_type)
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
543 {
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
544 return s1 == s2;
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
545 }
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
546
5415
1fbae9f046e3 [project @ 2005-07-18 18:16:12 by jwe]
jwe
parents: 5307
diff changeset
547 DEFUN (strcmp, args, ,
1fbae9f046e3 [project @ 2005-07-18 18:16:12 by jwe]
jwe
parents: 5307
diff changeset
548 "-*- texinfo -*-\n\
6678
49724abe1236 [project @ 2007-05-31 19:44:45 by jwe]
jwe
parents: 6256
diff changeset
549 @deftypefn {Built-in Function} {} strcmp (@var{s1}, @var{s2})\n\
5415
1fbae9f046e3 [project @ 2005-07-18 18:16:12 by jwe]
jwe
parents: 5307
diff changeset
550 Return 1 if the character strings @var{s1} and @var{s2} are the same,\n\
1fbae9f046e3 [project @ 2005-07-18 18:16:12 by jwe]
jwe
parents: 5307
diff changeset
551 and 0 otherwise.\n\
5674
86adc85cc471 [project @ 2006-03-16 03:46:45 by jwe]
jwe
parents: 5531
diff changeset
552 \n\
86adc85cc471 [project @ 2006-03-16 03:46:45 by jwe]
jwe
parents: 5531
diff changeset
553 If either @var{s1} or @var{s2} is a cell array of strings, then an array\n\
86adc85cc471 [project @ 2006-03-16 03:46:45 by jwe]
jwe
parents: 5531
diff changeset
554 of the same size is returned, containing the values described above for\n\
9036
58604c45ca74 Cleanup of data types related documentation
Rik <rdrider0-list@yahoo.com>
parents: 8920
diff changeset
555 every member of the cell array. The other argument may also be a cell\n\
5674
86adc85cc471 [project @ 2006-03-16 03:46:45 by jwe]
jwe
parents: 5531
diff changeset
556 array of strings (of the same size or with only one element), char matrix\n\
86adc85cc471 [project @ 2006-03-16 03:46:45 by jwe]
jwe
parents: 5531
diff changeset
557 or character string.\n\
86adc85cc471 [project @ 2006-03-16 03:46:45 by jwe]
jwe
parents: 5531
diff changeset
558 \n\
9064
7c02ec148a3c Check grammar on all .cc files
Rik <rdrider0-list@yahoo.com>
parents: 9036
diff changeset
559 @strong{Caution:} For compatibility with @sc{matlab}, Octave's strcmp\n\
5674
86adc85cc471 [project @ 2006-03-16 03:46:45 by jwe]
jwe
parents: 5531
diff changeset
560 function returns 1 if the character strings are equal, and 0 otherwise.\n\
86adc85cc471 [project @ 2006-03-16 03:46:45 by jwe]
jwe
parents: 5531
diff changeset
561 This is just the opposite of the corresponding C library function.\n\
86adc85cc471 [project @ 2006-03-16 03:46:45 by jwe]
jwe
parents: 5531
diff changeset
562 @seealso{strcmpi, strncmp, strncmpi}\n\
5415
1fbae9f046e3 [project @ 2005-07-18 18:16:12 by jwe]
jwe
parents: 5307
diff changeset
563 @end deftypefn")
1fbae9f046e3 [project @ 2005-07-18 18:16:12 by jwe]
jwe
parents: 5307
diff changeset
564 {
5531
ad2e70544c77 [project @ 2005-11-07 19:12:21 by jwe]
jwe
parents: 5530
diff changeset
565 octave_value retval;
5415
1fbae9f046e3 [project @ 2005-07-18 18:16:12 by jwe]
jwe
parents: 5307
diff changeset
566
5416
9433f94acd29 [project @ 2005-07-18 18:36:29 by jwe]
jwe
parents: 5415
diff changeset
567 if (args.length () == 2)
9433f94acd29 [project @ 2005-07-18 18:36:29 by jwe]
jwe
parents: 5415
diff changeset
568 {
10108
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
569 retval = do_strcmp_fun (args (0), args (1), 0,
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
570 "strcmp", strcmp_array_op, strcmp_str_op);
5415
1fbae9f046e3 [project @ 2005-07-18 18:16:12 by jwe]
jwe
parents: 5307
diff changeset
571 }
5416
9433f94acd29 [project @ 2005-07-18 18:36:29 by jwe]
jwe
parents: 5415
diff changeset
572 else
5823
080c08b192d8 [project @ 2006-05-19 05:32:17 by jwe]
jwe
parents: 5765
diff changeset
573 print_usage ();
5415
1fbae9f046e3 [project @ 2005-07-18 18:16:12 by jwe]
jwe
parents: 5307
diff changeset
574
1fbae9f046e3 [project @ 2005-07-18 18:16:12 by jwe]
jwe
parents: 5307
diff changeset
575 return retval;
1fbae9f046e3 [project @ 2005-07-18 18:16:12 by jwe]
jwe
parents: 5307
diff changeset
576 }
1fbae9f046e3 [project @ 2005-07-18 18:16:12 by jwe]
jwe
parents: 5307
diff changeset
577
5862
2b35a7fe9302 [project @ 2006-06-20 04:31:56 by jwe]
jwe
parents: 5823
diff changeset
578 /*
2b35a7fe9302 [project @ 2006-06-20 04:31:56 by jwe]
jwe
parents: 5823
diff changeset
579 %!shared x
2b35a7fe9302 [project @ 2006-06-20 04:31:56 by jwe]
jwe
parents: 5823
diff changeset
580 %! x = char (zeros (0, 2));
2b35a7fe9302 [project @ 2006-06-20 04:31:56 by jwe]
jwe
parents: 5823
diff changeset
581 %!assert (strcmp ('', x) == false);
2b35a7fe9302 [project @ 2006-06-20 04:31:56 by jwe]
jwe
parents: 5823
diff changeset
582 %!assert (strcmp (x, '') == false);
2b35a7fe9302 [project @ 2006-06-20 04:31:56 by jwe]
jwe
parents: 5823
diff changeset
583 %!assert (strcmp (x, x) == true);
5911
a65b51ed388c [project @ 2006-07-28 16:31:00 by jwe]
jwe
parents: 5862
diff changeset
584 ## %!assert (strcmp ({''}, x) == false);
a65b51ed388c [project @ 2006-07-28 16:31:00 by jwe]
jwe
parents: 5862
diff changeset
585 ## %!assert (strcmp ({x}, '') == false);
a65b51ed388c [project @ 2006-07-28 16:31:00 by jwe]
jwe
parents: 5862
diff changeset
586 ## %!assert (strcmp ({x}, x) == true);
a65b51ed388c [project @ 2006-07-28 16:31:00 by jwe]
jwe
parents: 5862
diff changeset
587 ## %!assert (strcmp ('', {x}) == false);
a65b51ed388c [project @ 2006-07-28 16:31:00 by jwe]
jwe
parents: 5862
diff changeset
588 ## %!assert (strcmp (x, {''}) == false);
a65b51ed388c [project @ 2006-07-28 16:31:00 by jwe]
jwe
parents: 5862
diff changeset
589 ## %!assert (strcmp (x, {x}) == true);
a65b51ed388c [project @ 2006-07-28 16:31:00 by jwe]
jwe
parents: 5862
diff changeset
590 ## %!assert (all (strcmp ({x; x}, '') == [false; false]));
a65b51ed388c [project @ 2006-07-28 16:31:00 by jwe]
jwe
parents: 5862
diff changeset
591 ## %!assert (all (strcmp ({x; x}, {''}) == [false; false]));
a65b51ed388c [project @ 2006-07-28 16:31:00 by jwe]
jwe
parents: 5862
diff changeset
592 ## %!assert (all (strcmp ('', {x; x}) == [false; false]));
a65b51ed388c [project @ 2006-07-28 16:31:00 by jwe]
jwe
parents: 5862
diff changeset
593 ## %!assert (all (strcmp ({''}, {x; x}) == [false; false]));
5862
2b35a7fe9302 [project @ 2006-06-20 04:31:56 by jwe]
jwe
parents: 5823
diff changeset
594 %!assert (strcmp ({'foo'}, x) == false);
2b35a7fe9302 [project @ 2006-06-20 04:31:56 by jwe]
jwe
parents: 5823
diff changeset
595 %!assert (strcmp ({'foo'}, 'foo') == true);
2b35a7fe9302 [project @ 2006-06-20 04:31:56 by jwe]
jwe
parents: 5823
diff changeset
596 %!assert (strcmp ({'foo'}, x) == false);
2b35a7fe9302 [project @ 2006-06-20 04:31:56 by jwe]
jwe
parents: 5823
diff changeset
597 %!assert (strcmp (x, {'foo'}) == false);
2b35a7fe9302 [project @ 2006-06-20 04:31:56 by jwe]
jwe
parents: 5823
diff changeset
598 %!assert (strcmp ('foo', {'foo'}) == true);
2b35a7fe9302 [project @ 2006-06-20 04:31:56 by jwe]
jwe
parents: 5823
diff changeset
599 %!assert (strcmp (x, {'foo'}) == false);
2b35a7fe9302 [project @ 2006-06-20 04:31:56 by jwe]
jwe
parents: 5823
diff changeset
600 %!shared y
2b35a7fe9302 [project @ 2006-06-20 04:31:56 by jwe]
jwe
parents: 5823
diff changeset
601 %! y = char (zeros (2, 0));
2b35a7fe9302 [project @ 2006-06-20 04:31:56 by jwe]
jwe
parents: 5823
diff changeset
602 %!assert (strcmp ('', y) == false);
2b35a7fe9302 [project @ 2006-06-20 04:31:56 by jwe]
jwe
parents: 5823
diff changeset
603 %!assert (strcmp (y, '') == false);
2b35a7fe9302 [project @ 2006-06-20 04:31:56 by jwe]
jwe
parents: 5823
diff changeset
604 %!assert (strcmp (y, y) == true);
2b35a7fe9302 [project @ 2006-06-20 04:31:56 by jwe]
jwe
parents: 5823
diff changeset
605 %!assert (all (strcmp ({''}, y) == [true; true]));
2b35a7fe9302 [project @ 2006-06-20 04:31:56 by jwe]
jwe
parents: 5823
diff changeset
606 %!assert (strcmp ({y}, '') == true);
2b35a7fe9302 [project @ 2006-06-20 04:31:56 by jwe]
jwe
parents: 5823
diff changeset
607 %!assert (all (strcmp ({y}, y) == [true; true]));
2b35a7fe9302 [project @ 2006-06-20 04:31:56 by jwe]
jwe
parents: 5823
diff changeset
608 %!assert (all (strcmp ('', {y}) == [true; true]));
2b35a7fe9302 [project @ 2006-06-20 04:31:56 by jwe]
jwe
parents: 5823
diff changeset
609 %!assert (all (strcmp (y, {''}) == [true; true]));
2b35a7fe9302 [project @ 2006-06-20 04:31:56 by jwe]
jwe
parents: 5823
diff changeset
610 %!assert (all (strcmp (y, {y}) == [true; true]));
5911
a65b51ed388c [project @ 2006-07-28 16:31:00 by jwe]
jwe
parents: 5862
diff changeset
611 ## %!assert (all (strcmp ({y; y}, '') == [false; false]));
a65b51ed388c [project @ 2006-07-28 16:31:00 by jwe]
jwe
parents: 5862
diff changeset
612 ## %!assert (all (strcmp ({y; y}, {''}) == [false; false]));
a65b51ed388c [project @ 2006-07-28 16:31:00 by jwe]
jwe
parents: 5862
diff changeset
613 ## %!assert (all (strcmp ('', {y; y}) == [false; false]));
a65b51ed388c [project @ 2006-07-28 16:31:00 by jwe]
jwe
parents: 5862
diff changeset
614 ## %!assert (all (strcmp ({''}, {y; y}) == [false; false]));
5862
2b35a7fe9302 [project @ 2006-06-20 04:31:56 by jwe]
jwe
parents: 5823
diff changeset
615 %!assert (all (strcmp ({'foo'}, y) == [false; false]));
2b35a7fe9302 [project @ 2006-06-20 04:31:56 by jwe]
jwe
parents: 5823
diff changeset
616 %!assert (all (strcmp ({'foo'}, y) == [false; false]));
2b35a7fe9302 [project @ 2006-06-20 04:31:56 by jwe]
jwe
parents: 5823
diff changeset
617 %!assert (all (strcmp (y, {'foo'}) == [false; false]));
2b35a7fe9302 [project @ 2006-06-20 04:31:56 by jwe]
jwe
parents: 5823
diff changeset
618 %!assert (all (strcmp (y, {'foo'}) == [false; false]));
2b35a7fe9302 [project @ 2006-06-20 04:31:56 by jwe]
jwe
parents: 5823
diff changeset
619 */
2b35a7fe9302 [project @ 2006-06-20 04:31:56 by jwe]
jwe
parents: 5823
diff changeset
620
10108
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
621 // Apparently, Matlab ignores the dims with strncmp. It also
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
622 static bool
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
623 strncmp_array_op (const charNDArray& s1, const charNDArray& s2, octave_idx_type n)
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
624 {
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
625 octave_idx_type l1 = s1.numel (), l2 = s2.numel ();
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
626 return (n > 0 && n <= l1 && n <= l2
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
627 && std::equal (s1.data (), s1.data () + n, s2.data ()));
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
628 }
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
629
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
630 // Otherwise, just use strings. Note that we neither extract substrings (which
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
631 // would mean a copy, at least in GCC), nor use string::compare (which is a
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
632 // 3-way compare).
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
633 static bool
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
634 strncmp_str_op (const std::string& s1, const std::string& s2, octave_idx_type n)
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
635 {
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
636 octave_idx_type l1 = s1.length (), l2 = s2.length ();
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
637 return (n > 0 && n <= l1 && n <= l2
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
638 && std::equal (s1.data (), s1.data () + n, s2.data ()));
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
639 }
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
640
6250
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
641 DEFUN (strncmp, args, ,
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
642 "-*- texinfo -*-\n\
6678
49724abe1236 [project @ 2007-05-31 19:44:45 by jwe]
jwe
parents: 6256
diff changeset
643 @deftypefn {Built-in Function} {} strncmp (@var{s1}, @var{s2}, @var{n})\n\
10840
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10350
diff changeset
644 Return 1 if the first @var{n} characters of strings @var{s1} and @var{s2} are\n\
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10350
diff changeset
645 the same, and 0 otherwise.\n\
6250
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
646 \n\
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
647 @example\n\
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
648 @group\n\
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
649 strncmp (\"abce\", \"abcd\", 3)\n\
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
650 @result{} 1\n\
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
651 @end group\n\
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
652 @end example\n\
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
653 \n\
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
654 If either @var{s1} or @var{s2} is a cell array of strings, then an array\n\
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
655 of the same size is returned, containing the values described above for\n\
9036
58604c45ca74 Cleanup of data types related documentation
Rik <rdrider0-list@yahoo.com>
parents: 8920
diff changeset
656 every member of the cell array. The other argument may also be a cell\n\
6250
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
657 array of strings (of the same size or with only one element), char matrix\n\
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
658 or character string.\n\
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
659 \n\
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
660 @example\n\
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
661 @group\n\
6256
83949ae13b2c [project @ 2007-01-30 18:45:59 by jwe]
jwe
parents: 6250
diff changeset
662 strncmp (\"abce\", @{\"abcd\", \"bca\", \"abc\"@}, 3)\n\
6250
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
663 @result{} [1, 0, 1]\n\
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
664 @end group\n\
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
665 @end example\n\
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
666 \n\
9064
7c02ec148a3c Check grammar on all .cc files
Rik <rdrider0-list@yahoo.com>
parents: 9036
diff changeset
667 @strong{Caution:} For compatibility with @sc{matlab}, Octave's strncmp\n\
6250
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
668 function returns 1 if the character strings are equal, and 0 otherwise.\n\
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
669 This is just the opposite of the corresponding C library function.\n\
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
670 @seealso{strncmpi, strcmp, strcmpi}\n\
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
671 @end deftypefn")
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
672 {
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
673 octave_value retval;
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
674
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
675 if (args.length () == 3)
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
676 {
10108
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
677 octave_idx_type n = args(2).idx_type_value ();
6250
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
678
10108
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
679 if (! error_state)
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
680 {
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
681 if (n > 0)
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
682 {
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
683 retval = do_strcmp_fun (args(0), args(1), n, "strncmp",
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
684 strncmp_array_op, strncmp_str_op);
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
685 }
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
686 else
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
687 error ("strncmp: N must be greater than 0");
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
688 }
6250
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
689 }
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
690 else
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
691 print_usage ();
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
692
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
693 return retval;
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
694 }
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
695
8461
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
696 /*
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
697 %!error <Invalid call to strncmp.*> strncmp ();
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
698 %!error <Invalid call to strncmp.*> strncmp ("abc", "def");
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
699 %!assert (strncmp ("abce", "abc", 3) == 1)
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
700 %!assert (strncmp (100, 100, 1) == 0)
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
701 %!assert (all (strncmp ("abce", {"abcd", "bca", "abc"}, 3) == [1, 0, 1]))
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
702 %!assert (all (strncmp ("abc", {"abcd", "bca", "abc"}, 4) == [0, 0, 0]))
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
703 %!assert (all (strncmp ({"abcd", "bca", "abc"},"abce", 3) == [1, 0, 1]))
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
704 %!assert (all (strncmp ({"abcd", "bca", "abc"},{"abcd", "bca", "abe"}, 3) == [1, 1, 0]))
8472
5451f7460ea6 strfns.cc (Fstrcmp, Fstrncmp): initialize all elements of output with constructor arg
John W. Eaton <jwe@octave.org>
parents: 8464
diff changeset
705 %!assert (all (strncmp("abc", {"abcd", 10}, 2) == [1, 0]))
8461
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
706 */
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
707
10108
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
708 // case-insensitive character equality functor
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
709 struct icmp_char_eq : public std::binary_function<char, char, bool>
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
710 {
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
711 bool operator () (char x, char y) const
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
712 { return std::toupper (x) == std::toupper (y); }
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
713 };
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
714
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
715 // strcmpi is equivalent to strcmp in that it checks all dims.
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
716 static bool
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
717 strcmpi_array_op (const charNDArray& s1, const charNDArray& s2, octave_idx_type)
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
718 {
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
719 return (s1.dims () == s2.dims ()
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
720 && std::equal (s1.data (), s1.data () + s1.numel (), s2.data (),
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
721 icmp_char_eq ()));
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
722 }
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
723
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
724 // Ditto for string.
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
725 static bool
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
726 strcmpi_str_op (const std::string& s1, const std::string& s2,
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
727 octave_idx_type)
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
728 {
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
729 return (s1.size () == s2.size ()
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
730 && std::equal (s1.data (), s1.data () + s1.size (), s2.data (),
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
731 icmp_char_eq ()));
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
732 }
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
733
10316
9966f1f71c32 use DEFUNX instead of DEFUN for strcmpi and strncmpi
John W. Eaton <jwe@octave.org>
parents: 10315
diff changeset
734 DEFUNX ("strcmpi", Fstrcmpi, args, ,
10108
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
735 "-*- texinfo -*-\n\
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
736 @deftypefn {Built-in Function} {} strcmpi (@var{s1}, @var{s2})\n\
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
737 Returns 1 if the character strings @var{s1} and @var{s2} are the same,\n\
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
738 disregarding case of alphabetic characters, and 0 otherwise.\n\
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
739 \n\
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
740 If either @var{s1} or @var{s2} is a cell array of strings, then an array\n\
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
741 of the same size is returned, containing the values described above for\n\
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
742 every member of the cell array. The other argument may also be a cell\n\
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
743 array of strings (of the same size or with only one element), char matrix\n\
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
744 or character string.\n\
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
745 \n\
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
746 @strong{Caution:} For compatibility with @sc{matlab}, Octave's strcmp\n\
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
747 function returns 1 if the character strings are equal, and 0 otherwise.\n\
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
748 This is just the opposite of the corresponding C library function.\n\
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
749 \n\
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
750 @strong{Caution:} National alphabets are not supported.\n\
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
751 @seealso{strcmp, strncmp, strncmpi}\n\
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
752 @end deftypefn")
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
753 {
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
754 octave_value retval;
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
755
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
756 if (args.length () == 2)
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
757 {
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
758 retval = do_strcmp_fun (args (0), args (1), 0,
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
759 "strcmpi", strcmpi_array_op, strcmpi_str_op);
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
760 }
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
761 else
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
762 print_usage ();
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
763
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
764 return retval;
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
765 }
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
766
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
767 /*
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
768 %!assert (strcmpi("abc123", "ABC123"), logical(1));
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
769 */
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
770
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
771 // Like strncmp.
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
772 static bool
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
773 strncmpi_array_op (const charNDArray& s1, const charNDArray& s2, octave_idx_type n)
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
774 {
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
775 octave_idx_type l1 = s1.numel (), l2 = s2.numel ();
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
776 return (n > 0 && n <= l1 && n <= l2
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
777 && std::equal (s1.data (), s1.data () + n, s2.data (),
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
778 icmp_char_eq ()));
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
779 }
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
780
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
781 // Ditto.
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
782 static bool
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
783 strncmpi_str_op (const std::string& s1, const std::string& s2, octave_idx_type n)
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
784 {
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
785 octave_idx_type l1 = s1.length (), l2 = s2.length ();
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
786 return (n > 0 && n <= l1 && n <= l2
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
787 && std::equal (s1.data (), s1.data () + n, s2.data (),
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
788 icmp_char_eq ()));
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
789 }
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
790
10318
f0ac2fa91733 fix incorrect strncmpi change
John W. Eaton <jwe@octave.org>
parents: 10316
diff changeset
791 DEFUNX ("strncmpi", Fstrncmpi, args, ,
10108
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
792 "-*- texinfo -*-\n\
10234
c4c2bda71f82 fix typo in strncmpi docstring
Jaroslav Hajek <highegg@gmail.com>
parents: 10160
diff changeset
793 @deftypefn {Built-in Function} {} strncmpi (@var{s1}, @var{s2}, @var{n})\n\
10108
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
794 Returns 1 if the first @var{n} character of @var{s1} and @var{s2} are the same,\n\
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
795 disregarding case of alphabetic characters, and 0 otherwise.\n\
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
796 \n\
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
797 If either @var{s1} or @var{s2} is a cell array of strings, then an array\n\
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
798 of the same size is returned, containing the values described above for\n\
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
799 every member of the cell array. The other argument may also be a cell\n\
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
800 array of strings (of the same size or with only one element), char matrix\n\
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
801 or character string.\n\
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
802 \n\
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
803 @strong{Caution:} For compatibility with @sc{matlab}, Octave's strncmpi\n\
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
804 function returns 1 if the character strings are equal, and 0 otherwise.\n\
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
805 This is just the opposite of the corresponding C library function.\n\
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
806 \n\
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
807 @strong{Caution:} National alphabets are not supported.\n\
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
808 @seealso{strncmp, strcmp, strcmpi}\n\
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
809 @end deftypefn")
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
810 {
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
811 octave_value retval;
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
812
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
813 if (args.length () == 3)
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
814 {
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
815 octave_idx_type n = args(2).idx_type_value ();
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
816
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
817 if (! error_state)
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
818 {
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
819 if (n > 0)
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
820 {
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
821 retval = do_strcmp_fun (args(0), args(1), n, "strncmpi",
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
822 strncmpi_array_op, strncmpi_str_op);
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
823 }
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
824 else
10316
9966f1f71c32 use DEFUNX instead of DEFUN for strcmpi and strncmpi
John W. Eaton <jwe@octave.org>
parents: 10315
diff changeset
825 error ("strncmpi: N must be greater than 0");
10108
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
826 }
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
827 }
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
828 else
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
829 print_usage ();
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
830
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
831 return retval;
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
832 }
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
833
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
834 /*
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
835 %!assert (strncmpi("abc123", "ABC456", 3), logical(1));
06e11df4592d implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents: 9307
diff changeset
836 */
8461
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
837
5690
161ebd1f3410 [project @ 2006-03-17 06:17:24 by jwe]
jwe
parents: 5674
diff changeset
838 DEFUN (list_in_columns, args, ,
161ebd1f3410 [project @ 2006-03-17 06:17:24 by jwe]
jwe
parents: 5674
diff changeset
839 "-*- texinfo -*-\n\
161ebd1f3410 [project @ 2006-03-17 06:17:24 by jwe]
jwe
parents: 5674
diff changeset
840 @deftypefn {Built-in Function} {} list_in_columns (@var{arg}, @var{width})\n\
161ebd1f3410 [project @ 2006-03-17 06:17:24 by jwe]
jwe
parents: 5674
diff changeset
841 Return a string containing the elements of @var{arg} listed in\n\
161ebd1f3410 [project @ 2006-03-17 06:17:24 by jwe]
jwe
parents: 5674
diff changeset
842 columns with an overall maximum width of @var{width}. The argument\n\
161ebd1f3410 [project @ 2006-03-17 06:17:24 by jwe]
jwe
parents: 5674
diff changeset
843 @var{arg} must be a cell array of character strings or a character array.\n\
161ebd1f3410 [project @ 2006-03-17 06:17:24 by jwe]
jwe
parents: 5674
diff changeset
844 If @var{width} is not specified, the width of the terminal screen is used.\n\
8461
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
845 Newline characters are used to break the lines in the output string.\n\
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
846 For example:\n\
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
847 \n\
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
848 @example\n\
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
849 @group\n\
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
850 list_in_columns (@{\"abc\", \"def\", \"ghijkl\", \"mnop\", \"qrs\", \"tuv\"@}, 20)\n\
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
851 @result{} ans = abc mnop\n\
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
852 def qrs\n\
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
853 ghijkl tuv\n\
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
854 \n\
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
855 whos ans\n\
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
856 @result{}\n\
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
857 Variables in the current scope:\n\
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
858 \n\
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
859 Attr Name Size Bytes Class\n\
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
860 ==== ==== ==== ===== =====\n\
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
861 ans 1x37 37 char\n\
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
862 \n\
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
863 Total is 37 elements using 37 bytes\n\
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
864 @end group\n\
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
865 @end example\n\
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
866 \n\
5690
161ebd1f3410 [project @ 2006-03-17 06:17:24 by jwe]
jwe
parents: 5674
diff changeset
867 @seealso{terminal_size}\n\
161ebd1f3410 [project @ 2006-03-17 06:17:24 by jwe]
jwe
parents: 5674
diff changeset
868 @end deftypefn")
161ebd1f3410 [project @ 2006-03-17 06:17:24 by jwe]
jwe
parents: 5674
diff changeset
869 {
161ebd1f3410 [project @ 2006-03-17 06:17:24 by jwe]
jwe
parents: 5674
diff changeset
870 octave_value retval;
161ebd1f3410 [project @ 2006-03-17 06:17:24 by jwe]
jwe
parents: 5674
diff changeset
871
161ebd1f3410 [project @ 2006-03-17 06:17:24 by jwe]
jwe
parents: 5674
diff changeset
872 int nargin = args.length ();
161ebd1f3410 [project @ 2006-03-17 06:17:24 by jwe]
jwe
parents: 5674
diff changeset
873
161ebd1f3410 [project @ 2006-03-17 06:17:24 by jwe]
jwe
parents: 5674
diff changeset
874 if (nargin == 1 || nargin == 2)
161ebd1f3410 [project @ 2006-03-17 06:17:24 by jwe]
jwe
parents: 5674
diff changeset
875 {
161ebd1f3410 [project @ 2006-03-17 06:17:24 by jwe]
jwe
parents: 5674
diff changeset
876 string_vector s = args(0).all_strings ();
161ebd1f3410 [project @ 2006-03-17 06:17:24 by jwe]
jwe
parents: 5674
diff changeset
877
161ebd1f3410 [project @ 2006-03-17 06:17:24 by jwe]
jwe
parents: 5674
diff changeset
878 if (! error_state)
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10234
diff changeset
879 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10234
diff changeset
880 std::ostringstream buf;
5690
161ebd1f3410 [project @ 2006-03-17 06:17:24 by jwe]
jwe
parents: 5674
diff changeset
881
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10234
diff changeset
882 if (nargin == 1)
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10234
diff changeset
883 // Let list_in_columns query terminal width.
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10234
diff changeset
884 s.list_in_columns (buf);
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10234
diff changeset
885 else
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10234
diff changeset
886 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10234
diff changeset
887 int width = args(1).int_value ();
5690
161ebd1f3410 [project @ 2006-03-17 06:17:24 by jwe]
jwe
parents: 5674
diff changeset
888
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10234
diff changeset
889 if (! error_state)
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10234
diff changeset
890 s.list_in_columns (buf, width);
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10234
diff changeset
891 else
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10234
diff changeset
892 error ("list_in_columns: expecting width to be an integer");
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10234
diff changeset
893 }
5690
161ebd1f3410 [project @ 2006-03-17 06:17:24 by jwe]
jwe
parents: 5674
diff changeset
894
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10234
diff changeset
895 retval = buf.str ();
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10234
diff changeset
896 }
5690
161ebd1f3410 [project @ 2006-03-17 06:17:24 by jwe]
jwe
parents: 5674
diff changeset
897 else
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10234
diff changeset
898 error ("list_in_columns: expecting cellstr or char array");
5690
161ebd1f3410 [project @ 2006-03-17 06:17:24 by jwe]
jwe
parents: 5674
diff changeset
899 }
161ebd1f3410 [project @ 2006-03-17 06:17:24 by jwe]
jwe
parents: 5674
diff changeset
900 else
5823
080c08b192d8 [project @ 2006-05-19 05:32:17 by jwe]
jwe
parents: 5765
diff changeset
901 print_usage ();
5690
161ebd1f3410 [project @ 2006-03-17 06:17:24 by jwe]
jwe
parents: 5674
diff changeset
902
161ebd1f3410 [project @ 2006-03-17 06:17:24 by jwe]
jwe
parents: 5674
diff changeset
903 return retval;
161ebd1f3410 [project @ 2006-03-17 06:17:24 by jwe]
jwe
parents: 5674
diff changeset
904 }
161ebd1f3410 [project @ 2006-03-17 06:17:24 by jwe]
jwe
parents: 5674
diff changeset
905
807
c03b179482fc [project @ 1994-10-14 14:21:58 by jwe]
jwe
parents:
diff changeset
906 /*
8461
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
907 %!error <Invalid call to list_in_columns.*> list_in_columns ();
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
908 %!error <Invalid call to list_in_columns.*> list_in_columns (["abc", "def"], 20, 2);
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
909 %!error <invalid conversion from string to real scalar.*> list_in_columns (["abc", "def"], "a");
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
910 %!test
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
911 %! input = {"abc", "def", "ghijkl", "mnop", "qrs", "tuv"};
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
912 %! result = "abc mnop\ndef qrs\nghijkl tuv\n";
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
913 %! assert (list_in_columns (input, 20) == result);
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
914 %!test
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
915 %! input = ["abc"; "def"; "ghijkl"; "mnop"; "qrs"; "tuv"];
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
916 %! result = "abc mnop \ndef qrs \nghijkl tuv \n";
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
917 %! assert (list_in_columns (input, 20) == result);
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
918 */