chiark / gitweb /
canon: use base64 not base62
[ypp-sc-tools.main.git] / yarrg / canon
index 9ae01e5abf476dd829dfcc69c7a04d83bf17ed62..35a17783292eeed336b719e7c4a790377b2ded30 100755 (executable)
@@ -25,12 +25,16 @@ sub alencodenum ($) {
     my $res= '';
     while ($val || !length($res)) {
         # allowing empty strings, reusing "0" for 62, doing base63,
-        # saves 0.5%
-        my $dig= $val % 62;
-        $val= ($val-$dig) / 62;
-        $res = chr($dig + ($dig<10 ? 48 :
-                           $dig<36 ? 97-10 :
-                           $dig<62 ? 65-36 : die $dig)) . $res;
+        # saves 0.5%, compared to base62
+        # actually we do base64 because it'll be faster to compute
+        my $dig= $val & 63;
+        $val >>= 6;
+        $res = ($dig < 10 ? chr(48 + $dig) :
+                $dig < 36 ? chr(97 + $dig - 10) :
+                $dig < 62 ? chr(65 + $dig - 36) :
+                $dig==62 ? '_' :
+                $dig==63 ? '.' : die $dig).
+                $res;
     }
     return $res;
 }