Change 13399 by jhi@alpha on 2001/12/01 15:40:13

        Add simple caches for ::viacode() and ::vianame().

Affected files ...

.... //depot/perl/lib/charnames.pm#18 edit
.... //depot/perl/lib/charnames.t#4 edit

Differences ...

==== //depot/perl/lib/charnames.pm#18 (text) ====
Index: perl/lib/charnames.pm
--- perl/lib/charnames.pm.~1~   Sat Dec  1 08:45:05 2001
+++ perl/lib/charnames.pm       Sat Dec  1 08:45:05 2001
@@ -123,6 +123,8 @@
   }
 }
 
+my %viacode;
+
 sub viacode
 {
     if (@_ != 1) {
@@ -139,15 +141,19 @@
         return;
     }
 
+    return $viacode{$hex} if exists $viacode{$hex};
+
     $txt = do "unicore/Name.pl" unless $txt;
 
     if ($txt =~ m/^$hex\t\t(.+)/m) {
-        return $1;
+        return $viacode{$hex} = $1;
     } else {
         return;
     }
 }
 
+my %vianame;
+
 sub vianame
 {
     if (@_ != 1) {
@@ -157,10 +163,12 @@
 
     my $arg = shift;
 
+    return $vianame{$arg} if exists $vianame{$arg};
+
     $txt = do "unicore/Name.pl" unless $txt;
 
     if ($txt =~ m/^([0-9A-F]+)\t\t($arg)/m) {
-        return hex $1;
+        return $vianame{$arg} = hex $1;
     } else {
         return;
     }
@@ -213,7 +221,7 @@
 
 Note that C<\N{...}> is compile-time, it's a special form of string
 constant used inside double-quoted strings: in other words, you cannot
-used variables inside the C<\N{...}>.  If you want similar run-time
+use variables inside the C<\N{...}>.  If you want similar run-time
 functionality, use charnames::vianame().
 
 =head1 CUSTOM TRANSLATORS

==== //depot/perl/lib/charnames.t#4 (text) ====
Index: perl/lib/charnames.t
--- perl/lib/charnames.t.~1~    Sat Dec  1 08:45:05 2001
+++ perl/lib/charnames.t        Sat Dec  1 08:45:05 2001
@@ -8,7 +8,7 @@
 }
 
 $| = 1;
-print "1..20\n";
+print "1..22\n";
 
 use charnames ':full';
 
@@ -146,3 +146,15 @@
        defined charnames::vianame("NONE SUCH");
     print "ok 20\n";
 }
+
+{
+    # check that caching at least hasn't broken anything
+
+    print "not " unless charnames::viacode(0x1234) eq "ETHIOPIC SYLLABLE SEE";
+    print "ok 21\n";
+
+    print "not " unless
+       sprintf "%04X\n", charnames::vianame("GOTHIC LETTER AHSA") eq "10330";
+    print "ok 22\n";
+
+}
End of Patch.

Reply via email to