annotate src/DLD-FUNCTIONS/strfind.cc @ 14501:60e5cf354d80

Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions. * __contourc__.cc, __delaunayn__.cc, __dispatch__.cc, __dsearchn__.cc, __fltk_uigetfile__.cc, __glpk__.cc, __lin_interpn__.cc, __magick_read__.cc, __pchip_deriv__.cc, __qp__.cc, __voronoi__.cc, besselj.cc, betainc.cc, bsxfun.cc, cellfun.cc, chol.cc, conv2.cc, convhulln.cc, dassl.cc, det.cc, dlmread.cc, dmperm.cc, dot.cc, eig.cc, eigs.cc, fft.cc, fft2.cc, filter.cc, find.cc, gammainc.cc, gcd.cc, givens.cc, hess.cc, hex2num.cc, inv.cc, kron.cc, lookup.cc, lsode.cc, lu.cc, luinc.cc, matrix_type.cc, max.cc, mgorth.cc, nproc.cc, qr.cc, quad.cc, quadcc.cc, qz.cc, rand.cc, rcond.cc, regexp.cc, schur.cc, spparms.cc, sqrtm.cc, str2double.cc, strfind.cc, sub2ind.cc, svd.cc, syl.cc, time.cc, tril.cc, tsearch.cc: Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
author Rik <octave@nomad.inbox5.com>
date Tue, 27 Mar 2012 22:46:45 -0700
parents 97883071e8e4
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
10022
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
1 /*
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
2
14138
72c96de7a403 maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents: 12786
diff changeset
3 Copyright (C) 2009-2012 Jaroslav Hajek
11523
fd0a3ac60b0e update copyright notices
John W. Eaton <jwe@octave.org>
parents: 10840
diff changeset
4 Copyright (C) 2009-2010 VZLU Prague
10022
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
5
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
6 This file is part of Octave.
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
7
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
8 Octave is free software; you can redistribute it and/or modify it
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
9 under the terms of the GNU General Public License as published by the
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
10 Free Software Foundation; either version 3 of the License, or (at your
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
11 option) any later version.
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
12
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
13 Octave is distributed in the hope that it will be useful, but WITHOUT
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
16 for more details.
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
17
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
18 You should have received a copy of the GNU General Public License
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
19 along with Octave; see the file COPYING. If not, see
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
20 <http://www.gnu.org/licenses/>.
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
21
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
22 */
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
23
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
24 #ifdef HAVE_CONFIG_H
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
25 #include <config.h>
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
26 #endif
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
27
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
28 #include <string>
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
29 #include <climits>
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
30 #include <algorithm>
10053
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
31 #include <deque>
10022
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
32
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
33 #include "Cell.h"
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
34 #include "ov.h"
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
35 #include "defun-dld.h"
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
36 #include "unwind-prot.h"
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
37 #include "gripes.h"
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
38 #include "utils.h"
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
39
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
40 // This allows safe indexing with char. In C++, char may be (and often is) signed!
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
41 #define ORD(ch) static_cast<unsigned char>(ch)
10074
5e2b4b7967cc allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10053
diff changeset
42 #define TABSIZE (UCHAR_MAX + 1)
10022
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
43
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
44 // This is the quick search algorithm, as described at
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
45 // 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
46 static void
10022
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
47 qs_preprocess (const Array<char>& needle,
10074
5e2b4b7967cc allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10053
diff changeset
48 octave_idx_type table[TABSIZE])
10022
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
49 {
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
50 const char *x = needle.data ();
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
51 octave_idx_type m = needle.numel ();
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
52
12786
4e70c4e61c24 strfind.cc: Fix infinite loop when pattern contains 255 (Bug #33670).
Rik <octave@nomad.inbox5.com>
parents: 12483
diff changeset
53 for (octave_idx_type i = 0; i < TABSIZE; i++)
10022
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
54 table[i] = m + 1;
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
55 for (octave_idx_type i = 0; i < m; i++)
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
56 table[ORD(x[i])] = m - i;
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
57 }
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
58
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
59
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11572
diff changeset
60 static Array<octave_idx_type>
10022
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
61 qs_search (const Array<char>& needle,
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
62 const Array<char>& haystack,
10074
5e2b4b7967cc allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10053
diff changeset
63 const octave_idx_type table[TABSIZE],
10053
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
64 bool overlaps = true)
10022
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
65 {
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
66 const char *x = needle.data ();
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
67 octave_idx_type m = needle.numel ();
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
68 const char *y = haystack.data ();
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
69 octave_idx_type n = haystack.numel ();
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
70
10053
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
71 // 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
72 // the operation we need.
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
73 std::deque<octave_idx_type> accum;
10133
2e4fc7fdba15 optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents: 10086
diff changeset
74 if (m == 1)
2e4fc7fdba15 optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents: 10086
diff changeset
75 {
2e4fc7fdba15 optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents: 10086
diff changeset
76 // Looking for a single character.
2e4fc7fdba15 optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents: 10086
diff changeset
77 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
78 {
2e4fc7fdba15 optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents: 10086
diff changeset
79 if (y[i] == x[0])
2e4fc7fdba15 optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents: 10086
diff changeset
80 accum.push_back (i);
2e4fc7fdba15 optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents: 10086
diff changeset
81 }
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 else if (m == 2)
10022
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
84 {
10133
2e4fc7fdba15 optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents: 10086
diff changeset
85 // Two characters.
2e4fc7fdba15 optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents: 10086
diff changeset
86 if (overlaps)
2e4fc7fdba15 optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents: 10086
diff changeset
87 {
2e4fc7fdba15 optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents: 10086
diff changeset
88 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
89 {
2e4fc7fdba15 optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents: 10086
diff changeset
90 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
91 accum.push_back (i);
2e4fc7fdba15 optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents: 10086
diff changeset
92 }
2e4fc7fdba15 optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents: 10086
diff changeset
93 }
2e4fc7fdba15 optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents: 10086
diff changeset
94 else
2e4fc7fdba15 optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents: 10086
diff changeset
95 {
2e4fc7fdba15 optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents: 10086
diff changeset
96 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
97 {
2e4fc7fdba15 optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents: 10086
diff changeset
98 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
99 accum.push_back (i++);
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 }
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 else if (n >= m)
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 // General case.
10022
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
106 octave_idx_type j = 0;
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
107
10053
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
108 if (overlaps)
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
109 {
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11572
diff changeset
110 while (j < n - m)
10053
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
111 {
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
112 if (std::equal (x, x + m, y + j))
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
113 accum.push_back (j);
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
114 j += table[ORD(y[j + m])];
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
115 }
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
116 }
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
117 else
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
118 {
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11572
diff changeset
119 while (j < n - m)
10053
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
120 {
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
121 if (std::equal (x, x + m, y + j))
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 accum.push_back (j);
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
124 j += m;
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
125 }
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
126 else
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
127 j += table[ORD(y[j + m])];
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
128 }
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
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
131 if (j == n - m && std::equal (x, x + m, y + j))
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
132 accum.push_back (j);
10022
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
133 }
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
134
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
135 octave_idx_type nmatch = accum.size ();
10258
e317791645c4 64-bit fixes
John W. Eaton <jwe@octave.org>
parents: 10133
diff changeset
136 octave_idx_type one = 1;
e317791645c4 64-bit fixes
John W. Eaton <jwe@octave.org>
parents: 10133
diff changeset
137 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
138 octave_idx_type k = 0;
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11572
diff changeset
139 for (std::deque<octave_idx_type>::const_iterator iter = accum.begin ();
10053
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
140 iter != accum.end (); iter++)
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
141 {
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
142 result.xelem (k++) = *iter;
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
143 }
10022
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
144
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
145 return result;
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
146 }
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
147
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
148 DEFUN_DLD (strfind, args, ,
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
149 "-*- texinfo -*-\n\
10840
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10677
diff changeset
150 @deftypefn {Loadable Function} {@var{idx} =} strfind (@var{str}, @var{pattern})\n\
10022
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
151 @deftypefnx {Loadable Function} {@var{idx} =} strfind (@var{cellstr}, @var{pattern})\n\
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
152 Search for @var{pattern} in the string @var{str} and return the\n\
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
153 starting index of every such occurrence in the vector @var{idx}.\n\
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
154 If there is no such occurrence, or if @var{pattern} is longer\n\
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
155 than @var{str}, then @var{idx} is the empty array @code{[]}.\n\
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
156 \n\
11553
01f703952eff Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
157 If a cell array of strings @var{cellstr} is specified\n\
01f703952eff Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
158 then @var{idx} is a cell array of vectors, as specified\n\
10022
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
159 above. Examples:\n\
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
160 \n\
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
161 @example\n\
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
162 @group\n\
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
163 strfind (\"abababa\", \"aba\")\n\
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
164 @result{} [1, 3, 5]\n\
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
165 \n\
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
166 strfind (@{\"abababa\", \"bebebe\", \"ab\"@}, \"aba\")\n\
14360
97883071e8e4 doc: Correct off-by-1 spacings in all .cc docstrings
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
167 @result{}\n\
10022
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
168 @{\n\
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
169 [1,1] =\n\
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
170 \n\
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
171 1 3 5\n\
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
172 \n\
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
173 [1,2] = [](1x0)\n\
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
174 [1,3] = [](1x0)\n\
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
175 @}\n\
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
176 @end group\n\
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
177 @end example\n\
11572
7d6d8c1e471f Grammarcheck Texinfo for files in src directory.
Rik <octave@nomad.inbox5.com>
parents: 11553
diff changeset
178 @seealso{findstr, strmatch, regexp, regexpi, find}\n\
10022
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
179 @end deftypefn")
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
180 {
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
181 octave_value retval;
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
182 int nargin = args.length ();
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
183 bool overlaps = true;
21defab4207c make strrep not skipping overlaps by default, allow an option in both strfind/strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10521
diff changeset
184
21defab4207c make strrep not skipping overlaps by default, allow an option in both strfind/strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10521
diff changeset
185 if (nargin == 4 && args(2).is_string () && args(3).is_scalar_type ())
21defab4207c make strrep not skipping overlaps by default, allow an option in both strfind/strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10521
diff changeset
186 {
21defab4207c make strrep not skipping overlaps by default, allow an option in both strfind/strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10521
diff changeset
187 std::string opt = args(2).string_value ();
21defab4207c make strrep not skipping overlaps by default, allow an option in both strfind/strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10521
diff changeset
188 if (opt == "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
189 {
21defab4207c make strrep not skipping overlaps by default, allow an option in both strfind/strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10521
diff changeset
190 overlaps = args(3).bool_value ();
21defab4207c make strrep not skipping overlaps by default, allow an option in both strfind/strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10521
diff changeset
191 nargin = 2;
21defab4207c make strrep not skipping overlaps by default, allow an option in both strfind/strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10521
diff changeset
192 }
21defab4207c make strrep not skipping overlaps by default, allow an option in both strfind/strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10521
diff changeset
193 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
194 {
21defab4207c make strrep not skipping overlaps by default, allow an option in both strfind/strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10521
diff changeset
195 error ("strfind: unknown option: %s", opt.c_str ());
21defab4207c make strrep not skipping overlaps by default, allow an option in both strfind/strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10521
diff changeset
196 return retval;
21defab4207c make strrep not skipping overlaps by default, allow an option in both strfind/strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10521
diff changeset
197 }
21defab4207c make strrep not skipping overlaps by default, allow an option in both strfind/strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10521
diff changeset
198 }
10022
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
199
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
200 if (nargin == 2)
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
201 {
10086
76df75b10c80 allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents: 10074
diff changeset
202 octave_value argstr = args(0), argpat = args(1);
76df75b10c80 allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents: 10074
diff changeset
203 if (argpat.is_string ())
10022
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
204 {
10086
76df75b10c80 allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents: 10074
diff changeset
205 Array<char> needle = argpat.char_array_value ();
12786
4e70c4e61c24 strfind.cc: Fix infinite loop when pattern contains 255 (Bug #33670).
Rik <octave@nomad.inbox5.com>
parents: 12483
diff changeset
206 OCTAVE_LOCAL_BUFFER (octave_idx_type, table, TABSIZE);
10022
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
207 qs_preprocess (needle, table);
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
208
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
209 if (argstr.is_string ())
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11572
diff changeset
210 retval = octave_value (qs_search (needle, argstr.char_array_value (),
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11572
diff changeset
211 table, overlaps),
10053
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
212 true, true);
10022
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
213 else if (argstr.is_cell ())
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
214 {
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
215 const Cell argsc = argstr.cell_value ();
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
216 Cell retc (argsc.dims ());
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
217 octave_idx_type ns = argsc.numel ();
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
218
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
219 for (octave_idx_type i = 0; i < ns; i++)
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
220 {
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
221 octave_value argse = argsc(i);
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
222 if (argse.is_string ())
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11572
diff changeset
223 retc(i) = octave_value (qs_search (needle, argse.char_array_value (),
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11572
diff changeset
224 table, overlaps),
10053
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
225 true, true);
10022
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
226 else
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
227 {
11553
01f703952eff Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
228 error ("strfind: each element of CELLSTR must be a string");
10022
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
229 break;
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
230 }
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
231 }
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
232
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
233 retval = retc;
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
234 }
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
235 else
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
236 error ("strfind: first argument must be a string or cell array of strings");
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
237 }
10086
76df75b10c80 allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents: 10074
diff changeset
238 else if (argpat.is_cell ())
76df75b10c80 allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents: 10074
diff changeset
239 retval = do_simple_cellfun (Fstrfind, "strfind", args);
10022
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
240 else
11553
01f703952eff Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
241 error ("strfind: PATTERN must be a string or cell array of strings");
10022
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
242 }
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
243 else
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
244 print_usage ();
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
245
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
246 return retval;
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
247 }
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
248
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
249 /*
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
250 %!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
251 %!assert (strfind ("abababa", "aba", "overlaps", false), [1, 5])
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
252 %!assert (strfind ({"abababa", "bla", "bla"}, "a"), {[1, 3, 5, 7], 3, 3})
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
253 %!assert (strfind ("Linux _is_ user-friendly. It just isn't ignorant-friendly or idiot-friendly.", "friendly"), [17, 50, 68])
10022
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
254
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
255 %!error strfind ()
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
256 %!error strfind ("foo", "bar", 1)
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
257 %!error <PATTERN must be a string> strfind ("foo", 100)
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
258 %!error <first argument must be a string> strfind (100, "foo")
10022
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
259 */
10053
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
260
10074
5e2b4b7967cc allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10053
diff changeset
261 static Array<char>
5e2b4b7967cc allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10053
diff changeset
262 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
263 const Array<char>& rep,
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
264 const octave_idx_type table[TABSIZE],
21defab4207c make strrep not skipping overlaps by default, allow an option in both strfind/strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10521
diff changeset
265 bool overlaps = true)
10074
5e2b4b7967cc allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10053
diff changeset
266 {
5e2b4b7967cc allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10053
diff changeset
267 Array<char> ret = str;
5e2b4b7967cc allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10053
diff changeset
268
5e2b4b7967cc allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10053
diff changeset
269 octave_idx_type siz = str.numel (), psiz = pat.numel (), rsiz = rep.numel ();
5e2b4b7967cc allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10053
diff changeset
270
5e2b4b7967cc allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10053
diff changeset
271 if (psiz != 0)
5e2b4b7967cc allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10053
diff changeset
272 {
5e2b4b7967cc allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10053
diff changeset
273 // 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
274 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
275 octave_idx_type nidx = idx.numel ();
5e2b4b7967cc allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10053
diff changeset
276
5e2b4b7967cc allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10053
diff changeset
277 if (nidx)
5e2b4b7967cc allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10053
diff changeset
278 {
5e2b4b7967cc allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10053
diff changeset
279 // 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
280 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
281 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
282 {
21defab4207c make strrep not skipping overlaps by default, allow an option in both strfind/strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10521
diff changeset
283 retsiz = 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
284 // OMG. Is this the "right answer" MW always looks for, or
21defab4207c make strrep not skipping overlaps by default, allow an option in both strfind/strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10521
diff changeset
285 // 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
286 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
287 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
288 {
21defab4207c make strrep not skipping overlaps by default, allow an option in both strfind/strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10521
diff changeset
289 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
290 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
291 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
292 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
293 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
294 }
21defab4207c make strrep not skipping overlaps by default, allow an option in both strfind/strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10521
diff changeset
295
21defab4207c make strrep not skipping overlaps by default, allow an option in both strfind/strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10521
diff changeset
296 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
297 }
21defab4207c make strrep not skipping overlaps by default, allow an option in both strfind/strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10521
diff changeset
298 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
299 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
300
10074
5e2b4b7967cc allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10053
diff changeset
301 ret.clear (dim_vector (1, retsiz));
5e2b4b7967cc allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10053
diff changeset
302 const char *src = str.data (), *reps = rep.data ();
5e2b4b7967cc allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10053
diff changeset
303 char *dest = ret.fortran_vec ();
5e2b4b7967cc allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10053
diff changeset
304
5e2b4b7967cc allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10053
diff changeset
305 octave_idx_type k = 0;
5e2b4b7967cc allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10053
diff changeset
306 for (octave_idx_type i = 0; i < nidx; i++)
5e2b4b7967cc allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10053
diff changeset
307 {
5e2b4b7967cc allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10053
diff changeset
308 octave_idx_type j = idx(i);
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
309 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
310 dest = std::copy (src + k, src + j, dest);
10074
5e2b4b7967cc allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10053
diff changeset
311 dest = std::copy (reps, reps + rsiz, dest);
5e2b4b7967cc allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10053
diff changeset
312 k = j + psiz;
5e2b4b7967cc allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10053
diff changeset
313 }
5e2b4b7967cc allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10053
diff changeset
314
5e2b4b7967cc allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10053
diff changeset
315 std::copy (src + k, src + siz, dest);
5e2b4b7967cc allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10053
diff changeset
316 }
5e2b4b7967cc allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10053
diff changeset
317 }
5e2b4b7967cc allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10053
diff changeset
318
5e2b4b7967cc allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10053
diff changeset
319 return ret;
5e2b4b7967cc allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10053
diff changeset
320 }
5e2b4b7967cc allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10053
diff changeset
321
10053
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
322 DEFUN_DLD (strrep, args, ,
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
323 "-*- texinfo -*-\n\
11553
01f703952eff Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
324 @deftypefn {Loadable Function} {} strrep (@var{s}, @var{ptn}, @var{rep})\n\
01f703952eff Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
325 @deftypefnx {Loadable Function} {} strrep (@var{s}, @var{ptn}, @var{rep}, \"overlaps\", @var{o})\n\
01f703952eff Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
326 Replace all occurrences of the substring @var{ptn} in the string @var{s}\n\
01f703952eff Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
327 with the string @var{rep} and return the result. For example:\n\
10053
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
328 \n\
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
329 @example\n\
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
330 @group\n\
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
331 strrep (\"This is a test string\", \"is\", \"&%$\")\n\
14360
97883071e8e4 doc: Correct off-by-1 spacings in all .cc docstrings
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
332 @result{} \"Th&%$ &%$ a test string\"\n\
10053
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
333 @end group\n\
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
334 @end example\n\
10074
5e2b4b7967cc allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10053
diff changeset
335 \n\
5e2b4b7967cc allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10053
diff changeset
336 @var{s} may also be a cell array of strings, in which case the replacement is\n\
5e2b4b7967cc allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10053
diff changeset
337 done for each element and a cell array is returned.\n\
10053
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
338 @seealso{regexprep, strfind, findstr}\n\
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
339 @end deftypefn")
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
340 {
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
341 octave_value retval;
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
342 int nargin = args.length ();
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
343 bool overlaps = true;
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
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 if (nargin == 5 && args(3).is_string () && args(4).is_scalar_type ())
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 {
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 std::string opt = args(3).string_value ();
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 if (opt == "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
349 {
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 overlaps = args(4).bool_value ();
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 nargin = 3;
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 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
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 error ("strrep: unknown option: %s", opt.c_str ());
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 return retval;
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 }
21defab4207c make strrep not skipping overlaps by default, allow an option in both strfind/strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10521
diff changeset
358 }
10053
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
359
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
360 if (nargin == 3)
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
361 {
10086
76df75b10c80 allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents: 10074
diff changeset
362 octave_value argstr = args(0), argpat = args(1), argrep = args(2);
76df75b10c80 allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents: 10074
diff changeset
363 if (argpat.is_string () && argrep.is_string ())
10053
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
364 {
10086
76df75b10c80 allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents: 10074
diff changeset
365 const Array<char> pat = argpat.char_array_value ();
76df75b10c80 allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents: 10074
diff changeset
366 const Array<char> rep = argrep.char_array_value ();
10074
5e2b4b7967cc allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10053
diff changeset
367
12786
4e70c4e61c24 strfind.cc: Fix infinite loop when pattern contains 255 (Bug #33670).
Rik <octave@nomad.inbox5.com>
parents: 12483
diff changeset
368 OCTAVE_LOCAL_BUFFER (octave_idx_type, table, TABSIZE);
10074
5e2b4b7967cc allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10053
diff changeset
369 qs_preprocess (pat, table);
10053
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
370
10074
5e2b4b7967cc allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10053
diff changeset
371 if (argstr.is_string ())
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
372 retval = qs_replace (argstr.char_array_value (), pat, rep, table, overlaps);
10074
5e2b4b7967cc allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10053
diff changeset
373 else if (argstr.is_cell ())
10053
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
374 {
10074
5e2b4b7967cc allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10053
diff changeset
375 const Cell argsc = argstr.cell_value ();
5e2b4b7967cc allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10053
diff changeset
376 Cell retc (argsc.dims ());
5e2b4b7967cc allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10053
diff changeset
377 octave_idx_type ns = argsc.numel ();
10053
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
378
10074
5e2b4b7967cc allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10053
diff changeset
379 for (octave_idx_type i = 0; i < ns; i++)
10053
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
380 {
10074
5e2b4b7967cc allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10053
diff changeset
381 octave_value argse = argsc(i);
5e2b4b7967cc allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10053
diff changeset
382 if (argse.is_string ())
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
383 retc(i) = qs_replace (argse.char_array_value (), pat, rep, table, overlaps);
10074
5e2b4b7967cc allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10053
diff changeset
384 else
10053
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
385 {
11553
01f703952eff Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
386 error ("strrep: each element of S must be a string");
10074
5e2b4b7967cc allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10053
diff changeset
387 break;
10053
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
388 }
10074
5e2b4b7967cc allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10053
diff changeset
389 }
10053
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
390
10074
5e2b4b7967cc allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10053
diff changeset
391 retval = retc;
10053
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
392 }
10074
5e2b4b7967cc allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10053
diff changeset
393 else
12483
7a5aacf65f81 Rewrite error strings in src/ to use variables named in documentation.
Rik <octave@nomad.inbox5.com>
parents: 11586
diff changeset
394 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
395 }
10086
76df75b10c80 allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents: 10074
diff changeset
396 else if (argpat.is_cell () || argrep.is_cell ())
76df75b10c80 allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents: 10074
diff changeset
397 retval = do_simple_cellfun (Fstrrep, "strrep", args);
10053
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
398 else
12483
7a5aacf65f81 Rewrite error strings in src/ to use variables named in documentation.
Rik <octave@nomad.inbox5.com>
parents: 11586
diff changeset
399 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
400 }
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
401 else
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
402 print_usage ();
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
403
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
404 return retval;
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
405 }
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
406
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
407 /*
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
408 %!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
409 %! "Th&%$ &%$ a test string")
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
410 %!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
411 %!assert (strrep ("abababc", "abab", "xyz", "overlaps", false), "xyzabc")
10053
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
412
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
413 %!error strrep ()
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
414 %!error strrep ("foo", "bar", 3, 4)
10053
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
415 */