📄 wchar.c
字号:
/* * conversion functions between pg_wchar and multibyte streams. * Tatsuo Ishii * $PostgreSQL: pgsql/src/backend/utils/mb/wchar.c,v 1.47.2.2 2006/05/21 20:05:48 tgl Exp $ * * WIN1250 client encoding updated by Pavel Behal * *//* can be used in either frontend or backend */#ifdef FRONTEND#include "postgres_fe.h"#define Assert(condition)#else#include "postgres.h"#endif#include "mb/pg_wchar.h"/* * conversion to pg_wchar is done by "table driven." * to add an encoding support, define mb2wchar_with_len(), mblen() * for the particular encoding. Note that if the encoding is only * supported in the client, you don't need to define * mb2wchar_with_len() function (SJIS is the case). *//* * SQL/ASCII */static int pg_ascii2wchar_with_len (const unsigned char *from, pg_wchar *to, int len){ int cnt = 0; while (len > 0 && *from) { *to++ = *from++; len--; cnt++; } *to = 0; return (cnt);}static intpg_ascii_mblen(const unsigned char *s){ return (1);}static intpg_ascii_dsplen(const unsigned char *s){ return (1);}/* * EUC */static int pg_euc2wchar_with_len (const unsigned char *from, pg_wchar *to, int len){ int cnt = 0; while (len > 0 && *from) { if (*from == SS2 && len >= 2) /* JIS X 0201 (so called "1 byte KANA") */ { from++; *to = (SS2 << 8) | *from++; len -= 2; } else if (*from == SS3 && len >= 3) /* JIS X 0212 KANJI */ { from++; *to = (SS3 << 16) | (*from++ << 8); *to |= *from++; len -= 3; } else if ((*from & 0x80) && len >= 2) /* JIS X 0208 KANJI */ { *to = *from++ << 8; *to |= *from++; len -= 2; } else /* must be ASCII */ { *to = *from++; len--; } to++; cnt++; } *to = 0; return (cnt);}static inline intpg_euc_mblen(const unsigned char *s){ int len; if (*s == SS2) len = 2; else if (*s == SS3) len = 3; else if (*s & 0x80) len = 2; else len = 1; return (len);}static inline intpg_euc_dsplen(const unsigned char *s){ int len; if (*s == SS2) len = 2; else if (*s == SS3) len = 2; else if (*s & 0x80) len = 2; else len = 1; return (len);}/* * EUC_JP */static int pg_eucjp2wchar_with_len (const unsigned char *from, pg_wchar *to, int len){ return (pg_euc2wchar_with_len(from, to, len));}static intpg_eucjp_mblen(const unsigned char *s){ return (pg_euc_mblen(s));}static intpg_eucjp_dsplen(const unsigned char *s){ int len; if (*s == SS2) len = 1; else if (*s == SS3) len = 2; else if (*s & 0x80) len = 2; else len = 1; return (len);}/* * EUC_KR */static int pg_euckr2wchar_with_len (const unsigned char *from, pg_wchar *to, int len){ return (pg_euc2wchar_with_len(from, to, len));}static intpg_euckr_mblen(const unsigned char *s){ return (pg_euc_mblen(s));}static intpg_euckr_dsplen(const unsigned char *s){ return (pg_euc_dsplen(s));}/* * EUC_CN * */static int pg_euccn2wchar_with_len (const unsigned char *from, pg_wchar *to, int len){ int cnt = 0; while (len > 0 && *from) { if (*from == SS2 && len >= 3) /* code set 2 (unused?) */ { from++; *to = (SS2 << 16) | (*from++ << 8); *to |= *from++; len -= 3; } else if (*from == SS3 && len >= 3) /* code set 3 (unsed ?) */ { from++; *to = (SS3 << 16) | (*from++ << 8); *to |= *from++; len -= 3; } else if ((*from & 0x80) && len >= 2) /* code set 1 */ { *to = *from++ << 8; *to |= *from++; len -= 2; } else { *to = *from++; len--; } to++; cnt++; } *to = 0; return (cnt);}static intpg_euccn_mblen(const unsigned char *s){ int len; if (*s & 0x80) len = 2; else len = 1; return (len);}static intpg_euccn_dsplen(const unsigned char *s){ int len; if (*s & 0x80) len = 2; else len = 1; return (len);}/* * EUC_TW * */static int pg_euctw2wchar_with_len (const unsigned char *from, pg_wchar *to, int len){ int cnt = 0; while (len > 0 && *from) { if (*from == SS2 && len >= 4) /* code set 2 */ { from++; *to = (SS2 << 24) | (*from++ << 16) ; *to |= *from++ << 8; *to |= *from++; len -= 4; } else if (*from == SS3 && len >= 3) /* code set 3 (unused?) */ { from++; *to = (SS3 << 16) | (*from++ << 8); *to |= *from++; len -= 3; } else if ((*from & 0x80) && len >= 2) /* code set 2 */ { *to = *from++ << 8; *to |= *from++; len -= 2; } else { *to = *from++; len--; } to++; cnt++; } *to = 0; return (cnt);}static intpg_euctw_mblen(const unsigned char *s){ int len; if (*s == SS2) len = 4; else if (*s == SS3) len = 3; else if (*s & 0x80) len = 2; else len = 1; return (len);}static intpg_euctw_dsplen(const unsigned char *s){ int len; if (*s == SS2) len = 2; else if (*s == SS3) len = 2; else if (*s & 0x80) len = 2; else len = 1; return (len);}/* * JOHAB */static intpg_johab2wchar_with_len(const unsigned char *from, pg_wchar *to, int len){ return (pg_euc2wchar_with_len(from, to, len));}static intpg_johab_mblen(const unsigned char *s){ return (pg_euc_mblen(s));}static intpg_johab_dsplen(const unsigned char *s){ return (pg_euc_dsplen(s));}/* * convert UTF8 string to pg_wchar (UCS-2) * caller should allocate enough space for "to" * len: length of from. * "from" not necessarily null terminated. */static intpg_utf2wchar_with_len(const unsigned char *from, pg_wchar *to, int len){ unsigned char c1, c2, c3; int cnt = 0; while (len > 0 && *from) { if ((*from & 0x80) == 0) { *to = *from++; len--; } else if ((*from & 0xe0) == 0xc0 && len >= 2) { c1 = *from++ & 0x1f; c2 = *from++ & 0x3f; *to = c1 << 6; *to |= c2; len -= 2; } else if ((*from & 0xe0) == 0xe0 && len >= 3) { c1 = *from++ & 0x0f; c2 = *from++ & 0x3f; c3 = *from++ & 0x3f; *to = c1 << 12; *to |= c2 << 6; *to |= c3; len -= 3; } else { *to = *from++; len--; } to++; cnt++; } *to = 0; return (cnt);}/* * returns the byte length of a UTF8 character pointed to by s */intpg_utf_mblen(const unsigned char *s){ int len = 1; if ((*s & 0x80) == 0) len = 1; else if ((*s & 0xe0) == 0xc0) len = 2; else if ((*s & 0xf0) == 0xe0) len = 3; else if ((*s & 0xf8) == 0xf0) len = 4; else if ((*s & 0xfc) == 0xf8) len = 5; else if ((*s & 0xfe) == 0xfc) len = 6; return (len);}static intpg_utf_dsplen(const unsigned char *s){ return 1; /* XXX fix me! */}/* * convert mule internal code to pg_wchar * caller should allocate enough space for "to" * len: length of from. * "from" not necessarily null terminated. */static intpg_mule2wchar_with_len(const unsigned char *from, pg_wchar *to, int len){ int cnt = 0; while (len > 0 && *from) { if (IS_LC1(*from) && len >= 2) { *to = *from++ << 16; *to |= *from++; len -= 2; } else if (IS_LCPRV1(*from) && len >= 3) { from++; *to = *from++ << 16; *to |= *from++; len -= 3; } else if (IS_LC2(*from) && len >= 3) { *to = *from++ << 16; *to |= *from++ << 8; *to |= *from++; len -= 3; } else if (IS_LCPRV2(*from) && len >= 4) { from++; *to = *from++ << 16; *to |= *from++ << 8; *to |= *from++; len -= 4; } else { /* assume ASCII */ *to = (unsigned char) *from++; len--; } to++; cnt++; } *to = 0; return (cnt);}intpg_mule_mblen(const unsigned char *s){ int len; if (IS_LC1(*s)) len = 2; else if (IS_LCPRV1(*s)) len = 3; else if (IS_LC2(*s)) len = 3; else if (IS_LCPRV2(*s)) len = 4; else { /* assume ASCII */ len = 1; } return (len);}static intpg_mule_dsplen(const unsigned char *s){ return 1; /* XXX fix me! */}/* * ISO8859-1 */static intpg_latin12wchar_with_len(const unsigned char *from, pg_wchar *to, int len){ int cnt = 0; while (len > 0 && *from) { *to++ = *from++; len--; cnt++; } *to = 0; return (cnt);}static intpg_latin1_mblen(const unsigned char *s){ return (1);}static intpg_latin1_dsplen(const unsigned char *s){ return (1);}/* * SJIS */static intpg_sjis_mblen(const unsigned char *s){ int len; if (*s >= 0xa1 && *s <= 0xdf) { /* 1 byte kana? */ len = 1; } else if (*s > 0x7f) { /* kanji? */ len = 2; } else { /* should be ASCII */ len = 1; } return (len);}static intpg_sjis_dsplen(const unsigned char *s){ int len; if (*s >= 0xa1 && *s <= 0xdf) { /* 1 byte kana? */ len = 1; } else if (*s > 0x7f) { /* kanji? */ len = 2; } else { /* should be ASCII */ len = 1; } return (len);}/* * Big5 */static intpg_big5_mblen(const unsigned char *s){ int len; if (*s > 0x7f) { /* kanji? */ len = 2; } else { /* should be ASCII */ len = 1; } return (len);}static intpg_big5_dsplen(const unsigned char *s){ int len; if (*s > 0x7f) { /* kanji? */ len = 2; } else { /* should be ASCII */ len = 1; } return (len);}/* * GBK */static intpg_gbk_mblen(const unsigned char *s){ int len; if (*s > 0x7f) { /* kanji? */ len = 2; } else { /* should be ASCII */ len = 1; } return (len);}static intpg_gbk_dsplen(const unsigned char *s){ int len; if (*s > 0x7f) { /* kanji? */ len = 2; } else { /* should be ASCII */ len = 1; } return (len);}/* * UHC */static intpg_uhc_mblen(const unsigned char *s){ int len; if (*s > 0x7f) { /* 2byte? */ len = 2; } else { /* should be ASCII */ len = 1; } return (len);}static intpg_uhc_dsplen(const unsigned char *s){ int len; if (*s > 0x7f) { /* 2byte? */ len = 2; } else { /* should be ASCII */ len = 1; } return (len);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -