14 use overload '""' => 'stringify', fallback => 1;
20 return bless [$value, $state], $class;
25 $self->[1] = ! $self->[1];
27 utf8::downgrade($self->[0]);
29 utf8::upgrade($self->[0]);
37 foreach my $t ("ASCII", "B\366se") {
38 my $length = length $t;
40 my $u = UTF8Toggle->new($t);
41 is (length $u, $length, "length of '$t'");
42 is (length $u, $length, "length of '$t'");
43 is (length $u, $length, "length of '$t'");
44 is (length $u, $length, "length of '$t'");
47 my $u = UTF8Toggle->new("\311");
50 is ($lc, "\311", "E acute -> e acute");
53 is ($lc, "\351", "E acute -> e acute");
56 is ($lc, "\311", "E acute -> e acute");
58 $u = UTF8Toggle->new("\351");
61 is ($uc, "\351", "e acute -> E acute");
64 is ($uc, "\311", "e acute -> E acute");
67 is ($uc, "\351", "e acute -> E acute");
69 $u = UTF8Toggle->new("\311");
72 is ($lc, "\311", "E acute -> e acute");
75 is ($lc, "\351", "E acute -> e acute");
78 is ($lc, "\311", "E acute -> e acute");
80 $u = UTF8Toggle->new("\351");
83 is ($uc, "\351", "e acute -> E acute");
86 is ($uc, "\311", "e acute -> E acute");
89 is ($uc, "\351", "e acute -> E acute");
91 my $have_setlocale = 0;
94 import POSIX ':locale_h';
99 if (!$have_setlocale) {
100 skip "No setlocale", 24;
101 } elsif (!setlocale(&POSIX::LC_ALL, "en_GB.ISO8859-1")) {
102 skip "Could not setlocale to en_GB.ISO8859-1", 24;
103 } elsif ($^O eq 'dec_osf' || $^O eq 'VMS') {
104 skip "$^O has broken en_GB.ISO8859-1 locale", 24;
107 my $u = UTF8Toggle->new("\311");
110 is ($lc, "\351", "E acute -> e acute");
113 is ($lc, "\351", "E acute -> e acute");
116 is ($lc, "\351", "E acute -> e acute");
118 $u = UTF8Toggle->new("\351");
121 is ($uc, "\311", "e acute -> E acute");
124 is ($uc, "\311", "e acute -> E acute");
127 is ($uc, "\311", "e acute -> E acute");
129 $u = UTF8Toggle->new("\311");
132 is ($lc, "\351", "E acute -> e acute");
135 is ($lc, "\351", "E acute -> e acute");
138 is ($lc, "\351", "E acute -> e acute");
140 $u = UTF8Toggle->new("\351");
143 is ($uc, "\311", "e acute -> E acute");
146 is ($uc, "\311", "e acute -> E acute");
149 is ($uc, "\311", "e acute -> E acute");
153 my $tmpfile = tempfile();
155 foreach my $operator ('print', 'syswrite', 'syswrite len', 'syswrite off',
156 'syswrite len off') {
157 foreach my $layer ('', ':utf8') {
158 open my $fh, "+>$layer", $tmpfile or die $!;
159 my $pad = $operator =~ /\boff\b/ ? "\243" : "";
160 my $trail = $operator =~ /\blen\b/ ? "!" : "";
161 my $u = UTF8Toggle->new("$pad\311\n$trail");
162 my $l = UTF8Toggle->new("$pad\351\n$trail", 1);
163 if ($operator eq 'print') {
171 } elsif ($operator eq 'syswrite') {
178 } elsif ($operator eq 'syswrite len') {
185 } elsif ($operator eq 'syswrite off'
186 || $operator eq 'syswrite len off') {
187 syswrite $fh, $u, 2, 1;
188 syswrite $fh, $u, 2, 1;
189 syswrite $fh, $u, 2, 1;
190 syswrite $fh, $l, 2, 1;
191 syswrite $fh, $l, 2, 1;
192 syswrite $fh, $l, 2, 1;
197 seek $fh, 0, 0 or die $!;
199 chomp ($line = <$fh>);
200 is ($line, "\311", "$operator $layer");
201 chomp ($line = <$fh>);
202 is ($line, "\311", "$operator $layer");
203 chomp ($line = <$fh>);
204 is ($line, "\311", "$operator $layer");
205 chomp ($line = <$fh>);
206 is ($line, "\351", "$operator $layer");
207 chomp ($line = <$fh>);
208 is ($line, "\351", "$operator $layer");
209 chomp ($line = <$fh>);
210 is ($line, "\351", "$operator $layer");
216 my $little = "\243\243";
217 my $big = " \243 $little ! $little ! $little \243 ";
218 my $right = rindex $big, $little;
219 my $right1 = rindex $big, $little, 11;
220 my $left = index $big, $little;
221 my $left1 = index $big, $little, 4;
223 cmp_ok ($right, ">", $right1, "Sanity check our rindex tests");
224 cmp_ok ($left, "<", $left1, "Sanity check our index tests");
226 foreach my $b ($big, UTF8Toggle->new($big)) {
227 foreach my $l ($little, UTF8Toggle->new($little),
228 UTF8Toggle->new($little, 1)) {
229 is (rindex ($b, $l), $right, "rindex");
230 is (rindex ($b, $l), $right, "rindex");
231 is (rindex ($b, $l), $right, "rindex");
233 is (rindex ($b, $l, 11), $right1, "rindex 11");
234 is (rindex ($b, $l, 11), $right1, "rindex 11");
235 is (rindex ($b, $l, 11), $right1, "rindex 11");
237 is (index ($b, $l), $left, "index");
238 is (index ($b, $l), $left, "index");
239 is (index ($b, $l), $left, "index");
241 is (index ($b, $l, 4), $left1, "index 4");
242 is (index ($b, $l, 4), $left1, "index 4");
243 is (index ($b, $l, 4), $left1, "index 4");
248 foreach my $pieces ($bits, UTF8Toggle->new($bits)) {
249 like ($bits ^ $pieces, qr/\A\0+\z/, "something xor itself is zeros");
250 like ($bits ^ $pieces, qr/\A\0+\z/, "something xor itself is zeros");
251 like ($bits ^ $pieces, qr/\A\0+\z/, "something xor itself is zeros");
253 like ($pieces ^ $bits, qr/\A\0+\z/, "something xor itself is zeros");
254 like ($pieces ^ $bits, qr/\A\0+\z/, "something xor itself is zeros");
255 like ($pieces ^ $bits, qr/\A\0+\z/, "something xor itself is zeros");
258 foreach my $value ("\243", UTF8Toggle->new("\243")) {
259 is (pack ("A/A", $value), pack ("A/A", "\243"),
260 "pack copes with overloading");
261 is (pack ("A/A", $value), pack ("A/A", "\243"));
262 is (pack ("A/A", $value), pack ("A/A", "\243"));
265 foreach my $value ("\243", UTF8Toggle->new("\243")) {
267 $v = substr $value, 0, 1;
269 $v = substr $value, 0, 1;
271 $v = substr $value, 0, 1;
277 use overload '""' => sub { $_[0]->{data} }
281 my $text = bless { data => "\x{3075}" }, 'RT69422';
282 my $p = substr $text, 0, 1;