Parcourir la source

Add simple correction of id sequences

 + Remove letters placed around ids
Leo il y a 8 ans
Parent
commit
e395020d21
2 fichiers modifiés avec 10 ajouts et 5 suppressions
  1. 6 5
      src/id_parsing.ml
  2. 4 0
      src/test/id_parsing_t.ml

+ 6 - 5
src/id_parsing.ml

@@ -45,20 +45,21 @@ type atom =
   | Unique of int (* Like 2 *)
 ;;
 
-(* Return atom corresponding to a given string *)
+(* Return atom corresponding to a given string, correcting simple error *)
 let to_atom str =
   let open Str in
   (* Regexp to test atoms *)
-  let between = regexp "\\([0-9]+\\)-\\([0-9]+\\)" in
-  let unique = regexp "\\([0-9]+\\)" in
+  let between = regexp "[a-zA-Z]*\\([0-9]+\\)-\\([0-9]+\\)[a-zA-Z]*" in
+  let unique = regexp "[a-zA-Z]*\\([0-9]+\\)[a-zA-Z]*" in
 
   if string_match between str 0; then
-    (* String is like "1-5" *)
+    (* String is like "1-5", we test this first to allow autocorrection of
+     * unique things *)
     ( (replace_first between "\\1" str), (replace_first between "\\2" str) )
     |> ( fun (a,b) -> Between ((Int.of_string a), (Int.of_string b)) )
     |> Option.some
   else if string_match unique str 0; then
-    (* String is like "1-5" *)
+    (* String is like "1", or "1e", in which case its corrected to "1" *)
     Unique (Int.of_string (replace_first unique "\\1" str))
     |> Option.some
   else None

+ 4 - 0
src/test/id_parsing_t.ml

@@ -61,7 +61,11 @@ let ll_data = [
   ( "1-3", [1;2;3], "With double" );
   ( "24-24", [24], "Interval a-a" );
   ( "", [], "Empty list" );
+  ( "1e", [1], "Basic correction of malformed input" );
   ( "aaaavvvbg", [], "Empty list resulting of incorrect input" );
+  ( "i5a", [5], "A number inside letters" );
+  ( "eff", [], "Only letters" );
+  ( "a3-1u", [3;2;1], "Between inside letters" );
 ]
 
 let llt_l =