UnicodeMacrosFromICU.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * Copyright (C) 1999-2004, International Business Machines Corporation and others. All Rights Reserved.
  3. *
  4. */
  5. #ifndef UnicodeMacrosFromICU_h
  6. #define UnicodeMacrosFromICU_h
  7. // some defines from ICU
  8. #define U_IS_BMP(c) ((UChar32)(c)<=0xffff)
  9. #define U16_IS_LEAD(c) (((c)&0xfffffc00)==0xd800)
  10. #define U16_IS_TRAIL(c) (((c)&0xfffffc00)==0xdc00)
  11. #define U16_SURROGATE_OFFSET ((0xd800<<10UL)+0xdc00-0x10000)
  12. #define U16_GET_SUPPLEMENTARY(lead, trail) \
  13. (((UChar32)(lead)<<10UL)+(UChar32)(trail)-U16_SURROGATE_OFFSET)
  14. #define U16_LEAD(supplementary) (UChar)(((supplementary)>>10)+0xd7c0)
  15. #define U16_TRAIL(supplementary) (UChar)(((supplementary)&0x3ff)|0xdc00)
  16. #define U16_LENGTH(c) ((uint32_t)(c) <= 0xffff ? 1 : 2)
  17. #define U_IS_SUPPLEMENTARY(c) ((UChar32)((c)-0x10000)<=0xfffff)
  18. #define U_IS_SURROGATE(c) (((c)&0xfffff800)==0xd800)
  19. #define U16_IS_SINGLE(c) !U_IS_SURROGATE(c)
  20. #define U16_IS_SURROGATE(c) U_IS_SURROGATE(c)
  21. #define U16_IS_SURROGATE_LEAD(c) (((c)&0x400)==0)
  22. #define U16_GET(s, start, i, length, c) { \
  23. (c)=(s)[i]; \
  24. if(U16_IS_SURROGATE(c)) { \
  25. uint16_t __c2; \
  26. if(U16_IS_SURROGATE_LEAD(c)) { \
  27. if((i)+1<(length) && U16_IS_TRAIL(__c2=(s)[(i)+1])) { \
  28. (c)=U16_GET_SUPPLEMENTARY((c), __c2); \
  29. } \
  30. } else { \
  31. if((i)-1>=(start) && U16_IS_LEAD(__c2=(s)[(i)-1])) { \
  32. (c)=U16_GET_SUPPLEMENTARY(__c2, (c)); \
  33. } \
  34. } \
  35. } \
  36. }
  37. #define U16_PREV(s, start, i, c) { \
  38. (c)=(s)[--(i)]; \
  39. if(U16_IS_TRAIL(c)) { \
  40. uint16_t __c2; \
  41. if((i)>(start) && U16_IS_LEAD(__c2=(s)[(i)-1])) { \
  42. --(i); \
  43. (c)=U16_GET_SUPPLEMENTARY(__c2, (c)); \
  44. } \
  45. } \
  46. }
  47. #define U16_BACK_1(s, start, i) { \
  48. if(U16_IS_TRAIL((s)[--(i)]) && (i)>(start) && U16_IS_LEAD((s)[(i)-1])) { \
  49. --(i); \
  50. } \
  51. }
  52. #define U16_NEXT(s, i, length, c) { \
  53. (c)=(s)[(i)++]; \
  54. if(U16_IS_LEAD(c)) { \
  55. uint16_t __c2; \
  56. if((i)<(length) && U16_IS_TRAIL(__c2=(s)[(i)])) { \
  57. ++(i); \
  58. (c)=U16_GET_SUPPLEMENTARY((c), __c2); \
  59. } \
  60. } \
  61. }
  62. #define U16_FWD_1(s, i, length) { \
  63. if(U16_IS_LEAD((s)[(i)++]) && (i)<(length) && U16_IS_TRAIL((s)[i])) { \
  64. ++(i); \
  65. } \
  66. }
  67. #define U_MASK(x) ((uint32_t)1<<(x))
  68. #define U8_MAX_LENGTH 4
  69. #define U8_APPEND_UNSAFE(s, i, c) { \
  70. if((uint32_t)(c)<=0x7f) { \
  71. (s)[(i)++]=(uint8_t)(c); \
  72. } else { \
  73. if((uint32_t)(c)<=0x7ff) { \
  74. (s)[(i)++]=(uint8_t)(((c)>>6)|0xc0); \
  75. } else { \
  76. if((uint32_t)(c)<=0xffff) { \
  77. (s)[(i)++]=(uint8_t)(((c)>>12)|0xe0); \
  78. } else { \
  79. (s)[(i)++]=(uint8_t)(((c)>>18)|0xf0); \
  80. (s)[(i)++]=(uint8_t)((((c)>>12)&0x3f)|0x80); \
  81. } \
  82. (s)[(i)++]=(uint8_t)((((c)>>6)&0x3f)|0x80); \
  83. } \
  84. (s)[(i)++]=(uint8_t)(((c)&0x3f)|0x80); \
  85. } \
  86. }
  87. #endif