diff --git a/app/src/processing/app/tools/AutoFormat.java b/app/src/processing/app/tools/AutoFormat.java index ba4fba89f..9fe956194 100644 --- a/app/src/processing/app/tools/AutoFormat.java +++ b/app/src/processing/app/tools/AutoFormat.java @@ -44,12 +44,12 @@ import java.io.*; public class AutoFormat implements Tool { Editor editor; - static final int BLOCK_MAXLEN = 1024; + static final int BLOCK_MAXLEN = 64000; StringBuffer strOut; int indentValue; String indentChar; - int EOF; + boolean EOF; CharArrayReader reader; int readCount, indexBlock, lineLength, lineNumber; char chars[]; @@ -62,14 +62,15 @@ public class AutoFormat implements Tool { int s_if_flg[]; int if_lev, if_flg, level; int ind[]; - int e_flg, paren; + boolean e_flg; + int paren; static int p_flg[]; char l_char, p_char; int a_flg, q_flg, ct; int s_tabs[][]; String w_if_, w_else, w_for, w_ds, w_case, w_cpp_comment, w_jdoc; int jdoc, j; - char string[]; + char buf[]; char cc; int s_flg; int peek; @@ -90,24 +91,24 @@ public class AutoFormat implements Tool { return "Auto Format"; } - public void comment() throws IOException { + private void comment() throws IOException { int save_s_flg; save_s_flg = s_flg; int done = 0; - c = string[j++] = getchr(); // extra char + c = buf[j++] = getchr(); // extra char while (done == 0) { - c = string[j++] = getchr(); - while ((c != '/') && (j < string.length)) { + c = buf[j++] = getchr(); + while ((c != '/') && (j < buf.length)) { if(c == '\n' || c == '\r') { lineNumber++; putcoms(); s_flg = 1; } - c = string[j++] = getchr(); + c = buf[j++] = getchr(); } //String tmpstr = new String(string); - if (j>1 && string[j-2] == '*') { + if (j>1 && buf[j-2] == '*') { done = 1; jdoc = 0; } @@ -120,22 +121,22 @@ public class AutoFormat implements Tool { } - public char get_string() throws IOException { + private char get_string() throws IOException { char ch; ch = '*'; while (true) { switch (ch) { default: - ch = string[j++] = getchr(); + ch = buf[j++] = getchr(); if (ch == '\\') { - string[j++] = getchr(); + buf[j++] = getchr(); break; } if (ch == '\'' || ch == '"') { - cc = string[j++] = getchr(); - while (cc != ch) { - if (cc == '\\') string[j++] = getchr(); - cc = string[j++] = getchr(); + cc = buf[j++] = getchr(); + while (!EOF && cc != ch) { + if (cc == '\\') buf[j++] = getchr(); + cc = buf[j++] = getchr(); } break; } @@ -151,23 +152,21 @@ public class AutoFormat implements Tool { } - public void indent_puts() { - string[j] = '\0'; + private void indent_puts() { + buf[j] = '\0'; if (j > 0) { if (s_flg != 0) { - if((tabs > 0) && (string[0] != '{') && (a_flg == 1)) { + if((tabs > 0) && (buf[0] != '{') && (a_flg == 1)) { tabs++; } p_tabs(); s_flg = 0; - if ((tabs > 0) && (string[0] != '{') && (a_flg == 1)) { + if ((tabs > 0) && (buf[0] != '{') && (a_flg == 1)) { tabs--; } a_flg = 0; } - String j_string = new String(string); - strOut.append(j_string.substring(0,j)); - for (int i=0; i= lineLength) { - for (int ib=0; ib= 'a' && r <= 'z') return(0); - if(r >= 'A' && r <= 'Z') return(0); - if(r >= '0' && r <= '9') return(0); - if(r == '_' || r == '&') return(0); - return (1); + r = buf[kk+keyword.length()]; + if(r >= 'a' && r <= 'z') return false; + if(r >= 'A' && r <= 'Z') return false; + if(r >= '0' && r <= '9') return false; + if(r == '_' || r == '&') return false; + return true; } - public int lookup_com (String keyword) + private int lookup_com (String keyword) { //char r; int l,kk; //,k,i; - String j_string = new String(string); + String j_string = new String(buf); if (j<1) return (0); kk=0; - while(string[kk] == ' ')kk++; + while(buf[kk] == ' ')kk++; l=0; l = j_string.indexOf(keyword); if (l<0 || l!=kk) @@ -441,7 +431,8 @@ public class AutoFormat implements Tool { indentChar = new String(" "); lineNumber = 0; - c_level = if_lev = level = e_flg = paren = 0; + e_flg = false; + c_level = if_lev = level = paren = 0; a_flg = q_flg = j = tabs = 0; if_flg = peek = -1; peekc = '`'; @@ -457,7 +448,7 @@ public class AutoFormat implements Tool { p_flg = new int[10]; s_tabs = new int[20][10]; - w_else = new String ("else"); + w_else = "else"; w_if_ = new String ("if"); w_for = new String ("for"); w_ds = new String ("default"); @@ -467,19 +458,13 @@ public class AutoFormat implements Tool { line_feed = new String ("\n"); // read as long as there is something to read - EOF = 0; // = 1 set in getchr when EOF + EOF = false; // = 1 set in getchr when EOF chars = new char[BLOCK_MAXLEN]; - string = new char[BLOCK_MAXLEN]; + buf = new char[BLOCK_MAXLEN]; try { // the whole process // open for input reader = new CharArrayReader(originalText.toCharArray()); - - // add buffering to that InputStream -// bin = new BufferedInputStream(in); - - for (int ib = 0; ib < BLOCK_MAXLEN; ib++) chars[ib] = '\0'; - lineLength = readCount = 0; // read up a block - remember how many bytes read readCount = reader.read(chars); @@ -489,13 +474,13 @@ public class AutoFormat implements Tool { lineNumber = 1; indexBlock = -1; j = 0; - while (EOF == 0) + while (!EOF) { c = getchr(); switch(c) { default: - string[j++] = c; + buf[j++] = c; if(c != ',') { l_char = c; @@ -504,33 +489,33 @@ public class AutoFormat implements Tool { case ' ': case '\t': - if(lookup(w_else) == 1) + if(lookup(w_else)) { gotelse(); - if(s_flg == 0 || j > 0)string[j++] = c; + if(s_flg == 0 || j > 0)buf[j++] = c; indent_puts(); s_flg = 0; break; } - if(s_flg == 0 || j > 0)string[j++] = c; + if(s_flg == 0 || j > 0)buf[j++] = c; break; case '\r': // for MS Windows 95 case '\n': lineNumber++; - if (EOF==1) + if (EOF) { break; } //String j_string = new String(string); e_flg = lookup(w_else); - if(e_flg == 1) gotelse(); + if(e_flg) gotelse(); if (lookup_com(w_cpp_comment) == 1) { - if (string[j] == '\n') + if (buf[j] == '\n') { - string[j] = '\0'; + buf[j] = '\0'; j--; } } @@ -539,7 +524,7 @@ public class AutoFormat implements Tool { //fprintf(outfil, line_feed); fprintf(line_feed); s_flg = 1; - if(e_flg == 1) + if(e_flg) { p_flg[level]++; tabs++; @@ -552,7 +537,7 @@ public class AutoFormat implements Tool { break; case '{': - if(lookup(w_else) == 1)gotelse(); + if(lookup(w_else))gotelse(); if (s_if_lev.length == c_level) { s_if_lev = PApplet.expand(s_if_lev); s_if_flg = PApplet.expand(s_if_flg); @@ -566,7 +551,7 @@ public class AutoFormat implements Tool { p_flg[level]--; tabs--; } - string[j++] = c; + buf[j++] = c; indent_puts(); getnl() ; indent_puts(); @@ -586,9 +571,10 @@ public class AutoFormat implements Tool { c_level--; if (c_level < 0) { - EOF = 1; + c_level = 0; + //EOF = true; //System.out.println("eof b"); - string[j++] = c; + buf[j++] = c; indent_puts(); break; } @@ -634,16 +620,16 @@ public class AutoFormat implements Tool { case '"': case '\'': - string[j++] = c; + buf[j++] = c; cc = getchr(); - while(cc != c) + while(!EOF && cc != c) { // max. length of line should be 256 - string[j++] = cc; + buf[j++] = cc; if(cc == '\\') { - cc = string[j++] = getchr(); + cc = buf[j++] = getchr(); } if(cc == '\n') { @@ -654,7 +640,7 @@ public class AutoFormat implements Tool { cc = getchr(); } - string[j++] = cc; + buf[j++] = cc; if(getnl() == 1) { l_char = cc; @@ -664,7 +650,7 @@ public class AutoFormat implements Tool { break; case ';': - string[j++] = c; + buf[j++] = c; indent_puts(); if(p_flg[level] > 0 && ind[level] == 0) { @@ -686,17 +672,17 @@ public class AutoFormat implements Tool { break; case '\\': - string[j++] = c; - string[j++] = getchr(); + buf[j++] = c; + buf[j++] = getchr(); break; case '?': q_flg = 1; - string[j++] = c; + buf[j++] = c; break; case ':': - string[j++] = c; + buf[j++] = c; peekc = getchr(); if(peekc == ':') { @@ -718,7 +704,7 @@ public class AutoFormat implements Tool { q_flg = 0; break; } - if(lookup(w_ds) == 0 && lookup(w_case) == 0) + if(!lookup(w_ds) && !lookup(w_case)) { s_flg = 0; indent_puts(); @@ -747,12 +733,12 @@ public class AutoFormat implements Tool { break; case '/': - string[j++] = c; + buf[j++] = c; peekc = getchr(); if(peekc == '/') { - string[j++] = peekc; + buf[j++] = peekc; peekc = '`'; peek = -1; cpp_comment(); @@ -770,38 +756,24 @@ public class AutoFormat implements Tool { } else { - if (j > 0) string[j--] = '\0'; + if (j > 0) buf[j--] = '\0'; if (j > 0) indent_puts(); - string[j++] = '/'; - string[j++] = '*'; + buf[j++] = '/'; + buf[j++] = '*'; peek = -1; peekc = '`'; comment(); break; } - case '#': - string[j++] = c; - cc = getchr(); - while(cc != '\n') - { - string[j++] = cc; - cc = getchr(); - } - string[j++] = cc; - s_flg = 0; - indent_puts(); - s_flg = 1; - break; - case ')': paren--; if (paren < 0) { - EOF = 1; + paren = 0;//EOF = true; //System.out.println("eof c"); } - string[j++] = c; + buf[j++] = c; indent_puts(); if(getnl() == 1) { @@ -821,9 +793,9 @@ public class AutoFormat implements Tool { break; case '(': - string[j++] = c; + buf[j++] = c; paren++; - if ((lookup(w_for) == 1)) + if ((lookup(w_for))) { c = get_string(); while(c != ';') c = get_string(); @@ -846,7 +818,7 @@ public class AutoFormat implements Tool { paren--; if (paren < 0) { - EOF = 1; + paren=0;//EOF = true; //System.out.println("eof d"); } indent_puts(); @@ -861,7 +833,7 @@ public class AutoFormat implements Tool { break; } - if(lookup(w_if_) == 1) + if(lookup(w_if_)) { indent_puts(); s_tabs[c_level][if_lev] = tabs;