Opened 7 years ago

Closed 7 years ago

Last modified 7 years ago

#14045 closed bug (fixed)

BString::Split doesn't work as expected

Reported by: Janus Owned by: nobody
Priority: normal Milestone: Unscheduled
Component: Kits/Support Kit Version: R1/Development
Keywords: Cc:
Blocked By: Blocking:
Platform: All

Description

Based on this issue https://github.com/HaikuArchives/Tipster/issues/1 I have investigated the problem... I think there is an error in the split function

499 bool
500 BString::Split(const char* separator, bool noEmptyStrings,
501	BStringList& _list) const
502{
503	int32 separatorLength = strlen(separator);
504	int32 length = Length();
505	if (separatorLength == 0 || length == 0 || separatorLength > length) {
506		if (length == 0 && noEmptyStrings)
507			return true;
508		return _list.Add(*this);
509	}
510
511	int32 index = 0;
512	for (;;) {
513		int32 endIndex = index < length ? FindFirst(separator, index) : length;
514		if (endIndex < 0)
515			endIndex = length;
516
517		if (endIndex > index || !noEmptyStrings) {
518			BString toAppend(String() + index, endIndex - index);
519			if (toAppend.Length() != endIndex - index
520				|| !_list.Add(toAppend)) {
521				return false;
522			}
523		}
524
525		if (endIndex == length)
526			break;
527
528		index = endIndex + 1;
529	}
530
531	return true;
532}

Line 528 should be

index = endIndex + separatorLength; 

The code works only if the separator is a single char...

I cannot compile Haiku someone can verify thanks.

Change History (3)

comment:1 by korli, 7 years ago

Resolution: fixed
Status: newclosed

comment:2 by pulkomandy, 7 years ago

An unit test would be welcome, too :)

Note: See TracTickets for help on using tickets.