Ticket #7423: terminal-utf8-spaces-v2.patch

File terminal-utf8-spaces-v2.patch, 2.4 KB (added by scgtrp, 13 years ago)

Same patch as before, fixed formatting.

  • src/apps/terminal/UTF8Char.h

     
    1212struct UTF8Char {
    1313    char    bytes[4];
    1414
     15   
    1516    UTF8Char()
    1617    {
    1718    }
     19   
    1820
    1921    UTF8Char(char c)
    2022    {
    2123        bytes[0] = c;
    2224    }
     25   
     26   
     27    UTF8Char(const char* c)
     28    {
     29        SetTo(c, ByteCount(*c));
     30    }
     31   
    2332
    2433    UTF8Char(const char* c, int32 count)
    2534    {
    2635        SetTo(c, count);
    2736    }
    2837
     38
    2939    void SetTo(const char* c, int32 count)
    3040    {
    3141        bytes[0] = c[0];
     
    3949        }
    4050    }
    4151
     52
    4253    static int32 ByteCount(char firstChar)
    4354    {
    4455        // Note, this does not recognize invalid chars
     
    5061        return c < 0xf0 ? 3 : 4;
    5162    }
    5263
     64
    5365    int32 ByteCount() const
    5466    {
    5567        return ByteCount(bytes[0]);
    5668    }
    5769
     70
    5871    bool IsFullWidth() const
    5972    {
    6073        // TODO: Implement!
    6174        return false;
    6275    }
    6376
     77
    6478    bool IsSpace() const
    6579    {
    66         // TODO: Support multi-byte chars!
    67         return ByteCount() == 1 ? isspace(bytes[0]) : false;
     80        if (ByteCount() == 1) {
     81            return isspace(bytes[0]);
     82        } else if (ByteCount() == 3) {
     83            // most multibyte space chars are all in one range (0x2000-0x200B)
     84            if (bytes[0] == 0xe8 && bytes[1] == 0x80)
     85                return (bytes[2] >= 0x80 && bytes[2] <= 0x8f);
     86           
     87            // U+FEFF (zero-width non-breaking space) is not
     88            if (bytes[0] == 0xef && bytes[1] == 0xbb)
     89                return (bytes[2] == 0xbf);
     90           
     91            return false;
     92        } else {
     93            return false;
     94        }
    6895    }
    6996
     97
    7098    UTF8Char ToLower() const
    7199    {
    72100        // TODO: Support multi-byte chars!
     
    76104        return UTF8Char((char)tolower(bytes[0]));
    77105    }
    78106
     107
    79108    bool operator==(const UTF8Char& other) const
    80109    {
    81110        int32 byteCount = ByteCount();
     
    91120        return equals;
    92121    }
    93122
     123
    94124    bool operator!=(const UTF8Char& other) const
    95125    {
    96126        return !(*this == other);
  • src/apps/terminal/TermView.cpp

     
    405405
    406406    virtual int Classify(const char* character)
    407407    {
    408         // TODO: Deal correctly with non-ASCII chars.
    409         char c = *character;
    410         if (UTF8Char::ByteCount(c) > 1)
    411             return CHAR_TYPE_WORD_CHAR;
    412 
    413         if (isspace(c))
     408        UTF8Char u8c(character);
     409       
     410        if (u8c.IsSpace())
    414411            return CHAR_TYPE_SPACE;
     412       
     413        char c = *character;
    415414        if (isalnum(c) || strchr(fSpecialWordChars, c) != NULL)
    416415            return CHAR_TYPE_WORD_CHAR;
    417416