package cn.reghao.bnt.web.util;

import java.util.regex.Pattern;

/**
 * @author reghao
 * @date 2022-01-06 13:02:04
 */
public class StringUtil {
    /**
     * 返回格式为 .mp4 形式的后缀名
     *
     * @param
     * @return
     * @date 2021-12-08 下午3:32
     */
    public static String getSuffix(String filename) {
        int idx = filename.lastIndexOf(".");
        return idx == -1 ? "" : filename.substring(idx);
    }

    public static String txtToHtml(String s) {
        StringBuilder builder = new StringBuilder();
        boolean previousWasASpace = false;
        for (char c : s.toCharArray()) {
            if (c == ' ') {
                if (previousWasASpace) {
                    builder.append("&nbsp;");
                    previousWasASpace = false;
                    continue;
                }
                previousWasASpace = true;
            } else {
                previousWasASpace = false;
            }
            switch (c) {
                case '<':
                    builder.append("&lt;");
                    break;
                case '>':
                    builder.append("&gt;");
                    break;
                case '&':
                    builder.append("&amp;");
                    break;
                case '"':
                    builder.append("&quot;");
                    break;
                case '\n':
                    builder.append("<br>");
                    break;
                // We need Tab support here, because we print StackTraces as HTML
                case '\t':
                    builder.append("&nbsp; &nbsp; &nbsp;");
                    break;
                default:
                    builder.append(c);

            }
        }
        String converted = builder.toString();
        String str = "(?i)\\b((?:https?://|www\\d{0,3}[.]|[a-z0-9.\\-]+[.][a-z]{2,4}/)(?:[^\\s()<>]+|\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\))+(?:\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\)|[^\\s`!()\\[\\]{};:\'\".,<>?«»“”‘’]))";
        Pattern patt = Pattern.compile(str);
        //Matcher matcher = patt.matcher(converted);
        //converted = matcher.replaceAll("<a href=\"$1\">$1</a>");
        return converted;
    }
}
