Skip to content

Commit 23c34ea

Browse files
authored
Merge pull request #1 from DarkSatyr/Null-condition-fix
Fixed crash on condition when at least 1 string is empty
2 parents c5f6b7d + 62b2aa4 commit 23c34ea

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

SwiftyLevenshtein.swift

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,11 +137,13 @@ public struct Array2D {
137137

138138
public 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

177179
public 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

Comments
 (0)