68
68
import com .oracle .graal .python .builtins .objects .dict .PDict ;
69
69
import com .oracle .graal .python .builtins .objects .ints .PInt ;
70
70
import com .oracle .graal .python .builtins .objects .iterator .PStringIterator ;
71
+ import com .oracle .graal .python .builtins .objects .list .ListBuiltins .ListAppendNode ;
72
+ import com .oracle .graal .python .builtins .objects .list .ListBuiltins .ListReverseNode ;
71
73
import com .oracle .graal .python .builtins .objects .list .PList ;
72
74
import com .oracle .graal .python .builtins .objects .slice .PSlice ;
73
75
import com .oracle .graal .python .builtins .objects .slice .PSlice .SliceInfo ;
80
82
import com .oracle .graal .python .nodes .function .PythonBuiltinBaseNode ;
81
83
import com .oracle .graal .python .nodes .function .PythonBuiltinNode ;
82
84
import com .oracle .graal .python .nodes .function .builtins .PythonBinaryBuiltinNode ;
85
+ import com .oracle .graal .python .nodes .function .builtins .PythonTernaryBuiltinNode ;
83
86
import com .oracle .graal .python .nodes .function .builtins .PythonUnaryBuiltinNode ;
84
87
import com .oracle .graal .python .nodes .object .GetClassNode ;
85
88
import com .oracle .graal .python .nodes .truffle .PythonArithmeticTypes ;
@@ -771,27 +774,51 @@ public String lower(String self) {
771
774
public abstract static class RPartitionNode extends PythonBuiltinNode {
772
775
@ Specialization
773
776
@ TruffleBoundary
774
- public PList doSplit (String self , String sep ) {
777
+ public PList doSplit (String self , String sep ,
778
+ @ Cached ("create()" ) ListAppendNode appendNode ) {
775
779
int lastIndexOf = self .lastIndexOf (sep );
776
780
PList list = factory ().createList ();
777
781
if (lastIndexOf == -1 ) {
778
- list . append ( "" );
779
- list . append ( "" );
780
- list . append ( self );
782
+ appendNode . execute ( list , "" );
783
+ appendNode . execute ( list , "" );
784
+ appendNode . execute ( list , self );
781
785
} else {
782
- list . append ( self .substring (0 , lastIndexOf ));
783
- list . append ( sep );
784
- list . append ( self .substring (lastIndexOf + sep .length ()));
786
+ appendNode . execute ( list , self .substring (0 , lastIndexOf ));
787
+ appendNode . execute ( list , sep );
788
+ appendNode . execute ( list , self .substring (lastIndexOf + sep .length ()));
785
789
}
786
790
return list ;
787
791
}
788
792
}
789
793
794
+ protected abstract static class SplitBaseNode extends PythonTernaryBuiltinNode {
795
+
796
+ @ Child private ListAppendNode appendNode ;
797
+ @ Child private ListReverseNode reverseNode ;
798
+
799
+ protected ListAppendNode getAppendNode () {
800
+ if (appendNode == null ) {
801
+ CompilerDirectives .transferToInterpreterAndInvalidate ();
802
+ appendNode = insert (ListAppendNode .create ());
803
+ }
804
+ return appendNode ;
805
+ }
806
+
807
+ protected ListReverseNode getReverseNode () {
808
+ if (reverseNode == null ) {
809
+ CompilerDirectives .transferToInterpreterAndInvalidate ();
810
+ reverseNode = insert (ListReverseNode .create ());
811
+ }
812
+ return reverseNode ;
813
+ }
814
+
815
+ }
816
+
790
817
// str.split
791
818
@ Builtin (name = "split" , maxNumOfPositionalArgs = 3 )
792
819
@ GenerateNodeFactory
793
820
@ TypeSystemReference (PythonArithmeticTypes .class )
794
- public abstract static class SplitNode extends PythonBuiltinNode {
821
+ public abstract static class SplitNode extends SplitBaseNode {
795
822
796
823
@ SuppressWarnings ("unused" )
797
824
@ Specialization
@@ -806,7 +833,7 @@ public PList doSplit(String self, String sep, PNone maxsplit) {
806
833
PList list = factory ().createList ();
807
834
String [] strs = self .split (Pattern .quote (sep ));
808
835
for (String s : strs ) {
809
- list . append ( s );
836
+ getAppendNode (). execute ( list , s );
810
837
}
811
838
return list ;
812
839
}
@@ -819,7 +846,7 @@ public PList doSplit(String self, String sep, int maxsplit) {
819
846
// parts
820
847
String [] strs = self .split (Pattern .quote (sep ), maxsplit + 1 );
821
848
for (String s : strs ) {
822
- list . append ( s );
849
+ getAppendNode (). execute ( list , s );
823
850
}
824
851
return list ;
825
852
}
@@ -883,7 +910,7 @@ private PList splitfields(String s, int maxsplit) {
883
910
}
884
911
885
912
// Make a piece from start up to index
886
- list . append ( s .substring (start , index ));
913
+ getAppendNode (). execute ( list , s .substring (start , index ));
887
914
splits ++;
888
915
889
916
// Start next segment search at that point
@@ -898,7 +925,7 @@ private PList splitfields(String s, int maxsplit) {
898
925
@ Builtin (name = "rsplit" , maxNumOfPositionalArgs = 3 )
899
926
@ GenerateNodeFactory
900
927
@ TypeSystemReference (PythonArithmeticTypes .class )
901
- public abstract static class RSplitNode extends PythonBuiltinNode {
928
+ public abstract static class RSplitNode extends SplitBaseNode {
902
929
903
930
@ SuppressWarnings ("unused" )
904
931
@ Specialization
@@ -912,8 +939,9 @@ public PList doSplit(String self, PNone sep, PNone maxsplit) {
912
939
public PList doSplit (String self , String sep , PNone maxsplit ) {
913
940
PList list = factory ().createList ();
914
941
String [] strs = self .split (Pattern .quote (sep ));
915
- for (String s : strs )
916
- list .append (s );
942
+ for (String s : strs ) {
943
+ getAppendNode ().execute (list , s );
944
+ }
917
945
return list ;
918
946
}
919
947
@@ -930,17 +958,17 @@ public PList doSplit(String self, String sep, int maxsplit) {
930
958
break ;
931
959
}
932
960
933
- list . append ( self .substring (idx + 1 , end ));
961
+ getAppendNode (). execute ( list , self .substring (idx + 1 , end ));
934
962
end = idx ;
935
963
splits ++;
936
964
remainder = remainder .substring (0 , end );
937
965
}
938
966
939
967
if (!remainder .isEmpty ()) {
940
- list . append ( remainder );
968
+ getAppendNode (). execute ( list , remainder );
941
969
}
942
970
943
- list . reverse ( );
971
+ getReverseNode (). execute ( list );
944
972
return list ;
945
973
}
946
974
@@ -1003,14 +1031,14 @@ private PList rsplitfields(String s, int maxsplit) {
1003
1031
}
1004
1032
1005
1033
// Make a piece from start up to index
1006
- list . append ( s .substring (index + 1 , end + 1 ));
1034
+ getAppendNode (). execute ( list , s .substring (index + 1 , end + 1 ));
1007
1035
splits ++;
1008
1036
1009
1037
// Start next segment search at that point
1010
1038
end = index ;
1011
1039
}
1012
1040
1013
- list . reverse ( );
1041
+ getReverseNode (). execute ( list );
1014
1042
return list ;
1015
1043
}
1016
1044
}
0 commit comments