Here you can find the source of quoteContentsOfNextBracket(String piece)
public static String quoteContentsOfNextBracket(String piece)
//package com.java2s; //License from project: Open Source License public class Main { private static final String START_ALREADY_PROCESSED_LEAVE_ALONE = ";start-already-processed-leave-alone"; private static final String END_ALREADY_PROCESSED_LEAVE_ALONE = ";end-already-processed-leave-alone"; public static String quoteContentsOfNextBracket(String piece) { // e.g. ... [a b] ... -> ... ["a" "b"] ... if (piece.trim().isEmpty()) { return ""; }/*from w w w. j av a 2s . c o m*/ int openBracket = openBracket(piece, 0); if (openBracket < 0) { return piece; } int closeBracket = closeBracket(piece, openBracket + 1); if (closeBracket < 0) { return piece; } return piece.substring(0, openBracket + 1) + quoteEach(piece.substring(openBracket + 1, closeBracket)) + piece.substring(closeBracket); } public static int openBracket(String s, int start) { // returns location within s of the the first open bracket // this ignores bracketed sub-expressions in parenthesis // and those bracketed by START_ALREADY_PROCESSED_LEAVE_ALONE and END_ALREADY_PROCESSED_LEAVE_ALONE // returns a negative number if there is no open bracket int skipStart = s.indexOf(START_ALREADY_PROCESSED_LEAVE_ALONE); int skipEnd = skipStart < 0 ? -1 : s.indexOf( END_ALREADY_PROCESSED_LEAVE_ALONE, skipStart); int skipCount = 0; for (int i = start; i < s.length(); i++) { if (i == skipStart) { i = skipEnd + END_ALREADY_PROCESSED_LEAVE_ALONE.length(); } char c = s.charAt(i); if (c == '[' && !isInAComment(s, i)) { if (skipCount == 0) { return i; } } else if (c == '(' && !isInAComment(s, i)) { skipCount++; } else if (c == ')' && !isInAComment(s, i)) { skipCount--; } } return -1; } public static int closeBracket(String s, int start) { // returns location within s of the matching closing bracket // this ignores bracketed sub-expressions // returns a negative number if there is no closing bracket int skipCount = 0; for (int i = start; i < s.length(); i++) { char c = s.charAt(i); if (c == ']' && !isInAComment(s, i)) { if (skipCount == 0) { return i; } else { skipCount--; } } else if (c == '[' && !isInAComment(s, i)) { skipCount++; } } return -1; } public static String quoteEach(String string) { String[] pieces = string.split("(\\s)+"); StringBuffer result = new StringBuffer(); for (String piece : pieces) { if (piece.indexOf("\"") >= 0) { // already quoted return string; } if (!piece.isEmpty()) { result.append(" \""); result.append(piece); result.append("\"\n"); } } return result.toString(); } public static boolean isInAComment(String code, int index) { // if a semi-colon is before the current index on the same line returns true for (int i = index - 1; i >= 0; i--) { char character = code.charAt(i); if (character == ';') { // and the ; is not proceeded by a ' or a \ if (i == 0 || i > 0 && code.charAt(i - 1) != '\'' && code.charAt(i - 1) != '\\' && !isInAString(code, i)) { return true; } } else if (character == '\r' || character == '\n') { return false; } } return false; } public static boolean isInAString(String code, int index) { // true if the current index is in a quoted string // if there are an odd number of quotes on the line before or after the index then it is in a string boolean stringStarted = false; char kindOfQuote = 0; for (int i = 0; i < index; i++) { char character = code.charAt(i); if (stringStarted) { if (character == kindOfQuote && (kindOfQuote != '"' || i == 0 || // don't count \" code.charAt(i - 1) != '\\')) { stringStarted = false; } } else { if (character == ';') { // NetLogo's comment so ignore this line i = code.indexOf('\n', i); if (i < 0) { // comment is last line -- no new line return stringStarted; } } else if (character == '\'') { kindOfQuote = '\''; stringStarted = true; } else if (character == '"' && // don't count \" (i == 0 || code.charAt(i - 1) != '\\')) { kindOfQuote = '"'; stringStarted = true; } } } return stringStarted; } }