Guest User

Untitled

a guest
Jan 19th, 2024
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.98 KB | None | 0 0
  1. diff --git a/plugins/cfpython/cjson.cpp b/plugins/cfpython/cjson.cpp
  2. index 5426c4dbb..d7bd84e2a 100644
  3. --- a/plugins/cfpython/cjson.cpp
  4. +++ b/plugins/cfpython/cjson.cpp
  5. @@ -657,18 +657,14 @@ static PyObject *encode_string(PyObject *string) {
  6. */
  7. static PyObject *encode_unicode(PyObject *unicode) {
  8. PyObject *repr;
  9. - Py_UNICODE *s;
  10. - Py_ssize_t size;
  11. + Py_ssize_t size, pos;
  12. char *p;
  13. static const char *hexdigit = "0123456789abcdef";
  14. -#ifdef Py_UNICODE_WIDE
  15. static const Py_ssize_t expandsize = 10;
  16. -#else
  17. - static const Py_ssize_t expandsize = 6;
  18. -#endif
  19.  
  20. - s = PyUnicode_AS_UNICODE(unicode);
  21. - size = PyUnicode_GET_SIZE(unicode);
  22. + int kind = PyUnicode_KIND(unicode);
  23. + void *data = PyUnicode_DATA(unicode);
  24. + size = PyUnicode_GET_LENGTH(unicode);
  25.  
  26. if (size > (PY_SSIZE_T_MAX-2-1)/expandsize) {
  27. PyErr_SetString(PyExc_OverflowError, "unicode object is too large to make repr");
  28. @@ -685,17 +681,21 @@ static PyObject *encode_unicode(PyObject *unicode) {
  29. p = PyByteArray_AS_STRING(repr);
  30.  
  31. *p++ = '"';
  32. + pos = 0;
  33.  
  34. - while (size-- > 0) {
  35. - Py_UNICODE ch = *s++;
  36. + const Py_UCS4 quote = PyByteArray_AS_STRING(repr)[0];
  37. +
  38. + while (pos < size) {
  39. + Py_UCS4 ch = PyUnicode_READ(kind, data, pos);
  40. + pos++;
  41.  
  42. /* Escape quotes */
  43. - if ((ch == (Py_UNICODE)PyByteArray_AS_STRING(repr)[0] || ch == '\\')) {
  44. + if ((ch == quote || ch == '\\')) {
  45. *p++ = '\\';
  46. *p++ = (char)ch;
  47. continue;
  48. }
  49. -#ifdef Py_UNICODE_WIDE
  50. +
  51. /* Map 21-bit characters to '\U00xxxxxx' */
  52. else if (ch >= 0x10000) {
  53. *p++ = '\\';
  54. @@ -710,33 +710,6 @@ static PyObject *encode_unicode(PyObject *unicode) {
  55. *p++ = hexdigit[ch&0x0000000F];
  56. continue;
  57. }
  58. -#else
  59. - /* Map UTF-16 surrogate pairs to Unicode \UXXXXXXXX escapes */
  60. - else if (ch >= 0xD800 && ch < 0xDC00) {
  61. - Py_UNICODE ch2;
  62. - Py_UCS4 ucs;
  63. -
  64. - ch2 = *s++;
  65. - size--;
  66. - if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) {
  67. - ucs = (((ch&0x03FF)<<10)|(ch2&0x03FF))+0x00010000;
  68. - *p++ = '\\';
  69. - *p++ = 'U';
  70. - *p++ = hexdigit[(ucs>>28)&0x0000000F];
  71. - *p++ = hexdigit[(ucs>>24)&0x0000000F];
  72. - *p++ = hexdigit[(ucs>>20)&0x0000000F];
  73. - *p++ = hexdigit[(ucs>>16)&0x0000000F];
  74. - *p++ = hexdigit[(ucs>>12)&0x0000000F];
  75. - *p++ = hexdigit[(ucs>>8)&0x0000000F];
  76. - *p++ = hexdigit[(ucs>>4)&0x0000000F];
  77. - *p++ = hexdigit[ucs&0x0000000F];
  78. - continue;
  79. - }
  80. - /* Fall through: isolated surrogates are copied as-is */
  81. - s--;
  82. - size++;
  83. - }
  84. -#endif
  85. /* Map 16-bit characters to '\uxxxx' */
  86. if (ch >= 256) {
  87. *p++ = '\\';
  88.  
Advertisement
Add Comment
Please, Sign In to add comment