зеркало из https://github.com/mozilla/pjs.git
Optimized decoder.
This commit is contained in:
Родитель
b0f90dfd14
Коммит
1877e4880c
|
@ -25,12 +25,10 @@
|
|||
|
||||
#include "nsGBKToUnicode.h"
|
||||
#include "nsUCvCnDll.h"
|
||||
|
||||
#include "gbku.h"
|
||||
|
||||
//----------------------------------------------------------------------
|
||||
// Class nsGB2312ToUnicode [implementation]
|
||||
|
||||
// Class nsGBKToUnicode [implementation]
|
||||
|
||||
nsresult nsGBKToUnicode::CreateInstance(nsISupports ** aResult)
|
||||
{
|
||||
|
@ -49,70 +47,58 @@ NS_IMETHODIMP nsGBKToUnicode::GetMaxLength(const char * aSrc,
|
|||
return NS_OK;
|
||||
}
|
||||
|
||||
|
||||
|
||||
//Overwriting the ConvertNoBuff() in nsUCvCnSupport.cpp.
|
||||
//side effects: all the helper functions called by UCvCnSupport are deprecated
|
||||
|
||||
void nsGBKToUnicode::GBKToUnicode(DByte *pGBCode, PRUnichar * pUnicode)
|
||||
{
|
||||
short int iGBKToUnicodeIndex;
|
||||
|
||||
if(pGBCode)
|
||||
iGBKToUnicodeIndex = ( (short int)(pGBCode->leftbyte) - 0x81)*0xbf +( (short int)(pGBCode->rightbyte) - 0x40);
|
||||
|
||||
if( (iGBKToUnicodeIndex >= 0 ) && ( iGBKToUnicodeIndex < MAX_GBK_LENGTH) )
|
||||
*pUnicode = GBKToUnicodeTable[iGBKToUnicodeIndex];
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
NS_IMETHODIMP nsGBKToUnicode::ConvertNoBuff(const char* aSrc,
|
||||
PRInt32 * aSrcLength,
|
||||
PRUnichar *aDest,
|
||||
PRInt32 * aDestLength)
|
||||
PRInt32 * aSrcLength,
|
||||
PRUnichar *aDest,
|
||||
PRInt32 * aDestLength)
|
||||
{
|
||||
|
||||
short int i=0;
|
||||
short int iSrcLength = (short int)(*aSrcLength);
|
||||
DByte *pSrcDBCode = (DByte *)aSrc;
|
||||
PRUnichar *pDestDBCode = (PRUnichar *)aDest;
|
||||
int iDestlen = 0;
|
||||
|
||||
|
||||
for (i=0;i<iSrcLength;i++)
|
||||
PRInt32 i=0;
|
||||
PRInt32 iSrcLength = (*aSrcLength);
|
||||
DByte *pSrcDBCode = (DByte *)aSrc;
|
||||
PRUnichar *pDestDBCode = (PRUnichar *)aDest;
|
||||
PRInt32 iDestlen = 0;
|
||||
PRUint8 left, right;
|
||||
PRUint16 iGBKToUnicodeIndex = 0;
|
||||
|
||||
for (i=0;i<iSrcLength;i++)
|
||||
{
|
||||
pSrcDBCode = (DByte *)aSrc;
|
||||
pDestDBCode = aDest;
|
||||
pSrcDBCode = (DByte *)aSrc;
|
||||
pDestDBCode = aDest;
|
||||
|
||||
if ( iDestlen >= (*aDestLength) )
|
||||
if ( iDestlen >= (*aDestLength) )
|
||||
{
|
||||
break;
|
||||
break;
|
||||
}
|
||||
|
||||
if ( *aSrc & 0x80 )
|
||||
{
|
||||
|
||||
// The source is a GBCode
|
||||
|
||||
if ( *aSrc & 0x80 )
|
||||
{
|
||||
// The source is a GBCode
|
||||
GBKToUnicode(pSrcDBCode, pDestDBCode);
|
||||
aSrc += 2;
|
||||
i++;
|
||||
left = pSrcDBCode->leftbyte;
|
||||
right = pSrcDBCode->rightbyte;
|
||||
|
||||
iGBKToUnicodeIndex = (left - 0x0081)*0x00BF + (right - 0x0040);
|
||||
*pDestDBCode = GBKToUnicodeTable[iGBKToUnicodeIndex];
|
||||
|
||||
aSrc += 2;
|
||||
i++;
|
||||
}
|
||||
else
|
||||
else
|
||||
{
|
||||
// The source is an ASCII
|
||||
*pDestDBCode = (PRUnichar) ( ((char)(*aSrc) )& 0x00ff);
|
||||
aSrc++;
|
||||
// The source is an ASCII
|
||||
*pDestDBCode = (PRUnichar) ( ((char )(*aSrc)) & 0x00ff);
|
||||
aSrc++;
|
||||
}
|
||||
|
||||
iDestlen++;
|
||||
aDest++;
|
||||
*aSrcLength = i+1;
|
||||
|
||||
iDestlen++;
|
||||
aDest++;
|
||||
*aSrcLength = i+1;
|
||||
}
|
||||
|
||||
*aDestLength = iDestlen;
|
||||
|
||||
return NS_OK;
|
||||
|
||||
*aDestLength = iDestlen;
|
||||
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -66,7 +66,6 @@ private:
|
|||
char rightbyte;
|
||||
|
||||
} DByte;
|
||||
void GBKToUnicode(DByte *pGBCode, PRUnichar * pUnicode);
|
||||
|
||||
};
|
||||
|
||||
|
|
|
@ -21,56 +21,47 @@
|
|||
*
|
||||
* @created 08/Sept/1999
|
||||
* @author Yueheng Xu, Yueheng.Xu@intel.com
|
||||
* Revision History
|
||||
* 04/Oct/1999. Yueheng Xu: used table UnicodeToGBKTable[0x5200] to make
|
||||
* Unicode to GB mapping fast
|
||||
*/
|
||||
|
||||
#include "nsUnicodeToGBK.h"
|
||||
#include "nsUCvCnDll.h"
|
||||
|
||||
|
||||
#define _UNICODE_TO_GBK_ENCODER_ // this is the place we allocate memory for UnicodeToGBKTable[0xFFFF]
|
||||
#define _GBKU_TABLE_ // to use a shared GBKU table
|
||||
#include "gbku.h"
|
||||
|
||||
//----------------------------------------------------------------------
|
||||
// Global functions and data [declaration]
|
||||
|
||||
|
||||
//----------------------------------------------------------------------
|
||||
// Class nsUnicodeToGBK [implementation]
|
||||
|
||||
|
||||
#define TRUE 1
|
||||
#define FALSE 0
|
||||
|
||||
void nsUnicodeToGBK::UnicodeToGBK(PRUnichar SrcUnicode, DByte *pGBCode)
|
||||
{
|
||||
short int iRet = FALSE;
|
||||
short int i = 0;
|
||||
short int iGBKToUnicodeIndex = 0;
|
||||
|
||||
|
||||
for ( i=0; i<MAX_GBK_LENGTH; i++)
|
||||
{
|
||||
if ( SrcUnicode == GBKToUnicodeTable[i] )
|
||||
{
|
||||
iGBKToUnicodeIndex = i;
|
||||
iRet = TRUE;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if ( iRet )
|
||||
{
|
||||
//convert from one dimensional index to (left, right) pair
|
||||
if(pGBCode)
|
||||
{
|
||||
pGBCode->leftbyte = (char) ( iGBKToUnicodeIndex / 0x00BF + 0x0081) ;
|
||||
pGBCode->leftbyte |= 0x80;
|
||||
pGBCode->rightbyte = (char) ( iGBKToUnicodeIndex % 0x00BF+ 0x0040);
|
||||
pGBCode->rightbyte |= 0x80;
|
||||
}
|
||||
}
|
||||
nsUnicodeToGBK::nsUnicodeToGBK()
|
||||
{
|
||||
PRUint8 left, right;
|
||||
PRUnichar unicode;
|
||||
PRUnichar 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -79,49 +70,77 @@ NS_IMETHODIMP nsUnicodeToGBK::ConvertNoBuff(const PRUnichar * aSrc,
|
|||
char * aDest,
|
||||
PRInt32 * aDestLength)
|
||||
{
|
||||
|
||||
PRInt32 i=0;
|
||||
PRInt32 iSrcLength = *aSrcLength;
|
||||
DByte *pDestDBCode;
|
||||
DByte *pSrcDBCode;
|
||||
PRInt32 iDestLength = 0;
|
||||
PRUnichar unicode;
|
||||
PRUint8 left, right;
|
||||
|
||||
PRUnichar *pSrc = (PRUnichar *)aSrc;
|
||||
|
||||
pDestDBCode = (DByte *)aDest;
|
||||
|
||||
for (i=0;i< iSrcLength;i++)
|
||||
{
|
||||
pDestDBCode = (DByte *)aDest;
|
||||
|
||||
if( (*pSrc) & 0xff00 )
|
||||
{
|
||||
// hi byte has something, it is not ASCII, must be a GB
|
||||
|
||||
UnicodeToGBK( *pSrc, pDestDBCode);
|
||||
aDest += 2; // increment 2 bytes
|
||||
pDestDBCode = (DByte *)aDest;
|
||||
iDestLength +=2;
|
||||
}
|
||||
else
|
||||
{
|
||||
// this is an ASCII
|
||||
pSrcDBCode = (DByte *)pSrc;
|
||||
*aDest = pSrcDBCode->leftbyte;
|
||||
aDest++; // increment 1 byte
|
||||
iDestLength +=1;
|
||||
}
|
||||
pSrc++; // increment 2 bytes
|
||||
|
||||
if ( iDestLength >= (*aDestLength) )
|
||||
{
|
||||
break;
|
||||
}
|
||||
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 ;
|
||||
pDestDBCode->rightbyte = UnicodeToGBKTable[unicode].rightbyte ;
|
||||
}
|
||||
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 = (PRUint8) ( i / 0x00BF + 0x0081) | 0x80 ;
|
||||
right = (PRUint8) ( i % 0x00BF+ 0x0040) | 0x80;
|
||||
pDestDBCode->leftbyte = left;
|
||||
pDestDBCode->rightbyte = right;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// UnicodeToGBK( *pSrc, pDestDBCode);
|
||||
aDest += 2; // increment 2 bytes
|
||||
pDestDBCode = (DByte *)aDest;
|
||||
iDestLength +=2;
|
||||
}
|
||||
else
|
||||
{
|
||||
// this is an ASCII
|
||||
pSrcDBCode = (DByte *)pSrc;
|
||||
*aDest = pSrcDBCode->leftbyte;
|
||||
aDest++; // increment 1 byte
|
||||
iDestLength +=1;
|
||||
}
|
||||
pSrc++; // increment 2 bytes
|
||||
|
||||
if ( iDestLength >= (*aDestLength) )
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
*aDestLength = iDestLength;
|
||||
*aSrcLength = i;
|
||||
|
||||
|
||||
return NS_OK;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
@ -147,51 +166,40 @@ NS_IMETHODIMP nsUnicodeToGBK::GetMaxLength(const PRUnichar * aSrc,
|
|||
}
|
||||
|
||||
|
||||
|
||||
|
||||
#define SET_REPRESENTABLE(info, c) (info)[(c) >> 5] |= (1L << ((c) & 0x1f))
|
||||
|
||||
NS_IMETHODIMP nsUnicodeToGBK::FillInfo(PRUint32 *aInfo)
|
||||
{
|
||||
|
||||
short int i,j;
|
||||
PRUnichar SrcUnicode;
|
||||
PRUint16 k;
|
||||
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" );
|
||||
|
||||
for ( k=0; k++;k<65536)
|
||||
// valid GBK rows are in 0x81 to 0xFE
|
||||
for ( i=0x0081; i<=0x00FF; i++)
|
||||
{
|
||||
aInfo[k] = 0x0000;
|
||||
}
|
||||
|
||||
// valid GBK rows are in 0x81 to 0xFE
|
||||
for ( i=0x81;i++;i<0xFE)
|
||||
{
|
||||
// HZ and GB2312 starts at row 0x21|0x80
|
||||
if ( i < ( 0x21 | 0x80))
|
||||
continue;
|
||||
|
||||
// valid GBK columns are in 0x41 to 0xFE
|
||||
for( j=0x41; j++; j<0xFE)
|
||||
{
|
||||
//HZ and GB2312 starts at col 0x21 | 0x80
|
||||
if ( j < (0x21 | 0x80))
|
||||
continue;
|
||||
|
||||
// k is index in GBKU.H table
|
||||
k = (i - 0x81)*(0xFE - 0x80)+(j-0x41);
|
||||
|
||||
// sanity check
|
||||
if ( (k>=0) && ( k < MAX_GBK_LENGTH))
|
||||
{
|
||||
SrcUnicode = GBKToUnicodeTable[i];
|
||||
if (( SrcUnicode != 0xFFFF ) && (SrcUnicode != 0xFFFD) )
|
||||
{
|
||||
SET_REPRESENTABLE(aInfo, SrcUnicode);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return NS_OK;
|
||||
for( j=0x0041; j<0x00FF; j++)
|
||||
{
|
||||
// k is index in GBKU.H table
|
||||
k = (i - 0x0081)*(0xFE - 0x0080)+(j-0x0041);
|
||||
SrcUnicode = GBKToUnicodeTable[k];
|
||||
if (( SrcUnicode != 0xFFFF ) && (SrcUnicode != 0xFFFD) )
|
||||
{
|
||||
SET_REPRESENTABLE(aInfo, SrcUnicode);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -39,7 +39,7 @@ public:
|
|||
/**
|
||||
* Class constructor.
|
||||
*/
|
||||
nsUnicodeToGBK(){};
|
||||
nsUnicodeToGBK();
|
||||
virtual ~nsUnicodeToGBK(){};
|
||||
|
||||
/**
|
||||
|
@ -77,9 +77,6 @@ protected:
|
|||
|
||||
} DByte;
|
||||
|
||||
void UnicodeToGBK(PRUnichar SrcUnicode, DByte *pGBCode);
|
||||
|
||||
|
||||
};
|
||||
|
||||
#endif /* nsUnicodeToGBK_h___ */
|
||||
|
|
Загрузка…
Ссылка в новой задаче