Skip to content

Commit e4e5bb8

Browse files
authored
Revert "Improve token parsing and better support control operators such as pi…"
This reverts commit eede2aa.
1 parent a5fa6df commit e4e5bb8

File tree

2 files changed

+58
-435
lines changed

2 files changed

+58
-435
lines changed

core/src/main/java/com/taobao/arthas/core/shell/cli/impl/CliTokenImpl.java

Lines changed: 26 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@
1111
* @author <a href="mailto:julien@julienviet.com">Julien Viet</a>
1212
*/
1313
public class CliTokenImpl implements CliToken {
14-
private static final String PIPE = "|";
15-
private static final String REDIRECT = ">";
16-
private static final String REDIRECT_APPEND = ">>";
1714

1815
final boolean text;
1916
final String raw;
@@ -74,7 +71,7 @@ public static List<CliToken> tokenize(String s) {
7471

7572
tokenize(s, 0, tokens);
7673

77-
tokens = adjustTokensForSpecialSymbols(tokens);
74+
tokens = correctPipeChar(tokens);
7875
return tokens;
7976

8077
}
@@ -90,53 +87,35 @@ public static List<CliToken> tokenize(String s) {
9087
* unsupported:
9188
* 3) thread|grep xxx
9289
* 4) trace -E classA|classB methodA|methodB|grep classA
93-
*
94-
* Also handles redirection of '>' and '>>' in the similar way
95-
*
96-
* @param tokens original tokens
97-
* @return adjusted tokens
90+
* @param tokens
91+
* @return
9892
*/
99-
private static List<CliToken> adjustTokensForSpecialSymbols(List<CliToken> tokens) {
100-
return separateLeadingAndTailingSymbol(tokens, PIPE, REDIRECT_APPEND, REDIRECT);
101-
}
102-
103-
private static List<CliToken> separateLeadingAndTailingSymbol(List<CliToken> tokens, String... symbols) {
104-
List<CliToken> adjustedTokens = new ArrayList<>();
93+
private static List<CliToken> correctPipeChar(List<CliToken> tokens) {
94+
List<CliToken> newTokens = new ArrayList<CliToken>(tokens.size()+4);
10595
for (CliToken token : tokens) {
106-
String value = token.value();
107-
String raw = token.raw();
108-
boolean handled = false;
109-
for (String symbol : symbols) {
110-
if (value.equals(symbol)) {
111-
break;
112-
} else if (value.endsWith(symbol)) {
113-
handled = true;
114-
int lastIndexOfSymbol = raw.lastIndexOf(symbol);
115-
adjustedTokens.add(new CliTokenImpl(
116-
token.isText(),
117-
raw.substring(0, lastIndexOfSymbol),
118-
value.substring(0, value.length() - symbol.length())
119-
));
120-
adjustedTokens.add(new CliTokenImpl(true, raw.substring(lastIndexOfSymbol), symbol));
121-
break;
122-
} else if (value.startsWith(symbol)) {
123-
handled = true;
124-
int firstIndexOfSymbol = raw.indexOf(symbol);
125-
adjustedTokens.add(new CliTokenImpl(true,
126-
raw.substring(0, firstIndexOfSymbol + symbol.length()), symbol));
127-
adjustedTokens.add(new CliTokenImpl(
128-
token.isText(),
129-
raw.substring(firstIndexOfSymbol + symbol.length()),
130-
value.substring(symbol.length())
131-
));
132-
break;
133-
}
134-
}
135-
if (!handled) {
136-
adjustedTokens.add(token);
96+
String tokenValue = token.value();
97+
if (tokenValue.length()>1 && tokenValue.endsWith("|")) {
98+
//split last char '|'
99+
tokenValue = tokenValue.substring(0, tokenValue.length()-1);
100+
String rawValue = token.raw();
101+
rawValue = rawValue.substring(0, rawValue.length()-1);
102+
newTokens.add(new CliTokenImpl(token.isText(), rawValue, tokenValue));
103+
//add '|' char
104+
newTokens.add(new CliTokenImpl(true, "|", "|"));
105+
106+
} else if (tokenValue.length()>1 && tokenValue.startsWith("|")) {
107+
//add '|' char
108+
newTokens.add(new CliTokenImpl(true, "|", "|"));
109+
//remove first char '|'
110+
tokenValue = tokenValue.substring(1);
111+
String rawValue = token.raw();
112+
rawValue = rawValue.substring(1);
113+
newTokens.add(new CliTokenImpl(token.isText(), rawValue, tokenValue));
114+
} else {
115+
newTokens.add(token);
137116
}
138117
}
139-
return adjustedTokens;
118+
return newTokens;
140119
}
141120

142121
private static void tokenize(String s, int index, List<CliToken> builder) {

0 commit comments

Comments
 (0)