This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
APItest/t/utf8_setup.pl: Split function into two
[perl5.git] / ext / XS-APItest / t / utf8_setup.pl
CommitLineData
6aa905cf
KW
1# Common subroutines and constants, called by .t files in this directory that
2# deal with UTF-8
3
4# The test files can't use byte_utf8a_to_utf8n() from t/charset_tools.pl
5# because that uses the same functions we are testing here. So UTF-EBCDIC
6# strings are hard-coded as I8 strings in this file instead, and we use the
7# translation functions to/from I8 from that file instead.
8
9sub isASCII { ord "A" == 65 }
10
e86447a3 11sub display_bytes_no_quotes {
6aa905cf
KW
12 use bytes;
13 my $string = shift;
e86447a3
KW
14 return join("", map { sprintf("\\x%02x", ord $_) } split "", $string)
15}
16
17sub display_bytes {
18 return '"' . display_bytes_no_quotes(shift) . '"';
6aa905cf
KW
19}
20
21sub output_warnings(@) {
22 diag "The warnings were:\n" . join("", @_);
23}
24
25sub start_byte_to_cont($) {
26
27 # Extract the code point information from the input UTF-8 start byte, and
28 # return a continuation byte containing the same information. This is
29 # used in constructing an overlong malformation from valid input.
30
31 my $byte = shift;
32 my $len = test_UTF8_SKIP($byte);
33 if ($len < 2) {
34 die "start_byte_to_cont() is expecting a UTF-8 variant";
35 }
36
37 $byte = ord native_to_I8($byte);
38
39 # Copied from utf8.h. This gets rid of the leading 1 bits.
40 $byte &= ((($len) >= 7) ? 0x00 : (0x1F >> (($len)-2)));
41
42 $byte |= (isASCII) ? 0x80 : 0xA0;
43 return I8_to_native(chr $byte);
44}
45
46$::is64bit = length sprintf("%x", ~0) > 8;
47
48$::first_continuation = (isASCII) ? 0x80 : 0xA0;
49
50$::I8c = (isASCII) ? "\x80" : "\xa0"; # A continuation byte
51
52
53$::max_bytes = (isASCII) ? 13 : 14; # Max number of bytes in a UTF-8 sequence
54 # representing a single code point
55
56# Copied from utf8.h
57$::UTF8_ALLOW_EMPTY = 0x0001;
58$::UTF8_GOT_EMPTY = $UTF8_ALLOW_EMPTY;
59$::UTF8_ALLOW_CONTINUATION = 0x0002;
60$::UTF8_GOT_CONTINUATION = $UTF8_ALLOW_CONTINUATION;
61$::UTF8_ALLOW_NON_CONTINUATION = 0x0004;
62$::UTF8_GOT_NON_CONTINUATION = $UTF8_ALLOW_NON_CONTINUATION;
63$::UTF8_ALLOW_SHORT = 0x0008;
64$::UTF8_GOT_SHORT = $UTF8_ALLOW_SHORT;
65$::UTF8_ALLOW_LONG = 0x0010;
66$::UTF8_ALLOW_LONG_AND_ITS_VALUE = $UTF8_ALLOW_LONG|0x0020;
67$::UTF8_GOT_LONG = $UTF8_ALLOW_LONG;
68$::UTF8_ALLOW_OVERFLOW = 0x0080;
69$::UTF8_GOT_OVERFLOW = $UTF8_ALLOW_OVERFLOW;
70$::UTF8_DISALLOW_SURROGATE = 0x0100;
71$::UTF8_GOT_SURROGATE = $UTF8_DISALLOW_SURROGATE;
72$::UTF8_WARN_SURROGATE = 0x0200;
73$::UTF8_DISALLOW_NONCHAR = 0x0400;
74$::UTF8_GOT_NONCHAR = $UTF8_DISALLOW_NONCHAR;
75$::UTF8_WARN_NONCHAR = 0x0800;
76$::UTF8_DISALLOW_SUPER = 0x1000;
77$::UTF8_GOT_SUPER = $UTF8_DISALLOW_SUPER;
78$::UTF8_WARN_SUPER = 0x2000;
79$::UTF8_DISALLOW_ABOVE_31_BIT = 0x4000;
80$::UTF8_GOT_ABOVE_31_BIT = $UTF8_DISALLOW_ABOVE_31_BIT;
81$::UTF8_WARN_ABOVE_31_BIT = 0x8000;
82$::UTF8_CHECK_ONLY = 0x10000;
a228a8af
KW
83$::UTF8_NO_CONFIDENCE_IN_CURLEN_ = 0x20000;
84
6aa905cf
KW
85$::UTF8_DISALLOW_ILLEGAL_C9_INTERCHANGE
86 = $UTF8_DISALLOW_SUPER|$UTF8_DISALLOW_SURROGATE;
87$::UTF8_DISALLOW_ILLEGAL_INTERCHANGE
88 = $UTF8_DISALLOW_ILLEGAL_C9_INTERCHANGE|$UTF8_DISALLOW_NONCHAR;
89$::UTF8_WARN_ILLEGAL_C9_INTERCHANGE
90 = $UTF8_WARN_SUPER|$UTF8_WARN_SURROGATE;
91$::UTF8_WARN_ILLEGAL_INTERCHANGE
92 = $UTF8_WARN_ILLEGAL_C9_INTERCHANGE|$UTF8_WARN_NONCHAR;
93
94# Test uvchr_to_utf8().
95$::UNICODE_WARN_SURROGATE = 0x0001;
96$::UNICODE_WARN_NONCHAR = 0x0002;
97$::UNICODE_WARN_SUPER = 0x0004;
98$::UNICODE_WARN_ABOVE_31_BIT = 0x0008;
99$::UNICODE_DISALLOW_SURROGATE = 0x0010;
100$::UNICODE_DISALLOW_NONCHAR = 0x0020;
101$::UNICODE_DISALLOW_SUPER = 0x0040;
102$::UNICODE_DISALLOW_ABOVE_31_BIT = 0x0080;