#
# $Id: H2Z.pm,v 2.0 2005/05/16 19:07:57 dankogai Exp $
#

package Jcode::H2Z;

use strict;
use vars qw($RCSID $VERSION);

$RCSID = q$Id: H2Z.pm,v 2.0 2005/05/16 19:07:57 dankogai Exp $;
$VERSION = do { my @r = (q$Revision: 2.0 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r };

use Carp;

use Jcode::Constants qw(:all);

use vars qw(%_D2Z  $_PAT_D2Z
	    %_Z2D  $_PAT_Z2D
	    %_H2Z  $_PAT_H2Z
	    %_Z2H  $_PAT_Z2H);

%_H2Z = (
	 "\x8e\xa1"	=>	"\xa1\xa3",	#¡£
	 "\x8e\xa2"	=>	"\xa1\xd6",	#¡Ö
	 "\x8e\xa3"	=>	"\xa1\xd7",	#¡×
	 "\x8e\xa4"	=>	"\xa1\xa2",	#¡¢
	 "\x8e\xa5"	=>	"\xa1\xa6",	#¡¦
	 "\x8e\xa6"	=>	"\xa5\xf2",	#¥ò
	 "\x8e\xa7"	=>	"\xa5\xa1",	#¥¡
	 "\x8e\xa8"	=>	"\xa5\xa3",	#¥£
	 "\x8e\xa9"	=>	"\xa5\xa5",	#¥¥
	 "\x8e\xaa"	=>	"\xa5\xa7",	#¥§
	 "\x8e\xab"	=>	"\xa5\xa9",	#¥©
	 "\x8e\xac"	=>	"\xa5\xe3",	#¥ã
	 "\x8e\xad"	=>	"\xa5\xe5",	#¥å
	 "\x8e\xae"	=>	"\xa5\xe7",	#¥ç
	 "\x8e\xaf"	=>	"\xa5\xc3",	#¥Ã
	 "\x8e\xb0"	=>	"\xa1\xbc",	#¡¼
	 "\x8e\xb1"	=>	"\xa5\xa2",	#¥¢
	 "\x8e\xb2"	=>	"\xa5\xa4",	#¥¤
	 "\x8e\xb3"	=>	"\xa5\xa6",	#¥¦
	 "\x8e\xb4"	=>	"\xa5\xa8",	#¥¨
	 "\x8e\xb5"	=>	"\xa5\xaa",	#¥ª
	 "\x8e\xb6"	=>	"\xa5\xab",	#¥«
	 "\x8e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ö
	 "\x8e\xcd\x8e\xde"	=>	"\xa5\xd9",	#¥Ù
	 "\x8e\xce\x8e\xde"	=>	"\xa5\xdc",	#¥Ü
	 "\x8e\xca\x8e\xdf"	=>	"\xa5\xd1",	#¥Ñ
	 "\x8e\xcb\x8e\xdf"	=>	"\xa5\xd4",	#¥Ô
	 "\x8e\xcc\x8e\xdf"	=>	"\xa5\xd7",	#¥×
	 "\x8e\xcd\x8e\xdf"	=>	"\xa5\xda",	#¥Ú
	 "\x8e\xce\x8e\xdf"	=>	"\xa5\xdd",	#¥Ý
	 "\x8e\xb3\x8e\xde"     =>      "\xa5\xf4",     #¥ô
);

# init only once;

#$_PAT_D2Z = join("|", keys %_D2Z);
#$_PAT_H2Z = join("|", keys %_H2Z);

%_Z2H = reverse %_H2Z;
%_Z2D = reverse %_D2Z;

#$_PAT_Z2H    = join("|", keys %_Z2H);
#$_PAT_Z2D    = join("|", keys %_Z2D);

sub h2z {
    my $r_str = shift;
    my ($keep_dakuten) = @_;
    my $n = 0;
    unless ($keep_dakuten){
	$n = (
	      $$r_str =~ s(
			   ($RE{EUC_KANA}
			    (?:\x8e[\xde\xdf])?)
			   ){
		  my $str = $1;
		  $_D2Z{$str} || $_H2Z{$str} || 
		      # in case dakuten and handakuten are side-by-side!
		      $_H2Z{substr($str,0,2)} . $_H2Z{substr($str,2,2)};
		  }eogx
	      );
    }else{
	$n = (
	      $$r_str =~ s(
			   ($RE{EUC_KANA})
			   ){
		  $_H2Z{$1};
		  }eogx
	      );
    }
    $n;
}

sub z2h {
    my $r_str = shift;
    my $n = (
	     $$r_str =~ s(
			  ($RE{EUC_C}|$RE{EUC_0212}|$RE{EUC_KANA})
			  ){
		 $_Z2D{$1} || $_Z2H{$1} || $1;
		 }eogx
	     );
    $n;
}

1;