makecasefoldhashtable.pl 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #!/usr/bin/perl -w
  2. use warnings;
  3. use strict;
  4. print <<__EOF__;
  5. /*
  6. * This file is part of PhysicsFS (http://icculus.org/physfs/)
  7. *
  8. * This data generated by physfs/extras/makecasefoldhashtable.pl ...
  9. * Do not manually edit this file!
  10. *
  11. * Please see the file LICENSE.txt in the source's root directory.
  12. */
  13. #ifndef __PHYSICSFS_INTERNAL__
  14. #error Do not include this header from your applications.
  15. #endif
  16. __EOF__
  17. my @foldPairs;
  18. for (my $i = 0; $i < 256; $i++) {
  19. $foldPairs[$i] = '';
  20. }
  21. open(FH,'<','casefolding.txt') or die("failed to open casefolding.txt: $!\n");
  22. while (<FH>) {
  23. chomp;
  24. # strip comments from textfile...
  25. s/\#.*\Z//;
  26. # strip whitespace...
  27. s/\A\s+//;
  28. s/\s+\Z//;
  29. next if not /\A([a-fA-F0-9]+)\;\s*(.)\;\s*(.+)\;/;
  30. my ($code, $status, $mapping) = ($1, $2, $3);
  31. my $hexxed = hex($code);
  32. my $hashed = (($hexxed ^ ($hexxed >> 8)) & 0xFF);
  33. #print("// code '$code' status '$status' mapping '$mapping'\n");
  34. #print("// hexxed '$hexxed' hashed '$hashed'\n");
  35. if (($status eq 'C') or ($status eq 'F')) {
  36. my ($map1, $map2, $map3) = ('0000', '0000', '0000');
  37. $map1 = $1 if $mapping =~ s/\A([a-fA-F0-9]+)(\s*|\Z)//;
  38. $map2 = $1 if $mapping =~ s/\A([a-fA-F0-9]+)(\s*|\Z)//;
  39. $map3 = $1 if $mapping =~ s/\A([a-fA-F0-9]+)(\s*|\Z)//;
  40. die("mapping space too small for '$code'\n") if ($mapping ne '');
  41. $foldPairs[$hashed] .= " { 0x$code, 0x$map1, 0x$map2, 0x$map3 },\n";
  42. }
  43. }
  44. close(FH);
  45. for (my $i = 0; $i < 256; $i++) {
  46. $foldPairs[$i] =~ s/,\n\Z//;
  47. my $str = $foldPairs[$i];
  48. next if $str eq '';
  49. my $num = '000' . $i;
  50. $num =~ s/\A.*?(\d\d\d)\Z/$1/;
  51. my $sym = "case_fold_${num}";
  52. print("static const CaseFoldMapping ${sym}[] = {\n$str\n};\n\n");
  53. }
  54. print("\nstatic const CaseFoldHashBucket case_fold_hash[256] = {\n");
  55. for (my $i = 0; $i < 256; $i++) {
  56. my $str = $foldPairs[$i];
  57. if ($str eq '') {
  58. print(" { 0, NULL },\n");
  59. } else {
  60. my $num = '000' . $i;
  61. $num =~ s/\A.*?(\d\d\d)\Z/$1/;
  62. my $sym = "case_fold_${num}";
  63. print(" { __PHYSFS_ARRAYLEN($sym), $sym },\n");
  64. }
  65. }
  66. print("};\n\n");
  67. exit 0;
  68. # end of makecashfoldhashtable.pl ...