Skip to content

Commit 7ac6cd0

Browse files
feat: avoid looping through the tokens every single time
Signed-off-by: Andreas Reichel <[email protected]>
1 parent b3c5b63 commit 7ac6cd0

File tree

1 file changed

+24
-15
lines changed

1 file changed

+24
-15
lines changed

src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,24 @@ PARSER_END(CCJSqlParser)
166166
TOKEN_MGR_DECLS : {
167167
public FeatureConfiguration configuration = new FeatureConfiguration();
168168

169+
// Identify the index of the quoting/escaping tokens
170+
public int charLiteralIndex = -1;
171+
public int squaredBracketOpenIndex = -1;
172+
{
173+
for (int i=0;i<CCJSqlParserConstants.tokenImage.length;i++) {
174+
if ( CCJSqlParserConstants.tokenImage[i].equals("<S_CHAR_LITERAL>") ) {
175+
charLiteralIndex = i;
176+
break;
177+
}
178+
}
179+
for (int i=0;i<CCJSqlParserConstants.tokenImage.length;i++) {
180+
if (CCJSqlParserConstants.tokenImage[i].equals("\"[\"")) {
181+
squaredBracketOpenIndex = i;
182+
break;
183+
}
184+
}
185+
}
186+
169187
public void CommonTokenAction(Token t)
170188
{
171189
t.absoluteBegin = getCurrentTokenAbsolutePosition();
@@ -741,31 +759,22 @@ TOKEN:
741759
if ( !configuration.getAsBoolean(Feature.allowBackslashEscapeCharacter)
742760
&& matchedToken.image.contains("\\'") ) {
743761
matchedToken.image = "'" + image.substring( 0, image.indexOf("\\'") + 1 ) + "'";
744-
for (int i=0;i<CCJSqlParserConstants.tokenImage.length;i++) {
745-
if ( CCJSqlParserConstants.tokenImage[i].equals("<S_CHAR_LITERAL>") ) {
746-
matchedToken.kind = i;
747-
}
748-
}
762+
// `charLiteralIndex` defined in TokenManagerDeclaration above
763+
matchedToken.kind = charLiteralIndex;
749764
input_stream.backup(image.length() + 1 - matchedToken.image.length());
750765
} else if ( configuration.getAsBoolean(Feature.allowBackslashEscapeCharacter) && matchedToken.image.contains("\\''") ) {
751766
matchedToken.image = "'" + image.substring( 0, image.lastIndexOf("\\'") + 3);
752-
for (int i=0;i<CCJSqlParserConstants.tokenImage.length;i++) {
753-
if ( CCJSqlParserConstants.tokenImage[i].equals("<S_CHAR_LITERAL>") ) {
754-
matchedToken.kind = i;
755-
}
756-
}
767+
// `charLiteralIndex` defined in TokenManagerDeclaration above
768+
matchedToken.kind = charLiteralIndex;
757769
input_stream.backup(image.length() + 1 - matchedToken.image.length() );
758770
}
759771
}
760772
| < S_QUOTED_IDENTIFIER: "\"" ( "\"\"" | ~["\n","\r","\""])* "\"" | "$$" (~["$"])* "$$" | ("`" (~["\n","\r","`"])+ "`") | ( "[" (~["\n","\r","]"])* "]" ) >
761773
{
762774
if ( !configuration.getAsBoolean(Feature.allowSquareBracketQuotation) && matchedToken.image.charAt(0) == '[' ) {
763775
matchedToken.image = "[";
764-
for (int i=0;i<CCJSqlParserConstants.tokenImage.length;i++) {
765-
if (CCJSqlParserConstants.tokenImage[i].equals("\"[\"")) {
766-
matchedToken.kind = i;
767-
}
768-
}
776+
// `squaredBracketOpenIndex` defined in TokenManagerDeclaration above
777+
matchedToken.kind = squaredBracketOpenIndex;
769778
input_stream.backup(image.length() - 1);
770779
}
771780
}

0 commit comments

Comments
 (0)