Skip to content

Commit

Permalink
Merge pull request #1 from DarkSatyr/Null-condition-fix
Browse files Browse the repository at this point in the history
Fixed crash on condition when at least 1 string is empty
  • Loading branch information
TheDarkCode committed Nov 1, 2016
2 parents c5f6b7d + 62b2aa4 commit 23c34ea
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions SwiftyLevenshtein.swift
Expand Up @@ -137,11 +137,13 @@ public struct Array2D {

public func slowlevenshtein(sourceString: String, target targetString: String) -> Int {

let targetLength = targetString.characters.count
let sourceLength = sourceString.characters.count
guard targetLength > 0 && sourceLength > 0 else { return max(targetLength, sourceLength) }

let source = Array(sourceString.unicodeScalars)
let target = Array(targetString.unicodeScalars)

let (sourceLength, targetLength) = (source.count, target.count)

var matrix = Array(count: targetLength + 1, repeatedValue: Array(count: sourceLength + 1, repeatedValue: 0))

for x in 1..<targetLength {
Expand Down Expand Up @@ -176,11 +178,13 @@ public func slowlevenshtein(sourceString: String, target targetString: String) -

public func levenshtein(sourceString: String, target targetString: String) -> Int {

let targetLength = targetString.characters.count
let sourceLength = sourceString.characters.count
guard targetLength > 0 && sourceLength > 0 else { return max(targetLength, sourceLength) }

let source = Array(sourceString.unicodeScalars)
let target = Array(targetString.unicodeScalars)

let (sourceLength, targetLength) = (source.count, target.count)

var distance = Array2D(columns: sourceLength + 1, rows: targetLength + 1)

for x in 1...sourceLength {
Expand Down

0 comments on commit 23c34ea

Please sign in to comment.