annotate scripts/strings/endsWith.m @ 28235:a6edb8097ec6

endsWith.m: delete duplicate line in docstring.
author Markus Mützel <markus.muetzel@gmx.de>
date Wed, 22 Apr 2020 13:23:05 +0200
parents 49384057fb03
children d8318c12d903
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
28212
d052c11d47c6 Add new functions startsWith and endsWith (bug #57041).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
1 ########################################################################
d052c11d47c6 Add new functions startsWith and endsWith (bug #57041).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
2 ##
d052c11d47c6 Add new functions startsWith and endsWith (bug #57041).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
3 ## Copyright (C) 2020 The Octave Project Developers
d052c11d47c6 Add new functions startsWith and endsWith (bug #57041).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
4 ##
d052c11d47c6 Add new functions startsWith and endsWith (bug #57041).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
5 ## See the file COPYRIGHT.md in the top-level directory of this
d052c11d47c6 Add new functions startsWith and endsWith (bug #57041).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
6 ## distribution or <https://octave.org/copyright/>.
d052c11d47c6 Add new functions startsWith and endsWith (bug #57041).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
7 ##
d052c11d47c6 Add new functions startsWith and endsWith (bug #57041).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
8 ## This file is part of Octave.
d052c11d47c6 Add new functions startsWith and endsWith (bug #57041).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
9 ##
d052c11d47c6 Add new functions startsWith and endsWith (bug #57041).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
10 ## Octave is free software: you can redistribute it and/or modify it
d052c11d47c6 Add new functions startsWith and endsWith (bug #57041).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
11 ## under the terms of the GNU General Public License as published by
d052c11d47c6 Add new functions startsWith and endsWith (bug #57041).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
12 ## the Free Software Foundation, either version 3 of the License, or
d052c11d47c6 Add new functions startsWith and endsWith (bug #57041).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
13 ## (at your option) any later version.
d052c11d47c6 Add new functions startsWith and endsWith (bug #57041).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
14 ##
d052c11d47c6 Add new functions startsWith and endsWith (bug #57041).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
15 ## Octave is distributed in the hope that it will be useful, but
d052c11d47c6 Add new functions startsWith and endsWith (bug #57041).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
16 ## WITHOUT ANY WARRANTY; without even the implied warranty of
d052c11d47c6 Add new functions startsWith and endsWith (bug #57041).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
17 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
d052c11d47c6 Add new functions startsWith and endsWith (bug #57041).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
18 ## GNU General Public License for more details.
d052c11d47c6 Add new functions startsWith and endsWith (bug #57041).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
19 ##
d052c11d47c6 Add new functions startsWith and endsWith (bug #57041).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
20 ## You should have received a copy of the GNU General Public License
d052c11d47c6 Add new functions startsWith and endsWith (bug #57041).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
21 ## along with Octave; see the file COPYING. If not, see
d052c11d47c6 Add new functions startsWith and endsWith (bug #57041).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
22 ## <https://www.gnu.org/licenses/>.
d052c11d47c6 Add new functions startsWith and endsWith (bug #57041).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
23 ##
d052c11d47c6 Add new functions startsWith and endsWith (bug #57041).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
24 ########################################################################
d052c11d47c6 Add new functions startsWith and endsWith (bug #57041).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
25
d052c11d47c6 Add new functions startsWith and endsWith (bug #57041).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
26 ## -*- texinfo -*-
d052c11d47c6 Add new functions startsWith and endsWith (bug #57041).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
27 ## @deftypefn {} {@var{retval} =} endsWith (@var{str}, @var{pattern})
28232
49384057fb03 endsWith.m: overhaul function.
Rik <rik@octave.org>
parents: 28212
diff changeset
28 ## @deftypefnx {} {@var{retval} =} endsWith (@var{str}, @var{pattern}, "IgnoreCase", @var{ignore_case})
49384057fb03 endsWith.m: overhaul function.
Rik <rik@octave.org>
parents: 28212
diff changeset
29 ## Check whether string(s) end with pattern(s).
28212
d052c11d47c6 Add new functions startsWith and endsWith (bug #57041).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
30 ##
28232
49384057fb03 endsWith.m: overhaul function.
Rik <rik@octave.org>
parents: 28212
diff changeset
31 ## Return an array of logical values that indicates which string(s) in the
49384057fb03 endsWith.m: overhaul function.
Rik <rik@octave.org>
parents: 28212
diff changeset
32 ## input @var{str} (a single string or cell array of strings) end with
49384057fb03 endsWith.m: overhaul function.
Rik <rik@octave.org>
parents: 28212
diff changeset
33 ## the input @var{pattern} (a single string or cell array of strings).
28212
d052c11d47c6 Add new functions startsWith and endsWith (bug #57041).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
34 ##
28232
49384057fb03 endsWith.m: overhaul function.
Rik <rik@octave.org>
parents: 28212
diff changeset
35 ## If the value of the parameter @qcode{"IgnoreCase"} is true, then the
49384057fb03 endsWith.m: overhaul function.
Rik <rik@octave.org>
parents: 28212
diff changeset
36 ## function will ignore the letter case of @var{str} and @var{pattern}. By
49384057fb03 endsWith.m: overhaul function.
Rik <rik@octave.org>
parents: 28212
diff changeset
37 ## default, the comparison is case sensitive.
28212
d052c11d47c6 Add new functions startsWith and endsWith (bug #57041).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
38 ##
d052c11d47c6 Add new functions startsWith and endsWith (bug #57041).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
39 ## Examples:
d052c11d47c6 Add new functions startsWith and endsWith (bug #57041).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
40 ##
d052c11d47c6 Add new functions startsWith and endsWith (bug #57041).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
41 ## @example
28232
49384057fb03 endsWith.m: overhaul function.
Rik <rik@octave.org>
parents: 28212
diff changeset
42 ## @group
28212
d052c11d47c6 Add new functions startsWith and endsWith (bug #57041).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
43 ## ## one string and one pattern while considering case
d052c11d47c6 Add new functions startsWith and endsWith (bug #57041).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
44 ## endsWith ("hello", "lo")
d052c11d47c6 Add new functions startsWith and endsWith (bug #57041).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
45 ## @result{} 1
28232
49384057fb03 endsWith.m: overhaul function.
Rik <rik@octave.org>
parents: 28212
diff changeset
46 ## @end group
28212
d052c11d47c6 Add new functions startsWith and endsWith (bug #57041).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
47 ##
28232
49384057fb03 endsWith.m: overhaul function.
Rik <rik@octave.org>
parents: 28212
diff changeset
48 ## @group
28212
d052c11d47c6 Add new functions startsWith and endsWith (bug #57041).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
49 ## ## one string and one pattern while ignoring case
d052c11d47c6 Add new functions startsWith and endsWith (bug #57041).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
50 ## endsWith ("hello", "LO", "IgnoreCase", true)
d052c11d47c6 Add new functions startsWith and endsWith (bug #57041).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
51 ## @result{} 1
28232
49384057fb03 endsWith.m: overhaul function.
Rik <rik@octave.org>
parents: 28212
diff changeset
52 ## @end group
28212
d052c11d47c6 Add new functions startsWith and endsWith (bug #57041).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
53 ##
28232
49384057fb03 endsWith.m: overhaul function.
Rik <rik@octave.org>
parents: 28212
diff changeset
54 ## @group
28212
d052c11d47c6 Add new functions startsWith and endsWith (bug #57041).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
55 ## ## multiple strings and multiple patterns while considering case
d052c11d47c6 Add new functions startsWith and endsWith (bug #57041).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
56 ## endsWith (@{"tests.txt", "mydoc.odt", "myFunc.m", "results.pptx"@},
d052c11d47c6 Add new functions startsWith and endsWith (bug #57041).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
57 ## @{".docx", ".odt", ".txt"@})
d052c11d47c6 Add new functions startsWith and endsWith (bug #57041).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
58 ## @result{} 1 1 0 0
28232
49384057fb03 endsWith.m: overhaul function.
Rik <rik@octave.org>
parents: 28212
diff changeset
59 ## @end group
28212
d052c11d47c6 Add new functions startsWith and endsWith (bug #57041).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
60 ##
28232
49384057fb03 endsWith.m: overhaul function.
Rik <rik@octave.org>
parents: 28212
diff changeset
61 ## @group
28212
d052c11d47c6 Add new functions startsWith and endsWith (bug #57041).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
62 ## ## multiple strings and one pattern while considering case
d052c11d47c6 Add new functions startsWith and endsWith (bug #57041).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
63 ## endsWith (@{"TESTS.TXT", "mydoc.odt", "result.txt", "myFunc.m"@},
d052c11d47c6 Add new functions startsWith and endsWith (bug #57041).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
64 ## ".txt", "IgnoreCase", false)
d052c11d47c6 Add new functions startsWith and endsWith (bug #57041).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
65 ## @result{} 0 0 1 0
28232
49384057fb03 endsWith.m: overhaul function.
Rik <rik@octave.org>
parents: 28212
diff changeset
66 ## @end group
28212
d052c11d47c6 Add new functions startsWith and endsWith (bug #57041).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
67 ##
28232
49384057fb03 endsWith.m: overhaul function.
Rik <rik@octave.org>
parents: 28212
diff changeset
68 ## @group
28212
d052c11d47c6 Add new functions startsWith and endsWith (bug #57041).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
69 ## ## multiple strings and one pattern while ignoring case
d052c11d47c6 Add new functions startsWith and endsWith (bug #57041).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
70 ## endsWith (@{"TESTS.TXT", "mydoc.odt", "result.txt", "myFunc.m"@},
d052c11d47c6 Add new functions startsWith and endsWith (bug #57041).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
71 ## ".txt", "IgnoreCase", true)
d052c11d47c6 Add new functions startsWith and endsWith (bug #57041).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
72 ## @result{} 1 0 1 0
28232
49384057fb03 endsWith.m: overhaul function.
Rik <rik@octave.org>
parents: 28212
diff changeset
73 ## @end group
28212
d052c11d47c6 Add new functions startsWith and endsWith (bug #57041).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
74 ## @end example
d052c11d47c6 Add new functions startsWith and endsWith (bug #57041).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
75 ##
28232
49384057fb03 endsWith.m: overhaul function.
Rik <rik@octave.org>
parents: 28212
diff changeset
76 ## @seealso{startsWith, regexp, strncmp, strncmpi}
28212
d052c11d47c6 Add new functions startsWith and endsWith (bug #57041).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
77 ## @end deftypefn
d052c11d47c6 Add new functions startsWith and endsWith (bug #57041).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
78
28232
49384057fb03 endsWith.m: overhaul function.
Rik <rik@octave.org>
parents: 28212
diff changeset
79 function retval = endsWith (str, pattern, IgnoreCase, ignore_case)
49384057fb03 endsWith.m: overhaul function.
Rik <rik@octave.org>
parents: 28212
diff changeset
80
49384057fb03 endsWith.m: overhaul function.
Rik <rik@octave.org>
parents: 28212
diff changeset
81 if (nargin != 2 && nargin != 4)
28212
d052c11d47c6 Add new functions startsWith and endsWith (bug #57041).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
82 print_usage ();
d052c11d47c6 Add new functions startsWith and endsWith (bug #57041).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
83 endif
d052c11d47c6 Add new functions startsWith and endsWith (bug #57041).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
84
28232
49384057fb03 endsWith.m: overhaul function.
Rik <rik@octave.org>
parents: 28212
diff changeset
85 ## Validate input str and pattern
49384057fb03 endsWith.m: overhaul function.
Rik <rik@octave.org>
parents: 28212
diff changeset
86 if (! (iscellstr (str) || ischar (str)))
49384057fb03 endsWith.m: overhaul function.
Rik <rik@octave.org>
parents: 28212
diff changeset
87 error ("endsWith: STR must be a string or cell array of strings");
28212
d052c11d47c6 Add new functions startsWith and endsWith (bug #57041).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
88 endif
28232
49384057fb03 endsWith.m: overhaul function.
Rik <rik@octave.org>
parents: 28212
diff changeset
89 if (! (iscellstr (pattern) || ischar (pattern)))
49384057fb03 endsWith.m: overhaul function.
Rik <rik@octave.org>
parents: 28212
diff changeset
90 error ("endsWith: PATTERN must be a string or cell array of strings");
49384057fb03 endsWith.m: overhaul function.
Rik <rik@octave.org>
parents: 28212
diff changeset
91 endif
28212
d052c11d47c6 Add new functions startsWith and endsWith (bug #57041).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
92
d052c11d47c6 Add new functions startsWith and endsWith (bug #57041).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
93 ## reverse str and pattern
28232
49384057fb03 endsWith.m: overhaul function.
Rik <rik@octave.org>
parents: 28212
diff changeset
94 str = cellfun (@flip, cellstr (str), "UniformOutput", false);
49384057fb03 endsWith.m: overhaul function.
Rik <rik@octave.org>
parents: 28212
diff changeset
95 pattern = cellfun (@flip, cellstr (pattern), "UniformOutput", false);
28212
d052c11d47c6 Add new functions startsWith and endsWith (bug #57041).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
96
d052c11d47c6 Add new functions startsWith and endsWith (bug #57041).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
97 if (nargin == 2)
28232
49384057fb03 endsWith.m: overhaul function.
Rik <rik@octave.org>
parents: 28212
diff changeset
98 ignore_case = false;
49384057fb03 endsWith.m: overhaul function.
Rik <rik@octave.org>
parents: 28212
diff changeset
99 else
49384057fb03 endsWith.m: overhaul function.
Rik <rik@octave.org>
parents: 28212
diff changeset
100 ## For Matlab compatibility accept any abbreviation of 3rd argument
49384057fb03 endsWith.m: overhaul function.
Rik <rik@octave.org>
parents: 28212
diff changeset
101 if (! ischar (IgnoreCase) || isempty (IgnoreCase)
49384057fb03 endsWith.m: overhaul function.
Rik <rik@octave.org>
parents: 28212
diff changeset
102 || ! strncmpi (IgnoreCase, "IgnoreCase", length (IgnoreCase)))
28212
d052c11d47c6 Add new functions startsWith and endsWith (bug #57041).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
103 error ('endsWith: third input must be "IgnoreCase"');
d052c11d47c6 Add new functions startsWith and endsWith (bug #57041).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
104 endif
d052c11d47c6 Add new functions startsWith and endsWith (bug #57041).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
105
28232
49384057fb03 endsWith.m: overhaul function.
Rik <rik@octave.org>
parents: 28212
diff changeset
106 if (! isscalar (ignore_case) || ! isreal (ignore_case))
49384057fb03 endsWith.m: overhaul function.
Rik <rik@octave.org>
parents: 28212
diff changeset
107 error ('endsWith: "IgnoreCase" value must be a logical scalar');
28212
d052c11d47c6 Add new functions startsWith and endsWith (bug #57041).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
108 endif
28232
49384057fb03 endsWith.m: overhaul function.
Rik <rik@octave.org>
parents: 28212
diff changeset
109 ignore_case = logical (ignore_case);
28212
d052c11d47c6 Add new functions startsWith and endsWith (bug #57041).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
110 endif
d052c11d47c6 Add new functions startsWith and endsWith (bug #57041).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
111
28232
49384057fb03 endsWith.m: overhaul function.
Rik <rik@octave.org>
parents: 28212
diff changeset
112 retval = false (size (str));
49384057fb03 endsWith.m: overhaul function.
Rik <rik@octave.org>
parents: 28212
diff changeset
113 if (ignore_case)
28212
d052c11d47c6 Add new functions startsWith and endsWith (bug #57041).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
114 for j = 1:numel (pattern)
d052c11d47c6 Add new functions startsWith and endsWith (bug #57041).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
115 retval |= strncmpi (str, pattern{j}, length (pattern{j}));
d052c11d47c6 Add new functions startsWith and endsWith (bug #57041).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
116 endfor
d052c11d47c6 Add new functions startsWith and endsWith (bug #57041).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
117 else
d052c11d47c6 Add new functions startsWith and endsWith (bug #57041).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
118 for j = 1:numel (pattern)
d052c11d47c6 Add new functions startsWith and endsWith (bug #57041).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
119 retval |= strncmp (str, pattern{j}, length (pattern{j}));
d052c11d47c6 Add new functions startsWith and endsWith (bug #57041).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
120 endfor
d052c11d47c6 Add new functions startsWith and endsWith (bug #57041).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
121 endif
d052c11d47c6 Add new functions startsWith and endsWith (bug #57041).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
122
d052c11d47c6 Add new functions startsWith and endsWith (bug #57041).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
123 endfunction
d052c11d47c6 Add new functions startsWith and endsWith (bug #57041).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
124
d052c11d47c6 Add new functions startsWith and endsWith (bug #57041).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
125
d052c11d47c6 Add new functions startsWith and endsWith (bug #57041).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
126 ## Test simple use with one string and one pattern
d052c11d47c6 Add new functions startsWith and endsWith (bug #57041).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
127 %!assert (endsWith ("hello", "lo"))
d052c11d47c6 Add new functions startsWith and endsWith (bug #57041).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
128 %!assert (! endsWith ("hello", "LO"))
28232
49384057fb03 endsWith.m: overhaul function.
Rik <rik@octave.org>
parents: 28212
diff changeset
129 %!assert (endsWith ("hello", "LO", "i", 5))
28212
d052c11d47c6 Add new functions startsWith and endsWith (bug #57041).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
130 %!assert (! endsWith ("hello", "no"))
d052c11d47c6 Add new functions startsWith and endsWith (bug #57041).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
131
d052c11d47c6 Add new functions startsWith and endsWith (bug #57041).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
132 ## Test multiple strings with a single pattern
d052c11d47c6 Add new functions startsWith and endsWith (bug #57041).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
133 %!test
28232
49384057fb03 endsWith.m: overhaul function.
Rik <rik@octave.org>
parents: 28212
diff changeset
134 %! str = {"myFile.odt", "results.ppt", "myCode.m"; ...
49384057fb03 endsWith.m: overhaul function.
Rik <rik@octave.org>
parents: 28212
diff changeset
135 %! "data-analysis.ppt", "foundations.txt", "data.odt"};
49384057fb03 endsWith.m: overhaul function.
Rik <rik@octave.org>
parents: 28212
diff changeset
136 %! pattern = ".odt";
49384057fb03 endsWith.m: overhaul function.
Rik <rik@octave.org>
parents: 28212
diff changeset
137 %! expected = [true, false, false; false, false, true];
49384057fb03 endsWith.m: overhaul function.
Rik <rik@octave.org>
parents: 28212
diff changeset
138 %! assert (endsWith (str, pattern), expected);
28212
d052c11d47c6 Add new functions startsWith and endsWith (bug #57041).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
139
d052c11d47c6 Add new functions startsWith and endsWith (bug #57041).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
140 ## Test multiple strings with multiple patterns
d052c11d47c6 Add new functions startsWith and endsWith (bug #57041).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
141 %!test
28232
49384057fb03 endsWith.m: overhaul function.
Rik <rik@octave.org>
parents: 28212
diff changeset
142 %! str = {"tests.txt", "mydoc.odt", "myFunc.m", "results.pptx"};
49384057fb03 endsWith.m: overhaul function.
Rik <rik@octave.org>
parents: 28212
diff changeset
143 %! pattern = {".docx", ".odt", ".txt"};
49384057fb03 endsWith.m: overhaul function.
Rik <rik@octave.org>
parents: 28212
diff changeset
144 %! expected = [true, true, false, false];
49384057fb03 endsWith.m: overhaul function.
Rik <rik@octave.org>
parents: 28212
diff changeset
145 %! assert (endsWith (str, pattern), expected);
28212
d052c11d47c6 Add new functions startsWith and endsWith (bug #57041).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
146
d052c11d47c6 Add new functions startsWith and endsWith (bug #57041).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
147 ## Test IgnoreCase
d052c11d47c6 Add new functions startsWith and endsWith (bug #57041).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
148 %!test
28232
49384057fb03 endsWith.m: overhaul function.
Rik <rik@octave.org>
parents: 28212
diff changeset
149 %! str = {"TESTS.TXT", "mydoc.odt", "result.txt", "myFunc.m"};
49384057fb03 endsWith.m: overhaul function.
Rik <rik@octave.org>
parents: 28212
diff changeset
150 %! pattern = ".txt";
49384057fb03 endsWith.m: overhaul function.
Rik <rik@octave.org>
parents: 28212
diff changeset
151 %! expected_ignore = [true, false, true, false];
49384057fb03 endsWith.m: overhaul function.
Rik <rik@octave.org>
parents: 28212
diff changeset
152 %! expected_wo_ignore = [false, false, true, false];
49384057fb03 endsWith.m: overhaul function.
Rik <rik@octave.org>
parents: 28212
diff changeset
153 %! assert (endsWith (str, pattern, "IgnoreCase", true), expected_ignore);
49384057fb03 endsWith.m: overhaul function.
Rik <rik@octave.org>
parents: 28212
diff changeset
154 %! assert (endsWith (str, pattern, "IgnoreCase", false), expected_wo_ignore);
49384057fb03 endsWith.m: overhaul function.
Rik <rik@octave.org>
parents: 28212
diff changeset
155 %! assert (endsWith (str, pattern, "I", 500), expected_ignore);
49384057fb03 endsWith.m: overhaul function.
Rik <rik@octave.org>
parents: 28212
diff changeset
156 %! assert (endsWith (str, pattern, "iG", 0), expected_wo_ignore);
28212
d052c11d47c6 Add new functions startsWith and endsWith (bug #57041).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
157
28232
49384057fb03 endsWith.m: overhaul function.
Rik <rik@octave.org>
parents: 28212
diff changeset
158 ## Test input validation
49384057fb03 endsWith.m: overhaul function.
Rik <rik@octave.org>
parents: 28212
diff changeset
159 %!error endsWith ()
49384057fb03 endsWith.m: overhaul function.
Rik <rik@octave.org>
parents: 28212
diff changeset
160 %!error endsWith ("A")
49384057fb03 endsWith.m: overhaul function.
Rik <rik@octave.org>
parents: 28212
diff changeset
161 %!error endsWith ("A", "B", "C")
49384057fb03 endsWith.m: overhaul function.
Rik <rik@octave.org>
parents: 28212
diff changeset
162 %!error endsWith ("A", "B", "C", "D", "E")
49384057fb03 endsWith.m: overhaul function.
Rik <rik@octave.org>
parents: 28212
diff changeset
163 %!error <STR must be a string> endsWith (152, "hi")
49384057fb03 endsWith.m: overhaul function.
Rik <rik@octave.org>
parents: 28212
diff changeset
164 %!error <STR must be a .* cell array of strings> endsWith ({152}, "hi")
49384057fb03 endsWith.m: overhaul function.
Rik <rik@octave.org>
parents: 28212
diff changeset
165 %!error <PATTERN must be a string> endsWith ("hi", 152)
49384057fb03 endsWith.m: overhaul function.
Rik <rik@octave.org>
parents: 28212
diff changeset
166 %!error <PATTERN must be a .* cell array of strings> endsWith ("hi", {152})
49384057fb03 endsWith.m: overhaul function.
Rik <rik@octave.org>
parents: 28212
diff changeset
167 %!error <third input must be "IgnoreCase"> endsWith ("hello", "lo", 1, 1)
49384057fb03 endsWith.m: overhaul function.
Rik <rik@octave.org>
parents: 28212
diff changeset
168 %!error <third input must be "IgnoreCase"> endsWith ("hello", "lo", "", 1)
49384057fb03 endsWith.m: overhaul function.
Rik <rik@octave.org>
parents: 28212
diff changeset
169 %!error <third input must be "IgnoreCase"> endsWith ("hello", "lo", "foo", 1)
49384057fb03 endsWith.m: overhaul function.
Rik <rik@octave.org>
parents: 28212
diff changeset
170 %!error <"IgnoreCase" value must be a logical scalar>
49384057fb03 endsWith.m: overhaul function.
Rik <rik@octave.org>
parents: 28212
diff changeset
171 %! endsWith ("hello", "hi", "i", "true");
49384057fb03 endsWith.m: overhaul function.
Rik <rik@octave.org>
parents: 28212
diff changeset
172 %!error <"IgnoreCase" value must be a logical scalar>
49384057fb03 endsWith.m: overhaul function.
Rik <rik@octave.org>
parents: 28212
diff changeset
173 %! endsWith ("hello", "hi", "i", {true});