import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
public class StringUtils
{
- private static final Pattern DELIMITERS_PATTERN = Pattern
- .compile(".*='[^']*(?!')");
-
private static final char PERCENT = '%';
private static final boolean DEBUG = false;
* Parses the input string into components separated by the delimiter. Unlike
* String.split(), this method will ignore occurrences of the delimiter which
* are nested within single quotes in name-value pair values, e.g. a='b,c'.
+ * New implementation to avoid Pattern for jalviewjs.
*
* @param input
* @param delimiter
public static String[] separatorListToArray(String input,
String delimiter)
{
- int seplen = delimiter.length();
- if (input == null || input.equals("") || input.equals(delimiter))
+ if (input == null
+ // these two shouldn't return null (one or two "" respectively)
+ || input.equals("") || input.equals(delimiter))
{
return null;
}
- List<String> jv = new ArrayList<>();
- int cp = 0, pos, escape;
- boolean wasescaped = false, wasquoted = false;
- String lstitem = null;
- while ((pos = input.indexOf(delimiter, cp)) >= cp)
+
+ final char escapeChar = '\\';
+ final char quoteChar = '\'';
+ int ilength = input.length();
+ int dlength = delimiter.length();
+ List<String> values = new ArrayList<>();
+
+ boolean escape = false;
+ boolean inquote = false;
+
+ int start = 0;
+ for (int i = 0; i < ilength; i++)
{
- escape = (pos > 0 && input.charAt(pos - 1) == '\\') ? -1 : 0;
- if (wasescaped || wasquoted)
+ if (!escape && !inquote && ilength >= i + dlength
+ && input.substring(i, i + dlength).equals(delimiter))
{
- // append to previous pos
- jv.set(jv.size() - 1, lstitem = lstitem + delimiter
- + input.substring(cp, pos + escape));
- }
- else
- {
- jv.add(lstitem = input.substring(cp, pos + escape));
+ // found a delimiter
+ values.add(input.substring(start, i));
+ i += dlength;
+ start = i;
+ continue;
}
- cp = pos + seplen;
- wasescaped = escape == -1;
- // last separator may be in an unmatched quote
- wasquoted = DELIMITERS_PATTERN.matcher(lstitem).matches();
- }
- if (cp < input.length())
- {
- String c = input.substring(cp);
- if (wasescaped || wasquoted)
+ char c = input.charAt(i);
+ if (c == escapeChar)
{
- // append final separator
- jv.set(jv.size() - 1, lstitem + delimiter + c);
+ escape = !escape;
+ continue;
}
- else
+ if (escape)
{
- if (!c.equals(delimiter))
- {
- jv.add(c);
- }
+ escape = false;
+ continue;
}
- }
- if (jv.size() > 0)
- {
- String[] v = jv.toArray(new String[jv.size()]);
- jv.clear();
- if (DEBUG)
+ if (c == quoteChar)
{
- jalview.bin.Console.errPrintln("Array from '" + delimiter
- + "' separated List:\n" + v.length);
- for (int i = 0; i < v.length; i++)
- {
- jalview.bin.Console.errPrintln("item " + i + " '" + v[i] + "'");
- }
+ inquote = !inquote;
}
- return v;
- }
- if (DEBUG)
- {
- jalview.bin.Console.errPrintln(
- "Empty Array from '" + delimiter + "' separated List");
}
- return null;
+ // add the last value
+ values.add(input.substring(start, ilength));
+
+ return values.toArray(new String[values.size()]);
}
/**
return min < text.length() + 1 ? min : -1;
}
+ public static boolean equalsIgnoreCase(String s1, String s2)
+ {
+ if (s1 == null || s2 == null)
+ {
+ return s1 == s2;
+ }
+ return s1.toLowerCase(Locale.ROOT).equals(s2.toLowerCase(Locale.ROOT));
+ }
+
public static int indexOfFirstWhitespace(String text)
{
- int index = -1;
- Pattern pat = Pattern.compile("\\s");
- Matcher m = pat.matcher(text);
- if (m.find())
+ // Rewritten to not use regex for Jalviewjs. Probably more efficient this
+ // way anyway.
+ if (text == null)
+ {
+ return -1;
+ }
+ for (int i = 0; i < text.length(); i++)
{
- index = m.start();
+ if (Character.isWhitespace(text.charAt(i)))
+ {
+ return i;
+ }
}
- return index;
+ return -1;
}
/*