Ticket #1: decodeUTF.patch

File decodeUTF.patch, 2.7 kB (added by keinfarbton, 2 years ago)

patch with the suggested changes

  • descent.core/src/descent/core/dom/Lexer.java

    old new  
    125125                    break; 
    126126     
    127127                default: 
    128                     if ((c & 0x80) != 0) { 
     128                    if (c >= 0x80) { 
    129129                        int u = decodeUTF(); 
    130130                        if (u == PS || u == LS) { 
    131131                            scriptLine.setSourceRange(0, p); 
     
    948948        do 
    949949        { 
    950950            c = input[++p]; 
    951         } while (c > 0 && Chars.isidchar(c) || ((c & 0x80) != 0 && UniAlpha.isUniAlpha(decodeUTF()))); 
     951        } while (c > 0 && Chars.isidchar(c) || ((c >= 0x80) && UniAlpha.isUniAlpha(decodeUTF()))); 
    952952        sv = stringtable.update(input, t.ptr, p - t.ptr); 
    953953        id = (Identifier) sv.ptrvalue; 
    954954        if (id == null) 
     
    12351235                c -= 'a' - 10; 
    12361236            else if (c >= 'A' && c <= 'F') 
    12371237                c -= 'A' - 10; 
    1238             else if ((c & 0x80) != 0) 
     1238            else if (c >= 0x80) 
    12391239            {   p--; 
    12401240                int u = decodeUTF(); 
    12411241                p++; 
     
    13201320            } 
    13211321 
    13221322            default: 
    1323             if ((c & 0x80) != 0) 
     1323            if (c >= 0x80) 
    13241324            { 
    13251325                p--; 
    13261326                c = decodeUTF(); 
     
    13851385        } 
    13861386 
    13871387        default: 
    1388             if ((c & 0x80) != 0) { 
     1388            if (c >= 0x80) { 
    13891389                p--; 
    13901390                c = decodeUTF(); 
    13911391                p++; 
     
    21572157                        break; 
    21582158 
    21592159                    default: 
    2160                         if ((c & 0x80) != 0) { 
     2160                        if (c >= 0x80) { 
    21612161                            int u = decodeUTF(); 
    21622162                            if (u == PS || u == LS) { 
    21632163                                error( 
     
    21812181                continue; 
    21822182 
    21832183            default: 
    2184                 if ((input[p] & 0x80) != 0) { 
     2184                if (input[p] >= 0x80) { 
    21852185                    int u = decodeUTF(); 
    21862186                    if (u == PS || u == LS) { 
    21872187                        pragma.setSourceRange(start, p - start); 
     
    22062206    } 
    22072207     
    22082208    private int decodeUTF() { 
    2209         int[] u = new int[] { 0 }; 
    2210         int s = p; 
    2211         int len; 
    2212         int[] idx; 
    2213         String msg = null; 
    2214  
    2215         // Check length of remaining string up to 6 UTF-8 characters 
    2216         for (len = 1; len < 6 && len < end; len++) 
    2217         ; 
    2218  
    2219         idx = new int[] { 0 }; 
    2220         msg = Utf.decodeChar(input, s, len, idx, u); 
    2221         p += idx[0] - 1; 
    2222         if (msg != null) 
    2223         { 
    2224             error(msg, IProblem.InvalidUtf8Sequence, linnum, p, 1); 
    2225         } 
    2226         return u[0]; 
     2209        try { 
     2210            // decode one codepoint, starting at the index p 
     2211            int result = Character.codePointAt(input, p); 
     2212            // increase p with the count of chars for the decoded codepoint. 
     2213            p = Character.offsetByCodePoints(input, 0, input.length, p, 1); 
     2214            return result; 
     2215        } catch (Exception e) { 
     2216            // a problem while decoding the codepoint occured => invalid input 
     2217            error("invalid input sequence", IProblem.InvalidUtf8Sequence, linnum, p, 1); 
     2218            return 0; 
     2219        } 
    22272220    } 
    22282221     
    22292222    /*