Topic
No replies
HermannSW
HermannSW
4128 Posts
ACCEPTED ANSWER

Pinned topic Arrows in function names: func:b64⇉hex() and func:bin┈⇢dec()

‏2011-10-25T22:20:36Z |
It is clear that function names may contain arbitrary unicode characters.
But today is the first time I made use of arrows in function names.

Normally I had names like "hex2base64" meaning conversion from "hex" "to" "base64".
But the inverse function name "base642hex" is ugly.

In the functions below I used '⇉' for conversions preserving leading '0's.
The characters '┈⇢' are used for conversions not preserving leading '0's.

This is how it looks like:
select="func:b64⇉hex($b64)"
select="func:bin┈⇢dec('01101001')"

Besides arrows
http://www.unicode.org/charts/PDF/U2190.pdf#page=2

there is also "Box Drawing"
http://www.unicode.org/charts/PDF/U2500.pdf#page=2

as well as "Block Elements"
http://www.unicode.org/charts/PDF/U2580.pdf

which can at least be used for diagrams in comments.

The "Terminal Graphic Characters" from "Block Elements" remind me on the graphics on
my very first computer back in 1982 -- the ZX81.
It had a 32x24 character display, and the "Terminal Graphic Characters" allowed for "64x48" graphics display ;-)

Processing on bin strings becomes possible by func:hex⇉bin() and func:bin⇉hex().
The XPath expressions making use of bidirectional mapping $hexbin are very compact.

"Nice" formatted output is done by func:display($bin,8,'bin').

This is the output of the small sample application, lookup preserving.xsl below for the details:

$ od -tx1 0est 0000000 00 65 73 74 0a 0000005 $ $ coproc2 preserving.xsl 0est http:
//dp3-l3:2224 b64 = AGVz dAo= hex = 006573 740A bin = 00000000 01100101 01110011 01110100 00001010 105 $

preserving.xsl (also attached):

<!DOCTYPE xsl:stylesheet [ <!ENTITY LF 
"<xsl:text>
</xsl:text>"> ]> <xsl:stylesheet version=
"1.0" xmlns:xsl=
"http://www.w3.org/1999/XSL/Transform" xmlns:func=
"http://exslt.org/functions" xmlns:str=
"http://exslt.org/strings" xmlns:dp=
"http://www.datapower.com/extensions" extension-element-prefixes=
"dp" exclude-result-prefixes=
"func" > <dp:input-mapping type=
"ffd" href=
"store:///pkcs7-convert-input.ffd"/>   <xsl:output omit-xml-declaration=
"yes" />   <xsl:template match=
"/"> <xsl:variable name=
"b64" select=
"dp:binary-encode(/object/message/node())"/>   <xsl:variable name=
"hex" select=
"func:b64⇉hex($b64)"/>   <xsl:variable name=
"bin" select=
"func:hex⇉bin($hex)"/>   <xsl:variable name=
"dec" select=
"func:bin┈⇢dec('01101001')"/>   <xsl:value-of select=
"func:display($b64,4,'b64')"/>&LF; <xsl:value-of select=
"func:display($hex,6,'hex')"/>&LF; <xsl:value-of select=
"func:display($bin,8,'bin')"/>&LF; <xsl:value-of select=
"$dec"/>&LF; </xsl:template>     <!-- leading 
'0' preserving dp:radix-convert(_,64,16) --> <func:function name=
"func:b64⇉hex"> <xsl:param name=
"b64"/>   <func:result select=
"substring(dp:radix-convert(concat('8AAA',$b64),64,16),7)"/> </func:function>     <!-- leading 
'0' preserving dp:radix-convert(_,16,64) --> <func:function name=
"func:hex⇉b64"> <xsl:param name=
"hex"/>   <func:result select=
"substring(dp:radix-convert(concat('F00000',$hex),16,64),5)"/> </func:function>     <!-- 

for bidirectional mapping --> <xsl:variable name=
"hexbin"> <x d=
'0'>0000</x><x d=
'1'>0001</x><x d=
'2'>0010</x><x d=
'3'>0011</x> <x d=
'4'>0100</x><x d=
'5'>0101</x><x d=
'6'>0110</x><x d=
'7'>0111</x> <x d=
'8'>1000</x><x d=
'9'>1001</x><x d=
'A'>1010</x><x d=
'B'>1011</x> <x d=
'C'>1100</x><x d=
'D'>1101</x><x d=
'E'>1110</x><x d=
'F'>1111</x> </xsl:variable>     <!-- leading 
'0' preserving (non-existant) dp:radix-convert(_,2,16) --> <func:function name=
"func:bin⇉hex"> <xsl:param name=
"str"/>   <xsl:variable name=
"len" select=
"string-length($str)"/>   <xsl:choose> <xsl:when test=
"not($str)"/>   <xsl:when test=
"$len mod 4"> <func:result select=
"func:bin⇉hex(concat(substring('000',$len mod 4),$str))"/> </xsl:when>   <xsl:otherwise> <func:result select=
"concat( $hexbin/x[starts-with($str,.)]/@d, func:bin⇉hex(substring($str,5)) )
" /> </xsl:otherwise> </xsl:choose> </func:function>     <!-- leading 
'0' preserving (non-existant) dp:radix-convert(_,16,2) --> <func:function name=
"func:hex⇉bin"> <xsl:param name=
"str"/>   <xsl:choose> <xsl:when test=
"not($str)"/>   <xsl:otherwise> <func:result select=
"concat( $hexbin/x[starts-with($str,@d)], func:hex⇉bin(substring($str,2)) )
" /> </xsl:otherwise> </xsl:choose> </func:function>     <!-- non-existant dp:radix-convert(_,2,10) --> <func:function name=
"func:bin┈⇢dec"> <xsl:param name=
"str"/>   <xsl:choose> <xsl:when test=
"not($str)"> <func:result select=
"0"/> </xsl:when> <xsl:otherwise> <func:result select=
"2*func:bin┈⇢dec(substring($str,1,string-length($str)-1)) + substring($str,string-length($str),1)
" /> </xsl:otherwise> </xsl:choose> </func:function>     <!-- xor of binary strings $a and $b --> <func:function name=
"func:xorBin"> <xsl:param name=
"a"/> <xsl:param name=
"b"/>   <xsl:choose> <xsl:when test=
"not($a)"/>   <xsl:otherwise> <func:result select=
"concat( number(substring($a,1,1)!=substring($b,1,1)), func:xorBin(substring($a,2),substring($b,2)) )
" /> </xsl:otherwise> </xsl:choose> </func:function>     <!-- cyclic 
"shift left" of $str by $cnt positions --> <func:function name=
"func:shiftLeft"> <xsl:param name=
"str"/> <xsl:param name=
"cnt"/>   <func:result select=
"concat(substring($str,1+$cnt),substring($str,1,$cnt))"/> </func:function>     <!-- auxiliary function 

for nice output str: string to output (hex, bin, ...) blk: length of a block (after which a space is placed) lab: label 

if present, 

for output 
"lab=..." --> <func:function name=
"func:display"> <xsl:param name=
"str"/> <xsl:param name=
"blk"/> <xsl:param name=
"lab"/>   <xsl:choose> <xsl:when test=
"$lab"> <func:result select=
"concat( str:align($lab,
'     ',
'right'), 
' = ', func:display($str,$blk) )
" /> </xsl:when>   <xsl:when test=
"not($str)"/>   <xsl:otherwise> <func:result select=
"concat( substring($str,1,$blk), 
' ', func:display(substring($str,1+$blk),$blk) )
" /> </xsl:otherwise> </xsl:choose> </func:function>     </xsl:stylesheet>


Hermann<myXsltBlog/>