Date: Fri, 25 Aug 2006 15:31:34 +0300 From: "Matti J. Karki" <mjk@iki.fi> To: "W. D." <WD@us-webmasters.com> Cc: questions@freebsd.org Subject: Re: Code beautifiers, anyone? Message-ID: <1b15366e0608250531q6187d598h78b02e14ab4b5ac2@mail.gmail.com> In-Reply-To: <5.1.0.14.2.20060825064053.01eebec0@209.152.117.178> References: <7.0.1.0.2.20060824145822.0194fc10@broadpark.no> <1b15366e0608240618j62d41ad3j537f095b2e566ed5@mail.gmail.com> <7.0.1.0.2.20060824192439.02386de8@broadpark.no> <5.1.0.14.2.20060825064053.01eebec0@209.152.117.178>
next in thread | previous in thread | raw e-mail | index | archive | help
On 8/25/06, W. D. <WD@us-webmasters.com> wrote: > At 17:00 8/24/2006, Matti J. Karki wrote: > >On 8/24/06, Kyrre Nyg=E5rd <kyrreny@broadpark.no> wrote: > >> > >> Perhaps you could share with us whatever scripts you've written? > >> > > > >Totally forgot to include the actual intendation script. > > > >There should be a Python script attached to this mail. Please note, > >that the script is not a silver bullet! It was designed to clean up > >some pretty messed up C code. > > Comments would be nice. > No doubt :) There's no comments in the code, because usually my scripts are disposable. As I said, I do those case by case. The code should be quite clear for most parts, but the bunch of regexps at the beginning of the code do the following things: inbuffer =3D re.sub('\n +', '\n', inbuffer) # This strips all spaces from the beginning of every line of code. inbuffer =3D re.sub('\t+', '', inbuffer) # This does the same for tab chara= cters. inbuffer =3D re.sub('\) *?\n\{', ') {', inbuffer) # This moves all curly braces where I want them to be, i.e. at the end of the line. inbuffer =3D re.sub('\) *?{', ') {', inbuffer) # This removes all extra spaces between the closing bracket ) and the opening curly bracket {. inbuffer =3D re.sub('else *?\n{', 'else {\n', inbuffer) # This fixes curly brackets in the else clauses. inbuffer =3D re.sub('{ *?(.+?\n)', '{\n\g<1>', inbuffer) # Sometimes there is code where curly brackets are all in the same line and the contents of the brackets are between the brackets. This moves the contents to new line. inbuffer =3D re.sub('(\n.+?)}', '\g<1>\n}', inbuffer) # This takes care of the closing bracket at the above case. inbuffer =3D re.sub('\n +', '\n', inbuffer) # This just cleans up all spaces that may appear when arranging the code. The rest of the script is just a simple indenting machine, which indents the code using four spaces as a single level of indentation. The indentation will be placed after every newline character and indentation will be increased and decreased based on the occurrences of curly brackets. So, basically (and now I'm just trying to remember from top of my head, it's been some time, I dealt with this particular source code) the script will do the following steps with the code: step 1) the original piece of code int main() { char *c =3D {'a', 'b', 'c'}; print("hello, world"); if (true) { printf("ok"); } else { printf("umm..."); } } step 2) stripping all indentation int main() { char *c =3D {'a', 'b', 'c'}; print("hello, world"); if (true) { printf("ok"); } else { printf("umm..."); } } step 3) applying the rest of the regexp rules and indenting with the for lo= op int main() { char *c =3D { 'a', 'b', 'c' }; print("hello, world"); if (true) { printf("ok"); } else { printf("umm..."); } } Hope this clears my script a little bit. -Matti
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?1b15366e0608250531q6187d598h78b02e14ab4b5ac2>