Prechádzať zdrojové kódy

Split + arrays : OK

Eric Streit 5 mesiacov pred
rodič
commit
a7ecf17212
16 zmenil súbory, kde vykonal 420 pridanie a 6 odobranie
  1. 1 1
      Readme.md
  2. 28 0
      SplitV1.def
  3. 157 0
      SplitV1.mod
  4. BIN
      SplitV1.o
  5. BIN
      Subrange
  6. 45 0
      Subrange.mod
  7. BIN
      Subrange.o
  8. 4 0
      strUtils.def
  9. 4 3
      strUtils.mod
  10. BIN
      strUtils.o
  11. BIN
      test12
  12. 2 2
      test12.mod
  13. BIN
      testSplit
  14. 179 0
      testSplit.mod
  15. BIN
      testSplit.o
  16. BIN
      testSplitV1

+ 1 - 1
Readme.md

@@ -1,6 +1,6 @@
 # strUtils
 
-I'm makinga Gnu-Modula-2 version of strUtils, originaly written in Oberon by Norayr Chilingarian on his github : https://github.com/norayr/strutils
+I'm making a Gnu-Modula-2 version of strUtils, originaly written in Oberon by Norayr Chilingarian on his github : https://github.com/norayr/strutils
 
 It's a work in progress, as I only begin to write in GNU Modula-2.
 

+ 28 - 0
SplitV1.def

@@ -0,0 +1,28 @@
+DEFINITION MODULE SplitV1;
+
+TYPE
+  Element = RECORD
+              pos : CARDINAL;
+              element : ARRAY[0..255] OF CHAR;
+            END;  
+  Structure = ARRAY[0..20] OF Element;    
+ 
+VAR 
+  trimMode    : BOOLEAN;
+  allTrimMode : BOOLEAN;
+  noDupMode   : BOOLEAN;
+  rtrim       : BOOLEAN;
+  ltrim       : BOOLEAN;
+  
+                  
+PROCEDURE Ltrim ( VAR src : ARRAY OF CHAR; separator : CHAR  );
+  
+PROCEDURE Rtrim ( VAR src : ARRAY OF CHAR ; separator : CHAR );
+    
+PROCEDURE removeDuplicate ( VAR src : ARRAY OF CHAR ; separator : CHAR );             
+                  
+PROCEDURE SplitStr(s : ARRAY OF CHAR; separator : CHAR; VAR out : Structure); 
+
+PROCEDURE InitStructure ( VAR out : Structure);
+
+END SplitV1.

+ 157 - 0
SplitV1.mod

@@ -0,0 +1,157 @@
+IMPLEMENTATION MODULE SplitV1;
+
+IMPORT Strings;
+FROM InOut IMPORT Write, WriteString, WriteCard, WriteLn;
+
+VAR
+  j : CARDINAL;
+  
+PROCEDURE Ltrim (VAR src : ARRAY OF CHAR; separator : CHAR  );
+
+VAR
+  longueur : CARDINAL;
+  
+  BEGIN
+    REPEAT
+      longueur := LENGTH(src);
+      IF src[0] = separator THEN
+      	(*PROCEDURE Delete (VAR stringVar: ARRAY OF CHAR; startIndex, numberToDelete:CARDINAL);*)
+        (* Deletes at most numberToDelete characters from stringVar, starting at position startIndex.*)
+        Strings.Delete(src,0,1);
+      END;  
+    UNTIL (src[0] # separator) OR (longueur = 0)
+  END Ltrim;     
+  
+PROCEDURE Rtrim ( VAR src : ARRAY OF CHAR ; separator : CHAR );
+
+VAR
+  longueur : CARDINAL;
+  
+  BEGIN
+    longueur := LENGTH(src);
+    REPEAT
+      longueur := LENGTH(src);
+      IF src[longueur - 1] = separator THEN
+      	(*PROCEDURE Delete (VAR stringVar: ARRAY OF CHAR; startIndex, numberToDelete:CARDINAL);*)
+        (* Deletes at most numberToDelete characters from stringVar, starting at position startIndex.*)
+        Strings.Delete(src,longueur - 1,1);
+      END;  
+      longueur := LENGTH(src);
+    UNTIL (src[longueur - 1] # separator) OR (longueur =0 ) 
+   END Rtrim;  
+  
+PROCEDURE removeDuplicate ( VAR src : ARRAY OF CHAR ; separator : CHAR );             
+
+VAR
+  longueur : CARDINAL;
+  pos      : CARDINAL;
+  
+  BEGIN
+    pos := 0;
+    longueur := LENGTH(src);
+    LOOP
+      IF (longueur = 0 ) OR (pos = longueur ) THEN
+        RETURN
+      END;
+      (*Write(src[pos]);Write(" ");*)
+      IF src[pos] = separator THEN 
+        pos := pos + 1;
+        WHILE src[pos] = separator DO 
+          Strings.Delete(src,pos,1);
+        END; 
+      ELSE 
+        INC(pos) 
+      END;  
+      longueur := LENGTH(src);
+    END; (* end loop*)
+  END removeDuplicate;   
+  
+PROCEDURE SplitStr(s : ARRAY OF CHAR; separator : CHAR; VAR out : Structure) ;
+
+(*
+  There are several cases to take account for :
+  - standard case : s begin with an element ans end with an element
+  - s begins with a separator and end with an element
+  - s begins with a separator and ends with a separators
+  - there are consecutive separators inside s
+  - there are consecutive separators at the beginning
+  - there are consecutive separators at the end
+*)
+
+VAR 
+  i : CARDINAL;
+  indice : CARDINAL;
+  longueur : CARDINAL;
+  resultat : ARRAY[0..255] OF CHAR;
+  deja : BOOLEAN;
+  c : CHAR;
+  
+  BEGIN
+    indice := 0; 
+    (* modes de fonctionnement *)
+    (*  trimMode    : BOOLEAN;
+        allTrimMode : BOOLEAN;
+        noDupMode   : BOOLEAN;
+        rtrim       : BOOLEAN;
+        ltrim       : BOOLEAN;*)
+    IF trimMode THEN
+      Rtrim ( s, separator);
+      Ltrim ( s, separator);
+    END;
+    
+    IF ltrim THEN
+      Ltrim ( s, separator);
+    END;
+    
+    IF rtrim THEN
+      Rtrim ( s, separator);
+    END;    
+    
+    IF noDupMode THEN
+      removeDuplicate ( s, separator);
+    END;
+    
+    IF allTrimMode THEN
+      Rtrim ( s, separator);
+      Ltrim ( s, separator);
+      removeDuplicate ( s, separator);
+    END;  
+    
+    longueur := LENGTH(s);
+    resultat := "";
+    deja := FALSE;
+    FOR i := 0 TO longueur -1 DO
+      c := s[i]; 						(* the first character of the string s *)
+      IF c # separator THEN 					(* the character belongs to an element *)
+         Strings.Append(Strings.String1(c),resultat);
+      ELSE
+        out[indice].element := resultat;
+        resultat := ""; 
+        INC(indice);
+      END;
+      out[indice].element := resultat;
+    END;
+    FOR i := indice + 1 TO HIGH(out) DO 
+      out[i].pos := MAX(CARDINAL);
+    END;
+  END SplitStr;
+
+PROCEDURE InitStructure ( VAR out : Structure);
+
+VAR 
+  i : CARDINAL;
+  
+BEGIN 
+  FOR i := 0 TO HIGH(out)   DO
+    out[i].pos := i;
+    out[i].element := "";
+  END;
+END InitStructure;
+
+BEGIN 
+  trimMode    := FALSE;
+  allTrimMode := FALSE;
+  noDupMode   := FALSE;
+  ltrim       := FALSE;
+  rtrim       := FALSE; 
+END SplitV1. 

BIN
SplitV1.o


BIN
Subrange


+ 45 - 0
Subrange.mod

@@ -0,0 +1,45 @@
+MODULE Subrange;
+
+TYPE Days = (mon,tue,wed,thu,fri,sat,sun);
+     Work = [mon..fri];
+     Rest = [sat..sun];
+
+VAR  Day      : Days;  (* This is any day of the week *)
+     Workday  : Work;  (* These are the working days  *)
+     Weekend  : Rest;  (* The two weekend days only   *)
+     Index    : [1..12];
+     Alphabet : ['a'..'z'];
+     Start    : ['a'..'e'];
+
+BEGIN    (* Main program *)
+(*  The following statements are commented out because they
+    contain various errors that will halt compilation.
+
+   Workday := sat;   sat is not part of Workday's subrange.
+   Rest := fri;      fri is not part of Rest's subrange.
+   Index := 13;      Index is only allowed to go up to 12,
+   Index := -1;        and down to 1.
+   Alphabet := 'A';  Alphabet, as defined, includes only the
+                       lowercase alphabet.
+   Start := 'h';     h is not in the first five letters.
+
+   End of the commented out section of program.
+*)
+
+   Workday  := tue;
+   Weekend  := sat;
+   Day      := Workday;
+   Day      := Weekend;
+   Index    := 3 + 2 * 2;
+   Start    := 'd';
+   Alphabet := Start;
+                            (* Since Alphabet is 'd'     *)
+   INC(Alphabet);           (*   and now 'e'             *)
+   Start := Alphabet;       (* Start will be 'e'         *)
+   DEC(Start);
+   DEC(Start);              (* Start will be 'c'         *)
+   Day := wed;
+   INC(Day);                (* Day will now be 'thu'     *)
+   INC(Day);                (* Day will now be 'fri'     *)
+   Index := ORD(Day);       (* Index will be 4 (fri = 4) *)
+END Subrange.

BIN
Subrange.o


+ 4 - 0
strUtils.def

@@ -36,24 +36,28 @@ PROCEDURE isascii(s : ARRAY OF CHAR) : BOOLEAN;
 
 PROCEDURE isdigit(s : ARRAY OF CHAR) : BOOLEAN;	
 (* Returns True if all characters in the string are digits *)
+(* there is a dedicated procedure in CharClass for that *)
 
 PROCEDURE isidentifier(s : ARRAY OF CHAR) : BOOLEAN;
 (* Returns True if the string is an identifier *)
 
 PROCEDURE islower(s : ARRAY OF CHAR) : BOOLEAN;	
 (* 	eturns True if all characters in the string are lower case *)
+(* there is a dedicated procedure in CharClass for that *)
 
 PROCEDURE isprintable(s : ARRAY OF CHAR) : BOOLEAN;
 (* Returns True if all characters in the string are printable *)
 
 PROCEDURE isspace(s : ARRAY OF CHAR) : BOOLEAN;	
 (* Returns True if all characters in the string are whitespaces *)
+(* there is a dedicated procedure in CharClass for that *)
 
 PROCEDURE istitle(s : ARRAY OF CHAR) : BOOLEAN;	
 (*  Returns True if the string follows the rules of a title *)
 
 PROCEDURE isupper(s : ARRAY OF CHAR) : BOOLEAN;	
 (* Returns True if all characters in the string are upper case *)
+(* there is a dedicated procedure in CharClass for that *)
 
 (* end of python alike functions *)
 

+ 4 - 3
strUtils.mod

@@ -41,7 +41,6 @@ PROCEDURE Copy(VAR Ns: ARRAY OF CHAR; S: ARRAY OF CHAR);
 
 (*
 MemCopy - copys a region of memory to the required destination.
-
 PROCEDURE MemCopy (from: ADDRESS; length: CARDINAL; to: ADDRESS) ;
 *)
 
@@ -214,7 +213,7 @@ BEGIN
 END isidentifier;
 
 PROCEDURE islower(s : ARRAY OF CHAR) : BOOLEAN;	
-(* 	eturns True if all characters in the string are lower case *)
+(* 	Returns True if all characters in the string are lower case *)
 
 VAR 
   i      : CARDINAL;
@@ -282,9 +281,10 @@ VAR
 BEGIN 
   result := TRUE;
   (* we need to separate the string into words and test the first char of every word *)
+  (* would be nice to have an equivalent to Python "split" *)  
   l := Strings.Length(s) -1;
   WHILE i <= l DO 
-    
+    DEC(i);
   END;
   RETURN result
 END istitle;
@@ -430,6 +430,7 @@ BEGIN
      END;  
   END
 END copyBytes;
+
 (*
 PROCEDURE appendNumChars(VAR extra: pstring; extraNum: LONGINT; VAR destination: pstring; destinationNum: LONGINT);
 VAR

BIN
strUtils.o


BIN
test12


+ 2 - 2
test12.mod

@@ -245,7 +245,7 @@ PROCEDURE testistitle;
 
 BEGIN 
    InOut.WriteString("test istitle");
-   chaine := "Estvoila";
+   chaine := "Et Voila";
    InOut.WriteLn;
    IF strUtils.istitle(chaine) THEN
      InOut.WriteString("C'est un titre");
@@ -253,7 +253,7 @@ BEGIN
      InOut.WriteString("Ce n'est pas un titre");
    END;
    InOut.WriteLn;
-   chaine := "sdkjhqs1";
+   chaine := "Sdkj hqs1";
    IF strUtils.istitle(chaine) THEN
      InOut.WriteString("C'est un titre");
    ELSE

BIN
testSplit


+ 179 - 0
testSplit.mod

@@ -0,0 +1,179 @@
+MODULE testSplit;
+
+IMPORT SplitV1;
+FROM InOut IMPORT Write, WriteLn, WriteString, WriteCard;
+
+VAR 
+  chaine1 : ARRAY[0..255] OF CHAR;
+  chaine2 : ARRAY[0..255] OF CHAR;
+  maStructure : SplitV1.Structure;
+  i : CARDINAL;
+
+BEGIN
+  SplitV1.InitStructure(maStructure);
+
+(*  WriteString (" Test de Ltrim ");
+  WriteLn;
+  chaine1 := "=====Encore=un=test==";
+  WriteString(chaine1);
+  WriteLn;
+  Write("*");
+  SplitV1.Ltrim(chaine1, "=");
+  WriteString(chaine1);
+  Write("*");
+  WriteLn;
+*)
+(*  
+  WriteString (" Test de Rtrim ");
+  WriteLn;
+  chaine1 := "!!Encore!un=test!!!!!";
+  WriteString(chaine1);
+  WriteLn;
+  Write("*");
+  SplitV1.Rtrim(chaine1, "!");
+  WriteString(chaine1);
+  Write("*");
+  WriteLn;
+*)  
+
+(*  WriteString (" Test de RemoveDuplicate ");
+  WriteLn;
+  chaine1 := "!!Encore!!un!!test!!!!!";
+  WriteString(chaine1);
+  WriteLn;
+  Write("*");
+  SplitV1.removeDuplicate(chaine1, "!");
+  WriteString(chaine1);
+  Write("*");
+  WriteLn; 
+*)
+
+(******************* test de Split dans les # cas *************)
+(*  ça fonctionne !
+  WriteString (" Test de Split version 1 sans separateurs en début ou à la fin et sans duplicats");
+  WriteLn;
+  Write("*");
+  chaine1 := "Encore de beaux jours devant nous";
+  WriteString(chaine1);
+  Write("*");
+  WriteLn;  
+
+  SplitV1.SplitStr(chaine1, " ", maStructure);
+  FOR i := 0 TO HIGH(maStructure) DO
+    WriteCard(maStructure[i].pos,5);
+    Write("-");
+    WriteString(maStructure[i].element);
+    WriteLn;
+  END;
+  WriteLn;
+  WriteLn;  
+*)
+  
+(* ça fonctionne !
+  WriteString (" Test de Split version avec separateurs au début ou à la fin et duplicata");
+  WriteLn;
+  Write("*");
+  chaine1 := " Encore   de beaux jours   devant nous ";
+  Write("*");
+  WriteString(chaine1);
+  WriteLn;
+  WriteString (" Test de Split ");
+  WriteLn;  
+
+  SplitV1.SplitStr(chaine1, " ", maStructure);
+  FOR i := 0 TO HIGH(maStructure) DO
+    WriteCard(maStructure[i].pos,5);
+    Write("-");
+    WriteString(maStructure[i].element);
+    WriteLn;
+  END;
+  WriteLn;
+  WriteLn;    
+*)
+(* ça marche ! 
+  WriteString (" Test de Split version avec separateurs au début ou à la fin et duplicata, mais en mode suppression duplicata");
+  WriteLn;
+  Write("*");
+  chaine1 := "###Encore####de#beaux##jours#####devant nous####";
+  WriteString(chaine1);
+  Write("*");
+  WriteLn;
+  WriteString (" Test de Split ");
+  WriteLn;  
+  SplitV1.noDupMode := TRUE ;
+  SplitV1.SplitStr(chaine1, "#", maStructure);
+  FOR i := 0 TO HIGH(maStructure) DO
+    WriteCard(maStructure[i].pos,5);
+    Write("-");
+    WriteString(maStructure[i].element);
+    WriteLn;
+  END;
+  WriteLn;
+  WriteLn;
+*)  
+(* ça marche !  
+  WriteString (" Test de Split version avec separateurs au début ou à la fin et duplicata, mais en mode ltrim");
+  WriteLn;
+  Write("*");
+  chaine1 := "###Encore####de#beaux##jours#####devant nous####";
+  WriteString(chaine1);
+  Write("*");
+  WriteLn;
+  WriteString (" Test de Split ");
+  WriteLn;  
+  SplitV1.ltrim := TRUE ;
+  SplitV1.SplitStr(chaine1, "#", maStructure);
+  FOR i := 0 TO HIGH(maStructure) DO
+    WriteCard(maStructure[i].pos,5);
+    Write("-");
+    WriteString(maStructure[i].element);
+    WriteLn;
+  END;
+  WriteLn;
+  WriteLn;  
+  *)
+
+(*
+  WriteString (" Test de Split version avec separateurs au début ou à la fin et duplicata, mais en mode rtrim");
+  WriteLn;
+  Write("*");
+  chaine1 := "###Encore####de#beaux##jours#####devant nous####";
+  WriteString(chaine1);
+  Write("*");
+  WriteLn;
+  WriteString (" Test de Split ");
+  WriteLn;  
+  SplitV1.rtrim := TRUE ;
+  SplitV1.SplitStr(chaine1, "#", maStructure);
+  FOR i := 0 TO HIGH(maStructure) DO
+    WriteCard(maStructure[i].pos,5);
+    Write("-");
+    WriteString(maStructure[i].element);
+    WriteLn;
+  END;
+  WriteLn;
+  WriteLn;
+ *)
+ 
+  WriteString (" Test de Split version avec separateurs au début ou à la fin et duplicata, mais en mode rtrim");
+  WriteLn;
+  Write("*");
+  chaine1 := "###Encore####de#beaux##jours#####devant nous####";
+  WriteString(chaine1);
+  Write("*");
+  WriteLn;
+  WriteString (" Test de Split ");
+  WriteLn;  
+  SplitV1.allTrimMode := TRUE ;
+  SplitV1.SplitStr(chaine1, "#", maStructure);
+  FOR i := 0 TO HIGH(maStructure) DO
+    WriteCard(maStructure[i].pos,5);
+    Write("-");
+    WriteString(maStructure[i].element);
+    WriteLn;
+  END;
+  WriteLn;
+  WriteLn; 
+  
+  WriteString (" Fin du test V1 ");
+END testSplit.

BIN
testSplit.o


BIN
testSplitV1