Ticket #7423: terminal-utf8-spaces.patch

File terminal-utf8-spaces.patch, 2.4 KB (added by scgtrp, 13 years ago)
  • 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        }
     83        else if (ByteCount() == 3) {
     84            // most multibyte space chars are all in one range (0x2000-0x200B)
     85            if (bytes[0] == 0xe8 && bytes[1] == 0x80)
     86                return (bytes[2] >= 0x80 && bytes[2] <= 0x8f);
     87           
     88            // U+FEFF (zero-width non-breaking space) is not
     89            if (bytes[0] == 0xef && bytes[1] == 0xbb)
     90                return (bytes[2] == 0xbf);
     91           
     92            return false;
     93        }
     94        else {
     95            return false;
     96        }
    6897    }
    6998
     99
    70100    UTF8Char ToLower() const
    71101    {
    72102        // TODO: Support multi-byte chars!
     
    76106        return UTF8Char((char)tolower(bytes[0]));
    77107    }
    78108
     109
    79110    bool operator==(const UTF8Char& other) const
    80111    {
    81112        int32 byteCount = ByteCount();
     
    91122        return equals;
    92123    }
    93124
     125
    94126    bool operator!=(const UTF8Char& other) const
    95127    {
    96128        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