JAL-4059 Tidy getting the namespace, and save the namespace in the Jalview instance...
[jalview.git] / src / jalview / util / StringUtils.java
index 89bc36d..cc51d07 100644 (file)
@@ -25,14 +25,9 @@ import java.net.URLEncoder;
 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;
@@ -145,6 +140,7 @@ public class StringUtils
    * 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
@@ -153,70 +149,54 @@ public class StringUtils
   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));
+        // found a delimiter
+        values.add(input.substring(start, i));
+        i += dlength;
+        start = i;
+        continue;
       }
-      else
+      char c = input.charAt(i);
+      if (c == escapeChar)
       {
-        jv.add(lstitem = input.substring(cp, pos + escape));
+        escape = !escape;
+        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)
+      if (escape)
       {
-        // append final separator
-        jv.set(jv.size() - 1, lstitem + delimiter + c);
+        escape = false;
+        continue;
       }
-      else
+      if (c == quoteChar)
       {
-        if (!c.equals(delimiter))
-        {
-          jv.add(c);
-        }
+        inquote = !inquote;
       }
     }
-    if (jv.size() > 0)
-    {
-      String[] v = jv.toArray(new String[jv.size()]);
-      jv.clear();
-      if (DEBUG)
-      {
-        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] + "'");
-        }
-      }
-      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()]);
   }
 
   /**
@@ -597,14 +577,20 @@ public class StringUtils
 
   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)
     {
-      index = m.start();
+      return -1;
+    }
+    for (int i = 0; i < text.length(); i++)
+    {
+      if (Character.isWhitespace(text.charAt(i)))
+      {
+        return i;
+      }
     }
-    return index;
+    return -1;
   }
 
   /*