Article ID: 129770
Article Last Modified on 11/21/2006
CString::TrimLeft() Before After ------------------- --A A-A --AB ABAB --ABC ABABC --ABCD ABCBCD --ABCDE ABCBCDE
memmove(m_pchData, lpsz, nDataLength+1);This line of code moves (nDataLength+1) bytes instead of characters.
memmove(m_pchData, lpsz, (nDataLength+1)*sizeof(TCHAR));Both solutions will work with both single-byte and double-byte character sets.
/* Compile options needed:
This change does not require special options.
*/
void TrimLeft(CString& str){
// Get the cstring length and buffer pointer:
int str_length = str.GetLength();
LPTSTR pstr_Data = str.GetBuffer(str_length);
// Find the first non white-space character:
LPCTSTR pstr_non_space = pstr_Data;
while (_istspace(*pstr_non_space))
pstr_non_space = _tcsinc(pstr_non_space);
// Find the new length in characters:
int nDataLength = str_length - (pstr_non_space - pstr_Data);
// Replace this line:
// memmove(m_pchData, lpsz, nDataLength + 1);
// WITH THE FOLLOWING TO MOVE CHARACTERS INSTEAD OF BYTES:
memmove(pstr_Data,pstr_non_space, (nDataLength+1)*sizeof(TCHAR));
// Release the buffer pointer, and adjust the cstring length:
str.ReleaseBuffer(nDataLength);
}
Additional query words: 2.10 3.10 3.1
Keywords: kbbug kbfix kbnoupdate kbvc220fix KB129770