Strings { public static String[] parseQualifiedName(String arg, int maxParts) { assert maxParts > 0 : maxParts; String[] result = new String[maxParts]; int resIndex = 0; char lastQuote = 0; int prevEnd = 0; for(int i = 0; i < arg.length(); ++i) { char c = arg.charAt(i); boolean take = false; boolean toLower = true; if(lastQuote != 0) { if(c == lastQuote) { take = true; toLower = false; lastQuote = 0; } } else if(c == '"' || c == '`') { lastQuote = c; prevEnd = i + 1; } else if(c == '.') { take = true; } if(take) { if(prevEnd < i) { result[resIndex++] = consumeIdentifier(arg, prevEnd, i, toLower); } prevEnd = i + 1; } } if((resIndex < maxParts) && (prevEnd < arg.length())) { result[resIndex++] = consumeIdentifier(arg, prevEnd, arg.length(), lastQuote == 0); } int diff = maxParts - resIndex; if(diff > 0) { System.arraycopy(result, 0, result, maxParts - resIndex, resIndex); for(int i = 0; i < diff; ++i) { result[i] = ""; } } return result; }  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 parseQualifiedNameMaxTwo() { String[][][] cases = { { { "test.t" }, { "test", "t" } }, { { "t.test" }, { "t", "test" } }, { { "t" }, { "", "t" } }, { { "" }, { "", "" } }, { { "x.y.z" }, { "x", "y" } }, { { "test." }, { "", "test" } }, { { "\"t\"" }, { "", "t" } }, { { "test.\"t\"" }, { "test", "t" } }, { { "\"test\".\"t\"" }, { "test", "t" } }, { { "\"te.st\".\"t\"" }, { "te.st", "t" } }, { { "`t`" }, { "", "t" } }, { { "test.`t`" }, { "test", "t" } }, { { "`test`.`t`" }, { "test", "t" } }, { { "`te.st`.`t`" }, { "te.st", "t" } }, { { "tEsT.T" }, { "test", "t" } }, { { "\"tEsT\".\"T\"" }, { "tEsT", "T" } }, { { "`test`.\"t\"" }, { "test", "t" } }, { { "`test\"`.\"t`\"" }, { "test\"", "t`" } }, { { "test.\"T" }, { "test", "T" } }, { { "'t'" }, { "", "'t'" } }, { { " test\t. t " }, { " test\t", " t " } }, }; for(String[][] c : cases) { String arg = c[0][0]; String[] actual = Strings.parseQualifiedName(arg, 2); assertEquals(arg, Arrays.asList(c[1]), Arrays.asList(actual)); } }
@Test public void parseQualifiedNameMaxThree() { String[][][] cases = { { { "test.t.id" }, { "test", "t", "id" } }, { { "id.t.test" }, { "id", "t", "test" } }, { { "test.t" }, { "", "test", "t" } }, { { "t" }, { "", "", "t" } }, { { "" }, { "", "", "" } }, { { "x.y.z.w" }, { "x", "y", "z" } }, { { "test.t." }, { "", "test", "t" } }, { { "test." }, { "", "", "test" } }, { { "\"a\".\"b\".\"c\"" }, { "a", "b", "c" } }, { { "a.\"b\".c" }, { "a", "b", "c" } }, }; for(String[][] c : cases) { String arg = c[0][0]; String[] actual = Strings.parseQualifiedName(arg, 3); assertEquals(arg, Arrays.asList(c[1]), Arrays.asList(actual)); } }