Correct trim() in XString.

This commit is contained in:
jief666 2020-08-16 12:33:41 +03:00
parent 11817925ae
commit de45197556
2 changed files with 23 additions and 5 deletions

View File

@ -935,13 +935,20 @@ public:
void trim()
{
size_t lengthInNativeBytes = __String<T, ThisXStringClass>::sizeInNativeChars();
if ( lengthInNativeBytes == 0 ) return;
T* start = 0;
size_t count = 0;
T* s = m_data;
while ( *s && unsigned_type(T)(*s) <= 32 ) s++;
if ( !*s ) {
m_data[0] = 0;
return;
}
start = s;
while ( *s && unsigned_type(T)(*s) > 32 ) s++;
count = uintptr_t(s - start);
s = m_data + lengthInNativeBytes - 1;
while ( *s && unsigned_type(T)(*s) <= 32 ) s--;
count = uintptr_t(s - start) + 1;
CheckSize(count); // We have to CheckSize in case this string point to a litteral.
memmove(m_data, start, count*sizeof(T));
m_data[count] = 0;

View File

@ -1577,9 +1577,20 @@ int XString_tests()
XStringW xsw2;
xsw2.takeValueFrom(xsw, 1);
XString8 xs8 = " toTRIM "_XS8;
// xs8.trim();
xs8.lowerAscii();
{
XString8 xs8 = " to TRIM "_XS8;
xs8.trim();
if ( xs8 != "to TRIM"_XS8 ) {
nbTestFailed += 1;
}
}
{
XString8 xs8 = "Apple Inc."_XS8;
xs8.trim();
if ( xs8 != "Apple Inc."_XS8 ) {
nbTestFailed += 1;
}
}
XString8 xsReplace = "babcbdeb"_XS8;
xsReplace.replaceAll(U'b', U'𐅃');