patchMake2 computes a list of patches to turn text1 into text2. text2 is not provided, diffs are the delta between text1 and text2.
(text1 string, diffs []Diff)
| 139 | // patchMake2 computes a list of patches to turn text1 into text2. |
| 140 | // text2 is not provided, diffs are the delta between text1 and text2. |
| 141 | func (dmp *DiffMatchPatch) patchMake2(text1 string, diffs []Diff) []Patch { |
| 142 | // Check for null inputs not needed since null can't be passed in C#. |
| 143 | patches := []Patch{} |
| 144 | if len(diffs) == 0 { |
| 145 | return patches // Get rid of the null case. |
| 146 | } |
| 147 | |
| 148 | patch := Patch{} |
| 149 | charCount1 := 0 // Number of characters into the text1 string. |
| 150 | charCount2 := 0 // Number of characters into the text2 string. |
| 151 | // Start with text1 (prepatchText) and apply the diffs until we arrive at text2 (postpatchText). We recreate the patches one by one to determine context info. |
| 152 | prepatchText := text1 |
| 153 | postpatchText := text1 |
| 154 | |
| 155 | for i, aDiff := range diffs { |
| 156 | if len(patch.diffs) == 0 && aDiff.Type != DiffEqual { |
| 157 | // A new patch starts here. |
| 158 | patch.Start1 = charCount1 |
| 159 | patch.Start2 = charCount2 |
| 160 | } |
| 161 | |
| 162 | switch aDiff.Type { |
| 163 | case DiffInsert: |
| 164 | patch.diffs = append(patch.diffs, aDiff) |
| 165 | patch.Length2 += len(aDiff.Text) |
| 166 | postpatchText = postpatchText[:charCount2] + |
| 167 | aDiff.Text + postpatchText[charCount2:] |
| 168 | case DiffDelete: |
| 169 | patch.Length1 += len(aDiff.Text) |
| 170 | patch.diffs = append(patch.diffs, aDiff) |
| 171 | postpatchText = postpatchText[:charCount2] + postpatchText[charCount2+len(aDiff.Text):] |
| 172 | case DiffEqual: |
| 173 | if len(aDiff.Text) <= 2*dmp.PatchMargin && |
| 174 | len(patch.diffs) != 0 && i != len(diffs)-1 { |
| 175 | // Small equality inside a patch. |
| 176 | patch.diffs = append(patch.diffs, aDiff) |
| 177 | patch.Length1 += len(aDiff.Text) |
| 178 | patch.Length2 += len(aDiff.Text) |
| 179 | } |
| 180 | if len(aDiff.Text) >= 2*dmp.PatchMargin { |
| 181 | // Time for a new patch. |
| 182 | if len(patch.diffs) != 0 { |
| 183 | patch = dmp.PatchAddContext(patch, prepatchText) |
| 184 | patches = append(patches, patch) |
| 185 | patch = Patch{} |
| 186 | // Unlike Unidiff, our patch lists have a rolling context. http://code.google.com/p/google-diff-match-patch/wiki/Unidiff Update prepatch text & pos to reflect the application of the just completed patch. |
| 187 | prepatchText = postpatchText |
| 188 | charCount1 = charCount2 |
| 189 | } |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | // Update the current character count. |
| 194 | if aDiff.Type != DiffInsert { |
| 195 | charCount1 += len(aDiff.Text) |
| 196 | } |
| 197 | if aDiff.Type != DiffDelete { |
| 198 | charCount2 += len(aDiff.Text) |
no test coverage detected