Skip to content

Commit

Permalink
Made MidicaPLParser more robust against wrong charsets in text-based …
Browse files Browse the repository at this point in the history
…messages

The following scenario caused a fatal error in step 3:
1: importing a MIDI file with chinese UTF-8 characters as ISO
2: exporting the result as MPL
3: re-importing the resulting MPL
This was caused by a regex not working correctly with the wrong encoding.
Fixed by replacing the regex by startsWith() and endsWith()
  • Loading branch information
truj committed Oct 8, 2023
1 parent 34041b6 commit 9218960
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
Binary file modified midica.jar
Binary file not shown.
4 changes: 2 additions & 2 deletions src/org/midica/Midica.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ public class Midica {
* After switching to a new major version, this has to be set to "-1" manually, so that
* precommit.pl starts with "0" again.
*/
private static final int VERSION_MINOR = 10;
private static final int VERSION_MINOR = 11;

/** UNIX timestamp of the last commit */
public static final int COMMIT_TIME = 1696410911;
public static final int COMMIT_TIME = 1696764269;

/** Branch name. Automatically changed by precommit.pl */
public static final String BRANCH = "master";
Expand Down
22 changes: 14 additions & 8 deletions src/org/midica/file/read/MidicaPLParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,6 @@ public class MidicaPLParser extends SequenceParser {
private static Pattern varAssignPattern = null;
private static Pattern chordSepPattern = null;
private static Pattern compactChannelPattern = null;
private static Pattern compactOptPattern = null;
private static Pattern invalidNoteIdxPattern = null;
private static boolean isSoftKaraoke = false;
public static boolean isPlayingTupletBlock = false;
Expand Down Expand Up @@ -702,9 +701,6 @@ private void compilePatterns() {
compactChannelPattern = Pattern.compile(
"^(\\d{1,2}|" + Pattern.quote(P) + ")" + Pattern.quote(COMPACT_CHANNEL) + "$"
);
compactOptPattern = Pattern.compile(
"^" + Pattern.quote(COMPACT_OPT_OPEN) + "(.+)" + Pattern.quote(COMPACT_OPT_CLOSE) + "$"
);

// invalid note index inside of a block inside a pattern
invalidNoteIdxPattern = Pattern.compile("^\\[\\d+\\]$");
Expand Down Expand Up @@ -2679,7 +2675,7 @@ else if (OPT_SHIFT.equals(optName)) {
String compactElement = patLineTokens[j];

// option: (name=value)?
if (compactOptPattern.matcher(compactElement).matches())
if (hasCompactOptions(compactElement))
continue;

// bar line?
Expand Down Expand Up @@ -3277,7 +3273,7 @@ public String[] addShift(String[] tokens, int shift) throws ParseException {
String[] parts = compactElement.split(Pattern.quote(COMPACT_NOTE_SEP), 2);

// rest or option? - leave unchanged
if (REST.equals(parts[0]) || compactOptPattern.matcher(compactElement).matches()) {
if (REST.equals(parts[0]) || hasCompactOptions(compactElement)) {
shiftedNotes.add(compactElement);
}
else if (compactElement.startsWith(BAR_LINE)) {
Expand Down Expand Up @@ -4815,8 +4811,7 @@ else if (currentDuration > tremolo) {
*/
public ArrayList<CommandOption> parseCompactOptions(String compactElement, boolean isFake) throws ParseException {

Matcher optMatcher = compactOptPattern.matcher(compactElement);
if (optMatcher.matches()) {
if (hasCompactOptions(compactElement)) {
String optStr = compactElement.replaceFirst("^" + Pattern.quote(COMPACT_OPT_OPEN), "");
optStr = optStr.replaceFirst(Pattern.quote(COMPACT_OPT_CLOSE) + "$", "");
return parseOptions(optStr, isFake);
Expand All @@ -4825,6 +4820,17 @@ public ArrayList<CommandOption> parseCompactOptions(String compactElement, boole
return null;
}

/**
* Determins if the given compact element contains compact options.
* That means, it starts with **(** and ends with **)**.
*
* @param compactElement the compact element to check
* @return **true** if the compact element contains compact options
*/
private boolean hasCompactOptions(String compactElement) {
return compactElement.startsWith(COMPACT_OPT_OPEN) && compactElement.endsWith(COMPACT_OPT_CLOSE);
}

/**
* Parses the options part of a command.
*
Expand Down

0 comments on commit 9218960

Please sign in to comment.