Mercurial > octave
annotate libinterp/corefcn/strfns.cc @ 25412:922a93fc73ec
Add function to index characters in UTF-8 encoded strings.
* strfns.cc (Funicode_idx): Add new function.
* unistr-wrappers.(cc/h): Add wrapper for "u8_strmblen".
* strings.txi: UTF-8 as expected encoding. Add doc string for "unicode_idx".
author | Markus Mützel <markus.muetzel@gmx.de> |
---|---|
date | Sun, 27 May 2018 19:20:47 +0200 |
parents | 6652d3823428 |
children | 996d78102a71 |
rev | line source |
---|---|
807 | 1 /* |
2 | |
25054
6652d3823428
maint: Update copyright dates in all source files.
John W. Eaton <jwe@octave.org>
parents:
24534
diff
changeset
|
3 Copyright (C) 1994-2018 John W. Eaton |
807 | 4 |
5 This file is part of Octave. | |
6 | |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
24474
diff
changeset
|
7 Octave is free software: you can redistribute it and/or modify it |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22407
diff
changeset
|
8 under the terms of the GNU General Public License as published by |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
24474
diff
changeset
|
9 the Free Software Foundation, either version 3 of the License, or |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22407
diff
changeset
|
10 (at your option) any later version. |
807 | 11 |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22407
diff
changeset
|
12 Octave is distributed in the hope that it will be useful, but |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22407
diff
changeset
|
13 WITHOUT ANY WARRANTY; without even the implied warranty of |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22407
diff
changeset
|
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22407
diff
changeset
|
15 GNU General Public License for more details. |
807 | 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 |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
24474
diff
changeset
|
19 <https://www.gnu.org/licenses/>. |
807 | 20 |
21 */ | |
22 | |
21724
aba2e6293dd8
use "#if ..." consistently instead of "#ifdef" and "#ifndef"
John W. Eaton <jwe@octave.org>
parents:
21547
diff
changeset
|
23 #if defined (HAVE_CONFIG_H) |
21301
40de9f8f23a6
Use '#include "config.h"' rather than <config.h>.
Rik <rik@octave.org>
parents:
21200
diff
changeset
|
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" | |
23123
c6ca5fe1505c
use wrappers for uniconv functions; style fixes for unicode conv fcns
John W. Eaton <jwe@octave.org>
parents:
23122
diff
changeset
|
33 #include "localcharset-wrapper.h" |
c6ca5fe1505c
use wrappers for uniconv functions; style fixes for unicode conv fcns
John W. Eaton <jwe@octave.org>
parents:
23122
diff
changeset
|
34 #include "uniconv-wrappers.h" |
25412
922a93fc73ec
Add function to index characters in UTF-8 encoded strings.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25054
diff
changeset
|
35 #include "unistr-wrappers.h" |
807 | 36 |
5416 | 37 #include "Cell.h" |
1355 | 38 #include "defun.h" |
807 | 39 #include "error.h" |
21100
e39e05d90788
Switch gripe_XXX to either err_XXX or warn_XXX naming scheme.
Rik <rik@octave.org>
parents:
21078
diff
changeset
|
40 #include "errwarn.h" |
2366 | 41 #include "ov.h" |
20940
48b2ad5ee801
maint: Rename oct-obj.[cc|h] to ovl.[cc|h] for clarity.
Rik <rik@octave.org>
parents:
20939
diff
changeset
|
42 #include "ovl.h" |
4457 | 43 #include "unwind-prot.h" |
807 | 44 #include "utils.h" |
45 | |
22280
26109cce022e
New string utility functions to replace caseless_str for liboctave (bug #48726)
Carnë Draug <carandraug@octave.org>
parents:
22022
diff
changeset
|
46 #include "oct-string.h" |
26109cce022e
New string utility functions to replace caseless_str for liboctave (bug #48726)
Carnë Draug <carandraug@octave.org>
parents:
22022
diff
changeset
|
47 |
4358 | 48 DEFUN (char, args, , |
21966
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
49 doc: /* -*- texinfo -*- |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
50 @deftypefn {} {} char (@var{x}) |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
51 @deftypefnx {} {} char (@var{x}, @dots{}) |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
52 @deftypefnx {} {} char (@var{s1}, @var{s2}, @dots{}) |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
53 @deftypefnx {} {} char (@var{cell_array}) |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
54 Create a string array from one or more numeric matrices, character |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
55 matrices, or cell arrays. |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
56 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
57 Arguments are concatenated vertically. The returned values are padded with |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
58 blanks as needed to make each row of the string array have the same length. |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
59 Empty input strings are significant and will concatenated in the output. |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
60 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
61 For numerical input, each element is converted to the corresponding ASCII |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
62 character. A range error results if an input is outside the ASCII range |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
63 (0-255). |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
64 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
65 For cell arrays, each element is concatenated separately. Cell arrays |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
66 converted through @code{char} can mostly be converted back with |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
67 @code{cellstr}. For example: |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
68 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
69 @example |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
70 @group |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
71 char ([97, 98, 99], "", @{"98", "99", 100@}, "str1", ["ha", "lf"]) |
24439
09c81c841b6f
Fix typo in cset 8354b505ad6b (bug #52702).
Rik <rik@octave.org>
parents:
24436
diff
changeset
|
72 @result{} ["abc " |
09c81c841b6f
Fix typo in cset 8354b505ad6b (bug #52702).
Rik <rik@octave.org>
parents:
24436
diff
changeset
|
73 " " |
09c81c841b6f
Fix typo in cset 8354b505ad6b (bug #52702).
Rik <rik@octave.org>
parents:
24436
diff
changeset
|
74 "98 " |
09c81c841b6f
Fix typo in cset 8354b505ad6b (bug #52702).
Rik <rik@octave.org>
parents:
24436
diff
changeset
|
75 "99 " |
09c81c841b6f
Fix typo in cset 8354b505ad6b (bug #52702).
Rik <rik@octave.org>
parents:
24436
diff
changeset
|
76 "d " |
09c81c841b6f
Fix typo in cset 8354b505ad6b (bug #52702).
Rik <rik@octave.org>
parents:
24436
diff
changeset
|
77 "str1" |
09c81c841b6f
Fix typo in cset 8354b505ad6b (bug #52702).
Rik <rik@octave.org>
parents:
24436
diff
changeset
|
78 "half"] |
21966
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
79 @end group |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
80 @end example |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
81 @seealso{strvcat, cellstr} |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
82 @end deftypefn */) |
4358 | 83 { |
84 octave_value retval; | |
85 | |
86 int nargin = args.length (); | |
87 | |
13135
af36cdb552a6
char: return empty string if called with no arguments
John W. Eaton <jwe@octave.org>
parents:
12900
diff
changeset
|
88 if (nargin == 0) |
af36cdb552a6
char: return empty string if called with no arguments
John W. Eaton <jwe@octave.org>
parents:
12900
diff
changeset
|
89 retval = ""; |
af36cdb552a6
char: return empty string if called with no arguments
John W. Eaton <jwe@octave.org>
parents:
12900
diff
changeset
|
90 else if (nargin == 1) |
5281 | 91 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
|
92 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
|
93 else |
4358 | 94 { |
95 int n_elts = 0; | |
96 | |
97 int max_len = 0; | |
98 | |
8463
a74871446af7
Fchar, Fstrvcat: use queue to avoid duplicate calls to all_strings
John W. Eaton <jwe@octave.org>
parents:
8462
diff
changeset
|
99 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
|
100 |
4358 | 101 for (int i = 0; i < nargin; i++) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10234
diff
changeset
|
102 { |
20990
fc9cca99b2de
Deprecate all_strings, replace with string_vector_value.
Rik <rik@octave.org>
parents:
20940
diff
changeset
|
103 string_vector s = args(i).xstring_vector_value ("char: unable to convert some args to strings"); |
4358 | 104 |
20232
a9574e3c6e9e
Deprecate Array::length() and Sparse::length() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
20193
diff
changeset
|
105 if (s.numel () > 0) |
a9574e3c6e9e
Deprecate Array::length() and Sparse::length() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
20193
diff
changeset
|
106 n_elts += s.numel (); |
8353
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
107 else |
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
108 n_elts += 1; |
4358 | 109 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10234
diff
changeset
|
110 int s_max_len = s.max_length (); |
4358 | 111 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10234
diff
changeset
|
112 if (s_max_len > max_len) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10234
diff
changeset
|
113 max_len = s_max_len; |
8463
a74871446af7
Fchar, Fstrvcat: use queue to avoid duplicate calls to all_strings
John W. Eaton <jwe@octave.org>
parents:
8462
diff
changeset
|
114 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10234
diff
changeset
|
115 args_as_strings.push (s); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10234
diff
changeset
|
116 } |
4358 | 117 |
118 string_vector result (n_elts); | |
119 | |
120 int k = 0; | |
121 | |
122 for (int i = 0; i < nargin; i++) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10234
diff
changeset
|
123 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10234
diff
changeset
|
124 string_vector s = args_as_strings.front (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10234
diff
changeset
|
125 args_as_strings.pop (); |
4358 | 126 |
20232
a9574e3c6e9e
Deprecate Array::length() and Sparse::length() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
20193
diff
changeset
|
127 int n = s.numel (); |
4358 | 128 |
8353
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
129 if (n > 0) |
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
130 { |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10234
diff
changeset
|
131 for (int j = 0; j < n; j++) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10234
diff
changeset
|
132 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10234
diff
changeset
|
133 std::string t = s[j]; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10234
diff
changeset
|
134 int t_len = t.length (); |
4358 | 135 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10234
diff
changeset
|
136 if (max_len > t_len) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10234
diff
changeset
|
137 t += std::string (max_len - t_len, ' '); |
4358 | 138 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10234
diff
changeset
|
139 result[k++] = t; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10234
diff
changeset
|
140 } |
8353
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
141 } |
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
142 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10234
diff
changeset
|
143 result[k++] = std::string (max_len, ' '); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10234
diff
changeset
|
144 } |
4358 | 145 |
5280 | 146 retval = octave_value (result, '\''); |
4358 | 147 } |
148 | |
149 return retval; | |
150 } | |
151 | |
8353
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
152 /* |
21317
a4faec57f4c8
maint: remove semicolon after %!assert tests to follow Octave conventions.
Rik <rik@octave.org>
parents:
21301
diff
changeset
|
153 %!assert (char (), '') |
a4faec57f4c8
maint: remove semicolon after %!assert tests to follow Octave conventions.
Rik <rik@octave.org>
parents:
21301
diff
changeset
|
154 %!assert (char (100), "d") |
14429
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
155 %!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
|
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]), ["dd"]) |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
158 %!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
|
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";" "]) |
24461
0a034d34a686
Modify BIST tests to pass when string_fill_char is not ' '.
Rik <rik@octave.org>
parents:
24441
diff
changeset
|
162 %!assert (char (["a ";"be"], {"c", 100}), ["a ";"be";"c ";"d "]) |
14429
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
163 %!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
|
164 %!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
|
165 |
98d23b0f16e1
maint: move test_string.m tests to source files
John W. Eaton <jwe@octave.org>
parents:
13135
diff
changeset
|
166 %!test |
98d23b0f16e1
maint: move test_string.m tests to source files
John W. Eaton <jwe@octave.org>
parents:
13135
diff
changeset
|
167 %! 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
|
168 %! assert (x(1,:), "foo "); |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
169 %! assert (x(2,:), "bar "); |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
170 %! assert (x(3,:), "foobar"); |
8353
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
171 */ |
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
172 |
8462
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
173 DEFUN (strvcat, args, , |
21966
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
174 doc: /* -*- texinfo -*- |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
175 @deftypefn {} {} strvcat (@var{x}) |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
176 @deftypefnx {} {} strvcat (@var{x}, @dots{}) |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
177 @deftypefnx {} {} strvcat (@var{s1}, @var{s2}, @dots{}) |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
178 @deftypefnx {} {} strvcat (@var{cell_array}) |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
179 Create a character array from one or more numeric matrices, character |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
180 matrices, or cell arrays. |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
181 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
182 Arguments are concatenated vertically. The returned values are padded with |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
183 blanks as needed to make each row of the string array have the same length. |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
184 Unlike @code{char}, empty strings are removed and will not appear in the |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
185 output. |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
186 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
187 For numerical input, each element is converted to the corresponding ASCII |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
188 character. A range error results if an input is outside the ASCII range |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
189 (0-255). |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
190 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
191 For cell arrays, each element is concatenated separately. Cell arrays |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
192 converted through @code{strvcat} can mostly be converted back with |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
193 @code{cellstr}. For example: |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
194 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
195 @example |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
196 @group |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
197 strvcat ([97, 98, 99], "", @{"98", "99", 100@}, "str1", ["ha", "lf"]) |
24436
8354b505ad6b
doc: Fix inconsistencies with char, strvcat, strread docstrings (bug #52702.
Rik <rik@octave.org>
parents:
23731
diff
changeset
|
198 @result{} ["abc " |
8354b505ad6b
doc: Fix inconsistencies with char, strvcat, strread docstrings (bug #52702.
Rik <rik@octave.org>
parents:
23731
diff
changeset
|
199 "98 " |
8354b505ad6b
doc: Fix inconsistencies with char, strvcat, strread docstrings (bug #52702.
Rik <rik@octave.org>
parents:
23731
diff
changeset
|
200 "99 " |
8354b505ad6b
doc: Fix inconsistencies with char, strvcat, strread docstrings (bug #52702.
Rik <rik@octave.org>
parents:
23731
diff
changeset
|
201 "d " |
8354b505ad6b
doc: Fix inconsistencies with char, strvcat, strread docstrings (bug #52702.
Rik <rik@octave.org>
parents:
23731
diff
changeset
|
202 "str1" |
8354b505ad6b
doc: Fix inconsistencies with char, strvcat, strread docstrings (bug #52702.
Rik <rik@octave.org>
parents:
23731
diff
changeset
|
203 "half"] |
21966
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
204 @end group |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
205 @end example |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
206 @seealso{char, strcat, cstrcat} |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
207 @end deftypefn */) |
8462
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
208 { |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
209 int nargin = args.length (); |
20166
196871335aa8
Allow call with empty argument list in strcat related functions (bug #44981)
Carnë Draug <carandraug@octave.org>
parents:
19861
diff
changeset
|
210 int n_elts = 0; |
196871335aa8
Allow call with empty argument list in strcat related functions (bug #44981)
Carnë Draug <carandraug@octave.org>
parents:
19861
diff
changeset
|
211 size_t max_len = 0; |
196871335aa8
Allow call with empty argument list in strcat related functions (bug #44981)
Carnë Draug <carandraug@octave.org>
parents:
19861
diff
changeset
|
212 std::queue<string_vector> args_as_strings; |
8463
a74871446af7
Fchar, Fstrvcat: use queue to avoid duplicate calls to all_strings
John W. Eaton <jwe@octave.org>
parents:
8462
diff
changeset
|
213 |
20166
196871335aa8
Allow call with empty argument list in strcat related functions (bug #44981)
Carnë Draug <carandraug@octave.org>
parents:
19861
diff
changeset
|
214 for (int i = 0; i < nargin; i++) |
196871335aa8
Allow call with empty argument list in strcat related functions (bug #44981)
Carnë Draug <carandraug@octave.org>
parents:
19861
diff
changeset
|
215 { |
20990
fc9cca99b2de
Deprecate all_strings, replace with string_vector_value.
Rik <rik@octave.org>
parents:
20940
diff
changeset
|
216 string_vector s = args(i).xstring_vector_value ("strvcat: unable to convert some args to strings"); |
8462
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
217 |
20232
a9574e3c6e9e
Deprecate Array::length() and Sparse::length() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
20193
diff
changeset
|
218 size_t n = s.numel (); |
20166
196871335aa8
Allow call with empty argument list in strcat related functions (bug #44981)
Carnë Draug <carandraug@octave.org>
parents:
19861
diff
changeset
|
219 |
196871335aa8
Allow call with empty argument list in strcat related functions (bug #44981)
Carnë Draug <carandraug@octave.org>
parents:
19861
diff
changeset
|
220 // do not count empty strings in calculation of number of elements |
196871335aa8
Allow call with empty argument list in strcat related functions (bug #44981)
Carnë Draug <carandraug@octave.org>
parents:
19861
diff
changeset
|
221 if (n > 0) |
196871335aa8
Allow call with empty argument list in strcat related functions (bug #44981)
Carnë Draug <carandraug@octave.org>
parents:
19861
diff
changeset
|
222 { |
196871335aa8
Allow call with empty argument list in strcat related functions (bug #44981)
Carnë Draug <carandraug@octave.org>
parents:
19861
diff
changeset
|
223 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
|
224 { |
23812
057a894914df
Use C++11 string fcns back() and pop_back() to simplify code.
Rik <rik@octave.org>
parents:
23732
diff
changeset
|
225 if (! s[j].empty ()) |
20166
196871335aa8
Allow call with empty argument list in strcat related functions (bug #44981)
Carnë Draug <carandraug@octave.org>
parents:
19861
diff
changeset
|
226 n_elts++; |
8462
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
227 } |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10234
diff
changeset
|
228 } |
8462
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
229 |
20166
196871335aa8
Allow call with empty argument list in strcat related functions (bug #44981)
Carnë Draug <carandraug@octave.org>
parents:
19861
diff
changeset
|
230 size_t s_max_len = s.max_length (); |
196871335aa8
Allow call with empty argument list in strcat related functions (bug #44981)
Carnë Draug <carandraug@octave.org>
parents:
19861
diff
changeset
|
231 |
196871335aa8
Allow call with empty argument list in strcat related functions (bug #44981)
Carnë Draug <carandraug@octave.org>
parents:
19861
diff
changeset
|
232 if (s_max_len > max_len) |
196871335aa8
Allow call with empty argument list in strcat related functions (bug #44981)
Carnë Draug <carandraug@octave.org>
parents:
19861
diff
changeset
|
233 max_len = s_max_len; |
196871335aa8
Allow call with empty argument list in strcat related functions (bug #44981)
Carnë Draug <carandraug@octave.org>
parents:
19861
diff
changeset
|
234 |
196871335aa8
Allow call with empty argument list in strcat related functions (bug #44981)
Carnë Draug <carandraug@octave.org>
parents:
19861
diff
changeset
|
235 args_as_strings.push (s); |
196871335aa8
Allow call with empty argument list in strcat related functions (bug #44981)
Carnë Draug <carandraug@octave.org>
parents:
19861
diff
changeset
|
236 } |
196871335aa8
Allow call with empty argument list in strcat related functions (bug #44981)
Carnë Draug <carandraug@octave.org>
parents:
19861
diff
changeset
|
237 |
196871335aa8
Allow call with empty argument list in strcat related functions (bug #44981)
Carnë Draug <carandraug@octave.org>
parents:
19861
diff
changeset
|
238 string_vector result (n_elts); |
8462
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
239 |
20166
196871335aa8
Allow call with empty argument list in strcat related functions (bug #44981)
Carnë Draug <carandraug@octave.org>
parents:
19861
diff
changeset
|
240 octave_idx_type k = 0; |
8462
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
241 |
20166
196871335aa8
Allow call with empty argument list in strcat related functions (bug #44981)
Carnë Draug <carandraug@octave.org>
parents:
19861
diff
changeset
|
242 for (int i = 0; i < nargin; i++) |
196871335aa8
Allow call with empty argument list in strcat related functions (bug #44981)
Carnë Draug <carandraug@octave.org>
parents:
19861
diff
changeset
|
243 { |
196871335aa8
Allow call with empty argument list in strcat related functions (bug #44981)
Carnë Draug <carandraug@octave.org>
parents:
19861
diff
changeset
|
244 string_vector s = args_as_strings.front (); |
196871335aa8
Allow call with empty argument list in strcat related functions (bug #44981)
Carnë Draug <carandraug@octave.org>
parents:
19861
diff
changeset
|
245 args_as_strings.pop (); |
196871335aa8
Allow call with empty argument list in strcat related functions (bug #44981)
Carnë Draug <carandraug@octave.org>
parents:
19861
diff
changeset
|
246 |
20232
a9574e3c6e9e
Deprecate Array::length() and Sparse::length() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
20193
diff
changeset
|
247 size_t n = s.numel (); |
20166
196871335aa8
Allow call with empty argument list in strcat related functions (bug #44981)
Carnë Draug <carandraug@octave.org>
parents:
19861
diff
changeset
|
248 |
196871335aa8
Allow call with empty argument list in strcat related functions (bug #44981)
Carnë Draug <carandraug@octave.org>
parents:
19861
diff
changeset
|
249 if (n > 0) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10234
diff
changeset
|
250 { |
20166
196871335aa8
Allow call with empty argument list in strcat related functions (bug #44981)
Carnë Draug <carandraug@octave.org>
parents:
19861
diff
changeset
|
251 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
|
252 { |
20166
196871335aa8
Allow call with empty argument list in strcat related functions (bug #44981)
Carnë Draug <carandraug@octave.org>
parents:
19861
diff
changeset
|
253 std::string t = s[j]; |
196871335aa8
Allow call with empty argument list in strcat related functions (bug #44981)
Carnë Draug <carandraug@octave.org>
parents:
19861
diff
changeset
|
254 if (t.length () > 0) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10234
diff
changeset
|
255 { |
20166
196871335aa8
Allow call with empty argument list in strcat related functions (bug #44981)
Carnë Draug <carandraug@octave.org>
parents:
19861
diff
changeset
|
256 size_t t_len = t.length (); |
8462
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
257 |
20166
196871335aa8
Allow call with empty argument list in strcat related functions (bug #44981)
Carnë Draug <carandraug@octave.org>
parents:
19861
diff
changeset
|
258 if (max_len > t_len) |
196871335aa8
Allow call with empty argument list in strcat related functions (bug #44981)
Carnë Draug <carandraug@octave.org>
parents:
19861
diff
changeset
|
259 t += std::string (max_len - t_len, ' '); |
8462
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
260 |
20166
196871335aa8
Allow call with empty argument list in strcat related functions (bug #44981)
Carnë Draug <carandraug@octave.org>
parents:
19861
diff
changeset
|
261 result[k++] = t; |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10234
diff
changeset
|
262 } |
8462
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
263 } |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10234
diff
changeset
|
264 } |
20166
196871335aa8
Allow call with empty argument list in strcat related functions (bug #44981)
Carnë Draug <carandraug@octave.org>
parents:
19861
diff
changeset
|
265 } |
8462
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
266 |
21127
df7891224709
maint: Use "return ovl (...)" in DEFUN macros.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
267 // Cannot use ovl. Relies on overloaded octave_value call. |
21078
49852ff04747
maint: Remove unnecessary declarations of retval.
Rik <rik@octave.org>
parents:
21017
diff
changeset
|
268 return octave_value (result, '\''); |
8462
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
269 } |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
270 |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
271 /* |
21317
a4faec57f4c8
maint: remove semicolon after %!assert tests to follow Octave conventions.
Rik <rik@octave.org>
parents:
21301
diff
changeset
|
272 %!assert (strvcat (""), "") |
a4faec57f4c8
maint: remove semicolon after %!assert tests to follow Octave conventions.
Rik <rik@octave.org>
parents:
21301
diff
changeset
|
273 %!assert (strvcat (100) == "d") |
14429
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
274 %!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
|
275 %!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
|
276 %!assert (strvcat ([100,100]), ["dd"]) |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
277 %!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
|
278 %!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
|
279 %!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
|
280 %!assert (strvcat ({100,{100, {""}}}), ["d";"d"]) |
24461
0a034d34a686
Modify BIST tests to pass when string_fill_char is not ' '.
Rik <rik@octave.org>
parents:
24441
diff
changeset
|
281 %!assert (strvcat (["a ";"be"], {"c", 100}), ["a ";"be";"c ";"d "]) |
14429
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
282 %!assert (strvcat ("a", "bb", "ccc"), ["a "; "bb "; "ccc"]) |
20166
196871335aa8
Allow call with empty argument list in strcat related functions (bug #44981)
Carnë Draug <carandraug@octave.org>
parents:
19861
diff
changeset
|
283 %!assert (strvcat (), "") |
8462
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
284 */ |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
285 |
4535 | 286 DEFUN (ischar, args, , |
21966
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
287 doc: /* -*- texinfo -*- |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
288 @deftypefn {} {} ischar (@var{x}) |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
289 Return true if @var{x} is a character array. |
24474
0b65949870e3
isstring.m: Add new function for identifying string arrays.
Rik <rik@octave.org>
parents:
24461
diff
changeset
|
290 @seealso{isfloat, isinteger, islogical, isnumeric, isstring, iscellstr, isa} |
21966
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
291 @end deftypefn */) |
807 | 292 { |
20804
a6eaedd8bd75
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20704
diff
changeset
|
293 if (args.length () != 1) |
5823 | 294 print_usage (); |
807 | 295 |
20939
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
296 return ovl (args(0).is_string ()); |
807 | 297 } |
298 | |
8461
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
299 /* |
14429
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
300 %!assert (ischar ("a"), true) |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
301 %!assert (ischar (["ab";"cd"]), true) |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
302 %!assert (ischar ({"ab"}), false) |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
303 %!assert (ischar (1), false) |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
304 %!assert (ischar ([1, 2]), false) |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
305 %!assert (ischar ([]), false) |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
306 %!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
|
307 %!assert (ischar (""), true) |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
308 %!assert (ischar ("test"), true) |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
309 %!assert (ischar (["test"; "ing"]), true) |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
310 %!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
|
311 |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
312 %!error ischar () |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
313 %!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
|
314 */ |
8461
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
315 |
10108
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
316 static octave_value |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
317 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
|
318 octave_idx_type n, const char *fcn_name, |
22280
26109cce022e
New string utility functions to replace caseless_str for liboctave (bug #48726)
Carnë Draug <carandraug@octave.org>
parents:
22022
diff
changeset
|
319 bool (*array_op) (const Array<char>&, const Array<char>&, |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
320 octave_idx_type), |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
321 bool (*str_op) (const std::string&, const std::string&, |
22280
26109cce022e
New string utility functions to replace caseless_str for liboctave (bug #48726)
Carnë Draug <carandraug@octave.org>
parents:
22022
diff
changeset
|
322 std::string::size_type)) |
10108
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
323 |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
324 { |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
325 octave_value retval; |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
326 |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
327 bool s1_string = arg0.is_string (); |
23576
00e518162fda
maint: Deprecate is_cell and replace with iscell.
Rik <rik@octave.org>
parents:
23575
diff
changeset
|
328 bool s1_cell = arg0.iscell (); |
10108
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
329 bool s2_string = arg1.is_string (); |
23576
00e518162fda
maint: Deprecate is_cell and replace with iscell.
Rik <rik@octave.org>
parents:
23575
diff
changeset
|
330 bool s2_cell = arg1.iscell (); |
10108
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
331 |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
332 if (s1_string && s2_string) |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
333 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
|
334 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
|
335 { |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
336 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
|
337 |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
338 if (s1_string) |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
339 { |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
340 str_val = arg0; |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
341 cell_val = arg1; |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
342 } |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
343 else |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
344 { |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
345 str_val = arg1; |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
346 cell_val = arg0; |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
347 } |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
348 |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
349 const Cell cell = cell_val.cell_value (); |
20990
fc9cca99b2de
Deprecate all_strings, replace with string_vector_value.
Rik <rik@octave.org>
parents:
20940
diff
changeset
|
350 const string_vector str = str_val.string_vector_value (); |
20232
a9574e3c6e9e
Deprecate Array::length() and Sparse::length() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
20193
diff
changeset
|
351 octave_idx_type r = str.numel (); |
10108
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 if (r == 0 || r == 1) |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
354 { |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
355 // Broadcast the string. |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
356 |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
357 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
|
358 |
23450
855122b993da
maint: Wrap tertiary operator in parentheses "(COND ? x : y)".
Rik <rik@octave.org>
parents:
23396
diff
changeset
|
359 std::string s = (r == 0 ? "" : str[0]); |
10108
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
360 |
23575
e95738a119da
maint: Deprecate is_cellstr and replace with iscellstr.
Rik <rik@octave.org>
parents:
23457
diff
changeset
|
361 if (cell_val.iscellstr ()) |
10108
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
362 { |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
363 const Array<std::string> cellstr = cell_val.cellstr_value (); |
20232
a9574e3c6e9e
Deprecate Array::length() and Sparse::length() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
20193
diff
changeset
|
364 for (octave_idx_type i = 0; i < cellstr.numel (); i++) |
10108
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
365 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
|
366 } |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
367 else |
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 // FIXME: should we warn here? |
20232
a9574e3c6e9e
Deprecate Array::length() and Sparse::length() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
20193
diff
changeset
|
370 for (octave_idx_type i = 0; i < cell.numel (); i++) |
10108
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
371 { |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
372 if (cell(i).is_string ()) |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
373 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
|
374 } |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
375 } |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
376 |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
377 retval = output; |
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 else if (r > 1) |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
380 { |
20232
a9574e3c6e9e
Deprecate Array::length() and Sparse::length() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
20193
diff
changeset
|
381 if (cell.numel () == 1) |
10108
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 // Broadcast the cell. |
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 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
|
386 boolNDArray output (dv, false); |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
387 |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
388 if (cell(0).is_string ()) |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
389 { |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
390 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
|
391 |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
392 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
|
393 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
|
394 } |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
395 |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
396 retval = output; |
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 else |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
399 { |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
400 // Must match in all dimensions. |
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 boolNDArray output (cell.dims (), false); |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
403 |
20232
a9574e3c6e9e
Deprecate Array::length() and Sparse::length() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
20193
diff
changeset
|
404 if (cell.numel () == r) |
10108
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
405 { |
23575
e95738a119da
maint: Deprecate is_cellstr and replace with iscellstr.
Rik <rik@octave.org>
parents:
23457
diff
changeset
|
406 if (cell_val.iscellstr ()) |
10108
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
407 { |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
408 const Array<std::string> cellstr |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
409 = cell_val.cellstr_value (); |
20232
a9574e3c6e9e
Deprecate Array::length() and Sparse::length() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
20193
diff
changeset
|
410 for (octave_idx_type i = 0; i < cellstr.numel (); i++) |
10108
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
411 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
|
412 } |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
413 else |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
414 { |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
415 // FIXME: should we warn here? |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
416 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
|
417 { |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
418 if (cell(i).is_string ()) |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
419 output(i) = str_op (str[i], |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
420 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
|
421 } |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
422 } |
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 retval = output; |
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 else |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
427 retval = false; |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
428 } |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
429 } |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
430 } |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
431 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
|
432 { |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
433 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
|
434 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
|
435 |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
436 if (r1 == 1) |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
437 { |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
438 // Make the singleton cell2. |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
439 |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
440 cell1_val = arg1; |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
441 cell2_val = arg0; |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
442 } |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
443 else |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
444 { |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
445 cell1_val = arg0; |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
446 cell2_val = arg1; |
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 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
|
450 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
|
451 r1 = cell1.numel (); |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
452 r2 = cell2.numel (); |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
453 |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
454 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
|
455 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
|
456 |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
457 boolNDArray output (size1, false); |
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 if (r2 == 1) |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
460 { |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
461 // Broadcast cell2. |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
462 |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
463 if (cell2(0).is_string ()) |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
464 { |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
465 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
|
466 |
23575
e95738a119da
maint: Deprecate is_cellstr and replace with iscellstr.
Rik <rik@octave.org>
parents:
23457
diff
changeset
|
467 if (cell1_val.iscellstr ()) |
10108
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 const Array<std::string> cellstr = cell1_val.cellstr_value (); |
20232
a9574e3c6e9e
Deprecate Array::length() and Sparse::length() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
20193
diff
changeset
|
470 for (octave_idx_type i = 0; i < cellstr.numel (); i++) |
10108
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
471 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
|
472 } |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
473 else |
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 // FIXME: should we warn here? |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
476 for (octave_idx_type i = 0; i < r1; i++) |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
477 { |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
478 if (cell1(i).is_string ()) |
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 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
|
481 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
|
482 } |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
483 } |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
484 } |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
485 } |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
486 } |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
487 else |
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 (size1 != size2) |
20831
35241c4b696c
eliminate return statements after calls to error
John W. Eaton <jwe@octave.org>
parents:
20804
diff
changeset
|
490 error ("%s: nonconformant cell arrays", fcn_name); |
10108
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
491 |
23575
e95738a119da
maint: Deprecate is_cellstr and replace with iscellstr.
Rik <rik@octave.org>
parents:
23457
diff
changeset
|
492 if (cell1.iscellstr () && cell2.iscellstr ()) |
10108
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
493 { |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
494 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
|
495 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
|
496 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
|
497 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
|
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 () && cell2(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 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
|
508 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
|
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 |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
514 retval = output; |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
515 } |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
516 else |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
517 retval = false; |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
518 |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
519 return retval; |
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 |
22280
26109cce022e
New string utility functions to replace caseless_str for liboctave (bug #48726)
Carnë Draug <carandraug@octave.org>
parents:
22022
diff
changeset
|
522 |
26109cce022e
New string utility functions to replace caseless_str for liboctave (bug #48726)
Carnë Draug <carandraug@octave.org>
parents:
22022
diff
changeset
|
523 // These are required so that they match the same signature as strncmp |
26109cce022e
New string utility functions to replace caseless_str for liboctave (bug #48726)
Carnë Draug <carandraug@octave.org>
parents:
22022
diff
changeset
|
524 // and strncmpi and can therefore be used in do_strcmp_fun. |
10108
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
525 |
22280
26109cce022e
New string utility functions to replace caseless_str for liboctave (bug #48726)
Carnë Draug <carandraug@octave.org>
parents:
22022
diff
changeset
|
526 template <typename T, typename T_size_type> |
10108
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
527 static bool |
22280
26109cce022e
New string utility functions to replace caseless_str for liboctave (bug #48726)
Carnë Draug <carandraug@octave.org>
parents:
22022
diff
changeset
|
528 strcmp_ignore_n (const T& s1, const T& s2, T_size_type) |
26109cce022e
New string utility functions to replace caseless_str for liboctave (bug #48726)
Carnë Draug <carandraug@octave.org>
parents:
22022
diff
changeset
|
529 { return octave::string::strcmp (s1, s2); } |
26109cce022e
New string utility functions to replace caseless_str for liboctave (bug #48726)
Carnë Draug <carandraug@octave.org>
parents:
22022
diff
changeset
|
530 |
26109cce022e
New string utility functions to replace caseless_str for liboctave (bug #48726)
Carnë Draug <carandraug@octave.org>
parents:
22022
diff
changeset
|
531 template <typename T, typename T_size_type> |
26109cce022e
New string utility functions to replace caseless_str for liboctave (bug #48726)
Carnë Draug <carandraug@octave.org>
parents:
22022
diff
changeset
|
532 static bool |
26109cce022e
New string utility functions to replace caseless_str for liboctave (bug #48726)
Carnë Draug <carandraug@octave.org>
parents:
22022
diff
changeset
|
533 strcmpi_ignore_n (const T& s1, const T& s2, T_size_type) |
26109cce022e
New string utility functions to replace caseless_str for liboctave (bug #48726)
Carnë Draug <carandraug@octave.org>
parents:
22022
diff
changeset
|
534 { return octave::string::strcmpi (s1, s2); } |
26109cce022e
New string utility functions to replace caseless_str for liboctave (bug #48726)
Carnë Draug <carandraug@octave.org>
parents:
22022
diff
changeset
|
535 |
10108
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
536 |
5415 | 537 DEFUN (strcmp, args, , |
21966
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
538 doc: /* -*- texinfo -*- |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
539 @deftypefn {} {} strcmp (@var{s1}, @var{s2}) |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
540 Return 1 if the character strings @var{s1} and @var{s2} are the same, |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
541 and 0 otherwise. |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
542 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
543 If either @var{s1} or @var{s2} is a cell array of strings, then an array |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
544 of the same size is returned, containing the values described above for |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
545 every member of the cell array. The other argument may also be a cell |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
546 array of strings (of the same size or with only one element), char matrix |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
547 or character string. |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
548 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
549 @strong{Caution:} For compatibility with @sc{matlab}, Octave's strcmp |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
550 function returns 1 if the character strings are equal, and 0 otherwise. |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
551 This is just the opposite of the corresponding C library function. |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
552 @seealso{strcmpi, strncmp, strncmpi} |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
553 @end deftypefn */) |
5415 | 554 { |
20804
a6eaedd8bd75
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20704
diff
changeset
|
555 if (args.length () != 2) |
5823 | 556 print_usage (); |
5415 | 557 |
20939
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
558 return ovl (do_strcmp_fun (args(0), args(1), 0, "strcmp", |
22280
26109cce022e
New string utility functions to replace caseless_str for liboctave (bug #48726)
Carnë Draug <carandraug@octave.org>
parents:
22022
diff
changeset
|
559 strcmp_ignore_n, strcmp_ignore_n)); |
5415 | 560 } |
561 | |
5862 | 562 /* |
563 %!shared x | |
14429
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
564 %! x = char (zeros (0, 2)); |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
565 %!assert (strcmp ("", x), false) |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
566 %!assert (strcmp (x, ""), false) |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
567 %!assert (strcmp (x, x), true) |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
568 ## %!assert (strcmp ({""}, x), true) |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
569 ## %!assert (strcmp ({x}, ""), false) |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
570 ## %!assert (strcmp ({x}, x), true) |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
571 ## %!assert (strcmp ("", {x}), false) |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
572 ## %!assert (strcmp (x, {""}), false) |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
573 ## %!assert (strcmp (x, {x}), true) |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
574 ## %!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
|
575 ## %!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
|
576 ## %!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
|
577 ## %!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
|
578 %!assert (strcmp ({"foo"}, x), false) |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
579 %!assert (strcmp ({"foo"}, "foo"), true) |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
580 %!assert (strcmp ({"foo"}, x), false) |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
581 %!assert (strcmp (x, {"foo"}), false) |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
582 %!assert (strcmp ("foo", {"foo"}), true) |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
583 %!assert (strcmp (x, {"foo"}), false) |
5862 | 584 %!shared y |
14429
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
585 %! y = char (zeros (2, 0)); |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
586 %!assert (strcmp ("", y), false) |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
587 %!assert (strcmp (y, ""), false) |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
588 %!assert (strcmp (y, y), true) |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
589 %!assert (strcmp ({""}, y), [true; true]) |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
590 %!assert (strcmp ({y}, ""), true) |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
591 %!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
|
592 %!assert (strcmp ("", {y}), true) |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
593 %!assert (strcmp (y, {""}), [true; true]) |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
594 %!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
|
595 %!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
|
596 %!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
|
597 %!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
|
598 %!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
|
599 %!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
|
600 %!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
|
601 %!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
|
602 %!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
|
603 %!assert (strcmp ("foobar", "foobar"), true) |
23731
0056bcaeb01c
make strncmp case sensitive again (bug #51384)
John W. Eaton <jwe@octave.org>
parents:
23219
diff
changeset
|
604 %!assert (strcmp ("foobar", "fooBar"), false) |
14429
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
605 %!assert (strcmp ("fooba", "foobar"), false) |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
606 |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
607 %!error strcmp () |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
608 %!error strcmp ("foo", "bar", 3) |
5862 | 609 */ |
610 | |
6250 | 611 DEFUN (strncmp, args, , |
21966
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
612 doc: /* -*- texinfo -*- |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
613 @deftypefn {} {} strncmp (@var{s1}, @var{s2}, @var{n}) |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
614 Return 1 if the first @var{n} characters of strings @var{s1} and @var{s2} |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
615 are the same, and 0 otherwise. |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
616 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
617 @example |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
618 @group |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
619 strncmp ("abce", "abcd", 3) |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
620 @result{} 1 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
621 @end group |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
622 @end example |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
623 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
624 If either @var{s1} or @var{s2} is a cell array of strings, then an array |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
625 of the same size is returned, containing the values described above for |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
626 every member of the cell array. The other argument may also be a cell |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
627 array of strings (of the same size or with only one element), char matrix |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
628 or character string. |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
629 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
630 @example |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
631 @group |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
632 strncmp ("abce", @{"abcd", "bca", "abc"@}, 3) |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
633 @result{} [1, 0, 1] |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
634 @end group |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
635 @end example |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
636 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
637 @strong{Caution:} For compatibility with @sc{matlab}, Octave's strncmp |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
638 function returns 1 if the character strings are equal, and 0 otherwise. |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
639 This is just the opposite of the corresponding C library function. |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
640 @seealso{strncmpi, strcmp, strcmpi} |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
641 @end deftypefn */) |
6250 | 642 { |
20804
a6eaedd8bd75
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20704
diff
changeset
|
643 if (args.length () != 3) |
a6eaedd8bd75
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20704
diff
changeset
|
644 print_usage (); |
a6eaedd8bd75
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20704
diff
changeset
|
645 |
a6eaedd8bd75
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20704
diff
changeset
|
646 octave_idx_type n = args(2).idx_type_value (); |
6250 | 647 |
20804
a6eaedd8bd75
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20704
diff
changeset
|
648 if (n > 0) |
20939
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
649 return ovl (do_strcmp_fun (args(0), args(1), n, "strncmp", |
22280
26109cce022e
New string utility functions to replace caseless_str for liboctave (bug #48726)
Carnë Draug <carandraug@octave.org>
parents:
22022
diff
changeset
|
650 octave::string::strncmp, |
26109cce022e
New string utility functions to replace caseless_str for liboctave (bug #48726)
Carnë Draug <carandraug@octave.org>
parents:
22022
diff
changeset
|
651 octave::string::strncmp)); |
6250 | 652 else |
20804
a6eaedd8bd75
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20704
diff
changeset
|
653 error ("strncmp: N must be greater than 0"); |
6250 | 654 } |
655 | |
8461
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
656 /* |
14429
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
657 %!assert (strncmp ("abce", "abc", 3), true) |
23731
0056bcaeb01c
make strncmp case sensitive again (bug #51384)
John W. Eaton <jwe@octave.org>
parents:
23219
diff
changeset
|
658 %!assert (strncmp ("abce", "aBc", 3), false) |
14429
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
659 %!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
|
660 %!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
|
661 %!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
|
662 %!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
|
663 %!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
|
664 %!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
|
665 |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
666 %!error strncmp () |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
667 %!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
|
668 */ |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
669 |
10316
9966f1f71c32
use DEFUNX instead of DEFUN for strcmpi and strncmpi
John W. Eaton <jwe@octave.org>
parents:
10315
diff
changeset
|
670 DEFUNX ("strcmpi", Fstrcmpi, args, , |
21966
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
671 doc: /* -*- texinfo -*- |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
672 @deftypefn {} {} strcmpi (@var{s1}, @var{s2}) |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
673 Return 1 if the character strings @var{s1} and @var{s2} are the same, |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
674 disregarding case of alphabetic characters, and 0 otherwise. |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
675 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
676 If either @var{s1} or @var{s2} is a cell array of strings, then an array |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
677 of the same size is returned, containing the values described above for |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
678 every member of the cell array. The other argument may also be a cell |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
679 array of strings (of the same size or with only one element), char matrix |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
680 or character string. |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
681 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
682 @strong{Caution:} For compatibility with @sc{matlab}, Octave's strcmp |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
683 function returns 1 if the character strings are equal, and 0 otherwise. |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
684 This is just the opposite of the corresponding C library function. |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
685 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
686 @strong{Caution:} National alphabets are not supported. |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
687 @seealso{strcmp, strncmp, strncmpi} |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
688 @end deftypefn */) |
10108
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
689 { |
20804
a6eaedd8bd75
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20704
diff
changeset
|
690 if (args.length () != 2) |
10108
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
691 print_usage (); |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
692 |
20939
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
693 return ovl (do_strcmp_fun (args(0), args(1), 0, "strcmpi", |
22280
26109cce022e
New string utility functions to replace caseless_str for liboctave (bug #48726)
Carnë Draug <carandraug@octave.org>
parents:
22022
diff
changeset
|
694 strcmpi_ignore_n, strcmpi_ignore_n)); |
10108
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
695 } |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
696 |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
697 /* |
14429
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
698 %!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
|
699 */ |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
700 |
10318
f0ac2fa91733
fix incorrect strncmpi change
John W. Eaton <jwe@octave.org>
parents:
10316
diff
changeset
|
701 DEFUNX ("strncmpi", Fstrncmpi, args, , |
21966
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
702 doc: /* -*- texinfo -*- |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
703 @deftypefn {} {} strncmpi (@var{s1}, @var{s2}, @var{n}) |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
704 Return 1 if the first @var{n} character of @var{s1} and @var{s2} are the |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
705 same, disregarding case of alphabetic characters, and 0 otherwise. |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
706 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
707 If either @var{s1} or @var{s2} is a cell array of strings, then an array |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
708 of the same size is returned, containing the values described above for |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
709 every member of the cell array. The other argument may also be a cell |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
710 array of strings (of the same size or with only one element), char matrix |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
711 or character string. |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
712 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
713 @strong{Caution:} For compatibility with @sc{matlab}, Octave's strncmpi |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
714 function returns 1 if the character strings are equal, and 0 otherwise. |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
715 This is just the opposite of the corresponding C library function. |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
716 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
717 @strong{Caution:} National alphabets are not supported. |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
718 @seealso{strncmp, strcmp, strcmpi} |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
719 @end deftypefn */) |
10108
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
720 { |
20804
a6eaedd8bd75
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20704
diff
changeset
|
721 if (args.length () != 3) |
a6eaedd8bd75
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20704
diff
changeset
|
722 print_usage (); |
a6eaedd8bd75
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20704
diff
changeset
|
723 |
a6eaedd8bd75
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20704
diff
changeset
|
724 octave_idx_type n = args(2).idx_type_value (); |
10108
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
725 |
20804
a6eaedd8bd75
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20704
diff
changeset
|
726 if (n > 0) |
20939
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
727 return ovl (do_strcmp_fun (args(0), args(1), n, "strncmpi", |
22280
26109cce022e
New string utility functions to replace caseless_str for liboctave (bug #48726)
Carnë Draug <carandraug@octave.org>
parents:
22022
diff
changeset
|
728 octave::string::strncmpi, |
26109cce022e
New string utility functions to replace caseless_str for liboctave (bug #48726)
Carnë Draug <carandraug@octave.org>
parents:
22022
diff
changeset
|
729 octave::string::strncmpi)); |
10108
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
730 else |
20804
a6eaedd8bd75
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20704
diff
changeset
|
731 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
|
732 } |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
733 |
06e11df4592d
implement built-in strcmpi/strncmpi, reduce some code duplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
734 /* |
14429
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
735 %!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
|
736 */ |
8461
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
737 |
23122
e310b5b6da6f
Add functions native2unicode and unicode2native (bug #49842).
Markus Mützel <markus.muetzel@gmx.de>
parents:
23084
diff
changeset
|
738 DEFUN (__native2unicode__, args, , |
e310b5b6da6f
Add functions native2unicode and unicode2native (bug #49842).
Markus Mützel <markus.muetzel@gmx.de>
parents:
23084
diff
changeset
|
739 doc: /* -*- texinfo -*- |
e310b5b6da6f
Add functions native2unicode and unicode2native (bug #49842).
Markus Mützel <markus.muetzel@gmx.de>
parents:
23084
diff
changeset
|
740 @deftypefn {} {@var{utf8_str} =} __native2unicode__ (@var{native_bytes}, @var{codepage}) |
e310b5b6da6f
Add functions native2unicode and unicode2native (bug #49842).
Markus Mützel <markus.muetzel@gmx.de>
parents:
23084
diff
changeset
|
741 Convert byte stream @var{native_bytes} to UTF-8 using @var{codepage}. |
e310b5b6da6f
Add functions native2unicode and unicode2native (bug #49842).
Markus Mützel <markus.muetzel@gmx.de>
parents:
23084
diff
changeset
|
742 |
e310b5b6da6f
Add functions native2unicode and unicode2native (bug #49842).
Markus Mützel <markus.muetzel@gmx.de>
parents:
23084
diff
changeset
|
743 @seealso{native2unicode, __unicode2native__} |
e310b5b6da6f
Add functions native2unicode and unicode2native (bug #49842).
Markus Mützel <markus.muetzel@gmx.de>
parents:
23084
diff
changeset
|
744 @end deftypefn */) |
e310b5b6da6f
Add functions native2unicode and unicode2native (bug #49842).
Markus Mützel <markus.muetzel@gmx.de>
parents:
23084
diff
changeset
|
745 { |
e310b5b6da6f
Add functions native2unicode and unicode2native (bug #49842).
Markus Mützel <markus.muetzel@gmx.de>
parents:
23084
diff
changeset
|
746 int nargin = args.length (); |
e310b5b6da6f
Add functions native2unicode and unicode2native (bug #49842).
Markus Mützel <markus.muetzel@gmx.de>
parents:
23084
diff
changeset
|
747 |
23123
c6ca5fe1505c
use wrappers for uniconv functions; style fixes for unicode conv fcns
John W. Eaton <jwe@octave.org>
parents:
23122
diff
changeset
|
748 if (nargin != 2) |
23122
e310b5b6da6f
Add functions native2unicode and unicode2native (bug #49842).
Markus Mützel <markus.muetzel@gmx.de>
parents:
23084
diff
changeset
|
749 print_usage (); |
e310b5b6da6f
Add functions native2unicode and unicode2native (bug #49842).
Markus Mützel <markus.muetzel@gmx.de>
parents:
23084
diff
changeset
|
750 |
e310b5b6da6f
Add functions native2unicode and unicode2native (bug #49842).
Markus Mützel <markus.muetzel@gmx.de>
parents:
23084
diff
changeset
|
751 if (args(0).is_string ()) |
23123
c6ca5fe1505c
use wrappers for uniconv functions; style fixes for unicode conv fcns
John W. Eaton <jwe@octave.org>
parents:
23122
diff
changeset
|
752 return ovl (args(0)); |
23122
e310b5b6da6f
Add functions native2unicode and unicode2native (bug #49842).
Markus Mützel <markus.muetzel@gmx.de>
parents:
23084
diff
changeset
|
753 |
23123
c6ca5fe1505c
use wrappers for uniconv functions; style fixes for unicode conv fcns
John W. Eaton <jwe@octave.org>
parents:
23122
diff
changeset
|
754 std::string tmp = args(1).xstring_value ("CODEPAGE must be a string"); |
c6ca5fe1505c
use wrappers for uniconv functions; style fixes for unicode conv fcns
John W. Eaton <jwe@octave.org>
parents:
23122
diff
changeset
|
755 const char *codepage |
23450
855122b993da
maint: Wrap tertiary operator in parentheses "(COND ? x : y)".
Rik <rik@octave.org>
parents:
23396
diff
changeset
|
756 = (tmp.empty () ? octave_locale_charset_wrapper () : tmp.c_str ()); |
23396
945b53af4655
maint: Strip trailing whitespace from source files.
John W. Eaton <jwe@octave.org>
parents:
23220
diff
changeset
|
757 |
23123
c6ca5fe1505c
use wrappers for uniconv functions; style fixes for unicode conv fcns
John W. Eaton <jwe@octave.org>
parents:
23122
diff
changeset
|
758 charNDArray native_bytes = args(0).char_array_value (); |
23122
e310b5b6da6f
Add functions native2unicode and unicode2native (bug #49842).
Markus Mützel <markus.muetzel@gmx.de>
parents:
23084
diff
changeset
|
759 |
23123
c6ca5fe1505c
use wrappers for uniconv functions; style fixes for unicode conv fcns
John W. Eaton <jwe@octave.org>
parents:
23122
diff
changeset
|
760 const char *src = native_bytes.data (); |
c6ca5fe1505c
use wrappers for uniconv functions; style fixes for unicode conv fcns
John W. Eaton <jwe@octave.org>
parents:
23122
diff
changeset
|
761 size_t srclen = native_bytes.numel (); |
c6ca5fe1505c
use wrappers for uniconv functions; style fixes for unicode conv fcns
John W. Eaton <jwe@octave.org>
parents:
23122
diff
changeset
|
762 |
23122
e310b5b6da6f
Add functions native2unicode and unicode2native (bug #49842).
Markus Mützel <markus.muetzel@gmx.de>
parents:
23084
diff
changeset
|
763 size_t length; |
23457
21baad6b35c4
maint: Use C++11 nullptr rather than 0 or NULL when possible.
Rik <rik@octave.org>
parents:
23450
diff
changeset
|
764 uint8_t *utf8_str = nullptr; |
23123
c6ca5fe1505c
use wrappers for uniconv functions; style fixes for unicode conv fcns
John W. Eaton <jwe@octave.org>
parents:
23122
diff
changeset
|
765 |
c6ca5fe1505c
use wrappers for uniconv functions; style fixes for unicode conv fcns
John W. Eaton <jwe@octave.org>
parents:
23122
diff
changeset
|
766 octave::unwind_protect frame; |
c6ca5fe1505c
use wrappers for uniconv functions; style fixes for unicode conv fcns
John W. Eaton <jwe@octave.org>
parents:
23122
diff
changeset
|
767 |
c6ca5fe1505c
use wrappers for uniconv functions; style fixes for unicode conv fcns
John W. Eaton <jwe@octave.org>
parents:
23122
diff
changeset
|
768 utf8_str = octave_u8_conv_from_encoding (codepage, src, srclen, &length); |
23122
e310b5b6da6f
Add functions native2unicode and unicode2native (bug #49842).
Markus Mützel <markus.muetzel@gmx.de>
parents:
23084
diff
changeset
|
769 |
23123
c6ca5fe1505c
use wrappers for uniconv functions; style fixes for unicode conv fcns
John W. Eaton <jwe@octave.org>
parents:
23122
diff
changeset
|
770 if (! utf8_str) |
c6ca5fe1505c
use wrappers for uniconv functions; style fixes for unicode conv fcns
John W. Eaton <jwe@octave.org>
parents:
23122
diff
changeset
|
771 error ("native2unicode: converting from codepage '%s' to UTF-8: %s", |
c6ca5fe1505c
use wrappers for uniconv functions; style fixes for unicode conv fcns
John W. Eaton <jwe@octave.org>
parents:
23122
diff
changeset
|
772 codepage, std::strerror (errno)); |
c6ca5fe1505c
use wrappers for uniconv functions; style fixes for unicode conv fcns
John W. Eaton <jwe@octave.org>
parents:
23122
diff
changeset
|
773 |
24379
56d1e4d9975e
avoid memory leak in utf8 conversion functions
John W. Eaton <jwe@octave.org>
parents:
23812
diff
changeset
|
774 frame.add_fcn (::free, static_cast<void *> (utf8_str)); |
56d1e4d9975e
avoid memory leak in utf8 conversion functions
John W. Eaton <jwe@octave.org>
parents:
23812
diff
changeset
|
775 |
23123
c6ca5fe1505c
use wrappers for uniconv functions; style fixes for unicode conv fcns
John W. Eaton <jwe@octave.org>
parents:
23122
diff
changeset
|
776 octave_idx_type len = length; |
23122
e310b5b6da6f
Add functions native2unicode and unicode2native (bug #49842).
Markus Mützel <markus.muetzel@gmx.de>
parents:
23084
diff
changeset
|
777 |
23123
c6ca5fe1505c
use wrappers for uniconv functions; style fixes for unicode conv fcns
John W. Eaton <jwe@octave.org>
parents:
23122
diff
changeset
|
778 charNDArray retval (dim_vector (1, len)); |
c6ca5fe1505c
use wrappers for uniconv functions; style fixes for unicode conv fcns
John W. Eaton <jwe@octave.org>
parents:
23122
diff
changeset
|
779 |
c6ca5fe1505c
use wrappers for uniconv functions; style fixes for unicode conv fcns
John W. Eaton <jwe@octave.org>
parents:
23122
diff
changeset
|
780 for (octave_idx_type i = 0; i < len; i++) |
c6ca5fe1505c
use wrappers for uniconv functions; style fixes for unicode conv fcns
John W. Eaton <jwe@octave.org>
parents:
23122
diff
changeset
|
781 retval.xelem(i) = utf8_str[i]; |
c6ca5fe1505c
use wrappers for uniconv functions; style fixes for unicode conv fcns
John W. Eaton <jwe@octave.org>
parents:
23122
diff
changeset
|
782 |
c6ca5fe1505c
use wrappers for uniconv functions; style fixes for unicode conv fcns
John W. Eaton <jwe@octave.org>
parents:
23122
diff
changeset
|
783 return ovl (retval); |
23122
e310b5b6da6f
Add functions native2unicode and unicode2native (bug #49842).
Markus Mützel <markus.muetzel@gmx.de>
parents:
23084
diff
changeset
|
784 } |
e310b5b6da6f
Add functions native2unicode and unicode2native (bug #49842).
Markus Mützel <markus.muetzel@gmx.de>
parents:
23084
diff
changeset
|
785 |
e310b5b6da6f
Add functions native2unicode and unicode2native (bug #49842).
Markus Mützel <markus.muetzel@gmx.de>
parents:
23084
diff
changeset
|
786 DEFUN (__unicode2native__, args, , |
e310b5b6da6f
Add functions native2unicode and unicode2native (bug #49842).
Markus Mützel <markus.muetzel@gmx.de>
parents:
23084
diff
changeset
|
787 doc: /* -*- texinfo -*- |
e310b5b6da6f
Add functions native2unicode and unicode2native (bug #49842).
Markus Mützel <markus.muetzel@gmx.de>
parents:
23084
diff
changeset
|
788 @deftypefn {} {@var{native_bytes} =} __unicode2native__ (@var{utf8_str}, @var{codepage}) |
e310b5b6da6f
Add functions native2unicode and unicode2native (bug #49842).
Markus Mützel <markus.muetzel@gmx.de>
parents:
23084
diff
changeset
|
789 Convert UTF-8 string @var{utf8_str} to byte stream @var{native_bytes} using |
e310b5b6da6f
Add functions native2unicode and unicode2native (bug #49842).
Markus Mützel <markus.muetzel@gmx.de>
parents:
23084
diff
changeset
|
790 @var{codepage}. |
e310b5b6da6f
Add functions native2unicode and unicode2native (bug #49842).
Markus Mützel <markus.muetzel@gmx.de>
parents:
23084
diff
changeset
|
791 |
e310b5b6da6f
Add functions native2unicode and unicode2native (bug #49842).
Markus Mützel <markus.muetzel@gmx.de>
parents:
23084
diff
changeset
|
792 @seealso{unicode2native, __native2unicode__} |
e310b5b6da6f
Add functions native2unicode and unicode2native (bug #49842).
Markus Mützel <markus.muetzel@gmx.de>
parents:
23084
diff
changeset
|
793 @end deftypefn */) |
e310b5b6da6f
Add functions native2unicode and unicode2native (bug #49842).
Markus Mützel <markus.muetzel@gmx.de>
parents:
23084
diff
changeset
|
794 { |
e310b5b6da6f
Add functions native2unicode and unicode2native (bug #49842).
Markus Mützel <markus.muetzel@gmx.de>
parents:
23084
diff
changeset
|
795 int nargin = args.length (); |
e310b5b6da6f
Add functions native2unicode and unicode2native (bug #49842).
Markus Mützel <markus.muetzel@gmx.de>
parents:
23084
diff
changeset
|
796 |
e310b5b6da6f
Add functions native2unicode and unicode2native (bug #49842).
Markus Mützel <markus.muetzel@gmx.de>
parents:
23084
diff
changeset
|
797 if (nargin != 2) |
e310b5b6da6f
Add functions native2unicode and unicode2native (bug #49842).
Markus Mützel <markus.muetzel@gmx.de>
parents:
23084
diff
changeset
|
798 print_usage (); |
e310b5b6da6f
Add functions native2unicode and unicode2native (bug #49842).
Markus Mützel <markus.muetzel@gmx.de>
parents:
23084
diff
changeset
|
799 |
23123
c6ca5fe1505c
use wrappers for uniconv functions; style fixes for unicode conv fcns
John W. Eaton <jwe@octave.org>
parents:
23122
diff
changeset
|
800 std::string tmp = args(1).xstring_value ("CODEPAGE must be a string"); |
c6ca5fe1505c
use wrappers for uniconv functions; style fixes for unicode conv fcns
John W. Eaton <jwe@octave.org>
parents:
23122
diff
changeset
|
801 const char *codepage |
23450
855122b993da
maint: Wrap tertiary operator in parentheses "(COND ? x : y)".
Rik <rik@octave.org>
parents:
23396
diff
changeset
|
802 = (tmp.empty () ? octave_locale_charset_wrapper () : tmp.c_str ()); |
23122
e310b5b6da6f
Add functions native2unicode and unicode2native (bug #49842).
Markus Mützel <markus.muetzel@gmx.de>
parents:
23084
diff
changeset
|
803 |
23123
c6ca5fe1505c
use wrappers for uniconv functions; style fixes for unicode conv fcns
John W. Eaton <jwe@octave.org>
parents:
23122
diff
changeset
|
804 charNDArray utf8_str = args(0).xchar_array_value ("UTF8_STR must be a string"); |
c6ca5fe1505c
use wrappers for uniconv functions; style fixes for unicode conv fcns
John W. Eaton <jwe@octave.org>
parents:
23122
diff
changeset
|
805 |
c6ca5fe1505c
use wrappers for uniconv functions; style fixes for unicode conv fcns
John W. Eaton <jwe@octave.org>
parents:
23122
diff
changeset
|
806 const uint8_t *src = reinterpret_cast<const uint8_t *> (utf8_str.data ()); |
c6ca5fe1505c
use wrappers for uniconv functions; style fixes for unicode conv fcns
John W. Eaton <jwe@octave.org>
parents:
23122
diff
changeset
|
807 size_t srclen = utf8_str.numel (); |
c6ca5fe1505c
use wrappers for uniconv functions; style fixes for unicode conv fcns
John W. Eaton <jwe@octave.org>
parents:
23122
diff
changeset
|
808 |
23122
e310b5b6da6f
Add functions native2unicode and unicode2native (bug #49842).
Markus Mützel <markus.muetzel@gmx.de>
parents:
23084
diff
changeset
|
809 size_t length; |
23457
21baad6b35c4
maint: Use C++11 nullptr rather than 0 or NULL when possible.
Rik <rik@octave.org>
parents:
23450
diff
changeset
|
810 char *native_bytes = nullptr; |
23123
c6ca5fe1505c
use wrappers for uniconv functions; style fixes for unicode conv fcns
John W. Eaton <jwe@octave.org>
parents:
23122
diff
changeset
|
811 |
c6ca5fe1505c
use wrappers for uniconv functions; style fixes for unicode conv fcns
John W. Eaton <jwe@octave.org>
parents:
23122
diff
changeset
|
812 octave::unwind_protect frame; |
c6ca5fe1505c
use wrappers for uniconv functions; style fixes for unicode conv fcns
John W. Eaton <jwe@octave.org>
parents:
23122
diff
changeset
|
813 |
c6ca5fe1505c
use wrappers for uniconv functions; style fixes for unicode conv fcns
John W. Eaton <jwe@octave.org>
parents:
23122
diff
changeset
|
814 native_bytes = octave_u8_conv_to_encoding (codepage, src, srclen, &length); |
23122
e310b5b6da6f
Add functions native2unicode and unicode2native (bug #49842).
Markus Mützel <markus.muetzel@gmx.de>
parents:
23084
diff
changeset
|
815 |
23123
c6ca5fe1505c
use wrappers for uniconv functions; style fixes for unicode conv fcns
John W. Eaton <jwe@octave.org>
parents:
23122
diff
changeset
|
816 if (! native_bytes) |
c6ca5fe1505c
use wrappers for uniconv functions; style fixes for unicode conv fcns
John W. Eaton <jwe@octave.org>
parents:
23122
diff
changeset
|
817 error ("native2unicode: converting from UTF-8 to codepage '%s': %s", |
c6ca5fe1505c
use wrappers for uniconv functions; style fixes for unicode conv fcns
John W. Eaton <jwe@octave.org>
parents:
23122
diff
changeset
|
818 codepage, std::strerror (errno)); |
c6ca5fe1505c
use wrappers for uniconv functions; style fixes for unicode conv fcns
John W. Eaton <jwe@octave.org>
parents:
23122
diff
changeset
|
819 |
24379
56d1e4d9975e
avoid memory leak in utf8 conversion functions
John W. Eaton <jwe@octave.org>
parents:
23812
diff
changeset
|
820 frame.add_fcn (::free, static_cast<void *> (native_bytes)); |
56d1e4d9975e
avoid memory leak in utf8 conversion functions
John W. Eaton <jwe@octave.org>
parents:
23812
diff
changeset
|
821 |
23123
c6ca5fe1505c
use wrappers for uniconv functions; style fixes for unicode conv fcns
John W. Eaton <jwe@octave.org>
parents:
23122
diff
changeset
|
822 octave_idx_type len = length; |
23122
e310b5b6da6f
Add functions native2unicode and unicode2native (bug #49842).
Markus Mützel <markus.muetzel@gmx.de>
parents:
23084
diff
changeset
|
823 |
23123
c6ca5fe1505c
use wrappers for uniconv functions; style fixes for unicode conv fcns
John W. Eaton <jwe@octave.org>
parents:
23122
diff
changeset
|
824 uint8NDArray retval (dim_vector (1, len)); |
c6ca5fe1505c
use wrappers for uniconv functions; style fixes for unicode conv fcns
John W. Eaton <jwe@octave.org>
parents:
23122
diff
changeset
|
825 |
c6ca5fe1505c
use wrappers for uniconv functions; style fixes for unicode conv fcns
John W. Eaton <jwe@octave.org>
parents:
23122
diff
changeset
|
826 for (octave_idx_type i = 0; i < len; i++) |
c6ca5fe1505c
use wrappers for uniconv functions; style fixes for unicode conv fcns
John W. Eaton <jwe@octave.org>
parents:
23122
diff
changeset
|
827 retval.xelem(i) = native_bytes[i]; |
c6ca5fe1505c
use wrappers for uniconv functions; style fixes for unicode conv fcns
John W. Eaton <jwe@octave.org>
parents:
23122
diff
changeset
|
828 |
c6ca5fe1505c
use wrappers for uniconv functions; style fixes for unicode conv fcns
John W. Eaton <jwe@octave.org>
parents:
23122
diff
changeset
|
829 return ovl (retval); |
23122
e310b5b6da6f
Add functions native2unicode and unicode2native (bug #49842).
Markus Mützel <markus.muetzel@gmx.de>
parents:
23084
diff
changeset
|
830 } |
23123
c6ca5fe1505c
use wrappers for uniconv functions; style fixes for unicode conv fcns
John W. Eaton <jwe@octave.org>
parents:
23122
diff
changeset
|
831 |
25412
922a93fc73ec
Add function to index characters in UTF-8 encoded strings.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25054
diff
changeset
|
832 DEFUN (unicode_idx, args, , |
922a93fc73ec
Add function to index characters in UTF-8 encoded strings.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25054
diff
changeset
|
833 doc: /* -*- texinfo -*- |
922a93fc73ec
Add function to index characters in UTF-8 encoded strings.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25054
diff
changeset
|
834 @deftypefn {} {@var{idx} =} unicode_idx (@var{str}) |
922a93fc73ec
Add function to index characters in UTF-8 encoded strings.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25054
diff
changeset
|
835 Return an array with the indices for each UTF-8 encoded character in @var{str}. |
922a93fc73ec
Add function to index characters in UTF-8 encoded strings.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25054
diff
changeset
|
836 |
922a93fc73ec
Add function to index characters in UTF-8 encoded strings.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25054
diff
changeset
|
837 @example |
922a93fc73ec
Add function to index characters in UTF-8 encoded strings.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25054
diff
changeset
|
838 @group |
922a93fc73ec
Add function to index characters in UTF-8 encoded strings.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25054
diff
changeset
|
839 unicode_idx ("aäbc") |
922a93fc73ec
Add function to index characters in UTF-8 encoded strings.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25054
diff
changeset
|
840 @result{} [1, 2, 2, 3, 4] |
922a93fc73ec
Add function to index characters in UTF-8 encoded strings.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25054
diff
changeset
|
841 @end group |
922a93fc73ec
Add function to index characters in UTF-8 encoded strings.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25054
diff
changeset
|
842 @end example |
922a93fc73ec
Add function to index characters in UTF-8 encoded strings.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25054
diff
changeset
|
843 |
922a93fc73ec
Add function to index characters in UTF-8 encoded strings.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25054
diff
changeset
|
844 @end deftypefn */) |
922a93fc73ec
Add function to index characters in UTF-8 encoded strings.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25054
diff
changeset
|
845 { |
922a93fc73ec
Add function to index characters in UTF-8 encoded strings.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25054
diff
changeset
|
846 int nargin = args.length (); |
922a93fc73ec
Add function to index characters in UTF-8 encoded strings.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25054
diff
changeset
|
847 |
922a93fc73ec
Add function to index characters in UTF-8 encoded strings.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25054
diff
changeset
|
848 if (nargin != 1) |
922a93fc73ec
Add function to index characters in UTF-8 encoded strings.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25054
diff
changeset
|
849 print_usage (); |
922a93fc73ec
Add function to index characters in UTF-8 encoded strings.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25054
diff
changeset
|
850 |
922a93fc73ec
Add function to index characters in UTF-8 encoded strings.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25054
diff
changeset
|
851 charNDArray str = args(0).xchar_array_value ("STR must be a string"); |
922a93fc73ec
Add function to index characters in UTF-8 encoded strings.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25054
diff
changeset
|
852 Array<octave_idx_type> p (dim_vector (str.ndims (), 1)); |
922a93fc73ec
Add function to index characters in UTF-8 encoded strings.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25054
diff
changeset
|
853 charNDArray str_p; |
922a93fc73ec
Add function to index characters in UTF-8 encoded strings.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25054
diff
changeset
|
854 if (str.ndims () > 1) |
922a93fc73ec
Add function to index characters in UTF-8 encoded strings.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25054
diff
changeset
|
855 { |
922a93fc73ec
Add function to index characters in UTF-8 encoded strings.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25054
diff
changeset
|
856 for (octave_idx_type i=0; i < str.ndims (); i++) |
922a93fc73ec
Add function to index characters in UTF-8 encoded strings.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25054
diff
changeset
|
857 p(i) = i; |
922a93fc73ec
Add function to index characters in UTF-8 encoded strings.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25054
diff
changeset
|
858 p(0) = 1; |
922a93fc73ec
Add function to index characters in UTF-8 encoded strings.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25054
diff
changeset
|
859 p(1) = 0; |
922a93fc73ec
Add function to index characters in UTF-8 encoded strings.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25054
diff
changeset
|
860 str_p = str.permute (p); |
922a93fc73ec
Add function to index characters in UTF-8 encoded strings.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25054
diff
changeset
|
861 } |
922a93fc73ec
Add function to index characters in UTF-8 encoded strings.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25054
diff
changeset
|
862 |
922a93fc73ec
Add function to index characters in UTF-8 encoded strings.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25054
diff
changeset
|
863 const uint8_t *src = reinterpret_cast<const uint8_t *> (str_p.data ()); |
922a93fc73ec
Add function to index characters in UTF-8 encoded strings.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25054
diff
changeset
|
864 octave_idx_type srclen = str.numel (); |
922a93fc73ec
Add function to index characters in UTF-8 encoded strings.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25054
diff
changeset
|
865 |
922a93fc73ec
Add function to index characters in UTF-8 encoded strings.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25054
diff
changeset
|
866 NDArray idx (str_p.dims ()); |
922a93fc73ec
Add function to index characters in UTF-8 encoded strings.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25054
diff
changeset
|
867 |
922a93fc73ec
Add function to index characters in UTF-8 encoded strings.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25054
diff
changeset
|
868 octave_idx_type u8_char_num = 1; |
922a93fc73ec
Add function to index characters in UTF-8 encoded strings.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25054
diff
changeset
|
869 for (octave_idx_type i = 0; i < srclen; u8_char_num++) |
922a93fc73ec
Add function to index characters in UTF-8 encoded strings.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25054
diff
changeset
|
870 { |
922a93fc73ec
Add function to index characters in UTF-8 encoded strings.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25054
diff
changeset
|
871 int mblen = octave_u8_strmblen_wrapper (src + i); |
922a93fc73ec
Add function to index characters in UTF-8 encoded strings.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25054
diff
changeset
|
872 if (mblen < 1) |
922a93fc73ec
Add function to index characters in UTF-8 encoded strings.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25054
diff
changeset
|
873 mblen = 1; |
922a93fc73ec
Add function to index characters in UTF-8 encoded strings.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25054
diff
changeset
|
874 for (octave_idx_type j = 0; j < mblen; j++) |
922a93fc73ec
Add function to index characters in UTF-8 encoded strings.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25054
diff
changeset
|
875 idx (i+j) = u8_char_num; |
922a93fc73ec
Add function to index characters in UTF-8 encoded strings.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25054
diff
changeset
|
876 i += mblen; |
922a93fc73ec
Add function to index characters in UTF-8 encoded strings.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25054
diff
changeset
|
877 } |
922a93fc73ec
Add function to index characters in UTF-8 encoded strings.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25054
diff
changeset
|
878 |
922a93fc73ec
Add function to index characters in UTF-8 encoded strings.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25054
diff
changeset
|
879 return ovl(str.ndims () > 1 ? idx.permute (p, true) : idx); |
922a93fc73ec
Add function to index characters in UTF-8 encoded strings.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25054
diff
changeset
|
880 } |
922a93fc73ec
Add function to index characters in UTF-8 encoded strings.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25054
diff
changeset
|
881 |
922a93fc73ec
Add function to index characters in UTF-8 encoded strings.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25054
diff
changeset
|
882 /* |
922a93fc73ec
Add function to index characters in UTF-8 encoded strings.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25054
diff
changeset
|
883 %!assert (unicode_idx (["aäou"; "Ä∞"]), [1 2 2 3 4; 5 5 6 6 6]); |
922a93fc73ec
Add function to index characters in UTF-8 encoded strings.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25054
diff
changeset
|
884 */ |
922a93fc73ec
Add function to index characters in UTF-8 encoded strings.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25054
diff
changeset
|
885 |
5690 | 886 DEFUN (list_in_columns, args, , |
21966
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
887 doc: /* -*- texinfo -*- |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
888 @deftypefn {} {} list_in_columns (@var{arg}, @var{width}, @var{prefix}) |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
889 Return a string containing the elements of @var{arg} listed in columns with |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
890 an overall maximum width of @var{width} and optional prefix @var{prefix}. |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
891 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
892 The argument @var{arg} must be a cell array of character strings or a |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
893 character array. |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
894 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
895 If @var{width} is not specified or is an empty matrix, or less than or equal |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
896 to zero, the width of the terminal screen is used. Newline characters are |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
897 used to break the lines in the output string. For example: |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
898 @c Set example in small font to prevent overfull line |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
899 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
900 @smallexample |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
901 @group |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
902 list_in_columns (@{"abc", "def", "ghijkl", "mnop", "qrs", "tuv"@}, 20) |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
903 @result{} abc mnop |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
904 def qrs |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
905 ghijkl tuv |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
906 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
907 whos ans |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
908 @result{} |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
909 Variables in the current scope: |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
910 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
911 Attr Name Size Bytes Class |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
912 ==== ==== ==== ===== ===== |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
913 ans 1x37 37 char |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
914 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
915 Total is 37 elements using 37 bytes |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
916 @end group |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
917 @end smallexample |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
918 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
919 @seealso{terminal_size} |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
920 @end deftypefn */) |
5690 | 921 { |
922 int nargin = args.length (); | |
923 | |
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
|
924 if (nargin < 1 || nargin > 3) |
20804
a6eaedd8bd75
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20704
diff
changeset
|
925 print_usage (); |
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
|
926 |
20990
fc9cca99b2de
Deprecate all_strings, replace with string_vector_value.
Rik <rik@octave.org>
parents:
20940
diff
changeset
|
927 string_vector s = args(0).xstring_vector_value ("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
|
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 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
|
930 |
23577
80c42f4cca13
maint: Deprecate is_empty and replace with isempty.
Rik <rik@octave.org>
parents:
23576
diff
changeset
|
931 if (nargin > 1 && ! args(1).isempty ()) |
20704
571508c1ed06
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20700
diff
changeset
|
932 width = args(1).xint_value ("list_in_columns: WIDTH must be an integer"); |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
933 |
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
|
934 std::string prefix; |
5690 | 935 |
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
|
936 if (nargin > 2) |
20700
68e3a747ca02
rename octave_value value extractors that accept error message args
John W. Eaton <jwe@octave.org>
parents:
20581
diff
changeset
|
937 prefix = args(2).xstring_value ("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
|
938 |
afa5e1d20671
* str-vec.h, str-vec.cc (string_vector::list_in_columns): Accept prefix.
John W. Eaton <jwe@octave.org>
parents:
14429
diff
changeset
|
939 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
|
940 |
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 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
|
942 |
20939
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
943 return ovl (buf.str ()); |
5690 | 944 } |
945 | |
807 | 946 /* |
14429
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
947 %!test |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
948 %! 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
|
949 %! 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
|
950 %! 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
|
951 %!test |
24461
0a034d34a686
Modify BIST tests to pass when string_fill_char is not ' '.
Rik <rik@octave.org>
parents:
24441
diff
changeset
|
952 %! input = char ("abc", "def", "ghijkl", "mnop", "qrs", "tuv"); |
14429
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
953 %! 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
|
954 %! 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
|
955 %!test |
24461
0a034d34a686
Modify BIST tests to pass when string_fill_char is not ' '.
Rik <rik@octave.org>
parents:
24441
diff
changeset
|
956 %! input = char ("abc", "def", "ghijkl", "mnop", "qrs", "tuv"); |
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
|
957 %! 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
|
958 %! 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
|
959 |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
960 %!error list_in_columns () |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
961 %!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
|
962 %!error list_in_columns (["abc", "def"], 20, " ", 3) |
20704
571508c1ed06
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20700
diff
changeset
|
963 %!error <list_in_columns: WIDTH must be an integer> 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
|
964 */ |