Mercurial > octave-nkf
annotate libinterp/corefcn/strfns.cc @ 20216:aa36fb998a4d stable
maint: Remove unnecessary whitespace at end of lines.
* libgui/Makefile.am, libgui/graphics/Canvas.cc,
libgui/graphics/EditControl.cc, libgui/graphics/Figure.cc,
libgui/graphics/MouseModeActionGroup.cc, libgui/graphics/annotation-dialog.cc,
libgui/graphics/annotation-dialog.h, libgui/graphics/gl-select.cc,
libgui/graphics/module.mk, libgui/kb-layouts/default.keytab,
libgui/kb-layouts/linux.keytab, libgui/kb-layouts/vt420pc.keytab,
libgui/src/m-editor/find-dialog.cc, libgui/src/main-window.cc,
libgui/src/octave-qt-link.cc, libgui/src/octave-qt-link.h,
libgui/src/shortcut-manager.h, libinterp/corefcn/error.cc,
libinterp/corefcn/find.cc, libinterp/corefcn/gl2ps-renderer.cc,
libinterp/corefcn/graphics.cc, libinterp/corefcn/graphics.in.h,
libinterp/corefcn/octave-link.cc, libinterp/corefcn/quadcc.cc,
libinterp/corefcn/strfns.cc, libinterp/corefcn/syscalls.cc,
libinterp/corefcn/sysdep.cc, libinterp/corefcn/urlwrite.cc,
libinterp/corefcn/utils.cc, libinterp/corefcn/variables.cc,
libinterp/dldfcn/__init_fltk__.cc, libinterp/dldfcn/ccolamd.cc,
libinterp/dldfcn/colamd.cc, libinterp/octave-value/ov-bool-sparse.cc,
libinterp/octave-value/ov-classdef.cc, libinterp/octave-value/ov-re-sparse.cc,
libinterp/octave-value/ov-struct.cc, libinterp/parse-tree/pt-arg-list.cc,
scripts/audio/@audiorecorder/play.m, scripts/audio/wavwrite.m,
scripts/general/cart2sph.m, scripts/geometry/inpolygon.m,
scripts/gui/listdlg.m, scripts/gui/msgbox.m,
scripts/gui/private/message_dialog.m, scripts/help/get_first_help_sentence.m,
scripts/help/lookfor.m, scripts/image/imshow.m, scripts/io/strread.m,
scripts/java/javamem.m, scripts/miscellaneous/dir.m,
scripts/miscellaneous/edit.m, scripts/miscellaneous/genvarname.m,
scripts/miscellaneous/gzip.m, scripts/miscellaneous/private/__w2mpth__.m,
scripts/plot/appearance/annotation.m, scripts/plot/draw/colorbar.m,
scripts/plot/draw/quiver3.m, scripts/plot/util/hold.m,
scripts/plot/util/print.m, scripts/polynomial/mkpp.m,
scripts/polynomial/polyder.m, scripts/polynomial/spline.m,
scripts/polynomial/unmkpp.m, scripts/signal/arma_rnd.m, scripts/sparse/gplot.m,
scripts/statistics/tests/t_test.m,
scripts/statistics/tests/t_test_regression.m, scripts/strings/mat2str.m,
scripts/strings/strsplit.m, scripts/strings/strtrunc.m,
scripts/strings/untabify.m, scripts/testfun/assert.m:
maint: Remove unnecessary whitespace at end of lines.
author | Rik <rik@octave.org> |
---|---|
date | Tue, 12 May 2015 09:22:01 -0700 |
parents | 4f45eaf83908 |
children | 88efded31037 |
rev | line source |
---|---|
807 | 1 /* |
2 | |
19731
4197fc428c7d
maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents:
19437
diff
changeset
|
3 Copyright (C) 1994-2015 John W. Eaton |
807 | 4 |
5 This file is part of Octave. | |
6 | |
7 Octave is free software; you can redistribute it and/or modify it | |
8 under the terms of the GNU General Public License as published by the | |
7016 | 9 Free Software Foundation; either version 3 of the License, or (at your |
10 option) any later version. | |
807 | 11 |
12 Octave is distributed in the hope that it will be useful, but WITHOUT | |
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
15 for more details. | |
16 | |
17 You should have received a copy of the GNU General Public License | |
7016 | 18 along with Octave; see the file COPYING. If not, see |
19 <http://www.gnu.org/licenses/>. | |
807 | 20 |
21 */ | |
22 | |
23 #ifdef HAVE_CONFIG_H | |
1192 | 24 #include <config.h> |
807 | 25 #endif |
26 | |
1355 | 27 #include <cctype> |
7528
26d8a92644de
try to avoid ctype macro problems
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
28 |
8463
a74871446af7
Fchar, Fstrvcat: use queue to avoid duplicate calls to all_strings
John W. Eaton <jwe@octave.org>
parents:
8462
diff
changeset
|
29 #include <queue> |
5765 | 30 #include <sstream> |
807 | 31 |
32 #include "dMatrix.h" | |
33 | |
5416 | 34 #include "Cell.h" |
1355 | 35 #include "defun.h" |
807 | 36 #include "error.h" |
37 #include "gripes.h" | |
2366 | 38 #include "ov.h" |
1355 | 39 #include "oct-obj.h" |
4457 | 40 #include "unwind-prot.h" |
807 | 41 #include "utils.h" |
42 | |
4358 | 43 DEFUN (char, args, , |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
44 "-*- texinfo -*-\n\ |
9307
c2923c27c877
Various documentation improvements
Rik <rdrider0-list@yahoo.com>
parents:
9064
diff
changeset
|
45 @deftypefn {Built-in Function} {} char (@var{x})\n\ |
c2923c27c877
Various documentation improvements
Rik <rdrider0-list@yahoo.com>
parents:
9064
diff
changeset
|
46 @deftypefnx {Built-in Function} {} char (@var{x}, @dots{})\n\ |
c2923c27c877
Various documentation improvements
Rik <rdrider0-list@yahoo.com>
parents:
9064
diff
changeset
|
47 @deftypefnx {Built-in Function} {} char (@var{s1}, @var{s2}, @dots{})\n\ |
4358 | 48 @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
|
49 Create a string array from one or more numeric matrices, character\n\ |
20207
4f45eaf83908
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19895
diff
changeset
|
50 matrices, or cell arrays.\n\ |
9307
c2923c27c877
Various documentation improvements
Rik <rdrider0-list@yahoo.com>
parents:
9064
diff
changeset
|
51 \n\ |
20207
4f45eaf83908
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19895
diff
changeset
|
52 Arguments are concatenated vertically. The returned values are padded with\n\ |
20216
aa36fb998a4d
maint: Remove unnecessary whitespace at end of lines.
Rik <rik@octave.org>
parents:
20207
diff
changeset
|
53 blanks as needed to make each row of the string array have the same length.\n\ |
20207
4f45eaf83908
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19895
diff
changeset
|
54 Empty input strings are significant and will concatenated in the output.\n\ |
4f45eaf83908
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19895
diff
changeset
|
55 \n\ |
4f45eaf83908
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19895
diff
changeset
|
56 For numerical input, each element is converted to the corresponding ASCII\n\ |
4f45eaf83908
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19895
diff
changeset
|
57 character. A range error results if an input is outside the ASCII range\n\ |
4f45eaf83908
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19895
diff
changeset
|
58 (0-255).\n\ |
9307
c2923c27c877
Various documentation improvements
Rik <rdrider0-list@yahoo.com>
parents:
9064
diff
changeset
|
59 \n\ |
10846
a4f482e66b65
Grammarcheck more of the documentation.
Rik <octave@nomad.inbox5.com>
parents:
10840
diff
changeset
|
60 For cell arrays, each element is concatenated separately. Cell arrays\n\ |
20207
4f45eaf83908
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19895
diff
changeset
|
61 converted through @code{char} can mostly be converted back with\n\ |
4f45eaf83908
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19895
diff
changeset
|
62 @code{cellstr}. For example:\n\ |
4358 | 63 \n\ |
64 @example\n\ | |
65 @group\n\ | |
9307
c2923c27c877
Various documentation improvements
Rik <rdrider0-list@yahoo.com>
parents:
9064
diff
changeset
|
66 char ([97, 98, 99], \"\", @{\"98\", \"99\", 100@}, \"str1\", [\"ha\", \"lf\"])\n\ |
14360
97883071e8e4
doc: Correct off-by-1 spacings in all .cc docstrings
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
67 @result{} [\"abc \"\n\ |
97883071e8e4
doc: Correct off-by-1 spacings in all .cc docstrings
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
68 \" \"\n\ |
97883071e8e4
doc: Correct off-by-1 spacings in all .cc docstrings
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
69 \"98 \"\n\ |
97883071e8e4
doc: Correct off-by-1 spacings in all .cc docstrings
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
70 \"99 \"\n\ |
97883071e8e4
doc: Correct off-by-1 spacings in all .cc docstrings
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
71 \"d \"\n\ |
97883071e8e4
doc: Correct off-by-1 spacings in all .cc docstrings
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
72 \"str1 \"\n\ |
97883071e8e4
doc: Correct off-by-1 spacings in all .cc docstrings
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
73 \"half \"]\n\ |
4358 | 74 @end group\n\ |
75 @end example\n\ | |
9307
c2923c27c877
Various documentation improvements
Rik <rdrider0-list@yahoo.com>
parents:
9064
diff
changeset
|
76 @seealso{strvcat, cellstr}\n\ |
4358 | 77 @end deftypefn") |
78 { | |
79 octave_value retval; | |
80 | |
81 int nargin = args.length (); | |
82 | |
13135
af36cdb552a6
char: return empty string if called with no arguments
John W. Eaton <jwe@octave.org>
parents:
12900
diff
changeset
|
83 if (nargin == 0) |
af36cdb552a6
char: return empty string if called with no arguments
John W. Eaton <jwe@octave.org>
parents:
12900
diff
changeset
|
84 retval = ""; |
af36cdb552a6
char: return empty string if called with no arguments
John W. Eaton <jwe@octave.org>
parents:
12900
diff
changeset
|
85 else if (nargin == 1) |
5281 | 86 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
|
87 args(0).is_dq_string () ? '"' : '\''); |
13135
af36cdb552a6
char: return empty string if called with no arguments
John W. Eaton <jwe@octave.org>
parents:
12900
diff
changeset
|
88 else |
4358 | 89 { |
90 int n_elts = 0; | |
91 | |
92 int max_len = 0; | |
93 | |
8463
a74871446af7
Fchar, Fstrvcat: use queue to avoid duplicate calls to all_strings
John W. Eaton <jwe@octave.org>
parents:
8462
diff
changeset
|
94 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
|
95 |
4358 | 96 for (int i = 0; i < nargin; i++) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10234
diff
changeset
|
97 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10234
diff
changeset
|
98 string_vector s = args(i).all_strings (); |
4358 | 99 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10234
diff
changeset
|
100 if (error_state) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10234
diff
changeset
|
101 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10234
diff
changeset
|
102 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
|
103 return retval; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10234
diff
changeset
|
104 } |
4358 | 105 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10234
diff
changeset
|
106 if (s.length () > 0) |
8353
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
107 n_elts += s.length (); |
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
108 else |
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
109 n_elts += 1; |
4358 | 110 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10234
diff
changeset
|
111 int s_max_len = s.max_length (); |
4358 | 112 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10234
diff
changeset
|
113 if (s_max_len > max_len) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10234
diff
changeset
|
114 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
|
115 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10234
diff
changeset
|
116 args_as_strings.push (s); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10234
diff
changeset
|
117 } |
4358 | 118 |
119 string_vector result (n_elts); | |
120 | |
121 int k = 0; | |
122 | |
123 for (int i = 0; i < nargin; i++) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10234
diff
changeset
|
124 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10234
diff
changeset
|
125 string_vector s = args_as_strings.front (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10234
diff
changeset
|
126 args_as_strings.pop (); |
4358 | 127 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10234
diff
changeset
|
128 int n = s.length (); |
4358 | 129 |
8353
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
130 if (n > 0) |
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
131 { |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10234
diff
changeset
|
132 for (int j = 0; j < n; j++) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10234
diff
changeset
|
133 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10234
diff
changeset
|
134 std::string t = s[j]; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10234
diff
changeset
|
135 int t_len = t.length (); |
4358 | 136 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10234
diff
changeset
|
137 if (max_len > t_len) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10234
diff
changeset
|
138 t += std::string (max_len - t_len, ' '); |
4358 | 139 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10234
diff
changeset
|
140 result[k++] = t; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10234
diff
changeset
|
141 } |
8353
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
142 } |
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
143 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10234
diff
changeset
|
144 result[k++] = std::string (max_len, ' '); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10234
diff
changeset
|
145 } |
4358 | 146 |
5280 | 147 retval = octave_value (result, '\''); |
4358 | 148 } |
149 | |
150 return retval; | |
151 } | |
152 | |
8353
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
153 /* |
13135
af36cdb552a6
char: return empty string if called with no arguments
John W. Eaton <jwe@octave.org>
parents:
12900
diff
changeset
|
154 %!assert (char (), ''); |
14429
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
155 %!assert (char (100), "d"); |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
156 %!assert (char (100,100), ["d";"d"]) |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
157 %!assert (char ({100,100}), ["d";"d"]) |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
158 %!assert (char ([100,100]), ["dd"]) |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
159 %!assert (char ({100,{100}}), ["d";"d"]) |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
160 %!assert (char (100, [], 100), ["d";" ";"d"]) |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
161 %!assert (char ({100, [], 100}), ["d";" ";"d"]) |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
162 %!assert (char ({100,{100, {""}}}), ["d";"d";" "]) |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
163 %!assert (char (["a";"be"], {"c", 100}), ["a";"be";"c";"d"]) |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
164 %!assert (char ("a", "bb", "ccc"), ["a "; "bb "; "ccc"]) |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
165 %!assert (char ([65, 83, 67, 73, 73]), "ASCII") |
13140
98d23b0f16e1
maint: move test_string.m tests to source files
John W. Eaton <jwe@octave.org>
parents:
13135
diff
changeset
|
166 |
98d23b0f16e1
maint: move test_string.m tests to source files
John W. Eaton <jwe@octave.org>
parents:
13135
diff
changeset
|
167 %!test |
98d23b0f16e1
maint: move test_string.m tests to source files
John W. Eaton <jwe@octave.org>
parents:
13135
diff
changeset
|
168 %! x = char ("foo", "bar", "foobar"); |
14429
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
169 %! assert (x(1,:), "foo "); |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
170 %! assert (x(2,:), "bar "); |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
171 %! assert (x(3,:), "foobar"); |
8353
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
172 */ |
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
173 |
8462
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
174 DEFUN (strvcat, args, , |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
175 "-*- texinfo -*-\n\ |
9307
c2923c27c877
Various documentation improvements
Rik <rdrider0-list@yahoo.com>
parents:
9064
diff
changeset
|
176 @deftypefn {Built-in Function} {} strvcat (@var{x})\n\ |
c2923c27c877
Various documentation improvements
Rik <rdrider0-list@yahoo.com>
parents:
9064
diff
changeset
|
177 @deftypefnx {Built-in Function} {} strvcat (@var{x}, @dots{})\n\ |
c2923c27c877
Various documentation improvements
Rik <rdrider0-list@yahoo.com>
parents:
9064
diff
changeset
|
178 @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
|
179 @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
|
180 Create a character array from one or more numeric matrices, character\n\ |
20207
4f45eaf83908
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19895
diff
changeset
|
181 matrices, or cell arrays.\n\ |
9307
c2923c27c877
Various documentation improvements
Rik <rdrider0-list@yahoo.com>
parents:
9064
diff
changeset
|
182 \n\ |
20207
4f45eaf83908
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19895
diff
changeset
|
183 Arguments are concatenated vertically. The returned values are padded with\n\ |
20216
aa36fb998a4d
maint: Remove unnecessary whitespace at end of lines.
Rik <rik@octave.org>
parents:
20207
diff
changeset
|
184 blanks as needed to make each row of the string array have the same length.\n\ |
20207
4f45eaf83908
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19895
diff
changeset
|
185 Unlike @code{char}, empty strings are removed and will not appear in the\n\ |
4f45eaf83908
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19895
diff
changeset
|
186 output.\n\ |
4f45eaf83908
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19895
diff
changeset
|
187 \n\ |
4f45eaf83908
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19895
diff
changeset
|
188 For numerical input, each element is converted to the corresponding ASCII\n\ |
4f45eaf83908
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19895
diff
changeset
|
189 character. A range error results if an input is outside the ASCII range\n\ |
4f45eaf83908
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19895
diff
changeset
|
190 (0-255).\n\ |
9307
c2923c27c877
Various documentation improvements
Rik <rdrider0-list@yahoo.com>
parents:
9064
diff
changeset
|
191 \n\ |
10846
a4f482e66b65
Grammarcheck more of the documentation.
Rik <octave@nomad.inbox5.com>
parents:
10840
diff
changeset
|
192 For cell arrays, each element is concatenated separately. Cell arrays\n\ |
20207
4f45eaf83908
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19895
diff
changeset
|
193 converted through @code{strvcat} can mostly be converted back with\n\ |
4f45eaf83908
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19895
diff
changeset
|
194 @code{cellstr}. For example:\n\ |
8462
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
195 \n\ |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
196 @example\n\ |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
197 @group\n\ |
9307
c2923c27c877
Various documentation improvements
Rik <rdrider0-list@yahoo.com>
parents:
9064
diff
changeset
|
198 strvcat ([97, 98, 99], \"\", @{\"98\", \"99\", 100@}, \"str1\", [\"ha\", \"lf\"])\n\ |
14360
97883071e8e4
doc: Correct off-by-1 spacings in all .cc docstrings
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
199 @result{} [\"abc \"\n\ |
97883071e8e4
doc: Correct off-by-1 spacings in all .cc docstrings
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
200 \"98 \"\n\ |
97883071e8e4
doc: Correct off-by-1 spacings in all .cc docstrings
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
201 \"99 \"\n\ |
97883071e8e4
doc: Correct off-by-1 spacings in all .cc docstrings
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
202 \"d \"\n\ |
97883071e8e4
doc: Correct off-by-1 spacings in all .cc docstrings
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
203 \"str1 \"\n\ |
97883071e8e4
doc: Correct off-by-1 spacings in all .cc docstrings
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
204 \"half \"]\n\ |
8462
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
205 @end group\n\ |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
206 @end example\n\ |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
207 @seealso{char, strcat, cstrcat}\n\ |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
208 @end deftypefn") |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
209 { |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
210 octave_value retval; |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
211 |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
212 int nargin = args.length (); |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
213 |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
214 if (nargin > 0) |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
215 { |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
216 int n_elts = 0; |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
217 |
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
|
218 size_t max_len = 0; |
8462
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
219 |
8463
a74871446af7
Fchar, Fstrvcat: use queue to avoid duplicate calls to all_strings
John W. Eaton <jwe@octave.org>
parents:
8462
diff
changeset
|
220 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
|
221 |
8462
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
222 for (int i = 0; i < nargin; i++) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10234
diff
changeset
|
223 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10234
diff
changeset
|
224 string_vector s = args(i).all_strings (); |
8462
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
225 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10234
diff
changeset
|
226 if (error_state) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10234
diff
changeset
|
227 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10234
diff
changeset
|
228 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
|
229 return retval; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10234
diff
changeset
|
230 } |
8462
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
231 |
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
|
232 size_t n = s.length (); |
8462
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
233 |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
234 // 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
|
235 if (n > 0) |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
236 { |
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
|
237 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
|
238 { |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
239 if (s[j].length () > 0) |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
240 n_elts++; |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
241 } |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
242 } |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
243 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10234
diff
changeset
|
244 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
|
245 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10234
diff
changeset
|
246 if (s_max_len > max_len) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10234
diff
changeset
|
247 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
|
248 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10234
diff
changeset
|
249 args_as_strings.push (s); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10234
diff
changeset
|
250 } |
8462
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
251 |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
252 string_vector result (n_elts); |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
253 |
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
|
254 octave_idx_type k = 0; |
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 for (int i = 0; i < nargin; i++) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10234
diff
changeset
|
257 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10234
diff
changeset
|
258 string_vector s = args_as_strings.front (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10234
diff
changeset
|
259 args_as_strings.pop (); |
8462
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
260 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10234
diff
changeset
|
261 size_t n = s.length (); |
8462
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
262 |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
263 if (n > 0) |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
264 { |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10234
diff
changeset
|
265 for (size_t j = 0; j < n; j++) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10234
diff
changeset
|
266 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10234
diff
changeset
|
267 std::string t = s[j]; |
8462
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
268 if (t.length () > 0) |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
269 { |
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
|
270 size_t t_len = t.length (); |
8462
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
271 |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
272 if (max_len > t_len) |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
273 t += std::string (max_len - t_len, ' '); |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
274 |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
275 result[k++] = t; |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
276 } |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10234
diff
changeset
|
277 } |
8462
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
278 } |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10234
diff
changeset
|
279 } |
8462
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 retval = octave_value (result, '\''); |
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 else |
12900
00f5292aefeb
Backout 12897:503865c2e542
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
12897
diff
changeset
|
284 print_usage (); |
8462
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
285 |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
286 return retval; |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
287 } |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
288 |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
289 /* |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
290 %!assert (strvcat (""), ""); |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
291 %!assert (strvcat (100) == "d"); |
14429
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
292 %!assert (strvcat (100,100), ["d";"d"]) |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
293 %!assert (strvcat ({100,100}), ["d";"d"]) |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
294 %!assert (strvcat ([100,100]), ["dd"]) |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
295 %!assert (strvcat ({100,{100}}), ["d";"d"]) |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
296 %!assert (strvcat (100, [], 100), ["d";"d"]) |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
297 %!assert (strvcat ({100, [], 100}), ["d";"d"]) |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
298 %!assert (strvcat ({100,{100, {""}}}), ["d";"d"]) |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
299 %!assert (strvcat (["a";"be"], {"c", 100}), ["a";"be";"c";"d"]) |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
300 %!assert (strvcat ("a", "bb", "ccc"), ["a "; "bb "; "ccc"]) |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
301 |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
302 %!error strvcat () |
8462
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
303 */ |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
304 |
8353
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
305 |
4535 | 306 DEFUN (ischar, args, , |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
307 "-*- texinfo -*-\n\ |
11431
0d9640d755b1
Improve docstrings for all isXXX functions.
Rik <octave@nomad.inbox5.com>
parents:
10846
diff
changeset
|
308 @deftypefn {Built-in Function} {} ischar (@var{x})\n\ |
0d9640d755b1
Improve docstrings for all isXXX functions.
Rik <octave@nomad.inbox5.com>
parents:
10846
diff
changeset
|
309 Return true if @var{x} is a character array.\n\ |
0d9640d755b1
Improve docstrings for all isXXX functions.
Rik <octave@nomad.inbox5.com>
parents:
10846
diff
changeset
|
310 @seealso{isfloat, isinteger, islogical, isnumeric, iscellstr, isa}\n\ |
3361 | 311 @end deftypefn") |
807 | 312 { |
4233 | 313 octave_value retval; |
807 | 314 |
315 int nargin = args.length (); | |
316 | |
317 if (nargin == 1 && args(0).is_defined ()) | |
4233 | 318 retval = args(0).is_string (); |
807 | 319 else |
5823 | 320 print_usage (); |
807 | 321 |
322 return retval; | |
323 } | |
324 | |
8461
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
325 /* |
14429
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
326 %!assert (ischar ("a"), true) |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
327 %!assert (ischar (["ab";"cd"]), true) |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
328 %!assert (ischar ({"ab"}), false) |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
329 %!assert (ischar (1), false) |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
330 %!assert (ischar ([1, 2]), false) |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
331 %!assert (ischar ([]), false) |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
332 %!assert (ischar ([1, 2; 3, 4]), false) |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
333 %!assert (ischar (""), true) |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
334 %!assert (ischar ("test"), true) |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
335 %!assert (ischar (["test"; "ing"]), true) |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
336 %!assert (ischar (struct ("foo", "bar")), false) |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
337 |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
338 %!error ischar () |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
339 %!error ischar ("test", 1) |
13140
98d23b0f16e1
maint: move test_string.m tests to source files
John W. Eaton <jwe@octave.org>
parents:
13135
diff
changeset
|
340 */ |
8461
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
341 |
10108
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
342 static octave_value |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
343 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
|
344 octave_idx_type n, const char *fcn_name, |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
345 bool (*array_op) (const charNDArray&, const charNDArray&, |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
346 octave_idx_type), |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
347 bool (*str_op) (const std::string&, const std::string&, |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
348 octave_idx_type)) |
10108
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
349 |
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 octave_value retval; |
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 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
|
354 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
|
355 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
|
356 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
|
357 |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
358 if (s1_string && s2_string) |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
359 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
|
360 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
|
361 { |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
362 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
|
363 |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
364 if (s1_string) |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
365 { |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
366 str_val = arg0; |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
367 cell_val = arg1; |
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 else |
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 str_val = arg1; |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
372 cell_val = arg0; |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
373 } |
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 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
|
376 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
|
377 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
|
378 |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
379 if (r == 0 || r == 1) |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
380 { |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
381 // Broadcast the string. |
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 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
|
384 |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
385 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
|
386 |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
387 if (cell_val.is_cellstr ()) |
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 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
|
390 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
|
391 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
|
392 } |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
393 else |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
394 { |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
395 // FIXME: should we warn here? |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
396 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
|
397 { |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
398 if (cell(i).is_string ()) |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
399 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
|
400 } |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
401 } |
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 retval = output; |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
404 } |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
405 else if (r > 1) |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
406 { |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
407 if (cell.length () == 1) |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
408 { |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
409 // Broadcast the cell. |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
410 |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
411 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
|
412 boolNDArray output (dv, false); |
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(0).is_string ()) |
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 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
|
417 |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
418 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
|
419 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
|
420 } |
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 retval = output; |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
423 } |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
424 else |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
425 { |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
426 // Must match in all dimensions. |
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 boolNDArray output (cell.dims (), false); |
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 if (cell.length () == r) |
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 if (cell_val.is_cellstr ()) |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
433 { |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
434 const Array<std::string> cellstr |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
435 = cell_val.cellstr_value (); |
10108
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
436 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
|
437 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
|
438 } |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
439 else |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
440 { |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
441 // FIXME: should we warn here? |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
442 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
|
443 { |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
444 if (cell(i).is_string ()) |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
445 output(i) = str_op (str[i], |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
446 cell(i).string_value (), n); |
10108
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
447 } |
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 |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
450 retval = output; |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
451 } |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
452 else |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
453 retval = false; |
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 } |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
456 } |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
457 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
|
458 { |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
459 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
|
460 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
|
461 |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
462 if (r1 == 1) |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
463 { |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
464 // Make the singleton cell2. |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
465 |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
466 cell1_val = arg1; |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
467 cell2_val = arg0; |
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 else |
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 cell1_val = arg0; |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
472 cell2_val = arg1; |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
473 } |
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 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
|
476 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
|
477 r1 = cell1.numel (); |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
478 r2 = cell2.numel (); |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
479 |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
480 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
|
481 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
|
482 |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
483 boolNDArray output (size1, false); |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
484 |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
485 if (r2 == 1) |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
486 { |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
487 // Broadcast cell2. |
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 if (cell2(0).is_string ()) |
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 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
|
492 |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
493 if (cell1_val.is_cellstr ()) |
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 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
|
496 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
|
497 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
|
498 } |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
499 else |
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 // FIXME: should we warn here? |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
502 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
|
503 { |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
504 if (cell1(i).is_string ()) |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
505 { |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
506 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
|
507 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
|
508 } |
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 } |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
511 } |
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 else |
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 if (size1 != size2) |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
516 { |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
517 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
|
518 return retval; |
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 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
|
522 { |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
523 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
|
524 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
|
525 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
|
526 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
|
527 } |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
528 else |
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 // FIXME: should we warn here? |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
531 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
|
532 { |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
533 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
|
534 { |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
535 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
|
536 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
|
537 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
|
538 } |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
539 } |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
540 } |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
541 } |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
542 |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
543 retval = output; |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
544 } |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
545 else |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
546 retval = false; |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
547 |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
548 return retval; |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
549 } |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
550 |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
551 // If both args are arrays, dimensions may be significant. |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11572
diff
changeset
|
552 static bool |
10108
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
553 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
|
554 { |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
555 return (s1.dims () == s2.dims () |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
556 && 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
|
557 } |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
558 |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11572
diff
changeset
|
559 // Otherwise, just use strings. |
10108
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
560 static bool |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
561 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
|
562 octave_idx_type) |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
563 { |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
564 return s1 == s2; |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
565 } |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
566 |
5415 | 567 DEFUN (strcmp, args, , |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
568 "-*- texinfo -*-\n\ |
6678 | 569 @deftypefn {Built-in Function} {} strcmp (@var{s1}, @var{s2})\n\ |
5415 | 570 Return 1 if the character strings @var{s1} and @var{s2} are the same,\n\ |
571 and 0 otherwise.\n\ | |
5674 | 572 \n\ |
573 If either @var{s1} or @var{s2} is a cell array of strings, then an array\n\ | |
574 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
|
575 every member of the cell array. The other argument may also be a cell\n\ |
5674 | 576 array of strings (of the same size or with only one element), char matrix\n\ |
577 or character string.\n\ | |
578 \n\ | |
9064
7c02ec148a3c
Check grammar on all .cc files
Rik <rdrider0-list@yahoo.com>
parents:
9036
diff
changeset
|
579 @strong{Caution:} For compatibility with @sc{matlab}, Octave's strcmp\n\ |
5674 | 580 function returns 1 if the character strings are equal, and 0 otherwise.\n\ |
581 This is just the opposite of the corresponding C library function.\n\ | |
582 @seealso{strcmpi, strncmp, strncmpi}\n\ | |
5415 | 583 @end deftypefn") |
584 { | |
5531 | 585 octave_value retval; |
5415 | 586 |
5416 | 587 if (args.length () == 2) |
588 { | |
18111
b560bac0fca2
maint: Don't use space between 'args' and '(' when doing indexing.
Rik <rik@octave.org>
parents:
18099
diff
changeset
|
589 retval = do_strcmp_fun (args(0), args(1), 0, |
10108
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
590 "strcmp", strcmp_array_op, strcmp_str_op); |
5415 | 591 } |
5416 | 592 else |
5823 | 593 print_usage (); |
5415 | 594 |
595 return retval; | |
596 } | |
597 | |
5862 | 598 /* |
599 %!shared x | |
14429
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
600 %! x = char (zeros (0, 2)); |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
601 %!assert (strcmp ("", x), false) |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
602 %!assert (strcmp (x, ""), false) |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
603 %!assert (strcmp (x, x), true) |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
604 ## %!assert (strcmp ({""}, x), true) |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
605 ## %!assert (strcmp ({x}, ""), false) |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
606 ## %!assert (strcmp ({x}, x), true) |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
607 ## %!assert (strcmp ("", {x}), false) |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
608 ## %!assert (strcmp (x, {""}), false) |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
609 ## %!assert (strcmp (x, {x}), true) |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
610 ## %!assert (strcmp ({x; x}, ""), [false; false]) |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
611 ## %!assert (strcmp ({x; x}, {""}), [false; false]) |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
612 ## %!assert (strcmp ("", {x; x}), [false; false]) |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
613 ## %!assert (strcmp ({""}, {x; x}), [false; false]) |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
614 %!assert (strcmp ({"foo"}, x), false) |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
615 %!assert (strcmp ({"foo"}, "foo"), true) |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
616 %!assert (strcmp ({"foo"}, x), false) |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
617 %!assert (strcmp (x, {"foo"}), false) |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
618 %!assert (strcmp ("foo", {"foo"}), true) |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
619 %!assert (strcmp (x, {"foo"}), false) |
5862 | 620 %!shared y |
14429
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
621 %! y = char (zeros (2, 0)); |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
622 %!assert (strcmp ("", y), false) |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
623 %!assert (strcmp (y, ""), false) |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
624 %!assert (strcmp (y, y), true) |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
625 %!assert (strcmp ({""}, y), [true; true]) |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
626 %!assert (strcmp ({y}, ""), true) |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
627 %!assert (strcmp ({y}, y), [true; true]) |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
628 %!assert (strcmp ("", {y}), true) |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
629 %!assert (strcmp (y, {""}), [true; true]) |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
630 %!assert (strcmp (y, {y}), [true; true]) |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
631 %!assert (strcmp ({y; y}, ""), [true; true]) |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
632 %!assert (strcmp ({y; y}, {""}), [true; true]) |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
633 %!assert (strcmp ("", {y; y}), [true; true]) |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
634 %!assert (strcmp ({""}, {y; y}), [true; true]) |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
635 %!assert (strcmp ({"foo"}, y), [false; false]) |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
636 %!assert (strcmp ({"foo"}, y), [false; false]) |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
637 %!assert (strcmp (y, {"foo"}), [false; false]) |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
638 %!assert (strcmp (y, {"foo"}), [false; false]) |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
639 %!assert (strcmp ("foobar", "foobar"), true) |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
640 %!assert (strcmp ("fooba", "foobar"), false) |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
641 |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
642 %!error strcmp () |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
643 %!error strcmp ("foo", "bar", 3) |
5862 | 644 */ |
645 | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11572
diff
changeset
|
646 // Apparently, Matlab ignores the dims with strncmp. It also |
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11572
diff
changeset
|
647 static bool |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
648 strncmp_array_op (const charNDArray& s1, const charNDArray& s2, |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
649 octave_idx_type n) |
10108
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
650 { |
18099
6a71e5030df5
Follow coding convention of defining and initializing only 1 variable per line in liboctinterp.
Rik <rik@octave.org>
parents:
17787
diff
changeset
|
651 octave_idx_type l1 = s1.numel (); |
6a71e5030df5
Follow coding convention of defining and initializing only 1 variable per line in liboctinterp.
Rik <rik@octave.org>
parents:
17787
diff
changeset
|
652 octave_idx_type l2 = s2.numel (); |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11572
diff
changeset
|
653 return (n > 0 && n <= l1 && n <= l2 |
10108
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
654 && 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
|
655 } |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
656 |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
657 // 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
|
658 // 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
|
659 // 3-way compare). |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
660 static bool |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
661 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
|
662 { |
18099
6a71e5030df5
Follow coding convention of defining and initializing only 1 variable per line in liboctinterp.
Rik <rik@octave.org>
parents:
17787
diff
changeset
|
663 octave_idx_type l1 = s1.length (); |
6a71e5030df5
Follow coding convention of defining and initializing only 1 variable per line in liboctinterp.
Rik <rik@octave.org>
parents:
17787
diff
changeset
|
664 octave_idx_type l2 = s2.length (); |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11572
diff
changeset
|
665 return (n > 0 && n <= l1 && n <= l2 |
10108
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
666 && 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
|
667 } |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
668 |
6250 | 669 DEFUN (strncmp, args, , |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
670 "-*- texinfo -*-\n\ |
6678 | 671 @deftypefn {Built-in Function} {} strncmp (@var{s1}, @var{s2}, @var{n})\n\ |
10840 | 672 Return 1 if the first @var{n} characters of strings @var{s1} and @var{s2} are\n\ |
673 the same, and 0 otherwise.\n\ | |
6250 | 674 \n\ |
675 @example\n\ | |
676 @group\n\ | |
677 strncmp (\"abce\", \"abcd\", 3)\n\ | |
14360
97883071e8e4
doc: Correct off-by-1 spacings in all .cc docstrings
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
678 @result{} 1\n\ |
6250 | 679 @end group\n\ |
680 @end example\n\ | |
681 \n\ | |
682 If either @var{s1} or @var{s2} is a cell array of strings, then an array\n\ | |
683 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
|
684 every member of the cell array. The other argument may also be a cell\n\ |
6250 | 685 array of strings (of the same size or with only one element), char matrix\n\ |
686 or character string.\n\ | |
687 \n\ | |
688 @example\n\ | |
689 @group\n\ | |
6256 | 690 strncmp (\"abce\", @{\"abcd\", \"bca\", \"abc\"@}, 3)\n\ |
6250 | 691 @result{} [1, 0, 1]\n\ |
692 @end group\n\ | |
693 @end example\n\ | |
694 \n\ | |
9064
7c02ec148a3c
Check grammar on all .cc files
Rik <rdrider0-list@yahoo.com>
parents:
9036
diff
changeset
|
695 @strong{Caution:} For compatibility with @sc{matlab}, Octave's strncmp\n\ |
6250 | 696 function returns 1 if the character strings are equal, and 0 otherwise.\n\ |
697 This is just the opposite of the corresponding C library function.\n\ | |
698 @seealso{strncmpi, strcmp, strcmpi}\n\ | |
699 @end deftypefn") | |
700 { | |
701 octave_value retval; | |
702 | |
703 if (args.length () == 3) | |
704 { | |
10108
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
705 octave_idx_type n = args(2).idx_type_value (); |
6250 | 706 |
10108
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
707 if (! error_state) |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
708 { |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
709 if (n > 0) |
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 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
|
712 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
|
713 } |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
714 else |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
715 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
|
716 } |
6250 | 717 } |
718 else | |
719 print_usage (); | |
720 | |
721 return retval; | |
722 } | |
723 | |
8461
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
724 /* |
14429
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
725 %!assert (strncmp ("abce", "abc", 3), true) |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
726 %!assert (strncmp (100, 100, 1), false) |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
727 %!assert (strncmp ("abce", {"abcd", "bca", "abc"}, 3), logical ([1, 0, 1])) |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
728 %!assert (strncmp ("abc", {"abcd", "bca", "abc"}, 4), logical ([0, 0, 0])) |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
729 %!assert (strncmp ({"abcd", "bca", "abc"},"abce", 3), logical ([1, 0, 1])) |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
730 %!assert (strncmp ({"abcd", "bca", "abc"},{"abcd", "bca", "abe"}, 3), logical ([1, 1, 0])) |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
731 %!assert (strncmp ("abc", {"abcd", 10}, 2), logical ([1, 0])) |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
732 |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
733 %!error strncmp () |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
734 %!error strncmp ("abc", "def") |
8461
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
735 */ |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
736 |
10108
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
737 // case-insensitive character equality functor |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
738 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
|
739 { |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
740 bool operator () (char x, char y) const |
19895
19755f4fc851
maint: Cleanup C++ code to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19731
diff
changeset
|
741 { return std::toupper (x) == std::toupper (y); } |
10108
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
742 }; |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
743 |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
744 // strcmpi is equivalent to strcmp in that it checks all dims. |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11572
diff
changeset
|
745 static bool |
10108
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
746 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
|
747 { |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
748 return (s1.dims () == s2.dims () |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
749 && 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
|
750 icmp_char_eq ())); |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
751 } |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
752 |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
753 // Ditto for string. |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
754 static bool |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
755 strcmpi_str_op (const std::string& s1, const std::string& s2, |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
756 octave_idx_type) |
10108
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 return (s1.size () == s2.size () |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
759 && 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
|
760 icmp_char_eq ())); |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
761 } |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
762 |
10316
9966f1f71c32
use DEFUNX instead of DEFUN for strcmpi and strncmpi
John W. Eaton <jwe@octave.org>
parents:
10315
diff
changeset
|
763 DEFUNX ("strcmpi", Fstrcmpi, args, , |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
764 "-*- texinfo -*-\n\ |
10108
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
765 @deftypefn {Built-in Function} {} strcmpi (@var{s1}, @var{s2})\n\ |
11572
7d6d8c1e471f
Grammarcheck Texinfo for files in src directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
766 Return 1 if the character strings @var{s1} and @var{s2} are the same,\n\ |
10108
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
767 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
|
768 \n\ |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
769 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
|
770 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
|
771 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
|
772 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
|
773 or character string.\n\ |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
774 \n\ |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
775 @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
|
776 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
|
777 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
|
778 \n\ |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
779 @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
|
780 @seealso{strcmp, strncmp, strncmpi}\n\ |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
781 @end deftypefn") |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
782 { |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
783 octave_value retval; |
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 if (args.length () == 2) |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
786 { |
18111
b560bac0fca2
maint: Don't use space between 'args' and '(' when doing indexing.
Rik <rik@octave.org>
parents:
18099
diff
changeset
|
787 retval = do_strcmp_fun (args(0), args(1), 0, |
10108
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
788 "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
|
789 } |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
790 else |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
791 print_usage (); |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
792 |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
793 return retval; |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
794 } |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
795 |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
796 /* |
14429
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
797 %!assert (strcmpi ("abc123", "ABC123"), true) |
10108
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
798 */ |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
799 |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
800 // Like strncmp. |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11572
diff
changeset
|
801 static bool |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
802 strncmpi_array_op (const charNDArray& s1, const charNDArray& s2, |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
803 octave_idx_type n) |
10108
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
804 { |
18099
6a71e5030df5
Follow coding convention of defining and initializing only 1 variable per line in liboctinterp.
Rik <rik@octave.org>
parents:
17787
diff
changeset
|
805 octave_idx_type l1 = s1.numel (); |
6a71e5030df5
Follow coding convention of defining and initializing only 1 variable per line in liboctinterp.
Rik <rik@octave.org>
parents:
17787
diff
changeset
|
806 octave_idx_type l2 = s2.numel (); |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11572
diff
changeset
|
807 return (n > 0 && n <= l1 && n <= l2 |
10108
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
808 && 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
|
809 icmp_char_eq ())); |
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 |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
812 // Ditto. |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
813 static bool |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
814 strncmpi_str_op (const std::string& s1, const std::string& s2, |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
815 octave_idx_type n) |
10108
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
816 { |
18099
6a71e5030df5
Follow coding convention of defining and initializing only 1 variable per line in liboctinterp.
Rik <rik@octave.org>
parents:
17787
diff
changeset
|
817 octave_idx_type l1 = s1.length (); |
6a71e5030df5
Follow coding convention of defining and initializing only 1 variable per line in liboctinterp.
Rik <rik@octave.org>
parents:
17787
diff
changeset
|
818 octave_idx_type l2 = s2.length (); |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11572
diff
changeset
|
819 return (n > 0 && n <= l1 && n <= l2 |
10108
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
820 && 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
|
821 icmp_char_eq ())); |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
822 } |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
823 |
10318
f0ac2fa91733
fix incorrect strncmpi change
John W. Eaton <jwe@octave.org>
parents:
10316
diff
changeset
|
824 DEFUNX ("strncmpi", Fstrncmpi, args, , |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
825 "-*- texinfo -*-\n\ |
10234
c4c2bda71f82
fix typo in strncmpi docstring
Jaroslav Hajek <highegg@gmail.com>
parents:
10160
diff
changeset
|
826 @deftypefn {Built-in Function} {} strncmpi (@var{s1}, @var{s2}, @var{n})\n\ |
11572
7d6d8c1e471f
Grammarcheck Texinfo for files in src directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
827 Return 1 if the first @var{n} character of @var{s1} and @var{s2} are the\n\ |
10846
a4f482e66b65
Grammarcheck more of the documentation.
Rik <octave@nomad.inbox5.com>
parents:
10840
diff
changeset
|
828 same, disregarding case of alphabetic characters, and 0 otherwise.\n\ |
10108
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
829 \n\ |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
830 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
|
831 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
|
832 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
|
833 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
|
834 or character string.\n\ |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
835 \n\ |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
836 @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
|
837 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
|
838 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
|
839 \n\ |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
840 @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
|
841 @seealso{strncmp, strcmp, strcmpi}\n\ |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
842 @end deftypefn") |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
843 { |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
844 octave_value retval; |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
845 |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
846 if (args.length () == 3) |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
847 { |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
848 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
|
849 |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
850 if (! error_state) |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
851 { |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
852 if (n > 0) |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
853 { |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
854 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
|
855 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
|
856 } |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
857 else |
10316
9966f1f71c32
use DEFUNX instead of DEFUN for strcmpi and strncmpi
John W. Eaton <jwe@octave.org>
parents:
10315
diff
changeset
|
858 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
|
859 } |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
860 } |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
861 else |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
862 print_usage (); |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
863 |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
864 return retval; |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
865 } |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
866 |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
867 /* |
14429
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
868 %!assert (strncmpi ("abc123", "ABC456", 3), true) |
10108
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
869 */ |
8461
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
870 |
5690 | 871 DEFUN (list_in_columns, args, , |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
872 "-*- texinfo -*-\n\ |
14841
afa5e1d20671
* str-vec.h, str-vec.cc (string_vector::list_in_columns): Accept prefix.
John W. Eaton <jwe@octave.org>
parents:
14429
diff
changeset
|
873 @deftypefn {Built-in Function} {} list_in_columns (@var{arg}, @var{width}, @var{prefix})\n\ |
20207
4f45eaf83908
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19895
diff
changeset
|
874 Return a string containing the elements of @var{arg} listed in columns with\n\ |
4f45eaf83908
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19895
diff
changeset
|
875 an overall maximum width of @var{width} and optional prefix @var{prefix}.\n\ |
4f45eaf83908
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19895
diff
changeset
|
876 \n\ |
4f45eaf83908
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19895
diff
changeset
|
877 The argument @var{arg} must be a cell array of character strings or a\n\ |
4f45eaf83908
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19895
diff
changeset
|
878 character array.\n\ |
4f45eaf83908
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19895
diff
changeset
|
879 \n\ |
4f45eaf83908
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19895
diff
changeset
|
880 If @var{width} is not specified or is an empty matrix, or less than or equal\n\ |
4f45eaf83908
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19895
diff
changeset
|
881 to zero, the width of the terminal screen is used. Newline characters are\n\ |
4f45eaf83908
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19895
diff
changeset
|
882 used to break the lines in the output string. For example:\n\ |
14093
050bc580cb60
doc: Various docstring improvements before 3.6.0 release.
Rik <octave@nomad.inbox5.com>
parents:
13915
diff
changeset
|
883 @c Set example in small font to prevent overfull line\n\ |
8461
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
884 \n\ |
14093
050bc580cb60
doc: Various docstring improvements before 3.6.0 release.
Rik <octave@nomad.inbox5.com>
parents:
13915
diff
changeset
|
885 @smallexample\n\ |
8461
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
886 @group\n\ |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
887 list_in_columns (@{\"abc\", \"def\", \"ghijkl\", \"mnop\", \"qrs\", \"tuv\"@}, 20)\n\ |
14360
97883071e8e4
doc: Correct off-by-1 spacings in all .cc docstrings
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
888 @result{} abc mnop\n\ |
97883071e8e4
doc: Correct off-by-1 spacings in all .cc docstrings
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
889 def qrs\n\ |
97883071e8e4
doc: Correct off-by-1 spacings in all .cc docstrings
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
890 ghijkl tuv\n\ |
8461
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
891 \n\ |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
892 whos ans\n\ |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
893 @result{}\n\ |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
894 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
|
895 \n\ |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
896 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
|
897 ==== ==== ==== ===== =====\n\ |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
898 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
|
899 \n\ |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
900 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
|
901 @end group\n\ |
14093
050bc580cb60
doc: Various docstring improvements before 3.6.0 release.
Rik <octave@nomad.inbox5.com>
parents:
13915
diff
changeset
|
902 @end smallexample\n\ |
8461
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
903 \n\ |
5690 | 904 @seealso{terminal_size}\n\ |
905 @end deftypefn") | |
906 { | |
907 octave_value retval; | |
908 | |
909 int nargin = args.length (); | |
910 | |
14841
afa5e1d20671
* str-vec.h, str-vec.cc (string_vector::list_in_columns): Accept prefix.
John W. Eaton <jwe@octave.org>
parents:
14429
diff
changeset
|
911 if (nargin < 1 || nargin > 3) |
5690 | 912 { |
14841
afa5e1d20671
* str-vec.h, str-vec.cc (string_vector::list_in_columns): Accept prefix.
John W. Eaton <jwe@octave.org>
parents:
14429
diff
changeset
|
913 print_usage (); |
afa5e1d20671
* str-vec.h, str-vec.cc (string_vector::list_in_columns): Accept prefix.
John W. Eaton <jwe@octave.org>
parents:
14429
diff
changeset
|
914 return retval; |
afa5e1d20671
* str-vec.h, str-vec.cc (string_vector::list_in_columns): Accept prefix.
John W. Eaton <jwe@octave.org>
parents:
14429
diff
changeset
|
915 } |
afa5e1d20671
* str-vec.h, str-vec.cc (string_vector::list_in_columns): Accept prefix.
John W. Eaton <jwe@octave.org>
parents:
14429
diff
changeset
|
916 |
afa5e1d20671
* str-vec.h, str-vec.cc (string_vector::list_in_columns): Accept prefix.
John W. Eaton <jwe@octave.org>
parents:
14429
diff
changeset
|
917 string_vector s = args(0).all_strings (); |
5690 | 918 |
14841
afa5e1d20671
* str-vec.h, str-vec.cc (string_vector::list_in_columns): Accept prefix.
John W. Eaton <jwe@octave.org>
parents:
14429
diff
changeset
|
919 if (error_state) |
afa5e1d20671
* str-vec.h, str-vec.cc (string_vector::list_in_columns): Accept prefix.
John W. Eaton <jwe@octave.org>
parents:
14429
diff
changeset
|
920 { |
19437
03067dab10ca
Use stricter input validation when looking for a string as input (bug #42651).
Rik <rik@octave.org>
parents:
18111
diff
changeset
|
921 error ("list_in_columns: ARG must be a cellstr or char array"); |
14841
afa5e1d20671
* str-vec.h, str-vec.cc (string_vector::list_in_columns): Accept prefix.
John W. Eaton <jwe@octave.org>
parents:
14429
diff
changeset
|
922 return retval; |
afa5e1d20671
* str-vec.h, str-vec.cc (string_vector::list_in_columns): Accept prefix.
John W. Eaton <jwe@octave.org>
parents:
14429
diff
changeset
|
923 } |
afa5e1d20671
* str-vec.h, str-vec.cc (string_vector::list_in_columns): Accept prefix.
John W. Eaton <jwe@octave.org>
parents:
14429
diff
changeset
|
924 |
afa5e1d20671
* str-vec.h, str-vec.cc (string_vector::list_in_columns): Accept prefix.
John W. Eaton <jwe@octave.org>
parents:
14429
diff
changeset
|
925 int width = -1; |
afa5e1d20671
* str-vec.h, str-vec.cc (string_vector::list_in_columns): Accept prefix.
John W. Eaton <jwe@octave.org>
parents:
14429
diff
changeset
|
926 |
afa5e1d20671
* str-vec.h, str-vec.cc (string_vector::list_in_columns): Accept prefix.
John W. Eaton <jwe@octave.org>
parents:
14429
diff
changeset
|
927 if (nargin > 1 && ! args(1).is_empty ()) |
afa5e1d20671
* str-vec.h, str-vec.cc (string_vector::list_in_columns): Accept prefix.
John W. Eaton <jwe@octave.org>
parents:
14429
diff
changeset
|
928 { |
afa5e1d20671
* str-vec.h, str-vec.cc (string_vector::list_in_columns): Accept prefix.
John W. Eaton <jwe@octave.org>
parents:
14429
diff
changeset
|
929 width = args(1).int_value (); |
5690 | 930 |
14841
afa5e1d20671
* str-vec.h, str-vec.cc (string_vector::list_in_columns): Accept prefix.
John W. Eaton <jwe@octave.org>
parents:
14429
diff
changeset
|
931 if (error_state) |
afa5e1d20671
* str-vec.h, str-vec.cc (string_vector::list_in_columns): Accept prefix.
John W. Eaton <jwe@octave.org>
parents:
14429
diff
changeset
|
932 { |
afa5e1d20671
* str-vec.h, str-vec.cc (string_vector::list_in_columns): Accept prefix.
John W. Eaton <jwe@octave.org>
parents:
14429
diff
changeset
|
933 error ("list_in_columns: WIDTH must be an integer"); |
afa5e1d20671
* str-vec.h, str-vec.cc (string_vector::list_in_columns): Accept prefix.
John W. Eaton <jwe@octave.org>
parents:
14429
diff
changeset
|
934 return retval; |
afa5e1d20671
* str-vec.h, str-vec.cc (string_vector::list_in_columns): Accept prefix.
John W. Eaton <jwe@octave.org>
parents:
14429
diff
changeset
|
935 } |
afa5e1d20671
* str-vec.h, str-vec.cc (string_vector::list_in_columns): Accept prefix.
John W. Eaton <jwe@octave.org>
parents:
14429
diff
changeset
|
936 } |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
937 |
14841
afa5e1d20671
* str-vec.h, str-vec.cc (string_vector::list_in_columns): Accept prefix.
John W. Eaton <jwe@octave.org>
parents:
14429
diff
changeset
|
938 std::string prefix; |
5690 | 939 |
14841
afa5e1d20671
* str-vec.h, str-vec.cc (string_vector::list_in_columns): Accept prefix.
John W. Eaton <jwe@octave.org>
parents:
14429
diff
changeset
|
940 if (nargin > 2) |
afa5e1d20671
* str-vec.h, str-vec.cc (string_vector::list_in_columns): Accept prefix.
John W. Eaton <jwe@octave.org>
parents:
14429
diff
changeset
|
941 { |
afa5e1d20671
* str-vec.h, str-vec.cc (string_vector::list_in_columns): Accept prefix.
John W. Eaton <jwe@octave.org>
parents:
14429
diff
changeset
|
942 if (args(2).is_string ()) |
19437
03067dab10ca
Use stricter input validation when looking for a string as input (bug #42651).
Rik <rik@octave.org>
parents:
18111
diff
changeset
|
943 prefix = args(2).string_value (); |
5690 | 944 else |
14841
afa5e1d20671
* str-vec.h, str-vec.cc (string_vector::list_in_columns): Accept prefix.
John W. Eaton <jwe@octave.org>
parents:
14429
diff
changeset
|
945 { |
19437
03067dab10ca
Use stricter input validation when looking for a string as input (bug #42651).
Rik <rik@octave.org>
parents:
18111
diff
changeset
|
946 error ("list_in_columns: PREFIX must be a string"); |
14841
afa5e1d20671
* str-vec.h, str-vec.cc (string_vector::list_in_columns): Accept prefix.
John W. Eaton <jwe@octave.org>
parents:
14429
diff
changeset
|
947 return retval; |
afa5e1d20671
* str-vec.h, str-vec.cc (string_vector::list_in_columns): Accept prefix.
John W. Eaton <jwe@octave.org>
parents:
14429
diff
changeset
|
948 } |
5690 | 949 } |
14841
afa5e1d20671
* str-vec.h, str-vec.cc (string_vector::list_in_columns): Accept prefix.
John W. Eaton <jwe@octave.org>
parents:
14429
diff
changeset
|
950 |
afa5e1d20671
* str-vec.h, str-vec.cc (string_vector::list_in_columns): Accept prefix.
John W. Eaton <jwe@octave.org>
parents:
14429
diff
changeset
|
951 std::ostringstream buf; |
afa5e1d20671
* str-vec.h, str-vec.cc (string_vector::list_in_columns): Accept prefix.
John W. Eaton <jwe@octave.org>
parents:
14429
diff
changeset
|
952 |
afa5e1d20671
* str-vec.h, str-vec.cc (string_vector::list_in_columns): Accept prefix.
John W. Eaton <jwe@octave.org>
parents:
14429
diff
changeset
|
953 s.list_in_columns (buf, width, prefix); |
afa5e1d20671
* str-vec.h, str-vec.cc (string_vector::list_in_columns): Accept prefix.
John W. Eaton <jwe@octave.org>
parents:
14429
diff
changeset
|
954 |
afa5e1d20671
* str-vec.h, str-vec.cc (string_vector::list_in_columns): Accept prefix.
John W. Eaton <jwe@octave.org>
parents:
14429
diff
changeset
|
955 retval = buf.str (); |
5690 | 956 |
957 return retval; | |
958 } | |
959 | |
807 | 960 /* |
14429
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
961 %!test |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
962 %! input = {"abc", "def", "ghijkl", "mnop", "qrs", "tuv"}; |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
963 %! result = "abc mnop\ndef qrs\nghijkl tuv\n"; |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
964 %! assert (list_in_columns (input, 20), result); |
8461
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
965 %!test |
14429
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
966 %! input = ["abc"; "def"; "ghijkl"; "mnop"; "qrs"; "tuv"]; |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
967 %! result = "abc mnop \ndef qrs \nghijkl tuv \n"; |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
968 %! assert (list_in_columns (input, 20), result); |
14841
afa5e1d20671
* str-vec.h, str-vec.cc (string_vector::list_in_columns): Accept prefix.
John W. Eaton <jwe@octave.org>
parents:
14429
diff
changeset
|
969 %!test |
afa5e1d20671
* str-vec.h, str-vec.cc (string_vector::list_in_columns): Accept prefix.
John W. Eaton <jwe@octave.org>
parents:
14429
diff
changeset
|
970 %! input = ["abc"; "def"; "ghijkl"; "mnop"; "qrs"; "tuv"]; |
afa5e1d20671
* str-vec.h, str-vec.cc (string_vector::list_in_columns): Accept prefix.
John W. Eaton <jwe@octave.org>
parents:
14429
diff
changeset
|
971 %! result = " abc mnop \n def qrs \n ghijkl tuv \n"; |
afa5e1d20671
* str-vec.h, str-vec.cc (string_vector::list_in_columns): Accept prefix.
John W. Eaton <jwe@octave.org>
parents:
14429
diff
changeset
|
972 %! assert (list_in_columns (input, 20, " "), result); |
14429
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
973 |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
974 %!error list_in_columns () |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
975 %!error list_in_columns (["abc", "def"], 20, 2) |
14841
afa5e1d20671
* str-vec.h, str-vec.cc (string_vector::list_in_columns): Accept prefix.
John W. Eaton <jwe@octave.org>
parents:
14429
diff
changeset
|
976 %!error list_in_columns (["abc", "def"], 20, " ", 3) |
14429
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
977 %!error <invalid conversion from string to real scalar> list_in_columns (["abc", "def"], "a") |
8461
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
978 */ |