@@ -137,11 +137,13 @@ public struct Array2D {
137137
138138public func slowlevenshtein( sourceString: String , target targetString: String ) -> Int {
139139
140+ let targetLength = targetString. characters. count
141+ let sourceLength = sourceString. characters. count
142+ guard targetLength > 0 && sourceLength > 0 else { return max ( targetLength, sourceLength) }
143+
140144 let source = Array ( sourceString. unicodeScalars)
141145 let target = Array ( targetString. unicodeScalars)
142146
143- let ( sourceLength, targetLength) = ( source. count, target. count)
144-
145147 var matrix = Array ( count: targetLength + 1 , repeatedValue: Array ( count: sourceLength + 1 , repeatedValue: 0 ) )
146148
147149 for x in 1 ..< targetLength {
@@ -176,11 +178,13 @@ public func slowlevenshtein(sourceString: String, target targetString: String) -
176178
177179public func levenshtein( sourceString: String , target targetString: String ) -> Int {
178180
181+ let targetLength = targetString. characters. count
182+ let sourceLength = sourceString. characters. count
183+ guard targetLength > 0 && sourceLength > 0 else { return max ( targetLength, sourceLength) }
184+
179185 let source = Array ( sourceString. unicodeScalars)
180186 let target = Array ( targetString. unicodeScalars)
181187
182- let ( sourceLength, targetLength) = ( source. count, target. count)
183-
184188 var distance = Array2D ( columns: sourceLength + 1 , rows: targetLength + 1 )
185189
186190 for x in 1 ... sourceLength {
0 commit comments