package foam.util;

public class StringUtil {
  /**
   * A string splitting mechanism which doesn't not split if the
   * separator has been escaped.
   */

  private static ThreadLocal<java.util.List<String>> storage =
    new ThreadLocal<java.util.List<String>>() {
      @Override
      protected java.util.List<String> initialValue() {
        return new java.util.ArrayList<String>(30);
      }
    };

  public static String[] split(String s, char separator) {
    java.util.List<String> list = storage.get();

    // TODO: Check if this retains the capacity of the list.
    // otherwise we might want to manage the inner array ourselves for
    // performance.
    list.clear();

    StringBuilder sb = new StringBuilder();
    boolean escaping = false;
    char escape = '\\';
    char prev;
    char[] cs = s.toCharArray();

    for ( int i = 0 ; i < cs.length ; i++ ) {
      char c = cs[i];

      if ( escaping ) {
        sb.append(c);
        escaping = false;
      } else if ( c == escape ) {
        escaping = true;
      } else if ( c == separator ) {
        list.add(sb.toString());
        sb.setLength(0);
      } else {
        sb.append(c);
      }
    }

    list.add(sb.toString());

    String[] result = new String[list.size()];

    return list.toArray(result);
  }
}

@@ -17,42 +17,12 @@
 l;%0A%0A
-public class StringUtil %7B%0A  
 /**%0A
-  
  * A
@@ -80,18 +80,16 @@
  if the%0A
-  
  * separ
@@ -116,12 +116,365 @@
 d.%0A 
-  
 */
+%0Apublic class StringUtil %7B%0A%0A  protected static ThreadLocal%3CStringBuilder%3E builder__ = new ThreadLocal%3CStringBuilder%3E() %7B%0A    @Override%0A    protected StringBuilder initialValue() %7B%0A      return new StringBuilder();%0A    %7D%0A    @Override%0A    public StringBuilder get() %7B%0A      StringBuilder sb = super.get();%0A      sb.setLength(0);%0A      return sb;%0A    %7D%0A  %7D;
 %0A%0A  
@@ -531,16 +531,18 @@
  storage
+__
  =%0A    n
@@ -720,79 +720,73 @@
   %7D%0A
+%0A
     
-%7D;%0A%0A  public static String%5B%5D split(String s, char separator
+  @Override%0A      public java.util.List%3CString%3E get(
 ) %7B%0A    
 java
@@ -781,16 +781,20 @@
 ) %7B%0A    
+   
+ 
 java.uti
@@ -812,30 +812,25 @@
 ng%3E 
-list = storage
+a = super
 .get();%0A
 %0A   
@@ -829,175 +829,167 @@
 ();%0A
-%0A
+  
     
-// TODO: Check if this retains the capacity of the list.%0A    // otherwise we might want to manage the inner array ourselves for%0A    // performance.%0A    list.clear
+  a.clear();%0A        return a;%0A      %7D%0A    %7D;%0A%0A  public static String%5B%5D split(String s, char separator) %7B%0A    java.util.List%3CString%3E list = storage__.get
 ();%0A
@@ -1012,33 +1012,29 @@
 er sb = 
-new StringBuilder
+builder__.get
 ();%0A    

Make StringUtil.split() use less memory.