Welcome to mirror list, hosted at ThFree Co, Russian Federation.

encoding_name.pl « MyHTML « utils - github.com/lexborisov/Modest.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d2b864172656947157ddc643d4dd11a254672cba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#!/usr/bin/perl -w

BEGIN {
        use FindBin;
        push @INC, $FindBin::Bin. "/../ext/";
};

use utf8;
use strict;
use MyHTML::Base;
use JSON::XS;

my $static_list_index_length = 419;

my $utils = MyHTML::Base->new(dirs => {source => "../../source/myhtml", template => "tmpl"});
my $raw_data = $utils->read_tmpl_raw("encoding/encodings.json");
my $encodings = decode_json(join("", @$raw_data));
#test_result();

my ($result, $index) = create_result($encodings, $static_list_index_length);
print_enum($index);
#print_for_test_meta($result);

#my $static_list = create_static_list_index($result);
#print $static_list, "\n";

print "";

sub print_for_test_meta {
        my ($result) = @_;
        
        foreach my $key (sort {$a cmp $b} keys %$result) {
                foreach my $encode_entry (sort {$a cmp $b} @{$result->{$key}})
                {
                        print $encode_entry->[0], "\n";
                        print '<meta http-equiv="Content-Type" Content="text/html; charset=', $encode_entry->[1], '">', "\n\n";
                }
        }
}

sub create_result {
        my ($encodings, $static_list_index_length) = @_;
        my $result = {};
        my $index = {};
        
        foreach my $entry (@$encodings) {
                foreach my $encoding (@{$entry->{encodings}}) {
                        next if $encoding->{name} =~ /replacement/i;
                        
                        $index->{ $encoding->{name} } = 1;
                        
                        foreach my $label (sort {$a cmp $b} @{$encoding->{labels}}) {
                                my $id = get_index_id($label, $static_list_index_length);
                                
                                push @{$result->{$id}}, [$encoding->{name}, $label, length($label), length($encoding->{name})];
                        }
                }
        }
        
        ($result, $index);
}

sub test_result {
        my $op = [0, undef];
        
        foreach my $idx (1..2048) {
                my $result = create_result($encodings, $idx);
                my $res_max = test_result_max_value($result, 0);
                
                if(!defined($op->[1]) || $op->[1] > $res_max) {
                        $op->[0] = $idx;
                        $op->[1] = $res_max;
                }
        }
        
        print "Best:\n";
        print $op->[0], ": ", $op->[1], "\n";
}

sub test_result_max_value {
        my ($res, $is_print) = @_;
        
        my $max = 0;
        foreach my $id (sort {scalar(@{$res->{$a}}) <=> scalar(@{$res->{$b}})} keys %$res) {
                print "$id: ", scalar(@{$res->{$id}}), "\n" if $is_print;
                
                $max = scalar(@{$res->{$id}}) if $max < scalar(@{$res->{$id}});
        }
        
        $max;
}

sub get_index_id {
        use bytes;
        
        my @chars = split //, $_[0];
        my $f = ord(lc($chars[0]));
        my $l = ord(lc($chars[-1]));
        
        ((($f * $l * scalar(@chars))) % $_[1]) + 1;
}

sub create_sub_static_list_index {
        my ($result, $struct, $offset) = @_;
        
        my @list_sorted = sort {$a->[2] <=> $b->[2]} @$result[0..$#$result];
        
        foreach my $i (1..$#list_sorted) {
                my $cur = $offset;
                $offset++;
                
                push @$struct, "\t{".
                '"'. $list_sorted[$i]->[0] .'", '. $list_sorted[$i]->[3] .', '.
                '"'. $list_sorted[$i]->[1] .'", '. $list_sorted[$i]->[2] .', '.
                name_to_myhtml_encoding($list_sorted[$i]->[0]), ', '.
                ($i < $#list_sorted ? $offset : 0) .", $cur},\n";
        }
        
        $offset;
}

sub create_static_list_index {
        my ($result) = @_;
        
        my @res;
        my $struct = [];
        my $offset = $static_list_index_length + 1;
        
        foreach my $i (0..$static_list_index_length)
        {
                if(exists $result->{$i}) {
                        my $id = 0;
                        
                        if(scalar @{$result->{$i}} > 1) {
                                $offset = create_sub_static_list_index($result->{$i}, $struct, $offset);
                                
                                $id = $offset - (@{$result->{$i}} - 1);
                        }
                        
                        my @list_sorted = sort {$a->[2] <=> $b->[2]} @{$result->{$i}}[0..$#{$result->{$i}}];
                        
                        push @res, "\t{".
                        '"'. $list_sorted[0]->[0] .'", '. $list_sorted[0]->[3] .', '.
                        '"'. $list_sorted[0]->[1] .'", '. $list_sorted[0]->[2] .', '.
                        name_to_myhtml_encoding($list_sorted[0]->[0]), ', '.
                        "$id, $i},\n";
				}
				else {
                        push @res, "\t{NULL, 0, NULL, 0, 0, 0, 0},\n";
                }
        }
        
        "static const myhtml_encoding_detect_name_entry_t myhtml_encoding_detect_name_entry_static_list_index[] = \n{\n". join("", @res, @$struct) ."};\n"
}

sub name_to_myhtml_encoding {
        my ($name) = @_;
        
        $name =~ s/[-]+/_/g;
        $name = "MyHTML_ENCODING_". uc($name);
        
        $name;
}

sub print_enum {
        my ($index) = @_;
	
	print "enum myhtml_encoding_list {\n\t";
        
        my @vals;
        push @vals, ["MyHTML_ENCODING_DEFAULT", "0x00", "utf_8"];
	push @vals, ["MyHTML_ENCODING_AUTO", "0x01", "NULL"];
	push @vals, ["MyHTML_ENCODING_NOT_DETERMINED", "0x02", "NULL"];
        push @vals, ["MyHTML_ENCODING_UTF_8", "0x00", "utf_8"];
	push @vals, ["MyHTML_ENCODING_UTF_16LE", "0x04", "utf_16le"];
	push @vals, ["MyHTML_ENCODING_UTF_16BE", "0x05", , "utf_16be"];
	push @vals, ["MyHTML_ENCODING_X_USER_DEFINED", "0x06", "x_user_defined"];
	
	my $i = 7; 
	foreach my $id (sort {$a cmp $b} keys %$index) {
                $id =~ s/[-\s]+/_/g;
                next if $id =~ /UTF_8$/i;
                next if $id =~ /UTF_16LE$/i;
                next if $id =~ /UTF_16BE$/i;
                next if $id =~ /X_USER_DEFINED$/i;
                
                push @vals, ["MyHTML_ENCODING_". uc($id), sprintf("0x%02x", $i), lc($id)];
                
		$i++;
	}
	
        push @vals, ["MyHTML_ENCODING_LAST_ENTRY", sprintf("0x%02x", $i), "NULL"];
        
        print join(",\n\t", @{MyHTML::Base->format_list_text(\@vals, "= ")}), "\n";
        
	print "}\ntypedef myhtml_encoding_t;\n\n";
        
        print "static const myhtml_encoding_custom_f myhtml_encoding_function_index[] = \n{\n";
        foreach my $entry (@vals) {
                if($entry->[2] eq "NULL") {
                        print "\t",  $entry->[2], ",\n";
                } else {
                        print "\t",  "myhtml_encoding_decode_", $entry->[2], ",\t// ", $entry->[0], "\n";
                }
        }
        
        print "};\n";
        
        print "static const myhtml_encoding_entry_name_index_t myhtml_encoding_entry_name_index_static_list_index[(MyHTML_ENCODING_LAST_ENTRY + 1)] =\n{\n";
        
        foreach my $entry (sort {hex($a->[1]) <=> hex($b->[1])} @vals) {
                $entry->[0] =~ s/MyHTML_ENCODING_//;
                
                if($entry->[0] ne "SHIFT_JIS") {
                        $entry->[0] =~ s/_/-/g;
                }
                
                if($entry->[0] eq "LAST-ENTRY") {
                        $entry->[0] = "";
                }
                
                #if($entry->[0] eq "AUTO") {
                #        $entry->[0] = "";
                #}
                
                print "\t", '{"', $entry->[0], '", ', length($entry->[0]), "}", ",\n";
        }
        
        print "};\n";
}