How about using transliteration? Transliteration is marginally quicker than pattern matching. Here's a solution that will trigger the error condition if the string contains anything outside of \0-\x7f range. It works by using the /c modifier on a tr/// transliteration operator. Refresher course: the /c modifier complements the search list, and if no "replace" list is specified, one that exactly matches the search list is generated behind the scenes. That has the effect of leaving the original string untouched, only counting characters that match the criteria (or in this case, counting the ones that match the complement to the criteria specified, thanks to /c)
for my $str ( "\x7f", "asdf", "asdf\x8f", "\x8f" ) { print "$str contans ", ( $str =~ tr/\0-\x7f//c ) ? "non-" : "only ", "ascii.\n"; }
A lot of code there is just testing framework. The engine at work is this:
tr/\0-\x7f//cIf that tests positive, you've got non-ascii characters in your string. Details about tr/// can be found in perlop.
Dave
In reply to Re: testing if a string is ascii
by davido
in thread testing if a string is ascii
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |