Mercurial > hg > octave-lyh
comparison scripts/strings/strchr.m @ 13320:9da8fbd17b92
strchr.m: Tune switching between algorithms. Add error messages to input validation.
* strchr.m: Switch to mask creation via indexing above 4 CHARS. Tell user
what went wrong when input validation fails.
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Tue, 11 Oct 2011 13:18:40 -0700 |
parents | 26d3164fd58d |
children | 72c96de7a403 |
comparison
equal
deleted
inserted
replaced
13319:eb0ce6ffefb0 | 13320:9da8fbd17b92 |
---|---|
18 | 18 |
19 ## -*- texinfo -*- | 19 ## -*- texinfo -*- |
20 ## @deftypefn {Function File} {@var{idx} =} strchr (@var{str}, @var{chars}) | 20 ## @deftypefn {Function File} {@var{idx} =} strchr (@var{str}, @var{chars}) |
21 ## @deftypefnx {Function File} {@var{idx} =} strchr (@var{str}, @var{chars}, @var{n}) | 21 ## @deftypefnx {Function File} {@var{idx} =} strchr (@var{str}, @var{chars}, @var{n}) |
22 ## @deftypefnx {Function File} {@var{idx} =} strchr (@var{str}, @var{chars}, @var{n}, @var{direction}) | 22 ## @deftypefnx {Function File} {@var{idx} =} strchr (@var{str}, @var{chars}, @var{n}, @var{direction}) |
23 ## @deftypefnx {Function File} {[@var{i}, @var{j}] =} strchr (@dots{}) | |
23 ## Search for the string @var{str} for occurrences of characters from | 24 ## Search for the string @var{str} for occurrences of characters from |
24 ## the set @var{chars}. The return value, as well as the @var{n} and | 25 ## the set @var{chars}. The return value(s), as well as the @var{n} and |
25 ## @var{direction} arguments behave identically as in @code{find}. | 26 ## @var{direction} arguments behave identically as in @code{find}. |
26 ## | 27 ## |
27 ## This will be faster than using regexp in most cases. | 28 ## This will be faster than using regexp in most cases. |
28 ## | 29 ## |
29 ## @seealso{find} | 30 ## @seealso{find} |
30 ## @end deftypefn | 31 ## @end deftypefn |
31 | 32 |
32 function varargout = strchr (str, chars, varargin) | 33 function varargout = strchr (str, chars, varargin) |
33 if (nargin < 2 || ! ischar (str) || ! ischar (chars)) | 34 |
35 if (nargin < 2) | |
34 print_usage (); | 36 print_usage (); |
37 elseif (! ischar (str)) | |
38 error ("strchr: STR argument must be a string or string array"); | |
39 elseif (! ischar (chars)) | |
40 error ("strchr: CHARS argument must be a string"); | |
35 endif | 41 endif |
42 | |
36 if (isempty (chars)) | 43 if (isempty (chars)) |
37 mask = false (size (str)); | 44 mask = false (size (str)); |
38 elseif (length (chars) <= 6) | 45 elseif (length (chars) <= 4) |
39 ## With a few characters, it pays off to build the mask incrementally. | 46 ## With a few characters, it pays off to build the mask incrementally. |
40 ## We do it via a for loop to save memory. | 47 ## We do it via a for loop to save memory. |
41 mask = str == chars(1); | 48 mask = str == chars(1); |
42 for i = 2:length (chars) | 49 for i = 2:length (chars) |
43 mask |= str == chars(i); | 50 mask |= str == chars(i); |
44 endfor | 51 endfor |
45 else | 52 else |
46 ## Index the str into a mask of valid values. This is slower than | 53 ## Index the str into a mask of valid values. |
47 ## it could be because of the +1 issue. | 54 ## This is slower than it could be because of the +1 issue. |
48 f = false (1, 256); | 55 f = false (256, 1); |
49 f(uint8(chars)+1) = true; | 56 f(uint8(chars)+1) = true; |
50 ## Default goes via double -- unnecessarily long. | 57 ## Default goes via double -- unnecessarily long. |
51 si = uint32 (str); | 58 si = uint32 (str); |
52 ## in-place | 59 ## in-place is faster than str+1 |
53 ++si; | 60 ++si; |
54 mask = reshape (f(si), size (str)); | 61 mask = reshape (f(si), size (str)); |
55 endif | 62 endif |
63 | |
56 varargout = cell (1, nargout); | 64 varargout = cell (1, nargout); |
57 varargout{1} = []; | 65 varargout{1} = []; |
58 [varargout{:}] = find (mask, varargin{:}); | 66 [varargout{:}] = find (mask, varargin{:}); |
67 | |
59 endfunction | 68 endfunction |
60 | 69 |
61 %!assert(strchr("Octave is the best software",""),zeros(1,0)) | |
62 %!assert(strchr("Octave is the best software","best"),[3, 6, 9, 11, 13, 15, 16, 17, 18, 20, 23, 27]) | |
63 %!assert(strchr("Octave is the best software","software"),[3, 4, 6, 9, 11, 13, 16, 17, 18, 20, 21, 22, 23, 24, 25, 26, 27]) | |
64 | 70 |
71 %!assert (strchr ("Octave is the best software", ""), zeros (1,0)) | |
72 %!assert (strchr ("Octave is the best software", "best"), [3, 6, 9, 11, 13, 15, 16, 17, 18, 20, 23, 27]) | |
73 %!assert (strchr ("Octave is the best software", "software"), [3, 4, 6, 9, 11, 13, 16, 17, 18, 20, 21, 22, 23, 24, 25, 26, 27]) | |
74 | |
75 %% Test input validation | |
76 %!error strchr () | |
77 %!error strchr (1) | |
78 %!error <STR argument must be a string> strchr (1, "aeiou") | |
79 %!error <CHARS argument must be a string> strchr ("aeiou", 1) | |
80 |