-
-
Save komiya-atsushi/2925028 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) { | |
// TODO while ループの外に出したい。 | |
StringBuilder buffer = new StringBuilder(); | |
while (chars[i] == ' ') { | |
i++; | |
} | |
if (chars[i] == '"') { // フレーズの場合は空白ではない文字まで繰り返す | |
i++; // SkipDQ | |
while (i < chars.length && chars[i] != '"') { | |
buffer.append(chars[i++]); | |
} | |
i++; // SkipDQ | |
} else { | |
while (i < chars.length && (chars[i] != ' ')) { | |
buffer.append(chars[i++]); | |
} | |
} | |
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