一、前言
关于taobao的tddl-common包(5.1.0)的com.taobao.tddl.common.utils.TStringUtil基于org.apache.commons.lang.StringUtils的重写自定义字符串工具类,详见源码说明。
二、源码说明
package com.taobao.tddl.common.utils;@b@@b@import java.io.IOException;@b@import java.io.StringReader;@b@import java.util.ArrayList;@b@import java.util.List;@b@import org.apache.commons.lang.StringUtils;@b@@b@public class TStringUtil extends StringUtils@b@{@b@ public static String getBetween(String sql, String start, String end)@b@ {@b@ if ((sql == null) || (start == null) || (end == null)) {@b@ return null;@b@ }@b@@b@ int index0 = sql.indexOf(start);@b@ if (index0 == -1)@b@ return null;@b@@b@ int index1 = sql.indexOf(end, index0);@b@ if (index1 == -1)@b@ return null;@b@@b@ return sql.substring(index0 + start.length(), index1).trim();@b@ }@b@@b@ public static String removeBetween(String sql, String start, String end)@b@ {@b@ if (sql == null) {@b@ return null;@b@ }@b@@b@ if ((start == null) || (end == null)) {@b@ return sql;@b@ }@b@@b@ int index0 = sql.indexOf(start);@b@ if (index0 == -1)@b@ return sql;@b@@b@ int index1 = sql.indexOf(end, index0);@b@ if (index1 == -1)@b@ return sql;@b@@b@ StringBuilder sb = new StringBuilder();@b@ sb.append(sql.substring(0, index0));@b@ sb.append(" ");@b@ sb.append(sql.substring(index1 + end.length()));@b@ return sb.toString();@b@ }@b@@b@ public static String[] twoPartSplit(String str, String splitor)@b@ {@b@ if ((str != null) && (splitor != null)) {@b@ int index = str.indexOf(splitor);@b@ if (index != -1) {@b@ String first = str.substring(0, index);@b@ String sec = str.substring(index + splitor.length());@b@ return new String[] { first, sec };@b@ }@b@ return new String[] { str };@b@ }@b@@b@ return new String[] { str };@b@ }@b@@b@ public static List<String> recursiveSplit(String str, String splitor)@b@ {@b@ List re = new ArrayList();@b@ String[] strs = twoPartSplit(str, splitor);@b@ if (strs.length == 2) {@b@ re.add(strs[0]);@b@ re.addAll(recursiveSplit(strs[1], splitor));@b@ } else {@b@ re.add(strs[0]);@b@ }@b@ return re;@b@ }@b@@b@ public static String fillTabWithSpace(String str)@b@ {@b@ if (str == null) {@b@ return null;@b@ }@b@@b@ str = str.trim();@b@ int sz = str.length();@b@ StringBuilder buffer = new StringBuilder(sz);@b@@b@ int index = 0; int index0 = -1; int index1 = -1;@b@ for (int i = 0; i < sz; ++i) {@b@ char c = str.charAt(i);@b@ if (!(Character.isWhitespace(c))) {@b@ if ((index0 != -1) && ((@b@ (index0 != index1) || (str.charAt(i - 1) != ' ')))) {@b@ buffer.append(str.substring(index, index0)).append(" ");@b@ index = index1 + 1;@b@ }@b@@b@ index0 = index1 = -1;@b@ }@b@ else if (index0 == -1) {@b@ index0 = index1 = i;@b@ } else {@b@ index1 = i;@b@ }@b@@b@ }@b@@b@ buffer.append(str.substring(index));@b@ return buffer.toString();@b@ }@b@@b@ public static boolean startsWithIgnoreCaseAndWs(String searchIn, String searchFor)@b@ {@b@ return startsWithIgnoreCaseAndWs(searchIn, searchFor, 0);@b@ }@b@@b@ public static boolean startsWithIgnoreCaseAndWs(String searchIn, String searchFor, int beginPos)@b@ {@b@ if (searchIn == null) {@b@ return (searchFor == null);@b@ }@b@@b@ int inLength = searchIn.length();@b@@b@ for (; beginPos < inLength; ++beginPos) {@b@ if (!(Character.isWhitespace(searchIn.charAt(beginPos))))@b@ break;@b@@b@ }@b@@b@ return startsWithIgnoreCase(searchIn, beginPos, searchFor);@b@ }@b@@b@ public static boolean startsWithIgnoreCase(String searchIn, int startAt, String searchFor)@b@ {@b@ return searchIn.regionMatches(true, startAt, searchFor, 0, searchFor.length());@b@ }@b@@b@ public static String stripComments(String src, String stringOpens, String stringCloses, boolean slashStarComments, boolean slashSlashComments, boolean hashComments, boolean dashDashComments)@b@ {@b@ if (src == null) {@b@ return null;@b@ }@b@@b@ StringBuffer buf = new StringBuffer(src.length());@b@@b@ StringReader sourceReader = new StringReader(src);@b@@b@ int contextMarker = 0;@b@ boolean escaped = false;@b@ int markerTypeFound = -1;@b@@b@ int ind = 0;@b@@b@ int currentChar = 0;@b@@b@ label409: label437: label455: @b@ try { while (true) { do { while (true) { do { if ((currentChar = sourceReader.read()) == -1) break label455;@b@ if (currentChar == 92) {@b@ escaped = !(escaped);@b@ } else if ((markerTypeFound != -1) && (currentChar == stringCloses.charAt(markerTypeFound)) && (!(escaped))) {@b@ contextMarker = 0;@b@ markerTypeFound = -1;@b@ } else if (((ind = stringOpens.indexOf(currentChar)) != -1) && (!(escaped)) && (contextMarker == 0))@b@ {@b@ markerTypeFound = ind;@b@ contextMarker = currentChar;@b@ }@b@@b@ if ((contextMarker == 0) && (currentChar == 47) && (((slashSlashComments) || (slashStarComments))))@b@ {@b@ currentChar = sourceReader.read();@b@ if ((currentChar == 42) && (slashStarComments)) {@b@ int prevChar = 0;@b@ while (true) { while (true) { if (((currentChar = sourceReader.read()) == 47) && (prevChar == 42));@b@ if (currentChar == 13)@b@ {@b@ currentChar = sourceReader.read();@b@ if (currentChar == 10)@b@ currentChar = sourceReader.read();@b@@b@ }@b@ else if (currentChar == 10)@b@ {@b@ currentChar = sourceReader.read();@b@ }@b@@b@ if (currentChar >= 0) break; @b@ }@b@ prevChar = currentChar;@b@ }@b@ }@b@ if ((currentChar != 47) || (!(slashSlashComments))) break label437;@b@ while (true) if (((currentChar = sourceReader.read()) == 10) || (currentChar == 13) || (currentChar < 0))@b@ break label437;@b@ }@b@ if ((contextMarker == 0) && (currentChar == 35) && (hashComments))@b@ while (true)@b@ if (((currentChar = sourceReader.read()) == 10) || (currentChar == 13) || (currentChar < 0))@b@ break label437;@b@ if ((contextMarker != 0) || (currentChar != 45) || (!(dashDashComments))) break label437;@b@ currentChar = sourceReader.read();@b@@b@ if ((currentChar != -1) && (currentChar == 45)) break label409;@b@ buf.append('-');@b@ }@b@ while (currentChar == -1);@b@ buf.append(currentChar);@b@ }@b@@b@ while (((currentChar = sourceReader.read()) != 10) && (currentChar != 13) && (currentChar >= 0));@b@ }@b@@b@ while (currentChar == -1);@b@ buf.append((char)currentChar);@b@ }@b@ }@b@ catch (IOException ioEx)@b@ {@b@ }@b@@b@ return buf.toString();@b@ }@b@@b@ public static String removeBetweenWithSplitor(String sql, String start, String end)@b@ {@b@ int index0 = sql.indexOf(start);@b@ if (index0 == -1)@b@ return sql;@b@@b@ int index1 = sql.indexOf(end, index0);@b@ if (index1 == -1)@b@ return sql;@b@@b@ StringBuilder sb = new StringBuilder();@b@ sb.append(sql.substring(0, index0));@b@ sb.append(" ");@b@ sb.append(sql.substring(index1 + end.length()));@b@ return sb.toString();@b@ }@b@@b@ public static String removeBetweenWithSplitorNotExistNull(String sql, String start, String end) {@b@ int index0 = sql.indexOf(start);@b@ if (index0 == -1)@b@ return null;@b@@b@ int index1 = sql.indexOf(end, index0);@b@ if (index1 == -1)@b@ return null;@b@@b@ StringBuilder sb = new StringBuilder();@b@ sb.append(sql.substring(0, index0));@b@ sb.append(" ");@b@ sb.append(sql.substring(index1 + end.length()));@b@ return sb.toString();@b@ }@b@@b@ public static boolean isTableFatherAndSon(String fatherTable, String sonTable)@b@ {@b@ if ((fatherTable == null) || (fatherTable.trim().isEmpty()) || (sonTable == null) || (sonTable.trim().isEmpty()))@b@ return false;@b@@b@ if ((!(sonTable.startsWith(fatherTable))) || (fatherTable.length() + 2 > sonTable.length()))@b@ return false;@b@@b@ String suffix = sonTable.substring(fatherTable.length());@b@@b@ return (suffix.matches("_[\\d]+"));@b@ }@b@}