File: 02_bounds_checking.re

package info (click to toggle)
re2c 4.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 51,512 kB
  • sloc: cpp: 34,160; ml: 8,494; sh: 5,311; makefile: 1,014; haskell: 611; python: 431; ansic: 234; javascript: 113
file content (41 lines) | stat: -rw-r--r-- 1,289 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// re2java $INPUT -o $OUTPUT

class Main {
    /*!max:re2c*/

    // Expects yymaxfill-padded string.
    static int lex(byte[] str) {
        // Pad string with yymaxfill zeroes at the end.
        byte[] yyinput = new byte[str.length + YYMAXFILL];
        System.arraycopy(str, 0, yyinput, 0, str.length); 

        int yycursor = 0;
        int yylimit = yyinput.length;
        int count = 0;

        loop: while (true) {
            /*!re2c
                re2c:YYCTYPE = "int";
                re2c:YYPEEK = "Byte.toUnsignedInt(yyinput[yycursor])";
                re2c:YYFILL = "return -1;";

                str = ['] ([^'\\] | [\\][^])* ['];

                [\x00] {
                    // Check that it is the sentinel, not some unexpected null.
                    return (yycursor - 1 == str.length) ? count : -1;
                }
                str  { count += 1; continue loop; }
                [ ]+ { continue loop; }
                *    { return -1; }
            */
        }
    }

    public static void main(String []args) {
        assert lex("".getBytes()) == 0;
        assert lex("'qu\0tes' 'are' 'fine: \\'' ".getBytes()) == 3;
        assert lex("'unterminated\\'".getBytes()) == -1;
        assert lex("'unexpected \00 null\\'".getBytes()) == -1;
    }
};