📄 cjkctype.cpp
字号:
// strspn (), strcspn () //
////////////////////////////////////////////
// string2 偵懏偝側偄暥帤偑 string1 偱嵟弶偵弌尰偡傞埵抲傪曉偡乮MULTIBYTE斉乯
// 尒偮偐傜側偗傟偽枛旜傪曉偡
size_t mbsspn_cp ( const char *string1, const char *string2, int codepage ) {
int c ;
const char *current ;
for ( current = string1 ; *current ; current ++ ) {
int islead = ismbblead_cp ( (unsigned char) *current, codepage ) ;
int issurrogate = ismbbsurrogate_cp ( (unsigned char) *(current + 1 ), codepage ) ;
if ( islead ) {
if ( issurrogate ) {
int c1 = ( (unsigned char) *( current + 0 ) << 8 ) | (unsigned char) *( current + 1 ) ;
int c2 = ( (unsigned char) *( current + 2 ) << 8 ) | (unsigned char) *( current + 3 ) ;
c = ( c1 << 16 ) | c2 ;
}
else {
c = ( (unsigned char) *current << 8 ) | (unsigned char) *( current + 1 ) ;
}
}
else {
c = *current ;
}
if ( ! mbschr_cp ( string2, c, codepage ) ) break ;
if ( islead ) {
if ( issurrogate ) {
current ++ ;
if ( *( current + 1 ) ) current ++ ;
if ( *( current + 1 ) ) current ++ ;
}
else {
if ( *( current + 1 ) ) current ++ ;
}
}
}
return current - string1 ;
}
// string2 偵懏偡暥帤偑 string1 偱嵟弶偵弌尰偡傞埵抲傪曉偡乮MULTIBYTE斉乯
// 尒偮偐傜側偗傟偽枛旜傪曉偡
size_t mbscspn_cp ( const char *string1, const char *string2, int codepage ) {
int c ;
const char *current ;
for ( current = string1 ; *current ; current ++ ) {
int islead = ismbblead_cp ( (unsigned char) *current, codepage ) ;
int issurrogate = ismbbsurrogate_cp ( (unsigned char) *(current + 1 ), codepage ) ;
if ( islead ) {
if ( issurrogate ) {
int c1 = ( (unsigned char) *( current + 0 ) << 8 ) | (unsigned char) *( current + 1 ) ;
int c2 = ( (unsigned char) *( current + 2 ) << 8 ) | (unsigned char) *( current + 3 ) ;
c = ( c1 << 16 ) | c2 ;
}
else {
c = ( (unsigned char) *current << 8 ) | (unsigned char) *( current + 1 ) ;
}
}
else {
c = *current ;
}
if ( mbschr_cp ( string2, c, codepage ) ) break ;
if ( islead ) {
if ( issurrogate ) {
current ++ ;
if ( *( current + 1 ) ) current ++ ;
if ( *( current + 1 ) ) current ++ ;
}
else {
if ( *( current + 1 ) ) current ++ ;
}
}
}
return current - string1 ;
}
////////////////////////////////////////////
// strspnp (), strpbrk () //
////////////////////////////////////////////
// string2 偵懏偝側偄暥帤偑 string1 偱嵟弶偵弌尰偡傞埵抲傪曉偡乮MULTIBYTE斉乯
// 尒偮偐傜側偗傟偽 NULL 傪曉偡
char *mbsspnp_cp ( const char *string1, const char *string2, int codepage ) {
const char *result = string1 + mbsspn_cp ( string1, string2, codepage ) ;
return *result ? (char*) result : NULL ;
}
// string2 偵懏偡暥帤偑 string1 偱嵟弶偵弌尰偡傞埵抲傪曉偡乮MULTIBYTE斉乯
// 尒偮偐傜側偗傟偽 NULL 傪曉偡
char *mbspbrk_cp ( const char *string1, const char *string2, int codepage ) {
const char *result = string1 + mbscspn_cp ( string1, string2, codepage ) ;
return *result ? (char*) result : NULL ;
}
////////////////////////////////////////////
// strlwr () //
////////////////////////////////////////////
// 暥帤楍傪彫暥帤偵乮MULTIBYTE斉乯
char *mbslwr_cp ( char *string, int codepage ) {
for ( char *current = string ; *current ; current ++ ) {
if ( *current >= 'A' && *current <= 'Z' ) *current -= 'A' - 'a' ;
if ( ismbblead_cp ( (unsigned char) *current, codepage ) && *( current + 1 ) ) current ++ ;
}
return string ;
}
////////////////////////////////////////////
// strupr () //
////////////////////////////////////////////
// 暥帤楍傪戝暥帤偵乮MULTIBYTE斉乯
char *mbsupr_cp ( char *string, int codepage ) {
for ( char *current = string ; *current ; current ++ ) {
if ( *current >= 'a' && *current <= 'z' ) *current += 'A' - 'a' ;
if ( ismbblead_cp ( (unsigned char) *current, codepage ) && *( current + 1 ) ) current ++ ;
}
return string ;
}
////////////////////////////////////////////
// strrev () //
////////////////////////////////////////////
// 暥帤楍傪媡揮乮MULTIBYTE斉乯
char *mbsrev_cp ( char *string, int codepage ) {
for ( char *current = string ; *current ; current ++ ) {
if ( ismbblead_cp ( (unsigned char) *current, codepage ) ) {
if ( ismbbsurrogate_cp ( (unsigned char) *( current + 1 ), codepage ) ) {
if ( *( current + 2 ) && *( current + 3 ) ) {
char c0 = *( current + 0 ) ;
char c1 = *( current + 1 ) ;
char c2 = *( current + 2 ) ;
char c3 = *( current + 3 ) ;
*( current + 3 ) = c0 ;
*( current + 2 ) = c1 ;
*( current + 1 ) = c2 ;
*( current + 0 ) = c3 ;
current ++ ;
current ++ ;
current ++ ;
continue ;
}
else {
*current = 0 ;
break ;
}
}
else {
if ( *( current + 1 ) ) {
char c0 = *( current + 0 ) ;
char c1 = *( current + 1 ) ;
*( current + 1 ) = c0 ;
*( current + 0 ) = c1 ;
current ++ ;
continue ;
}
else {
*current = 0 ;
break ;
}
}
}
}
return strrev ( string ) ;
}
////////////////////////////////////////////
// strtrunc () //
////////////////////////////////////////////
// 暥帤楍傪抁弅乮MULTIBYTE斉乯
// length 偼嵟戝帤悢乮廔抂僰儖暥帤傪娷傑側偄乯
char *mbstrunc_cp ( char *string, size_t length, int codepage ) {
char *end = string + length ;
for ( char *current = string ; *current ; current ++ ) {
if ( ismbblead_cp ( (unsigned char) *current, codepage ) ) {
if ( ismbbsurrogate_cp ( (unsigned char) *( current + 1 ), codepage ) ) {
if ( current >= end - 3 || ! *( current + 2 ) || ! *( current + 3 ) ) {
*current = 0 ;
break ;
}
current ++ ;
current ++ ;
current ++ ;
}
else {
if ( current >= end - 1 || ! *( current + 1 ) ) {
*current = 0 ;
break ;
}
current ++ ;
}
}
else {
if ( current >= end ) {
*current = 0 ;
break ;
}
}
}
return string ;
}
////////////////////////////////////////////
// strlen () //
////////////////////////////////////////////
// 暥帤悢傪曉偡乮MULTIBYTE斉乯
// 俀僶僀僩暥帤偼侾暥帤偲偟偰悢偊傞
size_t mbslen_cp ( const char *string, int codepage ) {
size_t n ;
for ( n = 0 ; *string ; n ++, string ++ ) {
if ( ismbblead_cp ( (unsigned char) *string, codepage ) ) {
if ( ismbbsurrogate_cp ( (unsigned char) *( string + 1 ), codepage ) ) {
string ++ ;
if ( *( string + 1 ) ) string ++ ;
if ( *( string + 1 ) ) string ++ ;
}
else {
if ( *( string + 1 ) ) string ++ ;
}
}
}
return n ;
}
////////////////////////////////////////////
// a/wdup () //
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -