fold.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. * String folding
  3. *
  4. * Copyright 2003 Jon Griffiths
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  19. */
  20. #include "wine/asm.h"
  21. #ifdef __ASM_OBSOLETE
  22. #include "unicode.h"
  23. static inline WCHAR to_unicode_digit( WCHAR ch )
  24. {
  25. extern const WCHAR wine_digitmap[] DECLSPEC_HIDDEN;
  26. WCHAR ret = wine_digitmap[wine_digitmap[wine_digitmap[ch >> 8] + ((ch >> 4) & 0x0f)] + (ch & 0xf)];
  27. return ret ? ret : ch;
  28. }
  29. static inline WCHAR to_unicode_native( WCHAR ch )
  30. {
  31. extern const WCHAR wine_compatmap[] DECLSPEC_HIDDEN;
  32. return ch + wine_compatmap[wine_compatmap[ch >> 8] + (ch & 0xff)];
  33. }
  34. static const WCHAR wine_ligatures[] =
  35. {
  36. 0x00c6, 0x00de, 0x00df, 0x00e6, 0x00fe, 0x0132, 0x0133, 0x0152,
  37. 0x0153, 0x01c4, 0x01c5, 0x01c6, 0x01c7, 0x01c8, 0x01c9, 0x01ca,
  38. 0x01cb, 0x01cc, 0x01e2, 0x01e3, 0x01f1, 0x01f2, 0x01f3, 0x01fc,
  39. 0x01fd, 0x05f0, 0x05f1, 0x05f2, 0xfb00, 0xfb01, 0xfb02, 0xfb03,
  40. 0xfb04, 0xfb05, 0xfb06
  41. };
  42. /* Unicode expanded ligatures */
  43. static const WCHAR wine_expanded_ligatures[][4] =
  44. {
  45. { 'A','E','\0',1 },
  46. { 'T','H','\0',1 },
  47. { 's','s','\0',1 },
  48. { 'a','e','\0',1 },
  49. { 't','h','\0',1 },
  50. { 'I','J','\0',1 },
  51. { 'i','j','\0',1 },
  52. { 'O','E','\0',1 },
  53. { 'o','e','\0',1 },
  54. { 'D',0x017d,'\0',1 },
  55. { 'D',0x017e,'\0',1 },
  56. { 'd',0x017e,'\0',1 },
  57. { 'L','J','\0',1 },
  58. { 'L','j','\0',1 },
  59. { 'l','j','\0',1 },
  60. { 'N','J','\0',1 },
  61. { 'N','j','\0',1 },
  62. { 'n','j','\0',1 },
  63. { 0x0100,0x0112,'\0',1 },
  64. { 0x0101,0x0113,'\0',1 },
  65. { 'D','Z','\0',1 },
  66. { 'D','z','\0',1 },
  67. { 'd','z','\0',1 },
  68. { 0x00c1,0x00c9,'\0',1 },
  69. { 0x00e1,0x00e9,'\0',1 },
  70. { 0x05d5,0x05d5,'\0',1 },
  71. { 0x05d5,0x05d9,'\0',1 },
  72. { 0x05d9,0x05d9,'\0',1 },
  73. { 'f','f','\0',1 },
  74. { 'f','i','\0',1 },
  75. { 'f','l','\0',1 },
  76. { 'f','f','i',2 },
  77. { 'f','f','l',2 },
  78. { 0x017f,'t','\0',1 },
  79. { 's','t','\0',1 }
  80. };
  81. static inline int get_ligature_len( WCHAR wc )
  82. {
  83. int low = 0, high = sizeof(wine_ligatures)/sizeof(WCHAR) -1;
  84. while (low <= high)
  85. {
  86. int pos = (low + high) / 2;
  87. if (wine_ligatures[pos] < wc)
  88. low = pos + 1;
  89. else if (wine_ligatures[pos] > wc)
  90. high = pos - 1;
  91. else
  92. return wine_expanded_ligatures[pos][3];
  93. }
  94. return 0;
  95. }
  96. static inline const WCHAR* get_ligature( WCHAR wc )
  97. {
  98. static const WCHAR empty_ligature[] = { '\0','\0','\0', 0 };
  99. int low = 0, high = sizeof(wine_ligatures)/sizeof(WCHAR) -1;
  100. while (low <= high)
  101. {
  102. int pos = (low + high) / 2;
  103. if (wine_ligatures[pos] < wc)
  104. low = pos + 1;
  105. else if (wine_ligatures[pos] > wc)
  106. high = pos - 1;
  107. else
  108. return wine_expanded_ligatures[pos];
  109. }
  110. return empty_ligature;
  111. }
  112. /* fold a unicode string */
  113. int wine_fold_string_obsolete( int flags, const WCHAR *src, int srclen, WCHAR *dst, int dstlen )
  114. {
  115. WCHAR *dstbase = dst;
  116. const WCHAR *expand;
  117. int i;
  118. if (srclen == -1)
  119. srclen = strlenW(src) + 1; /* Include terminating NUL in count */
  120. if (!dstlen)
  121. {
  122. /* Calculate the required size for dst */
  123. dstlen = srclen;
  124. if (flags & MAP_EXPAND_LIGATURES)
  125. {
  126. while (srclen--)
  127. {
  128. dstlen += get_ligature_len(*src);
  129. src++;
  130. }
  131. }
  132. else if (flags & MAP_COMPOSITE)
  133. {
  134. /* FIXME */
  135. }
  136. else if (flags & MAP_PRECOMPOSED)
  137. {
  138. /* FIXME */
  139. }
  140. return dstlen;
  141. }
  142. if (srclen > dstlen)
  143. return 0;
  144. dstlen -= srclen;
  145. /* Actually perform the mapping(s) specified */
  146. for (i = 0; i < srclen; i++)
  147. {
  148. WCHAR ch = *src;
  149. if (flags & MAP_EXPAND_LIGATURES)
  150. {
  151. expand = get_ligature(ch);
  152. if (expand[0])
  153. {
  154. if (!dstlen--)
  155. return 0;
  156. dst[0] = expand[0];
  157. if (expand[2])
  158. {
  159. if (!dstlen--)
  160. return 0;
  161. *++dst = expand[1];
  162. ch = expand[2];
  163. }
  164. else
  165. ch = expand[1];
  166. dst++;
  167. }
  168. }
  169. else if (flags & MAP_COMPOSITE)
  170. {
  171. /* FIXME */
  172. }
  173. else if (flags & MAP_PRECOMPOSED)
  174. {
  175. /* FIXME */
  176. }
  177. if (flags & MAP_FOLDDIGITS)
  178. ch = to_unicode_digit(ch);
  179. if (flags & MAP_FOLDCZONE)
  180. ch = to_unicode_native(ch);
  181. *dst++ = ch;
  182. src++;
  183. }
  184. return dst - dstbase;
  185. }
  186. __ASM_OBSOLETE(wine_fold_string);
  187. #endif /* __ASM_OBSOLETE */