mergedoc.core.FastStringUtils.java Source code

Java tutorial

Introduction

Here is the source code for mergedoc.core.FastStringUtils.java

Source

/*
 * Copyright (c) 2003- Shinji Kashihara. All rights reserved.
 * This program are made available under the terms of the Common Public License
 * v1.0 which accompanies this distribution, and is available at cpl-v10.html.
 */
package mergedoc.core;

import java.util.LinkedList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * ??
 * @author Shinji Kashihara
 */
public class FastStringUtils {

    /**  */
    private static final Log log = LogFactory.getLog(FastStringUtils.class);

    /**
     * ?????
     */
    private FastStringUtils() {
    }

    /**
     * ?? target ??????????
     *
     * <p>????? JDK ? String#replace(CharSequence,CharSequence)
     * ?????????? Pattern 
     * ?????? target ???2 ????
     *
     * <pre>
     *     input.replace(target, replacement);
     * </pre>
     *
     * ?<br>
     * target ???????? Pattern ?
     * ????????????????
     *
     * @param input ??
     * @param target ??
     * @param replacement ?
     * @return ??
     */
    public static String replace(String input, String target, String replacement) {
        Pattern pattern = PatternCache.getLiteralPattern(target);
        return pattern.matcher(input).replaceAll(Matcher.quoteReplacement(replacement));
    }

    /**
     * ?? regex ????????
     *
     * <p>????? JDK ? String#replaceFirst(String,String)
     * ?????????? Pattern 
     * ?????? regex ???2 ????
     *
     * <pre>
     *     input.replaceFirst(regex, replacement);
     * </pre>
     *
     * ?<br>
     * regex ???????? Pattern ?
     * ????????????????
     *
     * @param input ??
     * @param regex ????
     * @param replacement ?
     * @return ??
     */
    public static String replaceFirst(String input, String regex, String replacement) {
        Pattern pattern = PatternCache.getPattern(regex);
        return pattern.matcher(input).replaceFirst(replacement);
    }

    /**
     * ?? regex ??????????
     *
     * <p>????? JDK ? String#replaceAll(String,String)
     * ?????????? Pattern 
     * ?????? regex ???2 ????
     *
     * <pre>
     *     input.replaceAll(regex, replacement);
     * </pre>
     *
     * ?<br>
     * regex ???????? Pattern ?
     * ????????????????
     *
     * @param input ??
     * @param regex ????
     * @param replacement ?
     * @return ??
     */
    public static String replaceAll(String input, String regex, String replacement) {
        Pattern pattern = PatternCache.getPattern(regex);
        return pattern.matcher(input).replaceAll(replacement);
    }

    /**
     * ?? regex ??????
     *
     * <p>????? JDK ? String#matches(String)
     * ?????????? Pattern 
     * ?????? regex ???2 ????
     *
     * <pre>
     *     input.matches(regex);
     * </pre>
     *
     * ?<br>
     * regex ???????? Pattern ?
     * ????????????????
     *
     * @param input ??
     * @param regex ????
     * @return ??? true
     */
    public static boolean matches(String input, String regex) {
        Pattern pattern = PatternCache.getPattern(regex);
        return pattern.matcher(input).matches();
    }

    /**
     * ?? regex ????
     *
     * <p>????? JDK ? String#split(String,int)
     * ?????????? Pattern 
     * ?????? regex ???2 ????
     *
     * <pre>
     *     input.split(regex, limit);
     * </pre>
     *
     * ?<br>
     * regex ???????? Pattern ?
     * ????????????????
     *
     * @param input ??
     * @param regex ???
     * @param limit ??????
     * @return ????
     */
    public static String[] split(String input, String regex, int limit) {
        Pattern pattern = PatternCache.getPattern(regex);
        return pattern.split(input, limit);
    }

    /**
     * ??? \ ????
     * ??? \ ??????? $ ??
     *
     * <p>?? JDK ? Matcher.quoteReplacement(String) ???
     * ????????
     *
     * @param  str  
     * @return ??
     */
    public static String quoteReplacement(String str) {

        //return str.replaceAll("(\\$|\\\\)", "\\\\$1");
        // ????????

        if (!str.contains("\\") && !str.contains("$")) {
            return str;
        }
        int size = str.length();
        int expectSize = (int) (size * 1.1);
        StringBuilder sb = new StringBuilder(expectSize);

        for (int i = 0; i < size; i++) {
            char c = str.charAt(i);
            if (c == '\\' || c == '$') {
                sb.append('\\');
            }
            sb.append(c);
        }
        return sb.toString();
    }

    /**
     * ????
     * <ol>
     * <li>CRLF  LF
     * <li>CR  LF
     * </ol>
     *
     * @param str 
     * @return ???
     */
    public static String optimizeLineSeparator(String str) {

        //return str.replaceAll("(\r\n|\r)", "\n");
        // ????????

        str += " ";
        int size = str.length();
        StringBuilder sb = new StringBuilder(size);

        for (int i = 0; i < size - 1; i++) {
            char c = str.charAt(i);
            if (c == '\r') {
                if (str.charAt(i + 1) != '\n') {
                    sb.append('\n');
                }
            } else {
                sb.append(c);
            }
        }
        return sb.toString();
    }

    /**
     * ????????
     * ?????JDK ????? 8 ???????
     *
     * @param str 
     * @return ???
     */
    public static String untabify(String str) {

        final int TAB_WIDTH = 8;
        int size = str.length();
        int expectSize = (int) (size * 1.2);
        StringBuilder sb = new StringBuilder(expectSize);

        for (int pos = 0, hPos = -1; pos < size; pos++) {
            char c = str.charAt(pos);
            hPos = (c == '\n') ? -1 : hPos + 1;

            if (c == '\t') {
                int fillSize = TAB_WIDTH - hPos % TAB_WIDTH;
                for (int f = 0; f < fillSize; f++) {
                    sb.append(' ');
                }
                hPos = hPos + fillSize - 1;
            } else {
                sb.append(c);
            }
        }
        return sb.toString();
    }

    /**
     * ?????
     * ??????LF????
     *
     * @param str 
     * @return ?
     */
    public static int heightOf(String str) {

        //return str.split("\n", -1).length - 1;
        // ? split ????

        int height = 0;
        for (int i = 0; i < str.length(); i++) {
            if (str.charAt(i) == '\n') {
                height++;
            }
        }
        return height;
    }

    /**
     * LF??????
     * @param str ??
     * @return 
     */
    public static List<String> splitLine(String str) {

        List<String> list = new LinkedList<String>();
        int size = str.length();
        int expectSize = 120;
        StringBuilder sb = new StringBuilder(expectSize);

        for (int i = 0; i < size; i++) {
            char c = str.charAt(i);
            if (c == '\n') {
                list.add(sb.toString());
                sb = new StringBuilder(expectSize);
            } else {
                sb.append(c);
            }
        }

        if (sb.length() > 0) {
            list.add(sb.toString());
        }
        return list;
    }
}