annotate libinterp/corefcn/strfind.cc @ 31605:e88a07dec498 stable

maint: Use macros to begin/end C++ namespaces. * oct-conf-post-public.in.h: Define two macros (OCTAVE_BEGIN_NAMESPACE, OCTAVE_END_NAMESPACE) that can be used to start/end a namespace. * mk-opts.pl, build-env.h, build-env.in.cc, __betainc__.cc, __contourc__.cc, __dsearchn__.cc, __eigs__.cc, __expint__.cc, __ftp__.cc, __gammainc__.cc, __ichol__.cc, __ilu__.cc, __isprimelarge__.cc, __lin_interpn__.cc, __magick_read__.cc, __pchip_deriv__.cc, __qp__.cc, amd.cc, auto-shlib.cc, auto-shlib.h, balance.cc, base-text-renderer.cc, base-text-renderer.h, besselj.cc, bitfcns.cc, bsxfun.cc, c-file-ptr-stream.cc, c-file-ptr-stream.h, call-stack.cc, call-stack.h, ccolamd.cc, cellfun.cc, chol.cc, colamd.cc, colloc.cc, conv2.cc, daspk.cc, dasrt.cc, dassl.cc, data.cc, data.h, debug.cc, defaults.cc, defaults.h, defun-int.h, defun.cc, det.cc, dirfns.cc, display.cc, display.h, dlmread.cc, dmperm.cc, dot.cc, dynamic-ld.cc, dynamic-ld.h, eig.cc, ellipj.cc, environment.cc, environment.h, error.cc, error.h, errwarn.h, event-manager.cc, event-manager.h, event-queue.cc, event-queue.h, fcn-info.cc, fcn-info.h, fft.cc, fft2.cc, fftn.cc, file-io.cc, filter.cc, find.cc, ft-text-renderer.cc, ft-text-renderer.h, gcd.cc, getgrent.cc, getpwent.cc, getrusage.cc, givens.cc, gl-render.cc, gl-render.h, gl2ps-print.cc, gl2ps-print.h, graphics-toolkit.cc, graphics-toolkit.h, graphics.cc, graphics.in.h, gsvd.cc, gtk-manager.cc, gtk-manager.h, hash.cc, help.cc, help.h, hess.cc, hex2num.cc, hook-fcn.cc, hook-fcn.h, input.cc, input.h, interpreter-private.cc, interpreter-private.h, interpreter.cc, interpreter.h, inv.cc, jsondecode.cc, jsonencode.cc, kron.cc, latex-text-renderer.cc, latex-text-renderer.h, load-path.cc, load-path.h, load-save.cc, load-save.h, lookup.cc, ls-ascii-helper.cc, ls-ascii-helper.h, ls-oct-text.cc, ls-utils.cc, ls-utils.h, lsode.cc, lu.cc, mappers.cc, matrix_type.cc, max.cc, mex-private.h, mex.cc, mgorth.cc, nproc.cc, oct-fstrm.cc, oct-fstrm.h, oct-hdf5-types.cc, oct-hdf5-types.h, oct-hist.cc, oct-hist.h, oct-iostrm.cc, oct-iostrm.h, oct-opengl.h, oct-prcstrm.cc, oct-prcstrm.h, oct-procbuf.cc, oct-procbuf.h, oct-process.cc, oct-process.h, oct-stdstrm.h, oct-stream.cc, oct-stream.h, oct-strstrm.cc, oct-strstrm.h, oct-tex-lexer.in.ll, oct-tex-parser.yy, ordqz.cc, ordschur.cc, pager.cc, pager.h, pinv.cc, pow2.cc, pr-flt-fmt.cc, pr-output.cc, procstream.cc, procstream.h, psi.cc, qr.cc, quad.cc, quadcc.cc, qz.cc, rand.cc, rcond.cc, regexp.cc, schur.cc, settings.cc, settings.h, sighandlers.cc, sighandlers.h, sparse-xdiv.cc, sparse-xdiv.h, sparse-xpow.cc, sparse-xpow.h, sparse.cc, spparms.cc, sqrtm.cc, stack-frame.cc, stack-frame.h, stream-euler.cc, strfind.cc, strfns.cc, sub2ind.cc, svd.cc, sylvester.cc, symbfact.cc, syminfo.cc, syminfo.h, symrcm.cc, symrec.cc, symrec.h, symscope.cc, symscope.h, symtab.cc, symtab.h, syscalls.cc, sysdep.cc, sysdep.h, text-engine.cc, text-engine.h, text-renderer.cc, text-renderer.h, time.cc, toplev.cc, tril.cc, tsearch.cc, typecast.cc, url-handle-manager.cc, url-handle-manager.h, urlwrite.cc, utils.cc, utils.h, variables.cc, variables.h, xdiv.cc, xdiv.h, xnorm.cc, xnorm.h, xpow.cc, xpow.h, __delaunayn__.cc, __fltk_uigetfile__.cc, __glpk__.cc, __init_fltk__.cc, __init_gnuplot__.cc, __ode15__.cc, __voronoi__.cc, audiodevinfo.cc, audioread.cc, convhulln.cc, fftw.cc, gzip.cc, mk-build-env-features.sh, mk-builtins.pl, cdef-class.cc, cdef-class.h, cdef-fwd.h, cdef-manager.cc, cdef-manager.h, cdef-method.cc, cdef-method.h, cdef-object.cc, cdef-object.h, cdef-package.cc, cdef-package.h, cdef-property.cc, cdef-property.h, cdef-utils.cc, cdef-utils.h, ov-base.cc, ov-base.h, ov-bool-mat.cc, ov-builtin.h, ov-cell.cc, ov-class.cc, ov-class.h, ov-classdef.cc, ov-classdef.h, ov-complex.cc, ov-fcn-handle.cc, ov-fcn-handle.h, ov-fcn.h, ov-java.cc, ov-java.h, ov-mex-fcn.h, ov-null-mat.cc, ov-oncleanup.cc, ov-struct.cc, ov-typeinfo.cc, ov-typeinfo.h, ov-usr-fcn.cc, ov-usr-fcn.h, ov.cc, ov.h, octave.cc, octave.h, mk-ops.sh, 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-fcdm-fcdm.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-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-m-cm.cc, op-m-cs.cc, op-m-m.cc, op-m-s.cc, op-m-scm.cc, op-m-sm.cc, op-mi.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, ops.h, anon-fcn-validator.cc, anon-fcn-validator.h, bp-table.cc, bp-table.h, comment-list.cc, comment-list.h, filepos.h, lex.h, lex.ll, oct-lvalue.cc, oct-lvalue.h, oct-parse.yy, parse.h, profiler.cc, profiler.h, pt-anon-scopes.cc, pt-anon-scopes.h, pt-arg-list.cc, pt-arg-list.h, pt-args-block.cc, pt-args-block.h, pt-array-list.cc, pt-array-list.h, pt-assign.cc, pt-assign.h, pt-binop.cc, pt-binop.h, pt-bp.cc, pt-bp.h, pt-cbinop.cc, pt-cbinop.h, pt-cell.cc, pt-cell.h, pt-check.cc, pt-check.h, pt-classdef.cc, pt-classdef.h, pt-cmd.h, pt-colon.cc, pt-colon.h, pt-const.cc, pt-const.h, pt-decl.cc, pt-decl.h, pt-eval.cc, pt-eval.h, pt-except.cc, pt-except.h, pt-exp.cc, pt-exp.h, pt-fcn-handle.cc, pt-fcn-handle.h, pt-id.cc, pt-id.h, pt-idx.cc, pt-idx.h, pt-jump.h, pt-loop.cc, pt-loop.h, pt-mat.cc, pt-mat.h, pt-misc.cc, pt-misc.h, pt-pr-code.cc, pt-pr-code.h, pt-select.cc, pt-select.h, pt-spmd.cc, pt-spmd.h, pt-stmt.cc, pt-stmt.h, pt-tm-const.cc, pt-tm-const.h, pt-unop.cc, pt-unop.h, pt-vm-eval.cc, pt-walk.cc, pt-walk.h, pt.cc, pt.h, token.cc, token.h, Range.cc, Range.h, idx-vector.cc, idx-vector.h, range-fwd.h, CollocWt.cc, CollocWt.h, aepbalance.cc, aepbalance.h, chol.cc, chol.h, gepbalance.cc, gepbalance.h, gsvd.cc, gsvd.h, hess.cc, hess.h, lo-mappers.cc, lo-mappers.h, lo-specfun.cc, lo-specfun.h, lu.cc, lu.h, oct-convn.cc, oct-convn.h, oct-fftw.cc, oct-fftw.h, oct-norm.cc, oct-norm.h, oct-rand.cc, oct-rand.h, oct-spparms.cc, oct-spparms.h, qr.cc, qr.h, qrp.cc, qrp.h, randgamma.cc, randgamma.h, randmtzig.cc, randmtzig.h, randpoisson.cc, randpoisson.h, schur.cc, schur.h, sparse-chol.cc, sparse-chol.h, sparse-lu.cc, sparse-lu.h, sparse-qr.cc, sparse-qr.h, svd.cc, svd.h, child-list.cc, child-list.h, dir-ops.cc, dir-ops.h, file-ops.cc, file-ops.h, file-stat.cc, file-stat.h, lo-sysdep.cc, lo-sysdep.h, lo-sysinfo.cc, lo-sysinfo.h, mach-info.cc, mach-info.h, oct-env.cc, oct-env.h, oct-group.cc, oct-group.h, oct-password.cc, oct-password.h, oct-syscalls.cc, oct-syscalls.h, oct-time.cc, oct-time.h, oct-uname.cc, oct-uname.h, action-container.cc, action-container.h, base-list.h, cmd-edit.cc, cmd-edit.h, cmd-hist.cc, cmd-hist.h, f77-fcn.h, file-info.cc, file-info.h, lo-array-errwarn.cc, lo-array-errwarn.h, lo-hash.cc, lo-hash.h, lo-ieee.h, lo-regexp.cc, lo-regexp.h, lo-utils.cc, lo-utils.h, oct-base64.cc, oct-base64.h, oct-glob.cc, oct-glob.h, oct-inttypes.h, oct-mutex.cc, oct-mutex.h, oct-refcount.h, oct-shlib.cc, oct-shlib.h, oct-sparse.cc, oct-sparse.h, oct-string.h, octave-preserve-stream-state.h, pathsearch.cc, pathsearch.h, quit.cc, quit.h, unwind-prot.cc, unwind-prot.h, url-transfer.cc, url-transfer.h : Use new macros to begin/end C++ namespaces.
author Rik <rik@octave.org>
date Thu, 01 Dec 2022 14:23:45 -0800
parents 83f9f8bda883
children 597f3ee61a48
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
1 ////////////////////////////////////////////////////////////////////////
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
2 //
30564
796f54d4ddbf update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents: 29961
diff changeset
3 // Copyright (C) 2009-2022 The Octave Project Developers
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
4 //
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
5 // See the file COPYRIGHT.md in the top-level directory of this
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
6 // distribution or <https://octave.org/copyright/>.
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
7 //
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
8 // This file is part of Octave.
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
9 //
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
10 // Octave is free software: you can redistribute it and/or modify it
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
11 // under the terms of the GNU General Public License as published by
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
12 // the Free Software Foundation, either version 3 of the License, or
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
13 // (at your option) any later version.
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
14 //
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
15 // Octave is distributed in the hope that it will be useful, but
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
18 // GNU General Public License for more details.
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
19 //
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
20 // You should have received a copy of the GNU General Public License
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
21 // along with Octave; see the file COPYING. If not, see
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
22 // <https://www.gnu.org/licenses/>.
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
23 //
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
24 ////////////////////////////////////////////////////////////////////////
10022
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
25
21724
aba2e6293dd8 use "#if ..." consistently instead of "#ifdef" and "#ifndef"
John W. Eaton <jwe@octave.org>
parents: 21547
diff changeset
26 #if defined (HAVE_CONFIG_H)
21301
40de9f8f23a6 Use '#include "config.h"' rather than <config.h>.
Rik <rik@octave.org>
parents: 21200
diff changeset
27 # include "config.h"
10022
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
28 #endif
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
29
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
30 #include <algorithm>
10053
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
31 #include <deque>
15215
9020dddc925a use std::numeric_limits for integer max and min values
John W. Eaton <jwe@octave.org>
parents: 15195
diff changeset
32 #include <limits>
9020dddc925a use std::numeric_limits for integer max and min values
John W. Eaton <jwe@octave.org>
parents: 15195
diff changeset
33 #include <string>
10022
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
34
19269
65554f5847ac don't include oct-locbuf.h in header files unnecessarily
John W. Eaton <jwe@octave.org>
parents: 19062
diff changeset
35 #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
36
10022
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
37 #include "Cell.h"
23454
30b6eccd6708 use builtin-defun-decls.h to ensure declarations of interpreter functions
John W. Eaton <jwe@octave.org>
parents: 23220
diff changeset
38 #include "builtin-defun-decls.h"
15039
e753177cde93 maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents: 14501
diff changeset
39 #include "defun.h"
23454
30b6eccd6708 use builtin-defun-decls.h to ensure declarations of interpreter functions
John W. Eaton <jwe@octave.org>
parents: 23220
diff changeset
40 #include "errwarn.h"
27130
396d17dcfb9f don't include error.h in ov-base.h
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
41 #include "error.h"
23454
30b6eccd6708 use builtin-defun-decls.h to ensure declarations of interpreter functions
John W. Eaton <jwe@octave.org>
parents: 23220
diff changeset
42 #include "ov.h"
10022
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
43 #include "unwind-prot.h"
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
44 #include "utils.h"
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
45
31605
e88a07dec498 maint: Use macros to begin/end C++ namespaces.
Rik <rik@octave.org>
parents: 30565
diff changeset
46 OCTAVE_BEGIN_NAMESPACE(octave)
29958
32c3a5805893 move DEFUN and DEFMETHOD functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29359
diff changeset
47
17787
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
48 // This allows safe indexing with char.
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
49 // In C++, char may be (and often is) signed!
10022
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
50 #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
51 #define TABSIZE (std::numeric_limits<unsigned char>::max () + 1)
10022
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
52
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
53 // This is the quick search algorithm, as described at
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
54 // 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
55 static void
10022
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
56 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
57 octave_idx_type *table)
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 const char *x = needle.data ();
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
60 octave_idx_type m = needle.numel ();
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
61
17787
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
62 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
63 table[i] = m + 1;
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
64 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
65 table[ORD(x[i])] = m - i;
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
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11572
diff changeset
68 static Array<octave_idx_type>
10022
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
69 qs_search (const Array<char>& needle,
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
70 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
71 const octave_idx_type *table,
10053
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
72 bool overlaps = true)
10022
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
73 {
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
74 const char *x = needle.data ();
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
75 octave_idx_type m = needle.numel ();
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
76 const char *y = haystack.data ();
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
77 octave_idx_type n = haystack.numel ();
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
78
10053
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
79 // 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
80 // the operation we need.
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
81 std::deque<octave_idx_type> accum;
10133
2e4fc7fdba15 optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents: 10086
diff changeset
82 if (m == 1)
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 // Looking for a single character.
2e4fc7fdba15 optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents: 10086
diff changeset
85 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
86 {
2e4fc7fdba15 optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents: 10086
diff changeset
87 if (y[i] == x[0])
2e4fc7fdba15 optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents: 10086
diff changeset
88 accum.push_back (i);
2e4fc7fdba15 optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents: 10086
diff changeset
89 }
2e4fc7fdba15 optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents: 10086
diff changeset
90 }
2e4fc7fdba15 optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents: 10086
diff changeset
91 else if (m == 2)
10022
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
92 {
10133
2e4fc7fdba15 optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents: 10086
diff changeset
93 // Two characters.
2e4fc7fdba15 optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents: 10086
diff changeset
94 if (overlaps)
2e4fc7fdba15 optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents: 10086
diff changeset
95 {
2e4fc7fdba15 optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents: 10086
diff changeset
96 for (octave_idx_type i = 0; i < n-1; i++)
2e4fc7fdba15 optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents: 10086
diff changeset
97 {
2e4fc7fdba15 optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents: 10086
diff changeset
98 if (y[i] == x[0] && y[i+1] == x[1])
2e4fc7fdba15 optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents: 10086
diff changeset
99 accum.push_back (i);
2e4fc7fdba15 optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents: 10086
diff changeset
100 }
2e4fc7fdba15 optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents: 10086
diff changeset
101 }
2e4fc7fdba15 optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents: 10086
diff changeset
102 else
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 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
105 {
2e4fc7fdba15 optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents: 10086
diff changeset
106 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
107 accum.push_back (i++);
2e4fc7fdba15 optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents: 10086
diff changeset
108 }
2e4fc7fdba15 optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents: 10086
diff changeset
109 }
2e4fc7fdba15 optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents: 10086
diff changeset
110 }
2e4fc7fdba15 optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents: 10086
diff changeset
111 else if (n >= m)
2e4fc7fdba15 optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents: 10086
diff changeset
112 {
2e4fc7fdba15 optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents: 10086
diff changeset
113 // General case.
10022
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
114 octave_idx_type j = 0;
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
115
10053
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
116 if (overlaps)
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
117 {
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11572
diff changeset
118 while (j < n - m)
10053
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 if (std::equal (x, x + m, y + j))
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
121 accum.push_back (j);
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
122 j += table[ORD(y[j + m])];
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 }
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
125 else
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
126 {
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11572
diff changeset
127 while (j < n - m)
10053
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 if (std::equal (x, x + m, y + j))
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 accum.push_back (j);
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
132 j += m;
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 else
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
135 j += table[ORD(y[j + m])];
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
136 }
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
137 }
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
138
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
139 if (j == n - m && std::equal (x, x + m, y + j))
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
140 accum.push_back (j);
10022
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
141 }
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
142
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
143 octave_idx_type nmatch = accum.size ();
10258
e317791645c4 64-bit fixes
John W. Eaton <jwe@octave.org>
parents: 10133
diff changeset
144 octave_idx_type one = 1;
e317791645c4 64-bit fixes
John W. Eaton <jwe@octave.org>
parents: 10133
diff changeset
145 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
146 octave_idx_type k = 0;
22862
e365e87371a3 maint: Use C++ range feature to simplify some for loops in libinterp/corefcn.
Rik <rik@octave.org>
parents: 22755
diff changeset
147 for (const auto& idx : accum)
e365e87371a3 maint: Use C++ range feature to simplify some for loops in libinterp/corefcn.
Rik <rik@octave.org>
parents: 22755
diff changeset
148 result.xelem (k++) = idx;
10022
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
149
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
150 return result;
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
151 }
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
152
15039
e753177cde93 maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents: 14501
diff changeset
153 DEFUN (strfind, args, ,
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21751
diff changeset
154 doc: /* -*- texinfo -*-
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21751
diff changeset
155 @deftypefn {} {@var{idx} =} strfind (@var{str}, @var{pattern})
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21751
diff changeset
156 @deftypefnx {} {@var{idx} =} strfind (@var{cellstr}, @var{pattern})
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21751
diff changeset
157 @deftypefnx {} {@var{idx} =} strfind (@dots{}, "overlaps", @var{val})
27315
86c5dd1283b6 Add support for "ForceCellOutput" option to strfind (bug #56699).
Guillaume Flandin <guillaume.offline@gmail.com>
parents: 27130
diff changeset
158 @deftypefnx {} {@var{idx} =} strfind (@dots{}, "forcecelloutput", @var{val})
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21751
diff changeset
159 Search for @var{pattern} in the string @var{str} and return the starting
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21751
diff changeset
160 index of every such occurrence in the vector @var{idx}.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21751
diff changeset
161
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21751
diff changeset
162 If there is no such occurrence, or if @var{pattern} is longer than
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21751
diff changeset
163 @var{str}, or if @var{pattern} itself is empty, then @var{idx} is the empty
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21751
diff changeset
164 array @code{[]}.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21751
diff changeset
165
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21751
diff changeset
166 The optional argument @qcode{"overlaps"} determines whether the pattern
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21751
diff changeset
167 can match at every position in @var{str} (true), or only for unique
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21751
diff changeset
168 occurrences of the complete pattern (false). The default is true.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21751
diff changeset
169
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21751
diff changeset
170 If a cell array of strings @var{cellstr} is specified then @var{idx} is a
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21751
diff changeset
171 cell array of vectors, as specified above.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21751
diff changeset
172
27315
86c5dd1283b6 Add support for "ForceCellOutput" option to strfind (bug #56699).
Guillaume Flandin <guillaume.offline@gmail.com>
parents: 27130
diff changeset
173 The optional argument @qcode{"forcecelloutput"} forces @var{idx} to be
86c5dd1283b6 Add support for "ForceCellOutput" option to strfind (bug #56699).
Guillaume Flandin <guillaume.offline@gmail.com>
parents: 27130
diff changeset
174 returned as a cell array of vectors. The default is false.
86c5dd1283b6 Add support for "ForceCellOutput" option to strfind (bug #56699).
Guillaume Flandin <guillaume.offline@gmail.com>
parents: 27130
diff changeset
175
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21751
diff changeset
176 Examples:
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21751
diff changeset
177
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21751
diff changeset
178 @example
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21751
diff changeset
179 @group
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21751
diff changeset
180 strfind ("abababa", "aba")
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21751
diff changeset
181 @result{} [1, 3, 5]
27796
e7dd7d98a058 doc: grammarcheck .txi files before 6.1 release.
Rik <rik@octave.org>
parents: 27315
diff changeset
182 @end group
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21751
diff changeset
183
27796
e7dd7d98a058 doc: grammarcheck .txi files before 6.1 release.
Rik <rik@octave.org>
parents: 27315
diff changeset
184 @group
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21751
diff changeset
185 strfind ("abababa", "aba", "overlaps", false)
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21751
diff changeset
186 @result{} [1, 5]
27796
e7dd7d98a058 doc: grammarcheck .txi files before 6.1 release.
Rik <rik@octave.org>
parents: 27315
diff changeset
187 @end group
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21751
diff changeset
188
27796
e7dd7d98a058 doc: grammarcheck .txi files before 6.1 release.
Rik <rik@octave.org>
parents: 27315
diff changeset
189 @group
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21751
diff changeset
190 strfind (@{"abababa", "bebebe", "ab"@}, "aba")
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21751
diff changeset
191 @result{}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21751
diff changeset
192 @{
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21751
diff changeset
193 [1,1] =
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21751
diff changeset
194
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21751
diff changeset
195 1 3 5
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21751
diff changeset
196
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21751
diff changeset
197 [1,2] = [](1x0)
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21751
diff changeset
198 [1,3] = [](1x0)
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21751
diff changeset
199 @}
27796
e7dd7d98a058 doc: grammarcheck .txi files before 6.1 release.
Rik <rik@octave.org>
parents: 27315
diff changeset
200 @end group
27315
86c5dd1283b6 Add support for "ForceCellOutput" option to strfind (bug #56699).
Guillaume Flandin <guillaume.offline@gmail.com>
parents: 27130
diff changeset
201
27796
e7dd7d98a058 doc: grammarcheck .txi files before 6.1 release.
Rik <rik@octave.org>
parents: 27315
diff changeset
202 @group
27315
86c5dd1283b6 Add support for "ForceCellOutput" option to strfind (bug #56699).
Guillaume Flandin <guillaume.offline@gmail.com>
parents: 27130
diff changeset
203 strfind ("abababa", "aba", "forcecelloutput", true)
86c5dd1283b6 Add support for "ForceCellOutput" option to strfind (bug #56699).
Guillaume Flandin <guillaume.offline@gmail.com>
parents: 27130
diff changeset
204 @result{}
86c5dd1283b6 Add support for "ForceCellOutput" option to strfind (bug #56699).
Guillaume Flandin <guillaume.offline@gmail.com>
parents: 27130
diff changeset
205 @{
86c5dd1283b6 Add support for "ForceCellOutput" option to strfind (bug #56699).
Guillaume Flandin <guillaume.offline@gmail.com>
parents: 27130
diff changeset
206 [1,1] =
86c5dd1283b6 Add support for "ForceCellOutput" option to strfind (bug #56699).
Guillaume Flandin <guillaume.offline@gmail.com>
parents: 27130
diff changeset
207
86c5dd1283b6 Add support for "ForceCellOutput" option to strfind (bug #56699).
Guillaume Flandin <guillaume.offline@gmail.com>
parents: 27130
diff changeset
208 1 3 5
86c5dd1283b6 Add support for "ForceCellOutput" option to strfind (bug #56699).
Guillaume Flandin <guillaume.offline@gmail.com>
parents: 27130
diff changeset
209 @}
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21751
diff changeset
210 @end group
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21751
diff changeset
211 @end example
25760
2ccad4396afc findstr.m: Make m-file a legacy function.
Rik <rik@octave.org>
parents: 25646
diff changeset
212 @seealso{regexp, regexpi, find}
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21751
diff changeset
213 @end deftypefn */)
10022
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
214 {
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
215 int nargin = args.length ();
20801
a542a9bf177e eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20172
diff changeset
216
a542a9bf177e eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20172
diff changeset
217 if (nargin != 4 && nargin != 2)
a542a9bf177e eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20172
diff changeset
218 print_usage ();
a542a9bf177e eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20172
diff changeset
219
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
220 bool overlaps = true;
27315
86c5dd1283b6 Add support for "ForceCellOutput" option to strfind (bug #56699).
Guillaume Flandin <guillaume.offline@gmail.com>
parents: 27130
diff changeset
221 bool forcecelloutput = false;
20801
a542a9bf177e eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20172
diff changeset
222 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
223 {
20939
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20853
diff changeset
224 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
225 error ("strfind: invalid optional arguments");
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20853
diff changeset
226
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20853
diff changeset
227 std::string opt = args(2).string_value ();
27315
86c5dd1283b6 Add support for "ForceCellOutput" option to strfind (bug #56699).
Guillaume Flandin <guillaume.offline@gmail.com>
parents: 27130
diff changeset
228 std::transform (opt.begin (), opt.end (), opt.begin (), tolower);
20801
a542a9bf177e eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20172
diff changeset
229
20939
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20853
diff changeset
230 if (opt == "overlaps")
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20853
diff changeset
231 overlaps = args(3).bool_value ();
27315
86c5dd1283b6 Add support for "ForceCellOutput" option to strfind (bug #56699).
Guillaume Flandin <guillaume.offline@gmail.com>
parents: 27130
diff changeset
232 else if (opt == "forcecelloutput")
86c5dd1283b6 Add support for "ForceCellOutput" option to strfind (bug #56699).
Guillaume Flandin <guillaume.offline@gmail.com>
parents: 27130
diff changeset
233 forcecelloutput = args(3).bool_value ();
10677
21defab4207c make strrep not skipping overlaps by default, allow an option in both strfind/strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10521
diff changeset
234 else
20939
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20853
diff changeset
235 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
236 }
10022
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
237
20939
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20853
diff changeset
238 octave_value retval;
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20853
diff changeset
239
20801
a542a9bf177e eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20172
diff changeset
240 octave_value argstr = args(0);
a542a9bf177e eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20172
diff changeset
241 octave_value argpat = args(1);
a542a9bf177e eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20172
diff changeset
242
a542a9bf177e eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20172
diff changeset
243 if (argpat.is_string ())
10022
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
244 {
20801
a542a9bf177e eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20172
diff changeset
245 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
246 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
247 qs_preprocess (needle, table);
10022
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
248
20801
a542a9bf177e eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20172
diff changeset
249 if (argstr.is_string ())
27315
86c5dd1283b6 Add support for "ForceCellOutput" option to strfind (bug #56699).
Guillaume Flandin <guillaume.offline@gmail.com>
parents: 27130
diff changeset
250 {
86c5dd1283b6 Add support for "ForceCellOutput" option to strfind (bug #56699).
Guillaume Flandin <guillaume.offline@gmail.com>
parents: 27130
diff changeset
251 if (argpat.isempty ())
86c5dd1283b6 Add support for "ForceCellOutput" option to strfind (bug #56699).
Guillaume Flandin <guillaume.offline@gmail.com>
parents: 27130
diff changeset
252 // Return a null matrix for null pattern for MW compatibility
86c5dd1283b6 Add support for "ForceCellOutput" option to strfind (bug #56699).
Guillaume Flandin <guillaume.offline@gmail.com>
parents: 27130
diff changeset
253 retval = Matrix ();
86c5dd1283b6 Add support for "ForceCellOutput" option to strfind (bug #56699).
Guillaume Flandin <guillaume.offline@gmail.com>
parents: 27130
diff changeset
254 else
86c5dd1283b6 Add support for "ForceCellOutput" option to strfind (bug #56699).
Guillaume Flandin <guillaume.offline@gmail.com>
parents: 27130
diff changeset
255 retval = octave_value (qs_search (needle,
86c5dd1283b6 Add support for "ForceCellOutput" option to strfind (bug #56699).
Guillaume Flandin <guillaume.offline@gmail.com>
parents: 27130
diff changeset
256 argstr.char_array_value (),
86c5dd1283b6 Add support for "ForceCellOutput" option to strfind (bug #56699).
Guillaume Flandin <guillaume.offline@gmail.com>
parents: 27130
diff changeset
257 table, overlaps),
86c5dd1283b6 Add support for "ForceCellOutput" option to strfind (bug #56699).
Guillaume Flandin <guillaume.offline@gmail.com>
parents: 27130
diff changeset
258 true, true);
86c5dd1283b6 Add support for "ForceCellOutput" option to strfind (bug #56699).
Guillaume Flandin <guillaume.offline@gmail.com>
parents: 27130
diff changeset
259 if (forcecelloutput)
86c5dd1283b6 Add support for "ForceCellOutput" option to strfind (bug #56699).
Guillaume Flandin <guillaume.offline@gmail.com>
parents: 27130
diff changeset
260 retval = Cell (retval);
86c5dd1283b6 Add support for "ForceCellOutput" option to strfind (bug #56699).
Guillaume Flandin <guillaume.offline@gmail.com>
parents: 27130
diff changeset
261 }
23576
00e518162fda maint: Deprecate is_cell and replace with iscell.
Rik <rik@octave.org>
parents: 23454
diff changeset
262 else if (argstr.iscell ())
20801
a542a9bf177e eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20172
diff changeset
263 {
a542a9bf177e eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20172
diff changeset
264 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
265 Cell retc (argsc.dims ());
a542a9bf177e eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20172
diff changeset
266 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
267
a542a9bf177e eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20172
diff changeset
268 for (octave_idx_type i = 0; i < ns; i++)
10022
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
269 {
20801
a542a9bf177e eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20172
diff changeset
270 octave_value argse = argsc(i);
20939
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20853
diff changeset
271 if (! argse.is_string ())
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20853
diff changeset
272 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
273
23577
80c42f4cca13 maint: Deprecate is_empty and replace with isempty.
Rik <rik@octave.org>
parents: 23576
diff changeset
274 if (argpat.isempty ())
20939
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20853
diff changeset
275 retc(i) = Matrix ();
20801
a542a9bf177e eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20172
diff changeset
276 else
20939
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20853
diff changeset
277 retc(i) = octave_value (qs_search (needle,
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20853
diff changeset
278 argse.char_array_value (),
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20853
diff changeset
279 table, overlaps),
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20853
diff changeset
280 true, true);
10022
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
281 }
20801
a542a9bf177e eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20172
diff changeset
282
a542a9bf177e eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20172
diff changeset
283 retval = retc;
10022
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
284 }
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
285 else
20801
a542a9bf177e eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20172
diff changeset
286 error ("strfind: first argument must be a string or cell array of strings");
10022
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
287 }
23576
00e518162fda maint: Deprecate is_cell and replace with iscell.
Rik <rik@octave.org>
parents: 23454
diff changeset
288 else if (argpat.iscell ())
29961
7d6709900da7 eliminate octave:: namespace tags in DEFUN and DEFMETHOD and more
John W. Eaton <jwe@octave.org>
parents: 29958
diff changeset
289 retval = do_simple_cellfun (Fstrfind, "strfind", args);
10022
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
290 else
20801
a542a9bf177e eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20172
diff changeset
291 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
292
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
293 return retval;
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
294 }
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
295
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
296 /*
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
297 %!assert (strfind ("abababa", "aba"), [1, 3, 5])
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
298 %!assert (strfind ("abababa", "aba", "overlaps", false), [1, 5])
27315
86c5dd1283b6 Add support for "ForceCellOutput" option to strfind (bug #56699).
Guillaume Flandin <guillaume.offline@gmail.com>
parents: 27130
diff changeset
299 %!assert (strfind ("abababa", "aba", "forcecelloutput", false), [1, 3, 5])
86c5dd1283b6 Add support for "ForceCellOutput" option to strfind (bug #56699).
Guillaume Flandin <guillaume.offline@gmail.com>
parents: 27130
diff changeset
300 %!assert (strfind ("abababa", "aba", "forcecelloutput", true), {[1, 3, 5]})
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
301 %!assert (strfind ({"abababa", "bla", "bla"}, "a"), {[1, 3, 5, 7], 3, 3})
30346
91c6288781ba maint: Shorten some long lines in libinterp to <= 80 characters (bug #57599)
Rik <rik@octave.org>
parents: 29961
diff changeset
302 %!assert (strfind ({"abababa", "bla", "bla"}, "a", "forcecelloutput", false),
91c6288781ba maint: Shorten some long lines in libinterp to <= 80 characters (bug #57599)
Rik <rik@octave.org>
parents: 29961
diff changeset
303 %! {[1, 3, 5, 7], 3, 3})
91c6288781ba maint: Shorten some long lines in libinterp to <= 80 characters (bug #57599)
Rik <rik@octave.org>
parents: 29961
diff changeset
304 %!assert (strfind ({"abababa", "bla", "bla"}, "a", "forcecelloutput", true),
91c6288781ba maint: Shorten some long lines in libinterp to <= 80 characters (bug #57599)
Rik <rik@octave.org>
parents: 29961
diff changeset
305 %! {[1, 3, 5, 7], 3, 3})
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
306 %!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
307 %!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
308 %!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
309 %!assert (strfind ({"abc", "def"}, ""), {[], []})
10022
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
310
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
311 %!error strfind ()
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
312 %!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
313 %!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
314 %!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
315 %!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
316 %!error <PATTERN must be a string> strfind ("foo", 100)
10022
a14dc255427f omitted file in last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
317 */
10053
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
318
10074
5e2b4b7967cc allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10053
diff changeset
319 static Array<char>
5e2b4b7967cc allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10053
diff changeset
320 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
321 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
322 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
323 bool overlaps = true)
10074
5e2b4b7967cc allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10053
diff changeset
324 {
5e2b4b7967cc allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10053
diff changeset
325 Array<char> ret = str;
5e2b4b7967cc allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10053
diff changeset
326
18100
6a71e5030df5 Follow coding convention of defining and initializing only 1 variable per line in liboctinterp.
Rik <rik@octave.org>
parents: 17787
diff changeset
327 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
328 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
329 octave_idx_type rsiz = rep.numel ();
10074
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 if (psiz != 0)
5e2b4b7967cc allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10053
diff changeset
332 {
5e2b4b7967cc allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10053
diff changeset
333 // 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
334 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
335 octave_idx_type nidx = idx.numel ();
5e2b4b7967cc allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10053
diff changeset
336
5e2b4b7967cc allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10053
diff changeset
337 if (nidx)
5e2b4b7967cc allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10053
diff changeset
338 {
5e2b4b7967cc allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10053
diff changeset
339 // 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
340 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
341 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
342 {
21defab4207c make strrep not skipping overlaps by default, allow an option in both strfind/strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10521
diff changeset
343 retsiz = 0;
21751
b571fc85953f maint: Use two spaces after period to indicate sentence break.
Rik <rik@octave.org>
parents: 21724
diff changeset
344 // OMG. Is this the "right answer" MW always looks for, or
10677
21defab4207c make strrep not skipping overlaps by default, allow an option in both strfind/strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10521
diff changeset
345 // 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
346 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
347 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
348 {
21defab4207c make strrep not skipping overlaps by default, allow an option in both strfind/strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10521
diff changeset
349 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
350 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
351 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
352 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
353 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
354 }
21defab4207c make strrep not skipping overlaps by default, allow an option in both strfind/strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10521
diff changeset
355
21defab4207c make strrep not skipping overlaps by default, allow an option in both strfind/strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10521
diff changeset
356 retsiz += siz - 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
357 }
21defab4207c make strrep not skipping overlaps by default, allow an option in both strfind/strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10521
diff changeset
358 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
359 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
360
18763
86818f2c4a92 Return 0x0 empty matrix from strrep for Matlab compatibility (bug #42341).
Rik <rik@octave.org>
parents: 18100
diff changeset
361 if (retsiz == 0)
86818f2c4a92 Return 0x0 empty matrix from strrep for Matlab compatibility (bug #42341).
Rik <rik@octave.org>
parents: 18100
diff changeset
362 ret.clear (dim_vector (0, 0));
19597
db92e7e28e1f strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 19386
diff changeset
363 else
10074
5e2b4b7967cc allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10053
diff changeset
364 {
18763
86818f2c4a92 Return 0x0 empty matrix from strrep for Matlab compatibility (bug #42341).
Rik <rik@octave.org>
parents: 18100
diff changeset
365 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
366 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
367 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
368 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
369
86818f2c4a92 Return 0x0 empty matrix from strrep for Matlab compatibility (bug #42341).
Rik <rik@octave.org>
parents: 18100
diff changeset
370 octave_idx_type k = 0;
86818f2c4a92 Return 0x0 empty matrix from strrep for Matlab compatibility (bug #42341).
Rik <rik@octave.org>
parents: 18100
diff changeset
371 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
372 {
86818f2c4a92 Return 0x0 empty matrix from strrep for Matlab compatibility (bug #42341).
Rik <rik@octave.org>
parents: 18100
diff changeset
373 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
374 if (j >= k)
86818f2c4a92 Return 0x0 empty matrix from strrep for Matlab compatibility (bug #42341).
Rik <rik@octave.org>
parents: 18100
diff changeset
375 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
376 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
377 k = j + psiz;
86818f2c4a92 Return 0x0 empty matrix from strrep for Matlab compatibility (bug #42341).
Rik <rik@octave.org>
parents: 18100
diff changeset
378 }
86818f2c4a92 Return 0x0 empty matrix from strrep for Matlab compatibility (bug #42341).
Rik <rik@octave.org>
parents: 18100
diff changeset
379
86818f2c4a92 Return 0x0 empty matrix from strrep for Matlab compatibility (bug #42341).
Rik <rik@octave.org>
parents: 18100
diff changeset
380 std::copy (src + k, src + siz, dest);
10074
5e2b4b7967cc allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10053
diff changeset
381 }
5e2b4b7967cc allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10053
diff changeset
382 }
5e2b4b7967cc allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10053
diff changeset
383 }
5e2b4b7967cc allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10053
diff changeset
384
5e2b4b7967cc allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10053
diff changeset
385 return ret;
5e2b4b7967cc allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10053
diff changeset
386 }
5e2b4b7967cc allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10053
diff changeset
387
15039
e753177cde93 maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents: 14501
diff changeset
388 DEFUN (strrep, args, ,
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21751
diff changeset
389 doc: /* -*- texinfo -*-
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21751
diff changeset
390 @deftypefn {} {@var{newstr} =} strrep (@var{str}, @var{ptn}, @var{rep})
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21751
diff changeset
391 @deftypefnx {} {@var{newstr} =} strrep (@var{cellstr}, @var{ptn}, @var{rep})
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21751
diff changeset
392 @deftypefnx {} {@var{newstr} =} strrep (@dots{}, "overlaps", @var{val})
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21751
diff changeset
393 Replace all occurrences of the pattern @var{ptn} in the string @var{str}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21751
diff changeset
394 with the string @var{rep} and return the result.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21751
diff changeset
395
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21751
diff changeset
396 The optional argument @qcode{"overlaps"} determines whether the pattern
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21751
diff changeset
397 can match at every position in @var{str} (true), or only for unique
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21751
diff changeset
398 occurrences of the complete pattern (false). The default is true.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21751
diff changeset
399
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21751
diff changeset
400 @var{s} may also be a cell array of strings, in which case the replacement
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21751
diff changeset
401 is done for each element and a cell array is returned.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21751
diff changeset
402
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21751
diff changeset
403 Example:
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21751
diff changeset
404
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21751
diff changeset
405 @example
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21751
diff changeset
406 @group
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21751
diff changeset
407 strrep ("This is a test string", "is", "&%$")
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21751
diff changeset
408 @result{} "Th&%$ &%$ a test string"
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21751
diff changeset
409 @end group
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21751
diff changeset
410 @end example
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21751
diff changeset
411
25760
2ccad4396afc findstr.m: Make m-file a legacy function.
Rik <rik@octave.org>
parents: 25646
diff changeset
412 @seealso{regexprep, strfind}
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21751
diff changeset
413 @end deftypefn */)
10053
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
414 {
20801
a542a9bf177e eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20172
diff changeset
415 int nargin = args.length ();
a542a9bf177e eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20172
diff changeset
416
a542a9bf177e eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20172
diff changeset
417 if (nargin != 3 && nargin != 5)
a542a9bf177e eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20172
diff changeset
418 print_usage ();
a542a9bf177e eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20172
diff changeset
419
20939
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20853
diff changeset
420 bool overlaps = true;
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20853
diff changeset
421
20801
a542a9bf177e eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20172
diff changeset
422 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
423 {
20939
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20853
diff changeset
424 if (! args(3).is_string () || ! args(4).is_scalar_type ())
28746
7dcc4cb6da53 Expand sttrrep to work an character matrices (bug #57867)
Rik <rik@octave.org>
parents: 27923
diff changeset
425 error ("strrep: invalid optional argument");
20939
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20853
diff changeset
426
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20853
diff changeset
427 std::string opt = args(3).string_value ();
28746
7dcc4cb6da53 Expand sttrrep to work an character matrices (bug #57867)
Rik <rik@octave.org>
parents: 27923
diff changeset
428 if (opt != "overlaps")
20939
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20853
diff changeset
429 error ("strrep: unknown option: %s", opt.c_str ());
28746
7dcc4cb6da53 Expand sttrrep to work an character matrices (bug #57867)
Rik <rik@octave.org>
parents: 27923
diff changeset
430
7dcc4cb6da53 Expand sttrrep to work an character matrices (bug #57867)
Rik <rik@octave.org>
parents: 27923
diff changeset
431 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
432 }
10053
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
433
20939
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20853
diff changeset
434 octave_value retval;
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20853
diff changeset
435
28746
7dcc4cb6da53 Expand sttrrep to work an character matrices (bug #57867)
Rik <rik@octave.org>
parents: 27923
diff changeset
436 // Aliasing for better code readability
20801
a542a9bf177e eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20172
diff changeset
437 octave_value argstr = args(0);
a542a9bf177e eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20172
diff changeset
438 octave_value argpat = args(1);
a542a9bf177e eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20172
diff changeset
439 octave_value argrep = args(2);
a542a9bf177e eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20172
diff changeset
440
a542a9bf177e eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20172
diff changeset
441 if (argpat.is_string () && argrep.is_string ())
10053
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
442 {
20801
a542a9bf177e eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20172
diff changeset
443 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
444 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
445
a542a9bf177e eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20172
diff changeset
446 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
447 qs_preprocess (pat, table);
10074
5e2b4b7967cc allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10053
diff changeset
448
20801
a542a9bf177e eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20172
diff changeset
449 if (argstr.is_string ())
28746
7dcc4cb6da53 Expand sttrrep to work an character matrices (bug #57867)
Rik <rik@octave.org>
parents: 27923
diff changeset
450 if (argstr.rows () == 1) // most common case of a single string
7dcc4cb6da53 Expand sttrrep to work an character matrices (bug #57867)
Rik <rik@octave.org>
parents: 27923
diff changeset
451 retval = qs_replace (argstr.char_array_value (), pat, rep,
7dcc4cb6da53 Expand sttrrep to work an character matrices (bug #57867)
Rik <rik@octave.org>
parents: 27923
diff changeset
452 table, overlaps);
7dcc4cb6da53 Expand sttrrep to work an character matrices (bug #57867)
Rik <rik@octave.org>
parents: 27923
diff changeset
453 else
7dcc4cb6da53 Expand sttrrep to work an character matrices (bug #57867)
Rik <rik@octave.org>
parents: 27923
diff changeset
454 {
7dcc4cb6da53 Expand sttrrep to work an character matrices (bug #57867)
Rik <rik@octave.org>
parents: 27923
diff changeset
455 const charMatrix argchm = argstr.char_matrix_value ();
7dcc4cb6da53 Expand sttrrep to work an character matrices (bug #57867)
Rik <rik@octave.org>
parents: 27923
diff changeset
456 octave_idx_type nel = argchm.rows ();
7dcc4cb6da53 Expand sttrrep to work an character matrices (bug #57867)
Rik <rik@octave.org>
parents: 27923
diff changeset
457 octave_idx_type nc = argchm.columns ();
7dcc4cb6da53 Expand sttrrep to work an character matrices (bug #57867)
Rik <rik@octave.org>
parents: 27923
diff changeset
458 charMatrix retchm (nel, 0);
7dcc4cb6da53 Expand sttrrep to work an character matrices (bug #57867)
Rik <rik@octave.org>
parents: 27923
diff changeset
459
7dcc4cb6da53 Expand sttrrep to work an character matrices (bug #57867)
Rik <rik@octave.org>
parents: 27923
diff changeset
460 for (octave_idx_type i = 0; i < nel; i++)
7dcc4cb6da53 Expand sttrrep to work an character matrices (bug #57867)
Rik <rik@octave.org>
parents: 27923
diff changeset
461 {
7dcc4cb6da53 Expand sttrrep to work an character matrices (bug #57867)
Rik <rik@octave.org>
parents: 27923
diff changeset
462 charMatrix rowchm;
7dcc4cb6da53 Expand sttrrep to work an character matrices (bug #57867)
Rik <rik@octave.org>
parents: 27923
diff changeset
463 rowchm = qs_replace (argchm.extract (i, 0, i, nc-1),
7dcc4cb6da53 Expand sttrrep to work an character matrices (bug #57867)
Rik <rik@octave.org>
parents: 27923
diff changeset
464 pat, rep, table, overlaps);
7dcc4cb6da53 Expand sttrrep to work an character matrices (bug #57867)
Rik <rik@octave.org>
parents: 27923
diff changeset
465 retchm.insert (rowchm, i, 0);
7dcc4cb6da53 Expand sttrrep to work an character matrices (bug #57867)
Rik <rik@octave.org>
parents: 27923
diff changeset
466 }
7dcc4cb6da53 Expand sttrrep to work an character matrices (bug #57867)
Rik <rik@octave.org>
parents: 27923
diff changeset
467
7dcc4cb6da53 Expand sttrrep to work an character matrices (bug #57867)
Rik <rik@octave.org>
parents: 27923
diff changeset
468 retval = retchm;
7dcc4cb6da53 Expand sttrrep to work an character matrices (bug #57867)
Rik <rik@octave.org>
parents: 27923
diff changeset
469 }
23576
00e518162fda maint: Deprecate is_cell and replace with iscell.
Rik <rik@octave.org>
parents: 23454
diff changeset
470 else if (argstr.iscell ())
20801
a542a9bf177e eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20172
diff changeset
471 {
28746
7dcc4cb6da53 Expand sttrrep to work an character matrices (bug #57867)
Rik <rik@octave.org>
parents: 27923
diff changeset
472 const Cell argcell = argstr.cell_value ();
7dcc4cb6da53 Expand sttrrep to work an character matrices (bug #57867)
Rik <rik@octave.org>
parents: 27923
diff changeset
473 if (! argcell.iscellstr ())
7dcc4cb6da53 Expand sttrrep to work an character matrices (bug #57867)
Rik <rik@octave.org>
parents: 27923
diff changeset
474 error ("strrep: each element of S must be a string");
10053
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
475
28746
7dcc4cb6da53 Expand sttrrep to work an character matrices (bug #57867)
Rik <rik@octave.org>
parents: 27923
diff changeset
476 Cell retcell (argcell.dims ());
7dcc4cb6da53 Expand sttrrep to work an character matrices (bug #57867)
Rik <rik@octave.org>
parents: 27923
diff changeset
477 octave_idx_type nel = argcell.numel ();
7dcc4cb6da53 Expand sttrrep to work an character matrices (bug #57867)
Rik <rik@octave.org>
parents: 27923
diff changeset
478
7dcc4cb6da53 Expand sttrrep to work an character matrices (bug #57867)
Rik <rik@octave.org>
parents: 27923
diff changeset
479 for (octave_idx_type i = 0; i < nel; i++)
10053
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
480 {
28746
7dcc4cb6da53 Expand sttrrep to work an character matrices (bug #57867)
Rik <rik@octave.org>
parents: 27923
diff changeset
481 retcell(i) = qs_replace (argcell(i).char_array_value (),
7dcc4cb6da53 Expand sttrrep to work an character matrices (bug #57867)
Rik <rik@octave.org>
parents: 27923
diff changeset
482 pat, rep, table, overlaps);
20801
a542a9bf177e eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20172
diff changeset
483 }
10053
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
484
28746
7dcc4cb6da53 Expand sttrrep to work an character matrices (bug #57867)
Rik <rik@octave.org>
parents: 27923
diff changeset
485 retval = retcell;
10053
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
486 }
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
487 else
20801
a542a9bf177e eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20172
diff changeset
488 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
489 }
23576
00e518162fda maint: Deprecate is_cell and replace with iscell.
Rik <rik@octave.org>
parents: 23454
diff changeset
490 else if (argpat.iscell () || argrep.iscell ())
29961
7d6709900da7 eliminate octave:: namespace tags in DEFUN and DEFMETHOD and more
John W. Eaton <jwe@octave.org>
parents: 29958
diff changeset
491 retval = do_simple_cellfun (Fstrrep, "strrep", args);
10053
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
492 else
20801
a542a9bf177e eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20172
diff changeset
493 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
494
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
495 return retval;
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
496 }
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
497
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
498 /*
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
499 %!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
500 %! "Th&%$ &%$ a test string")
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
501 %!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
502 %!assert (strrep ("abababc", "abab", "xyz", "overlaps", false), "xyzabc")
28746
7dcc4cb6da53 Expand sttrrep to work an character matrices (bug #57867)
Rik <rik@octave.org>
parents: 27923
diff changeset
503 %!assert (strrep ({"Hello World"; "Goodbye World"}, "World", "Jane"),
7dcc4cb6da53 Expand sttrrep to work an character matrices (bug #57867)
Rik <rik@octave.org>
parents: 27923
diff changeset
504 %! {"Hello Jane"; "Goodbye Jane"})
7dcc4cb6da53 Expand sttrrep to work an character matrices (bug #57867)
Rik <rik@octave.org>
parents: 27923
diff changeset
505 %!assert (strrep (char ("Hello World", "Goodbye World"), "World", "Jane"),
7dcc4cb6da53 Expand sttrrep to work an character matrices (bug #57867)
Rik <rik@octave.org>
parents: 27923
diff changeset
506 %! char ("Hello Jane", "Goodbye Jane"))
10053
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
507
18763
86818f2c4a92 Return 0x0 empty matrix from strrep for Matlab compatibility (bug #42341).
Rik <rik@octave.org>
parents: 18100
diff changeset
508 %!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
509
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
510 %!error strrep ()
28746
7dcc4cb6da53 Expand sttrrep to work an character matrices (bug #57867)
Rik <rik@octave.org>
parents: 27923
diff changeset
511 %!error strrep ("A")
7dcc4cb6da53 Expand sttrrep to work an character matrices (bug #57867)
Rik <rik@octave.org>
parents: 27923
diff changeset
512 %!error strrep ("A", "B")
7dcc4cb6da53 Expand sttrrep to work an character matrices (bug #57867)
Rik <rik@octave.org>
parents: 27923
diff changeset
513 %!error strrep ("A", "B", "C", "D")
7dcc4cb6da53 Expand sttrrep to work an character matrices (bug #57867)
Rik <rik@octave.org>
parents: 27923
diff changeset
514 %!error strrep ("A", "B", "C", "D", "E", "F")
7dcc4cb6da53 Expand sttrrep to work an character matrices (bug #57867)
Rik <rik@octave.org>
parents: 27923
diff changeset
515 %!error <invalid optional argument> strrep ("A", "B", "C", 3, true)
7dcc4cb6da53 Expand sttrrep to work an character matrices (bug #57867)
Rik <rik@octave.org>
parents: 27923
diff changeset
516 %!error <invalid optional argument> strrep ("A", "B", "C", "str", ones (2,2))
7dcc4cb6da53 Expand sttrrep to work an character matrices (bug #57867)
Rik <rik@octave.org>
parents: 27923
diff changeset
517 %!error <unknown option: foobar> strrep ("A", "B", "C", "foobar", true)
7dcc4cb6da53 Expand sttrrep to work an character matrices (bug #57867)
Rik <rik@octave.org>
parents: 27923
diff changeset
518 %!error <each element of S must be a string> strrep ({"A", 1.0}, "B", "C")
7dcc4cb6da53 Expand sttrrep to work an character matrices (bug #57867)
Rik <rik@octave.org>
parents: 27923
diff changeset
519 %!error <S must be a string or cell array of strings> strrep (1.0, "B", "C")
7dcc4cb6da53 Expand sttrrep to work an character matrices (bug #57867)
Rik <rik@octave.org>
parents: 27923
diff changeset
520 %!error <PTN and REP arguments must be strings> strrep ("A", 1.0, "C")
7dcc4cb6da53 Expand sttrrep to work an character matrices (bug #57867)
Rik <rik@octave.org>
parents: 27923
diff changeset
521 %!error <PTN and REP arguments must be strings> strrep ("A", "B", 1.0)
10053
830986c43dee implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents: 10022
diff changeset
522 */
29958
32c3a5805893 move DEFUN and DEFMETHOD functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29359
diff changeset
523
31605
e88a07dec498 maint: Use macros to begin/end C++ namespaces.
Rik <rik@octave.org>
parents: 30565
diff changeset
524 OCTAVE_END_NAMESPACE(octave)