|
53 | 53 | import com.oracle.graal.python.builtins.PythonBuiltins;
|
54 | 54 | import com.oracle.graal.python.builtins.objects.PNone;
|
55 | 55 | import com.oracle.graal.python.builtins.objects.PNotImplemented;
|
| 56 | +import com.oracle.graal.python.builtins.objects.common.HashingCollectionNodes.SetItemNode; |
56 | 57 | import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes;
|
57 | 58 | import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes.NormalizeIndexNode;
|
| 59 | +import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes.ToByteArrayNode; |
| 60 | +import com.oracle.graal.python.builtins.objects.dict.PDict; |
58 | 61 | import com.oracle.graal.python.builtins.objects.iterator.PSequenceIterator;
|
59 | 62 | import com.oracle.graal.python.builtins.objects.list.PList;
|
60 | 63 | import com.oracle.graal.python.nodes.PNodeWithContext;
|
@@ -513,4 +516,30 @@ Object getitem(PBytes self, Object idx, Object value) {
|
513 | 516 | throw raise(TypeError, "'bytes' object does not support item assignment");
|
514 | 517 | }
|
515 | 518 | }
|
| 519 | + |
| 520 | + // static str.maketrans() |
| 521 | + @Builtin(name = "maketrans", fixedNumOfPositionalArgs = 2) |
| 522 | + @GenerateNodeFactory |
| 523 | + public abstract static class MakeTransNode extends PythonBuiltinNode { |
| 524 | + |
| 525 | + @Specialization |
| 526 | + public PDict maketrans(PBytes from, PBytes to, |
| 527 | + @Cached("create()") SetItemNode setItemNode, |
| 528 | + @Cached("create()") ToByteArrayNode toByteArrayNode) { |
| 529 | + byte[] fromB = toByteArrayNode.execute(from.getSequenceStorage()); |
| 530 | + byte[] toB = toByteArrayNode.execute(to.getSequenceStorage()); |
| 531 | + if (fromB.length != toB.length) { |
| 532 | + throw new RuntimeException("maketrans arguments must have same length"); |
| 533 | + } |
| 534 | + |
| 535 | + PDict translation = factory().createDict(); |
| 536 | + for (int i = 0; i < fromB.length; i++) { |
| 537 | + int key = fromB[i]; |
| 538 | + int value = toB[i]; |
| 539 | + setItemNode.execute(translation, key, value); |
| 540 | + } |
| 541 | + |
| 542 | + return translation; |
| 543 | + } |
| 544 | + } |
516 | 545 | }
|
0 commit comments