Skip to content

Commit

Permalink
Add LineNumberingWriter
Browse files Browse the repository at this point in the history
  • Loading branch information
cgrand committed Aug 20, 2024
1 parent 7a9ee7b commit d6c8b4a
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions java/src/cljd/lang/LineNumberingWriter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package cljd.lang;

import java.io.FilterWriter;
import java.io.IOException;
import java.io.Writer;

public class LineNumberingWriter extends FilterWriter {

private int line = 1;
private int column = 0;

public LineNumberingWriter(Writer out) {
super(out);
}

@Override
public void write(int c) throws IOException {
super.write(c);
if (c == '\n') {
line++;
column = 0;
} else column++;
}

@Override
public void write(char[] cbuf, int off, int len) throws IOException {
super.write(cbuf, off, len);
for (int i = off; i < off + len; i++) {
if (cbuf[i] == '\n') {
line++;
column = 0;
} else column++;
}
}

@Override
public void write(String str, int off, int len) throws IOException {
super.write(str, off, len);
for (int i = off; i < off + len; i++) {
if (str.charAt(i) == '\n') {
line++;
column = 0;
} else column++;
}
}

public int getLine() {
return line;
}

public int getColumn() {
return line;
}
}

0 comments on commit d6c8b4a

Please sign in to comment.