Skip to content

Commit

Permalink
Speedy configuration loading
Browse files Browse the repository at this point in the history
No need to use regex for this little INI-like format
  • Loading branch information
ME1312 committed Jan 19, 2024
1 parent 258368c commit ba3d615
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 25 deletions.
6 changes: 3 additions & 3 deletions java/vanillacord/data/Digest.java
Expand Up @@ -8,7 +8,7 @@ public static boolean isEqual(byte[] digest, String target) {
while (d != 0 && t != 0) {
i = (c = target.charAt(--t)) - ((c >= 'a')? 87 : ((c >= 'A')? 55 : 48));
if (t != 0) {
i += ((c = target.charAt(--t)) - ((c >= 'a')? 87 : ((c >= 'A')? 55 : 48))) * 16;
i += ((c = target.charAt(--t)) - ((c >= 'a')? 87 : ((c >= 'A')? 55 : 48))) << 4;
}
if (digest[--d] != (byte) i) break;
if (d == 0 && t == 0) return true;
Expand All @@ -19,8 +19,8 @@ public static boolean isEqual(byte[] digest, String target) {
public static String toHex(byte[] array) {
StringBuilder hex = new StringBuilder(array.length * 2);
for (int c, i = 0; i != array.length;) {
hex.append((char) ((((c = array[i++] & 0xFF) > 0x9F)? 87 : 48) + (c / 16)));
hex.append((char) ((((c %= 16) > 9)? 87 : 48) + c));
hex.append((char) ((((c = array[i++] & 0xFF) > 0x9F)? 87 : 48) + (c >>> 4)));
hex.append((char) ((((c &= 0x0F) > 0x09)? 87 : 48) + c));
}
return hex.toString();
}
Expand Down
30 changes: 10 additions & 20 deletions java/vanillacord/server/VanillaCord.java
Expand Up @@ -25,32 +25,22 @@ public class VanillaCord {

try {
if (file.isFile()) {
final Pattern format = Pattern.compile("^\\s*([^#].*?)(\\s*)=");
try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file)))) {
for (String value; (value = reader.readLine()) != null;) {
final Matcher header = format.matcher(value);
if (header.find()) {
int i = header.end();
whitespace: for (int c, maximum = Math.min(i + header.group(2).length(), value.length()); i < maximum;) {
switch (c = value.codePointAt(i)) {
case ' ':
case '\t':
i += Character.charCount(c);
continue;
default:
break whitespace;
}
try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), UTF_8))) {
for (String line; (line = reader.readLine()) != null;) {
int start, end;
if (line.lastIndexOf('#', 0) < 0 && (start = line.indexOf('=')) >= 0) {
for (end = start + 1; start != 0 && line.codePointBefore(start) == ' '; --start) {
if (end != line.length() && line.codePointAt(end) == ' ') ++end;
}
value = value.substring(i);
switch (header.group(1).toLowerCase(Locale.ROOT)) {
switch (line.substring(0, start).toLowerCase(Locale.ROOT)) {
case "version":
version = Double.parseDouble(value);
version = Double.parseDouble(line.substring(end));
break;
case "forwarding":
forwarding = value;
forwarding = line.substring(end);
break;
case "seecret":
if (value.length() > 1) seecrets.add(value);
if (line.length() > end) seecrets.add(line.substring(end));
break;
}
}
Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Expand Up @@ -108,10 +108,10 @@
<goal>run</goal>
</goals>
<configuration>
<tasks>
<target>
<mkdir dir="${project.build.directory}" />
<copy file="${basedir}/LICENSE" todir="${project.build.directory}/classes" />
</tasks>
</target>
</configuration>
</execution>
</executions>
Expand Down

0 comments on commit ba3d615

Please sign in to comment.