diff --git a/rEFIt_UEFI/cpp_foundation/XStringAbstract.h b/rEFIt_UEFI/cpp_foundation/XStringAbstract.h index 0db41fe11..cd4692e69 100755 --- a/rEFIt_UEFI/cpp_foundation/XStringAbstract.h +++ b/rEFIt_UEFI/cpp_foundation/XStringAbstract.h @@ -935,13 +935,20 @@ public: void trim() { + size_t lengthInNativeBytes = __String::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; diff --git a/rEFIt_UEFI/cpp_unit_test/XString_test.cpp b/rEFIt_UEFI/cpp_unit_test/XString_test.cpp index e652cea21..d15d7c20e 100755 --- a/rEFIt_UEFI/cpp_unit_test/XString_test.cpp +++ b/rEFIt_UEFI/cpp_unit_test/XString_test.cpp @@ -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'𐅃');