check in code drop from Xu, Yueheng <yueheng.xu@intel.com>

This commit is contained in:
ftang%netscape.com 1999-09-22 03:35:31 +00:00
parent 580ddeefd0
commit 49217a4b1e
12 changed files with 289 additions and 174 deletions

View File

@ -21,48 +21,18 @@
#include "nsGB2312ToUnicodeV2.h"
#include "nsUCvCnDll.h"
#include "gbu.h"
#define _GBKU_TABLE_ // to use a shared GBKU table
#include "gbku.h"
//----------------------------------------------------------------------
// Global functions and data [declaration]
static PRInt16 g_ASCIIShiftTable[] = {
0, u1ByteCharset,
ShiftCell(0,0,0,0,0,0,0,0)
};
static PRInt16 g_GB2312ShiftTable[] = {
0, u2BytesGRCharset,
ShiftCell(0,0,0,0,0,0,0,0)
};
static PRInt16 *g_GB2312ShiftTableSet [] = {
g_ASCIIShiftTable,
g_GB2312ShiftTable
};
static PRUint16 *g_GB2312MappingTableSet [] ={
g_AsciiMapping,
g_utGB2312Mapping
};
static uRange g_GB2312Ranges[] = {
{ 0x00, 0x7E },
{ 0xA1, 0xFE }
};
//----------------------------------------------------------------------
// Class nsGB2312ToUnicodeV2 [implementation]
nsGB2312ToUnicodeV2::nsGB2312ToUnicodeV2()
: nsMultiTableDecoderSupport(2,
(uRange *) &g_GB2312Ranges,
(uShiftTable**) &g_GB2312ShiftTableSet,
(uMappingTable**) &g_GB2312MappingTableSet)
{
}
nsresult nsGB2312ToUnicodeV2::CreateInstance(nsISupports ** aResult)
{
*aResult = new nsGB2312ToUnicodeV2();
@ -76,32 +46,31 @@ NS_IMETHODIMP nsGB2312ToUnicodeV2::GetMaxLength(const char * aSrc,
PRInt32 aSrcLength,
PRInt32 * aDestLength)
{
// we are a single byte to Unicode converter, so...
*aDestLength = aSrcLength;
return NS_OK_UDEC_EXACTLENGTH;
return NS_OK;
}
//Overwriting the ConvertNoBuff() in nsUCvCnSupport.cpp.
//side effects: all the helper functions called by UCvCnSupport are deprecated
void GBToUnicode(DByte *pGBCode, PRUnichar * pUnicode)
void nsGB2312ToUnicodeV2::GBKToUnicode(DByte *pGBCode, PRUnichar * pUnicode)
{
short int iGBToUnicodeIndex;
short int iGBKToUnicodeIndex;
if(pGBCode)
iGBToUnicodeIndex = ( (short int)(pGBCode->leftbyte & 0x7f) - 0x21)*(0x7e - 0x20 )+( (short int)(pGBCode->rightbyte &0x7f ) - 0x21);
iGBKToUnicodeIndex = ( (short int)(pGBCode->leftbyte) - 0x81)*0xbf +( (short int)(pGBCode->rightbyte) - 0x40);
if( (iGBToUnicodeIndex >= 0 ) && ( iGBToUnicodeIndex < MAX_GB_LENGTH) )
*pUnicode = GBToUnicodeTable[iGBToUnicodeIndex];
if( (iGBKToUnicodeIndex >= 0 ) && ( iGBKToUnicodeIndex < MAX_GBK_LENGTH) )
*pUnicode = GBKToUnicodeTable[iGBKToUnicodeIndex];
// e.g. 0xb0a1 --> Index = 0 --> left = 0x00, right = 0x30, value = 0x3000
}
NS_IMETHODIMP nsGB2312ToUnicodeV2::ConvertNoBuff(const char* aSrc,
PRInt32 * aSrcLength,
PRUnichar *aDest,
@ -128,7 +97,7 @@ NS_IMETHODIMP nsGB2312ToUnicodeV2::ConvertNoBuff(const char* aSrc,
if ( *aSrc & 0x80 )
{
// The source is a GBCode
GBToUnicode(pSrcDBCode, pDestDBCode);
GBKToUnicode(pSrcDBCode, pDestDBCode);
aSrc += 2;
i++;
}

View File

@ -31,14 +31,15 @@
* @created 06/Apr/1999
* @author Catalin Rotaru [CATA]
*/
class nsGB2312ToUnicodeV2 : public nsMultiTableDecoderSupport
class nsGB2312ToUnicodeV2 : public nsBufferDecoderSupport
{
public:
/**
* Class constructor.
*/
nsGB2312ToUnicodeV2();
nsGB2312ToUnicodeV2(){};
virtual ~nsGB2312ToUnicodeV2(){};
/**
* Static class constructor.
@ -58,6 +59,18 @@ protected:
NS_IMETHOD GetMaxLength(const char * aSrc, PRInt32 aSrcLength,
PRInt32 * aDestLength);
};
private:
typedef struct
{
char leftbyte;
char rightbyte;
} DByte;
void GBKToUnicode(DByte *pGBCode, PRUnichar * pUnicode);
};
#endif /* nsGB2312ToUnicodeV2_h___ */

View File

@ -28,45 +28,9 @@
#include "gbku.h"
//----------------------------------------------------------------------
// Global functions and data [declaration]
static PRInt16 g_ASCIIShiftTable[] = {
0, u1ByteCharset,
ShiftCell(0,0,0,0,0,0,0,0)
};
static PRInt16 g_GB2312ShiftTable[] = {
0, u2BytesGRCharset,
ShiftCell(0,0,0,0,0,0,0,0)
};
static PRInt16 *g_GB2312ShiftTableSet [] = {
g_ASCIIShiftTable,
g_GB2312ShiftTable
};
static PRUint16 *g_GB2312MappingTableSet [] ={
g_AsciiMapping,
g_utGB2312Mapping
};
static uRange g_GB2312Ranges[] = {
{ 0x00, 0x7E },
{ 0xA1, 0xFE }
};
//----------------------------------------------------------------------
// Class nsGB2312ToUnicode [implementation]
nsGBKToUnicode::nsGBKToUnicode()
: nsMultiTableDecoderSupport(2,
(uRange *) &g_GB2312Ranges,
(uShiftTable**) &g_GB2312ShiftTableSet,
(uMappingTable**) &g_GB2312MappingTableSet)
{
}
nsresult nsGBKToUnicode::CreateInstance(nsISupports ** aResult)
{
@ -81,9 +45,8 @@ NS_IMETHODIMP nsGBKToUnicode::GetMaxLength(const char * aSrc,
PRInt32 aSrcLength,
PRInt32 * aDestLength)
{
// we are a single byte to Unicode converter, so...
*aDestLength = aSrcLength;
return NS_OK_UDEC_EXACTLENGTH;
return NS_OK;
}
@ -91,7 +54,7 @@ NS_IMETHODIMP nsGBKToUnicode::GetMaxLength(const char * aSrc,
//Overwriting the ConvertNoBuff() in nsUCvCnSupport.cpp.
//side effects: all the helper functions called by UCvCnSupport are deprecated
void GBKToUnicode(DByte *pGBCode, PRUnichar * pUnicode)
void nsGBKToUnicode::GBKToUnicode(DByte *pGBCode, PRUnichar * pUnicode)
{
short int iGBKToUnicodeIndex;

View File

@ -30,15 +30,15 @@
* @created 07/Sept/1999
* @author Yueheng Xu, Yueheng.Xu@intel.com
*/
class nsGBKToUnicode : public nsMultiTableDecoderSupport
class nsGBKToUnicode : public nsBufferDecoderSupport
{
public:
/**
* Class constructor.
*/
nsGBKToUnicode();
nsGBKToUnicode(){};
virtual ~nsGBKToUnicode(){};
/**
* Static class constructor.
*/
@ -57,7 +57,18 @@ protected:
NS_IMETHOD GetMaxLength(const char * aSrc, PRInt32 aSrcLength,
PRInt32 * aDestLength);
};
private:
typedef struct
{
char leftbyte;
char rightbyte;
} DByte;
void GBKToUnicode(DByte *pGBCode, PRUnichar * pUnicode);
};
#endif /* nsGBKToUnicode_h___ */

View File

@ -67,9 +67,8 @@ NS_IMETHODIMP nsHZToUnicode::GetMaxLength(const char * aSrc,
PRInt32 aSrcLength,
PRInt32 * aDestLength)
{
// we are a single byte to Unicode converter, so...
*aDestLength = aSrcLength;
return NS_OK_UDEC_EXACTLENGTH;
return NS_OK;
}

View File

@ -38,8 +38,8 @@ public:
/**
* Class constructor.
*/
nsHZToUnicode() {};
virtual ~nsHZToUnicode() {};
nsHZToUnicode(){};
virtual ~nsHZToUnicode(){};
/**
* Static class constructor.

View File

@ -21,58 +21,32 @@
#include "nsUCvCnDll.h"
#define _GBU_TABLE_ // make the table in the include file to be extern
#include "gbu.h"
#define _GBKU_TABLE_ // make the table in the include file to be extern
#include "gbku.h"
//----------------------------------------------------------------------
// Global functions and data [declaration]
static PRInt16 g_ASCIIShiftTable[] = {
0, u1ByteCharset,
ShiftCell(0,0,0,0,0,0,0,0)
};
static PRInt16 g_GB2312ShiftTable[] = {
0, u2BytesGRCharset,
ShiftCell(0,0,0,0,0,0,0,0)
};
static PRInt16 *g_GB2312ShiftTableSet [] = {
g_ASCIIShiftTable,
g_GB2312ShiftTable
};
static PRUint16 *g_GB2312MappingTableSet [] ={
g_AsciiMapping,
g_ufGB2312Mapping
};
//----------------------------------------------------------------------
// Class nsUnicodeToGB2312V2 [implementation]
nsUnicodeToGB2312V2::nsUnicodeToGB2312V2()
: nsMultiTableEncoderSupport(2,
(uShiftTable**) &g_GB2312ShiftTableSet,
(uMappingTable**) &g_GB2312MappingTableSet)
{
}
#define TRUE 1
#define FALSE 0
void UnicodeToGB(PRUnichar SrcUnicode, DByte *pGBCode)
void nsUnicodeToGB2312V2::UnicodeToGBK(PRUnichar SrcUnicode, DByte *pGBCode)
{
short int iRet = FALSE;
short int i = 0;
short int iGBToUnicodeIndex = 0;
short int iGBKToUnicodeIndex = 0;
for ( i=0; i<MAX_GB_LENGTH; i++)
for ( i=0; i<MAX_GBK_LENGTH; i++)
{
if ( SrcUnicode == GBToUnicodeTable[i] )
if ( SrcUnicode == GBKToUnicodeTable[i] )
{
iGBToUnicodeIndex = i;
iGBKToUnicodeIndex = i;
iRet = TRUE;
break;
}
@ -83,9 +57,9 @@ void UnicodeToGB(PRUnichar SrcUnicode, DByte *pGBCode)
//convert from one dimensional index to (left, right) pair
if(pGBCode)
{
pGBCode->leftbyte = (char) ( iGBToUnicodeIndex / (0x007e - 0x0020) + 0x0021) ;
pGBCode->leftbyte = (char) ( iGBKToUnicodeIndex / 0x00BF + 0x0081) ;
pGBCode->leftbyte |= 0x80;
pGBCode->rightbyte = (char) ( iGBToUnicodeIndex % (0x007e - 0x0020)+ 0x0021);
pGBCode->rightbyte = (char) ( iGBKToUnicodeIndex % 0x00BF+ 0x0040);
pGBCode->rightbyte |= 0x80;
}
}
@ -94,6 +68,7 @@ void UnicodeToGB(PRUnichar SrcUnicode, DByte *pGBCode)
}
NS_IMETHODIMP nsUnicodeToGB2312V2::ConvertNoBuff(const PRUnichar * aSrc,
PRInt32 * aSrcLength,
char * aDest,
@ -117,7 +92,7 @@ NS_IMETHODIMP nsUnicodeToGB2312V2::ConvertNoBuff(const PRUnichar * aSrc,
{
// hi byte has something, it is not ASCII, must be a GB
UnicodeToGB( *pSrc, pDestDBCode);
UnicodeToGBK( *pSrc, pDestDBCode);
aDest += 2; // increment 2 bytes
pDestDBCode = (DByte *)aDest;
iDestLength +=2;
@ -165,3 +140,51 @@ NS_IMETHODIMP nsUnicodeToGB2312V2::GetMaxLength(const PRUnichar * aSrc,
*aDestLength = 2 * aSrcLength;
return NS_OK;
}
#define SET_REPRESENTABLE(info, c) (info)[(c) >> 5] |= (1L << ((c) & 0x1f))
NS_IMETHODIMP nsUnicodeToGB2312V2::FillInfo(PRUint32 *aInfo)
{
short int i,j;
PRUnichar SrcUnicode;
PRUint16 k;
for ( k=0; k++;k<65536)
{
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;
}

View File

@ -31,14 +31,15 @@
* @created 06/Apr/1999
* @author Catalin Rotaru [CATA]
*/
class nsUnicodeToGB2312V2 : public nsMultiTableEncoderSupport
class nsUnicodeToGB2312V2 : public nsEncoderSupport
{
public:
/**
* Class constructor.
*/
nsUnicodeToGB2312V2();
nsUnicodeToGB2312V2(){};
virtual ~nsUnicodeToGB2312V2(){};
/**
* Static class constructor.
@ -57,6 +58,26 @@ protected:
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;
void UnicodeToGBK(PRUnichar SrcUnicode, DByte *pGBCode);
};
#endif /* nsUnicodeToGB2312V2_h___ */

View File

@ -34,40 +34,14 @@
// Global functions and data [declaration]
static PRInt16 g_ASCIIShiftTable[] = {
0, u1ByteCharset,
ShiftCell(0,0,0,0,0,0,0,0)
};
static PRInt16 g_GB2312ShiftTable[] = {
0, u2BytesGRCharset,
ShiftCell(0,0,0,0,0,0,0,0)
};
static PRInt16 *g_GB2312ShiftTableSet [] = {
g_ASCIIShiftTable,
g_GB2312ShiftTable
};
static PRUint16 *g_GB2312MappingTableSet [] ={
g_AsciiMapping,
g_ufGB2312Mapping
};
//----------------------------------------------------------------------
// Class nsUnicodeToGBK [implementation]
nsUnicodeToGBK::nsUnicodeToGBK()
: nsMultiTableEncoderSupport(2,
(uShiftTable**) &g_GB2312ShiftTableSet,
(uMappingTable**) &g_GB2312MappingTableSet)
{
}
#define TRUE 1
#define FALSE 0
void UnicodeToGBK(PRUnichar SrcUnicode, DByte *pGBCode)
void nsUnicodeToGBK::UnicodeToGBK(PRUnichar SrcUnicode, DByte *pGBCode)
{
short int iRet = FALSE;
short int i = 0;
@ -172,3 +146,52 @@ NS_IMETHODIMP nsUnicodeToGBK::GetMaxLength(const PRUnichar * aSrc,
return NS_OK;
}
#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;
for ( k=0; k++;k<65536)
{
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;
}

View File

@ -32,14 +32,15 @@
//----------------------------------------------------------------------
// Class nsUnicodeToGBK [declaration]
class nsUnicodeToGBK: public nsMultiTableEncoderSupport
class nsUnicodeToGBK: public nsEncoderSupport
{
public:
/**
* Class constructor.
*/
nsUnicodeToGBK();
nsUnicodeToGBK(){};
virtual ~nsUnicodeToGBK(){};
/**
* Static class constructor.
@ -58,6 +59,27 @@ protected:
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;
void UnicodeToGBK(PRUnichar SrcUnicode, DByte *pGBCode);
};
#endif /* nsUnicodeToGBK_h___ */

View File

@ -37,6 +37,42 @@
// Class nsUnicodeToGBK [implementation]
#define TRUE 1
#define FALSE 0
void nsUnicodeToHZ::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;
}
}
}
#define TRUE 1
#define FALSE 0
@ -168,9 +204,51 @@ NS_IMETHODIMP nsUnicodeToHZ::ConvertNoBuff(const PRUnichar * aSrc,
return NS_OK;
}
#define SET_REPRESENTABLE(info, c) (info)[(c) >> 5] |= (1L << ((c) & 0x1f))
NS_IMETHODIMP nsUnicodeToHZ::FillInfo(PRUint32 *aInfo)
{
short int i,j;
PRUnichar SrcUnicode;
PRUint16 k;
for ( k=0; k++;k<65536)
{
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;
}
@ -194,12 +272,3 @@ NS_IMETHODIMP nsUnicodeToHZ::GetMaxLength(const PRUnichar * aSrc,
*aDestLength = 6 * aSrcLength;
return NS_OK;
}
NS_IMETHODIMP nsUnicodeToHZ::ConvertNoBuffNoErr(
const PRUnichar * aSrc,
PRInt32 * aSrcLength,
char * aDest,
PRInt32 * aDestLength)
{
return NS_OK;
}

View File

@ -57,13 +57,14 @@ protected:
char * aDest,
PRInt32 * aDestLength);
NS_IMETHOD ConvertNoBuffNoErr( const PRUnichar * aSrc,
PRInt32 * aSrcLength,
char * aDest,
PRInt32 * aDestLength);
NS_IMETHOD FillInfo(PRUint32 *aInfo);
NS_IMETHOD ConvertNoBuffNoErr(const PRUnichar * aSrc, PRInt32 * aSrcLength,
char * aDest, PRInt32 * aDestLength)
{
return NS_OK;
}; // just make it not abstract;
NS_IMETHOD GetMaxLength(const PRUnichar * aSrc, PRInt32 aSrcLength,
PRInt32 * aDestLength);
@ -76,6 +77,7 @@ typedef struct
} DByte;
void UnicodeToHZ(PRUnichar SrcUnicode, DByte *pGBCode);
void UnicodeToGBK(PRUnichar SrcUnicode, DByte *pGBCode);
};