optimize
This commit is contained in:
+35
-14
@@ -273,28 +273,42 @@ CString join( const CString &Delimitor, CStringArray::const_iterator begin, CStr
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
template <class S>
|
template <class S>
|
||||||
void do_split( const S &Source, const S &Delimitor, vector<S> &AddIt, const bool bIgnoreEmpty )
|
static int DelimitorLength( const S &Delimitor )
|
||||||
|
{
|
||||||
|
return Delimitor.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
static int DelimitorLength( char Delimitor )
|
||||||
|
{
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class S, class C>
|
||||||
|
void do_split( const S &Source, const C Delimitor, vector<S> &AddIt, const bool bIgnoreEmpty )
|
||||||
{
|
{
|
||||||
size_t startpos = 0;
|
size_t startpos = 0;
|
||||||
|
|
||||||
do {
|
do {
|
||||||
size_t pos;
|
size_t pos;
|
||||||
if( Delimitor.size() == 1 )
|
pos = Source.find( Delimitor, startpos );
|
||||||
pos = Source.find( Delimitor[0], startpos );
|
|
||||||
else
|
|
||||||
pos = Source.find( Delimitor, startpos );
|
|
||||||
if( pos == Source.npos )
|
if( pos == Source.npos )
|
||||||
pos = Source.size();
|
pos = Source.size();
|
||||||
|
|
||||||
if( pos-startpos > 0 || !bIgnoreEmpty )
|
if( pos-startpos > 0 || !bIgnoreEmpty )
|
||||||
{
|
{
|
||||||
const S AddCString = Source.substr(startpos, pos-startpos);
|
/* Optimization: if we're copying the whole string, avoid substr; this
|
||||||
AddIt.push_back(AddCString);
|
* allows this copy to be refcounted, which is much faster. */
|
||||||
|
if( startpos == 0 && pos-startpos == Source.size() )
|
||||||
|
AddIt.push_back(Source);
|
||||||
|
else
|
||||||
|
{
|
||||||
|
const S AddCString = Source.substr(startpos, pos-startpos);
|
||||||
|
AddIt.push_back(AddCString);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
startpos = pos+Delimitor.size();
|
startpos = pos+DelimitorLength(Delimitor);
|
||||||
} while ( startpos <= Source.size() );
|
} while ( startpos <= Source.size() );
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -988,15 +1002,22 @@ const wchar_t INVALID_CHAR = 0xFFFD; /* U+FFFD REPLACEMENT CHARACTER */
|
|||||||
wstring CStringToWstring( const CString &s )
|
wstring CStringToWstring( const CString &s )
|
||||||
{
|
{
|
||||||
wstring ret;
|
wstring ret;
|
||||||
|
ret.reserve( s.size() );
|
||||||
for( unsigned start = 0; start < s.size(); )
|
for( unsigned start = 0; start < s.size(); )
|
||||||
{
|
{
|
||||||
wchar_t ch;
|
char c = s[start];
|
||||||
if( !utf8_to_wchar_ec( s, start, ch ) )
|
if( !(c&0x80) )
|
||||||
{
|
{
|
||||||
ret += INVALID_CHAR;
|
/* ASCII fast path */
|
||||||
|
ret += c;
|
||||||
|
++start;
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
else
|
|
||||||
ret += ch;
|
wchar_t ch;
|
||||||
|
if( !utf8_to_wchar( s, start, ch ) )
|
||||||
|
ch = INVALID_CHAR;
|
||||||
|
ret += ch;
|
||||||
}
|
}
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
|
|||||||
Reference in New Issue
Block a user