Fix string split when last part is shorter than delimiter
authorKevin Chou <jlz@fb.com>
Wed, 2 Jul 2014 16:14:24 +0000 (09:14 -0700)
committerTudor Bosman <tudorb@fb.com>
Mon, 7 Jul 2014 15:42:01 +0000 (08:42 -0700)
Summary: The bug is when ignoreEmpty is true, we use the wrong token size, which will be 0 when last token is shorter than delimiter.

Test Plan:
1. Add unit test
2. fbconfig -r folly && fbmake runtests

Reviewed By: philipp@fb.com

Subscribers: maxime, xiaol

FB internal diff: D1415803

folly/String-inl.h
folly/test/StringTest.cpp

index 52b665c75e6e662d464fa65ba42dfb18999f9078..65870582e7cf094314a2abe8ddfb34a8537d1484 100644 (file)
@@ -335,9 +335,8 @@ void internalSplit(DelimT delim, StringPiece sp, OutputIterator out,
       ++tokenSize;
     }
   }
-
+  tokenSize = strSize - tokenStartPos;
   if (!ignoreEmpty || tokenSize > 0) {
-    tokenSize = strSize - tokenStartPos;
     *out++ = conv(StringPiece(&s[tokenStartPos], tokenSize));
   }
 }
index 0bcc2d1e070723c1235f6e59a79bb6af51bb6547..c6da53063c9f0afe481b2c0dd176110aee84ca62 100644 (file)
@@ -624,6 +624,13 @@ void splitTest() {
   EXPECT_EQ(parts[2], "kdbk");
   parts.clear();
 
+  // test last part is shorter than the delimiter
+  folly::split("bc", "abcd", parts, true);
+  EXPECT_EQ(parts.size(), 2);
+  EXPECT_EQ(parts[0], "a");
+  EXPECT_EQ(parts[1], "d");
+  parts.clear();
+
   string orig = "ab2342asdfv~~!";
   folly::split("", orig, parts, true);
   EXPECT_EQ(parts.size(), 1);