Skip to content

Commit

Permalink
Merge pull request #1 from katbyte/sergi/pull/125
Browse files Browse the repository at this point in the history
copy of sergi#125 to solve sergi#123 for katbyte/terrafmt
  • Loading branch information
katbyte authored Aug 10, 2022
2 parents cee9a28 + fc6e569 commit 7df81bf
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions diffmatchpatch/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const (
DiffInsert Operation = 1
// DiffEqual item represents an equal diff.
DiffEqual Operation = 0
//IndexSeparator is used to seperate the array indexes in an index string
// IndexSeparator is used to seperate the array indexes in an index string
IndexSeparator = ","
)

Expand Down Expand Up @@ -1168,7 +1168,7 @@ func (dmp *DiffMatchPatch) DiffPrettyText(diffs []Diff) string {

// DiffText1 computes and returns the source text (all equalities and deletions).
func (dmp *DiffMatchPatch) DiffText1(diffs []Diff) string {
//StringBuilder text = new StringBuilder()
// StringBuilder text = new StringBuilder()
var text bytes.Buffer

for _, aDiff := range diffs {
Expand Down Expand Up @@ -1312,18 +1312,19 @@ func (dmp *DiffMatchPatch) DiffFromDelta(text1 string, delta string) (diffs []Di
func (dmp *DiffMatchPatch) diffLinesToStrings(text1, text2 string) (string, string, []string) {
// '\x00' is a valid character, but various debuggers don't like it. So we'll insert a junk entry to avoid generating a null character.
lineArray := []string{""} // e.g. lineArray[4] == 'Hello\n'
lineHash := make(map[string]int)
lineHash[""] = 0

//Each string has the index of lineArray which it points to
strIndexArray1 := dmp.diffLinesToStringsMunge(text1, &lineArray)
strIndexArray2 := dmp.diffLinesToStringsMunge(text2, &lineArray)
// Each string has the index of lineArray which it points to
strIndexArray1 := dmp.diffLinesToStringsMunge(text1, &lineArray, lineHash)
strIndexArray2 := dmp.diffLinesToStringsMunge(text2, &lineArray, lineHash)

return intArrayToString(strIndexArray1), intArrayToString(strIndexArray2), lineArray
}

// diffLinesToStringsMunge splits a text into an array of strings, and reduces the texts to a []string.
func (dmp *DiffMatchPatch) diffLinesToStringsMunge(text string, lineArray *[]string) []uint32 {
func (dmp *DiffMatchPatch) diffLinesToStringsMunge(text string, lineArray *[]string, lineHash map[string]int) []uint32 {
// Walk the text, pulling out a substring for each line. text.split('\n') would would temporarily double our memory footprint. Modifying text would create many large strings to garbage collect.
lineHash := map[string]int{} // e.g. lineHash['Hello\n'] == 4
lineStart := 0
lineEnd := -1
strs := []uint32{}
Expand Down

0 comments on commit 7df81bf

Please sign in to comment.