Skip to content

Commit 9364654

Browse files
committed
Refactorings for readability
Author: https://github.com/sanifalimomin See also: #66
1 parent 77a5b20 commit 9364654

1 file changed

Lines changed: 65 additions & 27 deletions

File tree

src/main/java/oakbot/bot/ChatCommand.java

Lines changed: 65 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -92,51 +92,89 @@ public List<String> getContentAsArgs() {
9292
}
9393

9494
var md = getContentMarkdown().trim();
95+
return parseArguments(md);
96+
}
97+
98+
/**
99+
* Parses a markdown string into whitespace-delimited arguments. Handles
100+
* quoted strings and escape sequences.
101+
* @param text the text to parse
102+
* @return the list of arguments
103+
*/
104+
private List<String> parseArguments(String text) {
95105
var args = new ArrayList<String>();
106+
var parser = new ArgumentParser();
107+
var it = new CharIterator(text);
96108

97-
var inQuotes = false;
98-
var escapeNext = false;
99-
var sb = new StringBuilder();
100-
var it = new CharIterator(md);
101109
while (it.hasNext()) {
102110
var c = it.next();
111+
parser.processCharacter(c, args);
112+
}
103113

114+
parser.finalizeCurrentArgument(args);
115+
return args;
116+
}
117+
118+
/**
119+
* Helper class to parse command arguments with quote and escape handling.
120+
*/
121+
private static class ArgumentParser {
122+
private final StringBuilder currentArg = new StringBuilder();
123+
private boolean inQuotes = false;
124+
private boolean escapeNext = false;
125+
126+
void processCharacter(char c, List<String> args) {
104127
if (escapeNext) {
105-
sb.append(c);
106-
escapeNext = false;
107-
continue;
128+
handleEscapedCharacter(c);
129+
return;
108130
}
109131

110-
if (Character.isWhitespace(c) && !inQuotes) {
111-
if (!sb.isEmpty()) {
112-
args.add(sb.toString());
113-
sb.setLength(0);
114-
}
115-
continue;
132+
if (c == '\\') {
133+
escapeNext = true;
134+
return;
116135
}
117136

118137
if (c == '"') {
119-
if (inQuotes) {
120-
args.add(sb.toString());
121-
sb.setLength(0);
122-
}
123-
inQuotes = !inQuotes;
124-
continue;
138+
handleQuote(args);
139+
return;
125140
}
126141

127-
if (c == '\\') {
128-
escapeNext = true;
129-
continue;
142+
if (Character.isWhitespace(c) && !inQuotes) {
143+
handleWhitespace(args);
144+
return;
130145
}
131146

132-
sb.append(c);
147+
currentArg.append(c);
133148
}
134149

135-
if (!sb.isEmpty()) {
136-
args.add(sb.toString());
150+
private void handleEscapedCharacter(char c) {
151+
currentArg.append(c);
152+
escapeNext = false;
137153
}
138154

139-
return args;
155+
private void handleQuote(List<String> args) {
156+
if (inQuotes) {
157+
addCurrentArgument(args);
158+
}
159+
inQuotes = !inQuotes;
160+
}
161+
162+
private void handleWhitespace(List<String> args) {
163+
if (currentArg.length() > 0) {
164+
addCurrentArgument(args);
165+
}
166+
}
167+
168+
private void addCurrentArgument(List<String> args) {
169+
args.add(currentArg.toString());
170+
currentArg.setLength(0);
171+
}
172+
173+
private void finalizeCurrentArgument(List<String> args) {
174+
if (currentArg.length() > 0) {
175+
addCurrentArgument(args);
176+
}
177+
}
140178
}
141179

142180
/**
@@ -341,4 +379,4 @@ public boolean equals(Object obj) {
341379
var other = (ChatCommand) obj;
342380
return Objects.equals(commandName, other.commandName) && Objects.equals(content, other.content) && Objects.equals(message, other.message);
343381
}
344-
}
382+
}

0 commit comments

Comments
 (0)