-
-
Save komiya-atsushi/2925087 to your computer and use it in GitHub Desktop.
なんか構造が美しくないのでリファクタしたい・・ こんなのはどうかな?(komiya)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static List<String> tokenize(String str) { | |
if (str == null || str.isEmpty()) { | |
return Collections.emptyList(); | |
} | |
ArrayList<String> tokens = new ArrayList<String>(); | |
int i = 0; | |
char[] chars = str.toCharArray(); | |
while (i < chars.length) { | |
// Skip Whitespace | |
while (i < chars.length && chars[i] == ' ') { | |
i++; | |
} | |
if (i == chars.length) { | |
break; | |
} | |
StringBuilder buffer = new StringBuilder(); | |
char delimiter = ' '; | |
if (chars[i] == '"') { | |
delimiter = '"'; | |
i++; // SkipDQ | |
} | |
while (i < chars.length && chars[i] != delimiter) { | |
buffer.append(chars[i++]); | |
} | |
i++; // SkipDQ or Whitespace | |
if (buffer.length() != 0) { | |
String token = buffer.toString().trim(); | |
if (!token.isEmpty()) { | |
tokens.add(token); | |
} | |
} | |
} | |
return tokens; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment