Bug 132583: deal more cleanly with passing non-UTF8 strings to NS_ConvertUTF8toUCS2. r=scc, sr=darin

This commit is contained in:
jaggernaut%netscape.com 2002-05-14 08:27:57 +00:00
Родитель f35ac0b379
Коммит 0abdd7c9ad
4 изменённых файлов: 30 добавлений и 20 удалений

Просмотреть файл

@ -1330,22 +1330,20 @@ NS_ConvertUTF8toUCS2::Init( const nsACString& aCString )
PRUint32 count = calculator.Length();
if (count) {
// Grow the buffer if we need to.
SetCapacity(count);
// |SetCapacity| normally doesn't guarantee the use we are putting it to here (see its interface comment in nsAString.h),
// we can only use it since our local implementation, |nsString::SetCapacity|, is known to do what we want
SetLength(count);
// All ready? Time to convert
ConvertUTF8toUCS2 converter(mUStr);
copy_string(aCString.BeginReading(start), aCString.EndReading(end), converter);
mLength = converter.Length();
if (GetCapacity())
mUStr[mLength] = '\0'; // null terminate
if (mLength != count) {
NS_ERROR("Input wasn't UTF8 or incorrect length was calculated");
Truncate();
}
}
NS_ASSERTION(count == mLength, "calculator calculated incorrect length");
}
/**

Просмотреть файл

@ -583,12 +583,16 @@ class ConvertUTF8toUCS2
typedef nsACString::char_type value_type;
typedef nsAString::char_type buffer_type;
ConvertUTF8toUCS2( buffer_type* aBuffer ) : mStart(aBuffer), mBuffer(aBuffer) {}
ConvertUTF8toUCS2( buffer_type* aBuffer )
: mStart(aBuffer), mBuffer(aBuffer), mErrorEncountered(PR_FALSE) {}
size_t Length() const { return mBuffer - mStart; }
PRUint32 write( const value_type* start, PRUint32 N )
{
if ( mErrorEncountered )
return N;
// algorithm assumes utf8 units won't
// be spread across fragments
const value_type* p = start;
@ -640,7 +644,8 @@ class ConvertUTF8toUCS2
else
{
NS_ERROR("Not a UTF-8 string. This code should only be used for converting from known UTF-8 strings.");
break;
mErrorEncountered = PR_TRUE;
return N;
}
while ( state-- )
@ -655,7 +660,8 @@ class ConvertUTF8toUCS2
else
{
NS_ERROR("not a UTF8 string");
return p - start;
mErrorEncountered = PR_TRUE;
return N;
}
}
@ -694,6 +700,7 @@ class ConvertUTF8toUCS2
private:
buffer_type* mStart;
buffer_type* mBuffer;
PRBool mErrorEncountered;
};
#endif /* !defined(nsString2_h__) */

Просмотреть файл

@ -1330,22 +1330,20 @@ NS_ConvertUTF8toUCS2::Init( const nsACString& aCString )
PRUint32 count = calculator.Length();
if (count) {
// Grow the buffer if we need to.
SetCapacity(count);
// |SetCapacity| normally doesn't guarantee the use we are putting it to here (see its interface comment in nsAString.h),
// we can only use it since our local implementation, |nsString::SetCapacity|, is known to do what we want
SetLength(count);
// All ready? Time to convert
ConvertUTF8toUCS2 converter(mUStr);
copy_string(aCString.BeginReading(start), aCString.EndReading(end), converter);
mLength = converter.Length();
if (GetCapacity())
mUStr[mLength] = '\0'; // null terminate
if (mLength != count) {
NS_ERROR("Input wasn't UTF8 or incorrect length was calculated");
Truncate();
}
}
NS_ASSERTION(count == mLength, "calculator calculated incorrect length");
}
/**

Просмотреть файл

@ -583,12 +583,16 @@ class ConvertUTF8toUCS2
typedef nsACString::char_type value_type;
typedef nsAString::char_type buffer_type;
ConvertUTF8toUCS2( buffer_type* aBuffer ) : mStart(aBuffer), mBuffer(aBuffer) {}
ConvertUTF8toUCS2( buffer_type* aBuffer )
: mStart(aBuffer), mBuffer(aBuffer), mErrorEncountered(PR_FALSE) {}
size_t Length() const { return mBuffer - mStart; }
PRUint32 write( const value_type* start, PRUint32 N )
{
if ( mErrorEncountered )
return N;
// algorithm assumes utf8 units won't
// be spread across fragments
const value_type* p = start;
@ -640,7 +644,8 @@ class ConvertUTF8toUCS2
else
{
NS_ERROR("Not a UTF-8 string. This code should only be used for converting from known UTF-8 strings.");
break;
mErrorEncountered = PR_TRUE;
return N;
}
while ( state-- )
@ -655,7 +660,8 @@ class ConvertUTF8toUCS2
else
{
NS_ERROR("not a UTF8 string");
return p - start;
mErrorEncountered = PR_TRUE;
return N;
}
}
@ -694,6 +700,7 @@ class ConvertUTF8toUCS2
private:
buffer_type* mStart;
buffer_type* mBuffer;
PRBool mErrorEncountered;
};
#endif /* !defined(nsString2_h__) */