clean c code


  • 0
    B
    static int  mask1      = 0x80, pattern1      = 0x00, 
                mask2      = 0xe0, pattern2      = 0xc0,
                mask3      = 0xf0, pattern3      = 0xe0,
                mask4      = 0xf8, pattern4      = 0xf0,
                maskOther  = 0xc0, patternOther  = 0x80;
    
    static int validContByte(int c) {
        return (c & maskOther) == patternOther;
    }
    
    static int valid1(int c) {
        return (c & mask1) == pattern1;
    }
    
    static int valid2(int c) {
        return (c & mask2) == pattern2;
    }
    
    static int valid3(int c) {
        return (c & mask3) == pattern3;
    }
    
    static int valid4(int c) {
        return (c & mask4) == pattern4;
    }
    
    bool validUtf8(int* data, int dataSize) {
        int i, c, contBytes;
        
        for (i = 0; i < dataSize; i++) {
            c = data[i];
            if (valid1(c))
                continue;
            else if (valid2(c))
                contBytes = 1;
            else if (valid3(c))
                contBytes = 2;
            else if (valid4(c))
                contBytes = 3;
            else
                return false;
                
            while (contBytes-- > 0)
                if (++i >= dataSize || !validContByte(data[i]))
                    return false;
        }
        return true;
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.