Can I use part of Processing source code in my code?

But if you’d rather prefer to go w/ PApplet’s split() or join() original methods w/o modifying them, and considering its limited distribution, you could simply put those 2 static methods in their own “.java” file, wrapped up inside a class w/ some made up name. :file_cabinet:

In its header, you can post the link to the original “PApplet.java” file and just state its license is LGPL 2.1. :registered:

“MiniPApplet.java”:

/**
 This file contains 2 LGPL2.1 methods [split() & join()] from this link below:
 https://GitHub.com/processing/processing/blob/master/core/src/processing/core/PApplet.java
 */

package processing.core;

import java.util.List;
import java.util.ArrayList;

public class MiniPApplet {
  static public String[] split(String value, char delim) {
    if (value == null) return null;

    char chars[] = value.toCharArray();
    int splitCount = 0; //1;
    for (int i = 0; i < chars.length; i++) {
      if (chars[i] == delim) splitCount++;
    }

    if (splitCount == 0) {
      String splits[] = new String[1];
      splits[0] = value;
      return splits;
    }

    String splits[] = new String[splitCount + 1];
    int splitIndex = 0;
    int startIndex = 0;
    for (int i = 0; i < chars.length; i++) {
      if (chars[i] == delim) {
        splits[splitIndex++] =
          new String(chars, startIndex, i-startIndex);
        startIndex = i + 1;
      }
    }

    splits[splitIndex] =
      new String(chars, startIndex, chars.length-startIndex);

    return splits;
  }

  static public String[] split(String value, String delim) {
    List<String> items = new ArrayList<>();
    int index;
    int offset = 0;
    while ((index = value.indexOf(delim, offset)) != -1) {
      items.add(value.substring(offset, index));
      offset = index + delim.length();
    }
    items.add(value.substring(offset));
    String[] outgoing = new String[items.size()];
    items.toArray(outgoing);
    return outgoing;
  }

  static public String join(String[] list, char separator) {
    return join(list, String.valueOf(separator));
  }

  static public String join(String[] list, String separator) {
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < list.length; i++) {
      if (i != 0) sb.append(separator);
      sb.append(list[i]);
    }
    return sb.toString();
  }
}

You can now import those methods into your tool class like this: :toolbox:
import static processing.core.MiniPApplet.*;

1 Like