annotate libinterp/corefcn/strfind.cc @ 17787:175b392e91fe

Use GNU style coding conventions for code in libinterp/ * libinterp/corefcn/Cell.h, libinterp/corefcn/__contourc__.cc, libinterp/corefcn/__dispatch__.cc, libinterp/corefcn/__lin_interpn__.cc, libinterp/corefcn/__pchip_deriv__.cc, libinterp/corefcn/__qp__.cc, libinterp/corefcn/balance.cc, libinterp/corefcn/besselj.cc, libinterp/corefcn/betainc.cc, libinterp/corefcn/bitfcns.cc, libinterp/corefcn/bsxfun.cc, libinterp/corefcn/c-file-ptr-stream.cc, libinterp/corefcn/c-file-ptr-stream.h, libinterp/corefcn/cellfun.cc, libinterp/corefcn/colloc.cc, libinterp/corefcn/comment-list.h, libinterp/corefcn/conv2.cc, libinterp/corefcn/daspk.cc, libinterp/corefcn/dasrt.cc, libinterp/corefcn/dassl.cc, libinterp/corefcn/data.cc, libinterp/corefcn/debug.cc, libinterp/corefcn/defaults.cc, libinterp/corefcn/defaults.in.h, libinterp/corefcn/defun-int.h, libinterp/corefcn/defun.cc, libinterp/corefcn/det.cc, libinterp/corefcn/dirfns.cc, libinterp/corefcn/display.cc, libinterp/corefcn/dlmread.cc, libinterp/corefcn/dot.cc, libinterp/corefcn/dynamic-ld.cc, libinterp/corefcn/dynamic-ld.h, libinterp/corefcn/eig.cc, libinterp/corefcn/ellipj.cc, libinterp/corefcn/error.cc, libinterp/corefcn/error.h, libinterp/corefcn/event-queue.h, libinterp/corefcn/fft.cc, libinterp/corefcn/fft2.cc, libinterp/corefcn/fftn.cc, libinterp/corefcn/file-io.cc, libinterp/corefcn/filter.cc, libinterp/corefcn/find.cc, libinterp/corefcn/gammainc.cc, libinterp/corefcn/gcd.cc, libinterp/corefcn/getgrent.cc, libinterp/corefcn/getpwent.cc, libinterp/corefcn/getrusage.cc, libinterp/corefcn/givens.cc, libinterp/corefcn/gl-render.cc, libinterp/corefcn/gl2ps-renderer.cc, libinterp/corefcn/gl2ps-renderer.h, libinterp/corefcn/graphics.cc, libinterp/corefcn/graphics.in.h, libinterp/corefcn/gripes.cc, libinterp/corefcn/gripes.h, libinterp/corefcn/help.cc, libinterp/corefcn/hess.cc, libinterp/corefcn/hex2num.cc, libinterp/corefcn/input.cc, libinterp/corefcn/input.h, libinterp/corefcn/inv.cc, libinterp/corefcn/jit-ir.h, libinterp/corefcn/jit-typeinfo.cc, libinterp/corefcn/jit-typeinfo.h, libinterp/corefcn/jit-util.h, libinterp/corefcn/kron.cc, libinterp/corefcn/load-path.cc, libinterp/corefcn/load-path.h, libinterp/corefcn/load-save.cc, libinterp/corefcn/load-save.h, libinterp/corefcn/lookup.cc, libinterp/corefcn/ls-ascii-helper.cc, libinterp/corefcn/ls-hdf5.cc, libinterp/corefcn/ls-hdf5.h, libinterp/corefcn/ls-mat-ascii.cc, libinterp/corefcn/ls-mat-ascii.h, libinterp/corefcn/ls-mat4.cc, libinterp/corefcn/ls-mat5.cc, libinterp/corefcn/ls-mat5.h, libinterp/corefcn/ls-oct-ascii.cc, libinterp/corefcn/lsode.cc, libinterp/corefcn/lu.cc, libinterp/corefcn/luinc.cc, libinterp/corefcn/mappers.cc, libinterp/corefcn/matrix_type.cc, libinterp/corefcn/max.cc, libinterp/corefcn/md5sum.cc, libinterp/corefcn/mex.cc, libinterp/corefcn/mexproto.h, libinterp/corefcn/mgorth.cc, libinterp/corefcn/mxarray.in.h, libinterp/corefcn/nproc.cc, libinterp/corefcn/oct-hist.cc, libinterp/corefcn/oct-lvalue.h, libinterp/corefcn/oct-map.cc, libinterp/corefcn/oct-map.h, libinterp/corefcn/oct-obj.h, libinterp/corefcn/oct-prcstrm.h, libinterp/corefcn/oct-stdstrm.h, libinterp/corefcn/oct-stream.cc, libinterp/corefcn/oct-stream.h, libinterp/corefcn/octave-link.cc, libinterp/corefcn/octave-link.h, libinterp/corefcn/pager.cc, libinterp/corefcn/pinv.cc, libinterp/corefcn/pr-output.cc, libinterp/corefcn/procstream.h, libinterp/corefcn/profiler.cc, libinterp/corefcn/pt-jit.cc, libinterp/corefcn/pt-jit.h, libinterp/corefcn/quad.cc, libinterp/corefcn/quadcc.cc, libinterp/corefcn/qz.cc, libinterp/corefcn/rand.cc, libinterp/corefcn/rcond.cc, libinterp/corefcn/regexp.cc, libinterp/corefcn/schur.cc, libinterp/corefcn/sighandlers.cc, libinterp/corefcn/sighandlers.h, libinterp/corefcn/sparse-xdiv.cc, libinterp/corefcn/sparse-xdiv.h, libinterp/corefcn/sparse-xpow.cc, libinterp/corefcn/sparse.cc, libinterp/corefcn/spparms.cc, libinterp/corefcn/sqrtm.cc, libinterp/corefcn/str2double.cc, libinterp/corefcn/strfind.cc, libinterp/corefcn/strfns.cc, libinterp/corefcn/sub2ind.cc, libinterp/corefcn/svd.cc, libinterp/corefcn/syl.cc, libinterp/corefcn/symtab.cc, libinterp/corefcn/symtab.h, libinterp/corefcn/syscalls.cc, libinterp/corefcn/sysdep.cc, libinterp/corefcn/sysdep.h, libinterp/corefcn/time.cc, libinterp/corefcn/toplev.cc, libinterp/corefcn/toplev.h, libinterp/corefcn/tril.cc, libinterp/corefcn/txt-eng-ft.cc, libinterp/corefcn/txt-eng-ft.h, libinterp/corefcn/txt-eng.h, libinterp/corefcn/typecast.cc, libinterp/corefcn/urlwrite.cc, libinterp/corefcn/utils.cc, libinterp/corefcn/variables.cc, libinterp/corefcn/variables.h, libinterp/corefcn/xdiv.cc, libinterp/corefcn/xdiv.h, libinterp/corefcn/xnorm.h, libinterp/corefcn/xpow.cc, libinterp/corefcn/xpow.h, libinterp/corefcn/zfstream.cc, libinterp/corefcn/zfstream.h, libinterp/dldfcn/__delaunayn__.cc, libinterp/dldfcn/__dsearchn__.cc, libinterp/dldfcn/__eigs__.cc, libinterp/dldfcn/__fltk_uigetfile__.cc, libinterp/dldfcn/__glpk__.cc, libinterp/dldfcn/__init_fltk__.cc, libinterp/dldfcn/__init_gnuplot__.cc, libinterp/dldfcn/__magick_read__.cc, libinterp/dldfcn/__voronoi__.cc, libinterp/dldfcn/amd.cc, libinterp/dldfcn/ccolamd.cc, libinterp/dldfcn/chol.cc, libinterp/dldfcn/colamd.cc, libinterp/dldfcn/convhulln.cc, libinterp/dldfcn/dmperm.cc, libinterp/dldfcn/fftw.cc, libinterp/dldfcn/qr.cc, libinterp/dldfcn/symbfact.cc, libinterp/dldfcn/symrcm.cc, libinterp/dldfcn/tsearch.cc, libinterp/octave-value/ov-base-diag.cc, libinterp/octave-value/ov-base-diag.h, libinterp/octave-value/ov-base-int.cc, libinterp/octave-value/ov-base-int.h, libinterp/octave-value/ov-base-mat.h, libinterp/octave-value/ov-base-scalar.cc, libinterp/octave-value/ov-base-scalar.h, libinterp/octave-value/ov-base-sparse.cc, libinterp/octave-value/ov-base-sparse.h, libinterp/octave-value/ov-base.cc, libinterp/octave-value/ov-base.h, libinterp/octave-value/ov-bool-mat.cc, libinterp/octave-value/ov-bool-mat.h, libinterp/octave-value/ov-bool-sparse.cc, libinterp/octave-value/ov-bool-sparse.h, libinterp/octave-value/ov-bool.cc, libinterp/octave-value/ov-bool.h, libinterp/octave-value/ov-builtin.cc, libinterp/octave-value/ov-builtin.h, libinterp/octave-value/ov-cell.cc, libinterp/octave-value/ov-cell.h, libinterp/octave-value/ov-ch-mat.cc, libinterp/octave-value/ov-ch-mat.h, libinterp/octave-value/ov-class.cc, libinterp/octave-value/ov-class.h, libinterp/octave-value/ov-colon.h, libinterp/octave-value/ov-complex.cc, libinterp/octave-value/ov-complex.h, libinterp/octave-value/ov-cx-diag.cc, libinterp/octave-value/ov-cx-diag.h, libinterp/octave-value/ov-cx-mat.cc, libinterp/octave-value/ov-cx-mat.h, libinterp/octave-value/ov-cx-sparse.cc, libinterp/octave-value/ov-cx-sparse.h, libinterp/octave-value/ov-dld-fcn.h, libinterp/octave-value/ov-fcn-handle.cc, libinterp/octave-value/ov-fcn-handle.h, libinterp/octave-value/ov-fcn-inline.cc, libinterp/octave-value/ov-fcn-inline.h, libinterp/octave-value/ov-fcn.h, libinterp/octave-value/ov-float.cc, libinterp/octave-value/ov-float.h, libinterp/octave-value/ov-flt-complex.cc, libinterp/octave-value/ov-flt-complex.h, libinterp/octave-value/ov-flt-cx-diag.cc, libinterp/octave-value/ov-flt-cx-diag.h, libinterp/octave-value/ov-flt-cx-mat.cc, libinterp/octave-value/ov-flt-cx-mat.h, libinterp/octave-value/ov-flt-re-diag.cc, libinterp/octave-value/ov-flt-re-diag.h, libinterp/octave-value/ov-flt-re-mat.cc, libinterp/octave-value/ov-flt-re-mat.h, libinterp/octave-value/ov-int16.cc, libinterp/octave-value/ov-int32.cc, libinterp/octave-value/ov-int64.cc, libinterp/octave-value/ov-int8.cc, libinterp/octave-value/ov-intx.h, libinterp/octave-value/ov-java.cc, libinterp/octave-value/ov-lazy-idx.h, libinterp/octave-value/ov-mex-fcn.cc, libinterp/octave-value/ov-mex-fcn.h, libinterp/octave-value/ov-null-mat.cc, libinterp/octave-value/ov-null-mat.h, libinterp/octave-value/ov-oncleanup.cc, libinterp/octave-value/ov-perm.cc, libinterp/octave-value/ov-perm.h, libinterp/octave-value/ov-range.cc, libinterp/octave-value/ov-range.h, libinterp/octave-value/ov-re-diag.cc, libinterp/octave-value/ov-re-diag.h, libinterp/octave-value/ov-re-mat.cc, libinterp/octave-value/ov-re-mat.h, libinterp/octave-value/ov-re-sparse.cc, libinterp/octave-value/ov-re-sparse.h, libinterp/octave-value/ov-scalar.cc, libinterp/octave-value/ov-scalar.h, libinterp/octave-value/ov-str-mat.cc, libinterp/octave-value/ov-str-mat.h, libinterp/octave-value/ov-struct.cc, libinterp/octave-value/ov-struct.h, libinterp/octave-value/ov-type-conv.h, libinterp/octave-value/ov-typeinfo.cc, libinterp/octave-value/ov-typeinfo.h, libinterp/octave-value/ov-uint16.cc, libinterp/octave-value/ov-uint32.cc, libinterp/octave-value/ov-uint64.cc, libinterp/octave-value/ov-uint8.cc, libinterp/octave-value/ov-usr-fcn.cc, libinterp/octave-value/ov-usr-fcn.h, libinterp/octave-value/ov.cc, libinterp/octave-value/ov.h, libinterp/octave.cc, libinterp/operators/op-b-bm.cc, libinterp/operators/op-b-sbm.cc, libinterp/operators/op-bm-b.cc, libinterp/operators/op-bm-bm.cc, libinterp/operators/op-cdm-cdm.cc, libinterp/operators/op-chm.cc, libinterp/operators/op-class.cc, libinterp/operators/op-cm-cm.cc, libinterp/operators/op-cm-cs.cc, libinterp/operators/op-cm-s.cc, libinterp/operators/op-cm-scm.cc, libinterp/operators/op-cm-sm.cc, libinterp/operators/op-cs-cm.cc, libinterp/operators/op-cs-cs.cc, libinterp/operators/op-cs-scm.cc, libinterp/operators/op-cs-sm.cc, libinterp/operators/op-dm-dm.cc, libinterp/operators/op-dm-scm.cc, libinterp/operators/op-double-conv.cc, libinterp/operators/op-fcdm-fcdm.cc, libinterp/operators/op-fcm-fcm.cc, libinterp/operators/op-fcm-fcs.cc, libinterp/operators/op-fcm-fm.cc, libinterp/operators/op-fcm-fs.cc, libinterp/operators/op-fcs-fcm.cc, libinterp/operators/op-fcs-fcs.cc, libinterp/operators/op-fcs-fm.cc, libinterp/operators/op-fcs-fs.cc, libinterp/operators/op-fdm-fdm.cc, libinterp/operators/op-float-conv.cc, libinterp/operators/op-fm-fcm.cc, libinterp/operators/op-fm-fcs.cc, libinterp/operators/op-fm-fm.cc, libinterp/operators/op-fm-fs.cc, libinterp/operators/op-fs-fcm.cc, libinterp/operators/op-fs-fcs.cc, libinterp/operators/op-fs-fm.cc, libinterp/operators/op-fs-fs.cc, libinterp/operators/op-m-cm.cc, libinterp/operators/op-m-cs.cc, libinterp/operators/op-m-m.cc, libinterp/operators/op-m-s.cc, libinterp/operators/op-m-scm.cc, libinterp/operators/op-m-sm.cc, libinterp/operators/op-pm-scm.cc, libinterp/operators/op-range.cc, libinterp/operators/op-s-cm.cc, libinterp/operators/op-s-cs.cc, libinterp/operators/op-s-scm.cc, libinterp/operators/op-sbm-b.cc, libinterp/operators/op-sbm-bm.cc, libinterp/operators/op-sbm-sbm.cc, libinterp/operators/op-scm-cm.cc, libinterp/operators/op-scm-cs.cc, libinterp/operators/op-scm-m.cc, libinterp/operators/op-scm-s.cc, libinterp/operators/op-scm-scm.cc, libinterp/operators/op-scm-sm.cc, libinterp/operators/op-sm-cm.cc, libinterp/operators/op-sm-m.cc, libinterp/operators/op-sm-s.cc, libinterp/operators/op-sm-scm.cc, libinterp/operators/op-sm-sm.cc, libinterp/operators/op-str-m.cc, libinterp/operators/op-str-s.cc, libinterp/operators/op-str-str.cc, libinterp/operators/ops.h, libinterp/parse-tree/lex.h, libinterp/parse-tree/parse.h, libinterp/parse-tree/pt-arg-list.cc, libinterp/parse-tree/pt-arg-list.h, libinterp/parse-tree/pt-assign.cc, libinterp/parse-tree/pt-assign.h, libinterp/parse-tree/pt-binop.cc, libinterp/parse-tree/pt-binop.h, libinterp/parse-tree/pt-bp.h, libinterp/parse-tree/pt-cbinop.cc, libinterp/parse-tree/pt-check.cc, libinterp/parse-tree/pt-colon.cc, libinterp/parse-tree/pt-colon.h, libinterp/parse-tree/pt-const.cc, libinterp/parse-tree/pt-decl.cc, libinterp/parse-tree/pt-decl.h, libinterp/parse-tree/pt-eval.cc, libinterp/parse-tree/pt-except.h, libinterp/parse-tree/pt-exp.h, libinterp/parse-tree/pt-fcn-handle.cc, libinterp/parse-tree/pt-id.cc, libinterp/parse-tree/pt-id.h, libinterp/parse-tree/pt-idx.cc, libinterp/parse-tree/pt-idx.h, libinterp/parse-tree/pt-loop.h, libinterp/parse-tree/pt-mat.cc, libinterp/parse-tree/pt-misc.cc, libinterp/parse-tree/pt-misc.h, libinterp/parse-tree/pt-pr-code.cc, libinterp/parse-tree/pt-select.h, libinterp/parse-tree/pt-stmt.h, libinterp/parse-tree/token.h, libinterp/version.cc: Use GNU style coding conventions for code in libinterp/
author Rik <rik@octave.org>
date Mon, 28 Oct 2013 19:51:46 -0700
parents d63878346099
children 6a71e5030df5
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
17744
d63878346099 maint: Update copyright notices for release.
John W. Eaton <jwe@octave.org>
parents: 17398
diff changeset
3 Copyright (C) 2009-2013 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 <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
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"
15039
e753177cde93 maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents: 14501
diff changeset
35 #include "defun.h"
10022
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
17787
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
40 // This allows safe indexing with char.
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
41 // In C++, char may be (and often is) signed!
10022
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
42 #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
43 #define TABSIZE (std::numeric_limits<unsigned char>::max () + 1)
10022
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
44
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
45 // This is the quick search algorithm, as described at
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
46 // 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
47 static void
10022
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
48 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
49 octave_idx_type *table)
10022
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
50 {
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
51 const char *x = needle.data ();
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
52 octave_idx_type m = needle.numel ();
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
53
17787
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
54 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
55 table[i] = m + 1;
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 < m; i++)
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
57 table[ORD(x[i])] = m - i;
10022
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
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
60
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11572
diff changeset
61 static Array<octave_idx_type>
10022
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
62 qs_search (const Array<char>& needle,
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
63 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
64 const octave_idx_type *table,
10053
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
65 bool overlaps = true)
10022
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
66 {
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
67 const char *x = needle.data ();
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
68 octave_idx_type m = needle.numel ();
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
69 const char *y = haystack.data ();
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
70 octave_idx_type n = haystack.numel ();
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
71
10053
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
72 // 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
73 // the operation we need.
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
74 std::deque<octave_idx_type> accum;
10133
2e4fc7fdba15 optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents: 10086
diff changeset
75 if (m == 1)
2e4fc7fdba15 optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents: 10086
diff changeset
76 {
2e4fc7fdba15 optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents: 10086
diff changeset
77 // Looking for a single character.
2e4fc7fdba15 optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents: 10086
diff changeset
78 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
79 {
2e4fc7fdba15 optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents: 10086
diff changeset
80 if (y[i] == x[0])
2e4fc7fdba15 optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents: 10086
diff changeset
81 accum.push_back (i);
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 }
2e4fc7fdba15 optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents: 10086
diff changeset
84 else if (m == 2)
10022
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
85 {
10133
2e4fc7fdba15 optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents: 10086
diff changeset
86 // Two characters.
2e4fc7fdba15 optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents: 10086
diff changeset
87 if (overlaps)
2e4fc7fdba15 optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents: 10086
diff changeset
88 {
2e4fc7fdba15 optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents: 10086
diff changeset
89 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
90 {
2e4fc7fdba15 optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents: 10086
diff changeset
91 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
92 accum.push_back (i);
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 }
2e4fc7fdba15 optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents: 10086
diff changeset
95 else
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 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
98 {
2e4fc7fdba15 optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents: 10086
diff changeset
99 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
100 accum.push_back (i++);
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 }
2e4fc7fdba15 optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents: 10086
diff changeset
104 else if (n >= m)
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 // General case.
10022
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
107 octave_idx_type j = 0;
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
108
10053
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
109 if (overlaps)
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
110 {
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11572
diff changeset
111 while (j < n - m)
10053
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
112 {
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
113 if (std::equal (x, x + m, y + j))
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
114 accum.push_back (j);
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
115 j += table[ORD(y[j + m])];
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 }
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
118 else
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
119 {
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11572
diff changeset
120 while (j < n - m)
10053
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
121 {
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
122 if (std::equal (x, x + m, y + j))
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 accum.push_back (j);
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
125 j += m;
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
126 }
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
127 else
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
128 j += table[ORD(y[j + m])];
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
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
132 if (j == n - m && std::equal (x, x + m, y + j))
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
133 accum.push_back (j);
10022
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
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
136 octave_idx_type nmatch = accum.size ();
10258
e317791645c4 64-bit fixes
John W. Eaton <jwe@octave.org>
parents: 10133
diff changeset
137 octave_idx_type one = 1;
e317791645c4 64-bit fixes
John W. Eaton <jwe@octave.org>
parents: 10133
diff changeset
138 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
139 octave_idx_type k = 0;
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11572
diff changeset
140 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
141 iter != accum.end (); iter++)
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
142 {
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
143 result.xelem (k++) = *iter;
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
144 }
10022
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
145
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
146 return result;
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
15039
e753177cde93 maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents: 14501
diff changeset
149 DEFUN (strfind, args, ,
17787
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
150 "-*- texinfo -*-\n\
15039
e753177cde93 maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents: 14501
diff changeset
151 @deftypefn {Built-in Function} {@var{idx} =} strfind (@var{str}, @var{pattern})\n\
e753177cde93 maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents: 14501
diff changeset
152 @deftypefnx {Built-in Function} {@var{idx} =} strfind (@var{cellstr}, @var{pattern})\n\
17398
67c6fc2b9f63 doc: Add info about "overlaps" option to strfind, strrep.
Rik <rik@octave.org>
parents: 15215
diff changeset
153 @deftypefnx {Built-in Function} {@var{idx} =} strfind (@dots{}, \"overlaps\", @var{val})\n\
10022
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
154 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
155 starting 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
156 \n\
10022
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
157 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
158 than @var{str}, then @var{idx} is the empty array @code{[]}.\n\
17398
67c6fc2b9f63 doc: Add info about "overlaps" option to strfind, strrep.
Rik <rik@octave.org>
parents: 15215
diff changeset
159 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
160 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
161 occurrences of the complete pattern (false). The default is true.\n\
10022
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
162 \n\
11553
01f703952eff Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
163 If a cell array of strings @var{cellstr} is specified\n\
17398
67c6fc2b9f63 doc: Add info about "overlaps" option to strfind, strrep.
Rik <rik@octave.org>
parents: 15215
diff changeset
164 then @var{idx} is a cell array of vectors, as specified above.\n\
67c6fc2b9f63 doc: Add info about "overlaps" option to strfind, strrep.
Rik <rik@octave.org>
parents: 15215
diff changeset
165 \n\
67c6fc2b9f63 doc: Add info about "overlaps" option to strfind, strrep.
Rik <rik@octave.org>
parents: 15215
diff changeset
166 Examples:\n\
10022
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
167 \n\
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
168 @example\n\
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
169 @group\n\
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
170 strfind (\"abababa\", \"aba\")\n\
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
171 @result{} [1, 3, 5]\n\
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
172 \n\
17398
67c6fc2b9f63 doc: Add info about "overlaps" option to strfind, strrep.
Rik <rik@octave.org>
parents: 15215
diff changeset
173 strfind (\"abababa\", \"aba\", \"overlaps\", false)\n\
67c6fc2b9f63 doc: Add info about "overlaps" option to strfind, strrep.
Rik <rik@octave.org>
parents: 15215
diff changeset
174 @result{} [1, 5]\n\
67c6fc2b9f63 doc: Add info about "overlaps" option to strfind, strrep.
Rik <rik@octave.org>
parents: 15215
diff changeset
175 \n\
10022
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
176 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
177 @result{}\n\
10022
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
178 @{\n\
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
179 [1,1] =\n\
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
180 \n\
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
181 1 3 5\n\
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
182 \n\
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
183 [1,2] = [](1x0)\n\
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
184 [1,3] = [](1x0)\n\
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
185 @}\n\
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
186 @end group\n\
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
187 @end example\n\
11572
7d6d8c1e471f Grammarcheck Texinfo for files in src directory.
Rik <octave@nomad.inbox5.com>
parents: 11553
diff changeset
188 @seealso{findstr, strmatch, regexp, regexpi, find}\n\
10022
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
189 @end deftypefn")
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
190 {
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
191 octave_value retval;
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
192 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
193 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
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 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
196 {
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 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
198 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
199 {
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 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
201 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
202 }
21defab4207c make strrep not skipping overlaps by default, allow an option in both strfind/strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10521
diff changeset
203 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
204 {
21defab4207c make strrep not skipping overlaps by default, allow an option in both strfind/strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10521
diff changeset
205 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
206 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
207 }
21defab4207c make strrep not skipping overlaps by default, allow an option in both strfind/strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10521
diff changeset
208 }
10022
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
209
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
210 if (nargin == 2)
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
211 {
10086
76df75b10c80 allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents: 10074
diff changeset
212 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
213 if (argpat.is_string ())
10022
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
214 {
10086
76df75b10c80 allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents: 10074
diff changeset
215 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
216 OCTAVE_LOCAL_BUFFER (octave_idx_type, table, TABSIZE);
10022
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
217 qs_preprocess (needle, table);
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 if (argstr.is_string ())
17787
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
220 retval = octave_value (qs_search (needle,
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
221 argstr.char_array_value (),
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11572
diff changeset
222 table, overlaps),
10053
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
223 true, true);
10022
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
224 else if (argstr.is_cell ())
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
225 {
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
226 const Cell argsc = argstr.cell_value ();
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
227 Cell retc (argsc.dims ());
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
228 octave_idx_type ns = argsc.numel ();
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
229
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
230 for (octave_idx_type i = 0; i < ns; i++)
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 octave_value argse = argsc(i);
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
233 if (argse.is_string ())
17787
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
234 retc(i)
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
235 = octave_value (qs_search (needle,
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
236 argse.char_array_value (),
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
237 table, overlaps),
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
238 true, true);
10022
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
239 else
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
240 {
11553
01f703952eff Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
241 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
242 break;
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
243 }
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
244 }
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 retval = retc;
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 else
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
249 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
250 }
10086
76df75b10c80 allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents: 10074
diff changeset
251 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
252 retval = do_simple_cellfun (Fstrfind, "strfind", args);
10022
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
253 else
11553
01f703952eff Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
254 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
255 }
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
256 else
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
257 print_usage ();
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
258
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
259 return retval;
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
260 }
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
261
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
262 /*
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
263 %!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
264 %!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
265 %!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
266 %!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
267
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
268 %!error strfind ()
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
269 %!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
270 %!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
271 %!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
272 */
10053
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
273
10074
5e2b4b7967cc allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10053
diff changeset
274 static Array<char>
5e2b4b7967cc allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10053
diff changeset
275 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
276 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
277 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
278 bool overlaps = true)
10074
5e2b4b7967cc allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10053
diff changeset
279 {
5e2b4b7967cc allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10053
diff changeset
280 Array<char> ret = str;
5e2b4b7967cc allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10053
diff changeset
281
5e2b4b7967cc allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10053
diff changeset
282 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
283
5e2b4b7967cc allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10053
diff changeset
284 if (psiz != 0)
5e2b4b7967cc allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10053
diff changeset
285 {
5e2b4b7967cc allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10053
diff changeset
286 // 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
287 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
288 octave_idx_type nidx = idx.numel ();
5e2b4b7967cc allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10053
diff changeset
289
5e2b4b7967cc allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10053
diff changeset
290 if (nidx)
5e2b4b7967cc allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10053
diff changeset
291 {
5e2b4b7967cc allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10053
diff changeset
292 // 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
293 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
294 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
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 = 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
297 // 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
298 // 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
299 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
300 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
301 {
21defab4207c make strrep not skipping overlaps by default, allow an option in both strfind/strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10521
diff changeset
302 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
303 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
304 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
305 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
306 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
307 }
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 += 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
310 }
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 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
312 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
313
10074
5e2b4b7967cc allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10053
diff changeset
314 ret.clear (dim_vector (1, retsiz));
5e2b4b7967cc allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10053
diff changeset
315 const char *src = str.data (), *reps = rep.data ();
5e2b4b7967cc allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10053
diff changeset
316 char *dest = ret.fortran_vec ();
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 octave_idx_type k = 0;
5e2b4b7967cc allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10053
diff changeset
319 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
320 {
5e2b4b7967cc allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10053
diff changeset
321 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
322 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
323 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
324 dest = std::copy (reps, reps + rsiz, dest);
5e2b4b7967cc allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10053
diff changeset
325 k = j + psiz;
5e2b4b7967cc allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10053
diff changeset
326 }
5e2b4b7967cc allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10053
diff changeset
327
5e2b4b7967cc allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10053
diff changeset
328 std::copy (src + k, src + siz, dest);
5e2b4b7967cc allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10053
diff changeset
329 }
5e2b4b7967cc allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10053
diff changeset
330 }
5e2b4b7967cc allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10053
diff changeset
331
5e2b4b7967cc allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10053
diff changeset
332 return ret;
5e2b4b7967cc allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10053
diff changeset
333 }
5e2b4b7967cc allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10053
diff changeset
334
15039
e753177cde93 maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents: 14501
diff changeset
335 DEFUN (strrep, args, ,
17787
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
336 "-*- texinfo -*-\n\
17398
67c6fc2b9f63 doc: Add info about "overlaps" option to strfind, strrep.
Rik <rik@octave.org>
parents: 15215
diff changeset
337 @deftypefn {Built-in Function} {@var{newstr} =} strrep (@var{str}, @var{ptn}, @var{rep})\n\
67c6fc2b9f63 doc: Add info about "overlaps" option to strfind, strrep.
Rik <rik@octave.org>
parents: 15215
diff changeset
338 @deftypefnx {Built-in Function} {@var{newstr} =} strrep (@var{cellstr}, @var{ptn}, @var{rep})\n\
67c6fc2b9f63 doc: Add info about "overlaps" option to strfind, strrep.
Rik <rik@octave.org>
parents: 15215
diff changeset
339 @deftypefnx {Built-in Function} {@var{newstr} =} strrep (@dots{}, \"overlaps\", @var{val})\n\
67c6fc2b9f63 doc: Add info about "overlaps" option to strfind, strrep.
Rik <rik@octave.org>
parents: 15215
diff changeset
340 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
341 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
342 \n\
67c6fc2b9f63 doc: Add info about "overlaps" option to strfind, strrep.
Rik <rik@octave.org>
parents: 15215
diff changeset
343 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
344 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
345 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
346 \n\
67c6fc2b9f63 doc: Add info about "overlaps" option to strfind, strrep.
Rik <rik@octave.org>
parents: 15215
diff changeset
347 @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
348 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
349 \n\
67c6fc2b9f63 doc: Add info about "overlaps" option to strfind, strrep.
Rik <rik@octave.org>
parents: 15215
diff changeset
350 Example:\n\
10053
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
351 \n\
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
352 @example\n\
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
353 @group\n\
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
354 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
355 @result{} \"Th&%$ &%$ a test string\"\n\
10053
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
356 @end group\n\
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
357 @end example\n\
10074
5e2b4b7967cc allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10053
diff changeset
358 \n\
10053
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
359 @seealso{regexprep, strfind, findstr}\n\
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
360 @end deftypefn")
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
361 {
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
362 octave_value retval;
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
363 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
364 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
365
21defab4207c make strrep not skipping overlaps by default, allow an option in both strfind/strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10521
diff changeset
366 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
367 {
21defab4207c make strrep not skipping overlaps by default, allow an option in both strfind/strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10521
diff changeset
368 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
369 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
370 {
21defab4207c make strrep not skipping overlaps by default, allow an option in both strfind/strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10521
diff changeset
371 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
372 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
373 }
21defab4207c make strrep not skipping overlaps by default, allow an option in both strfind/strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10521
diff changeset
374 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
375 {
21defab4207c make strrep not skipping overlaps by default, allow an option in both strfind/strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10521
diff changeset
376 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
377 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
378 }
21defab4207c make strrep not skipping overlaps by default, allow an option in both strfind/strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10521
diff changeset
379 }
10053
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
380
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
381 if (nargin == 3)
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
382 {
10086
76df75b10c80 allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents: 10074
diff changeset
383 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
384 if (argpat.is_string () && argrep.is_string ())
10053
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
385 {
10086
76df75b10c80 allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents: 10074
diff changeset
386 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
387 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
388
12786
4e70c4e61c24 strfind.cc: Fix infinite loop when pattern contains 255 (Bug #33670).
Rik <octave@nomad.inbox5.com>
parents: 12483
diff changeset
389 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
390 qs_preprocess (pat, table);
10053
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
391
10074
5e2b4b7967cc allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10053
diff changeset
392 if (argstr.is_string ())
17787
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
393 retval = qs_replace (argstr.char_array_value (), pat, rep,
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
394 table, overlaps);
10074
5e2b4b7967cc allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10053
diff changeset
395 else if (argstr.is_cell ())
10053
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
396 {
10074
5e2b4b7967cc allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10053
diff changeset
397 const Cell argsc = argstr.cell_value ();
5e2b4b7967cc allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10053
diff changeset
398 Cell retc (argsc.dims ());
5e2b4b7967cc allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10053
diff changeset
399 octave_idx_type ns = argsc.numel ();
10053
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
400
10074
5e2b4b7967cc allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10053
diff changeset
401 for (octave_idx_type i = 0; i < ns; i++)
10053
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
402 {
10074
5e2b4b7967cc allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10053
diff changeset
403 octave_value argse = argsc(i);
5e2b4b7967cc allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10053
diff changeset
404 if (argse.is_string ())
17787
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
405 retc(i) = qs_replace (argse.char_array_value (), pat, rep,
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
406 table, overlaps);
10074
5e2b4b7967cc allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10053
diff changeset
407 else
10053
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
408 {
11553
01f703952eff Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
409 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
410 break;
10053
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
411 }
10074
5e2b4b7967cc allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10053
diff changeset
412 }
10053
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
413
10074
5e2b4b7967cc allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10053
diff changeset
414 retval = retc;
10053
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
415 }
10074
5e2b4b7967cc allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10053
diff changeset
416 else
12483
7a5aacf65f81 Rewrite error strings in src/ to use variables named in documentation.
Rik <octave@nomad.inbox5.com>
parents: 11586
diff changeset
417 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
418 }
10086
76df75b10c80 allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents: 10074
diff changeset
419 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
420 retval = do_simple_cellfun (Fstrrep, "strrep", args);
10053
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
421 else
12483
7a5aacf65f81 Rewrite error strings in src/ to use variables named in documentation.
Rik <octave@nomad.inbox5.com>
parents: 11586
diff changeset
422 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
423 }
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
424 else
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
425 print_usage ();
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
426
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
427 return retval;
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
428 }
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
429
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
430 /*
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
431 %!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
432 %! "Th&%$ &%$ a test string")
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
433 %!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
434 %!assert (strrep ("abababc", "abab", "xyz", "overlaps", false), "xyzabc")
10053
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
435
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
436 %!error strrep ()
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
437 %!error strrep ("foo", "bar", 3, 4)
10053
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
438 */