Want to help DMD bugfixing? Write a simple utility.

Tyro Tyro
Sun Mar 20 09:54:38 PDT 2011


Not very elegant but this should get the job done:

000 module strip;
001 import std.algoritm : countUntil;
002 import std.array    : strip;
003 import std.file     : read;
004 import std.string   : splitlines;
005 import std.stdio    : writeln;
006
007 void main(string[] args)
008 {
009     bool start = false;
010     bool end = false;
011
012     bool comment = false;
013     bool nested  = false;
014
015     int lBrace, rBrace;
016
017     auto f = cast(char[]) read(args[1]);
018     auto file = splitlines(f);
019
020     foreach(ref ndx, line; file)
021     {
022         if(countUntil(strip(line), "//") == 0)
023         {
024             continue;
025         }
026
027         if(!comment && countUntil(line, "/+", != -1)
028         {
029             nested = true;
030
031             if(countUntil(line, "+/") != -1)
032                 nested = false;
033
034             continue;
035         }
036
037         while(nested)
038         {
039             if(countUntil(file[ndx], "+/") != -1)
040             {
041                 nested = false;
042                 end = true;
043                 goto endTest;
044             }
045             ndx++;
046         }
047
048         if(!nested && countUntil(line, "/*", != -1)
049         {
050             comment = true;
051
052             if(countUntil(line, "*/") != -1)
053                 comment = false;
054
055             continue;
056         }
057
058         while(comment)
059         {
060             if(countUntil(file[ndx], "*/") != -1)
061             {
062                 comment = false;
063                 end = true;
064                 goto endTest;
065             }
066             ndx++;
067         }
068
069         if(!end && countUntil(line, "unittest") != -1)
070         {
071             start = true;
072         }
073
074         if(!nested && start)
075         {
076             if(countUntil(line, "{") != -1)
077             {
078                 lBrace++;
079             }
080
081             if(countUntil(line, "}") != -1)
082             {
083                 rBrace++;
084                 if(rBrace > 0 && lBrace == rBrace)
085                 {
086                     end = true;
087                     lBrace = rBrace = 0;
088                 }
089             }
090         }
091
092         if(!start)
093             writeln(line);
094
095         endTest:;
096         if(end)
097         {
098             start = false;
099             end = false;
100         }
101     }
102 }

cheers.


More information about the Digitalmars-d-learn mailing list