StringUtil.java

/*
Copyright (c) 2025 James Ahlborn

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package com.healthmarketscience.jackcess.impl;

import java.text.BreakIterator;
import java.util.Locale;

/**
 * String manipulation utilities.
 * @author James Ahlborn
 */
public final class StringUtil
{
  private StringUtil() {}

  public static boolean isEmpty(String str) {
    return ((str == null) || str.isEmpty());
  }

  public static boolean isBlank(String str) {
    if(isEmpty(str)) {
      return true;
    }
    int len = str.length();
    for (int i = 0; i < len; i++) {
      if (!Character.isWhitespace(str.charAt(i))) {
        return false;
      }
    }
    return true;
  }

  public static boolean startsWithIgnoreCase(String str, String prefix) {
    return ((str != null) && (prefix != null) &&
            str.regionMatches(true, 0, prefix, 0, prefix.length()));
  }

  public static String trimToNull(String str) {
    if(str != null) {
      str = str.trim();
    }
    return isEmpty(str) ? null : str;
  }

  /**
   * Converts in the root locale, which is what a value compared against the
   * database rather than shown to a user needs.  The locale default would
   * turn an "i" into "\u0130" in Turkish.
   *
   * @return the given string in upper case, or null if it is null
   */
  public static String toUpperCase(String str) {
    return ((str != null) ? str.toUpperCase(Locale.ROOT) : null);
  }

  /**
   * The lower case counterpart of {@link #toUpperCase}.
   *
   * @return the given string in lower case, or null if it is null
   */
  public static String toLowerCase(String str) {
    return ((str != null) ? str.toLowerCase(Locale.ROOT) : null);
  }

  /**
   * @return the given string, cut to the given length if it is longer than
   *         that, and unchanged if it is null
   */
  public static String truncate(String str, int maxLength) {
    return (((str == null) || (str.length() <= maxLength)) ?
            str : str.substring(0, maxLength));
  }

  public static String replace(String str, String target, String replacement) {
    return isEmpty(str) ? str : str.replace(target, replacement);
  }

  public static String remove(String str, char c) {
    if(isEmpty(str) || str.indexOf(c) < 0) {
      return str;
    }
    return str.replace(String.valueOf(c), "");
  }

  public static String capitalizeFully(String text, Locale locale) {
    if (isEmpty(text)) {
      return text;
    }

    BreakIterator wordIterator = BreakIterator.getWordInstance(locale);
    wordIterator.setText(text);

    StringBuilder sb = new StringBuilder(text.length());
    int start = wordIterator.first();

    for (int end = wordIterator.next(); end != BreakIterator.DONE;
         start = end, end = wordIterator.next()) {

      String word = text.substring(start, end);

      if (!Character.isLetterOrDigit(word.charAt(0))) {
        sb.append(word);
        continue;
      }

      int cp = word.codePointAt(0);

      String first = new String(Character.toChars(cp)).toUpperCase(locale);
      first = new String(Character.toChars(
                             Character.toTitleCase(first.codePointAt(0))));

      sb.append(first)
        .append(word.substring(Character.charCount(cp)).toLowerCase(locale));
    }

    return sb.toString();
  }

}