You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

297 lines
7.5 KiB

  1. /*
  2. Unicode character type helpers.
  3. Written by Marc-Andre Lemburg (mal@lemburg.com).
  4. Modified for Python 2.0 by Fredrik Lundh (fredrik@pythonware.com)
  5. Copyright (c) Corporation for National Research Initiatives.
  6. */
  7. #include "Python.h"
  8. #define ALPHA_MASK 0x01
  9. #define DECIMAL_MASK 0x02
  10. #define DIGIT_MASK 0x04
  11. #define LOWER_MASK 0x08
  12. #define LINEBREAK_MASK 0x10
  13. #define SPACE_MASK 0x20
  14. #define TITLE_MASK 0x40
  15. #define UPPER_MASK 0x80
  16. #define XID_START_MASK 0x100
  17. #define XID_CONTINUE_MASK 0x200
  18. #define PRINTABLE_MASK 0x400
  19. #define NUMERIC_MASK 0x800
  20. #define CASE_IGNORABLE_MASK 0x1000
  21. #define CASED_MASK 0x2000
  22. #define EXTENDED_CASE_MASK 0x4000
  23. typedef struct {
  24. /*
  25. These are either deltas to the character or offsets in
  26. _PyUnicode_ExtendedCase.
  27. */
  28. const int upper;
  29. const int lower;
  30. const int title;
  31. /* Note if more flag space is needed, decimal and digit could be unified. */
  32. const unsigned char decimal;
  33. const unsigned char digit;
  34. const unsigned short flags;
  35. } _PyUnicode_TypeRecord;
  36. #include "unicodetype_db.h"
  37. static const _PyUnicode_TypeRecord *
  38. gettyperecord(Py_UCS4 code)
  39. {
  40. int index;
  41. if (code >= 0x110000)
  42. index = 0;
  43. else
  44. {
  45. index = index1[(code>>SHIFT)];
  46. index = index2[(index<<SHIFT)+(code&((1<<SHIFT)-1))];
  47. }
  48. return &_PyUnicode_TypeRecords[index];
  49. }
  50. /* Returns the titlecase Unicode characters corresponding to ch or just
  51. ch if no titlecase mapping is known. */
  52. Py_UCS4 _PyUnicode_ToTitlecase(Py_UCS4 ch)
  53. {
  54. const _PyUnicode_TypeRecord *ctype = gettyperecord(ch);
  55. if (ctype->flags & EXTENDED_CASE_MASK)
  56. return _PyUnicode_ExtendedCase[ctype->title & 0xFFFF];
  57. return ch + ctype->title;
  58. }
  59. /* Returns 1 for Unicode characters having the category 'Lt', 0
  60. otherwise. */
  61. int _PyUnicode_IsTitlecase(Py_UCS4 ch)
  62. {
  63. const _PyUnicode_TypeRecord *ctype = gettyperecord(ch);
  64. return (ctype->flags & TITLE_MASK) != 0;
  65. }
  66. /* Returns 1 for Unicode characters having the XID_Start property, 0
  67. otherwise. */
  68. int _PyUnicode_IsXidStart(Py_UCS4 ch)
  69. {
  70. const _PyUnicode_TypeRecord *ctype = gettyperecord(ch);
  71. return (ctype->flags & XID_START_MASK) != 0;
  72. }
  73. /* Returns 1 for Unicode characters having the XID_Continue property,
  74. 0 otherwise. */
  75. int _PyUnicode_IsXidContinue(Py_UCS4 ch)
  76. {
  77. const _PyUnicode_TypeRecord *ctype = gettyperecord(ch);
  78. return (ctype->flags & XID_CONTINUE_MASK) != 0;
  79. }
  80. /* Returns the integer decimal (0-9) for Unicode characters having
  81. this property, -1 otherwise. */
  82. int _PyUnicode_ToDecimalDigit(Py_UCS4 ch)
  83. {
  84. const _PyUnicode_TypeRecord *ctype = gettyperecord(ch);
  85. return (ctype->flags & DECIMAL_MASK) ? ctype->decimal : -1;
  86. }
  87. int _PyUnicode_IsDecimalDigit(Py_UCS4 ch)
  88. {
  89. if (_PyUnicode_ToDecimalDigit(ch) < 0)
  90. return 0;
  91. return 1;
  92. }
  93. /* Returns the integer digit (0-9) for Unicode characters having
  94. this property, -1 otherwise. */
  95. int _PyUnicode_ToDigit(Py_UCS4 ch)
  96. {
  97. const _PyUnicode_TypeRecord *ctype = gettyperecord(ch);
  98. return (ctype->flags & DIGIT_MASK) ? ctype->digit : -1;
  99. }
  100. int _PyUnicode_IsDigit(Py_UCS4 ch)
  101. {
  102. if (_PyUnicode_ToDigit(ch) < 0)
  103. return 0;
  104. return 1;
  105. }
  106. /* Returns the numeric value as double for Unicode characters having
  107. this property, -1.0 otherwise. */
  108. int _PyUnicode_IsNumeric(Py_UCS4 ch)
  109. {
  110. const _PyUnicode_TypeRecord *ctype = gettyperecord(ch);
  111. return (ctype->flags & NUMERIC_MASK) != 0;
  112. }
  113. /* Returns 1 for Unicode characters to be hex-escaped when repr()ed,
  114. 0 otherwise.
  115. All characters except those characters defined in the Unicode character
  116. database as following categories are considered printable.
  117. * Cc (Other, Control)
  118. * Cf (Other, Format)
  119. * Cs (Other, Surrogate)
  120. * Co (Other, Private Use)
  121. * Cn (Other, Not Assigned)
  122. * Zl Separator, Line ('\u2028', LINE SEPARATOR)
  123. * Zp Separator, Paragraph ('\u2029', PARAGRAPH SEPARATOR)
  124. * Zs (Separator, Space) other than ASCII space('\x20').
  125. */
  126. int _PyUnicode_IsPrintable(Py_UCS4 ch)
  127. {
  128. const _PyUnicode_TypeRecord *ctype = gettyperecord(ch);
  129. return (ctype->flags & PRINTABLE_MASK) != 0;
  130. }
  131. /* Returns 1 for Unicode characters having the category 'Ll', 0
  132. otherwise. */
  133. int _PyUnicode_IsLowercase(Py_UCS4 ch)
  134. {
  135. const _PyUnicode_TypeRecord *ctype = gettyperecord(ch);
  136. return (ctype->flags & LOWER_MASK) != 0;
  137. }
  138. /* Returns 1 for Unicode characters having the category 'Lu', 0
  139. otherwise. */
  140. int _PyUnicode_IsUppercase(Py_UCS4 ch)
  141. {
  142. const _PyUnicode_TypeRecord *ctype = gettyperecord(ch);
  143. return (ctype->flags & UPPER_MASK) != 0;
  144. }
  145. /* Returns the uppercase Unicode characters corresponding to ch or just
  146. ch if no uppercase mapping is known. */
  147. Py_UCS4 _PyUnicode_ToUppercase(Py_UCS4 ch)
  148. {
  149. const _PyUnicode_TypeRecord *ctype = gettyperecord(ch);
  150. if (ctype->flags & EXTENDED_CASE_MASK)
  151. return _PyUnicode_ExtendedCase[ctype->upper & 0xFFFF];
  152. return ch + ctype->upper;
  153. }
  154. /* Returns the lowercase Unicode characters corresponding to ch or just
  155. ch if no lowercase mapping is known. */
  156. Py_UCS4 _PyUnicode_ToLowercase(Py_UCS4 ch)
  157. {
  158. const _PyUnicode_TypeRecord *ctype = gettyperecord(ch);
  159. if (ctype->flags & EXTENDED_CASE_MASK)
  160. return _PyUnicode_ExtendedCase[ctype->lower & 0xFFFF];
  161. return ch + ctype->lower;
  162. }
  163. int _PyUnicode_ToLowerFull(Py_UCS4 ch, Py_UCS4 *res)
  164. {
  165. const _PyUnicode_TypeRecord *ctype = gettyperecord(ch);
  166. if (ctype->flags & EXTENDED_CASE_MASK) {
  167. int index = ctype->lower & 0xFFFF;
  168. int n = ctype->lower >> 24;
  169. int i;
  170. for (i = 0; i < n; i++)
  171. res[i] = _PyUnicode_ExtendedCase[index + i];
  172. return n;
  173. }
  174. res[0] = ch + ctype->lower;
  175. return 1;
  176. }
  177. int _PyUnicode_ToTitleFull(Py_UCS4 ch, Py_UCS4 *res)
  178. {
  179. const _PyUnicode_TypeRecord *ctype = gettyperecord(ch);
  180. if (ctype->flags & EXTENDED_CASE_MASK) {
  181. int index = ctype->title & 0xFFFF;
  182. int n = ctype->title >> 24;
  183. int i;
  184. for (i = 0; i < n; i++)
  185. res[i] = _PyUnicode_ExtendedCase[index + i];
  186. return n;
  187. }
  188. res[0] = ch + ctype->title;
  189. return 1;
  190. }
  191. int _PyUnicode_ToUpperFull(Py_UCS4 ch, Py_UCS4 *res)
  192. {
  193. const _PyUnicode_TypeRecord *ctype = gettyperecord(ch);
  194. if (ctype->flags & EXTENDED_CASE_MASK) {
  195. int index = ctype->upper & 0xFFFF;
  196. int n = ctype->upper >> 24;
  197. int i;
  198. for (i = 0; i < n; i++)
  199. res[i] = _PyUnicode_ExtendedCase[index + i];
  200. return n;
  201. }
  202. res[0] = ch + ctype->upper;
  203. return 1;
  204. }
  205. int _PyUnicode_ToFoldedFull(Py_UCS4 ch, Py_UCS4 *res)
  206. {
  207. const _PyUnicode_TypeRecord *ctype = gettyperecord(ch);
  208. if (ctype->flags & EXTENDED_CASE_MASK && (ctype->lower >> 20) & 7) {
  209. int index = (ctype->lower & 0xFFFF) + (ctype->lower >> 24);
  210. int n = (ctype->lower >> 20) & 7;
  211. int i;
  212. for (i = 0; i < n; i++)
  213. res[i] = _PyUnicode_ExtendedCase[index + i];
  214. return n;
  215. }
  216. return _PyUnicode_ToLowerFull(ch, res);
  217. }
  218. int _PyUnicode_IsCased(Py_UCS4 ch)
  219. {
  220. const _PyUnicode_TypeRecord *ctype = gettyperecord(ch);
  221. return (ctype->flags & CASED_MASK) != 0;
  222. }
  223. int _PyUnicode_IsCaseIgnorable(Py_UCS4 ch)
  224. {
  225. const _PyUnicode_TypeRecord *ctype = gettyperecord(ch);
  226. return (ctype->flags & CASE_IGNORABLE_MASK) != 0;
  227. }
  228. /* Returns 1 for Unicode characters having the category 'Ll', 'Lu', 'Lt',
  229. 'Lo' or 'Lm', 0 otherwise. */
  230. int _PyUnicode_IsAlpha(Py_UCS4 ch)
  231. {
  232. const _PyUnicode_TypeRecord *ctype = gettyperecord(ch);
  233. return (ctype->flags & ALPHA_MASK) != 0;
  234. }