Fix and simplify user id parsing

This commit is contained in:
Dominik Schürmann
2014-01-31 17:55:24 +01:00
parent 3c897d68ff
commit d8a91f15df
6 changed files with 50 additions and 54 deletions

View File

@@ -539,9 +539,23 @@ public class PgpKeyHelper {
* @return array with naming (0), email (1), comment (2)
*/
public static String[] splitUserId(String userId) {
String[] result = new String[] { "", "", "" };
String[] result = new String[] { null, null, null };
Pattern withComment = Pattern.compile("^(.*) \\((.*)\\) <(.*)>$");
if (userId == null || userId.equals("")) {
return result;
}
/*
* User ID matching:
* http://fiddle.re/t4p6f
*
* test cases:
* "Max Mustermann (this is a comment) <max@example.com>"
* "Max Mustermann <max@example.com>"
* "Max Mustermann (this is a comment)"
* "Max Mustermann [this is nothing]"
*/
Pattern withComment = Pattern.compile("^(.*?)(?: \\((.*)\\))?(?: <(.*)>)?$");
Matcher matcher = withComment.matcher(userId);
if (matcher.matches()) {
result[0] = matcher.group(1);
@@ -550,13 +564,6 @@ public class PgpKeyHelper {
return result;
}
Pattern withoutComment = Pattern.compile("^(.*) <(.*)>$");
matcher = withoutComment.matcher(userId);
if (matcher.matches()) {
result[0] = matcher.group(1);
result[1] = matcher.group(2);
return result;
}
return result;
}