Files
owui-tools/octave/shim/contains.m
T
2026-09-15 15:24:03 -04:00

55 lines
1.7 KiB
Matlab

function tf = contains (str, pat, varargin)
%CONTAINS True if a pattern occurs in a string (MATLAB R2016b+).
%
% One of the owuitools compatibility shims. This Octave has no
% `contains`, so a perfectly ordinary MATLAB line failed here and the
% model spent calls rediscovering `strfind`. The rule for everything in
% this directory: implement the documented behaviour for the forms that
% are actually written, and raise a clear error for the rest. A shim
% that quietly does something *close to* MATLAB is worse than one that
% is not there, because the run then certifies the wrong answer.
%
% Text here means a char row vector or a cell array of them - this
% Octave has no string type, and pretending otherwise is exactly the
% quiet difference this file exists to avoid.
if (nargin < 2)
error ('contains: not enough input arguments');
end
ignore_case = false;
if (numel (varargin) == 2 && ischar (varargin{1}) ...
&& strcmpi (varargin{1}, 'IgnoreCase'))
ignore_case = logical (varargin{2});
elseif (~isempty (varargin))
error ('contains: the only supported option is ''IgnoreCase''');
end
strs = owuitools_text_cell (str, 'contains', 'str');
pats = owuitools_text_cell (pat, 'contains', 'pat');
if (isempty (pats))
error ('contains: pat must not be empty');
end
tf = false (size (strs));
for i = 1:numel (strs)
s = strs{i};
for j = 1:numel (pats)
p = pats{j};
if (ignore_case)
hit = ~isempty (strfind (lower (s), lower (p)));
else
hit = ~isempty (strfind (s, p));
end
if (hit)
tf(i) = true;
break;
end
end
end
if (ischar (str))
tf = tf(1);
end
end