annotate scripts/strings/strchr.m @ 31239:dd6b37f67db2

Accept negative inputs to -2^63 for dec2bin and dec2hex (bug #63089) dec2bin.m: Accept negative inputs lower than -flintmax down to -2^63 Return 64-bit string in certain cases for Matlab compatibility Activate BISTs commented out earlier dec2hex.m: Remove repeated code and call dec2bin instead Accept negative inputs lower than -flintmax down to -2^63 Update and activate BISTs commented out earlier
author Arun Giridhar <arungiridhar@gmail.com>
date Sun, 25 Sep 2022 06:22:25 -0400
parents 796f54d4ddbf
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: 30330
diff changeset
3 ## Copyright (C) 2008-2022 The Octave Project Developers
27918
b442ec6dda5c use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
4 ##
27923
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/>.
8552
3591fe09f3b1 add the strchr function
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
7 ##
3591fe09f3b1 add the strchr function
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
8 ## This file is part of Octave.
3591fe09f3b1 add the strchr function
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
9 ##
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 23220
diff changeset
10 ## Octave is free software: you can redistribute it and/or modify it
8552
3591fe09f3b1 add the strchr function
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
11 ## under the terms of the GNU General Public License as published by
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 23220
diff changeset
12 ## the Free Software Foundation, either version 3 of the License, or
22755
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22323
diff changeset
13 ## (at your option) any later version.
8552
3591fe09f3b1 add the strchr function
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
14 ##
3591fe09f3b1 add the strchr function
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
15 ## Octave is distributed in the hope that it will be useful, but
3591fe09f3b1 add the strchr function
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
16 ## WITHOUT ANY WARRANTY; without even the implied warranty of
22755
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22323
diff changeset
17 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22323
diff changeset
18 ## GNU General Public License for more details.
8552
3591fe09f3b1 add the strchr function
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
19 ##
3591fe09f3b1 add the strchr function
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
20 ## You should have received a copy of the GNU General Public License
3591fe09f3b1 add the strchr function
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
21 ## along with Octave; see the file COPYING. If not, see
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 23220
diff changeset
22 ## <https://www.gnu.org/licenses/>.
27923
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 ########################################################################
8552
3591fe09f3b1 add the strchr function
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
25
3591fe09f3b1 add the strchr function
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
26 ## -*- texinfo -*-
20852
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 20164
diff changeset
27 ## @deftypefn {} {@var{idx} =} strchr (@var{str}, @var{chars})
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 20164
diff changeset
28 ## @deftypefnx {} {@var{idx} =} strchr (@var{str}, @var{chars}, @var{n})
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 20164
diff changeset
29 ## @deftypefnx {} {@var{idx} =} strchr (@var{str}, @var{chars}, @var{n}, @var{direction})
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 20164
diff changeset
30 ## @deftypefnx {} {[@var{i}, @var{j}] =} strchr (@dots{})
28042
89f3e53e9723 strchr.m: Fix miswording in documentation (bug #57739).
Rik <rik@octave.org>
parents: 27923
diff changeset
31 ## Search through the string @var{str} for occurrences of characters from the
89f3e53e9723 strchr.m: Fix miswording in documentation (bug #57739).
Rik <rik@octave.org>
parents: 27923
diff changeset
32 ## set @var{chars}.
20164
df437a52bcaf doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
33 ##
df437a52bcaf doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
34 ## The return value(s), as well as the @var{n} and @var{direction} arguments
df437a52bcaf doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
35 ## behave identically as in @code{find}.
8552
3591fe09f3b1 add the strchr function
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
36 ##
28042
89f3e53e9723 strchr.m: Fix miswording in documentation (bug #57739).
Rik <rik@octave.org>
parents: 27923
diff changeset
37 ## This will be faster than using @code{regexp} in most cases.
8552
3591fe09f3b1 add the strchr function
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
38 ##
3591fe09f3b1 add the strchr function
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
39 ## @seealso{find}
8715
954b6f69f51d doc fixes
John W. Eaton <jwe@octave.org>
parents: 8552
diff changeset
40 ## @end deftypefn
8552
3591fe09f3b1 add the strchr function
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
41
3591fe09f3b1 add the strchr function
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
42 function varargout = strchr (str, chars, varargin)
13320
9da8fbd17b92 strchr.m: Tune switching between algorithms. Add error messages to input validation.
Rik <octave@nomad.inbox5.com>
parents: 12474
diff changeset
43
28901
3c37ae43996a maint: Code cleanup associated with varargin checking.
Rik <rik@octave.org>
parents: 28896
diff changeset
44 if (nargin < 2 || nargin > 4)
8552
3591fe09f3b1 add the strchr function
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
45 print_usage ();
13320
9da8fbd17b92 strchr.m: Tune switching between algorithms. Add error messages to input validation.
Rik <octave@nomad.inbox5.com>
parents: 12474
diff changeset
46 elseif (! ischar (str))
9da8fbd17b92 strchr.m: Tune switching between algorithms. Add error messages to input validation.
Rik <octave@nomad.inbox5.com>
parents: 12474
diff changeset
47 error ("strchr: STR argument must be a string or string array");
9da8fbd17b92 strchr.m: Tune switching between algorithms. Add error messages to input validation.
Rik <octave@nomad.inbox5.com>
parents: 12474
diff changeset
48 elseif (! ischar (chars))
9da8fbd17b92 strchr.m: Tune switching between algorithms. Add error messages to input validation.
Rik <octave@nomad.inbox5.com>
parents: 12474
diff changeset
49 error ("strchr: CHARS argument must be a string");
8552
3591fe09f3b1 add the strchr function
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
50 endif
13320
9da8fbd17b92 strchr.m: Tune switching between algorithms. Add error messages to input validation.
Rik <octave@nomad.inbox5.com>
parents: 12474
diff changeset
51
10408
a8869743d9fe optimize strchr
Jaroslav Hajek <highegg@gmail.com>
parents: 9036
diff changeset
52 if (isempty (chars))
a8869743d9fe optimize strchr
Jaroslav Hajek <highegg@gmail.com>
parents: 9036
diff changeset
53 mask = false (size (str));
13320
9da8fbd17b92 strchr.m: Tune switching between algorithms. Add error messages to input validation.
Rik <octave@nomad.inbox5.com>
parents: 12474
diff changeset
54 elseif (length (chars) <= 4)
10408
a8869743d9fe optimize strchr
Jaroslav Hajek <highegg@gmail.com>
parents: 9036
diff changeset
55 ## With a few characters, it pays off to build the mask incrementally.
a8869743d9fe optimize strchr
Jaroslav Hajek <highegg@gmail.com>
parents: 9036
diff changeset
56 ## We do it via a for loop to save memory.
a8869743d9fe optimize strchr
Jaroslav Hajek <highegg@gmail.com>
parents: 9036
diff changeset
57 mask = str == chars(1);
a8869743d9fe optimize strchr
Jaroslav Hajek <highegg@gmail.com>
parents: 9036
diff changeset
58 for i = 2:length (chars)
a8869743d9fe optimize strchr
Jaroslav Hajek <highegg@gmail.com>
parents: 9036
diff changeset
59 mask |= str == chars(i);
a8869743d9fe optimize strchr
Jaroslav Hajek <highegg@gmail.com>
parents: 9036
diff changeset
60 endfor
a8869743d9fe optimize strchr
Jaroslav Hajek <highegg@gmail.com>
parents: 9036
diff changeset
61 else
13320
9da8fbd17b92 strchr.m: Tune switching between algorithms. Add error messages to input validation.
Rik <octave@nomad.inbox5.com>
parents: 12474
diff changeset
62 ## Index the str into a mask of valid values.
9da8fbd17b92 strchr.m: Tune switching between algorithms. Add error messages to input validation.
Rik <octave@nomad.inbox5.com>
parents: 12474
diff changeset
63 ## This is slower than it could be because of the +1 issue.
9da8fbd17b92 strchr.m: Tune switching between algorithms. Add error messages to input validation.
Rik <octave@nomad.inbox5.com>
parents: 12474
diff changeset
64 f = false (256, 1);
14868
5d3a684236b0 maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
65 f(uint8 (chars) + 1) = true;
11261
9c19b450b254 strchr.m: style fixes
John W. Eaton <jwe@octave.org>
parents: 11260
diff changeset
66 ## Default goes via double -- unnecessarily long.
9c19b450b254 strchr.m: style fixes
John W. Eaton <jwe@octave.org>
parents: 11260
diff changeset
67 si = uint32 (str);
13320
9da8fbd17b92 strchr.m: Tune switching between algorithms. Add error messages to input validation.
Rik <octave@nomad.inbox5.com>
parents: 12474
diff changeset
68 ## in-place is faster than str+1
11261
9c19b450b254 strchr.m: style fixes
John W. Eaton <jwe@octave.org>
parents: 11260
diff changeset
69 ++si;
10408
a8869743d9fe optimize strchr
Jaroslav Hajek <highegg@gmail.com>
parents: 9036
diff changeset
70 mask = reshape (f(si), size (str));
a8869743d9fe optimize strchr
Jaroslav Hajek <highegg@gmail.com>
parents: 9036
diff changeset
71 endif
13320
9da8fbd17b92 strchr.m: Tune switching between algorithms. Add error messages to input validation.
Rik <octave@nomad.inbox5.com>
parents: 12474
diff changeset
72
8552
3591fe09f3b1 add the strchr function
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
73 varargout = cell (1, nargout);
3591fe09f3b1 add the strchr function
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
74 varargout{1} = [];
10408
a8869743d9fe optimize strchr
Jaroslav Hajek <highegg@gmail.com>
parents: 9036
diff changeset
75 [varargout{:}] = find (mask, varargin{:});
13320
9da8fbd17b92 strchr.m: Tune switching between algorithms. Add error messages to input validation.
Rik <octave@nomad.inbox5.com>
parents: 12474
diff changeset
76
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
77 endfunction
8552
3591fe09f3b1 add the strchr function
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
78
13320
9da8fbd17b92 strchr.m: Tune switching between algorithms. Add error messages to input validation.
Rik <octave@nomad.inbox5.com>
parents: 12474
diff changeset
79
9da8fbd17b92 strchr.m: Tune switching between algorithms. Add error messages to input validation.
Rik <octave@nomad.inbox5.com>
parents: 12474
diff changeset
80 %!assert (strchr ("Octave is the best software", ""), zeros (1,0))
30330
01de0045b2e3 maint: Shorten some long lines to <= 80 characters (bug #57599)
Rik <rik@octave.org>
parents: 29359
diff changeset
81 %!assert (strchr ("Octave is the best software", "best"),
01de0045b2e3 maint: Shorten some long lines to <= 80 characters (bug #57599)
Rik <rik@octave.org>
parents: 29359
diff changeset
82 %! [3, 6, 9, 11, 13, 15, 16, 17, 18, 20, 23, 27])
01de0045b2e3 maint: Shorten some long lines to <= 80 characters (bug #57599)
Rik <rik@octave.org>
parents: 29359
diff changeset
83 %!assert (strchr ("Octave is the best software", "software"),
01de0045b2e3 maint: Shorten some long lines to <= 80 characters (bug #57599)
Rik <rik@octave.org>
parents: 29359
diff changeset
84 %! [3, 4, 6, 9, 11, 13, 16, 17, 18, 20, 21, 22, 23, 24, 25, 26, 27])
11260
fd18ba11af57 Extra tests in strchr
Kim Hansen <kim@ange.dk>
parents: 10793
diff changeset
85
19833
9fc020886ae9 maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19697
diff changeset
86 ## Test input validation
28896
90fea9cc9caa test: Add expected error message <Invalid call> to BIST tests for nargin.
Rik <rik@octave.org>
parents: 28042
diff changeset
87 %!error <Invalid call> strchr ()
90fea9cc9caa test: Add expected error message <Invalid call> to BIST tests for nargin.
Rik <rik@octave.org>
parents: 28042
diff changeset
88 %!error <Invalid call> strchr (1)
28901
3c37ae43996a maint: Code cleanup associated with varargin checking.
Rik <rik@octave.org>
parents: 28896
diff changeset
89 %!error <Invalid call> strchr ("s", "a", 1, "last", 5)
13320
9da8fbd17b92 strchr.m: Tune switching between algorithms. Add error messages to input validation.
Rik <octave@nomad.inbox5.com>
parents: 12474
diff changeset
90 %!error <STR argument must be a string> strchr (1, "aeiou")
9da8fbd17b92 strchr.m: Tune switching between algorithms. Add error messages to input validation.
Rik <octave@nomad.inbox5.com>
parents: 12474
diff changeset
91 %!error <CHARS argument must be a string> strchr ("aeiou", 1)