Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: How to generate random sequence of UTF-8 characters

by ted.byers (Scribe)
on Dec 20, 2012 at 23:47 UTC ( #1009816=note: print w/ replies, xml ) Need Help??


in reply to How to generate random sequence of UTF-8 characters

OK. Slapping ideas together, and extracting commonalities, from among the responses so far and combining with constructing a random string, I tried the following experiment.

use strict; use warnings; use Math::Random::MT::Auto::Range; binmode(STDOUT, ":utf8"); $| = 1; #the following exhausts all memory #my $all = join q[], grep /^\w$/, map chr, shuffle 0 .. 0x10FFFF; #print $all,"\n"; # # so I tried: my @chars = map(chr, 0 .. 0x10FFFF); my $rng = Math::Random::MT::Auto::Range->new(LO => 0, HI => 0x10FFFF, TYPE => 'INTEGER' +); # three options for doing the basically the same thing for (my $i = 0 ; $i < 10 ; $i++) { my $code = $rng->rrand; print "$i <=> $code <=>",chr($code),"\n"; print "\t$i <=> $code <=>",$chars[$code],"\n"; print "\t$i <=> $code <=>",pack("U",$code),"\n"; }

There are a couple problems with this. chr generates some errors and warnings. There are a couple thousand instances of "UTF-16 surrogate 0xd81b at c:/Work/test.utf8.latin1.pl line 16.", and over a hundred instances of "Unicode non-character 0xfdd0 is illegal for interchange at c:/Work/test.utf8.latin1.pl line 16.", each for a different integer. Line 16 is the line where @chars is initialized. I suppose a couple thousand problem characters in @chars is not a huge issue, but given that I want to use it to test functions for converting from utf-8 to latin1 and back, I expect that if they happen to occur in my sample, there'd be some false indications of errors in these functions (from the Perl package 'Encode').

The second problem is that though I put the statement "binmode(STDOUT, ":utf8");" at the start of the script, the printout contains only rectangles and square where the UTF-8 character ought to be; at least when I execute within Emacs. When I execute the script in the Windows commandline terminal, I invariably get gibberish (different for each character) the width of four characters. How, then, do I actually see the characters? I thought, probably mistakenly, I'd see a few greek or sanskrit characters or characters from other alphabets.

What I was thinking of doing is amend the loop I show above to construct a single string from the ten characters produced, and then use the functions from 'Encode' to convert it to latin1, and then back to utf-8, to see if the input string and the output string are the same (and if not, then this whole idea is doomed to fail). I'd repeat this test for a few million random utf-8 strings, and if there are no failures, then I could use this idea as a temporary measure until I can test our systems to see how best to adapt to use of utf-8 throughout.

What, then, do I do to exclude the integers that either result in a utf-16 surrogate and those that represent illegal characters? And is it possible for me to actually see the characters produced?

Thanks

Ted


Comment on Re: How to generate random sequence of UTF-8 characters
Download Code
Re^2: How to generate random sequence of UTF-8 characters
by GrandFather (Cardinal) on Dec 21, 2012 at 01:29 UTC

    At this point you need to take a step back and consider what you actually want to achieve. To help that process take a look at Unicode#Code_point_planes_and_blocks and consider that large areas of the code space are undefined and that even the areas where there are defined characters it is very likely that your system doesn't have font support for the characters anyway. Also take a look at ISO-8859-1 - if all you are doing is checking conversion from latin 1 to UTF-8 then you have fewer than 256 characters to bother about.

    The other part of the problem is that Windows essentially does not do UTF-8. Internally Windows uses UTF-16LE, but most often what gets rendered is the Windows-1252 code page. Emacs seems to be doing the right thing - render a rectangle when the character is missing from whatever font is being used.

    True laziness is hard work

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1009816]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (13)
As of 2014-08-20 13:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (113 votes), past polls