|
65 | 65 |
|
66 | 66 | import com.ibm.icu.lang.UCharacter;
|
67 | 67 | import com.ibm.icu.lang.UProperty;
|
| 68 | +import com.ibm.icu.text.CaseMap; |
68 | 69 | import com.oracle.graal.python.PythonLanguage;
|
69 | 70 | import com.oracle.graal.python.annotations.ArgumentClinic;
|
70 | 71 | import com.oracle.graal.python.annotations.ArgumentClinic.ClinicConversion;
|
|
136 | 137 | import com.oracle.graal.python.util.PythonUtils;
|
137 | 138 | import com.oracle.truffle.api.CompilerAsserts;
|
138 | 139 | import com.oracle.truffle.api.CompilerDirectives;
|
| 140 | +import com.oracle.truffle.api.CompilerDirectives.CompilationFinal; |
139 | 141 | import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
|
140 | 142 | import com.oracle.truffle.api.dsl.Cached;
|
141 | 143 | import com.oracle.truffle.api.dsl.Cached.Exclusive;
|
@@ -920,19 +922,34 @@ static String doGeneric(Object self,
|
920 | 922 | @GenerateNodeFactory
|
921 | 923 | public abstract static class CapitalizeNode extends PythonUnaryBuiltinNode {
|
922 | 924 |
|
| 925 | + @CompilationFinal private static CaseMap.Title titlecaser; |
| 926 | + |
| 927 | + @Specialization |
| 928 | + static String capitalize(String self) { |
| 929 | + if (self.isEmpty()) { |
| 930 | + return ""; |
| 931 | + } else { |
| 932 | + return capitalizeImpl(self); |
| 933 | + } |
| 934 | + } |
| 935 | + |
923 | 936 | @Specialization
|
924 | 937 | static String doGeneric(Object self,
|
925 | 938 | @Cached CastToJavaStringCheckedNode castToJavaStringNode) {
|
926 | 939 | return capitalize(castToJavaStringNode.cast(self, ErrorMessages.REQUIRES_STR_OBJECT_BUT_RECEIVED_P, "capitalize", self));
|
927 | 940 | }
|
928 | 941 |
|
929 |
| - @TruffleBoundary |
930 |
| - private static String capitalize(String self) { |
931 |
| - if (self.isEmpty()) { |
932 |
| - return ""; |
933 |
| - } else { |
934 |
| - return self.substring(0, 1).toUpperCase() + self.substring(1).toLowerCase(); |
| 942 | + private static String capitalizeImpl(String str) { |
| 943 | + if (titlecaser == null) { |
| 944 | + CompilerDirectives.transferToInterpreterAndInvalidate(); |
| 945 | + titlecaser = CaseMap.toTitle().wholeString().noBreakAdjustment(); |
935 | 946 | }
|
| 947 | + return apply(str); |
| 948 | + } |
| 949 | + |
| 950 | + @TruffleBoundary |
| 951 | + private static String apply(String str) { |
| 952 | + return titlecaser.apply(Locale.ROOT, null, str); |
936 | 953 | }
|
937 | 954 | }
|
938 | 955 |
|
|
0 commit comments