User:Jr/assorted
< User:Jr
Jump to navigation
Jump to search
this page has been created to house stand-alone macros, for now, I reserve the right :-) to add "how to" snippets and such.
macros
LatinYear()
this macro started life as several in a include/header file, but, with some help, it now is "stand-alone", and just 40-odd lines of code.
#macro LatinYear(year_)
#if (int(year_) != year_)
#error "oops, bad 'year'."
#elseif (year_ < 1 | 3999 < year_)
#error "oops, out-of-range 'year'."
#end
#macro __cvt(dgt_, s1_, s5_, s10_)
#local units_ = concat(s1_,s1_,s1_);
#local n_ = val(dgt_);
#switch (n_)
#case(0) #local s_ = ""; #break
#range(1,3) #local s_ = substr(units_,1,n_); #break
#case(4) #local s_ = concat(s1_,s5_); #break
#case(5) #local s_ = s5_; #break
#range(6,8) #local s_ = concat(s5_,substr(units_,1,n_-5)); #break
#case(9) #local s_ = concat(s1_,s10_); #break
#else #error "oops, \"cannot happen\" error in '__cvt()'."
#end
s_
#end
#local ystr_ = str(year_,0,0);
#switch (strlen(ystr_))
#case(4)
#local s_ = concat(substr("MMM",1,val(substr(ystr_,1,1))),
__cvt(substr(ystr_,2,1),"C","D","M"),
__cvt(substr(ystr_,3,1),"X","L","C"),
__cvt(substr(ystr_,4,1),"I","V","X"));
#break
#case(3)
#local s_ = concat(__cvt(substr(ystr_,1,1),"C","D","M"),
__cvt(substr(ystr_,2,1),"X","L","C"),
__cvt(substr(ystr_,3,1),"I","V","X"));
#break
#case(2)
#local s_ = concat(__cvt(substr(ystr_,1,1),"X","L","C"),
__cvt(substr(ystr_,2,1),"I","V","X"));
#break
#case(1)
#local s_ = __cvt(ystr_,"I","V","X");
#break
#else #error "oops, \"cannot happen\" error in 'LatinYear()'."
#end
s_
#end
a copy'n'paste set of calls to try it out:
#debug concat("2000 ",LatinYear(2000),".\n")
#debug concat("1984 ",LatinYear(1984),".\n")
#debug concat("2026 ",LatinYear(2026),".\n")
#debug concat(" 26 ",LatinYear(26),".\n")
#debug concat(" 4 ",LatinYear(4),".\n")
#debug concat("3999 ",LatinYear(3999),".\n")
which shows:
2000 MM. 1984 MCMLXXXIV. 2026 MMXXVI. 26 XXVI. 4 IV. 3999 MMMCMXCIX.
(fingers crossed :-))