Last active
August 29, 2015 14:08
-
-
Save ClickerMonkey/1317dca151453e65ee81 to your computer and use it in GitHub Desktop.
Transforms Java code to be as many lines as possible.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
public class Elongate | |
{ | |
public static void main( String[] args ) | |
{ | |
Scanner in = new Scanner( System.in ); | |
StringBuilder code = new StringBuilder(); | |
while (in.hasNextLine()) | |
{ | |
code.append( in.nextLine() + "\n" ); | |
} | |
in.close(); | |
elongate( code ); | |
} | |
public static void elongate( StringBuilder x ) | |
{ | |
State s = State.DEFAULT; | |
char prevc = 0; | |
for (int i = 0; i < x.length(); i++) | |
{ | |
final char c = x.charAt(i); | |
final State n = getState( s, c ); | |
switch (n) { | |
case BLOCK_COMMENT: | |
case BLOCK_COMMENT_ASTERISK: | |
case BLOCK_COMMENT_END: | |
case LINE_COMMENT: | |
case SLASH: | |
break; | |
case DOT: | |
System.out.println(); | |
System.out.print(c); | |
break; | |
case DOT_DOT: | |
System.out.print(c); | |
break; | |
case SPLITTABLE: | |
if (s == State.SLASH) { | |
System.out.println('/'); | |
} | |
if (!Character.isWhitespace( c )) { | |
if (!isSpecial( prevc, c, "++", "--", "+=", "*=", "-=", "/=", "|=", "==", "&=", "&&", "||", "<=", ">=", "!=", "%=", "^=", "~=", ".." )) { | |
System.out.println(); | |
} | |
System.out.print(c); | |
} | |
break; | |
default: | |
if (s == State.SLASH) { | |
System.out.println('/'); | |
} | |
if (s == State.SPLITTABLE || s == State.DOT) { | |
System.out.println(); | |
} | |
System.out.print(c); | |
break; | |
} | |
s = n; | |
prevc = c; | |
} | |
} | |
public static enum State | |
{ | |
DEFAULT, | |
STRING, | |
STRING_ESCAPE, | |
CHAR, | |
CHAR_ESCAPE, | |
SPLITTABLE, | |
SLASH, | |
LINE_COMMENT, | |
BLOCK_COMMENT, | |
BLOCK_COMMENT_ASTERISK, | |
BLOCK_COMMENT_END, | |
DOT, | |
DOT_DOT, | |
} | |
private static State getInitialState(char c, State defaultNext) | |
{ | |
if (c == '/') return State.SLASH; | |
if (c == '"') return State.STRING; | |
if (c == '\'') return State.CHAR; | |
if (c == '.') return State.DOT; | |
if (" \t\n\r,()[]{}<>=+-/*&^%$#@!;:?~".indexOf( c ) != -1) return State.SPLITTABLE; | |
return defaultNext; | |
} | |
public static State getState(State currentState, char c) | |
{ | |
switch (currentState) { | |
case DOT: | |
if (c == '.') { | |
return State.DOT_DOT; | |
} | |
case SPLITTABLE: | |
case DEFAULT: | |
return getInitialState( c, State.DEFAULT ); | |
case DOT_DOT: | |
return State.SPLITTABLE; | |
case CHAR: | |
if (c == '\\') return State.CHAR_ESCAPE; | |
if (c == '\'') return State.DEFAULT; | |
break; | |
case CHAR_ESCAPE: | |
return State.CHAR; | |
case STRING_ESCAPE: | |
return State.STRING; | |
case STRING: | |
if (c == '\\') return State.STRING_ESCAPE; | |
if (c == '"') return State.DEFAULT; | |
return State.STRING; | |
/* Block Comment, Line Comment, or Division */ | |
case SLASH: | |
if (c == '*') return State.BLOCK_COMMENT; | |
if (c == '/') return State.LINE_COMMENT; | |
return getInitialState( c, State.DEFAULT ); | |
/* Line Comment */ | |
case LINE_COMMENT: | |
if (c == '\r' || c == '\n') return State.DEFAULT; | |
return State.LINE_COMMENT; | |
/* Block Comment */ | |
case BLOCK_COMMENT: | |
if (c == '*') return State.BLOCK_COMMENT_ASTERISK; | |
return State.BLOCK_COMMENT; | |
case BLOCK_COMMENT_ASTERISK: | |
if (c == '/') return State.BLOCK_COMMENT_END; | |
return State.BLOCK_COMMENT; | |
case BLOCK_COMMENT_END: | |
return getInitialState( c, State.DEFAULT ); | |
} | |
return currentState; | |
} | |
public static boolean isSpecial(char a, char b, String ... special) | |
{ | |
for (String x : special) | |
{ | |
if (x.charAt( 0 ) == a && x.charAt( 1 ) == b) | |
{ | |
return true; | |
} | |
} | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment