2 Data structures for encoding transformations.
4 Perl works internally in either a native 'byte' encoding or
5 in UTF-8 encoded Unicode. We have no immediate need for a "wchar_t"
6 representation. When we do we can use utf8_to_uv().
8 Most character encodings are either simple byte mappings or
9 variable length multi-byte encodings. UTF-8 can be viewed as a
10 rather extreme case of the latter.
12 So to solve an important part of perl's encode needs we need to solve the
13 "multi-byte -> multi-byte" case. The simple byte forms are then just degenerate
14 case. (Where one of multi-bytes will usually be UTF-8.)
16 The other type of encoding is a shift encoding where a prefix sequence
17 determines what subsequent bytes mean. Such encodings have state.
19 We also need to handle case where a character in one encoding has to be
20 represented as multiple characters in the other. e.g. letter+diacritic.
22 The process can be considered as pseudo perl:
27 my $size = $count($src);
28 my $in_seq = substr($src,0,$size,'');
29 my $out_seq = $s2d_hash{$in_seq};
41 That has the following components:
42 &src_count - a "rule" for how many bytes make up the next character in the
44 %s2d_hash - a mapping from input sequences to output sequences
46 The problem with that scheme is that it does not allow the output
47 character repertoire to affect the characters considered from the
50 So we use a "trie" representation which can also be considered
55 my $next = \@s2d_next;
58 my $byte = $substr($src,0,1,'');
59 my $out_seq = $seq->[$byte];
68 ($next,$seq) = @$next->[$byte] if $next;
72 There is now a pair of data structures to represent everything.
73 It is valid for output sequence at a particular point to
74 be defined but zero length, that just means "don't know yet".
75 For the single byte case there is no 'next' so new tables will be the same as
76 the original tables. For a multi-byte case a prefix byte will flip to the tables
77 for the next page (adding nothing to the output), then the tables for the page
78 will provide the actual output and set tables back to original base page.
80 This scheme can also handle shift encodings.
82 A slight enhancement to the scheme also allows for look-ahead - if
83 we add a flag to re-add the removed byte to the source we could handle
85 ab -> a (and take b back please)
95 do_encode(encpage_t * enc, const U8 * src, STRLEN * slen, U8 * dst,
96 STRLEN dlen, STRLEN * dout, int approx)
99 const U8 *send = s + *slen;
107 while (byte > e->max)
109 if (byte >= e->min && e->slen && (approx || !(e->slen & 0x80))) {
110 const U8 *cend = s + (e->slen & 0x7f);
114 const U8 *out = e->seq + n * (byte - e->min);
123 code = ENCODE_NOSPACE;
133 if (approx && (e->slen & 0x80))
134 code = ENCODE_FALLBACK;
139 /* partial source character */
140 code = ENCODE_PARTIAL;
145 /* Cannot represent */