<commit_before>//
// $Id$
//
// samskivert library - useful routines for java programs
// Copyright (C) 2001-2010 <NAME>, et al.
//
// This library is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

package com.samskivert.util;

import java.awt.geom.Dimension2D;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;

import java.io.UnsupportedEncodingException;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

import java.net.URLEncoder;
import java.net.URLDecoder;

import java.text.NumberFormat;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import java.util.Arrays;
import java.util.Collection;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Locale;
import java.util.StringTokenizer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.samskivert.annotation.ReplacedBy;

/**
 * String related utility functions.
 */
public class StringUtil
{
    /**
     * Used to format objects in {@link #listToString(Object,StringUtil.Formatter)}.
     */
    public static class Formatter
    {
        /** Formats the supplied object into a string. */
        public String toString (Object object) {
            return object == null ? "null" : object.toString();
        }

        /** Returns the string that will be prepended to a formatted list. */
        public String getOpenBox () {
            return "(";
        }

        /** Returns the string that will be appended to a formatted list. */
        public String getCloseBox () {
            return ")";
        }
    }

    /**
     * @return true if the string is null or empty, false otherwise.
     *
     * @deprecated use isBlank instead.
     */
    @Deprecated
    public static boolean blank (String value)
    {
        return isBlank(value);
    }

    /**
     * @return true if the string is null or consists only of whitespace, false otherwise.
     */
    public static boolean isBlank (String value)
    {
        for (int ii = 0, ll = (value == null) ? 0 : value.length(); ii < ll; ii++) {
            if (!Character.isWhitespace(value.charAt(ii))) {
                return false;
            }
        }
        return true;
    }

    /**
     * Calls {@link String#trim} on non-null values, returns null for null values.
     */
    public static String trim (String value)
    {
        return (value == null) ? null : value.trim();
    }

    /**
     * @return the supplied string if it is non-null, "" if it is null.
     */
    public static String deNull (String value)
    {
        return (value == null) ? "" : value;
    }

    /**
     * Truncate the specified String if it is longer than maxLength.
     */
    public static String truncate (String s, int maxLength)
    {
        return truncate(s, maxLength, "");
    }

    /**
     * Returns the string if it is non-blank (see {@link #isBlank}), the default value otherwise.
     */
    @ReplacedBy("com.google.common.base.Object.firstNonNull()")
    public static String getOr (String value, String defval)
    {
        return isBlank(value) ? defval : value;
    }

    /**
     * Truncate the specified String if it is longer than maxLength.  The string will be truncated
     * at a position such that it is maxLength chars long after the addition of the 'append'
     * String.
     *
     * @param append a String to add to the truncated String only after truncation.
     */
    public static String truncate (String s, int maxLength, String append)
    {
        if ((s == null) || (s.length() <= maxLength)) {
            return s;
        } else {<commit_msg>J<NAME> points out that I made a booboo here. Our getOr() will skip the first argument if it is "blank" (null, empty, or all whitespace), not just null.<commit_after>//
// $Id$
//
// samskivert library - useful routines for java programs
// Copyright (C) 2001-2010 <NAME>, et al.
//
// This library is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

package com.samskivert.util;

import java.awt.geom.Dimension2D;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;

import java.io.UnsupportedEncodingException;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

import java.net.URLEncoder;
import java.net.URLDecoder;

import java.text.NumberFormat;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import java.util.Arrays;
import java.util.Collection;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Locale;
import java.util.StringTokenizer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.samskivert.annotation.ReplacedBy;

/**
 * String related utility functions.
 */
public class StringUtil
{
    /**
     * Used to format objects in {@link #listToString(Object,StringUtil.Formatter)}.
     */
    public static class Formatter
    {
        /** Formats the supplied object into a string. */
        public String toString (Object object) {
            return object == null ? "null" : object.toString();
        }

        /** Returns the string that will be prepended to a formatted list. */
        public String getOpenBox () {
            return "(";
        }

        /** Returns the string that will be appended to a formatted list. */
        public String getCloseBox () {
            return ")";
        }
    }

    /**
     * @return true if the string is null or empty, false otherwise.
     *
     * @deprecated use isBlank instead.
     */
    @Deprecated
    public static boolean blank (String value)
    {
        return isBlank(value);
    }

    /**
     * @return true if the string is null or consists only of whitespace, false otherwise.
     */
    public static boolean isBlank (String value)
    {
        for (int ii = 0, ll = (value == null) ? 0 : value.length(); ii < ll; ii++) {
            if (!Character.isWhitespace(value.charAt(ii))) {
                return false;
            }
        }
        return true;
    }

    /**
     * Calls {@link String#trim} on non-null values, returns null for null values.
     */
    public static String trim (String value)
    {
        return (value == null) ? null : value.trim();
    }

    /**
     * @return the supplied string if it is non-null, "" if it is null.
     */
    public static String deNull (String value)
    {
        return (value == null) ? "" : value;
    }

    /**
     * Truncate the specified String if it is longer than maxLength.
     */
    public static String truncate (String s, int maxLength)
    {
        return truncate(s, maxLength, "");
    }

    /**
     * Returns the string if it is non-blank (see {@link #isBlank}), the default value otherwise.
     */
    public static String getOr (String value, String defval)
    {
        return isBlank(value) ? defval : value;
    }

    /**
     * Truncate the specified String if it is longer than maxLength.  The string will be truncated
     * at a position such that it is maxLength chars long after the addition of the 'append'
     * String.
     *
     * @param append a String to add to the truncated String only after truncation.
     */
    public static String truncate (String s, int maxLength, String append)
    {
        if ((s == null) || (s.length() <= maxLength)) {
            return s;
        } else {
{'fraction_non_alphanumeric': 0.09985130961912388, 'fraction_numerical': 0.006633878531396546, 'mean_word_length': 2.5258064516129033, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1779579', 'n_tokens_mistral': 2477, 'n_tokens_neox': 2406, 'n_words': 940}