comparison scripts/strings/strmatch.m @ 12937:1eebae7ac5d2

strmatch.m: Trim search pattern of spaces and nulls. * strmatch.m: Trim search pattern of spaces and nulls
author Rik <octave@nomad.inbox5.com>
date Mon, 08 Aug 2011 11:19:09 -0700
parents a499469b05a4
children b80b18f537ca
comparison
equal deleted inserted replaced
12936:b74cb659e757 12937:1eebae7ac5d2
23 ## @deftypefnx {Function File} {} strmatch (@var{s}, @var{A}, "exact") 23 ## @deftypefnx {Function File} {} strmatch (@var{s}, @var{A}, "exact")
24 ## Return indices of entries of @var{A} which begin with the string @var{s}. 24 ## Return indices of entries of @var{A} which begin with the string @var{s}.
25 ## The second argument @var{A} must be a string, character matrix, or a cell 25 ## The second argument @var{A} must be a string, character matrix, or a cell
26 ## array of strings. If the third argument @code{"exact"} is not given, then 26 ## array of strings. If the third argument @code{"exact"} is not given, then
27 ## @var{s} only needs to match @var{A} up to the length of @var{s}. 27 ## @var{s} only needs to match @var{A} up to the length of @var{s}.
28 ## Trailing spaces and nulls in @var{A} are ignored even with the @code{"exact"} 28 ## Trailing spaces and nulls in @var{s} and @var{A} are ignored when matching.
29 ## option. 29 ## option.
30 ## 30 ##
31 ## For example: 31 ## For example:
32 ## 32 ##
33 ## @example 33 ## @example
61 error ("strmatch: S must be a string"); 61 error ("strmatch: S must be a string");
62 elseif (! (ischar (A) || iscellstr (A))) 62 elseif (! (ischar (A) || iscellstr (A)))
63 error ("strmatch: A must be a string or cell array of strings"); 63 error ("strmatch: A must be a string or cell array of strings");
64 endif 64 endif
65 65
66 ## Trim blanks and nulls from search string
67 s = regexprep (s, "[ \\0]+$", '');
66 len = length (s); 68 len = length (s);
67 69
68 exact = nargin == 3 && ischar (exact) && strcmp (exact, "exact"); 70 exact = nargin == 3 && ischar (exact) && strcmp (exact, "exact");
69 71
70 if (ischar (A)) 72 if (ischar (A))
98 %!assert (strmatch("a", {"aaa", "bab", "bbb"}), 1); 100 %!assert (strmatch("a", {"aaa", "bab", "bbb"}), 1);
99 %!assert (strmatch ("apple", "apple juice"), 1); 101 %!assert (strmatch ("apple", "apple juice"), 1);
100 %!assert (strmatch ("apple", ["apple pie"; "apple juice"; "an apple"]), [1; 2]); 102 %!assert (strmatch ("apple", ["apple pie"; "apple juice"; "an apple"]), [1; 2]);
101 %!assert (strmatch ("apple", {"apple pie"; "apple juice"; "tomato"}), [1; 2]); 103 %!assert (strmatch ("apple", {"apple pie"; "apple juice"; "tomato"}), [1; 2]);
102 %!assert (strmatch ("apple pie", "apple"), []); 104 %!assert (strmatch ("apple pie", "apple"), []);
103 %!assert (strmatch ("a ", "a"), []); 105 %!assert (strmatch ("a ", "a"), 1);
104 %!assert (strmatch ("a", "a \0", "exact"), 1); 106 %!assert (strmatch ("a", "a \0", "exact"), 1);
105 %!assert (strmatch ("a b", {"a b", "a c", "c d"}), 1); 107 %!assert (strmatch ("a b", {"a b", "a c", "c d"}), 1);
106 %!assert (strmatch ("", {"", "foo", "bar", ""}), [1, 4]); 108 %!assert (strmatch ("", {"", "foo", "bar", ""}), [1, 4]);
107 %!assert (strmatch ('', { '', '% comment', 'var a = 5', ''}, 'exact'), [1,4]); 109 %!assert (strmatch ('', { '', '% comment', 'var a = 5', ''}, 'exact'), [1,4]);
108 110