Simplify the split method

Use a regexp with two groups, instead of
doing some black magic manually.
master
jvoisin 5 years ago
parent e1583691d8
commit 29b083ddac
  1. 19
      airsonic-main/src/main/java/org/airsonic/player/util/StringUtil.java

@ -20,7 +20,6 @@
package org.airsonic.player.util; package org.airsonic.player.util;
import org.apache.commons.codec.binary.Hex; import org.apache.commons.codec.binary.Hex;
import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import java.io.*; import java.io.*;
@ -44,6 +43,8 @@ public final class StringUtil {
public static final String ENCODING_UTF8 = "UTF-8"; public static final String ENCODING_UTF8 = "UTF-8";
private static final Pattern SPLIT_PATTERN = Pattern.compile("\"([^\"]*)\"|(\\S+)");
private static final String[][] MIME_TYPES = { private static final String[][] MIME_TYPES = {
{"mp3", "audio/mpeg"}, {"mp3", "audio/mpeg"},
{"ogg", "audio/ogg"}, {"ogg", "audio/ogg"},
@ -195,16 +196,14 @@ public final class StringUtil {
return new String[0]; return new String[0];
} }
Pattern pattern = Pattern.compile("\".*?\"|\\S+"); List<String> result = new ArrayList<>();
Matcher matcher = pattern.matcher(input); Matcher m = SPLIT_PATTERN.matcher(input);
while (m.find()) {
List<String> result = new ArrayList<String>(); if (m.group(1) != null) {
while (matcher.find()) { result.add(m.group(1)); // quoted string
String element = matcher.group(); } else {
if (element.startsWith("\"") && element.endsWith("\"") && element.length() > 1) { result.add(m.group(2)); // unquoted string
element = element.substring(1, element.length() - 1);
} }
result.add(element);
} }
return result.toArray(new String[result.size()]); return result.toArray(new String[result.size()]);

Loading…
Cancel
Save