MHonArc/UTF8/MapUTF8.pm untested

filter: Statementstmt 0.0% 0 / 88 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 146 CC 16 SCAR 37.1
CC
16
Cov
0%
clip
72.0
str2sgml
56.0
to_utf8
12.0
CRAP 40.7
1 ##---------------------------------------------------------------------------##
2 ## File:
3 ## $Id: MapUTF8.pm,v 1.2 2003/03/05 22:17:15 ehood Exp $
4 ## Author:
5 ## Earl Hood earl@earlhood.com
6 ## Description:
7 ## POD after __END__.
8 ##---------------------------------------------------------------------------##
9 ## Copyright (C) 2002 Earl Hood, earl@earlhood.com
10 ##
11 ## This program is free software; you can redistribute it and/or modify
12 ## it under the terms of the GNU General Public License as published by
13 ## the Free Software Foundation; either version 2 of the License, or
14 ## (at your option) any later version.
15 ##
16 ## This program is distributed in the hope that it will be useful,
17 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ## GNU General Public License for more details.
20 ##
21 ## You should have received a copy of the GNU General Public License
22 ## along with this program; if not, write to the Free Software
23 ## Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
24 ## 02111-1307, USA
25 ##---------------------------------------------------------------------------##
26  
27 package MHonArc::UTF8::MapUTF8;
28  
29 use strict;
30 use Unicode::String;
31 use Unicode::MapUTF8;
32 use MHonArc::CharMaps;
33  
34 our $VERSION = '2.6.24';
35  
36 sub clip {
37 use utf8;
38 my $str = \shift; # Prevent unnecessary copy.
39 my $len = shift; # Clip length
40 my $is_html = shift; # If entity references should be considered
41 my $has_tags = shift; # If html tags should be stripped
42  
43 my $u = Unicode::String::utf8($$str);
44  
45 if (!$is_html) {
46 return $u->substr(0, $len);
47 }
48  
49 my $text = Unicode::String::utf8('');
50 my $subtext;
51 my $html_len = $u->length;
52 my ($pos, $sublen, $real_len, $semi);
53 my $er_len = 0;
54  
55 for ($pos = 0, $sublen = $len; $pos < $html_len;) {
56 $subtext = $u->substr($pos, $sublen);
57 $pos += $sublen;
58  
59 # strip tags
60 if ($has_tags) {
61 # Strip full tags
62 $subtext =~ s/<[^>]*>//g;
63 # Check if clipped part of a tag
64 if ($subtext =~ s/<[^>]*\Z//) {
65 my $gt = $u->index('>', $pos);
66 $pos = ($gt < 0) ? $html_len : ($gt + 1);
67 }
68 }
69  
70 # check for clipped entity reference
71 if (($pos < $html_len) && ($subtext =~ /\&[^;]*\Z/)) {
72 my $semi = $u->index(';', $pos);
73 if ($semi < 0) {
74 # malformed entity reference
75 $subtext .= $u->substr($pos);
76 $pos = $html_len;
77 } else {
78 $subtext .= $u->substr($pos, $semi - $pos + 1);
79 $pos = $semi + 1;
80 }
81 }
82  
83 # compute entity reference lengths to determine "real" character
84 # count and not raw character count.
85 while ($subtext =~ /(\&[^;]+);/g) {
86 $er_len += length($1);
87 }
88  
89 $text .= $subtext;
90  
91 # done if we have enough
92 $real_len = $text->length - $er_len;
93 if ($real_len >= $len) {
94 last;
95 }
96 $sublen = $len - ($text->length - $er_len);
97 }
98 $text;
99 }
100  
101 sub to_utf8 {
102 my $text = shift;
103 my $charset = lc shift;
104 my $text_r = ref($text) ? $text : \$text;
105  
106 if (Unicode::MapUTF8::utf8_supported_charset($charset)) {
107 return Unicode::MapUTF8::to_utf8(
108 {-string => $$text_r, -charset => $charset});
109 }
110 # Invoke fallback implementation.
111 require MHonArc::UTF8::MhaEncode;
112 return MHonArc::UTF8::MhaEncode::to_utf8($text_r, $charset);
113 }
114  
115 sub str2sgml {
116 my $text = shift;
117 my $charset = lc shift;
118 my $text_r = ref($text) ? $text : \$text;
119  
120 if ($charset eq 'us-ascii') {
121 if ($$text_r =~ /[\x80-\xFF]/) {
122 $charset = 'iso-8859-1';
123 } else {
124 $$text_r =~ s/([$HTMLSpecials])/$HTMLSpecials{$1}/go;
125 return $$text_r;
126 }
127 }
128 if ($charset eq 'utf-8' || $charset eq 'utf8') {
129 $$text_r =~ s/([$HTMLSpecials])/$HTMLSpecials{$1}/go;
130 return $$text_r;
131 }
132 if ($charset eq 'utf-8' || $charset eq 'utf8') {
133 $$text_r =~ s/([$HTMLSpecials])/$HTMLSpecials{$1}/go;
134 return $$text_r;
135 }
136 if (Unicode::MapUTF8::utf8_supported_charset($charset)) {
137 $$text_r = Unicode::MapUTF8::to_utf8(
138 {-string => $$text_r, -charset => $charset});
139 $$text_r =~ s/([$HTMLSpecials])/$HTMLSpecials{$1}/go;
140 return $$text_r;
141 }
142 # Invoke fallback implementation.
143 require MHonArc::UTF8::MhaEncode;
144 return MHonArc::UTF8::MhaEncode::str2sgml($text_r, $charset);
145 }
146  
147 ##---------------------------------------------------------------------------##
148 1;
149 __END__