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

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

Now with even more spaces!

  • src/apps/terminal/UTF8Char.h

     
    77
    88#include <ctype.h>
    99#include <string.h>
     10#include <stdio.h>
    1011
    11 
    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() == 2) {
     83            uchar b0 = bytes[0];
     84            uchar b1 = bytes[1];
     85           
     86            // U+00A0 (no-break space), U+0085 (next line)
     87            if (b0 == 0xc2)
     88                return (b1 == 0xa0 || b1 == 0x85);
     89           
     90            return false;
     91        } else if (ByteCount() == 3) {
     92            uchar b0 = bytes[0];
     93            uchar b1 = bytes[1];
     94            uchar b2 = bytes[2];
     95           
     96            // most multibyte space chars are all in one range (0x2000-0x200B)
     97            if (b0 == 0xe2 && b1 == 0x80)
     98                return (b2 >= 0x80 && b2 <= 0x8f);
     99           
     100            // U+1680 (ogham space mark)
     101            if (b0 == 0xe1 && b1 == 0x9a)
     102                return (b2 == 0x80);
     103           
     104            // U+2028 (line separator), U+2029 (paragraph separator),
     105            // U+202F (narrow no-break space)
     106            if (b0 == 0xe2 && b1 == 0x80)
     107                return (b2 == 0xa8 || b2 == 0xa9 || b2 == 0xaf);
     108           
     109            // U+205F (medium mathematical space)
     110            if (b0 == 0xe2 && b1 == 0x81)
     111                return (b2 == 0x9f);
     112           
     113            // U+3000 (ideographic space)
     114            if (b0 == 0xe3 && b1 == 0x80)
     115                return (b2 == 0x80);
     116           
     117            // U+FEFF (zero-width non-breaking space)
     118            if (b0 == 0xef && b1 == 0xbb)
     119                return (b2 == 0xbf);
     120           
     121            return false;
     122        } else {
     123            return false;
     124        }
    68125    }
    69126
    70127    UTF8Char ToLower() const
     
    76133        return UTF8Char((char)tolower(bytes[0]));
    77134    }
    78135
     136
    79137    bool operator==(const UTF8Char& other) const
    80138    {
    81139        int32 byteCount = ByteCount();
     
    91149        return equals;
    92150    }
    93151
     152
    94153    bool operator!=(const UTF8Char& other) const
    95154    {
    96155        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.
     408        UTF8Char u8c(character);
     409       
     410        if (u8c.IsSpace())
     411            return CHAR_TYPE_SPACE;
     412       
    409413        char c = *character;
    410         if (UTF8Char::ByteCount(c) > 1)
     414        if (u8c.ByteCount() > 1 || isalnum(c) || strchr(fSpecialWordChars, c) != NULL)
    411415            return CHAR_TYPE_WORD_CHAR;
    412416
    413         if (isspace(c))
    414             return CHAR_TYPE_SPACE;
    415         if (isalnum(c) || strchr(fSpecialWordChars, c) != NULL)
    416             return CHAR_TYPE_WORD_CHAR;
    417 
    418417        return CHAR_TYPE_WORD_DELIMITER;
    419418    }
    420419