提交 abbcc8fe authored 作者: Andrey Turbanov's avatar Andrey Turbanov

generate less garbage for String substring+trim

上级 817b0944
...@@ -244,9 +244,9 @@ public class DbContextRule implements Rule { ...@@ -244,9 +244,9 @@ public class DbContextRule implements Rule {
} }
String incompleteSentence = sentence.getQueryUpper(); String incompleteSentence = sentence.getQueryUpper();
String incompleteFunctionName = incompleteSentence; String incompleteFunctionName = incompleteSentence;
if (incompleteSentence.contains("(")) { int bracketIndex = incompleteSentence.indexOf('(');
incompleteFunctionName = incompleteSentence.substring(0, if (bracketIndex != -1) {
incompleteSentence.indexOf('(')).trim(); incompleteFunctionName = StringUtils.trimSubstring(incompleteSentence, 0, bracketIndex);
} }
// Common elements // Common elements
......
...@@ -2343,7 +2343,7 @@ public class Parser { ...@@ -2343,7 +2343,7 @@ public class Parser {
} }
private void setSQL(Prepared command, String start, int startIndex) { 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) { if (start != null) {
sql = start + " " + sql; sql = start + " " + sql;
} }
......
...@@ -1644,9 +1644,10 @@ public class JdbcDatabaseMetaData extends TraceObject implements ...@@ -1644,9 +1644,10 @@ public class JdbcDatabaseMetaData extends TraceObject implements
for (String a : array) { for (String a : array) {
buff.appendExceptFirst(","); buff.appendExceptFirst(",");
String f = a.trim(); String f = a.trim();
if (f.indexOf(' ') >= 0) { int spaceIndex = f.indexOf(' ');
if (spaceIndex >= 0) {
// remove 'Function' from 'INSERT Function' // remove 'Function' from 'INSERT Function'
f = f.substring(0, f.indexOf(' ')).trim(); f = StringUtils.trimSubstring(f, 0, spaceIndex);
} }
buff.append(f); buff.append(f);
} }
......
...@@ -10,6 +10,7 @@ import java.util.HashMap; ...@@ -10,6 +10,7 @@ import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import org.h2.util.New; import org.h2.util.New;
import org.h2.util.StringUtils;
/** /**
* A page parser can parse an HTML page and replace the tags there. * A page parser can parse an HTML page and replace the tags there.
...@@ -141,7 +142,7 @@ public class PageParser { ...@@ -141,7 +142,7 @@ public class PageParser {
setError(i); setError(i);
return; return;
} }
String item = p.substring(i, j).trim(); String item = StringUtils.trimSubstring(p, i, j);
i = j; i = j;
String s = (String) get(item); String s = (String) get(item);
replaceTags(s); replaceTags(s);
......
...@@ -115,7 +115,7 @@ class WebThread extends WebApp implements Runnable { ...@@ -115,7 +115,7 @@ class WebThread extends WebApp implements Runnable {
if (begin < 0 || end < begin) { if (begin < 0 || end < begin) {
file = ""; file = "";
} else { } else {
file = head.substring(begin + 1, end).trim(); file = StringUtils.trimSubstring(head, begin + 1, end);
} }
trace(head + ": " + file); trace(head + ": " + file);
file = getAllowedFile(file); file = getAllowedFile(file);
......
...@@ -862,6 +862,24 @@ public class StringUtils { ...@@ -862,6 +862,24 @@ public class StringUtils {
return s.substring(begin, end); 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 * 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, * 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 { ...@@ -40,6 +40,7 @@ public class TestStringUtils extends TestBase {
testPad(); testPad();
testReplaceAll(); testReplaceAll();
testTrim(); testTrim();
testTrimSubstring();
} }
private void testHex() { private void testHex() {
...@@ -252,5 +253,15 @@ public class TestStringUtils extends TestBase { ...@@ -252,5 +253,15 @@ public class TestStringUtils extends TestBase {
StringUtils.trim("zzbbzz", true, true, "z")); 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 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论