~ubuntu-branches/debian/sid/octave3.0/sid

« back to all changes in this revision

Viewing changes to scripts/strings/strfind.m

  • Committer: Bazaar Package Importer
  • Author(s): Rafael Laboissiere
  • Date: 2007-12-23 16:04:15 UTC
  • Revision ID: james.westby@ubuntu.com-20071223160415-n4gk468dihy22e9v
Tags: upstream-3.0.0
ImportĀ upstreamĀ versionĀ 3.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
## Copyright (C) 2004, 2006, 2007 by Alois Schloegl
 
2
##
 
3
## This file is part of Octave.
 
4
##
 
5
## Octave is free software; you can redistribute it and/or modify it
 
6
## under the terms of the GNU General Public License as published by
 
7
## the Free Software Foundation; either version 3 of the License, or (at
 
8
## your option) any later version.
 
9
##
 
10
## Octave is distributed in the hope that it will be useful, but
 
11
## WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
13
## General Public License for more details.
 
14
##
 
15
## You should have received a copy of the GNU General Public License
 
16
## along with Octave; see the file COPYING.  If not, see
 
17
## <http://www.gnu.org/licenses/>.
 
18
 
 
19
## -*- texinfo -*-
 
20
## @deftypefn {Function File} {@var{idx} =} strfind (@var{str}, @var{pattern})
 
21
## @deftypefnx {Function File} {@var{idx} =} strfind (@var{cellstr}, @var{pattern})
 
22
## Search for @var{pattern} in the string @var{str} and return the
 
23
## starting index of every such occurrence in the vector @var{idx}.
 
24
## If there is no such occurrence, or if @var{pattern} is longer
 
25
## than @var{str}, then @var{idx} is the empty array @code{[]}.
 
26
##
 
27
## If the cell array of strings @var{cellstr} is specified instead of the
 
28
## string @var{str}, then @var{idx} is a cell array of vectors, as specified
 
29
## above.
 
30
## @seealso{findstr, strmatch, strcmp, strncmp, strcmpi, strncmpi}
 
31
## @end deftypefn
 
32
 
 
33
## Author: alois schloegl <a.schloegl@ieee.org>
 
34
## Created: 1 November 2004
 
35
## Adapted-By: William Poetra Yoga Hadisoeseno <williampoetra@gmail.com>
 
36
 
 
37
function idx = strfind (text, pattern)
 
38
 
 
39
  if (nargin != 2)
 
40
    print_usage ();
 
41
  elseif (! ischar (pattern))
 
42
    error ("strfind: pattern must be a string value");
 
43
  endif
 
44
 
 
45
  lp = length (pattern);
 
46
 
 
47
  if (ischar (text))
 
48
    idx = __strfind_string__ (text, pattern, lp);
 
49
  elseif (iscellstr (text))
 
50
    idx = cell (size (text));
 
51
    for i = 1:(numel (text))
 
52
      idx{i} = __strfind_string__ (text{i}, pattern, lp);
 
53
    endfor
 
54
  else
 
55
    error ("strfind: text must be a string or cell array of strings");
 
56
  endif
 
57
 
 
58
### endfunction
 
59
 
 
60
function idx = __strfind_string__ (text, pattern, lp)
 
61
 
 
62
  idx = 1:(length (text) - lp + 1);
 
63
  k = 0;
 
64
  while (k < lp && ! isempty (idx))
 
65
    idx = idx(text(idx + k) == pattern(++k));
 
66
  endwhile
 
67
 
 
68
### endfunction