function skip = pFindLinesToSkip(match,skip_padding)
% Finds lines which can be hidden in a Text Comparison report
%
% Given a logical array indicating which lines in a text comparison are
% unmodified, and the number of lines which should be shown on either side
% of a "skipped" section, returns a numeric array indicating which lines
% should be skipped.  In this array:
%  zero indicates a line which should not be skipped
% a non-zero value indicates a line which should be skipped, and the first
% non-zero element in any series of non-zero elements indicates the length
% of that series (e.g. [0 3 1 1 0]).

%   Copyright 2010 The MathWorks, Inc.

% There's no point in replacing a single line with a line saying "skipped 1
% line", so the minimum number of lines we can skip is skip_padding*2 + 2,
% or, at either end of the file, just skip_padding+2.

% Count the length of each series of matching lines, and when we find the
% end of the series mark any which we can skip.

skip = zeros(size(match));

matchcount = 0; % number of matching lines in this series.

for i=1:numel(match)
    if match(i)
        % This line matches.  Increment the count.
        matchcount = matchcount + 1;
    elseif matchcount<i-1 && matchcount>=skip_padding+skip_padding+2
        % End of a sufficiently long series of matching lines.  Skip some
        % of them, and set the value at the first skipped line to be the
        % number of lines skipped.
        first_skip = i-matchcount+skip_padding;
        skip(first_skip+1:i-skip_padding-1) = 1;
        skip(first_skip) = matchcount-(skip_padding*2);
        matchcount = 0;
    elseif matchcount==i-1 && matchcount>=skip_padding+2
        % End of a sufficiently long series of matching lines at the
        % beginning of the file.
        skip(1:i-skip_padding-1) = 1;
        skip(1) = matchcount-skip_padding;
        matchcount = 0;
    elseif matchcount>0
        matchcount = 0;
    end
end
if matchcount>=skip_padding+2
    % A sufficiently long series of matching lines at the end of the file.
    if matchcount==length(skip)
        % Matches all the way from the beginning.  Skip the whole lot.
        skip(1) = matchcount;
        skip(2:end) = 1;
    else
        first_skip = length(skip)+1-matchcount+skip_padding;
        skip(first_skip+1:end) = 1;
        skip(first_skip) = matchcount-skip_padding;
    end
end

