|
49 | 49 | import static com.oracle.graal.python.runtime.exception.PythonErrorType.UnicodeEncodeError;
|
50 | 50 | import static com.oracle.graal.python.runtime.exception.PythonErrorType.ValueError;
|
51 | 51 |
|
| 52 | +import java.math.BigInteger; |
52 | 53 | import java.nio.ByteBuffer;
|
53 | 54 | import java.nio.CharBuffer;
|
54 | 55 | import java.nio.charset.CharacterCodingException;
|
|
77 | 78 | import com.oracle.graal.python.builtins.objects.slice.PSlice;
|
78 | 79 | import com.oracle.graal.python.builtins.objects.slice.PSlice.SliceInfo;
|
79 | 80 | import com.oracle.graal.python.builtins.objects.str.StringBuiltinsFactory.SpliceNodeGen;
|
| 81 | +import com.oracle.graal.python.builtins.objects.str.StringBuiltinsFactory.StringLenNodeFactory; |
80 | 82 | import com.oracle.graal.python.builtins.objects.tuple.PTuple;
|
81 | 83 | import com.oracle.graal.python.nodes.PNodeWithContext;
|
82 | 84 | import com.oracle.graal.python.nodes.SpecialMethodNames;
|
|
107 | 109 | import com.oracle.truffle.api.dsl.TypeSystemReference;
|
108 | 110 | import com.oracle.truffle.api.profiles.BranchProfile;
|
109 | 111 | import com.oracle.truffle.api.profiles.ConditionProfile;
|
110 |
| -import java.math.BigInteger; |
| 112 | +import com.oracle.truffle.api.profiles.ValueProfile; |
111 | 113 |
|
112 | 114 | @CoreFunctions(extendClasses = PythonBuiltinClassType.PString)
|
113 | 115 | public final class StringBuiltins extends PythonBuiltins {
|
@@ -1406,15 +1408,29 @@ String rstrip(String self, PNone chars) {
|
1406 | 1408 |
|
1407 | 1409 | @Builtin(name = SpecialMethodNames.__LEN__, minNumOfPositionalArgs = 1)
|
1408 | 1410 | @GenerateNodeFactory
|
1409 |
| - public abstract static class LenNode extends PythonUnaryBuiltinNode { |
| 1411 | + public abstract static class StringLenNode extends PythonUnaryBuiltinNode { |
1410 | 1412 | @Specialization
|
1411 | 1413 | public int len(String self) {
|
1412 | 1414 | return self.length();
|
1413 | 1415 | }
|
1414 | 1416 |
|
1415 | 1417 | @Specialization
|
1416 |
| - public int len(PString self) { |
1417 |
| - return self.len(); |
| 1418 | + public int len(PString self, |
| 1419 | + @Cached("createClassProfile()") ValueProfile classProfile, |
| 1420 | + @Cached("create()") BranchProfile uncommonStringTypeProfile) { |
| 1421 | + Object profiled = classProfile.profile(self.getCharSequence()); |
| 1422 | + if (profiled instanceof String) { |
| 1423 | + return ((String) profiled).length(); |
| 1424 | + } else if (profiled instanceof LazyString) { |
| 1425 | + return ((LazyString) profiled).length(); |
| 1426 | + } else { |
| 1427 | + uncommonStringTypeProfile.enter(); |
| 1428 | + return ((CharSequence) profiled).length(); |
| 1429 | + } |
| 1430 | + } |
| 1431 | + |
| 1432 | + public static StringLenNode create() { |
| 1433 | + return StringLenNodeFactory.create(); |
1418 | 1434 | }
|
1419 | 1435 | }
|
1420 | 1436 |
|
|
0 commit comments