|
40 | 40 | */
|
41 | 41 | package com.oracle.graal.python.builtins.modules;
|
42 | 42 |
|
| 43 | +import static com.oracle.graal.python.runtime.exception.PythonErrorType.TypeError; |
43 | 44 | import static com.oracle.graal.python.nodes.BuiltinNames.J_UNICODEDATA;
|
44 | 45 | import static com.oracle.graal.python.nodes.BuiltinNames.T_UNICODEDATA;
|
45 | 46 | import static com.oracle.graal.python.runtime.exception.PythonErrorType.KeyError;
|
|
50 | 51 | import java.util.List;
|
51 | 52 |
|
52 | 53 | import com.oracle.graal.python.builtins.objects.module.PythonModule;
|
| 54 | +import com.oracle.graal.python.nodes.function.builtins.PythonUnaryBuiltinNode; |
| 55 | +import com.oracle.graal.python.nodes.util.CastToTruffleStringNode; |
| 56 | +import com.oracle.truffle.api.strings.TruffleString.CodePointAtByteIndexNode; |
| 57 | +import com.oracle.truffle.api.strings.TruffleString.CodePointLengthNode; |
53 | 58 | import com.oracle.truffle.api.strings.TruffleString.FromJavaStringNode;
|
54 | 59 | import com.oracle.truffle.api.strings.TruffleString.ToJavaStringNode;
|
55 | 60 | import org.graalvm.shadowed.com.ibm.icu.lang.UCharacter;
|
@@ -327,4 +332,52 @@ protected ArgumentClinicProvider getArgumentClinic() {
|
327 | 332 | return UnicodeDataModuleBuiltinsClinicProviders.CategoryNodeClinicProviderGen.INSTANCE;
|
328 | 333 | }
|
329 | 334 | }
|
| 335 | + |
| 336 | + // unicode.east_asia_width(chr) |
| 337 | + @Builtin(name = "east_asian_width", minNumOfPositionalArgs = 1, numOfPositionalOnlyArgs = 1, parameterNames = {"chr"}) |
| 338 | + @GenerateNodeFactory |
| 339 | + public abstract static class EastAsianWidthNode extends PythonUnaryBuiltinNode { |
| 340 | + @Specialization |
| 341 | + @TruffleBoundary |
| 342 | + static TruffleString eastAsianWidth(Object object, |
| 343 | + @Bind Node inliningTarget, |
| 344 | + @Cached CastToTruffleStringNode castToTruffleStringNode, |
| 345 | + @Cached CodePointLengthNode codePointLengthNode, |
| 346 | + @Cached CodePointAtByteIndexNode codePointAtByteIndexNode, |
| 347 | + @Cached FromJavaStringNode fromJavaStringNode) { |
| 348 | + final TruffleString chr; |
| 349 | + |
| 350 | + try { |
| 351 | + chr = CastToTruffleStringNode.getUncached().execute(inliningTarget, object); |
| 352 | + } catch (CannotCastException e) { |
| 353 | + throw PRaiseNode.raiseStatic(inliningTarget, TypeError, ErrorMessages.S_ARG_MUST_BE_S_NOT_P, "east_asian_width()", "a unicode character", object); |
| 354 | + } |
| 355 | + |
| 356 | + if (CodePointLengthNode.getUncached().execute(chr, TS_ENCODING) != 1) { |
| 357 | + throw PRaiseNode.raiseStatic(inliningTarget, TypeError, ErrorMessages.S_ARG_MUST_BE_S_NOT_P, "east_asian_width()", "a unicode character", object); |
| 358 | + } |
| 359 | + |
| 360 | + int codepoint = CodePointAtByteIndexNode.getUncached().execute(chr, 0, TS_ENCODING); |
| 361 | + String widthName = getWidthName(codepoint); |
| 362 | + return fromJavaStringNode.execute(widthName, TS_ENCODING); |
| 363 | + } |
| 364 | + |
| 365 | + @TruffleBoundary |
| 366 | + private static String getWidthName(int codepoint) { |
| 367 | + int widthNameCode = UCharacter.getIntPropertyValue(codepoint, UProperty.EAST_ASIAN_WIDTH); |
| 368 | + String widthName; |
| 369 | + |
| 370 | + switch (widthNameCode) { |
| 371 | + case UCharacter.EastAsianWidth.AMBIGUOUS -> widthName = "A"; |
| 372 | + case UCharacter.EastAsianWidth.FULLWIDTH -> widthName = "F"; |
| 373 | + case UCharacter.EastAsianWidth.HALFWIDTH -> widthName = "H"; |
| 374 | + case UCharacter.EastAsianWidth.NARROW -> widthName = "Na"; |
| 375 | + case UCharacter.EastAsianWidth.NEUTRAL -> widthName = "N"; |
| 376 | + case UCharacter.EastAsianWidth.WIDE -> widthName = "W"; |
| 377 | + default -> widthName = ""; // EastAsianWidth.COUNT |
| 378 | + } |
| 379 | + |
| 380 | + return widthName; |
| 381 | + } |
| 382 | + } |
330 | 383 | }
|
0 commit comments