Commit 02a9ec75 authored by Kevin Chou's avatar Kevin Chou Committed by Tudor Bosman

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
parent d057a592
...@@ -335,9 +335,8 @@ void internalSplit(DelimT delim, StringPiece sp, OutputIterator out, ...@@ -335,9 +335,8 @@ void internalSplit(DelimT delim, StringPiece sp, OutputIterator out,
++tokenSize; ++tokenSize;
} }
} }
tokenSize = strSize - tokenStartPos;
if (!ignoreEmpty || tokenSize > 0) { if (!ignoreEmpty || tokenSize > 0) {
tokenSize = strSize - tokenStartPos;
*out++ = conv(StringPiece(&s[tokenStartPos], tokenSize)); *out++ = conv(StringPiece(&s[tokenStartPos], tokenSize));
} }
} }
......
...@@ -624,6 +624,13 @@ void splitTest() { ...@@ -624,6 +624,13 @@ void splitTest() {
EXPECT_EQ(parts[2], "kdbk"); EXPECT_EQ(parts[2], "kdbk");
parts.clear(); 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~~!"; string orig = "ab2342asdfv~~!";
folly::split("", orig, parts, true); folly::split("", orig, parts, true);
EXPECT_EQ(parts.size(), 1); EXPECT_EQ(parts.size(), 1);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment