diff --git a/llvm/include/llvm/ADT/edit_distance.h b/llvm/include/llvm/ADT/edit_distance.h --- a/llvm/include/llvm/ADT/edit_distance.h +++ b/llvm/include/llvm/ADT/edit_distance.h @@ -57,6 +57,10 @@ typename ArrayRef::size_type m = FromArray.size(); typename ArrayRef::size_type n = ToArray.size(); + // if either of the strings are empty, return the length of the other string. + if(m == 0) return n; + if(n == 0) return m; + const unsigned SmallBufferSize = 64; unsigned SmallBuffer[SmallBufferSize]; std::unique_ptr Allocated; diff --git a/llvm/unittests/ADT/StringRefTest.cpp b/llvm/unittests/ADT/StringRefTest.cpp --- a/llvm/unittests/ADT/StringRefTest.cpp +++ b/llvm/unittests/ADT/StringRefTest.cpp @@ -508,6 +508,11 @@ } TEST(StringRefTest, EditDistance) { + StringRef StrNull; + StringRef StrEmpty(""); + + EXPECT_EQ(0U, StrNull.edit_distance("")); + EXPECT_EQ(0U, StrEmpty.edit_distance("")); StringRef Hello("hello"); EXPECT_EQ(2U, Hello.edit_distance("hill"));