Java String Quote quoteIfNeeded(StringBuilder buf, String str, String delim)

Here you can find the source of quoteIfNeeded(StringBuilder buf, String str, String delim)

Description

Append into buf the provided string, adding quotes if needed.

License

Open Source License

Parameter

Parameter Description
buf the buffer to append to
str the string to possibly quote
delim the delimiter characters that will trigger automatic quoting

Declaration

public static void quoteIfNeeded(StringBuilder buf, String str, String delim) 

Method Source Code

//package com.java2s;
//  are made available under the terms of the Eclipse Public License v1.0

public class Main {
    private static final char UNICODE_TAG = 0xFFFF;
    private static final char[] escapes = new char[32];

    /**//from  w ww  .  j av  a 2 s.  c o  m
     * Append into buf the provided string, adding quotes if needed.
     * <p>
     * Quoting is determined if any of the characters in the <code>delim</code> are found in the input <code>str</code>.
     * 
     * @param buf
     *            the buffer to append to
     * @param str
     *            the string to possibly quote
     * @param delim
     *            the delimiter characters that will trigger automatic quoting
     */
    public static void quoteIfNeeded(StringBuilder buf, String str, String delim) {
        if (str == null) {
            return;
        }
        // check for delimiters in input string
        int len = str.length();
        if (len == 0) {
            return;
        }
        int ch;
        for (int i = 0; i < len; i++) {
            ch = str.codePointAt(i);
            if (delim.indexOf(ch) >= 0) {
                // found a delimiter codepoint. we need to quote it.
                quote(buf, str);
                return;
            }
        }

        // no special delimiters used, no quote needed.
        buf.append(str);
    }

    /**
     * Simple quote of a string, escaping where needed.
     * 
     * @param buf
     *            the StringBuilder to append to
     * @param str
     *            the string to quote
     */
    public static void quote(StringBuilder buf, String str) {
        buf.append('"');
        escape(buf, str);
        buf.append('"');
    }

    public static void escape(StringBuilder buf, String str) {
        for (char c : str.toCharArray()) {
            if (c >= 32) {
                // non special character
                if ((c == '"') || (c == '\\')) {
                    buf.append('\\');
                }
                buf.append(c);
            } else {
                // special characters, requiring escaping
                char escaped = escapes[c];

                // is this a unicode escape?
                if (escaped == UNICODE_TAG) {
                    buf.append("\\u00");
                    if (c < 0x10) {
                        buf.append('0');
                    }
                    buf.append(Integer.toString(c, 16)); // hex
                } else {
                    // normal escape
                    buf.append('\\').append(escaped);
                }
            }
        }
    }
}

Related

  1. quoteIfCeylonKeyword(String name)
  2. quoteIfNeeded(String id)
  3. quoteIfNeeded(String input)
  4. quoteIfNeeded(String source)
  5. quoteIfNeeded(String value)
  6. quoteIfNotNull(String str)
  7. quoteIfNotNull(String text)
  8. quoteIfNotNull(StringBuilder sb, String val)
  9. quoteIfString(Object obj)