public CharSequence escape(CharSequence candidate, boolean complete) { StringBuilder sb = new StringBuilder(candidate); Predicate<Integer> needToBeEscaped; // Completion is protected by an opening quote: // Delimiters (spaces) don't need to be escaped, nor do other quotes, but everything else does. // Also, close the quote at the end if (openingQuote != null) { needToBeEscaped = i -> isRawEscapeChar(sb.charAt(i)) || String.valueOf(sb.charAt(i)).equals(openingQuote); } // No quote protection, need to escape everything: delimiter chars (spaces), quote chars // and escapes themselves else { needToBeEscaped = i -> isDelimiterChar(sb, i) || isRawEscapeChar(sb.charAt(i)) || isRawQuoteChar(sb.charAt(i)); } for (int i = 0; i < sb.length(); i++) { if (needToBeEscaped.test(i)) { sb.insert(i++, escapeChars[0]); } } if (openingQuote != null) { sb.insert(0, openingQuote); if (complete) { sb.append(openingQuote); } } return sb; }
/** * Returns true if the specified character is a whitespace parameter. Check to ensure that the character is not * escaped by any of {@link #getQuoteChars}, and is not escaped by ant of the {@link #getEscapeChars}, and * returns true from {@link #isDelimiterChar}. * * @param buffer The complete command buffer * @param pos The index of the character in the buffer * @return True if the character should be a delimiter */ public boolean isDelimiter(final CharSequence buffer, final int pos) { return !isQuoted(buffer, pos) && !isEscaped(buffer, pos) && isDelimiterChar(buffer, pos); }