Skip to content

Commit

Permalink
use utf8.RuneCountInString
Browse files Browse the repository at this point in the history
  • Loading branch information
pakohan committed Mar 10, 2020
1 parent c6eeac7 commit e013302
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions diffmatchpatch/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -670,16 +670,16 @@ func (dmp *DiffMatchPatch) DiffCleanupSemantic(diffs []Diff) []Diff {
// An insertion or deletion.

if diffs[pointer].Type == DiffInsert {
lengthInsertions2 += len([]rune(diffs[pointer].Text))
lengthInsertions2 += utf8.RuneCountInString(diffs[pointer].Text)
} else {
lengthDeletions2 += len([]rune(diffs[pointer].Text))
lengthDeletions2 += utf8.RuneCountInString(diffs[pointer].Text)
}
// Eliminate an equality that is smaller or equal to the edits on both sides of it.
difference1 := int(math.Max(float64(lengthInsertions1), float64(lengthDeletions1)))
difference2 := int(math.Max(float64(lengthInsertions2), float64(lengthDeletions2)))
if len([]rune(lastequality)) > 0 &&
(len([]rune(lastequality)) <= difference1) &&
(len([]rune(lastequality)) <= difference2) {
if utf8.RuneCountInString(lastequality) > 0 &&
(utf8.RuneCountInString(lastequality) <= difference1) &&
(utf8.RuneCountInString(lastequality) <= difference2) {
// Duplicate record.
insPoint := equalities[len(equalities)-1]
diffs = splice(diffs, insPoint, 0, Diff{DiffDelete, lastequality})
Expand Down Expand Up @@ -728,8 +728,8 @@ func (dmp *DiffMatchPatch) DiffCleanupSemantic(diffs []Diff) []Diff {
overlapLength1 := dmp.DiffCommonOverlap(deletion, insertion)
overlapLength2 := dmp.DiffCommonOverlap(insertion, deletion)
if overlapLength1 >= overlapLength2 {
if float64(overlapLength1) >= float64(len([]rune(deletion)))/2 ||
float64(overlapLength1) >= float64(len([]rune(insertion)))/2 {
if float64(overlapLength1) >= float64(utf8.RuneCountInString(deletion))/2 ||
float64(overlapLength1) >= float64(utf8.RuneCountInString(insertion))/2 {

// Overlap found. Insert an equality and trim the surrounding edits.
diffs = splice(diffs, pointer, 0, Diff{DiffEqual, insertion[:overlapLength1]})
Expand All @@ -739,8 +739,8 @@ func (dmp *DiffMatchPatch) DiffCleanupSemantic(diffs []Diff) []Diff {
pointer++
}
} else {
if float64(overlapLength2) >= float64(len([]rune(deletion)))/2 ||
float64(overlapLength2) >= float64(len([]rune(insertion)))/2 {
if float64(overlapLength2) >= float64(utf8.RuneCountInString(deletion))/2 ||
float64(overlapLength2) >= float64(utf8.RuneCountInString(insertion))/2 {
// Reverse overlap found. Insert an equality and swap and trim the surrounding edits.
overlap := Diff{DiffEqual, deletion[:overlapLength2]}
diffs = splice(diffs, pointer, 0, overlap)
Expand Down

0 comments on commit e013302

Please sign in to comment.