Skip to content

Commit 2990f78

Browse files
committed
Add nqp::setinputlinesep for JVM.
1 parent eccf03e commit 2990f78

File tree

5 files changed

+49
-10
lines changed

5 files changed

+49
-10
lines changed

src/vm/jvm/QAST/Compiler.nqp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1889,6 +1889,7 @@ QAST::OperationsJAST.map_classlib_core_op('getstdin', $TYPE_OPS, 'getstdin', [],
18891889
QAST::OperationsJAST.map_classlib_core_op('getstdout', $TYPE_OPS, 'getstdout', [], $RT_OBJ, :tc);
18901890
QAST::OperationsJAST.map_classlib_core_op('getstderr', $TYPE_OPS, 'getstderr', [], $RT_OBJ, :tc);
18911891
QAST::OperationsJAST.map_classlib_core_op('setencoding', $TYPE_OPS, 'setencoding', [$RT_OBJ, $RT_STR], $RT_OBJ, :tc);
1892+
QAST::OperationsJAST.map_classlib_core_op('setinputlinesep', $TYPE_OPS, 'setinputlinesep', [$RT_OBJ, $RT_STR], $RT_OBJ, :tc);
18921893
QAST::OperationsJAST.map_classlib_core_op('tellfh', $TYPE_OPS, 'tellfh', [$RT_OBJ], $RT_INT, :tc);
18931894
QAST::OperationsJAST.map_classlib_core_op('readfh', $TYPE_OPS, 'readfh', [$RT_OBJ, $RT_OBJ, $RT_INT], $RT_OBJ, :tc);
18941895
QAST::OperationsJAST.map_classlib_core_op('writefh', $TYPE_OPS, 'writefh', [$RT_OBJ, $RT_OBJ], $RT_OBJ, :tc);
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package org.perl6.nqp.io;
2+
3+
import org.perl6.nqp.runtime.ThreadContext;
4+
5+
public interface IIOLineSeparable {
6+
public void setInputLineSeparator(ThreadContext tc, String sep);
7+
}

src/vm/jvm/runtime/org/perl6/nqp/io/StandardReadHandle.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,11 @@
11
package org.perl6.nqp.io;
22

33
import java.io.BufferedReader;
4-
import java.io.File;
54
import java.io.IOException;
65
import java.io.InputStream;
76
import java.io.InputStreamReader;
87
import java.io.OutputStreamWriter;
9-
import java.nio.ByteBuffer;
10-
import java.nio.CharBuffer;
11-
import java.nio.channels.FileChannel;
128
import java.nio.charset.Charset;
13-
import java.nio.file.Path;
14-
import java.nio.file.StandardOpenOption;
15-
import java.util.ArrayList;
169

1710
import jline.ConsoleReader;
1811

src/vm/jvm/runtime/org/perl6/nqp/io/SyncHandle.java

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,14 @@
1313
import org.perl6.nqp.runtime.ThreadContext;
1414

1515
public abstract class SyncHandle implements IIOClosable, IIOEncodable,
16-
IIOSyncReadable, IIOSyncWritable {
16+
IIOSyncReadable, IIOSyncWritable, IIOLineSeparable {
1717

1818
protected ByteChannel chan;
1919
protected CharsetEncoder enc;
2020
protected CharsetDecoder dec;
2121
protected boolean eof = false;
2222
protected ByteBuffer readBuffer;
23+
protected byte[] linesep = { '\n' };
2324

2425
public void close(ThreadContext tc) {
2526
try {
@@ -84,9 +85,19 @@ public synchronized String readline(ThreadContext tc) {
8485
int start = readBuffer.position();
8586
int end = start;
8687
while (!foundLine && end < readBuffer.limit()) {
87-
if (readBuffer.get(end) == '\n')
88+
int index = 0;
89+
while (index < linesep.length
90+
&& end + index < readBuffer.limit()
91+
&& readBuffer.get(end + index) == linesep[index])
92+
index++;
93+
94+
if (index == linesep.length)
95+
{
96+
end += index;
8897
foundLine = true;
89-
end++;
98+
} else {
99+
end++;
100+
}
90101
}
91102

92103
/* Copy what we found into the results. */
@@ -169,4 +180,13 @@ public void say(ThreadContext tc, String s) {
169180
print(tc, s);
170181
print(tc, System.lineSeparator());
171182
}
183+
184+
public void setInputLineSeparator(ThreadContext tc, String sep) {
185+
try {
186+
linesep = enc.charset().newEncoder().encode(
187+
CharBuffer.wrap(sep)).array();
188+
} catch (IOException e) {
189+
throw ExceptionHandling.dieInternal(tc, e);
190+
}
191+
}
172192
}

src/vm/jvm/runtime/org/perl6/nqp/runtime/Ops.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import org.perl6.nqp.io.IIOClosable;
4040
import org.perl6.nqp.io.IIOEncodable;
4141
import org.perl6.nqp.io.IIOInteractive;
42+
import org.perl6.nqp.io.IIOLineSeparable;
4243
import org.perl6.nqp.io.IIOSeekable;
4344
import org.perl6.nqp.io.IIOSyncReadable;
4445
import org.perl6.nqp.io.IIOSyncWritable;
@@ -444,6 +445,23 @@ else if (encoding.equals("binary"))
444445
return obj;
445446
}
446447

448+
public static SixModelObject setinputlinesep(SixModelObject obj, String sep, ThreadContext tc) {
449+
if (obj instanceof IOHandleInstance) {
450+
IOHandleInstance h = (IOHandleInstance)obj;
451+
452+
if (h.handle instanceof IIOLineSeparable)
453+
((IIOLineSeparable)h.handle).setInputLineSeparator(tc, sep);
454+
else
455+
throw ExceptionHandling.dieInternal(tc,
456+
"This handle does not support setting input line separator");
457+
}
458+
else {
459+
throw ExceptionHandling.dieInternal(tc,
460+
"setinputlinesep requires an object with the IOHandle REPR");
461+
}
462+
return obj;
463+
}
464+
447465
public static long tellfh(SixModelObject obj, ThreadContext tc) {
448466
if (obj instanceof IOHandleInstance) {
449467
IOHandleInstance h = (IOHandleInstance)obj;

0 commit comments

Comments
 (0)