Unverified 提交 ca14d3bc authored 作者: Noel Grandin's avatar Noel Grandin 提交者: GitHub

Merge pull request #1040 from turbanoff/master

generate less garbage for String substring+trim
......@@ -244,9 +244,9 @@ public class DbContextRule implements Rule {
}
String incompleteSentence = sentence.getQueryUpper();
String incompleteFunctionName = incompleteSentence;
if (incompleteSentence.contains("(")) {
incompleteFunctionName = incompleteSentence.substring(0,
incompleteSentence.indexOf('(')).trim();
int bracketIndex = incompleteSentence.indexOf('(');
if (bracketIndex != -1) {
incompleteFunctionName = StringUtils.trimSubstring(incompleteSentence, 0, bracketIndex);
}
// Common elements
......
......@@ -2343,7 +2343,7 @@ public class Parser {
}
private void setSQL(Prepared command, String start, int startIndex) {
String sql = originalSQL.substring(startIndex, lastParseIndex).trim();
String sql = StringUtils.trimSubstring(originalSQL, startIndex, lastParseIndex);
if (start != null) {
sql = start + " " + sql;
}
......
......@@ -1644,9 +1644,10 @@ public class JdbcDatabaseMetaData extends TraceObject implements
for (String a : array) {
buff.appendExceptFirst(",");
String f = a.trim();
if (f.indexOf(' ') >= 0) {
int spaceIndex = f.indexOf(' ');
if (spaceIndex >= 0) {
// remove 'Function' from 'INSERT Function'
f = f.substring(0, f.indexOf(' ')).trim();
f = StringUtils.trimSubstring(f, 0, spaceIndex);
}
buff.append(f);
}
......
......@@ -10,6 +10,7 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.h2.util.New;
import org.h2.util.StringUtils;
/**
* A page parser can parse an HTML page and replace the tags there.
......@@ -141,7 +142,7 @@ public class PageParser {
setError(i);
return;
}
String item = p.substring(i, j).trim();
String item = StringUtils.trimSubstring(p, i, j);
i = j;
String s = (String) get(item);
replaceTags(s);
......
......@@ -115,7 +115,7 @@ class WebThread extends WebApp implements Runnable {
if (begin < 0 || end < begin) {
file = "";
} else {
file = head.substring(begin + 1, end).trim();
file = StringUtils.trimSubstring(head, begin + 1, end);
}
trace(head + ": " + file);
file = getAllowedFile(file);
......
......@@ -862,6 +862,24 @@ public class StringUtils {
return s.substring(begin, end);
}
/**
* Trim a character from a substring. Equivalence of {@code substring(begin, end).trim()}.
*
* @param s the string
* @param beginIndex start index of substring (inclusive)
* @param endIndex end index of substring (exclusive)
* @return trimmed substring
*/
public static String trimSubstring(String s, int beginIndex, int endIndex) {
while (beginIndex < endIndex && s.charAt(beginIndex) <= ' ') {
beginIndex++;
}
while (beginIndex < endIndex && s.charAt(endIndex - 1) <= ' ') {
endIndex--;
}
return s.substring(beginIndex, endIndex);
}
/**
* Get the string from the cache if possible. If the string has not been
* found, it is added to the cache. If there is such a string in the cache,
......
......@@ -40,6 +40,7 @@ public class TestStringUtils extends TestBase {
testPad();
testReplaceAll();
testTrim();
testTrimSubstring();
}
private void testHex() {
......@@ -252,5 +253,15 @@ public class TestStringUtils extends TestBase {
StringUtils.trim("zzbbzz", true, true, "z"));
}
private void testTrimSubstring() {
assertEquals("", StringUtils.trimSubstring("", 0, 0));
assertEquals("", StringUtils.trimSubstring(" ", 0, 0));
assertEquals("", StringUtils.trimSubstring(" ", 4, 4));
assertEquals("select", StringUtils.trimSubstring(" select from", 1, 7));
assertEquals("a b", StringUtils.trimSubstring(" a b ", 1, 4));
new AssertThrows(StringIndexOutOfBoundsException.class) { @Override
public void test() { StringUtils.trimSubstring(" with (", 1, 8); }
};
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论