| 1 | # /=====================================================================\ # | ||||||||||
| 2 | # | LaTeXML::Util::Radix | # | ||||||||||
| 3 | # | PostProcessing driver | # | ||||||||||
| 4 | # |=====================================================================| # | ||||||||||
| 5 | # | Part of LaTeXML: | # | ||||||||||
| 6 | # | Public domain software, produced as part of work done by the | # | ||||||||||
| 7 | # | United States Government & not subject to copyright in the US. | # | ||||||||||
| 8 | # |---------------------------------------------------------------------| # | ||||||||||
| 9 | # | Bruce Miller <bruce.miller@nist.gov> #_# | # | ||||||||||
| 10 | # | http://dlmf.nist.gov/LaTeXML/ (o o) | # | ||||||||||
| 11 | # \=========================================================ooo==U==ooo=/ # | ||||||||||
| 12 | package LaTeXML::Util::Radix; | ||||||||||
| 13 | 28 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | 28 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | 28 | ▶ | use base qw(Exporter); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 16 | 28 | our @EXPORT = (qw( &radix_format | |||||||||
| 17 | &radix_alpha &radix_Alpha &radix_greek &radix_Greek | ||||||||||
| 18 | &radix_roman &radix_Roman)); | ||||||||||
| 19 | #====================================================================== | ||||||||||
| 20 | # This isn't really any sort of general purpose Radix module, | ||||||||||
| 21 | # probably the term "radix" is a misnomer here! | ||||||||||
| 22 | # It is used to primarily generate labels, or uniquifying suffixes to make ID's, | ||||||||||
| 23 | # Bibtex year tags like 2013a, etc using alphabetic letters, or | ||||||||||
| 24 | # perhaps greek, or even from a set of symbols. | ||||||||||
| 25 | # | ||||||||||
| 26 | # The general idea is simply to generate labels in the sequence: | ||||||||||
| 27 | # a,b,c,...y,z,aa,ab,ac,...az,ba,...zy,zz,aaa,aab,.... and so on. | ||||||||||
| 28 | # I would assume that the usual advise is that it is bad style to pass, | ||||||||||
| 29 | # or even approach "z"; However, this is an automaton, and things happen. | ||||||||||
| 30 | #====================================================================== | ||||||||||
| 31 | |||||||||||
| 32 | 0 | ▶ | sub radix_format { | ||||||||
|
Subroutine
radix_format: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 33 | 0 | my ($number, @symbols) = @_; | |||||||||
| 34 | 0 | my $string = ''; | |||||||||
| 35 | 0 | my $max = scalar(@symbols); | |||||||||
| 36 | 0 | ▶ | while ($number > 0) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 37 | 0 | $string = $symbols[($number - 1) % $max] . $string; | |||||||||
| 38 | 0 | $number = int(($number - 1) / $max); } | |||||||||
| 39 | 0 | return $string; } | |||||||||
| 40 | |||||||||||
| 41 | 28 | my @letters = (qw(a b c d e f g h i j k l m n o p q r s t u v w x y z)); | |||||||||
| 42 | 28 | my @Letters = (qw(A B C D E F G H I J K L M N O P Q R S T U V W X Y Z)); | |||||||||
| 43 | 28 | my @greek = ("\x{03B1}", "\x{03B2}", "\x{03B3}", | |||||||||
| 44 | "\x{03B4}", "\x{03B5}", "\x{03B6}", "\x{03B7}", | ||||||||||
| 45 | "\x{03B8}", "\x{03B9}", "\x{03BA}", "\x{03BB}", | ||||||||||
| 46 | "\x{03BC}", "\x{03BD}", "\x{03BE}", "\x{03BF}", | ||||||||||
| 47 | "\x{03C0}", "\x{03C1}", "\x{03C3}", | ||||||||||
| 48 | "\x{03C4}", "\x{03C5}", "\x{03C6}", "\x{03C7}", | ||||||||||
| 49 | "\x{03C8}", "\x{03C9}"); | ||||||||||
| 50 | 28 | my @Greek = ("\x{0391}", "\x{0392}", "\x{0393}", | |||||||||
| 51 | "\x{0394}", "\x{0395}", "\x{0396}", "\x{0397}", | ||||||||||
| 52 | "\x{0398}", "\x{0399}", "\x{039A}", "\x{039B}", | ||||||||||
| 53 | "\x{039C}", "\x{039D}", "\x{039E}", "\x{039F}", | ||||||||||
| 54 | "\x{03A0}", "\x{03A1}", "\x{03A3}", | ||||||||||
| 55 | "\x{03A4}", "\x{03A5}", "\x{03A6}", "\x{03A7}", | ||||||||||
| 56 | "\x{03A8}", "\x{03A9}"); | ||||||||||
| 57 | |||||||||||
| 58 | 0 | ▶ | sub radix_alpha { | ||||||||
|
Subroutine
radix_alpha: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 59 | 0 | my ($n) = @_; | |||||||||
| 60 | 0 | return radix_format($n, @letters); } | |||||||||
| 61 | |||||||||||
| 62 | 0 | ▶ | sub radix_Alpha { | ||||||||
|
Subroutine
radix_Alpha: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 63 | 0 | my ($n) = @_; | |||||||||
| 64 | 0 | return radix_format($n, @Letters); } | |||||||||
| 65 | |||||||||||
| 66 | 0 | ▶ | sub radix_greek { | ||||||||
|
Subroutine
radix_greek: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 67 | 0 | my ($n) = @_; | |||||||||
| 68 | 0 | return radix_format($n, @greek); } | |||||||||
| 69 | |||||||||||
| 70 | 0 | ▶ | sub radix_Greek { | ||||||||
|
Subroutine
radix_Greek: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 71 | 0 | my ($n) = @_; | |||||||||
| 72 | 0 | return radix_format($n, @Greek); } | |||||||||
| 73 | |||||||||||
| 74 | # Dumb place for this, but where else... | ||||||||||
| 75 | # Note: This is one "The TeX Way"! (bah!! hint: try a large number) | ||||||||||
| 76 | # namely, it's very limited.... what happened to my much-improved version? | ||||||||||
| 77 | 28 | my @rmletters = ('i', 'v', 'x', 'l', 'c', 'd', 'm'); # [CONSTANT] | |||||||||
| 78 | |||||||||||
| 79 | 0 | ▶ | sub radix_roman { | ||||||||
|
Subroutine
radix_roman: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||
| 80 | 0 | my ($n) = @_; | |||||||||
| 81 | 0 | my $div = 1000; | |||||||||
| 82 | 0 | ▶ | my $s = ($n > $div ? ('m' x int($n / $div)) : ''); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 83 | 0 | my $p = 4; | |||||||||
| 84 | 0 | ▶ | while ($n %= $div) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 85 | 0 | $div /= 10; | |||||||||
| 86 | 0 | my $d = int($n / $div); | |||||||||
| 87 | 0 | ▶ | if ($d % 5 == 4) { $s .= $rmletters[$p]; $d++; } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 88 | 0 | ▶ | if ($d > 4) { $s .= $rmletters[$p + int($d / 5)]; $d %= 5; } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 89 | 0 | ▶ | if ($d) { $s .= $rmletters[$p] x $d; } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 90 | 0 | $p -= 2; } | |||||||||
| 91 | 0 | return $s; } | |||||||||
| 92 | |||||||||||
| 93 | # Convert the number to lower case roman numerals, returning a list of LaTeXML::Core::Token | ||||||||||
| 94 | 0 | ▶ | sub radix_Roman { | ||||||||
|
Subroutine
radix_Roman: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 95 | 0 | my ($n) = @_; | |||||||||
| 96 | 0 | return uc(radix_roman($n)); } | |||||||||
| 97 | |||||||||||
| 98 | #====================================================================== | ||||||||||
| 99 | 28 | 1; | |||||||||
| 100 | |||||||||||
| 101 | __END__ | ||||||||||