Mercurial > octave
annotate libinterp/corefcn/strfind.cc @ 21301:40de9f8f23a6
Use '#include "config.h"' rather than <config.h>.
* mk-octave-config-h.sh, mk-opts.pl, Backend.cc, BaseControl.cc,
ButtonControl.cc, Canvas.cc, CheckBoxControl.cc, Container.cc, ContextMenu.cc,
EditControl.cc, Figure.cc, FigureWindow.cc, GLCanvas.cc, KeyMap.cc,
ListBoxControl.cc, Logger.cc, Menu.cc, MouseModeActionGroup.cc, Object.cc,
ObjectFactory.cc, ObjectProxy.cc, Panel.cc, PopupMenuControl.cc,
PushButtonControl.cc, PushTool.cc, QtHandlesUtils.cc, RadioButtonControl.cc,
SliderControl.cc, TextControl.cc, TextEdit.cc, ToggleButtonControl.cc,
ToggleTool.cc, ToolBar.cc, ToolBarButton.cc, __init_qt__.cc,
annotation-dialog.cc, gl-select.cc, module.mk, kpty.cpp, color-picker.cc,
dialog.cc, documentation-dock-widget.cc, files-dock-widget.cc,
find-files-dialog.cc, find-files-model.cc, history-dock-widget.cc,
file-editor-tab.cc, file-editor-tab.h, file-editor.cc, find-dialog.cc,
marker.cc, octave-qscintilla.cc, octave-txt-lexer.cc, main-window.cc,
octave-cmd.cc, octave-dock-widget.cc, octave-gui.cc, octave-interpreter.cc,
octave-qt-link.cc, parser.cc, webinfo.cc, resource-manager.cc,
settings-dialog.cc, shortcut-manager.cc, terminal-dock-widget.cc,
thread-manager.cc, welcome-wizard.cc, workspace-model.cc, workspace-view.cc,
build-env-features.sh, build-env.in.cc, Cell.cc, __contourc__.cc,
__dispatch__.cc, __dsearchn__.cc, __ichol__.cc, __ilu__.cc, __lin_interpn__.cc,
__pchip_deriv__.cc, __qp__.cc, balance.cc, besselj.cc, betainc.cc, bitfcns.cc,
bsxfun.cc, c-file-ptr-stream.cc, cdisplay.c, cellfun.cc, coct-hdf5-types.c,
colloc.cc, comment-list.cc, conv2.cc, daspk.cc, dasrt.cc, dassl.cc, data.cc,
debug.cc, defaults.cc, defun.cc, det.cc, dirfns.cc, display.cc, dlmread.cc,
dot.cc, dynamic-ld.cc, eig.cc, ellipj.cc, error.cc, errwarn.cc, event-queue.cc,
fft.cc, fft2.cc, fftn.cc, file-io.cc, filter.cc, find.cc, ft-text-renderer.cc,
gammainc.cc, gcd.cc, getgrent.cc, getpwent.cc, getrusage.cc, givens.cc,
gl-render.cc, gl2ps-print.cc, graphics.cc, gripes.cc, hash.cc, help.cc,
hess.cc, hex2num.cc, hook-fcn.cc, input.cc, inv.cc, jit-ir.cc, jit-typeinfo.cc,
jit-util.cc, kron.cc, load-path.cc, load-save.cc, lookup.cc,
ls-ascii-helper.cc, ls-hdf5.cc, ls-mat-ascii.cc, ls-mat4.cc, ls-mat5.cc,
ls-oct-binary.cc, ls-oct-text.cc, ls-utils.cc, lsode.cc, lu.cc, luinc.cc,
mappers.cc, matrix_type.cc, max.cc, mex.cc, mgorth.cc, nproc.cc,
oct-errno.in.cc, oct-fstrm.cc, oct-hdf5-types.cc, oct-hist.cc, oct-iostrm.cc,
oct-lvalue.cc, oct-map.cc, oct-prcstrm.cc, oct-procbuf.cc, oct-stream.cc,
oct-strstrm.cc, oct-tex-lexer.in.ll, oct-tex-parser.in.yy, octave-link.cc,
ordschur.cc, pager.cc, pinv.cc, pr-output.cc, procstream.cc, profiler.cc,
psi.cc, pt-jit.cc, quad.cc, quadcc.cc, qz.cc, rand.cc, rcond.cc, regexp.cc,
schur.cc, sighandlers.cc, siglist.c, sparse-xdiv.cc, sparse-xpow.cc, sparse.cc,
spparms.cc, sqrtm.cc, str2double.cc, strfind.cc, strfns.cc, sub2ind.cc, svd.cc,
sylvester.cc, symtab.cc, syscalls.cc, sysdep.cc, text-renderer.cc, time.cc,
toplev.cc, tril.cc, tsearch.cc, txt-eng.cc, typecast.cc, urlwrite.cc, utils.cc,
variables.cc, xdiv.cc, xgl2ps.c, xnorm.cc, xpow.cc, zfstream.cc,
__delaunayn__.cc, __eigs__.cc, __fltk_uigetfile__.cc, __glpk__.cc,
__init_fltk__.cc, __init_gnuplot__.cc, __magick_read__.cc, __osmesa_print__.cc,
__voronoi__.cc, amd.cc, audiodevinfo.cc, audioread.cc, ccolamd.cc, chol.cc,
colamd.cc, convhulln.cc, dmperm.cc, fftw.cc, qr.cc, symbfact.cc, symrcm.cc,
mkbuiltins, mkops, ov-base-diag.cc, ov-base-int.cc, ov-base-mat.cc,
ov-base-scalar.cc, ov-base-sparse.cc, ov-base.cc, ov-bool-mat.cc,
ov-bool-sparse.cc, ov-bool.cc, ov-builtin.cc, ov-cell.cc, ov-ch-mat.cc,
ov-class.cc, ov-classdef.cc, ov-colon.cc, ov-complex.cc, ov-cs-list.cc,
ov-cx-diag.cc, ov-cx-mat.cc, ov-cx-sparse.cc, ov-dld-fcn.cc, ov-fcn-handle.cc,
ov-fcn-inline.cc, ov-fcn.cc, ov-float.cc, ov-flt-complex.cc, ov-flt-cx-diag.cc,
ov-flt-cx-mat.cc, ov-flt-re-diag.cc, ov-flt-re-mat.cc, ov-int16.cc,
ov-int32.cc, ov-int64.cc, ov-int8.cc, ov-java.cc, ov-lazy-idx.cc,
ov-mex-fcn.cc, ov-null-mat.cc, ov-oncleanup.cc, ov-perm.cc, ov-range.cc,
ov-re-diag.cc, ov-re-mat.cc, ov-re-sparse.cc, ov-scalar.cc, ov-str-mat.cc,
ov-struct.cc, ov-typeinfo.cc, ov-uint16.cc, ov-uint32.cc, ov-uint64.cc,
ov-uint8.cc, ov-usr-fcn.cc, ov.cc, ovl.cc, octave.cc, op-b-b.cc, op-b-bm.cc,
op-b-sbm.cc, op-bm-b.cc, op-bm-bm.cc, op-bm-sbm.cc, op-cdm-cdm.cc, op-cell.cc,
op-chm.cc, op-class.cc, op-cm-cm.cc, op-cm-cs.cc, op-cm-m.cc, op-cm-s.cc,
op-cm-scm.cc, op-cm-sm.cc, op-cs-cm.cc, op-cs-cs.cc, op-cs-m.cc, op-cs-s.cc,
op-cs-scm.cc, op-cs-sm.cc, op-dm-dm.cc, op-dm-scm.cc, op-dm-sm.cc,
op-dm-template.cc, op-dms-template.cc, op-double-conv.cc, op-fcdm-fcdm.cc,
op-fcdm-fdm.cc, op-fcm-fcm.cc, op-fcm-fcs.cc, op-fcm-fm.cc, op-fcm-fs.cc,
op-fcn.cc, op-fcs-fcm.cc, op-fcs-fcs.cc, op-fcs-fm.cc, op-fcs-fs.cc,
op-fdm-fdm.cc, op-float-conv.cc, op-fm-fcm.cc, op-fm-fcs.cc, op-fm-fm.cc,
op-fm-fs.cc, op-fs-fcm.cc, op-fs-fcs.cc, op-fs-fm.cc, op-fs-fs.cc,
op-i16-i16.cc, op-i32-i32.cc, op-i64-i64.cc, op-i8-i8.cc, op-int-concat.cc,
op-int-conv.cc, op-m-cm.cc, op-m-cs.cc, op-m-m.cc, op-m-s.cc, op-m-scm.cc,
op-m-sm.cc, op-pm-pm.cc, op-pm-scm.cc, op-pm-sm.cc, op-pm-template.cc,
op-range.cc, op-s-cm.cc, op-s-cs.cc, op-s-m.cc, op-s-s.cc, op-s-scm.cc,
op-s-sm.cc, op-sbm-b.cc, op-sbm-bm.cc, op-sbm-sbm.cc, op-scm-cm.cc,
op-scm-cs.cc, op-scm-m.cc, op-scm-s.cc, op-scm-scm.cc, op-scm-sm.cc,
op-sm-cm.cc, op-sm-cs.cc, op-sm-m.cc, op-sm-s.cc, op-sm-scm.cc, op-sm-sm.cc,
op-str-m.cc, op-str-s.cc, op-str-str.cc, op-struct.cc, op-ui16-ui16.cc,
op-ui32-ui32.cc, op-ui64-ui64.cc, op-ui8-ui8.cc, lex.ll, oct-parse.in.yy,
pt-arg-list.cc, pt-array-list.cc, pt-assign.cc, pt-binop.cc, pt-bp.cc,
pt-cbinop.cc, pt-cell.cc, pt-check.cc, pt-classdef.cc, pt-cmd.cc, pt-colon.cc,
pt-const.cc, pt-decl.cc, pt-eval.cc, pt-except.cc, pt-exp.cc, pt-fcn-handle.cc,
pt-funcall.cc, pt-id.cc, pt-idx.cc, pt-jump.cc, pt-loop.cc, pt-mat.cc,
pt-misc.cc, pt-pr-code.cc, pt-select.cc, pt-stmt.cc, pt-unop.cc, pt.cc,
token.cc, Array-jit.cc, Array-os.cc, Array-sym.cc, Array-tc.cc, version.cc,
Array-C.cc, Array-b.cc, Array-ch.cc, Array-d.cc, Array-f.cc, Array-fC.cc,
Array-i.cc, Array-idx-vec.cc, Array-s.cc, Array-str.cc, Array-util.cc,
Array-voidp.cc, Array.cc, CColVector.cc, CDiagMatrix.cc, CMatrix.cc,
CNDArray.cc, CRowVector.cc, CSparse.cc, DiagArray2.cc, MArray-C.cc,
MArray-d.cc, MArray-f.cc, MArray-fC.cc, MArray-i.cc, MArray-s.cc, MArray.cc,
MDiagArray2.cc, MSparse-C.cc, MSparse-d.cc, MatrixType.cc, PermMatrix.cc,
Range.cc, Sparse-C.cc, Sparse-b.cc, Sparse-d.cc, Sparse.cc, boolMatrix.cc,
boolNDArray.cc, boolSparse.cc, chMatrix.cc, chNDArray.cc, dColVector.cc,
dDiagMatrix.cc, dMatrix.cc, dNDArray.cc, dRowVector.cc, dSparse.cc,
dim-vector.cc, fCColVector.cc, fCDiagMatrix.cc, fCMatrix.cc, fCNDArray.cc,
fCRowVector.cc, fColVector.cc, fDiagMatrix.cc, fMatrix.cc, fNDArray.cc,
fRowVector.cc, idx-vector.cc, int16NDArray.cc, int32NDArray.cc,
int64NDArray.cc, int8NDArray.cc, intNDArray.cc, uint16NDArray.cc,
uint32NDArray.cc, uint64NDArray.cc, uint8NDArray.cc, blaswrap.c, cquit.c,
f77-extern.cc, f77-fcn.c, lo-error.c, quit.cc, CollocWt.cc, DASPK.cc, DASRT.cc,
DASSL.cc, EIG.cc, LSODE.cc, ODES.cc, Quad.cc, aepbalance.cc, chol.cc,
eigs-base.cc, fEIG.cc, gepbalance.cc, hess.cc, lo-mappers.cc, lo-specfun.cc,
lu.cc, oct-convn.cc, oct-fftw.cc, oct-norm.cc, oct-rand.cc, oct-spparms.cc,
qr.cc, qrp.cc, randgamma.c, randmtzig.c, randpoisson.c, schur.cc,
sparse-chol.cc, sparse-dmsolve.cc, sparse-lu.cc, sparse-qr.cc, svd.cc,
mk-ops.awk, dir-ops.cc, file-ops.cc, file-stat.cc, lo-sysdep.cc, mach-info.cc,
oct-env.cc, oct-group.cc, oct-passwd.cc, oct-syscalls.cc, oct-time.cc,
oct-uname.cc, cmd-edit.cc, cmd-hist.cc, data-conv.cc, f2c-main.c,
glob-match.cc, kpse.cc, lo-array-errwarn.cc, lo-array-gripes.cc, lo-cutils.c,
lo-ieee.cc, lo-regexp.cc, lo-utils.cc, oct-base64.cc, oct-glob.cc,
oct-inttypes.cc, oct-locbuf.cc, oct-mutex.cc, oct-rl-edit.c, oct-rl-hist.c,
oct-shlib.cc, oct-sort.cc, pathsearch.cc, singleton-cleanup.cc, sparse-sort.cc,
sparse-util.cc, str-vec.cc, unwind-prot.cc, url-transfer.cc,
display-available.c, main-cli.cc, main-gui.cc, main.in.cc, mkoctfile.in.cc,
octave-config.in.cc:
Use '#include "config.h"' rather than <config.h>.
author | Rik <rik@octave.org> |
---|---|
date | Thu, 18 Feb 2016 13:34:50 -0800 |
parents | fcac5dbbf9ed |
children | ad0599a0acc6 |
rev | line source |
---|---|
10022 | 1 /* |
2 | |
19697
4197fc428c7d
maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents:
19597
diff
changeset
|
3 Copyright (C) 2009-2015 Jaroslav Hajek |
11523 | 4 Copyright (C) 2009-2010 VZLU Prague |
10022 | 5 |
6 This file is part of Octave. | |
7 | |
8 Octave is free software; you can redistribute it and/or modify it | |
9 under the terms of the GNU General Public License as published by the | |
10 Free Software Foundation; either version 3 of the License, or (at your | |
11 option) any later version. | |
12 | |
13 Octave is distributed in the hope that it will be useful, but WITHOUT | |
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
16 for more details. | |
17 | |
18 You should have received a copy of the GNU General Public License | |
19 along with Octave; see the file COPYING. If not, see | |
20 <http://www.gnu.org/licenses/>. | |
21 | |
22 */ | |
23 | |
24 #ifdef HAVE_CONFIG_H | |
21301
40de9f8f23a6
Use '#include "config.h"' rather than <config.h>.
Rik <rik@octave.org>
parents:
21200
diff
changeset
|
25 # include "config.h" |
10022 | 26 #endif |
27 | |
28 #include <algorithm> | |
10053
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
29 #include <deque> |
15215
9020dddc925a
use std::numeric_limits for integer max and min values
John W. Eaton <jwe@octave.org>
parents:
15195
diff
changeset
|
30 #include <limits> |
9020dddc925a
use std::numeric_limits for integer max and min values
John W. Eaton <jwe@octave.org>
parents:
15195
diff
changeset
|
31 #include <string> |
10022 | 32 |
19269
65554f5847ac
don't include oct-locbuf.h in header files unnecessarily
John W. Eaton <jwe@octave.org>
parents:
19062
diff
changeset
|
33 #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
|
34 |
10022 | 35 #include "Cell.h" |
36 #include "ov.h" | |
15039
e753177cde93
maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents:
14501
diff
changeset
|
37 #include "defun.h" |
10022 | 38 #include "unwind-prot.h" |
21100
e39e05d90788
Switch gripe_XXX to either err_XXX or warn_XXX naming scheme.
Rik <rik@octave.org>
parents:
20939
diff
changeset
|
39 #include "errwarn.h" |
10022 | 40 #include "utils.h" |
41 | |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
42 // This allows safe indexing with char. |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
43 // In C++, char may be (and often is) signed! |
10022 | 44 #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
|
45 #define TABSIZE (std::numeric_limits<unsigned char>::max () + 1) |
10022 | 46 |
47 // This is the quick search algorithm, as described at | |
48 // 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
|
49 static void |
10022 | 50 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
|
51 octave_idx_type *table) |
10022 | 52 { |
53 const char *x = needle.data (); | |
54 octave_idx_type m = needle.numel (); | |
55 | |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
56 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
|
57 table[i] = m + 1; |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
58 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
|
59 table[ORD(x[i])] = m - i; |
10022 | 60 } |
61 | |
62 | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11572
diff
changeset
|
63 static Array<octave_idx_type> |
10022 | 64 qs_search (const Array<char>& needle, |
65 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
|
66 const octave_idx_type *table, |
10053
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
67 bool overlaps = true) |
10022 | 68 { |
69 const char *x = needle.data (); | |
70 octave_idx_type m = needle.numel (); | |
71 const char *y = haystack.data (); | |
72 octave_idx_type n = haystack.numel (); | |
73 | |
10053
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
74 // 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
|
75 // the operation we need. |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
76 std::deque<octave_idx_type> accum; |
10133
2e4fc7fdba15
optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents:
10086
diff
changeset
|
77 if (m == 1) |
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 // Looking for a single character. |
2e4fc7fdba15
optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents:
10086
diff
changeset
|
80 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
|
81 { |
2e4fc7fdba15
optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents:
10086
diff
changeset
|
82 if (y[i] == x[0]) |
2e4fc7fdba15
optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents:
10086
diff
changeset
|
83 accum.push_back (i); |
2e4fc7fdba15
optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents:
10086
diff
changeset
|
84 } |
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 else if (m == 2) |
10022 | 87 { |
10133
2e4fc7fdba15
optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents:
10086
diff
changeset
|
88 // Two characters. |
2e4fc7fdba15
optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents:
10086
diff
changeset
|
89 if (overlaps) |
2e4fc7fdba15
optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents:
10086
diff
changeset
|
90 { |
2e4fc7fdba15
optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents:
10086
diff
changeset
|
91 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
|
92 { |
2e4fc7fdba15
optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents:
10086
diff
changeset
|
93 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
|
94 accum.push_back (i); |
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 } |
2e4fc7fdba15
optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents:
10086
diff
changeset
|
97 else |
2e4fc7fdba15
optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents:
10086
diff
changeset
|
98 { |
2e4fc7fdba15
optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents:
10086
diff
changeset
|
99 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
|
100 { |
2e4fc7fdba15
optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents:
10086
diff
changeset
|
101 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
|
102 accum.push_back (i++); |
2e4fc7fdba15
optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents:
10086
diff
changeset
|
103 } |
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 } |
2e4fc7fdba15
optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents:
10086
diff
changeset
|
106 else if (n >= m) |
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 // General case. |
10022 | 109 octave_idx_type j = 0; |
110 | |
10053
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
111 if (overlaps) |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
112 { |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11572
diff
changeset
|
113 while (j < n - m) |
10053
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
114 { |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
115 if (std::equal (x, x + m, y + j)) |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
116 accum.push_back (j); |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
117 j += table[ORD(y[j + m])]; |
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 } |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
120 else |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
121 { |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11572
diff
changeset
|
122 while (j < n - m) |
10053
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 if (std::equal (x, x + m, y + j)) |
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 accum.push_back (j); |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
127 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 else |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
130 j += table[ORD(y[j + m])]; |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
131 } |
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 |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
134 if (j == n - m && std::equal (x, x + m, y + j)) |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
135 accum.push_back (j); |
10022 | 136 } |
137 | |
138 octave_idx_type nmatch = accum.size (); | |
10258 | 139 octave_idx_type one = 1; |
140 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
|
141 octave_idx_type k = 0; |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11572
diff
changeset
|
142 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
|
143 iter != accum.end (); iter++) |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
144 { |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
145 result.xelem (k++) = *iter; |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
146 } |
10022 | 147 |
148 return result; | |
149 } | |
150 | |
15039
e753177cde93
maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents:
14501
diff
changeset
|
151 DEFUN (strfind, args, , |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
152 "-*- texinfo -*-\n\ |
20853
1142cf6abc0d
2015 Code Sprint: remove class of function from docstring for all C++ files.
Rik <rik@octave.org>
parents:
20831
diff
changeset
|
153 @deftypefn {} {@var{idx} =} strfind (@var{str}, @var{pattern})\n\ |
1142cf6abc0d
2015 Code Sprint: remove class of function from docstring for all C++ files.
Rik <rik@octave.org>
parents:
20831
diff
changeset
|
154 @deftypefnx {} {@var{idx} =} strfind (@var{cellstr}, @var{pattern})\n\ |
1142cf6abc0d
2015 Code Sprint: remove class of function from docstring for all C++ files.
Rik <rik@octave.org>
parents:
20831
diff
changeset
|
155 @deftypefnx {} {@var{idx} =} strfind (@dots{}, \"overlaps\", @var{val})\n\ |
20172
4f45eaf83908
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19957
diff
changeset
|
156 Search for @var{pattern} in the string @var{str} and return the starting\n\ |
4f45eaf83908
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19957
diff
changeset
|
157 index of every such occurrence in the vector @var{idx}.\n\ |
17398
67c6fc2b9f63
doc: Add info about "overlaps" option to strfind, strrep.
Rik <rik@octave.org>
parents:
15215
diff
changeset
|
158 \n\ |
20172
4f45eaf83908
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19957
diff
changeset
|
159 If there is no such occurrence, or if @var{pattern} is longer than\n\ |
4f45eaf83908
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19957
diff
changeset
|
160 @var{str}, or if @var{pattern} itself is empty, then @var{idx} is the empty\n\ |
4f45eaf83908
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19957
diff
changeset
|
161 array @code{[]}.\n\ |
4f45eaf83908
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19957
diff
changeset
|
162 \n\ |
17398
67c6fc2b9f63
doc: Add info about "overlaps" option to strfind, strrep.
Rik <rik@octave.org>
parents:
15215
diff
changeset
|
163 The optional argument @qcode{\"overlaps\"} determines whether the pattern\n\ |
67c6fc2b9f63
doc: Add info about "overlaps" option to strfind, strrep.
Rik <rik@octave.org>
parents:
15215
diff
changeset
|
164 can match at every position in @var{str} (true), or only for unique\n\ |
67c6fc2b9f63
doc: Add info about "overlaps" option to strfind, strrep.
Rik <rik@octave.org>
parents:
15215
diff
changeset
|
165 occurrences of the complete pattern (false). The default is true.\n\ |
10022 | 166 \n\ |
20172
4f45eaf83908
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19957
diff
changeset
|
167 If a cell array of strings @var{cellstr} is specified then @var{idx} is a\n\ |
4f45eaf83908
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19957
diff
changeset
|
168 cell array of vectors, as specified above.\n\ |
17398
67c6fc2b9f63
doc: Add info about "overlaps" option to strfind, strrep.
Rik <rik@octave.org>
parents:
15215
diff
changeset
|
169 \n\ |
67c6fc2b9f63
doc: Add info about "overlaps" option to strfind, strrep.
Rik <rik@octave.org>
parents:
15215
diff
changeset
|
170 Examples:\n\ |
10022 | 171 \n\ |
172 @example\n\ | |
173 @group\n\ | |
174 strfind (\"abababa\", \"aba\")\n\ | |
175 @result{} [1, 3, 5]\n\ | |
176 \n\ | |
17398
67c6fc2b9f63
doc: Add info about "overlaps" option to strfind, strrep.
Rik <rik@octave.org>
parents:
15215
diff
changeset
|
177 strfind (\"abababa\", \"aba\", \"overlaps\", false)\n\ |
67c6fc2b9f63
doc: Add info about "overlaps" option to strfind, strrep.
Rik <rik@octave.org>
parents:
15215
diff
changeset
|
178 @result{} [1, 5]\n\ |
67c6fc2b9f63
doc: Add info about "overlaps" option to strfind, strrep.
Rik <rik@octave.org>
parents:
15215
diff
changeset
|
179 \n\ |
10022 | 180 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
|
181 @result{}\n\ |
10022 | 182 @{\n\ |
183 [1,1] =\n\ | |
184 \n\ | |
185 1 3 5\n\ | |
186 \n\ | |
187 [1,2] = [](1x0)\n\ | |
188 [1,3] = [](1x0)\n\ | |
189 @}\n\ | |
190 @end group\n\ | |
191 @end example\n\ | |
19957
e78c0514523d
restore strmatch function; backout changeset f9959972949a
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
192 @seealso{findstr, strmatch, regexp, regexpi, find}\n\ |
10022 | 193 @end deftypefn") |
194 { | |
195 int nargin = args.length (); | |
20801
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
196 |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
197 if (nargin != 4 && nargin != 2) |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
198 print_usage (); |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
199 |
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
|
200 bool overlaps = true; |
20801
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
201 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
|
202 { |
20939
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
203 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
|
204 error ("strfind: invalid optional arguments"); |
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
205 |
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
206 std::string opt = args(2).string_value (); |
20801
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
207 |
20939
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
208 if (opt == "overlaps") |
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
209 overlaps = 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
|
210 else |
20939
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
211 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
|
212 } |
10022 | 213 |
20939
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
214 octave_value retval; |
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
215 |
20801
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
216 octave_value argstr = args(0); |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
217 octave_value argpat = args(1); |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
218 |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
219 if (argpat.is_string ()) |
10022 | 220 { |
20801
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
221 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
|
222 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
|
223 qs_preprocess (needle, table); |
10022 | 224 |
20801
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
225 if (argstr.is_string ()) |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
226 if (argpat.is_empty ()) |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
227 // Return a null matrix for null pattern for MW compatibility |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
228 retval = Matrix (); |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
229 else |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
230 retval = octave_value (qs_search (needle, |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
231 argstr.char_array_value (), |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
232 table, overlaps), |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
233 true, true); |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
234 else if (argstr.is_cell ()) |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
235 { |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
236 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
|
237 Cell retc (argsc.dims ()); |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
238 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
|
239 |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
240 for (octave_idx_type i = 0; i < ns; i++) |
10022 | 241 { |
20801
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
242 octave_value argse = argsc(i); |
20939
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
243 if (! argse.is_string ()) |
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
244 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
|
245 |
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
246 if (argpat.is_empty ()) |
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
247 retc(i) = Matrix (); |
20801
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
248 else |
20939
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
249 retc(i) = octave_value (qs_search (needle, |
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
250 argse.char_array_value (), |
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
251 table, overlaps), |
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
252 true, true); |
10022 | 253 } |
20801
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
254 |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
255 retval = retc; |
10022 | 256 } |
257 else | |
20801
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
258 error ("strfind: first argument must be a string or cell array of strings"); |
10022 | 259 } |
20801
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
260 else if (argpat.is_cell ()) |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
261 retval = do_simple_cellfun (Fstrfind, "strfind", args); |
10022 | 262 else |
20801
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
263 error ("strfind: PATTERN must be a string or cell array of strings"); |
10022 | 264 |
265 return retval; | |
266 } | |
267 | |
268 /* | |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
269 %!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
|
270 %!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
|
271 %!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
|
272 %!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
|
273 %!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
|
274 %!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
|
275 %!assert (strfind ({"abc", "def"}, ""), {[], []}) |
10022 | 276 |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
277 %!error strfind () |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
278 %!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
|
279 %!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
|
280 %!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
|
281 %!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
|
282 %!error <PATTERN must be a string> strfind ("foo", 100) |
10022 | 283 */ |
10053
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
284 |
10074
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
285 static Array<char> |
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
286 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
|
287 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
|
288 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
|
289 bool overlaps = true) |
10074
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
290 { |
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
291 Array<char> ret = str; |
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
292 |
18100
6a71e5030df5
Follow coding convention of defining and initializing only 1 variable per line in liboctinterp.
Rik <rik@octave.org>
parents:
17787
diff
changeset
|
293 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
|
294 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
|
295 octave_idx_type rsiz = rep.numel (); |
10074
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
296 |
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
297 if (psiz != 0) |
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
298 { |
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
299 // 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
|
300 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
|
301 octave_idx_type nidx = idx.numel (); |
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
302 |
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
303 if (nidx) |
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 // 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
|
306 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
|
307 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
|
308 { |
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 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
|
310 // 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
|
311 // 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
|
312 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
|
313 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
|
314 { |
21defab4207c
make strrep not skipping overlaps by default, allow an option in both strfind/strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
315 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
|
316 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
|
317 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
|
318 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
|
319 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
|
320 } |
21defab4207c
make strrep not skipping overlaps by default, allow an option in both strfind/strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
321 |
21defab4207c
make strrep not skipping overlaps by default, allow an option in both strfind/strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
322 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
|
323 } |
21defab4207c
make strrep not skipping overlaps by default, allow an option in both strfind/strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
324 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
|
325 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
|
326 |
18763
86818f2c4a92
Return 0x0 empty matrix from strrep for Matlab compatibility (bug #42341).
Rik <rik@octave.org>
parents:
18100
diff
changeset
|
327 if (retsiz == 0) |
86818f2c4a92
Return 0x0 empty matrix from strrep for Matlab compatibility (bug #42341).
Rik <rik@octave.org>
parents:
18100
diff
changeset
|
328 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
|
329 else |
10074
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
330 { |
18763
86818f2c4a92
Return 0x0 empty matrix from strrep for Matlab compatibility (bug #42341).
Rik <rik@octave.org>
parents:
18100
diff
changeset
|
331 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
|
332 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
|
333 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
|
334 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
|
335 |
86818f2c4a92
Return 0x0 empty matrix from strrep for Matlab compatibility (bug #42341).
Rik <rik@octave.org>
parents:
18100
diff
changeset
|
336 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
|
337 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
|
338 { |
86818f2c4a92
Return 0x0 empty matrix from strrep for Matlab compatibility (bug #42341).
Rik <rik@octave.org>
parents:
18100
diff
changeset
|
339 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
|
340 if (j >= k) |
86818f2c4a92
Return 0x0 empty matrix from strrep for Matlab compatibility (bug #42341).
Rik <rik@octave.org>
parents:
18100
diff
changeset
|
341 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
|
342 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
|
343 k = j + psiz; |
86818f2c4a92
Return 0x0 empty matrix from strrep for Matlab compatibility (bug #42341).
Rik <rik@octave.org>
parents:
18100
diff
changeset
|
344 } |
86818f2c4a92
Return 0x0 empty matrix from strrep for Matlab compatibility (bug #42341).
Rik <rik@octave.org>
parents:
18100
diff
changeset
|
345 |
86818f2c4a92
Return 0x0 empty matrix from strrep for Matlab compatibility (bug #42341).
Rik <rik@octave.org>
parents:
18100
diff
changeset
|
346 std::copy (src + k, src + siz, dest); |
10074
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
347 } |
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
348 } |
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
349 } |
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
350 |
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
351 return ret; |
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
352 } |
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
353 |
15039
e753177cde93
maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents:
14501
diff
changeset
|
354 DEFUN (strrep, args, , |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
355 "-*- texinfo -*-\n\ |
20853
1142cf6abc0d
2015 Code Sprint: remove class of function from docstring for all C++ files.
Rik <rik@octave.org>
parents:
20831
diff
changeset
|
356 @deftypefn {} {@var{newstr} =} strrep (@var{str}, @var{ptn}, @var{rep})\n\ |
1142cf6abc0d
2015 Code Sprint: remove class of function from docstring for all C++ files.
Rik <rik@octave.org>
parents:
20831
diff
changeset
|
357 @deftypefnx {} {@var{newstr} =} strrep (@var{cellstr}, @var{ptn}, @var{rep})\n\ |
1142cf6abc0d
2015 Code Sprint: remove class of function from docstring for all C++ files.
Rik <rik@octave.org>
parents:
20831
diff
changeset
|
358 @deftypefnx {} {@var{newstr} =} strrep (@dots{}, \"overlaps\", @var{val})\n\ |
17398
67c6fc2b9f63
doc: Add info about "overlaps" option to strfind, strrep.
Rik <rik@octave.org>
parents:
15215
diff
changeset
|
359 Replace all occurrences of the pattern @var{ptn} in the string @var{str}\n\ |
67c6fc2b9f63
doc: Add info about "overlaps" option to strfind, strrep.
Rik <rik@octave.org>
parents:
15215
diff
changeset
|
360 with the string @var{rep} and return the result.\n\ |
67c6fc2b9f63
doc: Add info about "overlaps" option to strfind, strrep.
Rik <rik@octave.org>
parents:
15215
diff
changeset
|
361 \n\ |
67c6fc2b9f63
doc: Add info about "overlaps" option to strfind, strrep.
Rik <rik@octave.org>
parents:
15215
diff
changeset
|
362 The optional argument @qcode{\"overlaps\"} determines whether the pattern\n\ |
67c6fc2b9f63
doc: Add info about "overlaps" option to strfind, strrep.
Rik <rik@octave.org>
parents:
15215
diff
changeset
|
363 can match at every position in @var{str} (true), or only for unique\n\ |
67c6fc2b9f63
doc: Add info about "overlaps" option to strfind, strrep.
Rik <rik@octave.org>
parents:
15215
diff
changeset
|
364 occurrences of the complete pattern (false). The default is true.\n\ |
67c6fc2b9f63
doc: Add info about "overlaps" option to strfind, strrep.
Rik <rik@octave.org>
parents:
15215
diff
changeset
|
365 \n\ |
67c6fc2b9f63
doc: Add info about "overlaps" option to strfind, strrep.
Rik <rik@octave.org>
parents:
15215
diff
changeset
|
366 @var{s} may also be a cell array of strings, in which case the replacement is\n\ |
67c6fc2b9f63
doc: Add info about "overlaps" option to strfind, strrep.
Rik <rik@octave.org>
parents:
15215
diff
changeset
|
367 done for each element and a cell array is returned.\n\ |
67c6fc2b9f63
doc: Add info about "overlaps" option to strfind, strrep.
Rik <rik@octave.org>
parents:
15215
diff
changeset
|
368 \n\ |
67c6fc2b9f63
doc: Add info about "overlaps" option to strfind, strrep.
Rik <rik@octave.org>
parents:
15215
diff
changeset
|
369 Example:\n\ |
10053
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
370 \n\ |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
371 @example\n\ |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
372 @group\n\ |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
373 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
|
374 @result{} \"Th&%$ &%$ a test string\"\n\ |
10053
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
375 @end group\n\ |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
376 @end example\n\ |
10074
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
377 \n\ |
10053
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
378 @seealso{regexprep, strfind, findstr}\n\ |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
379 @end deftypefn") |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
380 { |
20801
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
381 int nargin = args.length (); |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
382 |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
383 if (nargin != 3 && nargin != 5) |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
384 print_usage (); |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
385 |
20939
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
386 bool overlaps = true; |
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
387 |
20801
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
388 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
|
389 { |
20939
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
390 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
|
391 error ("strrep: invalid optional arguments"); |
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
392 |
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
393 std::string opt = args(3).string_value (); |
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
394 if (opt == "overlaps") |
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
395 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
|
396 else |
20939
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
397 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
|
398 } |
10053
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
399 |
20939
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
400 octave_value retval; |
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
401 |
20801
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
402 octave_value argstr = args(0); |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
403 octave_value argpat = args(1); |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
404 octave_value argrep = args(2); |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
405 |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
406 if (argpat.is_string () && argrep.is_string ()) |
10053
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
407 { |
20801
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
408 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
|
409 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
|
410 |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
411 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
|
412 qs_preprocess (pat, table); |
10074
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
413 |
20801
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
414 if (argstr.is_string ()) |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
415 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
|
416 table, overlaps); |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
417 else if (argstr.is_cell ()) |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
418 { |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
419 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
|
420 Cell retc (argsc.dims ()); |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
421 octave_idx_type ns = argsc.numel (); |
10053
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
422 |
20801
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
423 for (octave_idx_type i = 0; i < ns; i++) |
10053
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
424 { |
20801
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
425 octave_value argse = argsc(i); |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
426 if (argse.is_string ()) |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
427 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
|
428 table, overlaps); |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
429 else |
20831
35241c4b696c
eliminate return statements after calls to error
John W. Eaton <jwe@octave.org>
parents:
20801
diff
changeset
|
430 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
|
431 } |
10053
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
432 |
20801
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
433 retval = retc; |
10053
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
434 } |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
435 else |
20801
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
436 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
|
437 } |
20801
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
438 else if (argpat.is_cell () || argrep.is_cell ()) |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
439 retval = do_simple_cellfun (Fstrrep, "strrep", args); |
10053
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
440 else |
20801
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
441 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
|
442 |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
443 return retval; |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
444 } |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
445 |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
446 /* |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
447 %!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
|
448 %! "Th&%$ &%$ a test string") |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
449 %!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
|
450 %!assert (strrep ("abababc", "abab", "xyz", "overlaps", false), "xyzabc") |
10053
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
451 |
18763
86818f2c4a92
Return 0x0 empty matrix from strrep for Matlab compatibility (bug #42341).
Rik <rik@octave.org>
parents:
18100
diff
changeset
|
452 %!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
|
453 |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
454 %!error strrep () |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
455 %!error strrep ("foo", "bar", 3, 4) |
10053
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
456 */ |