changeset 8552:3591fe09f3b1

add the strchr function
author Jaroslav Hajek <highegg@gmail.com>
date Wed, 21 Jan 2009 14:54:20 +0100
parents 906f976d35a8
children c7ff200e45f5
files scripts/ChangeLog scripts/strings/Makefile.in scripts/strings/strchr.m
diffstat 3 files changed, 49 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/ChangeLog
+++ b/scripts/ChangeLog
@@ -1,3 +1,8 @@
+2009-01-21  Jaroslav Hajek  <highegg@gmail.com>
+
+	* strings/strchr.m: New function.
+	* strings/Makefile.in: Add it.
+
 2009-01-20  Jaroslav Hajek  <highegg@gmail.com>
 
 	* optimization/fsolve.m: Only use qrupdate if available.
--- a/scripts/strings/Makefile.in
+++ b/scripts/strings/Makefile.in
@@ -36,7 +36,7 @@
 SOURCES = base2dec.m bin2dec.m blanks.m deblank.m dec2base.m \
   dec2bin.m dec2hex.m findstr.m hex2dec.m index.m isletter.m isstrprop.m \
   mat2str.m regexptranslate.m rindex.m split.m str2double.m \
-  str2num.m strcat.m cstrcat.m strcmpi.m strfind.m strjust.m strmatch.m \
+  str2num.m strcat.m cstrcat.m strcmpi.m strchr.m strfind.m strjust.m strmatch.m \
   strncmpi.m strrep.m strtok.m strtrim.m strtrunc.m \
   substr.m validatestring.m
 
new file mode 100644
--- /dev/null
+++ b/scripts/strings/strchr.m
@@ -0,0 +1,43 @@
+## Copyright (C) 2008 Jaroslav Hajek
+##
+## This file is part of Octave.
+##
+## Octave is free software; you can redistribute it and/or modify it
+## under the terms of the GNU General Public License as published by
+## the Free Software Foundation; either version 3 of the License, or (at
+## your option) any later version.
+##
+## Octave is distributed in the hope that it will be useful, but
+## WITHOUT ANY WARRANTY; without even the implied warranty of
+## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+## General Public License for more details.
+##
+## You should have received a copy of the GNU General Public License
+## along with Octave; see the file COPYING.  If not, see
+## <http://www.gnu.org/licenses/>.
+
+## -*- texinfo -*-
+## @deftypefn {Function File} {@var{idx} =} strchr (@var{str}, @var{chars})
+## @deftypefnx {Function File} {@var{idx} =} strchr (@var{str}, @var{chars}, @var{n})
+## @deftypefnx {Function File} {@var{idx} =} strchr (@var{str}, @var{chars}, 
+## @var{n}, @var{direction})
+## Search for the string @var{str} for occurences of characters from the set @var{chars}.
+## The return value, as well as the @var{n} and @var{direction} arguments behave
+## identically as in @code{find}.
+##
+## This will be faster than using regexp in most cases.
+##
+## @seealso{find}
+
+function varargout = strchr (str, chars, varargin)
+  if (nargin < 2 || ! ischar (str) || ! ischar (chars))
+    print_usage ();
+  endif
+  f = false (1, 256);
+  f(chars + 1) = true;
+  varargout = cell (1, nargout);
+  varargout{1} = [];
+  [varargout{:}] = find (reshape (f(str + 1), size (str)), varargin{:});
+endfunction 
+
+%!assert(strchr("Octave is the best software","best"),[3, 6, 9, 11, 13, 15, 16, 17, 18, 20, 23, 27])