Mercurial > octave
annotate libinterp/corefcn/strfind.cc @ 27919:1891570abac8
update Octave Project Developers copyright for the new year
In files that have the "Octave Project Developers" copyright notice,
update for 2020.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Mon, 06 Jan 2020 22:29:51 -0500 |
parents | b442ec6dda5c |
children | bd51beb6205e |
rev | line source |
---|---|
10022 | 1 /* |
2 | |
27919
1891570abac8
update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents:
27918
diff
changeset
|
3 Copyright (C) 2009-2020 The Octave Project Developers |
27918
b442ec6dda5c
use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents:
27796
diff
changeset
|
4 |
b442ec6dda5c
use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents:
27796
diff
changeset
|
5 See the file COPYRIGHT.md in the top-level directory of this distribution |
b442ec6dda5c
use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents:
27796
diff
changeset
|
6 or <https://octave.org/COPYRIGHT.html/>. |
b442ec6dda5c
use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents:
27796
diff
changeset
|
7 |
10022 | 8 |
9 This file is part of Octave. | |
10 | |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23577
diff
changeset
|
11 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
|
12 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:
23577
diff
changeset
|
13 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
|
14 (at your option) any later version. |
10022 | 15 |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22407
diff
changeset
|
16 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
|
17 WITHOUT ANY WARRANTY; without even the implied warranty of |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22407
diff
changeset
|
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22407
diff
changeset
|
19 GNU General Public License for more details. |
10022 | 20 |
21 You should have received a copy of the GNU General Public License | |
22 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:
23577
diff
changeset
|
23 <https://www.gnu.org/licenses/>. |
10022 | 24 |
25 */ | |
26 | |
21724
aba2e6293dd8
use "#if ..." consistently instead of "#ifdef" and "#ifndef"
John W. Eaton <jwe@octave.org>
parents:
21547
diff
changeset
|
27 #if defined (HAVE_CONFIG_H) |
21301
40de9f8f23a6
Use '#include "config.h"' rather than <config.h>.
Rik <rik@octave.org>
parents:
21200
diff
changeset
|
28 # include "config.h" |
10022 | 29 #endif |
30 | |
31 #include <algorithm> | |
10053
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
32 #include <deque> |
15215
9020dddc925a
use std::numeric_limits for integer max and min values
John W. Eaton <jwe@octave.org>
parents:
15195
diff
changeset
|
33 #include <limits> |
9020dddc925a
use std::numeric_limits for integer max and min values
John W. Eaton <jwe@octave.org>
parents:
15195
diff
changeset
|
34 #include <string> |
10022 | 35 |
19269
65554f5847ac
don't include oct-locbuf.h in header files unnecessarily
John W. Eaton <jwe@octave.org>
parents:
19062
diff
changeset
|
36 #include "oct-locbuf.h" |
65554f5847ac
don't include oct-locbuf.h in header files unnecessarily
John W. Eaton <jwe@octave.org>
parents:
19062
diff
changeset
|
37 |
10022 | 38 #include "Cell.h" |
23454
30b6eccd6708
use builtin-defun-decls.h to ensure declarations of interpreter functions
John W. Eaton <jwe@octave.org>
parents:
23220
diff
changeset
|
39 #include "builtin-defun-decls.h" |
15039
e753177cde93
maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents:
14501
diff
changeset
|
40 #include "defun.h" |
23454
30b6eccd6708
use builtin-defun-decls.h to ensure declarations of interpreter functions
John W. Eaton <jwe@octave.org>
parents:
23220
diff
changeset
|
41 #include "errwarn.h" |
27130
396d17dcfb9f
don't include error.h in ov-base.h
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
42 #include "error.h" |
23454
30b6eccd6708
use builtin-defun-decls.h to ensure declarations of interpreter functions
John W. Eaton <jwe@octave.org>
parents:
23220
diff
changeset
|
43 #include "ov.h" |
10022 | 44 #include "unwind-prot.h" |
45 #include "utils.h" | |
46 | |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
47 // This allows safe indexing with char. |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
48 // In C++, char may be (and often is) signed! |
10022 | 49 #define ORD(ch) static_cast<unsigned char>(ch) |
15215
9020dddc925a
use std::numeric_limits for integer max and min values
John W. Eaton <jwe@octave.org>
parents:
15195
diff
changeset
|
50 #define TABSIZE (std::numeric_limits<unsigned char>::max () + 1) |
10022 | 51 |
52 // This is the quick search algorithm, as described at | |
53 // http://www-igm.univ-mlv.fr/~lecroq/string/node19.html | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11572
diff
changeset
|
54 static void |
10022 | 55 qs_preprocess (const Array<char>& needle, |
15215
9020dddc925a
use std::numeric_limits for integer max and min values
John W. Eaton <jwe@octave.org>
parents:
15195
diff
changeset
|
56 octave_idx_type *table) |
10022 | 57 { |
58 const char *x = needle.data (); | |
59 octave_idx_type m = needle.numel (); | |
60 | |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
61 for (octave_idx_type i = 0; i < TABSIZE; i++) |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
62 table[i] = m + 1; |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
63 for (octave_idx_type i = 0; i < m; i++) |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
64 table[ORD(x[i])] = m - i; |
10022 | 65 } |
66 | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11572
diff
changeset
|
67 static Array<octave_idx_type> |
10022 | 68 qs_search (const Array<char>& needle, |
69 const Array<char>& haystack, | |
15215
9020dddc925a
use std::numeric_limits for integer max and min values
John W. Eaton <jwe@octave.org>
parents:
15195
diff
changeset
|
70 const octave_idx_type *table, |
10053
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
71 bool overlaps = true) |
10022 | 72 { |
73 const char *x = needle.data (); | |
74 octave_idx_type m = needle.numel (); | |
75 const char *y = haystack.data (); | |
76 octave_idx_type n = haystack.numel (); | |
77 | |
10053
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
78 // We'll use deque because it typically has the most favorable properties for |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
79 // the operation we need. |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
80 std::deque<octave_idx_type> accum; |
10133
2e4fc7fdba15
optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents:
10086
diff
changeset
|
81 if (m == 1) |
2e4fc7fdba15
optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents:
10086
diff
changeset
|
82 { |
2e4fc7fdba15
optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents:
10086
diff
changeset
|
83 // Looking for a single character. |
2e4fc7fdba15
optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents:
10086
diff
changeset
|
84 for (octave_idx_type i = 0; i < n; i++) |
2e4fc7fdba15
optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents:
10086
diff
changeset
|
85 { |
2e4fc7fdba15
optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents:
10086
diff
changeset
|
86 if (y[i] == x[0]) |
2e4fc7fdba15
optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents:
10086
diff
changeset
|
87 accum.push_back (i); |
2e4fc7fdba15
optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents:
10086
diff
changeset
|
88 } |
2e4fc7fdba15
optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents:
10086
diff
changeset
|
89 } |
2e4fc7fdba15
optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents:
10086
diff
changeset
|
90 else if (m == 2) |
10022 | 91 { |
10133
2e4fc7fdba15
optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents:
10086
diff
changeset
|
92 // Two characters. |
2e4fc7fdba15
optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents:
10086
diff
changeset
|
93 if (overlaps) |
2e4fc7fdba15
optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents:
10086
diff
changeset
|
94 { |
2e4fc7fdba15
optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents:
10086
diff
changeset
|
95 for (octave_idx_type i = 0; i < n-1; i++) |
2e4fc7fdba15
optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents:
10086
diff
changeset
|
96 { |
2e4fc7fdba15
optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents:
10086
diff
changeset
|
97 if (y[i] == x[0] && y[i+1] == x[1]) |
2e4fc7fdba15
optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents:
10086
diff
changeset
|
98 accum.push_back (i); |
2e4fc7fdba15
optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents:
10086
diff
changeset
|
99 } |
2e4fc7fdba15
optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents:
10086
diff
changeset
|
100 } |
2e4fc7fdba15
optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents:
10086
diff
changeset
|
101 else |
2e4fc7fdba15
optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents:
10086
diff
changeset
|
102 { |
2e4fc7fdba15
optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents:
10086
diff
changeset
|
103 for (octave_idx_type i = 0; i < n-1; i++) |
2e4fc7fdba15
optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents:
10086
diff
changeset
|
104 { |
2e4fc7fdba15
optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents:
10086
diff
changeset
|
105 if (y[i] == x[0] && y[i+1] == x[1]) |
2e4fc7fdba15
optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents:
10086
diff
changeset
|
106 accum.push_back (i++); |
2e4fc7fdba15
optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents:
10086
diff
changeset
|
107 } |
2e4fc7fdba15
optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents:
10086
diff
changeset
|
108 } |
2e4fc7fdba15
optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents:
10086
diff
changeset
|
109 } |
2e4fc7fdba15
optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents:
10086
diff
changeset
|
110 else if (n >= m) |
2e4fc7fdba15
optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents:
10086
diff
changeset
|
111 { |
2e4fc7fdba15
optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents:
10086
diff
changeset
|
112 // General case. |
10022 | 113 octave_idx_type j = 0; |
114 | |
10053
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
115 if (overlaps) |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
116 { |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11572
diff
changeset
|
117 while (j < n - m) |
10053
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
118 { |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
119 if (std::equal (x, x + m, y + j)) |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
120 accum.push_back (j); |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
121 j += table[ORD(y[j + m])]; |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
122 } |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
123 } |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
124 else |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
125 { |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11572
diff
changeset
|
126 while (j < n - m) |
10053
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
127 { |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
128 if (std::equal (x, x + m, y + j)) |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
129 { |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
130 accum.push_back (j); |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
131 j += m; |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
132 } |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
133 else |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
134 j += table[ORD(y[j + m])]; |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
135 } |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
136 } |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
137 |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
138 if (j == n - m && std::equal (x, x + m, y + j)) |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
139 accum.push_back (j); |
10022 | 140 } |
141 | |
142 octave_idx_type nmatch = accum.size (); | |
10258 | 143 octave_idx_type one = 1; |
144 Array<octave_idx_type> result (dim_vector (std::min (one, nmatch), nmatch)); | |
10053
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
145 octave_idx_type k = 0; |
22862
e365e87371a3
maint: Use C++ range feature to simplify some for loops in libinterp/corefcn.
Rik <rik@octave.org>
parents:
22755
diff
changeset
|
146 for (const auto& idx : accum) |
e365e87371a3
maint: Use C++ range feature to simplify some for loops in libinterp/corefcn.
Rik <rik@octave.org>
parents:
22755
diff
changeset
|
147 result.xelem (k++) = idx; |
10022 | 148 |
149 return result; | |
150 } | |
151 | |
15039
e753177cde93
maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents:
14501
diff
changeset
|
152 DEFUN (strfind, 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
|
153 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
|
154 @deftypefn {} {@var{idx} =} strfind (@var{str}, @var{pattern}) |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
155 @deftypefnx {} {@var{idx} =} strfind (@var{cellstr}, @var{pattern}) |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
156 @deftypefnx {} {@var{idx} =} strfind (@dots{}, "overlaps", @var{val}) |
27315
86c5dd1283b6
Add support for "ForceCellOutput" option to strfind (bug #56699).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
27130
diff
changeset
|
157 @deftypefnx {} {@var{idx} =} strfind (@dots{}, "forcecelloutput", @var{val}) |
21966
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
158 Search for @var{pattern} in the string @var{str} and return the starting |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
159 index of every such occurrence in the vector @var{idx}. |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
160 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
161 If there is no such occurrence, or if @var{pattern} is longer than |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
162 @var{str}, or if @var{pattern} itself is empty, then @var{idx} is the empty |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
163 array @code{[]}. |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
164 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
165 The optional argument @qcode{"overlaps"} determines whether the pattern |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
166 can match at every position in @var{str} (true), or only for unique |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
167 occurrences of the complete pattern (false). The default is true. |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
168 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
169 If a cell array of strings @var{cellstr} is specified then @var{idx} is a |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
170 cell array of vectors, as specified above. |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
171 |
27315
86c5dd1283b6
Add support for "ForceCellOutput" option to strfind (bug #56699).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
27130
diff
changeset
|
172 The optional argument @qcode{"forcecelloutput"} forces @var{idx} to be |
86c5dd1283b6
Add support for "ForceCellOutput" option to strfind (bug #56699).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
27130
diff
changeset
|
173 returned as a cell array of vectors. The default is false. |
86c5dd1283b6
Add support for "ForceCellOutput" option to strfind (bug #56699).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
27130
diff
changeset
|
174 |
21966
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
175 Examples: |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
176 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
177 @example |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
178 @group |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
179 strfind ("abababa", "aba") |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
180 @result{} [1, 3, 5] |
27796
e7dd7d98a058
doc: grammarcheck .txi files before 6.1 release.
Rik <rik@octave.org>
parents:
27315
diff
changeset
|
181 @end group |
21966
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
182 |
27796
e7dd7d98a058
doc: grammarcheck .txi files before 6.1 release.
Rik <rik@octave.org>
parents:
27315
diff
changeset
|
183 @group |
21966
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
184 strfind ("abababa", "aba", "overlaps", false) |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
185 @result{} [1, 5] |
27796
e7dd7d98a058
doc: grammarcheck .txi files before 6.1 release.
Rik <rik@octave.org>
parents:
27315
diff
changeset
|
186 @end group |
21966
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
187 |
27796
e7dd7d98a058
doc: grammarcheck .txi files before 6.1 release.
Rik <rik@octave.org>
parents:
27315
diff
changeset
|
188 @group |
21966
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
189 strfind (@{"abababa", "bebebe", "ab"@}, "aba") |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
190 @result{} |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
191 @{ |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
192 [1,1] = |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
193 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
194 1 3 5 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
195 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
196 [1,2] = [](1x0) |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
197 [1,3] = [](1x0) |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
198 @} |
27796
e7dd7d98a058
doc: grammarcheck .txi files before 6.1 release.
Rik <rik@octave.org>
parents:
27315
diff
changeset
|
199 @end group |
27315
86c5dd1283b6
Add support for "ForceCellOutput" option to strfind (bug #56699).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
27130
diff
changeset
|
200 |
27796
e7dd7d98a058
doc: grammarcheck .txi files before 6.1 release.
Rik <rik@octave.org>
parents:
27315
diff
changeset
|
201 @group |
27315
86c5dd1283b6
Add support for "ForceCellOutput" option to strfind (bug #56699).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
27130
diff
changeset
|
202 strfind ("abababa", "aba", "forcecelloutput", true) |
86c5dd1283b6
Add support for "ForceCellOutput" option to strfind (bug #56699).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
27130
diff
changeset
|
203 @result{} |
86c5dd1283b6
Add support for "ForceCellOutput" option to strfind (bug #56699).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
27130
diff
changeset
|
204 @{ |
86c5dd1283b6
Add support for "ForceCellOutput" option to strfind (bug #56699).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
27130
diff
changeset
|
205 [1,1] = |
86c5dd1283b6
Add support for "ForceCellOutput" option to strfind (bug #56699).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
27130
diff
changeset
|
206 |
86c5dd1283b6
Add support for "ForceCellOutput" option to strfind (bug #56699).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
27130
diff
changeset
|
207 1 3 5 |
86c5dd1283b6
Add support for "ForceCellOutput" option to strfind (bug #56699).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
27130
diff
changeset
|
208 @} |
21966
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
209 @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
|
210 @end example |
25760
2ccad4396afc
findstr.m: Make m-file a legacy function.
Rik <rik@octave.org>
parents:
25646
diff
changeset
|
211 @seealso{regexp, regexpi, find} |
21966
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
212 @end deftypefn */) |
10022 | 213 { |
214 int nargin = args.length (); | |
20801
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
215 |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
216 if (nargin != 4 && nargin != 2) |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
217 print_usage (); |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
218 |
10677
21defab4207c
make strrep not skipping overlaps by default, allow an option in both strfind/strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
219 bool overlaps = true; |
27315
86c5dd1283b6
Add support for "ForceCellOutput" option to strfind (bug #56699).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
27130
diff
changeset
|
220 bool forcecelloutput = false; |
20801
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
221 if (nargin == 4) |
10677
21defab4207c
make strrep not skipping overlaps by default, allow an option in both strfind/strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
222 { |
20939
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
223 if (! args(2).is_string () || ! args(3).is_scalar_type ()) |
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
224 error ("strfind: invalid optional arguments"); |
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
225 |
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
226 std::string opt = args(2).string_value (); |
27315
86c5dd1283b6
Add support for "ForceCellOutput" option to strfind (bug #56699).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
27130
diff
changeset
|
227 std::transform (opt.begin (), opt.end (), opt.begin (), tolower); |
20801
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
228 |
20939
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
229 if (opt == "overlaps") |
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
230 overlaps = args(3).bool_value (); |
27315
86c5dd1283b6
Add support for "ForceCellOutput" option to strfind (bug #56699).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
27130
diff
changeset
|
231 else if (opt == "forcecelloutput") |
86c5dd1283b6
Add support for "ForceCellOutput" option to strfind (bug #56699).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
27130
diff
changeset
|
232 forcecelloutput = args(3).bool_value (); |
10677
21defab4207c
make strrep not skipping overlaps by default, allow an option in both strfind/strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
233 else |
20939
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
234 error ("strfind: unknown option: %s", opt.c_str ()); |
10677
21defab4207c
make strrep not skipping overlaps by default, allow an option in both strfind/strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
235 } |
10022 | 236 |
20939
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
237 octave_value retval; |
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
238 |
20801
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
239 octave_value argstr = args(0); |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
240 octave_value argpat = args(1); |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
241 |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
242 if (argpat.is_string ()) |
10022 | 243 { |
20801
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
244 Array<char> needle = argpat.char_array_value (); |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
245 OCTAVE_LOCAL_BUFFER (octave_idx_type, table, TABSIZE); |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
246 qs_preprocess (needle, table); |
10022 | 247 |
20801
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
248 if (argstr.is_string ()) |
27315
86c5dd1283b6
Add support for "ForceCellOutput" option to strfind (bug #56699).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
27130
diff
changeset
|
249 { |
86c5dd1283b6
Add support for "ForceCellOutput" option to strfind (bug #56699).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
27130
diff
changeset
|
250 if (argpat.isempty ()) |
86c5dd1283b6
Add support for "ForceCellOutput" option to strfind (bug #56699).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
27130
diff
changeset
|
251 // Return a null matrix for null pattern for MW compatibility |
86c5dd1283b6
Add support for "ForceCellOutput" option to strfind (bug #56699).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
27130
diff
changeset
|
252 retval = Matrix (); |
86c5dd1283b6
Add support for "ForceCellOutput" option to strfind (bug #56699).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
27130
diff
changeset
|
253 else |
86c5dd1283b6
Add support for "ForceCellOutput" option to strfind (bug #56699).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
27130
diff
changeset
|
254 retval = octave_value (qs_search (needle, |
86c5dd1283b6
Add support for "ForceCellOutput" option to strfind (bug #56699).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
27130
diff
changeset
|
255 argstr.char_array_value (), |
86c5dd1283b6
Add support for "ForceCellOutput" option to strfind (bug #56699).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
27130
diff
changeset
|
256 table, overlaps), |
86c5dd1283b6
Add support for "ForceCellOutput" option to strfind (bug #56699).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
27130
diff
changeset
|
257 true, true); |
86c5dd1283b6
Add support for "ForceCellOutput" option to strfind (bug #56699).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
27130
diff
changeset
|
258 if (forcecelloutput) |
86c5dd1283b6
Add support for "ForceCellOutput" option to strfind (bug #56699).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
27130
diff
changeset
|
259 retval = Cell (retval); |
86c5dd1283b6
Add support for "ForceCellOutput" option to strfind (bug #56699).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
27130
diff
changeset
|
260 } |
23576
00e518162fda
maint: Deprecate is_cell and replace with iscell.
Rik <rik@octave.org>
parents:
23454
diff
changeset
|
261 else if (argstr.iscell ()) |
20801
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
262 { |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
263 const Cell argsc = argstr.cell_value (); |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
264 Cell retc (argsc.dims ()); |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
265 octave_idx_type ns = argsc.numel (); |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
266 |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
267 for (octave_idx_type i = 0; i < ns; i++) |
10022 | 268 { |
20801
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
269 octave_value argse = argsc(i); |
20939
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
270 if (! argse.is_string ()) |
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
271 error ("strfind: each element of CELLSTR must be a string"); |
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
272 |
23577
80c42f4cca13
maint: Deprecate is_empty and replace with isempty.
Rik <rik@octave.org>
parents:
23576
diff
changeset
|
273 if (argpat.isempty ()) |
20939
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
274 retc(i) = Matrix (); |
20801
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
275 else |
20939
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
276 retc(i) = octave_value (qs_search (needle, |
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
277 argse.char_array_value (), |
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
278 table, overlaps), |
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
279 true, true); |
10022 | 280 } |
20801
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
281 |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
282 retval = retc; |
10022 | 283 } |
284 else | |
20801
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
285 error ("strfind: first argument must be a string or cell array of strings"); |
10022 | 286 } |
23576
00e518162fda
maint: Deprecate is_cell and replace with iscell.
Rik <rik@octave.org>
parents:
23454
diff
changeset
|
287 else if (argpat.iscell ()) |
25646
4d565baa475e
move libinterp/utils functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
288 retval = octave::do_simple_cellfun (Fstrfind, "strfind", args); |
10022 | 289 else |
20801
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
290 error ("strfind: PATTERN must be a string or cell array of strings"); |
10022 | 291 |
292 return retval; | |
293 } | |
294 | |
295 /* | |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
296 %!assert (strfind ("abababa", "aba"), [1, 3, 5]) |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
297 %!assert (strfind ("abababa", "aba", "overlaps", false), [1, 5]) |
27315
86c5dd1283b6
Add support for "ForceCellOutput" option to strfind (bug #56699).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
27130
diff
changeset
|
298 %!assert (strfind ("abababa", "aba", "forcecelloutput", false), [1, 3, 5]) |
86c5dd1283b6
Add support for "ForceCellOutput" option to strfind (bug #56699).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
27130
diff
changeset
|
299 %!assert (strfind ("abababa", "aba", "forcecelloutput", true), {[1, 3, 5]}) |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
300 %!assert (strfind ({"abababa", "bla", "bla"}, "a"), {[1, 3, 5, 7], 3, 3}) |
27315
86c5dd1283b6
Add support for "ForceCellOutput" option to strfind (bug #56699).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
27130
diff
changeset
|
301 %!assert (strfind ({"abababa", "bla", "bla"}, "a", "forcecelloutput", false), {[1, 3, 5, 7], 3, 3}) |
86c5dd1283b6
Add support for "ForceCellOutput" option to strfind (bug #56699).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
27130
diff
changeset
|
302 %!assert (strfind ({"abababa", "bla", "bla"}, "a", "forcecelloutput", true), {[1, 3, 5, 7], 3, 3}) |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
303 %!assert (strfind ("Linux _is_ user-friendly. It just isn't ignorant-friendly or idiot-friendly.", "friendly"), [17, 50, 68]) |
19386
f054112912df
Change strfind to return empty array for empty pattern for compatibility (bug #43649).
Rik <rik@octave.org>
parents:
19269
diff
changeset
|
304 %!assert (strfind ("abc", ""), []) |
f054112912df
Change strfind to return empty array for empty pattern for compatibility (bug #43649).
Rik <rik@octave.org>
parents:
19269
diff
changeset
|
305 %!assert (strfind ("abc", {"", "b", ""}), {[], 2, []}) |
f054112912df
Change strfind to return empty array for empty pattern for compatibility (bug #43649).
Rik <rik@octave.org>
parents:
19269
diff
changeset
|
306 %!assert (strfind ({"abc", "def"}, ""), {[], []}) |
10022 | 307 |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
308 %!error strfind () |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
309 %!error strfind ("foo", "bar", 1) |
19386
f054112912df
Change strfind to return empty array for empty pattern for compatibility (bug #43649).
Rik <rik@octave.org>
parents:
19269
diff
changeset
|
310 %!error <unknown option: foobar> strfind ("foo", 100, "foobar", 1) |
f054112912df
Change strfind to return empty array for empty pattern for compatibility (bug #43649).
Rik <rik@octave.org>
parents:
19269
diff
changeset
|
311 %!error <each element of CELLSTR must be a string> strfind ({"A", 1}, "foo") |
f054112912df
Change strfind to return empty array for empty pattern for compatibility (bug #43649).
Rik <rik@octave.org>
parents:
19269
diff
changeset
|
312 %!error <first argument must be a string> strfind (100, "foo") |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
313 %!error <PATTERN must be a string> strfind ("foo", 100) |
10022 | 314 */ |
10053
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
315 |
10074
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
316 static Array<char> |
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
317 qs_replace (const Array<char>& str, const Array<char>& pat, |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11572
diff
changeset
|
318 const Array<char>& rep, |
15215
9020dddc925a
use std::numeric_limits for integer max and min values
John W. Eaton <jwe@octave.org>
parents:
15195
diff
changeset
|
319 const octave_idx_type *table, |
10677
21defab4207c
make strrep not skipping overlaps by default, allow an option in both strfind/strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
320 bool overlaps = true) |
10074
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
321 { |
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
322 Array<char> ret = str; |
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
323 |
18100
6a71e5030df5
Follow coding convention of defining and initializing only 1 variable per line in liboctinterp.
Rik <rik@octave.org>
parents:
17787
diff
changeset
|
324 octave_idx_type siz = str.numel (); |
6a71e5030df5
Follow coding convention of defining and initializing only 1 variable per line in liboctinterp.
Rik <rik@octave.org>
parents:
17787
diff
changeset
|
325 octave_idx_type psiz = pat.numel (); |
6a71e5030df5
Follow coding convention of defining and initializing only 1 variable per line in liboctinterp.
Rik <rik@octave.org>
parents:
17787
diff
changeset
|
326 octave_idx_type rsiz = rep.numel (); |
10074
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
327 |
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
328 if (psiz != 0) |
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
329 { |
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
330 // Look up matches, without overlaps. |
10677
21defab4207c
make strrep not skipping overlaps by default, allow an option in both strfind/strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
331 const Array<octave_idx_type> idx = qs_search (pat, str, table, overlaps); |
10074
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
332 octave_idx_type nidx = idx.numel (); |
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
333 |
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
334 if (nidx) |
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
335 { |
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
336 // Compute result size. |
10677
21defab4207c
make strrep not skipping overlaps by default, allow an option in both strfind/strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
337 octave_idx_type retsiz; |
21defab4207c
make strrep not skipping overlaps by default, allow an option in both strfind/strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
338 if (overlaps) |
21defab4207c
make strrep not skipping overlaps by default, allow an option in both strfind/strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
339 { |
21defab4207c
make strrep not skipping overlaps by default, allow an option in both strfind/strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
340 retsiz = 0; |
21751
b571fc85953f
maint: Use two spaces after period to indicate sentence break.
Rik <rik@octave.org>
parents:
21724
diff
changeset
|
341 // OMG. Is this the "right answer" MW always looks for, or |
10677
21defab4207c
make strrep not skipping overlaps by default, allow an option in both strfind/strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
342 // someone was just lazy? |
21defab4207c
make strrep not skipping overlaps by default, allow an option in both strfind/strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
343 octave_idx_type k = 0; |
21defab4207c
make strrep not skipping overlaps by default, allow an option in both strfind/strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
344 for (octave_idx_type i = 0; i < nidx; i++) |
21defab4207c
make strrep not skipping overlaps by default, allow an option in both strfind/strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
345 { |
21defab4207c
make strrep not skipping overlaps by default, allow an option in both strfind/strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
346 octave_idx_type j = idx(i); |
21defab4207c
make strrep not skipping overlaps by default, allow an option in both strfind/strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
347 if (j >= k) |
21defab4207c
make strrep not skipping overlaps by default, allow an option in both strfind/strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
348 retsiz += j - k; |
21defab4207c
make strrep not skipping overlaps by default, allow an option in both strfind/strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
349 retsiz += rsiz; |
21defab4207c
make strrep not skipping overlaps by default, allow an option in both strfind/strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
350 k = j + psiz; |
21defab4207c
make strrep not skipping overlaps by default, allow an option in both strfind/strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
351 } |
21defab4207c
make strrep not skipping overlaps by default, allow an option in both strfind/strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
352 |
21defab4207c
make strrep not skipping overlaps by default, allow an option in both strfind/strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
353 retsiz += siz - k; |
21defab4207c
make strrep not skipping overlaps by default, allow an option in both strfind/strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
354 } |
21defab4207c
make strrep not skipping overlaps by default, allow an option in both strfind/strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
355 else |
21defab4207c
make strrep not skipping overlaps by default, allow an option in both strfind/strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
356 retsiz = siz + nidx * (rsiz - psiz); |
21defab4207c
make strrep not skipping overlaps by default, allow an option in both strfind/strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
357 |
18763
86818f2c4a92
Return 0x0 empty matrix from strrep for Matlab compatibility (bug #42341).
Rik <rik@octave.org>
parents:
18100
diff
changeset
|
358 if (retsiz == 0) |
86818f2c4a92
Return 0x0 empty matrix from strrep for Matlab compatibility (bug #42341).
Rik <rik@octave.org>
parents:
18100
diff
changeset
|
359 ret.clear (dim_vector (0, 0)); |
19597
db92e7e28e1f
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
19386
diff
changeset
|
360 else |
10074
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
361 { |
18763
86818f2c4a92
Return 0x0 empty matrix from strrep for Matlab compatibility (bug #42341).
Rik <rik@octave.org>
parents:
18100
diff
changeset
|
362 ret.clear (dim_vector (1, retsiz)); |
86818f2c4a92
Return 0x0 empty matrix from strrep for Matlab compatibility (bug #42341).
Rik <rik@octave.org>
parents:
18100
diff
changeset
|
363 const char *src = str.data (); |
86818f2c4a92
Return 0x0 empty matrix from strrep for Matlab compatibility (bug #42341).
Rik <rik@octave.org>
parents:
18100
diff
changeset
|
364 const char *reps = rep.data (); |
86818f2c4a92
Return 0x0 empty matrix from strrep for Matlab compatibility (bug #42341).
Rik <rik@octave.org>
parents:
18100
diff
changeset
|
365 char *dest = ret.fortran_vec (); |
86818f2c4a92
Return 0x0 empty matrix from strrep for Matlab compatibility (bug #42341).
Rik <rik@octave.org>
parents:
18100
diff
changeset
|
366 |
86818f2c4a92
Return 0x0 empty matrix from strrep for Matlab compatibility (bug #42341).
Rik <rik@octave.org>
parents:
18100
diff
changeset
|
367 octave_idx_type k = 0; |
86818f2c4a92
Return 0x0 empty matrix from strrep for Matlab compatibility (bug #42341).
Rik <rik@octave.org>
parents:
18100
diff
changeset
|
368 for (octave_idx_type i = 0; i < nidx; i++) |
86818f2c4a92
Return 0x0 empty matrix from strrep for Matlab compatibility (bug #42341).
Rik <rik@octave.org>
parents:
18100
diff
changeset
|
369 { |
86818f2c4a92
Return 0x0 empty matrix from strrep for Matlab compatibility (bug #42341).
Rik <rik@octave.org>
parents:
18100
diff
changeset
|
370 octave_idx_type j = idx(i); |
86818f2c4a92
Return 0x0 empty matrix from strrep for Matlab compatibility (bug #42341).
Rik <rik@octave.org>
parents:
18100
diff
changeset
|
371 if (j >= k) |
86818f2c4a92
Return 0x0 empty matrix from strrep for Matlab compatibility (bug #42341).
Rik <rik@octave.org>
parents:
18100
diff
changeset
|
372 dest = std::copy (src + k, src + j, dest); |
86818f2c4a92
Return 0x0 empty matrix from strrep for Matlab compatibility (bug #42341).
Rik <rik@octave.org>
parents:
18100
diff
changeset
|
373 dest = std::copy (reps, reps + rsiz, dest); |
86818f2c4a92
Return 0x0 empty matrix from strrep for Matlab compatibility (bug #42341).
Rik <rik@octave.org>
parents:
18100
diff
changeset
|
374 k = j + psiz; |
86818f2c4a92
Return 0x0 empty matrix from strrep for Matlab compatibility (bug #42341).
Rik <rik@octave.org>
parents:
18100
diff
changeset
|
375 } |
86818f2c4a92
Return 0x0 empty matrix from strrep for Matlab compatibility (bug #42341).
Rik <rik@octave.org>
parents:
18100
diff
changeset
|
376 |
86818f2c4a92
Return 0x0 empty matrix from strrep for Matlab compatibility (bug #42341).
Rik <rik@octave.org>
parents:
18100
diff
changeset
|
377 std::copy (src + k, src + siz, dest); |
10074
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
378 } |
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
379 } |
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
380 } |
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
381 |
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
382 return ret; |
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
383 } |
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
384 |
15039
e753177cde93
maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents:
14501
diff
changeset
|
385 DEFUN (strrep, 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
|
386 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
|
387 @deftypefn {} {@var{newstr} =} strrep (@var{str}, @var{ptn}, @var{rep}) |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
388 @deftypefnx {} {@var{newstr} =} strrep (@var{cellstr}, @var{ptn}, @var{rep}) |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
389 @deftypefnx {} {@var{newstr} =} strrep (@dots{}, "overlaps", @var{val}) |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
390 Replace all occurrences of the pattern @var{ptn} in the string @var{str} |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
391 with the string @var{rep} and return the result. |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
392 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
393 The optional argument @qcode{"overlaps"} determines whether the pattern |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
394 can match at every position in @var{str} (true), or only for unique |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
395 occurrences of the complete pattern (false). The default is true. |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
396 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
397 @var{s} may also be a cell array of strings, in which case the replacement |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
398 is done for each element and a cell array is returned. |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
399 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
400 Example: |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
401 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
402 @example |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
403 @group |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
404 strrep ("This is a test string", "is", "&%$") |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
405 @result{} "Th&%$ &%$ a test string" |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
406 @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
|
407 @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
|
408 |
25760
2ccad4396afc
findstr.m: Make m-file a legacy function.
Rik <rik@octave.org>
parents:
25646
diff
changeset
|
409 @seealso{regexprep, strfind} |
21966
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
410 @end deftypefn */) |
10053
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
411 { |
20801
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
412 int nargin = args.length (); |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
413 |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
414 if (nargin != 3 && nargin != 5) |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
415 print_usage (); |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
416 |
20939
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
417 bool overlaps = true; |
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
418 |
20801
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
419 if (nargin == 5) |
10677
21defab4207c
make strrep not skipping overlaps by default, allow an option in both strfind/strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
420 { |
20939
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
421 if (! args(3).is_string () || ! args(4).is_scalar_type ()) |
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
422 error ("strrep: invalid optional arguments"); |
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
423 |
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
424 std::string opt = args(3).string_value (); |
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
425 if (opt == "overlaps") |
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
426 overlaps = args(4).bool_value (); |
10677
21defab4207c
make strrep not skipping overlaps by default, allow an option in both strfind/strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
427 else |
20939
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
428 error ("strrep: unknown option: %s", opt.c_str ()); |
10677
21defab4207c
make strrep not skipping overlaps by default, allow an option in both strfind/strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
429 } |
10053
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
430 |
20939
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
431 octave_value retval; |
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
432 |
20801
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
433 octave_value argstr = args(0); |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
434 octave_value argpat = args(1); |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
435 octave_value argrep = args(2); |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
436 |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
437 if (argpat.is_string () && argrep.is_string ()) |
10053
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
438 { |
20801
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
439 const Array<char> pat = argpat.char_array_value (); |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
440 const Array<char> rep = argrep.char_array_value (); |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
441 |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
442 OCTAVE_LOCAL_BUFFER (octave_idx_type, table, TABSIZE); |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
443 qs_preprocess (pat, table); |
10074
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
444 |
20801
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
445 if (argstr.is_string ()) |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
446 retval = qs_replace (argstr.char_array_value (), pat, rep, |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
447 table, overlaps); |
23576
00e518162fda
maint: Deprecate is_cell and replace with iscell.
Rik <rik@octave.org>
parents:
23454
diff
changeset
|
448 else if (argstr.iscell ()) |
20801
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
449 { |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
450 const Cell argsc = argstr.cell_value (); |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
451 Cell retc (argsc.dims ()); |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
452 octave_idx_type ns = argsc.numel (); |
10053
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
453 |
20801
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
454 for (octave_idx_type i = 0; i < ns; i++) |
10053
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
455 { |
20801
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
456 octave_value argse = argsc(i); |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
457 if (argse.is_string ()) |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
458 retc(i) = qs_replace (argse.char_array_value (), pat, rep, |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
459 table, overlaps); |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
460 else |
20831
35241c4b696c
eliminate return statements after calls to error
John W. Eaton <jwe@octave.org>
parents:
20801
diff
changeset
|
461 error ("strrep: each element of S must be a string"); |
20801
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
462 } |
10053
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
463 |
20801
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
464 retval = retc; |
10053
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
465 } |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
466 else |
20801
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
467 error ("strrep: S must be a string or cell array of strings"); |
10053
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
468 } |
23576
00e518162fda
maint: Deprecate is_cell and replace with iscell.
Rik <rik@octave.org>
parents:
23454
diff
changeset
|
469 else if (argpat.iscell () || argrep.iscell ()) |
25646
4d565baa475e
move libinterp/utils functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
470 retval = octave::do_simple_cellfun (Fstrrep, "strrep", args); |
10053
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
471 else |
20801
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
472 error ("strrep: PTN and REP arguments must be strings or cell arrays of strings"); |
10053
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
473 |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
474 return retval; |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
475 } |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
476 |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
477 /* |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
478 %!assert (strrep ("This is a test string", "is", "&%$"), |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
479 %! "Th&%$ &%$ a test string") |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
480 %!assert (strrep ("abababc", "abab", "xyz"), "xyzxyzc") |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
481 %!assert (strrep ("abababc", "abab", "xyz", "overlaps", false), "xyzabc") |
10053
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
482 |
18763
86818f2c4a92
Return 0x0 empty matrix from strrep for Matlab compatibility (bug #42341).
Rik <rik@octave.org>
parents:
18100
diff
changeset
|
483 %!assert (size (strrep ("a", "a", "")), [0 0]) |
86818f2c4a92
Return 0x0 empty matrix from strrep for Matlab compatibility (bug #42341).
Rik <rik@octave.org>
parents:
18100
diff
changeset
|
484 |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
485 %!error strrep () |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
486 %!error strrep ("foo", "bar", 3, 4) |
10053
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
487 */ |