# HG changeset patch # User Jaroslav Hajek # Date 1232546060 -3600 # Node ID 3591fe09f3b1a74da26171a660e3aa9bb1a4f01c # Parent 906f976d35a842d47d4c1687c42fd20affdeae18 add the strchr function diff -r 906f976d35a8 -r 3591fe09f3b1 scripts/ChangeLog --- a/scripts/ChangeLog Wed Jan 21 13:02:49 2009 +0100 +++ b/scripts/ChangeLog Wed Jan 21 14:54:20 2009 +0100 @@ -1,3 +1,8 @@ +2009-01-21 Jaroslav Hajek + + * strings/strchr.m: New function. + * strings/Makefile.in: Add it. + 2009-01-20 Jaroslav Hajek * optimization/fsolve.m: Only use qrupdate if available. diff -r 906f976d35a8 -r 3591fe09f3b1 scripts/strings/Makefile.in --- a/scripts/strings/Makefile.in Wed Jan 21 13:02:49 2009 +0100 +++ b/scripts/strings/Makefile.in Wed Jan 21 14:54:20 2009 +0100 @@ -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 diff -r 906f976d35a8 -r 3591fe09f3b1 scripts/strings/strchr.m --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/scripts/strings/strchr.m Wed Jan 21 14:54:20 2009 +0100 @@ -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 +## . + +## -*- 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])