From 02a9ec75b0c1ee5740a07b356d012db64f657dcf Mon Sep 17 00:00:00 2001 From: Kevin Chou Date: Wed, 2 Jul 2014 09:14:24 -0700 Subject: [PATCH] Fix string split when last part is shorter than delimiter 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 | 3 +-- folly/test/StringTest.cpp | 7 +++++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/folly/String-inl.h b/folly/String-inl.h index 52b665c7..65870582 100644 --- a/folly/String-inl.h +++ b/folly/String-inl.h @@ -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)); } } diff --git a/folly/test/StringTest.cpp b/folly/test/StringTest.cpp index 0bcc2d1e..c6da5306 100644 --- a/folly/test/StringTest.cpp +++ b/folly/test/StringTest.cpp @@ -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); -- 2.34.1