Use the new implementation for GB2312GL
This commit is contained in:
ftang%netscape.com 2000-05-17 05:19:04 +00:00
Родитель 8d35eb3da5
Коммит abb5c1d985
2 изменённых файлов: 177 добавлений и 20 удалений

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

@ -18,35 +18,129 @@
* Rights Reserved.
*
* Contributor(s):
* Yueheng Xu, yueheng.xu@intel.com
*/
#include "nsUnicodeToGB2312GL.h"
#include "nsUCvCnDll.h"
//----------------------------------------------------------------------
// Global functions and data [declaration]
static PRInt16 g_2BytesShiftTable[] = {
0, u2BytesCharset,
ShiftCell(0,0,0,0,0,0,0,0)
};
#define _GBKU_TABLE_ // use a shared table
#include "gbku.h"
//----------------------------------------------------------------------
// Class nsUnicodeToGB2312GL [implementation]
nsUnicodeToGB2312GL::nsUnicodeToGB2312GL()
: nsTableEncoderSupport(
(uShiftTable*) &g_2BytesShiftTable,
(uMappingTable*) &g_ufGB2312Mapping)
#define TRUE 1
#define FALSE 0
nsUnicodeToGB2312GL::nsUnicodeToGB2312GL()
{
PRUint8 left, right;
PRUnichar unicode;
PRUint16 i;
for ( i=0; i<MAX_GBK_LENGTH; i++ )
{
left = ( i / 0x00BF + 0x0081);
right = ( i % 0x00BF+ 0x0040);
unicode = GBKToUnicodeTable[i];
// to reduce size of UnicodeToGBKTable, we only do direct unicode to GB
// table mapping between unicode 0x4E00 and 0xA000. Others by searching
// GBKToUnicodeTable. There is a trade off between memory usage and speed.
if ( (unicode >= 0x4E00 ) && ( unicode <= 0xA000 ))
{
unicode -= 0x4E00;
UnicodeToGBKTable[unicode].leftbyte = left;
UnicodeToGBKTable[unicode].rightbyte = right;
}
}
}
NS_IMETHODIMP nsUnicodeToGB2312GL::ConvertNoBuff(const PRUnichar * aSrc,
PRInt32 * aSrcLength,
char * aDest,
PRInt32 * aDestLength)
{
PRInt32 i=0;
PRInt32 iSrcLength = 0;
DByte *pDestDBCode;
PRInt32 iDestLength = 0;
PRUnichar unicode;
PRUint8 left, right;
nsresult res = NS_OK;
PRUnichar *pSrc = (PRUnichar *)aSrc;
pDestDBCode = (DByte *)aDest;
while( iSrcLength < *aSrcLength )
{
pDestDBCode = (DByte *)aDest;
unicode = *pSrc;
//if unicode's hi byte has something, it is not ASCII, must be a GB
if( unicode & 0xff00 )
{
// To reduce the UnicodeToGBKTable size, we only use direct table mapping
// for unicode between 0x4E00 - 0xA000
if ( (unicode >= 0x4E00 ) && ( unicode <= 0xA000 ) )
{
unicode -= 0x4E00;
pDestDBCode->leftbyte = UnicodeToGBKTable[unicode].leftbyte & 0x7f;
pDestDBCode->rightbyte = UnicodeToGBKTable[unicode].rightbyte & 0x7f;
}
else
{
// other ones we search in GBK to Unicode table
for ( i=0; i<MAX_GBK_LENGTH; i++)
{
if ( unicode == GBKToUnicodeTable[i] )
{
//this manipulation handles the little endian / big endian issues
left = (char) ( i / 0x00BF + 0x0081) & 0x7f ;
right = (char) ( i % 0x00BF+ 0x0040) & 0x7f;
pDestDBCode->leftbyte = left;
pDestDBCode->rightbyte = right;
}
}
}
// UnicodeToGBK( *pSrc, pDestDBCode);
aDest += 2; // increment 2 bytes
pDestDBCode = (DByte *)aDest;
iDestLength +=2;
iSrcLength +=2;
pSrc++; // increment 2 bytes
}
else
{ // This is ASCII, not expected, report error.
res = NS_ERROR_UENC_NOMAPPING;
break;
}
// if dest buffer not big enough, handles it here.
if ( (iDestLength >= *aDestLength) && (iSrcLength > 0) )
{
res = NS_OK_UENC_MOREOUTPUT;
break;
}
}
*aDestLength = iDestLength;
*aSrcLength = iSrcLength;
return res;
}
nsresult nsUnicodeToGB2312GL::CreateInstance(nsISupports ** aResult)
{
nsIUnicodeEncoder *p = new nsUnicodeToGB2312GL();
if(p) {
*aResult = p;
return NS_OK;
*aResult = p;
return NS_OK;
}
return NS_ERROR_OUT_OF_MEMORY;
}
@ -59,5 +153,45 @@ NS_IMETHODIMP nsUnicodeToGB2312GL::GetMaxLength(const PRUnichar * aSrc,
PRInt32 * aDestLength)
{
*aDestLength = 2 * aSrcLength;
return NS_OK_UENC_EXACTLENGTH;
return NS_OK;
}
#define SET_REPRESENTABLE(info, c) (info)[(c) >> 5] |= (1L << ((c) & 0x1f))
NS_IMETHODIMP nsUnicodeToGB2312GL::FillInfo(PRUint32 *aInfo)
{
PRUint16 i,j, k;
PRUnichar SrcUnicode;
// aInfo should already been initialized as 2048 element array of 32 bit by caller
NS_ASSERTION( sizeof(aInfo[0]) == 4, "aInfo size incorrect" );
// valid GBK rows are in 0x81 to 0xFE
for ( i=0x0081;i<0x00FF;i++)
{
// HZ and GB2312 starts at row 0x21|0x80 = 0xA1
if ( i < 0xA1 )
continue;
// valid GBK columns are in 0x41 to 0xFE
for( j=0x0041;j<0x00FF;j++)
{
//HZ and GB2312 starts at col 0x21 | 0x80 = 0xA1
if ( j < 0xA1 )
continue;
// k is index in GBKU.H table
k = (i - 0x0081)*(0x00FE - 0x0080)+(j-0x0041);
SrcUnicode = GBKToUnicodeTable[k];
if (( SrcUnicode != 0xFFFF ) && (SrcUnicode != 0xFFFD) )
{
SET_REPRESENTABLE(aInfo, SrcUnicode);
}
}
}
return NS_OK;
}

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

@ -18,6 +18,7 @@
* Rights Reserved.
*
* Contributor(s):
* Yueheng Xu, yueheng.xu@intel.com
*/
#ifndef nsUnicodeToGB2312GL_h___
@ -33,28 +34,50 @@
*
* @created 06/Apr/1999
* @author Catalin Rotaru [CATA]
* @Modified to use the newer tables in file gbku.h.
* Yueheng.xu@intel.com. 3/May/2000
*/
class nsUnicodeToGB2312GL : public nsTableEncoderSupport
class nsUnicodeToGB2312GL : public nsEncoderSupport
{
public:
/**
* Class constructor.
*/
nsUnicodeToGB2312GL();
virtual ~nsUnicodeToGB2312GL(){};
/**
* Static class constructor.
*/
static nsresult CreateInstance(nsISupports **aResult);
protected:
NS_IMETHOD ConvertNoBuff(const PRUnichar * aSrc,
PRInt32 * aSrcLength,
char * aDest,
PRInt32 * aDestLength);
//--------------------------------------------------------------------
// Subclassing of nsEncoderSupport class [declaration]
NS_IMETHOD GetMaxLength(const PRUnichar * aSrc, PRInt32 aSrcLength,
PRInt32 * aDestLength);
NS_IMETHOD ConvertNoBuffNoErr(const PRUnichar * aSrc, PRInt32 * aSrcLength,
char * aDest, PRInt32 * aDestLength)
{
return NS_OK;
}; // just make it not abstract;
NS_IMETHOD FillInfo(PRUint32 *aInfo);
private:
typedef struct
{
char leftbyte;
char rightbyte;
} DByte;
};
#endif /* nsUnicodeToGB2312GL_h___ */