Unicode encoder related change for unmapped characters.

If unicode encoder cannot map a character to the target encoding, it returns used unicode length including the unmapped character.
That is true for all encoder except ISO-2022-JP (bug #15706). Changed the callers to match the encoder behavior. bug 8865, r=cata.
This commit is contained in:
nhotta%netscape.com 1999-10-26 20:16:11 +00:00
parent 1486d36542
commit 35df054c4f
3 changed files with 16 additions and 20 deletions

View File

@ -183,7 +183,7 @@ nsSaveAsCharset::DoCharsetConversion(const PRUnichar *inString, char **outString
dstLength = bufferLength - pos2; dstLength = bufferLength - pos2;
rv = mEncoder->Convert(&inString[pos1], &srcLength, &dstPtr[pos2], &dstLength); rv = mEncoder->Convert(&inString[pos1], &srcLength, &dstPtr[pos2], &dstLength);
pos1 += srcLength; pos1 += srcLength ? srcLength : 1;
pos2 += dstLength; pos2 += dstLength;
dstPtr[pos2] = '\0'; dstPtr[pos2] = '\0';
@ -200,7 +200,7 @@ nsSaveAsCharset::DoCharsetConversion(const PRUnichar *inString, char **outString
// do the fallback // do the fallback
if (!ATTR_NO_FALLBACK(mAttribute)) { if (!ATTR_NO_FALLBACK(mAttribute)) {
PRUnichar unMappedChar = inString[pos1]; PRUnichar unMappedChar = inString[pos1-1];
rv = mEncoder->GetMaxLength(inString+pos1, inStringLength-pos1, &dstLength); rv = mEncoder->GetMaxLength(inString+pos1, inStringLength-pos1, &dstLength);
if (NS_FAILED(rv)) if (NS_FAILED(rv))
@ -211,7 +211,6 @@ nsSaveAsCharset::DoCharsetConversion(const PRUnichar *inString, char **outString
break; break;
dstPtr[pos2] = '\0'; dstPtr[pos2] = '\0';
} }
pos1++; // for the unmapped char
} }
if (NS_FAILED(rv)) { if (NS_FAILED(rv)) {

View File

@ -90,13 +90,13 @@ nsresult ConvertFromUnicode(const nsString& aCharset,
if (0 >= unicharLength || NS_ERROR_UENC_NOMAPPING != res) { if (0 >= unicharLength || NS_ERROR_UENC_NOMAPPING != res) {
break; break;
} }
// could not map unicode to the destination charset, skip one unichar and continue // could not map unicode to the destination charset
// increment for source unicode, skip one unichar // increment for source unicode and continue
unichars += unicharLength + 1; unichars += unicharLength;
oldUnicharLength -= (unicharLength + 1); oldUnicharLength -= unicharLength; // adjust availabe buffer size
unicharLength = oldUnicharLength; unicharLength = oldUnicharLength;
// estimate target length again // reset the encoder
(void) encoder->GetMaxLength(unichars, unicharLength, &dstLength); encoder->Reset();
} }
(*outCString)[totalCLength] = '\0'; (*outCString)[totalCLength] = '\0';
} }

View File

@ -1483,7 +1483,6 @@ PRInt32 MimeCharsetConverterClass::Convert(const char* inBuffer, const PRInt32 i
else { else {
// convert from unicode // convert from unicode
PRUnichar *currentUStringPtr = unichars; PRUnichar *currentUStringPtr = unichars;
PRInt32 oldUnicharLength = unicharLength;
PRInt32 currentUnicharLength = unicharLength; PRInt32 currentUnicharLength = unicharLength;
char *currentCStringPtr = dstPtr; char *currentCStringPtr = dstPtr;
PRInt32 totalCLength = 0; PRInt32 totalCLength = 0;
@ -1499,19 +1498,17 @@ PRInt32 MimeCharsetConverterClass::Convert(const char* inBuffer, const PRInt32 i
if (0 >= currentUnicharLength || NS_ERROR_UENC_NOMAPPING != res) { if (0 >= currentUnicharLength || NS_ERROR_UENC_NOMAPPING != res) {
break; break;
} }
// could not map unicode to the destination charset, skip one unichar and continue // could not map unicode to the destination charset
// increment for source unicode, skip one unichar // increment for source unicode and continue
if (NULL != numUnConverted) { if (NULL != numUnConverted) {
(*numUnConverted)++; (*numUnConverted)++;
} }
currentUStringPtr += currentUnicharLength + 1; currentUStringPtr += currentUnicharLength;
oldUnicharLength -= (currentUnicharLength + 1); unicharLength -= currentUnicharLength; // adjust availabe buffer size
currentUnicharLength = oldUnicharLength; currentUnicharLength = unicharLength;
// estimate target length again
(void) encoder->GetMaxLength(currentUStringPtr, currentUnicharLength, &dstLength); // reset the encoder
if (dstLength > unicharLength) { encoder->Reset();
dstLength = unicharLength; // not to exceed allocated buffer length
}
} }
dstPtr[totalCLength] = '\0'; dstPtr[totalCLength] = '\0';
*outBuffer = dstPtr; // set the result string *outBuffer = dstPtr; // set the result string