1
0
mirror of https://github.com/arduino/Arduino.git synced 2025-02-06 01:08:25 +01:00

Updated StringReplacer.quotedSplit() to accept more than one quote char.

This commit is contained in:
Cristian Maglie 2013-07-17 14:36:20 +02:00
parent c70cba8fcd
commit ee8110e731

View File

@ -34,7 +34,7 @@ public class StringReplacer {
String res; String res;
// Recursive replace with a max depth of 10 levels. // Recursive replace with a max depth of 10 levels.
for (int i=0; i<10; i++) { for (int i = 0; i < 10; i++) {
// Do a replace with dictionary // Do a replace with dictionary
res = StringReplacer.replaceFromMapping(src, dict); res = StringReplacer.replaceFromMapping(src, dict);
if (!recursive) if (!recursive)
@ -45,30 +45,31 @@ public class StringReplacer {
} }
// Split the resulting string in arguments // Split the resulting string in arguments
return quotedSplit(src, '"', false); return quotedSplit(src, "\"'", false);
} }
public static String[] quotedSplit(String src, char escapeChar, public static String[] quotedSplit(String src, String quoteChars,
boolean acceptEmptyArguments) boolean acceptEmptyArguments)
throws Exception { throws Exception {
String quote = "" + escapeChar;
List<String> res = new ArrayList<String>(); List<String> res = new ArrayList<String>();
String escapedArg = null; String escapedArg = null;
boolean escaping = false; String escapingChar = null;
for (String i : src.split(" ")) { for (String i : src.split(" ")) {
if (!escaping) { if (escapingChar == null) {
if (!i.startsWith(quote)) { // If the first char is not an escape char..
String first = i.substring(0, 1);
if (!quoteChars.contains(first)) {
if (i.trim().length() != 0 || acceptEmptyArguments) if (i.trim().length() != 0 || acceptEmptyArguments)
res.add(i); res.add(i);
continue; continue;
} }
escaping = true; escapingChar = first;
i = i.substring(1); i = i.substring(1);
escapedArg = ""; escapedArg = "";
} }
if (!i.endsWith(quote)) { if (!i.endsWith(escapingChar)) {
escapedArg += i + " "; escapedArg += i + " ";
continue; continue;
} }
@ -76,11 +77,11 @@ public class StringReplacer {
escapedArg += i.substring(0, i.length() - 1); escapedArg += i.substring(0, i.length() - 1);
if (escapedArg.trim().length() != 0 || acceptEmptyArguments) if (escapedArg.trim().length() != 0 || acceptEmptyArguments)
res.add(escapedArg); res.add(escapedArg);
escaping = false; escapingChar = null;
} }
if (escaping) if (escapingChar != null)
throw new Exception("Invalid quoting: no closing '" + escapeChar + throw new Exception("Invalid quoting: no closing [" + escapingChar +
"' char found."); "] char found.");
return res.toArray(new String[0]); return res.toArray(new String[0]);
} }