본문 바로가기
tip

How to detect UTF-8

by [김경민]™ ┌(  ̄∇ ̄)┘™ 2013. 4. 27.
728x90

A. case 1

 

[출처] http://stackoverflow.com/questions/1031645/how-to-detect-utf-8-in-plain-c

 

 

_Bool is_utf8(const char * string)
{
   
if(!string) return 0;

   
const unsigned char * bytes = (const unsigned char *)string;
   
while(*bytes)
   
{
       
if( (// ASCII
            
// use bytes[0] <= 0x7F to allow ASCII control characters
                bytes
[0] == 0x09 ||
                bytes
[0] == 0x0A ||
                bytes
[0] == 0x0D ||
               
(0x20 <= bytes[0] && bytes[0] <= 0x7E)
           
)
       
) {
            bytes
+= 1;
           
continue;
       
}

       
if( (// non-overlong 2-byte
               
(0xC2 <= bytes[0] && bytes[0] <= 0xDF) &&
               
(0x80 <= bytes[1] && bytes[1] <= 0xBF)
           
)
       
) {
            bytes
+= 2;
           
continue;
       
}

       
if( (// excluding overlongs
                bytes
[0] == 0xE0 &&
               
(0xA0 <= bytes[1] && bytes[1] <= 0xBF) &&
               
(0x80 <= bytes[2] && bytes[2] <= 0xBF)
           
) ||
           
(// straight 3-byte
               
((0xE1 <= bytes[0] && bytes[0] <= 0xEC) ||
                    bytes
[0] == 0xEE ||
                    bytes
[0] == 0xEF) &&
               
(0x80 <= bytes[1] && bytes[1] <= 0xBF) &&
               
(0x80 <= bytes[2] && bytes[2] <= 0xBF)
           
) ||
           
(// excluding surrogates
                bytes
[0] == 0xED &&
               
(0x80 <= bytes[1] && bytes[1] <= 0x9F) &&
               
(0x80 <= bytes[2] && bytes[2] <= 0xBF)
           
)
       
) {
            bytes
+= 3;
           
continue;
       
}

       
if( (// planes 1-3
                bytes
[0] == 0xF0 &&
               
(0x90 <= bytes[1] && bytes[1] <= 0xBF) &&
               
(0x80 <= bytes[2] && bytes[2] <= 0xBF) &&
               
(0x80 <= bytes[3] && bytes[3] <= 0xBF)
           
) ||
           
(// planes 4-15
               
(0xF1 <= bytes[0] && bytes[0] <= 0xF3) &&
               
(0x80 <= bytes[1] && bytes[1] <= 0xBF) &&
               
(0x80 <= bytes[2] && bytes[2] <= 0xBF) &&
               
(0x80 <= bytes[3] && bytes[3] <= 0xBF)
           
) ||
           
(// plane 16
                bytes
[0] == 0xF4 &&
               
(0x80 <= bytes[1] && bytes[1] <= 0x8F) &&
               
(0x80 <= bytes[2] && bytes[2] <= 0xBF) &&
               
(0x80 <= bytes[3] && bytes[3] <= 0xBF)
           
)
       
) {
            bytes
+= 4;
           
continue;
       
}

       
return 0;
   
}

   
return 1;
} 

 

B. case 2

 

[출처] http://www.devpia.com/Maeul/Contents/Detail.aspx?BoardID=51&MAEULNO=20&no=8619&page=1

 


BOOL IsUTF8Encode(const char * strUrl)
{
    if (NULL == strUrl)
    {
        return FALSE;
    }

    unsigned char * bytes = (unsigned char *)strUrl;
    while(*bytes)
    {
        if (    (//ASCII
            bytes[0] == 0x09 ||
            bytes[0] == 0x0A ||
            bytes[0] == 0x0D ||
            (0x20 <= bytes[0] && bytes[0] <= 0x7E)
            )
            )
        {
            bytes += 1;
            continue;
        }
        if (    (// non-overlong 2-byte
            (0xC2 <= bytes[0] && bytes[0] <= 0xDF) &&
            (0x80 <= bytes[1] && bytes[1] <= 0xBF)
            )
            )
        {
            bytes += 2;
            continue;
        }
        if (    (// excluding overlongs
            bytes[0] == 0xE0 &&
            (0xA0 <= bytes[1] && bytes[1] <= 0xBF) &&
            (0x80 <= bytes[2] && bytes[2] <= 0xBF)
            )||
            (// straight 3-byte
            ((0xE1 <= bytes[0] && bytes[0] <= 0xEC) ||
            bytes[0] == 0xEE ||
            bytes[0] == 0xEF) &&
            (0x80 <= bytes[1] && bytes[1] <= 0xBF) &&
            (0x80 <= bytes[2] && bytes[2] <= 0xBF)
            )||
            (// excluding surrogates
            bytes[0] == 0xED &&
            (0x80 <= bytes[1] && bytes[1] <= 0x9F) &&
            (0x80 <= bytes[2] && bytes[2] <= 0xBF)
            )
            )
        {
            bytes += 3;
            continue;
        }
        if (    (// planes 1-3
            bytes[0] == 0xF0 &&
            (0x90 <= bytes[1] && bytes[1] <= 0xBF) &&
            (0x80 <= bytes[2] && bytes[2] <= 0xBF) &&
            (0x80 <= bytes[3] && bytes[3] <= 0xBF)
            )||
            (// planes 4-15
            (0xF1 <= bytes[0] && bytes[0] <= 0xF3) &&
            (0x80 <= bytes[1] && bytes[1] <= 0xBF) &&
            (0x80 <= bytes[2] && bytes[2] <= 0xBF) &&
            (0x80 <= bytes[3] && bytes[3] <= 0xBF)
            )||
            (// planes 16
            bytes[0] == 0xF4 &&
            (0x80 <= bytes[1] && bytes[1] <= 0x8F) &&
            (0x80 <= bytes[2] && bytes[2] <= 0xBF) &&
            (0x80 <= bytes[3] && bytes[3] <= 0xBF)
            )

            ){
                bytes += 4;
                continue;
        }
        return FALSE;
    }
    return TRUE;
}

 

 

 

 

728x90

'tip' 카테고리의 다른 글

Windows 방화벽 동작을 제어 하는 "netsh 방화벽" 컨텍스트  (0) 2013.04.30
svn store password unencrypted  (0) 2013.04.27
android debug Source not found  (0) 2013.04.27
TileTemplateType  (0) 2013.04.27
Segoe UI Symbols  (0) 2013.04.27

댓글