Coder Social home page Coder Social logo

cminify's Introduction

C Minifier

A basic, portable C/C++ minifier I initially wrote to minify shaders.

This basically trims whitespace and removes comments. It doesn't do any lexical analyzing, so complex or twisted cases may make it fail.

I hope it will be as useful to you as it is to me.

Requirements

  • Python 2.7.6 and higher

Use

  • python minifier.py source.c

Example

Given the following input file, test.c:

void	do_math(int * x) {
  *x += 5;
}

int	main(void) {
  int	result = -1, val = 4;

  do_math(&val);
  return result;
}

,

python minifier.py test.c will output the following result:

void do_math(int*x){*x+=5;}int main(void){int result=-1,val=4;do_math(&val);return result;}

License

GPL v3

cminify's People

Contributors

arlon1 avatar psettle avatar scylardor avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

cminify's Issues

negative number macro not working

this was the input

define MAXIMUM_SCALE 16383

define ESCAPE 256

define DONE -1

define FLUSH -2

this was the output:

define MAXIMUM_SCALE 16383

define ESCAPE 256

define DONE-1

define FLUSH-2

define BUFFER_SIZE 256

overly aggressive whitespace removal

from blog article published here,

minifying the following source code:

void printbin(long long val)
{
        int count = 63; 
        char binstr[65] = {0};

        if (!val) {
                printf("0b0");
                return;
        }   

        /* If negative, get the 2's complement */
        if (val < 0) {
                printf("-0b");
                val = ~val + 1;
         } else
                printf("0b");

         while (val && count >= 0) {
                binstr[count--] = "01"[val & 1]; 
                val >>= 1;
        }   

        count++;

        if (val)
                printf("OVERFLOW %s", binstr + count);
        else
                printf("%s", binstr + count);
}

produced:

void printbin(long long val){int count=63;char binstr[65]={0};if(!val){printf("0b0");return;}if(val<0){printf("-0b");val=~val+1;}elseprintf("0b");while(val && count>=0){binstr[count--]="01"[val & 1];val>>=1;}count++;if(val)printf("OVERFLOW %s",binstr+count);elseprintf("%s",binstr+count);}

it's missing a <SPACE> between the else and printf.

issue when there are multiple #define inside a function

Hi, I have seen some odd behavior, and it seems to me, that when there are two #defines inside a function, the minifier process puts them in one line. Outside a function there is no problem.

main() {
  #define ONE 1  
  #define TWO 2
}

Then:
./minifier.py test.c

main(){#define ONE 1#define TWO 2}

Which won't compile, with stray '#' in program error.

Don't compress macro names with their definitions

I'm currently writing a patch for this and I'll create a pull request tomorrow, but basically this:

#define A ((uint8_t) 3)

is turned into

#define A((uint8_t)3)

which is probably not what you wanted

And this:

#define a(c, x) bv(c, x)

becomes

#define a(c,x)bv(c,x)

which is almost certainly not what you want

the patch is almost complete, i just need to clean up the fix a little and write tests.

Two-word typenames are wrongly compressed

I usually typedef long double ldbl_t anyways, and I don't have a quick patch for this, but const long double t = ... becomes const long doublet=... and long long int a = ... is turned to long long inta=....

Cannot Run on macOS 10.12.4, python 2.7.10

When I run the command python minifier.py A.cpp
It returns:

Traceback (most recent call last):
  File "minifier.py", line 220, in <module>
    main()
  File "minifier.py", line 217, in main
    minify_source_file(args, filename)
  File "minifier.py", line 185, in minify_source_file
    print(final_string)
UnboundLocalError: local variable 'final_string' referenced before assignment

How to fix it?

Runs together identifiers across linebreaks

Input:
`#include <stdio.h>

int
main(int argc, char **argv) {
printf("hello world\n");
}
`

Output:
#include<stdio.h> intmain(int argc,char**argv){printf("hello world\n");}
Note "intmain". Placing the return type of a function on the line before is often useful, since it lets you search for the definition of the function by searching for it at the beginning of a line.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.