Skip to content

Commit

Permalink
Fix index error
Browse files Browse the repository at this point in the history
  • Loading branch information
pemistahl committed Dec 27, 2022
1 parent 321f7a5 commit 783d916
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,15 +246,17 @@ func (detector languageDetector) DetectMultipleLanguagesOf(text string) []Detect

results = mergeAdjacentResults(results, mergeableResultIndices)

mergeableResultIndices = nil
if len(results) > 1 {
mergeableResultIndices = nil

for i := 0; i < len(results)-1; i++ {
if results[i].Language() == results[i+1].Language() {
mergeableResultIndices = append(mergeableResultIndices, i+1)
for i := 0; i < len(results)-1; i++ {
if results[i].Language() == results[i+1].Language() {
mergeableResultIndices = append(mergeableResultIndices, i+1)
}
}
}

results = mergeAdjacentResults(results, mergeableResultIndices)
results = mergeAdjacentResults(results, mergeableResultIndices)
}
}

detector.languages = previousDetectorLanguages
Expand Down Expand Up @@ -794,6 +796,10 @@ func mergeAdjacentResults(results []detectionResult, mergeableResultIndices []in
results[i-1].endIndex = results[i].endIndex
}
results = slices.Delete(results, i, i+1)

if len(results) == 1 {
break
}
}

return results
Expand Down

0 comments on commit 783d916

Please sign in to comment.