#!/usr/bin/perl

use HTML::Parser ();
use warnings;
use strict;

try_test($ARGV[0], $ARGV[1]);
exit;

my $text;

sub try_test {
  my $usemytext = shift;
  my $applyfix = shift;

  my $msg = q{
     <html><body>&trade;</body></html>};

  $text = '';
  my $p = HTML::Parser->new( api_version => 3, text_h => [ \&text, "dtext"]);
  $p->parse($msg);
  $p->eof;

  # first arg: set to 1 to use a replacement $text string, which contains
  # the same data as the HTML::Parser-produced string.
  if ($usemytext) {
    $text = "\x0a     \xe2\x84\xa2";
  }

  # second arg: set to 1 to apply a regexp sub to the string, which
  # also fixes the bug somehow
  if ($applyfix) {
    $text =~ s/^\s+//s;
  }

  run_regexp($text);
  print "[$text]\n";
}

sub text {
  $text .= shift;
}

sub run_regexp {
  my $t = shift;
  $t =~ /
    (
      d|
    )(
      [il1:\|\*]|
      \xC4[\xA8-\xB0]|
      \xC4\xBA|
      \xC4\xBC|
      \xC4\xBE|
      \xC5\x80|
      \xC5\x82|
      \xC7[\x8F-\x90]|
      \xD0[\x86-\x87]|
      \xD1[\x96-\x97]|
      \xCE\x8A|
      \xCE\x90|
      \xCE\x99|
      \xCE\xAA|
      \xCE\xAF|
      \xCE\xB9|
      \xCF\x8A
    )(
      [a4\*\@]|\/\\|
      \xC4[\x80-\x85]|
      \xC7[\x8D-\x8E]|
      \xC7[\xBA-\xBB]|
      \xCE\x86|
      \xCE\x91|
      \xCE\x94|
      \xCE\x9B|
      \xCE\xAC|
      \xCE\xB1|
      \xD0\x90|
      \xD0\xB0
    )(
      [t\+]|
      \xC5[\xA2-\xA7]|
      \xCE\xA4|
      \xCF\x84|
      \xD0\xA2|
      \xD1\x82
    )(
      [il1:\|\*]|
      \xC4[\xA8-\xB0]|
      \xC4\xBA|
      \xC4\xBC|
      \xC4\xBE|
      \xC5\x80|
      \xC5\x82|
      \xC7[\x8F-\x90]|
      \xD0[\x86-\x87]|
      \xD1[\x96-\x97]|
      \xCE\x8A|
      \xCE\x90|
      \xCE\x99|
      \xCE\xAA|
      \xCE\xAF|
      \xCE\xB9|
      \xCF\x8A
    )(
      [o0\*]|\(\)|\[\]|
      \xC5[\x8C-\x91]|
      \xC6[\xA0-\xA1]|
      \xC7[\x91-\x92]|
      \xC7[\xBE-\xBF]|
      \xCE\x8C|
      \xCE\x98|
      \xCE\x9F|
      \xCE\xB8|
      \xCE\xBF|
      \xCF\x8C|
      \xD0\x9E|
      \xD0\xBE|
      \xD5\x95
    )(
      n|\|\\\||
      \xC5[\x83-\x8B]|
      \xCE\x9D|
      \xCE\xA0|
      \xCE\xAE|
      \xCE\xB7|
      \xD5\xB2|
      \xD5\xB8
    )(
      [s5]|
      \xC5[\x9A-\xA1]|
      \xD0\x85|
      \xD1\x95|
      \xD5\x8F
    )
  /six;

}


