Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

221 lines
5.6 KiB

  1. package utf8;
  2. my $DEBUG = 0;
  3. my $seq = "AAA0000";
  4. sub DESTROY {}
  5. sub croak { require Carp; Carp::croak(@_) }
  6. sub SWASHNEW {
  7. my ($class, $type, $list, $minbits, $none) = @_;
  8. local $^D = 0 if $^D;
  9. print STDERR "SWASHNEW @_\n" if $DEBUG;
  10. my $extras;
  11. my $bits;
  12. if ($type and ref ${"${class}::{$type}"} eq $class) {
  13. warn qq/Found \${"${class}::{$type}"}\n/ if $DEBUG;
  14. return ${"${class}::{$type}"}; # Already there...
  15. }
  16. $type ||= $seq++;
  17. my $caller;
  18. my $i = 0;
  19. while (($caller = caller($i)) eq __PACKAGE__) { $i++ }
  20. my $encoding = $enc{$caller} || "unicode";
  21. (my $file = $type) =~ s!::!/!g;
  22. $file =~ s#^(I[sn]|To)([A-Z].*)#$1/$2#;
  23. $list ||= eval { $caller->$type(); }
  24. || do "$file.pl"
  25. || do "$encoding/$file.pl"
  26. || do "$encoding/Is/${type}.pl"
  27. || croak("Can't find $encoding character property definition via $caller->$type or $file.pl");
  28. $| = 1;
  29. if ($list) {
  30. my @tmp = split(/^/m, $list);
  31. my %seen;
  32. no warnings;
  33. $extras = join '', grep /^[^0-9a-fA-F]/, @tmp;
  34. $list = join '',
  35. sort { hex $a <=> hex $b }
  36. grep {/^([0-9a-fA-F]+)/ and not $seen{$1}++} @tmp; # XXX doesn't do ranges right
  37. }
  38. if ($none) {
  39. my $hextra = sprintf "%04x", $none + 1;
  40. $list =~ s/\tXXXX$/\t$hextra/mg;
  41. }
  42. if ($minbits < 32) {
  43. my $top = 0;
  44. while ($list =~ /^([0-9a-fA-F]+)(?:\t([0-9a-fA-F]+)?)(?:\t([0-9a-fA-F]+))?/mg) {
  45. my $min = hex $1;
  46. my $max = hex(defined $2 ? $2 : $1);
  47. my $val = hex(defined $3 ? $3 : "");
  48. $val += $max - $min if defined $3;
  49. $top = $val if $val > $top;
  50. }
  51. $bits =
  52. $top > 0xffff ? 32 :
  53. $top > 0xff ? 16 :
  54. $top > 1 ? 8 : 1
  55. }
  56. $bits = $minbits if $bits < $minbits;
  57. my @extras;
  58. for my $x ($extras) {
  59. pos $x = 0;
  60. while ($x =~ /^([^0-9a-fA-F\n])(.*)/mg) {
  61. my $char = $1;
  62. my $name = $2;
  63. # print STDERR "$1 => $2\n" if $DEBUG;
  64. if ($char =~ /[-+!]/) {
  65. my ($c,$t) = split(/::/, $name, 2); # bogus use of ::, really
  66. my $subobj = $c->SWASHNEW($t, "", 0, 0, 0);
  67. push @extras, $name => $subobj;
  68. $bits = $subobj->{BITS} if $bits < $subobj->{BITS};
  69. }
  70. }
  71. }
  72. print STDERR "CLASS = $class, TYPE => $type, BITS => $bits, NONE => $none\nEXTRAS =>\n$extras\nLIST =>\n$list\n" if $DEBUG;
  73. ${"${class}::{$type}"} = bless {
  74. TYPE => $type,
  75. BITS => $bits,
  76. EXTRAS => $extras,
  77. LIST => $list,
  78. NONE => $none,
  79. @extras,
  80. } => $class;
  81. }
  82. # NOTE: utf8.c:swash_init() assumes entries are never modified once generated.
  83. sub SWASHGET {
  84. my ($self, $start, $len) = @_;
  85. local $^D = 0 if $^D;
  86. my $type = $self->{TYPE};
  87. my $bits = $self->{BITS};
  88. my $none = $self->{NONE};
  89. print STDERR "SWASHGET @_ [$type/$bits/$none]\n" if $DEBUG;
  90. my $end = $start + $len;
  91. my $swatch = "";
  92. my $key;
  93. vec($swatch, $len - 1, $bits) = 0; # Extend to correct length.
  94. if ($none) {
  95. for $key (0 .. $len - 1) { vec($swatch, $key, $bits) = $none }
  96. }
  97. for ($self->{LIST}) {
  98. pos $_ = 0;
  99. if ($bits > 1) {
  100. LINE:
  101. while (/^([0-9a-fA-F]+)(?:\t([0-9a-fA-F]+)?)(?:\t([0-9a-fA-F]+))?/mg) {
  102. my $min = hex $1;
  103. my $max = (defined $2 ? hex $2 : $min);
  104. my $val = hex $3;
  105. next if $max < $start;
  106. # print "$min $max $val\n";
  107. if ($none) {
  108. if ($min < $start) {
  109. $val += $start - $min if $val < $none;
  110. $min = $start;
  111. }
  112. for ($key = $min; $key <= $max; $key++) {
  113. last LINE if $key >= $end;
  114. # print STDERR "$key => $val\n" if $DEBUG;
  115. vec($swatch, $key - $start, $bits) = $val;
  116. ++$val if $val < $none;
  117. }
  118. }
  119. else {
  120. if ($min < $start) {
  121. $val += $start - $min;
  122. $min = $start;
  123. }
  124. for ($key = $min; $key <= $max; $key++, $val++) {
  125. last LINE if $key >= $end;
  126. # print STDERR "$key => $val\n" if $DEBUG;
  127. vec($swatch, $key - $start, $bits) = $val;
  128. }
  129. }
  130. }
  131. }
  132. else {
  133. LINE:
  134. while (/^([0-9a-fA-F]+)(?:\t([0-9a-fA-F]+))?/mg) {
  135. my $min = hex $1;
  136. my $max = (defined $2 ? hex $2 : $min);
  137. next if $max < $start;
  138. if ($min < $start) {
  139. $min = $start;
  140. }
  141. for ($key = $min; $key <= $max; $key++) {
  142. last LINE if $key >= $end;
  143. # print STDERR "$key => 1\n" if $DEBUG;
  144. vec($swatch, $key - $start, 1) = 1;
  145. }
  146. }
  147. }
  148. }
  149. for my $x ($self->{EXTRAS}) {
  150. pos $x = 0;
  151. while ($x =~ /^([-+!])(.*)/mg) {
  152. my $char = $1;
  153. my $name = $2;
  154. print STDERR "INDIRECT $1 $2\n" if $DEBUG;
  155. my $otherbits = $self->{$name}->{BITS};
  156. croak("SWASHGET size mismatch") if $bits < $otherbits;
  157. my $other = $self->{$name}->SWASHGET($start, $len);
  158. if ($char eq '+') {
  159. if ($bits == 1 and $otherbits == 1) {
  160. $swatch |= $other;
  161. }
  162. else {
  163. for ($key = 0; $key < $len; $key++) {
  164. vec($swatch, $key, $bits) = vec($other, $key, $otherbits);
  165. }
  166. }
  167. }
  168. elsif ($char eq '!') {
  169. if ($bits == 1 and $otherbits == 1) {
  170. $swatch |= ~$other;
  171. }
  172. else {
  173. for ($key = 0; $key < $len; $key++) {
  174. if (!vec($other, $key, $otherbits)) {
  175. vec($swatch, $key, $bits) = 1;
  176. }
  177. }
  178. }
  179. }
  180. elsif ($char eq '-') {
  181. if ($bits == 1 and $otherbits == 1) {
  182. $swatch &= ~$other;
  183. }
  184. else {
  185. for ($key = 0; $key < $len; $key++) {
  186. if (vec($other, $key, $otherbits)) {
  187. vec($swatch, $key, $bits) = 0;
  188. }
  189. }
  190. }
  191. }
  192. }
  193. }
  194. if ($DEBUG) {
  195. print STDERR "CELLS ";
  196. for ($key = 0; $key < $len; $key++) {
  197. print STDERR vec($swatch, $key, $bits), " ";
  198. }
  199. print STDERR "\n";
  200. }
  201. $swatch;
  202. }
  203. 1;