Browse Source

Optimize some regex.

Stefano Cossu 2 years ago
parent
commit
31fbb20e19
2 changed files with 8 additions and 18 deletions
  1. 5 6
      src/codec/lexer_ttl.re
  2. 3 12
      test/test_codec_ttl.c

+ 5 - 6
src/codec/lexer_ttl.re

@@ -75,7 +75,7 @@ static void parse_init (ParseIterator *it, FILE *fh)
 }
 
 
-static void newline (ParseIterator *it) {
+static inline void newline (ParseIterator *it) {
     it->line ++;
     it->bol = YYCURSOR;
     log_trace ("New line: #%u.", it->line);
@@ -106,6 +106,7 @@ static int lex (ParseIterator *it, YYCTYPE **token_p)
 
     // Character classes.
     EOL             = [\n\r];
+    DIG             = [0-9];
     HEX             = [\x30-\x39\x41-\x46];
     CHAR_BASE       = "\\u" HEX{4} | "\\U" HEX{8} | '\\'
                     | [\U0000005D-\U0010FFFF];
@@ -127,12 +128,10 @@ static int lex (ParseIterator *it, YYCTYPE **token_p)
     // Constructs.
     COMMENT         = '#' [^\n\r]*;
     WS              = ([\t\x20] | EOL | COMMENT)+;
-    INTEGER         = ('-' | '+')? [0-9]+;
+    INTEGER         = [-+]? DIG+;
     EXPONENT        = [eE] INTEGER;
-    DOUBLE          = ('-' | '+') ? ([0-9]+ '.' [0-9]* EXPONENT
-                    | '.'? ([0-9])+ EXPONENT);
-    DECIMAL         = ('-' | '+')?
-                    ( [0-9]+ '.' [0-9]* | '.' ([0-9])+ | ([0-9])+ );
+    DOUBLE          = [-+]? (DIG+ '.' DIG* EXPONENT | '.'? DIG+ EXPONENT);
+    DECIMAL         = [-+]? (DIG+ '.' DIG* | '.'? DIG+);
 
      */
 

+ 3 - 12
test/test_codec_ttl.c

@@ -52,15 +52,6 @@ test_w3c_neg()
     size_t ct;
     char *err;
 
-    for (int i = 0; i <= W3C_NEG_TEST_CT; i++) {
-        sprintf (test_fname, "test/assets/ttl/bad-%02d.ttl", i);
-        FILE *test_stream = fopen (test_fname, "r");
-        log_info ("Testing %s", test_fname);
-
-        LSUP_rc rc = codec.decode_graph (test_stream, &gr, &ct, &err);
-        log_info ("rc: %d", rc);
-        ASSERT (rc == LSUP_PARSE_ERR, "Bad test did not raise a parse error!");
-    }
     for (int i = 0; i <= W3C_NEG_TEST_CT; i++) {
         sprintf (test_fname, "test/assets/ttl/bad-%02d.ttl", i);
         FILE *test_stream = fopen (test_fname, "r");
@@ -82,11 +73,11 @@ int codec_ttl_tests()
 
     codec = ttl_codec;
 
-    RUN (test_w3c_pos);
-    RUN (test_w3c_neg);
-    //RUN (test_encode_nt_graph);
+    //RUN (test_encode_ttl_graph);
     RUN (test_decode_nt_graph);
     RUN (test_decode_nt_bad_graph);
+    RUN (test_w3c_pos);
+    RUN (test_w3c_neg);
 
     free_terms(terms);