1 # This module is part of debbugs, and is released
2 # under the terms of the GPL version 2, or any later
3 # version at your option.
4 # See the file README and COPYING for more information.
6 # Copyright 2013 by Don Armstrong <don@donarmstrong.com>.
12 Debbugs::UTF8 -- Routines for handling conversion of charsets to UTF8
21 This module contains routines which convert from various different
30 use vars qw($VERSION $DEBUG %EXPORT_TAGS @EXPORT_OK @EXPORT);
31 use base qw(Exporter);
35 $DEBUG = 0 unless defined $DEBUG;
37 %EXPORT_TAGS = (utf8 => [qw(encode_utf8_structure encode_utf8_safely),
40 @EXPORT = (@{$EXPORT_TAGS{utf8}});
42 Exporter::export_ok_tags(keys %EXPORT_TAGS);
43 $EXPORT_TAGS{all} = [@EXPORT_OK];
49 use Encode qw(encode_utf8 is_utf8 decode);
51 use Storable qw(dclone);
56 These functions are exported with the :utf8 tag
58 =head2 encode_utf8_structure
60 %newdata = encode_utf8_structure(%newdata);
62 Takes a complex data structure and encodes any strings with is_utf8
63 set into their constituent octets.
68 sub encode_utf8_structure {
72 if (ref($_) eq 'HASH') {
73 push @ret, {encode_utf8_structure(%{$depth == 1 ? dclone($_):$_})};
75 elsif (ref($_) eq 'ARRAY') {
76 push @ret, [encode_utf8_structure(@{$depth == 1 ? dclone($_):$_})];
79 # we don't know how to handle non hash or non arrays
83 push @ret,encode_utf8_safely($_);
90 =head2 encode_utf8_safely
92 $octets = encode_utf8_safely($string);
94 Given a $string, returns the octet equivalent of $string if $string is
95 in perl's internal encoding; otherwise returns $string.
97 Silently returns REFs without encoding them. [If you want to deeply
98 encode REFs, see encode_utf8_structure.]
103 sub encode_utf8_safely{
106 if (not ref($r) and is_utf8($r)) {
107 $r = encode_utf8($r);
111 return wantarray ? @ret : (length @_ > 1 ? @ret : $_[0]);
114 =head2 convert_to_utf8
116 $utf8 = convert_to_utf8("text","charset");
120 our %iconv_converters;
122 sub convert_to_utf8 {
123 my ($data,$charset) = @_;
124 if (is_utf8($data)) {
125 return encode_utf8($data);
127 $charset = uc($charset);
128 if ($charset eq 'RAW') {
131 if (not defined $iconv_converters{$charset}) {
133 $iconv_converters{$charset} = Text::Iconv->new($charset,"UTF-8") or
134 die "Unable to create converter for '$charset'";
138 # We weren't able to create the converter, so use Encode
140 return __fallback_convert_to_utf8($data,$charset);
143 if (not defined $iconv_converters{$charset}) {
144 warn "The converter for $charset wasn't created properly somehow!";
145 return __fallback_convert_to_utf8($data,$charset);
147 my $converted_data = $iconv_converters{$charset}->convert($data);
148 $converted_data = "$converted_data";
149 # if the conversion failed, retval will be undefined or perhaps
151 my $retval = $iconv_converters{$charset}->retval();
152 if (not defined $retval or
155 # Fallback to encode, which will probably also fail.
156 return __fallback_convert_to_utf8($data,$charset);
158 return $converted_data;
162 # we're switching this to return UTF8 octets instead of perl's internal
164 sub __fallback_convert_to_utf8 {
165 my ($data, $charset) = @_;
166 # raw data just gets returned (that's the charset WordDecorder
167 # uses when it doesn't know what to do)
168 return $data if $charset eq 'raw';
169 if (not defined $charset and not is_utf8($data)) {
170 warn ("Undefined charset, and string '$data' is not in perl's internal encoding");
173 # lets assume everything that doesn't have a charset is utf8
177 $result = decode($charset,$data) unless is_utf8($data);
178 $result = encode_utf8($result);
181 warn "Unable to decode charset; '$charset' and '$data': $@";