libunicode.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. * Unicode utilities
  3. *
  4. * Copyright (c) 2017-2018 Fabrice Bellard
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. #ifndef LIBUNICODE_H
  25. #define LIBUNICODE_H
  26. #include <stdint.h>
  27. /* unicode standard version */
  28. #define LIBUNICODE_UNICODE_VERSION_MAJOR 17
  29. #define LIBUNICODE_UNICODE_VERSION_MINOR 0
  30. #define LIBUNICODE_UNICODE_VERSION_PATCH 0
  31. /* define it to include all the unicode tables (40KB larger) */
  32. #define CONFIG_ALL_UNICODE
  33. #define LRE_CC_RES_LEN_MAX 3
  34. /* char ranges */
  35. typedef struct {
  36. int len; /* in points, always even */
  37. int size;
  38. uint32_t *points; /* points sorted by increasing value */
  39. void *mem_opaque;
  40. void *(*realloc_func)(void *opaque, void *ptr, size_t size);
  41. } CharRange;
  42. typedef enum {
  43. CR_OP_UNION,
  44. CR_OP_INTER,
  45. CR_OP_XOR,
  46. CR_OP_SUB,
  47. } CharRangeOpEnum;
  48. void cr_init(CharRange *cr, void *mem_opaque, void *(*realloc_func)(void *opaque, void *ptr, size_t size));
  49. void cr_free(CharRange *cr);
  50. int cr_realloc(CharRange *cr, int size);
  51. int cr_copy(CharRange *cr, const CharRange *cr1);
  52. static inline int cr_add_point(CharRange *cr, uint32_t v)
  53. {
  54. if (cr->len >= cr->size) {
  55. if (cr_realloc(cr, cr->len + 1))
  56. return -1;
  57. }
  58. cr->points[cr->len++] = v;
  59. return 0;
  60. }
  61. static inline int cr_add_interval(CharRange *cr, uint32_t c1, uint32_t c2)
  62. {
  63. if ((cr->len + 2) > cr->size) {
  64. if (cr_realloc(cr, cr->len + 2))
  65. return -1;
  66. }
  67. cr->points[cr->len++] = c1;
  68. cr->points[cr->len++] = c2;
  69. return 0;
  70. }
  71. int cr_op(CharRange *cr, const uint32_t *a_pt, int a_len,
  72. const uint32_t *b_pt, int b_len, int op);
  73. int cr_op1(CharRange *cr, const uint32_t *b_pt, int b_len, int op);
  74. static inline int cr_union_interval(CharRange *cr, uint32_t c1, uint32_t c2)
  75. {
  76. uint32_t b_pt[2];
  77. b_pt[0] = c1;
  78. b_pt[1] = c2 + 1;
  79. return cr_op1(cr, b_pt, 2, CR_OP_UNION);
  80. }
  81. int cr_invert(CharRange *cr);
  82. int cr_regexp_canonicalize(CharRange *cr, int is_unicode);
  83. typedef enum {
  84. UNICODE_NFC,
  85. UNICODE_NFD,
  86. UNICODE_NFKC,
  87. UNICODE_NFKD,
  88. } UnicodeNormalizationEnum;
  89. int unicode_normalize(uint32_t **pdst, const uint32_t *src, int src_len,
  90. UnicodeNormalizationEnum n_type,
  91. void *opaque, void *(*realloc_func)(void *opaque, void *ptr, size_t size));
  92. /* Unicode character range functions */
  93. int unicode_script(CharRange *cr, const char *script_name, int is_ext);
  94. int unicode_general_category(CharRange *cr, const char *gc_name);
  95. int unicode_prop(CharRange *cr, const char *prop_name);
  96. typedef void UnicodeSequencePropCB(void *opaque, const uint32_t *buf, int len);
  97. int unicode_sequence_prop(const char *prop_name, UnicodeSequencePropCB *cb, void *opaque,
  98. CharRange *cr);
  99. int lre_case_conv(uint32_t *res, uint32_t c, int conv_type);
  100. int lre_canonicalize(uint32_t c, int is_unicode);
  101. /* Code point type categories */
  102. enum {
  103. UNICODE_C_SPACE = (1 << 0),
  104. UNICODE_C_DIGIT = (1 << 1),
  105. UNICODE_C_UPPER = (1 << 2),
  106. UNICODE_C_LOWER = (1 << 3),
  107. UNICODE_C_UNDER = (1 << 4),
  108. UNICODE_C_DOLLAR = (1 << 5),
  109. UNICODE_C_XDIGIT = (1 << 6),
  110. };
  111. extern uint8_t const lre_ctype_bits[256];
  112. /* zero or non-zero return value */
  113. int lre_is_cased(uint32_t c);
  114. int lre_is_case_ignorable(uint32_t c);
  115. int lre_is_id_start(uint32_t c);
  116. int lre_is_id_continue(uint32_t c);
  117. static inline int lre_is_space_byte(uint8_t c) {
  118. return lre_ctype_bits[c] & UNICODE_C_SPACE;
  119. }
  120. static inline int lre_is_id_start_byte(uint8_t c) {
  121. return lre_ctype_bits[c] & (UNICODE_C_UPPER | UNICODE_C_LOWER |
  122. UNICODE_C_UNDER | UNICODE_C_DOLLAR);
  123. }
  124. static inline int lre_is_id_continue_byte(uint8_t c) {
  125. return lre_ctype_bits[c] & (UNICODE_C_UPPER | UNICODE_C_LOWER |
  126. UNICODE_C_UNDER | UNICODE_C_DOLLAR |
  127. UNICODE_C_DIGIT);
  128. }
  129. static inline int lre_is_word_byte(uint8_t c) {
  130. return lre_ctype_bits[c] & (UNICODE_C_UPPER | UNICODE_C_LOWER |
  131. UNICODE_C_UNDER | UNICODE_C_DIGIT);
  132. }
  133. int lre_is_space_non_ascii(uint32_t c);
  134. static inline int lre_is_space(uint32_t c) {
  135. if (c < 256)
  136. return lre_is_space_byte(c);
  137. else
  138. return lre_is_space_non_ascii(c);
  139. }
  140. static inline int lre_js_is_ident_first(uint32_t c) {
  141. if (c < 128) {
  142. return lre_is_id_start_byte(c);
  143. } else {
  144. #ifdef CONFIG_ALL_UNICODE
  145. return lre_is_id_start(c);
  146. #else
  147. return !lre_is_space_non_ascii(c);
  148. #endif
  149. }
  150. }
  151. static inline int lre_js_is_ident_next(uint32_t c) {
  152. if (c < 128) {
  153. return lre_is_id_continue_byte(c);
  154. } else {
  155. /* ZWNJ and ZWJ are accepted in identifiers */
  156. if (c >= 0x200C && c <= 0x200D)
  157. return TRUE;
  158. #ifdef CONFIG_ALL_UNICODE
  159. return lre_is_id_continue(c);
  160. #else
  161. return !lre_is_space_non_ascii(c);
  162. #endif
  163. }
  164. }
  165. #endif /* LIBUNICODE_H */