From 783d916a882104b8f192da1990271e208cd98e82 Mon Sep 17 00:00:00 2001 From: Peter Stahl Date: Tue, 27 Dec 2022 23:01:24 +0100 Subject: [PATCH] Fix index error --- detector.go | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/detector.go b/detector.go index 59e1c32..07718a4 100644 --- a/detector.go +++ b/detector.go @@ -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 @@ -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