private static int removeClosingQuote(StringBuilder sb) {
    if (sb.length() < 2 || sb.charAt(sb.length() - 1) != Q) return -1;

    // Strip the closing quote and halve the number of trailing backslashes (if any):
    // they no more precede a double quote, hence lose their special treatment during unescaping.
    sb.setLength(sb.length() - 1);
    int numTrailingBackslashes = sb.length() - StringUtil.trimTrailing(sb, '\\').length();
    StringUtil.repeatSymbol(sb, '\\', numTrailingBackslashes / 2);

    if (numTrailingBackslashes % 2 == 1) {
      // retreat, it was a backslash-escaped quote, restore it
      StringUtil.repeatSymbol(sb, '\\', numTrailingBackslashes / 2 + 1);
      sb.append(Q);
      return -1;
    }

    return numTrailingBackslashes / 2;
  }
private static int removeClosingQuote(StringBuilder sb)