[PATCH] D67000: Fixes bug 28780

Shreyansh Chouhan via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Aug 30 06:00:58 PDT 2019


BK1603 created this revision.
Herald added a subscriber: dexonsmith.
Herald added a project: LLVM.
BK1603 retitled this revision from "Summary:
Fixes bug 28780 [https://bugs.llvm.org/show_bug.cgi?id=28780]" to "Summary:Fixes bug 28780 [https://bugs.llvm.org/show_bug.cgi?id=28780]".
BK1603 added reviewers: chandlerc, xiangzhai.
BK1603 retitled this revision from "Summary:Fixes bug 28780 [https://bugs.llvm.org/show_bug.cgi?id=28780]" to "Fixes bug 28780 ".
BK1603 edited the summary of this revision.

Fixes the edit_distance bug where if both the strings are
empty the algorithm returns a random integer.

Edited the already existing test and added a test for the case
when both strings are empty.

Bug link: [https://bugs.llvm.org/show_bug.cgi?id=28780]


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D67000

Files:
  llvm/include/llvm/ADT/edit_distance.h
  llvm/unittests/ADT/StringRefTest.cpp


Index: llvm/unittests/ADT/StringRefTest.cpp
===================================================================
--- llvm/unittests/ADT/StringRefTest.cpp
+++ 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"));
 
Index: llvm/include/llvm/ADT/edit_distance.h
===================================================================
--- llvm/include/llvm/ADT/edit_distance.h
+++ llvm/include/llvm/ADT/edit_distance.h
@@ -57,6 +57,9 @@
   typename ArrayRef<T>::size_type m = FromArray.size();
   typename ArrayRef<T>::size_type n = ToArray.size();
 
+	if(m == 0) return n;
+	if(n == 0) return m;
+
   const unsigned SmallBufferSize = 64;
   unsigned SmallBuffer[SmallBufferSize];
   std::unique_ptr<unsigned[]> Allocated;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D67000.218075.patch
Type: text/x-patch
Size: 974 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190830/72d1bf03/attachment.bin>


More information about the llvm-commits mailing list