Exemple #1
0
 public Replacement(Deletion deletion, Insertion insertion)
 {
     base.Index    = Math.Min(deletion.Index, insertion.Index);
     base.Type     = EditType.Replacement;
     base.Position = insertion.Position;
     base.Value    = insertion.Value;
 }
Exemple #2
0
        /// <summary>
        /// Unpacks a byte array into an edit script for patching data.
        /// </summary>
        /// <param name="packedEditScript">The byte array representing the edit script.</param>
        /// <returns>The unpacked edit script.</returns>
        public static EditScript Unpack(byte[] packedEditScript)
        {
            EditScript editScript = new EditScript();

            // Read the format which determines how the packed edit script is read
            var format = (EditScriptFormat)packedEditScript[Header.Format];

            if (format == EditScriptFormat.Grouped)
            {
                // Read the number of deletions, insertions and replacements
                int deletions    = packedEditScript[Header.Deletions];
                int insertions   = packedEditScript[Header.Insertions];
                int replacements = packedEditScript[Header.Replacements];

                // Set the start indexes of the deletion, insertion and replacement sections
                Deletion.Start    = Header.Size;
                Insertion.Start   = Deletion.Start + deletions;
                Replacement.Start = Insertion.Start + insertions;

                // Read and all all deletions
                for (int current = 0; current < deletions; current++)
                {
                    // Read and create the deletion
                    int position = packedEditScript[Deletion.Start + current];
                    var deletion = new Diff.Data.Deletion(position);

                    // Add the deletion into the edit script
                    editScript.Deletions.Add(deletion);
                    editScript.Edits.Add(deletion);
                }

                // Read and add all insertions
                for (int current = 0; current < insertions; current++)
                {
                    // Read and create the insertion
                    byte value     = packedEditScript[Insertion.Start + Insertion.Size * current + Insertion.Value];
                    int  position  = packedEditScript[Insertion.Start + Insertion.Size * current + Insertion.Position];
                    var  insertion = new Diff.Data.Insertion(position, value);

                    // Add the insertion into the edit script
                    editScript.Insertions.Add(insertion);
                    editScript.Edits.Add(insertion);
                }

                // Read and add all replacements
                for (int current = 0; current < replacements; current++)
                {
                    // Read and create the replacement
                    byte value     = packedEditScript[Replacement.Start + Replacement.Size * current + Replacement.Value];
                    int  position  = packedEditScript[Replacement.Start + Replacement.Size * current + Replacement.Position];
                    var  insertion = new Diff.Data.Replacement(position, value);

                    // Add the replacement into the edit script
                    editScript.Insertions.Add(insertion);
                    editScript.Edits.Add(insertion);
                }
            }

            return(editScript);
        }