HHH-14217 Minor optimisations in the highlighting formatter implementation
This commit is contained in:
parent
916513a7eb
commit
225e29d2b2
|
@ -19,7 +19,7 @@ import java.util.StringTokenizer;
|
||||||
*
|
*
|
||||||
* @author Gavin King
|
* @author Gavin King
|
||||||
*/
|
*/
|
||||||
public class HighlightingFormatter implements Formatter {
|
public final class HighlightingFormatter implements Formatter {
|
||||||
|
|
||||||
private static final Set<String> KEYWORDS = new HashSet<>( AnsiSqlKeywords.INSTANCE.sql2003() );
|
private static final Set<String> KEYWORDS = new HashSet<>( AnsiSqlKeywords.INSTANCE.sql2003() );
|
||||||
static {
|
static {
|
||||||
|
@ -33,6 +33,7 @@ public class HighlightingFormatter implements Formatter {
|
||||||
"36", // cyan
|
"36", // cyan
|
||||||
"32"
|
"32"
|
||||||
);
|
);
|
||||||
|
private static final String SYMBOLS_AND_WS = "=><!+-*/()',.|&`\"?" + StringHelper.WHITESPACE;
|
||||||
|
|
||||||
private static String escape(String code) {
|
private static String escape(String code) {
|
||||||
return "\u001b[" + code + "m";
|
return "\u001b[" + code + "m";
|
||||||
|
@ -56,11 +57,10 @@ public class HighlightingFormatter implements Formatter {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String format(String sql) {
|
public String format(String sql) {
|
||||||
String symbolsAndWs = "=><!+-*/()',.|&`\"?" + StringHelper.WHITESPACE;
|
|
||||||
StringBuilder result = new StringBuilder();
|
StringBuilder result = new StringBuilder();
|
||||||
boolean inString = false;
|
boolean inString = false;
|
||||||
boolean inQuoted = false;
|
boolean inQuoted = false;
|
||||||
for ( StringTokenizer tokenizer = new StringTokenizer( sql, symbolsAndWs, true );
|
for ( StringTokenizer tokenizer = new StringTokenizer( sql, SYMBOLS_AND_WS, true );
|
||||||
tokenizer.hasMoreTokens(); ) {
|
tokenizer.hasMoreTokens(); ) {
|
||||||
String token = tokenizer.nextToken();
|
String token = tokenizer.nextToken();
|
||||||
switch ( token ) {
|
switch ( token ) {
|
||||||
|
@ -80,15 +80,15 @@ public class HighlightingFormatter implements Formatter {
|
||||||
break;
|
break;
|
||||||
case "'":
|
case "'":
|
||||||
if ( inQuoted ) {
|
if ( inQuoted ) {
|
||||||
result.append( "'" );
|
result.append( '\'' );
|
||||||
}
|
}
|
||||||
else if ( inString ) {
|
else if ( inString ) {
|
||||||
inString = false;
|
inString = false;
|
||||||
result.append( "'" ).append( normalEscape );
|
result.append( '\'' ).append( normalEscape );
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
inString = true;
|
inString = true;
|
||||||
result.append( stringEscape ).append( "'" );
|
result.append( stringEscape ).append( '\'' );
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
|
|
Loading…
Reference in New Issue