📄 cjkctype.cpp
字号:
// cjkctype.cpp
#include <windows.h>
#include <string.h>
#include <ctype.h>
#include <mbctype.h>
#include <limits.h>
#include <errno.h>
#include "msc.h"
#include "codepage.h"
#undef DEBUG_CONST_STRING
#include "cjkctype.h"
// for safety
#undef ismbblead
#undef ismbbtrail
////////////////////////////////////////////
// strlcpy () //
////////////////////////////////////////////
size_t mbslcpy_cp ( char *dst, const char *src, size_t bufsiz, int codepage ) {
size_t srclen = strlen ( src ) ;
if ( bufsiz ) {
size_t cpylen = srclen ;
if ( cpylen >= bufsiz ) cpylen = bufsiz - 1 ;
memmove ( dst, src, cpylen * sizeof(char) ) ;
mbstrunc_cp ( dst, cpylen, codepage ) ;
}
return srclen ;
}
////////////////////////////////////////////
// strlcat () //
////////////////////////////////////////////
size_t mbslcat_cp ( char *dst, const char *src, size_t bufsiz, int codepage ) {
size_t srclen = strlen ( src ) ;
size_t dstlen = strlen ( dst ) ;
if ( bufsiz > dstlen ) {
size_t cpylen = srclen ;
if ( cpylen >= bufsiz - dstlen ) cpylen = bufsiz - dstlen - 1 ;
memmove ( dst + dstlen, src, cpylen * sizeof(char) ) ;
mbstrunc_cp ( dst, dstlen + cpylen, codepage ) ;
}
return srclen + dstlen ;
}
////////////////////////////////////////////
// stricmp () //
////////////////////////////////////////////
int mbsicmp_cp ( const char *string1, const char *string2, int codepage ) {
return mbsnicmp_cp ( string1, string2, SIZE_MAX, codepage ) ;
}
////////////////////////////////////////////
// strnicmp () //
////////////////////////////////////////////
int mbsnicmp_cp ( const char *string1, const char *string2, size_t count, int codepage ) {
int c1, c2 ;
int isprevlead1 = 0, isprevlead2 = 0 ;
while ( 1 ) {
if ( ! count ) return 0 ;
c1 = (unsigned char) *string1 ;
if ( isprevlead1 ) isprevlead1 = 0 ;
else {
if ( ismbblead_cp ( c1, codepage ) ) isprevlead1 = 1 ;
else c1 = tolower ( c1 ) ;
}
c2 = (unsigned char) *string2 ;
if ( isprevlead2 ) isprevlead2 = 0 ;
else {
if ( ismbblead_cp ( c2, codepage ) ) isprevlead2 = 1 ;
else c2 = tolower ( c2 ) ;
}
if ( ! c1 || c1 != c2 ) break ;
string1 ++ ;
string2 ++ ;
count -- ;
}
return c1 - c2 ;
}
////////////////////////////////////////////
// strrcmp () //
////////////////////////////////////////////
// 暥帤楍傪枛旜偐傜斾妑乮MULTIBYTE斉乯
int mbsrcmp_cp ( const char *string1, const char *string2, int codepage ) {
return mbsrncmp_cp ( string1, string2, SIZE_MAX, codepage ) ;
}
////////////////////////////////////////////
// strricmp () //
////////////////////////////////////////////
// 暥帤楍傪枛旜偐傜 ignore case 偱斾妑乮MULTIBYTE斉乯
int mbsricmp_cp ( const char *string1, const char *string2, int codepage ) {
return mbsrnicmp_cp ( string1, string2, SIZE_MAX, codepage ) ;
}
////////////////////////////////////////////
// strrncmp () //
////////////////////////////////////////////
// 暥帤楍傪枛旜偐傜斾妑乮MULTIBYTE斉乯
int mbsrncmp_cp ( const char *string1, const char *string2, size_t count, int codepage ) {
int c1, c2 ;
const char *current1 = strend ( string1 ) - 1 ;
const char *current2 = strend ( string2 ) - 1 ;
while ( 1 ) {
if ( ! count ) return 0 ;
if ( current1 >= string1 ) {
c1 = (unsigned char) *current1 ;
if ( ismbstrail_cp ( string1, current1, codepage ) ) c1 += 0x100 ;
}
else {
c1 = 0 ;
}
if ( current2 >= string2 ) {
c2 = (unsigned char) *current2 ;
if ( ismbstrail_cp ( string2, current2, codepage ) ) c2 += 0x100 ;
}
else {
c2 = 0 ;
}
if ( ! c1 || c1 != c2 ) break ;
current1 -- ;
current2 -- ;
count -- ;
}
return c1 - c2 ;
}
////////////////////////////////////////////
// strrnicmp () //
////////////////////////////////////////////
// 暥帤楍傪枛旜偐傜 ignore case 偱斾妑乮MULTIBYTE斉乯
int mbsrnicmp_cp ( const char *string1, const char *string2, size_t count, int codepage ) {
int c1, c2 ;
const char *current1 = strend ( string1 ) - 1 ;
const char *current2 = strend ( string2 ) - 1 ;
while ( 1 ) {
if ( ! count ) return 0 ;
if ( current1 >= string1 ) {
c1 = (unsigned char) *current1 ;
if ( ismbstrail_cp ( string1, current1, codepage ) ) c1 += 0x100 ;
c1 = tolower ( c1 ) ;
}
else {
c1 = 0 ;
}
if ( current2 >= string2 ) {
c2 = (unsigned char) *current2 ;
if ( ismbstrail_cp ( string2, current2, codepage ) ) c2 += 0x100 ;
c2 = tolower ( c2 ) ;
}
else {
c2 = 0 ;
}
if ( ! c1 || c1 != c2 ) break ;
current1 -- ;
current2 -- ;
count -- ;
}
return c1 - c2 ;
}
////////////////////////////////////////////
// strheadicmp () //
////////////////////////////////////////////
// 暥帤楍偺愭摢傪 ignore case 偱斾妑乮MULTIBYTE斉乯
int mbsheadicmp_cp ( const char *string, const char *head, int codepage ) {
return mbsnicmp_cp ( string, head, strlen ( head ), codepage ) ;
}
////////////////////////////////////////////
// strtailcmp () //
////////////////////////////////////////////
// 暥帤楍偺枛旜傪斾妑乮MULTIBYTE斉乯
int mbstailcmp_cp ( const char *string, const char *tail, int codepage ) {
return mbsrncmp_cp ( string, tail, strlen ( tail ), codepage ) ;
}
////////////////////////////////////////////
// strtailicmp () //
////////////////////////////////////////////
// 暥帤楍偺枛旜傪 ignore case 偱斾妑乮MULTIBYTE斉乯
int mbstailicmp_cp ( const char *string, const char *tail, int codepage ) {
return mbsrnicmp_cp ( string, tail, strlen ( tail ), codepage ) ;
}
////////////////////////////////////////////
// strstr () //
////////////////////////////////////////////
// 暥帤楍偐傜暥帤楍傪専嶕乮MULTIBYTE斉乯
char *mbsstr_cp ( const char *string1, const char *string2, int codepage ) {
size_t length1 = strlen ( string1 ) ;
size_t length2 = strlen ( string2 ) ;
if ( ! length2 ) return (char*) string1 ;
const char *tail = string1 + length1 - length2 ;
int c2 = (unsigned char) *string2 ;
while ( string1 <= tail ) {
int c1 = (unsigned char) *string1 ;
if ( c1 == c2 && ! strncmp ( string1, string2, length2 ) ) return (char*) string1 ;
if ( ismbblead_cp ( (unsigned char) *string1, codepage ) ) {
if ( ismbbsurrogate_cp ( (unsigned char) *( string1 + 1 ), codepage ) ) {
string1 ++ ;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -