Skip to content

Commit

Permalink
* Invalid regular expressions now produce empty outputs with XX (`r…
Browse files Browse the repository at this point in the history
…egexp`) on Octave, like they do in Matlab

* Invalid regular expressions now do nothing with `YX` (`regexprep`) on Octave, like they do in Matlab

* `YX` now converts cell array contents into char, like `XX` does

* `matl -e` not correctly produces automatic comments for `break` and `continue

* `e` (`reshape`) with second input logical now works as follows: contiguous equal values indicate dimensions that will be collapsed; and a final `[]` is implicit
  • Loading branch information
lmendo committed Jun 8, 2016
1 parent e31f859 commit 412b07a
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 7 deletions.
12 changes: 12 additions & 0 deletions compatibility/regexp_comp.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
function varargout = regexp(varargin)
% If regular expression is invalid, instead of an error it gives empty outputs,
% like Matlab does. For that we use a try...catch block; if an error is found we
% use an empty regexp to produce empty outputs
nargout = max(nargout,1);
try
[varargout{1:nargout}] = builtin('regexp', varargin{:});
catch
varargin{2} = '';
[varargout{1:nargout}] = builtin('regexp', varargin{:});
end
end
12 changes: 12 additions & 0 deletions compatibility/regexprep_comp.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
function varargout = regexprep(varargin)
% If regular expression is invalid, instead of an error it gives empty outputs,
% like Matlab does. For that we use a try...catch block; if an error is found we
% use an empty regexp to produce empty outputs
nargout = max(nargout,1);
try
[varargout{1:nargout}] = builtin('regexprep', varargin{:});
catch
varargin{2} = '';
[varargout{1:nargout}] = builtin('regexprep', varargin{:});
end
end
Binary file modified doc/MATL_spec.pdf
Binary file not shown.
Binary file modified funDef.mat
Binary file not shown.
25 changes: 21 additions & 4 deletions funDef.txt
Original file line number Diff line number Diff line change
Expand Up @@ -703,12 +703,15 @@ XW
YW
ZW
X
XX 2 9 2 3 1 6 max(1,sum(ismember(cellfun(@num2str, in(3:end), 'uniformoutput', false), {'start' 'end' 'tokenExtents' 'match' 'tokens' 'split' '1' '2' '3' '5' '6' '7'}))) true true true true str = {'start' 'split' 'end' 'once' 'tokenExtents' 'tokens' 'match'}; match regular expression \matlab+regexp+. With $2$ inputs: \matlab+regexp(..., ..., 'match')+. If first or second inputs are numeric they are converted to char. \matlab+'names'+ output is not supported. This function allows flag strings in third and subsequent inputs to be replaced by numbers, as follows: 1: \matlab+'start'+, 2: \matlab+'split'+, 3: \matlab+'end'+, 4: \matlab+'once'+, 5: \matlab+'tokenExtents'+, 6: \matlab+'tokens'+, 7: \matlab+'match'+
XX 2 9 2 3 1 6 max(1,sum(ismember(cellfun(@num2str, in(3:end), 'uniformoutput', false), {'start' 'end' 'tokenExtents' 'match' 'tokens' 'split' '1' '2' '3' '5' '6' '7'}))) true true true true str = {'start' 'split' 'end' 'once' 'tokenExtents' 'tokens' 'match'}; match regular expression \matlab+regexp+. With $2$ inputs: \matlab+regexp(..., ..., 'match')+. If first or second inputs are numeric they are converted to char. If they are numeric or char arrays they are linearized into a row. If they are cells their contents are converted to char. \matlab+'names'+ output is not supported. This function allows flag strings in third and subsequent inputs to be replaced by numbers, as follows: 1: \matlab+'start'+, 2: \matlab+'split'+, 3: \matlab+'end'+, 4: \matlab+'once'+, 5: \matlab+'tokenExtents'+, 6: \matlab+'tokens'+, 7: \matlab+'match'+
for k = 3:numel(in), if isnumeric(in{k}), in(k) = str(in{k}); end; end; clear str
if numel(in)==2, in{3} = 'match'; end
if isnumeric(in{1}), in{1}=char(in{1}); end; if isnumeric(in{2}), in{2}=char(in{2}); end
if size(in{1},1)>1, in{1} = in{1}(:).'; end; if size(in{2},1)>1, in{2} = in{2}(:).'; end
if iscell(in{1}), in{1} = cellfun(@char, in{1}, 'UniformOutput', false); end; if iscell(in{2}), in{2} = cellfun(@char, in{2}, 'UniformOutput', false); end;
[out{:}] = regexp(in{:});
YX 3 5 3 4 1 1 1 true true true true if isnumeric(in{1}), in{1}=char(in{1}); end; if isnumeric(in{2}), in{2}=char(in{2}); end; if isnumeric(in{3}), in{3}=char(in{3}); end replace string using regular expression \matlab+regexprep+. If first, second or third inputs are numeric they are converted to char. If they are cells their contents are converted to char
YX 3 5 3 4 1 1 1 true true true true if isnumeric(in{1}), in{1}=char(in{1}); end; if isnumeric(in{2}), in{2}=char(in{2}); end; if isnumeric(in{3}), in{3}=char(in{3}); end replace string using regular expression \matlab+regexprep+. If first, second or third inputs are numeric they are converted to char. If they are numeric or char arrays they are linearized into a row. If they are cells their contents are converted to char
if size(in{1},1)>1, in{1} = in{1}(:).'; end; if size(in{2},1)>1, in{2} = in{2}(:).'; end; if size(in{3},1)>1, in{3} = in{3}(:).'; end;
if iscell(in{1}), in{1} = cellfun(@char, in{1}, 'UniformOutput', false); end; if iscell(in{2}), in{2} = cellfun(@char, in{2}, 'UniformOutput', false); end; if iscell(in{3}), in{3} = cellfun(@char, in{3}, 'UniformOutput', false); end
[out{:}] = regexprep(in{:});
ZX
Expand Down Expand Up @@ -922,8 +925,21 @@ Zd 1 2 2 1 1 3 1 true true true true if numel(in)==1 && numel(out)==1, x=in{1}(
clear nd sz1 sz2 rm1 rm2 insx1 insx2
end
end
e 1 inf 2 1 1 1 1 true true true true if numel(in)>1 reshape array / remove singleton dimensions With $1$ input: \matlab+squeeze+. With more than $1$ input: \matlab+reshape+. If second input is logical, \matlab+size+ is automatically applied to it. If second input is a (non-logical) scalar, a final \matlab+[]+ is implicit. If size specification doesn't contain \matlab+[]+ (explicit or implicit), the first input is padded or truncated if needed; padding is done with \matlab+0+, \matlab+false+, \matlab+char(0)+ or \matlab+{[]}+ according to type of first input. If size specification contains \matlab+[]+ (explicit or implicit), the first input is padded if needed
if islogical(in{2}), in{2} = size(in{2}); end
e 1 inf 2 1 1 1 1 true true true true if numel(in)>1 reshape array / remove singleton dimensions (i) With $1$ input: \matlab+squeeze+. (ii) With more than $1$ input: \matlab+reshape+. If second input is logical, contiguous equal values indicate dimensions that will be collapsed; and a final \matlab+[]+ is implicit. If second input is a (non-logical) scalar, a final \matlab+[]+ is implicit. If size specification doesn't contain \matlab+[]+ (explicit or implicit), the first input is padded or truncated if needed; padding is done with \matlab+0+, \matlab+false+, \matlab+char(0)+ or \matlab+{[]}+ according to type of first input. If size specification contains \matlab+[]+ (explicit or implicit), the first input is padded if needed
if islogical(in{2})
nd = {size(in{1},1)};
v = in{2}(1);
for k = 2:numel(in{2});
if in{2}(k)==v
nd{end} = nd{end}*size(in{1},k);
else
nd{end+1} = size(in{1},k);
v = in{2}(k);
end
end
nd{end+1} = [];
out{1} = reshape(in{1}, nd{:});
else
if numel(in)==2 && isscalar(in{2}), in{3} = []; end
if ~any(cellfun(@isempty, in(2:end))) && prod([in{2:end}])>numel(in{1})
if isnumeric(in{1}), in{1} = in{1}(:); in{1}(end+1:prod([in{2:end}])) = 0;
Expand All @@ -947,6 +963,7 @@ e 1 inf 2 1 1 1 1 true true true true if numel(in)>1 reshape array / remove sin
else error('MATL:runtime', 'MATL run-time error: unrecognized type of first input');
end, end
out{1} = reshape(in{:});
end
else
out{1} = squeeze(in{:});
end
Expand Down
Binary file modified help.mat
Binary file not shown.
2 changes: 1 addition & 1 deletion matl_compile.m
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@
fnames = {'num2str' 'im2col' 'spiral' 'unique' 'union' 'intersect' 'setdiff' 'setxor' 'ismember' ...
'triu' 'tril' 'randsample' 'nchoosek' 'vpa' 'sum' 'mean' 'diff' 'mod' 'repelem' 'dec2bin' 'dec2base' ...
'hypergeom' 'disp' 'str2func' 'logical' 'circshift' 'pdist2' 'strsplit' 'max' 'min' 'strncmp' 'round'...
'datestr' };
'datestr' 'regexp' 'regexprep'};
verNumTh = [4 0 0]; % first version in which a modified function is not needed:
if (verNum(1)<verNumTh(1)) || ((verNum(1)==verNumTh(1)) && (verNum(2)<verNumTh(2))) || ((verNum(1)==verNumTh(1)) && (verNum(2)==verNumTh(2)) && (verNum(3)<verNumTh(3)))
fnames = [fnames {'colon'}];
Expand Down
4 changes: 2 additions & 2 deletions matl_disp.m
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ function matl_disp(S, F, indentBase, indentStep, indentCommentSymbol, indentComm
texts(strcmp(Stype, 'controlFlow.doWhileIndex')) = {'do...while loop iteration index'};
texts(strcmp(Stype, 'controlFlow.whileIndex')) = {'while loop iteration index'};
texts(strcmp(Stype, 'controlFlow.end')) = {'end'};
texts(strcmp(Stype, 'controlFlow.conditionalBreak')) = {'conditional break'};
texts(strcmp(Stype, 'controlFlow.conditionalContinue')) = {'conditional continue'};
texts(strcmp(Stype, 'controlFlow.break')) = {'break'};
texts(strcmp(Stype, 'controlFlow.continue')) = {'continue'};
ind = find(ismember(Stype, {'function'}));
[val, indF] = ismember({S(ind).source}, {F.source}); % val equal to false indicates there's no comment (or perhaps no function)
texts(ind(val)) = {F(indF(val)).comment};
Expand Down

0 comments on commit 412b07a

Please sign in to comment.