Skip to content

Commit 986d67f

Browse files
committed
Serial monitor timestamp: cache newline status
This saves an access to the Document object (as well as a bunch temporary object allocations).
1 parent 8128525 commit 986d67f

File tree

1 file changed

+5
-19
lines changed

1 file changed

+5
-19
lines changed

app/src/processing/app/AbstractTextMonitor.java

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,7 @@
2525
import javax.swing.JTextField;
2626
import javax.swing.SwingUtilities;
2727
import javax.swing.border.EmptyBorder;
28-
import javax.swing.text.BadLocationException;
2928
import javax.swing.text.DefaultCaret;
30-
import javax.swing.text.Document;
3129

3230
import cc.arduino.packages.BoardPort;
3331

@@ -182,36 +180,24 @@ public void message(final String s) {
182180
Date t = new Date();
183181
String now;
184182
StringBuilder out = new StringBuilder(16384);
183+
boolean isStartingLine = false;
185184

186185
public void run() {
187186
if (addTimeStampBox.isSelected()) {
188187
t.setTime(System.currentTimeMillis());
189188
now = logDateFormat.format(t);
190189
out.setLength(0);
191190

192-
boolean isStartingLine;
193-
try {
194-
Document doc = textArea.getDocument();
195-
isStartingLine = doc.getLength() == 0 || ((int) doc.getText(doc.getLength() - 1, 1).charAt(0) == 10);
196-
} catch (BadLocationException e) {
197-
// Should not happen but...
198-
e.printStackTrace();
199-
return;
200-
}
201-
202191
StringTokenizer tokenizer = new StringTokenizer(s, "\n", true);
203192
while (tokenizer.hasMoreTokens()) {
204193
if (isStartingLine) {
205194
out.append(now);
206195
out.append(" -> ");
207196
}
208-
out.append(tokenizer.nextToken());
209-
210-
// Check if we have a "\n" token
211-
if (tokenizer.hasMoreTokens()) {
212-
out.append(tokenizer.nextToken());
213-
isStartingLine = true;
214-
}
197+
String token = tokenizer.nextToken();
198+
out.append(token);
199+
// tokenizer returns "\n" as a single token
200+
isStartingLine = token.charAt(0) == '\n';
215201
}
216202

217203
textArea.append(out.toString());

0 commit comments

Comments
 (0)