Skip to content

Commit c3c778c

Browse files
String.split looks seems better
1 parent 873662f commit c3c778c

File tree

1 file changed

+28
-21
lines changed

1 file changed

+28
-21
lines changed

src/main/java/me/itzg/helpers/modrinth/ProjectRef.java

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -42,32 +42,39 @@ public class ProjectRef {
4242
private final String versionNumber;
4343

4444
public static ProjectRef parse(String projectRef) {
45-
// First, try to split into potential loader prefix and the rest
46-
final int firstColon = projectRef.indexOf(':');
45+
final String[] parts = projectRef.split(":", 3);
46+
47+
// Handle cases with no colon
48+
if (parts.length == 1) {
49+
return new ProjectRef(parts[0], null, null);
50+
}
51+
52+
// Check if first part is a valid loader
4753
Loader loader = null;
48-
String rest = projectRef;
54+
int idIndex = 0;
55+
if (VALID_LOADERS.contains(parts[0].toLowerCase())) {
56+
loader = Loader.valueOf(parts[0].toLowerCase());
57+
idIndex = 1;
58+
}
4959

50-
if (firstColon > 0) {
51-
final String prefix = projectRef.substring(0, firstColon);
52-
if (VALID_LOADERS.contains(prefix.toLowerCase())) {
53-
loader = Loader.valueOf(prefix.toLowerCase());
54-
rest = projectRef.substring(firstColon + 1);
60+
// Handle remaining parts
61+
if (parts.length == 2) {
62+
// Either loader:id or id:version
63+
if (loader != null) {
64+
return new ProjectRef(parts[1], null, loader);
65+
} else {
66+
return new ProjectRef(parts[0], parts[1], null);
5567
}
68+
}
69+
else if (parts.length == 3) {
70+
// Must be loader:id:version
71+
if (loader == null) {
72+
throw new InvalidParameterException("Invalid loader in project reference: " + parts[0]);
73+
}
74+
return new ProjectRef(parts[1], parts[2], loader);
5675
}
57-
58-
// Now process the rest of the string
59-
final int versionSeparator = rest.indexOf(':');
60-
String idSlug;
61-
String version = null;
6276

63-
if (versionSeparator >= 0) {
64-
idSlug = rest.substring(0, versionSeparator);
65-
version = rest.substring(versionSeparator + 1);
66-
} else {
67-
idSlug = rest;
68-
}
69-
70-
return new ProjectRef(idSlug, version, loader);
77+
throw new InvalidParameterException("Invalid project reference: " + projectRef);
7178
}
7279

7380
/**

0 commit comments

Comments
 (0)