Move some text-parsing methods to Util class.

master
Nekojimi 2 years ago
parent 1dba55620f
commit 0914cadaf5
  1. 33
      src/main/java/moe/nekojimi/chords/Format.java
  2. 43
      src/main/java/moe/nekojimi/chords/Util.java

@ -18,8 +18,6 @@ import javax.json.JsonObject;
class Format class Format
{ {
private static final Pattern SIZE_PATTERN = Pattern.compile("\\b([0-9]+\\.?[0-9]*)([kkMmGg])i?[bB]\\b");
private static final Pattern SAMPLE_RATE_PATTERN = Pattern.compile("\\b([0-9]+)k(b(ps?))?\\b");
private final String code; private final String code;
private final String extension; private final String extension;
@ -97,29 +95,9 @@ class Format
return size; return size;
// try to find eg. "1.32MiB" inside note // try to find eg. "1.32MiB" inside note
Matcher matcher = SIZE_PATTERN.matcher(note); long size = Util.parseSize(note);
if (matcher.find())
{
double value = Double.parseDouble(matcher.group(1));
String mag = matcher.group(2).toUpperCase();
long mult = 1;
switch (mag)
{
case "K":
mult = 1024;
break;
case "M":
mult = 1024 * 1024;
break;
case "G":
mult = 1024 * 1024 * 1024;
break;
}
value *= mult;
return (long) value;
} return size;
return -1;
} }
public int getSampleRate() public int getSampleRate()
@ -128,12 +106,7 @@ class Format
return samplerate; return samplerate;
// try to find eg. "51k" inside note // try to find eg. "51k" inside note
Matcher matcher = SAMPLE_RATE_PATTERN.matcher(note); return Util.parseSampleRate(note);
if (matcher.find())
{
return Integer.parseInt(matcher.group(1)) * 1000;
}
return -1;
} }
public void setSampleRate(int samplerate) public void setSampleRate(int samplerate)

@ -1,6 +1,8 @@
package moe.nekojimi.chords; package moe.nekojimi.chords;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/* /*
* Copyright (C) 2022 jimj316 * Copyright (C) 2022 jimj316
@ -25,6 +27,10 @@ import java.nio.ByteBuffer;
*/ */
public class Util public class Util
{ {
private static final Pattern SIZE_PATTERN = Pattern.compile("\\b([0-9]+\\.?[0-9]*)([kkMmGg])i?[bB]\\b");
private static final Pattern SAMPLE_RATE_PATTERN = Pattern.compile("\\b([0-9]+)k(b(ps?))?\\b");
public static String printSamples(ByteBuffer buf) public static String printSamples(ByteBuffer buf)
{ {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
@ -34,4 +40,41 @@ public class Util
} }
return sb.toString(); return sb.toString();
} }
public static int parseSampleRate(String input)
{
Matcher matcher = SAMPLE_RATE_PATTERN.matcher(input);
if (matcher.find())
{
return Integer.parseInt(matcher.group(1)) * 1000;
}
return -1;
}
public static long parseSize(String note)
{
Matcher matcher = SIZE_PATTERN.matcher(note);
if (matcher.find())
{
double value = Double.parseDouble(matcher.group(1));
String mag = matcher.group(2).toUpperCase();
long mult = 1;
switch (mag)
{
case "K":
mult = 1024;
break;
case "M":
mult = 1024 * 1024;
break;
case "G":
mult = 1024 * 1024 * 1024;
break;
}
value *= mult;
return (long) value;
}
return -1L;
}
} }

Loading…
Cancel
Save