content: package io.github.tapcard.emvnfccard.parser;
  
  import java.util.ArrayList;
  import java.util.List;
  
  /**
   * Part of Apache commons StringUtils
   */
  
  @SuppressWarnings({"JavadocReference", "SameParameterValue", "Convert2Diamond"})
  public class StringUtils {
  
      /**
       * <p>Splits the provided text into an array, separators specified.
       * This is an alternative to using StringTokenizer.</p>
       *
       * <p>The separator is not included in the returned String array.
       * Adjacent separators are treated as one separator.
       * For more control over the split use the StrTokenizer class.</p>
       *
       * <p>A {@code null} input String returns {@code null}.
       * A {@code null} separatorChars splits on whitespace.</p>
       *
       * <pre>
       * StringUtils.split(null, *)         = null
       * StringUtils.split("", *)           = []
       * StringUtils.split("abc def", null) = ["abc", "def"]
       * StringUtils.split("abc def", " ")  = ["abc", "def"]
       * StringUtils.split("abc  def", " ") = ["abc", "def"]
       * StringUtils.split("ab:cd:ef", ":") = ["ab", "cd", "ef"]
       * </pre>
       *
       * @param str  the String to parse, may be null
       * @param separatorChars  the characters used as the delimiters,
       *  {@code null} splits on whitespace
       * @return an array of parsed Strings, {@code null} if null String input
       */
      static String[] split(String str, String separatorChars) {
          return splitWorker(str, separatorChars, -1, false);
      }
  
      /**
       * Performs the logic for the {@code split} and
       * {@code splitPreserveAllTokens} methods that return a maximum array
       * length.
       *
       * @param str  the String to parse, may be {@code null}
       * @param separatorChars the separate character
       * @param max  the maximum number of elements to include in the
       *  array. A zero or negative value implies no limit.
       * @param preserveAllTokens if {@code true}, adjacent separators are
       * treated as empty token separators; if {@code false}, adjacent
       * separators are treated as one separator.
       * @return an array of parsed Strings, {@code null} if null String input
       */
      private static String[] splitWorker(String str, String separatorChars, int max, boolean preserveAllTokens) {
          // Performance tuned for 2.0 (JDK1.4)
          // Direct code is quicker than StringTokenizer.
          // Also, StringTokenizer uses isSpace() not isWhitespace()
  
          if (str == null) {
              return null;
          }
          int len = str.length();
          if (len == 0) {
              return ArrayUtils.EMPTY_STRING_ARRAY;
          }
          List<String> list = new ArrayList<String>();
          int sizePlus1 = 1;
          int i = 0, start = 0;
          boolean match = false;
          boolean lastMatch = false;
          if (separatorChars == null) {
              // Null separator means use whitespace
              while (i < len) {
                  if (Character.isWhitespace(str.charAt(i))) {
                      if (match || preserveAllTokens) {
                          lastMatch = true;
                          if (sizePlus1++ == max) {
                              i = len;
                              lastMatch = false;
                          }
                          list.add(str.substring(start, i));
                          match = false;
                      }
                      start = ++i;
                      continue;
                  }
                  lastMatch = false;
                  match = true;
                  i++;
              }
          } else if (separatorChars.length() == 1) {
              // Optimise 1 character case
              char sep = separatorChars.charAt(0);
              while (i < len) {
                  if (str.charAt(i) == sep) {
                      if (match || preserveAllTokens) {
                          lastMatch = true;
                          if (sizePlus1++ == max) {
                              i = len;
                              lastMatch = false;
                          }
                          list.add(str.substring(start, i));
                          match = false;
                      }
                      start = ++i;
                      continue;
                  }
                  lastMatch = false;
                  match = true;
                  i++;
              }
          } else {
              // standard case
              while (i < len) {
                  if (separatorChars.indexOf(str.charAt(i)) >= 0) {
                      if (match || preserveAllTokens) {
                          lastMatch = true;
                          if (sizePlus1++ == max) {
                              i = len;
                              lastMatch = false;
                          }
                          list.add(str.substring(start, i));
                          match = false;
                      }
                      start = ++i;
                      continue;
                  }
                  lastMatch = false;
                  match = true;
                  i++;
              }
          }
          if (match || preserveAllTokens && lastMatch) {
              list.add(str.substring(start, i));
          }
          return list.toArray(new String[list.size()]);
      }
  
      /**
       * <p>Removes control characters (char &lt;= 32) from both
       * ends of this String returning {@code null} if the String is
       * empty ("") after the trim or if it is {@code null}.
       *
       * <p>The String is trimmed using {@link String#trim()}.
       * Trim removes start and end characters &lt;= 32.
       * To strip whitespace use {@link #stripToNull(String)}.</p>
       *
       * <pre>
       * StringUtils.trimToNull(null)          = null
       * StringUtils.trimToNull("")            = null
       * StringUtils.trimToNull("     ")       = null
       * StringUtils.trimToNull("abc")         = "abc"
       * StringUtils.trimToNull("    abc    ") = "abc"
       * </pre>
       *
       * @param str  the String to be trimmed, may be null
       * @return the trimmed String,
       *  {@code null} if only chars &lt;= 32, empty or null String input
       * @since 2.0
       */
      static String trimToNull(String str) {
          String ts = trim(str);
          return isEmpty(ts) ? null : ts;
      }
  
      /**
       * <p>Removes control characters (char &lt;= 32) from both
       * ends of this String, handling {@code null} by returning
       * {@code null}.</p>
       *
       * <p>The String is trimmed using {@link String#trim()}.
       * Trim removes start and end characters &lt;= 32.
       * To strip whitespace use {@link #strip(String)}.</p>
       *
       * <p>To trim your choice of characters, use the
       * {@link #strip(String, String)} methods.</p>
       *
       * <pre>
       * StringUtils.trim(null)          = null
       * StringUtils.trim("")            = ""
       * StringUtils.trim("     ")       = ""
       * StringUtils.trim("abc")         = "abc"
       * StringUtils.trim("    abc    ") = "abc"
       * </pre>
       *
       * @param str  the String to be trimmed, may be null
       * @return the trimmed string, {@code null} if null String input
       */
      private static String trim(String str) {
          return str == null ? null : str.trim();
      }
  
      /**
       * <p>Checks if a CharSequence is empty ("") or null.</p>
       *
       * <pre>
       * StringUtils.isEmpty(null)      = true
       * StringUtils.isEmpty("")        = true
       * StringUtils.isEmpty(" ")       = false
       * StringUtils.isEmpty("bob")     = false
       * StringUtils.isEmpty("  bob  ") = false
       * </pre>
       *
       * <p>NOTE: This method changed in Lang version 2.0.
       * It no longer trims the CharSequence.
       * That functionality is available in isBlank().</p>
       *
       * @param cs  the CharSequence to check, may be null
       * @return {@code true} if the CharSequence is empty or null
       * @since 3.0 Changed signature from isEmpty(String) to isEmpty(CharSequence)
       */
      public static boolean isEmpty(CharSequence cs) {
          return cs == null || cs.length() == 0;
      }
  
  }
  