Skip to content

Commit d5f156f

Browse files
committed
Cleanup hpy_jni.c: augment_UnicodeFromWideChar
1 parent 14a450f commit d5f156f

File tree

1 file changed

+11
-38
lines changed
  • graalpython/com.oracle.graal.python.jni/src

1 file changed

+11
-38
lines changed

graalpython/com.oracle.graal.python.jni/src/hpy_jni.c

Lines changed: 11 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -330,69 +330,42 @@ static int augment_ListCheck(HPyContext ctx, HPy obj) {
330330
}
331331
}
332332

333+
#define MAX_UNICODE 0x10ffff
333334

334335
static HPy augment_UnicodeFromWideChar(HPyContext ctx, const wchar_t *u, HPy_ssize_t size) {
335336
if (u == NULL && size != 0) {
336337
return HPy_NULL;
337338
}
338339

340+
if (sizeof(wchar_t) != sizeof(uint32_t)) {
341+
HPyErr_SetString(ctx, ctx->h_SystemError, "unsupported size of type wchar_t");
342+
return HPy_NULL;
343+
}
344+
339345
if (size == -1) {
340346
size = wcslen(u);
341347
}
342348

343349
if (size > INT32_MAX) {
344-
/* TODO(fa): error message */
350+
HPyErr_SetString(ctx, ctx->h_SystemError, "wchar_t array is too large");
345351
return HPy_NULL;
346352
}
347353

348-
/* If the Unicode data is known at construction time, we can apply
349-
some optimizations which share commonly used objects. */
350-
351-
/* Optimization for empty strings */
352-
/* TODO(fa)
353-
if (size == 0)
354-
_Py_RETURN_UNICODE_EMPTY();
355-
*/
356-
357-
/* Single character Unicode objects in the Latin-1 range are
358-
shared when using this constructor */
359-
/* TODO(fa)
360-
if (size == 1 && (Py_UCS4)*u < 256)
361-
return get_latin1_char((unsigned char)*u);
362-
*/
363-
364-
/* If not empty and not single character, copy the Unicode data
365-
into the new object */
366354
uint32_t maxchar = 0;
367355
wchar_t ch;
368356
HPy_ssize_t i;
369357
for (i = 0; i < size; i++) {
370-
#if SIZEOF_WCHAR_T == 2
371-
if (Py_UNICODE_IS_HIGH_SURROGATE(iter[0])
372-
&& (iter+1) < end
373-
&& Py_UNICODE_IS_LOW_SURROGATE(iter[1]))
374-
{
375-
ch = Py_UNICODE_JOIN_SURROGATES(iter[0], iter[1]);
376-
++(*num_surrogates);
377-
iter += 2;
378-
}
379-
else
380-
#endif
381358
ch = u[i];
382359
if (ch > maxchar) {
383360
maxchar = ch;
384-
/* TODO(fa): error
385-
if (*maxchar > MAX_UNICODE) {
386-
PyErr_Format(PyExc_ValueError,
387-
"character U+%x is not in range [U+0000; U+10ffff]",
388-
ch);
389-
return -1;
361+
if (maxchar > MAX_UNICODE) {
362+
HPyErr_SetString(ctx, ctx->h_ValueError, "character is not in range [U+0000; U+10ffff]");
363+
return HPy_NULL;
390364
}
391-
*/
392365
}
393366
}
394367

395-
if (maxchar < 65536) {
368+
if (maxchar < UINT16_MAX) {
396369
jarray jCharArray = (*jniEnv)->NewCharArray(jniEnv, (jsize) size);
397370
jchar *content = (*jniEnv)->GetPrimitiveArrayCritical(jniEnv, jCharArray, 0);
398371
HPy_ssize_t i;

0 commit comments

Comments
 (0)