Sfoglia il codice sorgente

Amélioration explode

Arthur Brandao 6 anni fa
parent
commit
88bbb6894e
1 ha cambiato i file con 21 aggiunte e 12 eliminazioni
  1. 21 12
      src/migl/lisp/LispImpl.java

+ 21 - 12
src/migl/lisp/LispImpl.java

@@ -48,22 +48,31 @@ public class LispImpl implements Lisp {
 		String[] tmp = expr.split("[ |\t]");
 		Stack<String> result = new Stack<>();
 		for(String str : tmp) {
-			if(str.length() > 0) {
-				if(str.charAt(0) == '(') {
-					result.add("" + str.charAt(0));
-					if(str.charAt(str.length() - 1) == ')') {
-						result.add(str.substring(1, str.length() - 1));
-						result.add("" + str.charAt(str.length() - 1));
-					} else {
-						result.add(str.substring(1));
+			char[] charArray = str.toCharArray();
+			StringBuilder builder = new StringBuilder();
+			for(char c : charArray) {
+				if(c == '(') {
+					//Si il y a une chaine dans le builder à mettre avant
+					if(builder.length() != 0) {
+						result.add(builder.toString());
+						builder = new StringBuilder();
 					}
-				} else if(str.charAt(str.length() - 1) == ')'){
-					result.add(str.substring(0, str.length() - 1));
-					result.add("" + str.charAt(str.length() - 1));
+					result.add("" + c);
+				} else if(c == ')') {
+					//Si il y a une chaine dans le builder à mettre avant
+					if(builder.length() != 0) {
+						result.add(builder.toString());
+						builder = new StringBuilder();
+					}
+					result.add("" + c);
 				} else {
-					result.add(str);
+					builder.append(c);
 				}
 			}
+			//Si il reste une chaine dans le builder à la fin
+			if(builder.length() != 0) {
+				result.add(builder.toString());
+			}
 		}
 		return result;
 	}