Skip to content

Commit c1f0e4c

Browse files
committed
[Truffle] - Implements String#chr
1 parent dbff750 commit c1f0e4c

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

core/src/main/java/org/jruby/truffle/nodes/core/StringNodes.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1249,7 +1249,6 @@ public RubyString capitalize(RubyString string) {
12491249

12501250
}
12511251

1252-
12531252
@CoreMethod(names = "clear")
12541253
public abstract static class ClearNode extends CoreMethodNode {
12551254

@@ -1272,4 +1271,30 @@ public RubyString clear(RubyString string) {
12721271
}
12731272
}
12741273

1274+
@CoreMethod(names = "chr")
1275+
public abstract static class ChrNode extends CoreMethodNode {
1276+
1277+
public ChrNode(RubyContext context, SourceSection sourceSection) {
1278+
super(context, sourceSection);
1279+
}
1280+
1281+
public ChrNode(ChrNode prev) {
1282+
super(prev);
1283+
}
1284+
1285+
@Specialization
1286+
public RubyString chr(RubyString string) {
1287+
notDesignedForCompilation();
1288+
if (string.toString().isEmpty()) {
1289+
return string;
1290+
} else {
1291+
String head = string.toString().substring(0, 1);
1292+
ByteList byteString = ByteList.create(head);
1293+
byteString.setEncoding(string.getBytes().getEncoding());
1294+
1295+
return string.getContext().makeString(byteString);
1296+
}
1297+
}
1298+
}
1299+
12751300
}

0 commit comments

Comments
 (0)