makecasefoldhashtable.pl 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. #!/usr/bin/perl -w
  2. use warnings;
  3. use strict;
  4. my $HASHBUCKETS1_16 = 256;
  5. my $HASHBUCKETS1_32 = 16;
  6. my $HASHBUCKETS2_16 = 16;
  7. my $HASHBUCKETS3_16 = 4;
  8. my $mem_used = 0;
  9. print <<__EOF__;
  10. /*
  11. * This file is part of PhysicsFS (https://icculus.org/physfs/)
  12. *
  13. * This data generated by physfs/extras/makecasefoldhashtable.pl ...
  14. * Do not manually edit this file!
  15. *
  16. * Please see the file LICENSE.txt in the source's root directory.
  17. */
  18. #ifndef _INCLUDE_PHYSFS_CASEFOLDING_H_
  19. #define _INCLUDE_PHYSFS_CASEFOLDING_H_
  20. #ifndef __PHYSICSFS_INTERNAL__
  21. #error Do not include this header from your applications.
  22. #endif
  23. /* We build three simple hashmaps here: one that maps Unicode codepoints to
  24. a one, two, or three lowercase codepoints. To retrieve this info: look at
  25. case_fold_hashX, where X is 1, 2, or 3. Most foldable codepoints fold to one,
  26. a few dozen fold to two, and a handful fold to three. If the codepoint isn't
  27. in any of these hashes, it doesn't fold (no separate upper and lowercase).
  28. Almost all these codepoints fit into 16 bits, so we hash them as such to save
  29. memory. If a codepoint is > 0xFFFF, we have separate hashes for them,
  30. since there are (currently) only about 120 of them and (currently) all of them
  31. map to a single lowercase codepoint. */
  32. typedef struct CaseFoldMapping1_32
  33. {
  34. PHYSFS_uint32 from;
  35. PHYSFS_uint32 to0;
  36. } CaseFoldMapping1_32;
  37. typedef struct CaseFoldMapping1_16
  38. {
  39. PHYSFS_uint16 from;
  40. PHYSFS_uint16 to0;
  41. } CaseFoldMapping1_16;
  42. typedef struct CaseFoldMapping2_16
  43. {
  44. PHYSFS_uint16 from;
  45. PHYSFS_uint16 to0;
  46. PHYSFS_uint16 to1;
  47. } CaseFoldMapping2_16;
  48. typedef struct CaseFoldMapping3_16
  49. {
  50. PHYSFS_uint16 from;
  51. PHYSFS_uint16 to0;
  52. PHYSFS_uint16 to1;
  53. PHYSFS_uint16 to2;
  54. } CaseFoldMapping3_16;
  55. typedef struct CaseFoldHashBucket1_16
  56. {
  57. const CaseFoldMapping1_16 *list;
  58. const PHYSFS_uint8 count;
  59. } CaseFoldHashBucket1_16;
  60. typedef struct CaseFoldHashBucket1_32
  61. {
  62. const CaseFoldMapping1_32 *list;
  63. const PHYSFS_uint8 count;
  64. } CaseFoldHashBucket1_32;
  65. typedef struct CaseFoldHashBucket2_16
  66. {
  67. const CaseFoldMapping2_16 *list;
  68. const PHYSFS_uint8 count;
  69. } CaseFoldHashBucket2_16;
  70. typedef struct CaseFoldHashBucket3_16
  71. {
  72. const CaseFoldMapping3_16 *list;
  73. const PHYSFS_uint8 count;
  74. } CaseFoldHashBucket3_16;
  75. __EOF__
  76. my @foldPairs1_16;
  77. my @foldPairs2_16;
  78. my @foldPairs3_16;
  79. my @foldPairs1_32;
  80. for (my $i = 0; $i < $HASHBUCKETS1_16; $i++) {
  81. $foldPairs1_16[$i] = '';
  82. }
  83. for (my $i = 0; $i < $HASHBUCKETS1_32; $i++) {
  84. $foldPairs1_32[$i] = '';
  85. }
  86. for (my $i = 0; $i < $HASHBUCKETS2_16; $i++) {
  87. $foldPairs2_16[$i] = '';
  88. }
  89. for (my $i = 0; $i < $HASHBUCKETS3_16; $i++) {
  90. $foldPairs3_16[$i] = '';
  91. }
  92. open(FH,'<','casefolding.txt') or die("failed to open casefolding.txt: $!\n");
  93. while (<FH>) {
  94. chomp;
  95. # strip comments from textfile...
  96. s/\#.*\Z//;
  97. # strip whitespace...
  98. s/\A\s+//;
  99. s/\s+\Z//;
  100. next if not /\A([a-fA-F0-9]+)\;\s*(.)\;\s*(.+)\;/;
  101. my ($code, $status, $mapping) = ($1, $2, $3);
  102. my $hexxed = hex($code);
  103. #print("// code '$code' status '$status' mapping '$mapping'\n");
  104. if (($status eq 'C') or ($status eq 'F')) {
  105. my ($map1, $map2, $map3) = (undef, undef, undef);
  106. $map1 = $1 if $mapping =~ s/\A([a-fA-F0-9]+)(\s*|\Z)//;
  107. $map2 = $1 if $mapping =~ s/\A([a-fA-F0-9]+)(\s*|\Z)//;
  108. $map3 = $1 if $mapping =~ s/\A([a-fA-F0-9]+)(\s*|\Z)//;
  109. die("mapping space too small for '$code'\n") if ($mapping ne '');
  110. die("problem parsing mapping for '$code'\n") if (not defined($map1));
  111. if ($hexxed < 128) {
  112. # Just ignore these, we'll handle the low-ASCII ones ourselves.
  113. } elsif ($hexxed > 0xFFFF) {
  114. # We just need to add the 32-bit 2 and/or 3 codepoint maps if this die()'s here.
  115. die("Uhoh, a codepoint > 0xFFFF that folds to multiple codepoints! Fixme.") if defined($map2);
  116. my $hashed = (($hexxed ^ ($hexxed >> 8)) & ($HASHBUCKETS1_32-1));
  117. #print("// hexxed '$hexxed' hashed1 '$hashed'\n");
  118. $foldPairs1_32[$hashed] .= " { 0x$code, 0x$map1 },\n";
  119. $mem_used += 8;
  120. } elsif (not defined($map2)) {
  121. my $hashed = (($hexxed ^ ($hexxed >> 8)) & ($HASHBUCKETS1_16-1));
  122. #print("// hexxed '$hexxed' hashed1 '$hashed'\n");
  123. $foldPairs1_16[$hashed] .= " { 0x$code, 0x$map1 },\n";
  124. $mem_used += 4;
  125. } elsif (not defined($map3)) {
  126. my $hashed = (($hexxed ^ ($hexxed >> 8)) & ($HASHBUCKETS2_16-1));
  127. #print("// hexxed '$hexxed' hashed2 '$hashed'\n");
  128. $foldPairs2_16[$hashed] .= " { 0x$code, 0x$map1, 0x$map2 },\n";
  129. $mem_used += 6;
  130. } else {
  131. my $hashed = (($hexxed ^ ($hexxed >> 8)) & ($HASHBUCKETS3_16-1));
  132. #print("// hexxed '$hexxed' hashed3 '$hashed'\n");
  133. $foldPairs3_16[$hashed] .= " { 0x$code, 0x$map1, 0x$map2, 0x$map3 },\n";
  134. $mem_used += 8;
  135. }
  136. }
  137. }
  138. close(FH);
  139. for (my $i = 0; $i < $HASHBUCKETS1_16; $i++) {
  140. $foldPairs1_16[$i] =~ s/,\n\Z//;
  141. my $str = $foldPairs1_16[$i];
  142. next if $str eq '';
  143. my $num = '000' . $i;
  144. $num =~ s/\A.*?(\d\d\d)\Z/$1/;
  145. my $sym = "case_fold1_16_${num}";
  146. print("static const CaseFoldMapping1_16 ${sym}[] = {\n$str\n};\n\n");
  147. }
  148. for (my $i = 0; $i < $HASHBUCKETS1_32; $i++) {
  149. $foldPairs1_32[$i] =~ s/,\n\Z//;
  150. my $str = $foldPairs1_32[$i];
  151. next if $str eq '';
  152. my $num = '000' . $i;
  153. $num =~ s/\A.*?(\d\d\d)\Z/$1/;
  154. my $sym = "case_fold1_32_${num}";
  155. print("static const CaseFoldMapping1_32 ${sym}[] = {\n$str\n};\n\n");
  156. }
  157. for (my $i = 0; $i < $HASHBUCKETS2_16; $i++) {
  158. $foldPairs2_16[$i] =~ s/,\n\Z//;
  159. my $str = $foldPairs2_16[$i];
  160. next if $str eq '';
  161. my $num = '000' . $i;
  162. $num =~ s/\A.*?(\d\d\d)\Z/$1/;
  163. my $sym = "case_fold2_16_${num}";
  164. print("static const CaseFoldMapping2_16 ${sym}[] = {\n$str\n};\n\n");
  165. }
  166. for (my $i = 0; $i < $HASHBUCKETS3_16; $i++) {
  167. $foldPairs3_16[$i] =~ s/,\n\Z//;
  168. my $str = $foldPairs3_16[$i];
  169. next if $str eq '';
  170. my $num = '000' . $i;
  171. $num =~ s/\A.*?(\d\d\d)\Z/$1/;
  172. my $sym = "case_fold3_16_${num}";
  173. print("static const CaseFoldMapping3_16 ${sym}[] = {\n$str\n};\n\n");
  174. }
  175. print("static const CaseFoldHashBucket1_16 case_fold_hash1_16[] = {\n");
  176. for (my $i = 0; $i < $HASHBUCKETS1_16; $i++) {
  177. my $str = $foldPairs1_16[$i];
  178. if ($str eq '') {
  179. print(" { NULL, 0 },\n");
  180. } else {
  181. my $num = '000' . $i;
  182. $num =~ s/\A.*?(\d\d\d)\Z/$1/;
  183. my $sym = "case_fold1_16_${num}";
  184. print(" { $sym, __PHYSFS_ARRAYLEN($sym) },\n");
  185. }
  186. $mem_used += 12;
  187. }
  188. print("};\n\n");
  189. print("static const CaseFoldHashBucket1_32 case_fold_hash1_32[] = {\n");
  190. for (my $i = 0; $i < $HASHBUCKETS1_32; $i++) {
  191. my $str = $foldPairs1_32[$i];
  192. if ($str eq '') {
  193. print(" { NULL, 0 },\n");
  194. } else {
  195. my $num = '000' . $i;
  196. $num =~ s/\A.*?(\d\d\d)\Z/$1/;
  197. my $sym = "case_fold1_32_${num}";
  198. print(" { $sym, __PHYSFS_ARRAYLEN($sym) },\n");
  199. }
  200. $mem_used += 12;
  201. }
  202. print("};\n\n");
  203. print("static const CaseFoldHashBucket2_16 case_fold_hash2_16[] = {\n");
  204. for (my $i = 0; $i < $HASHBUCKETS2_16; $i++) {
  205. my $str = $foldPairs2_16[$i];
  206. if ($str eq '') {
  207. print(" { NULL, 0 },\n");
  208. } else {
  209. my $num = '000' . $i;
  210. $num =~ s/\A.*?(\d\d\d)\Z/$1/;
  211. my $sym = "case_fold2_16_${num}";
  212. print(" { $sym, __PHYSFS_ARRAYLEN($sym) },\n");
  213. }
  214. $mem_used += 12;
  215. }
  216. print("};\n\n");
  217. print("static const CaseFoldHashBucket3_16 case_fold_hash3_16[] = {\n");
  218. for (my $i = 0; $i < $HASHBUCKETS3_16; $i++) {
  219. my $str = $foldPairs3_16[$i];
  220. if ($str eq '') {
  221. print(" { NULL, 0 },\n");
  222. } else {
  223. my $num = '000' . $i;
  224. $num =~ s/\A.*?(\d\d\d)\Z/$1/;
  225. my $sym = "case_fold3_16_${num}";
  226. print(" { $sym, __PHYSFS_ARRAYLEN($sym) },\n");
  227. }
  228. $mem_used += 12;
  229. }
  230. print("};\n\n");
  231. print <<__EOF__;
  232. #endif /* _INCLUDE_PHYSFS_CASEFOLDING_H_ */
  233. /* end of physfs_casefolding.h ... */
  234. __EOF__
  235. print STDERR "Memory required for case-folding hashtable: $mem_used bytes\n";
  236. exit 0;
  237. # end of makecashfoldhashtable.pl ...