CreateDeleteSource() public static method

public static CreateDeleteSource ( int sourceIndex, int length ) : DiffResultSpan
sourceIndex int
length int
return DiffResultSpan
Example #1
0
        public List <DiffResultSpan> DiffReport()
        {
            var retval = new List <DiffResultSpan>();
            var dcount = _dest.Count();
            var scount = _source.Count();

            //Deal with the special case of empty files
            if (dcount == 0)
            {
                if (scount > 0)
                {
                    retval.Add(DiffResultSpan.CreateDeleteSource(0, scount));
                }
                return(retval);
            }
            else
            {
                if (scount == 0)
                {
                    retval.Add(DiffResultSpan.CreateAddDestination(0, dcount));
                    return(retval);
                }
            }


            _matchList.Sort();
            var            curDest   = 0;
            var            curSource = 0;
            DiffResultSpan last      = null;

            //Process each match record
            foreach (var drs in _matchList)
            {
                if ((!AddChanges(retval, curDest, drs.DestIndex, curSource, drs.SourceIndex)) &&
                    (last != null))
                {
                    last.AddLength(drs.Length);
                }
                else
                {
                    retval.Add(drs);
                }
                curDest   = drs.DestIndex + drs.Length;
                curSource = drs.SourceIndex + drs.Length;
                last      = drs;
            }

            //Process any tail end data
            AddChanges(retval, curDest, dcount, curSource, scount);

            return(retval);
        }
Example #2
0
        private bool AddChanges(
            List <DiffResultSpan> report,
            int curDest,
            int nextDest,
            int curSource,
            int nextSource)
        {
            var retval     = false;
            var diffDest   = nextDest - curDest;
            var diffSource = nextSource - curSource;
            var minDiff    = 0;

            if (diffDest > 0)
            {
                if (diffSource > 0)
                {
                    minDiff = Math.Min(diffDest, diffSource);
                    report.Add(DiffResultSpan.CreateReplace(curDest, curSource, minDiff));
                    if (diffDest > diffSource)
                    {
                        curDest += minDiff;
                        report.Add(DiffResultSpan.CreateAddDestination(curDest, diffDest - diffSource));
                    }
                    else
                    {
                        if (diffSource > diffDest)
                        {
                            curSource += minDiff;
                            report.Add(DiffResultSpan.CreateDeleteSource(curSource, diffSource - diffDest));
                        }
                    }
                }
                else
                {
                    report.Add(DiffResultSpan.CreateAddDestination(curDest, diffDest));
                }
                retval = true;
            }
            else
            {
                if (diffSource > 0)
                {
                    report.Add(DiffResultSpan.CreateDeleteSource(curSource, diffSource));
                    retval = true;
                }
            }
            return(retval);
        }