code: public static void quote(StringBuilder buf, String arg)
     {
        if (!mightNeedQuotes(arg)) {
           buf.append(arg);
           return;
        }
        buf.append('"');
  
        // The length of the current run of backslashes.
        int nPending = 0;
  
        for (int i = 0; i < arg.length(); i++) {
           char c = arg.charAt(i);
  
           if (c == '\\') {
              nPending++;
           }
           else {
              if (c == '"') {
                 // Escape all the backslashes we've collected up till now.
                 for (int j = 0; j < nPending; j++) {
                    buf.append('\\');
                 }
                 // Escape the quote.
                 buf.append('\\');
              }
              nPending = 0;
           }
  
           buf.append(c);
        }
  
        // Escape all the backslashes that appear before the final closing quote.
        for (int j = 0; j < nPending; j++) {
           buf.append('\\');
        }
  
        buf.append('"');
     }
docs: Same as {@link #quote(String)} except appends to a {@code StringBuilder}.