I think there was a missunderstanding of the problem, sorry for my bad explanation.
With the new code from trunk I used the following test code:
Code: Select all
#include <irrlicht.h>
#include <iostream>
using namespace irr;
using namespace core;
int main(int argc, char *argv[])
{
array<string<c8> > split1;
array<string<c8> > split2;
u32 splitsize1;
u32 splitsize2;
string<c8> str1 ("1;2;3;4;5");
string<c8> str2 ("1;2;3;4;");
splitsize1=str1.split(split1,";",1,false,false);
splitsize2=str2.split(split2,";",1,false,false);
std::cout <<"Array split1 contains "<<splitsize1<< " Elements\n";
for (s32 i=0; i<splitsize1;i++)
{
std::cout <<"Index "<< i << ": Value: "<< split1[i].c_str()<<"\n";
}
std::cout <<"Array split2 contains "<<splitsize2<< " Elements\n";
for (s32 i=0; i<splitsize2;i++)
{
std::cout <<"Index "<< i << ": Value: "<< split2[i].c_str()<<"\n";
}
}
with the followingoutput result:
Array split1 contains 5 Elements
Index 0: Value: 1
Index 1: Value: 2
Index 2: Value: 3
Index 3: Value: 4
Index 4: Value: 5
Array split2 contains 4 Elements
Index 0: Value: 1
Index 1: Value: 2
Index 2: Value: 3
Index 3: Value: 4
The first string splits correctly, but in the second string I get only 4 substrings, the last one, that is empty, is missing.
The reason for this is, that the case of ignoreEmptyTokens set to false is not considered for the last pushback outside of the loop.
I think the code should be as follows:
Code: Select all
u32 split(container& ret, const T* const delimiter, u32 countDelimiters=1, bool ignoreEmptyTokens=true, bool keepSeparators=false) const
{
if (!delimiter)
return 0;
const u32 oldSize=ret.size();
u32 tokenStartIdx = 0;
for (u32 i=0; i<used; ++i)
{
for (u32 j=0; j<countDelimiters; ++j)
{
if (array[i] == delimiter[j])
{
if ( keepSeparators )
{
ret.push_back(string<T,TAlloc>(&array[tokenStartIdx], i+1 - tokenStartIdx));
}
else
{
if (i - tokenStartIdx > 0)
ret.push_back(string<T,TAlloc>(&array[tokenStartIdx], i - tokenStartIdx));
else if ( !ignoreEmptyTokens )
ret.push_back(string<T,TAlloc>());
}
tokenStartIdx = i+1;
break;
}
}
}
if ((used - 1) > tokenStartIdx)
ret.push_back(string<T,TAlloc>(&array[tokenStartIdx], (used - 1) - tokenStartIdx));
// change:
else if ( !ignoreEmptyTokens )
ret.push_back(string<T,TAlloc>());
// end change
return ret.size()-oldSize;
}
Now I get the following output:
Array split1 contains 5 Elements
Index 0: Value: 1
Index 1: Value: 2
Index 2: Value: 3
Index 3: Value: 4
Index 4: Value: 5
Array split2 contains 5 Elements
Index 0: Value: 1
Index 1: Value: 2
Index 2: Value: 3
Index 3: Value: 4
Index 4: Value:
Now the second string is correctly split into 5 substrings, where the last one is empty.