Strings { public static String quotedIdent(String s, char quote, boolean force) { String quoteS = Character.toString(quote); if (s.contains(quoteS)){ s = s.replaceAll( ("[" + quoteS + "]") , quoteS + quoteS ); } if (!force && !NodeToString.isReserved(s) && s.matches("[a-z][_a-z0-9$]*") ) { return s; } else { return quote + s + quote; } }  static List<String> entriesToString(Map<?, ?> map); static String nl(); static String join(Collection<?> strings); static String join(Object... strings); static String join(Collection<?> strings, String delimiter); static String repeatString(String str, int count); static List<String> stringAndSort(Collection<?> inputs); static List<String> stringAndSort(Map<?,?> inputs); static List<String> dumpResource(Class<?> forClass, String path); static List<String> dumpURLs(Enumeration<URL> urls); @SuppressWarnings("unused") // primarily useful in debuggers static String[] dumpException(Throwable t); static String toOctal(byte[] bytes); static String hex(byte[] bytes); static String hex(ByteSource byteSource); static String hex(byte[] bytes, int start, int length); static ByteSource parseHexWithout0x(String st); static ByteSource parseHex(String string); static List<String> readStream(InputStream is); static void readStreamTo(InputStream is, Appendable out, boolean keepNL); static String toString(Multimap<T,?> map); static String stripr(String input, String suffix); static List<String> dumpFile(File file); static String dumpFileToString(File file); static List<String> mapToString(Collection<?> collection); static boolean equalCharsets(Charset one, String two); static String formatMD5(byte[] md5, boolean toLowerCase); static String truncateIfNecessary(String str, int codePointCount); static String toBase64(byte[] bytes); static String toBase64(byte[] bytes, int offset, int length); static byte[] fromBase64(CharSequence cs); static String[] parseQualifiedName(String arg, int maxParts); static String quotedIdent(String s, char quote, boolean force); static String escapeIdentifier(String s); static final String NL; }
@Test public void quoteIdent(){ assertEquals("a", Strings.quotedIdent("a", '`', false) ); assertEquals("`a`", Strings.quotedIdent("a", '`', true) ); assertEquals("\"a\"", Strings.quotedIdent("a", '"', true) ); assertEquals("`3a`", Strings.quotedIdent("3a", '`', false)); assertEquals("\"3a\"", Strings.quotedIdent("3a", '"', false)); assertEquals("a$", Strings.quotedIdent("a$", '`', false)); assertEquals("`a-`", Strings.quotedIdent("a-", '`', false)); assertEquals("\"NUMERIC\"", Strings.quotedIdent("NUMERIC", '"', false)); assertEquals("`sum`", Strings.quotedIdent("sum", '`', false)); assertEquals("`test``quote`", Strings.quotedIdent("test`quote", '`', false)); assertEquals("period", Strings.quotedIdent("period", '.', false)); assertEquals(".pe..rio..d.", Strings.quotedIdent("pe.rio.d", '.', false)); }