--- np2/common/milstr.c 2004/06/19 17:54:19 1.7 +++ np2/common/milstr.c 2005/02/07 14:46:08 1.13 @@ -1,27 +1,26 @@ #include "compiler.h" - -// ---- ANK +// ---- ANK / UCS2 / UCS4 #if defined(SUPPORT_ANK) -int milank_charsize(const char *str) { +int STRCALL milank_charsize(const OEMCHAR *str) { return((str[0] != '\0')?1:0); } -int milank_cmp(const char *str, const char *cmp) { +int STRCALL milank_cmp(const OEMCHAR *str, const OEMCHAR *cmp) { int s; int c; do { - s = (BYTE)*str++; - if (((s - 'a') & 0xff) < 26) { + s = *str++; + if ((s >= 'a') && (s <= 'z')) { s -= 0x20; } - c = (BYTE)*cmp++; - if (((c - 'a') & 0xff) < 26) { + c = *cmp++; + if ((c >= 'a') && (c <= 'z')) { c -= 0x20; } if (s != c) { @@ -31,28 +30,28 @@ int milank_cmp(const char *str, const ch return(0); } -int milank_memcmp(const char *str, const char *cmp) { +int STRCALL milank_memcmp(const OEMCHAR *str, const OEMCHAR *cmp) { int s; int c; do { - c = (BYTE)*cmp++; + c = *cmp++; if (c == 0) { return(0); } - if (((c - 'a') & 0xff) < 26) { + if ((c >= 'a') && (c <= 'z')) { c -= 0x20; } - s = (BYTE)*str++; - if (((s - 'a') & 0xff) < 26) { + s = *str++; + if ((s >= 'a') && (s <= 'z')) { s -= 0x20; } } while(s == c); return((s > c)?1:-1); } -void milank_ncpy(char *dst, const char *src, int maxlen) { +void STRCALL milank_ncpy(OEMCHAR *dst, const OEMCHAR *src, int maxlen) { int i; @@ -65,7 +64,7 @@ void milank_ncpy(char *dst, const char * } } -void milank_ncat(char *dst, const char *src, int maxlen) { +void STRCALL milank_ncat(OEMCHAR *dst, const OEMCHAR *src, int maxlen) { int i; int j; @@ -84,7 +83,7 @@ void milank_ncat(char *dst, const char * } } -char *milank_chr(const char *str, int c) { +OEMCHAR * STRCALL milank_chr(const OEMCHAR *str, int c) { int s; @@ -92,7 +91,7 @@ char *milank_chr(const char *str, int c) do { s = *str; if (s == c) { - return((char *)str); + return((OEMCHAR *)str); } str++; } while(s); @@ -105,7 +104,7 @@ char *milank_chr(const char *str, int c) // ---- Shift-JIS #if defined(SUPPORT_SJIS) -int milsjis_charsize(const char *str) { +int STRCALL milsjis_charsize(const char *str) { int pos; @@ -113,26 +112,26 @@ int milsjis_charsize(const char *str) { return((str[pos] != '\0')?(pos+1):0); } -int milsjis_cmp(const char *str, const char *cmp) { +int STRCALL milsjis_cmp(const char *str, const char *cmp) { int s; int c; do { - s = (BYTE)*str++; + s = (UINT8)*str++; if ((((s ^ 0x20) - 0xa1) & 0xff) < 0x3c) { - c = (BYTE)*cmp++; + c = (UINT8)*cmp++; if (s != c) { goto mscp_err; } - s = (BYTE)*str++; - c = (BYTE)*cmp++; + s = (UINT8)*str++; + c = (UINT8)*cmp++; } else { if (((s - 'a') & 0xff) < 26) { s -= 0x20; } - c = (BYTE)*cmp++; + c = (UINT8)*cmp++; if (((c - 'a') & 0xff) < 26) { c -= 0x20; } @@ -147,26 +146,26 @@ mscp_err: return((s > c)?1:-1); } -int milsjis_memcmp(const char *str, const char *cmp) { +int STRCALL milsjis_memcmp(const char *str, const char *cmp) { int s; int c; do { - c = (BYTE)*cmp++; + c = (UINT8)*cmp++; if ((((c ^ 0x20) - 0xa1) & 0xff) < 0x3c) { - s = (BYTE)*str++; + s = (UINT8)*str++; if (c != s) { break; } - c = (BYTE)*cmp++; - s = (BYTE)*str++; + c = (UINT8)*cmp++; + s = (UINT8)*str++; } else if (c) { if (((c - 'a') & 0xff) < 26) { c &= ~0x20; } - s = (BYTE)*str++; + s = (UINT8)*str++; if (((s - 'a') & 0xff) < 26) { s &= ~0x20; } @@ -178,7 +177,7 @@ int milsjis_memcmp(const char *str, cons return((s > c)?1:-1); } -int milsjis_kanji1st(const char *str, int pos) { +int STRCALL milsjis_kanji1st(const char *str, int pos) { int ret; @@ -190,17 +189,18 @@ int milsjis_kanji1st(const char *str, in return(ret); } -int milsjis_kanji2nd(const char *str, int pos) { +int STRCALL milsjis_kanji2nd(const char *str, int pos) { - int ret = 0; + int ret; + ret = 0; while((pos > 0) && ((((str[--pos] ^ 0x20) - 0xa1) & 0xff) < 0x3c)) { ret ^= 1; } return(ret); } -void milsjis_ncpy(char *dst, const char *src, int maxlen) { +void STRCALL milsjis_ncpy(char *dst, const char *src, int maxlen) { int i; @@ -218,7 +218,7 @@ void milsjis_ncpy(char *dst, const char } } -void milsjis_ncat(char *dst, const char *src, int maxlen) { +void STRCALL milsjis_ncat(char *dst, const char *src, int maxlen) { int i; int j; @@ -242,7 +242,7 @@ void milsjis_ncat(char *dst, const char } } -char *milsjis_chr(const char *str, int c) { +char * STRCALL milsjis_chr(const char *str, int c) { int s; @@ -267,34 +267,34 @@ char *milsjis_chr(const char *str, int c // ---- EUC #if defined(SUPPORT_EUC) // あれ 半角カナ忘れてるぞ? -int mileuc_charsize(const char *str) { +int STRCALL mileuc_charsize(const char *str) { int pos; - pos = (((str[0] - 0xa1) & 0xff) < 0x5d)?1:0; + pos = (str[0] & 0x80)?1:0; return((str[pos] != '\0')?(pos+1):0); } -int mileuc_cmp(const char *str, const char *cmp) { +int STRCALL mileuc_cmp(const char *str, const char *cmp) { int s; int c; do { - s = (BYTE)*str++; - if (((s - 0xa1) & 0xff) < 0x5d) { - c = (BYTE)*cmp++; + s = (UINT8)*str++; + if (s & 0x80) { + c = (UINT8)*cmp++; if (s != c) { goto mscp_err; } - s = (BYTE)*str++; - c = (BYTE)*cmp++; + s = (UINT8)*str++; + c = (UINT8)*cmp++; } else { if (((s - 'a') & 0xff) < 26) { s -= 0x20; } - c = (BYTE)*cmp++; + c = (UINT8)*cmp++; if (((c - 'a') & 0xff) < 26) { c -= 0x20; } @@ -309,26 +309,26 @@ mscp_err: return((s > c)?1:-1); } -int mileuc_memcmp(const char *str, const char *cmp) { +int STRCALL mileuc_memcmp(const char *str, const char *cmp) { int s; int c; do { - c = (BYTE)*cmp++; - if (((c - 0xa1) & 0xff) < 0x5d) { - s = (BYTE)*str++; + c = (UINT8)*cmp++; + if (c & 0x80) { + s = (UINT8)*str++; if (c != s) { break; } - c = (BYTE)*cmp++; - s = (BYTE)*str++; + c = (UINT8)*cmp++; + s = (UINT8)*str++; } else if (c) { if (((c - 'a') & 0xff) < 26) { c -= 0x20; } - s = (BYTE)*str++; + s = (UINT8)*str++; if (((s - 'a') & 0xff) < 26) { s -= 0x20; } @@ -340,29 +340,29 @@ int mileuc_memcmp(const char *str, const return((s > c)?1:-1); } -int mileuc_kanji1st(const char *str, int pos) { +int STRCALL mileuc_kanji1st(const char *str, int pos) { int ret; ret = 0; - while((pos >= 0) && - (((str[pos--] - 0xa1) & 0xff) < 0x5d)) { + while((pos >= 0) && (str[pos--] & 0x80)) { ret ^= 1; } return(ret); } -int mileuc_kanji2nd(const char *str, int pos) { +int STRCALL mileuc_kanji2nd(const char *str, int pos) { - int ret = 0; + int ret; - while((pos > 0) && (((str[--pos] - 0xa1) & 0xff) < 0x5d)) { + ret = 0; + while((pos > 0) && (str[--pos] & 0x80)) { ret ^= 1; } return(ret); } -void mileuc_ncpy(char *dst, const char *src, int maxlen) { +void STRCALL mileuc_ncpy(char *dst, const char *src, int maxlen) { int i; @@ -380,7 +380,7 @@ void mileuc_ncpy(char *dst, const char * } } -void mileuc_ncat(char *dst, const char *src, int maxlen) { +void STRCALL mileuc_ncat(char *dst, const char *src, int maxlen) { int i; int j; @@ -404,7 +404,7 @@ void mileuc_ncat(char *dst, const char * } } -char *mileuc_chr(const char *str, int c) { +char * STRCALL mileuc_chr(const char *str, int c) { int s; @@ -414,7 +414,7 @@ char *mileuc_chr(const char *str, int c) if (s == c) { return((char *)str); } - if (((s - 0xa1) & 0xff) < 0x5d) { + if (s & 0x80) { str++; s = *str; } @@ -426,16 +426,157 @@ char *mileuc_chr(const char *str, int c) #endif +// ---- UTF8 + +#if defined(SUPPORT_UTF8) +int STRCALL milutf8_charsize(const char *str) { + + if (str[0] == '\0') { + return(0); + } + else if (!(str[0] & 0x80)) { + return(1); + } + else if ((str[0] & 0xe0) == 0xc0) { + if ((str[1] & 0xc0) == 0x80) { + return(2); + } + } + else if ((str[0] & 0xf0) == 0xe0) { + if (((str[1] & 0xc0) == 0x80) || + ((str[2] & 0xc0) == 0x80)) { + return(3); + } + } + return(0); +} + +int STRCALL milutf8_cmp(const char *str, const char *cmp) { + + int s; + int c; + + do { + s = (UINT8)*str++; + if (((s - 'a') & 0xff) < 26) { + s -= 0x20; + } + c = (UINT8)*cmp++; + if (((c - 'a') & 0xff) < 26) { + c -= 0x20; + } + if (s != c) { + return((s > c)?1:-1); + } + } while(s); + return(0); +} + +int STRCALL milutf8_memcmp(const char *str, const char *cmp) { + + int s; + int c; + + do { + c = (UINT8)*cmp++; + if (c == 0) { + return(0); + } + if (((c - 'a') & 0xff) < 26) { + c -= 0x20; + } + s = (UINT8)*str++; + if (((s - 'a') & 0xff) < 26) { + s -= 0x20; + } + } while(s == c); + return((s > c)?1:-1); +} + +int STRCALL milutf8_kanji1st(const char *str, int pos) { + + return(((str[pos] & 0xc0) >= 0xc0)?1:0); +} + +int STRCALL milutf8_kanji2nd(const char *str, int pos) { + + return(((str[pos] & 0xc0) == 0x80)?1:0); +} + +void STRCALL milutf8_ncpy(char *dst, const char *src, int maxlen) { + + int i; + + if (maxlen > 0) { + maxlen--; + for (i=0; i 0) { + maxlen--; + for (i=0; i c)?1:-1); } -char *milstr_nextword(const char *str) { +OEMCHAR * STRCALL milstr_nextword(const OEMCHAR *str) { if (str) { - while(!(((*str) - 1) & 0xe0)) { + while((*str > '\0') && (*str <= ' ')) { str++; } } - return((char *)str); + return((OEMCHAR *)str); } -int milstr_getarg(char *str, char *arg[], int maxarg) { +int STRCALL milstr_getarg(OEMCHAR *str, OEMCHAR *arg[], int maxarg) { int ret = 0; - char *p; - BOOL quot; + OEMCHAR *p; + BRESULT quot; while(maxarg--) { quot = FALSE; - while((*str) && (((BYTE)*str) <= 0x20)) { + while((*str > '\0') && (*str <= ' ')) { str++; } if (*str == '\0') { @@ -491,10 +632,10 @@ int milstr_getarg(char *str, char *arg[] arg[ret++] = str; p = str; while(*str) { - if (*str == 0x22) { + if (*str == '\"') { quot = !quot; } - else if ((((BYTE)*str) > 0x20) || (quot)) { + else if ((quot) || (*str < '\0') || (*str > ' ')) { *p++ = *str; } else { @@ -508,11 +649,11 @@ int milstr_getarg(char *str, char *arg[] return(ret); } -long milstr_solveHEX(const char *str) { +long STRCALL milstr_solveHEX(const OEMCHAR *str) { long ret; int i; - char c; + OEMCHAR c; ret = 0; for (i=0; i<8; i++) { @@ -535,7 +676,7 @@ long milstr_solveHEX(const char *str) { return(ret); } -long milstr_solveINT(const char *str) { +long STRCALL milstr_solveINT(const OEMCHAR *str) { long ret; int c; @@ -564,7 +705,7 @@ long milstr_solveINT(const char *str) { return(ret * s); } -char *milstr_list(const char *lststr, UINT pos) { +OEMCHAR * STRCALL milstr_list(const OEMCHAR *lststr, UINT pos) { if (lststr) { while(pos) { @@ -573,6 +714,6 @@ char *milstr_list(const char *lststr, UI } } } - return((char *)lststr); + return((OEMCHAR *)lststr); }