/[PAMELA software]/quicklook/dataToXML/Data/numberutils_lib.xsl
ViewVC logotype

Contents of /quicklook/dataToXML/Data/numberutils_lib.xsl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1 - (show annotations) (download) (as text)
Tue Apr 25 09:00:20 2006 UTC (18 years, 7 months ago) by kusanagi
Branch point for: MAIN
File MIME type: application/xml
Initial revision

1 <?xml version="1.0"?>
2 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
3 <!--
4 ==========================================================================
5 Stylesheet: numberutils_lib.xsl
6 ========================================================================== -->
7
8 <!-- ========================================================= -->
9 <!-- Function: Bin2Dec(<value>) => Decimal value -->
10 <!-- Parameters:- -->
11 <!-- <value> - the binary string to be converted to decimal -->
12 <xsl:template name="Bin2Dec">
13 <xsl:param name="value" select="'0'"/>
14 <!-- the following paremeters are used only during recursion -->
15 <xsl:param name="bin-power" select="number(1)"/>
16 <xsl:param name="accum" select="number(0)"/>
17 <!-- isolate last binary digit -->
18 <xsl:variable name="bin-digit" select="substring($value,string-length($value),1)"/>
19 <!-- check that binary digit is valid -->
20 <xsl:choose>
21 <xsl:when test="not(contains('01',$bin-digit))">
22 <!-- not a binary digit! -->
23 <xsl:text>NaN</xsl:text>
24 </xsl:when>
25 <xsl:when test="string-length($bin-digit) = 0">
26 <!-- unexpected end of hex string -->
27 <xsl:text>0</xsl:text>
28 </xsl:when>
29 <xsl:otherwise>
30 <!-- OK so far -->
31 <xsl:variable name="remainder" select="substring($value,1,string-length($value)-1)"/>
32 <xsl:variable name="this-digit-value" select="number($bin-digit) * $bin-power"/>
33 <!-- determine whether this is the end of the hex string -->
34 <xsl:choose>
35 <xsl:when test="string-length($remainder) = 0">
36 <!-- end - output final result -->
37 <xsl:value-of select="$accum + $this-digit-value"/>
38 </xsl:when>
39 <xsl:otherwise>
40 <!-- recurse to self for next digit -->
41 <xsl:call-template name="Bin2Dec">
42 <xsl:with-param name="value" select="$remainder"/>
43 <xsl:with-param name="bin-power" select="$bin-power * 2"/>
44 <xsl:with-param name="accum" select="$accum + $this-digit-value"/>
45 </xsl:call-template>
46 </xsl:otherwise>
47 </xsl:choose>
48 </xsl:otherwise>
49 </xsl:choose>
50 </xsl:template>
51
52 <!-- ======================================================== -->
53 <!-- Function: Hex2Dec(<value>) => Decimal value -->
54 <!-- Parameters:- -->
55 <!-- <value> - the hex string to be converted to decimal -->
56 <!-- (case of hex string is unimportant) -->
57 <xsl:template name="Hex2Dec">
58 <xsl:param name="value" select="'0'"/>
59 <!-- the following paremeters are used only during recursion -->
60 <xsl:param name="hex-power" select="number(1)"/>
61 <xsl:param name="accum" select="number(0)"/>
62 <!-- isolate last hex digit (and convert it to upper case) -->
63 <xsl:variable name="hex-digit" select="translate(substring($value,string-length($value),1),'abcdef','ABCDEF')"/>
64 <!-- check that hex digit is valid -->
65 <xsl:choose>
66 <xsl:when test="not(contains('0123456789ABCDEF',$hex-digit))">
67 <!-- not a hex digit! -->
68 <xsl:text>NaN</xsl:text>
69 </xsl:when>
70 <xsl:when test="string-length($hex-digit) = 0">
71 <!-- unexpected end of hex string -->
72 <xsl:text>0</xsl:text>
73 </xsl:when>
74 <xsl:otherwise>
75 <!-- OK so far -->
76 <xsl:variable name="remainder" select="substring($value,1,string-length($value)-1)"/>
77 <xsl:variable name="this-digit-value" select="string-length(substring-before('0123456789ABCDEF',$hex-digit)) * $hex-power"/>
78 <!-- determine whether this is the end of the hex string -->
79 <xsl:choose>
80 <xsl:when test="string-length($remainder) = 0">
81 <!-- end - output final result -->
82 <xsl:value-of select="$accum + $this-digit-value"/>
83 </xsl:when>
84 <xsl:otherwise>
85 <!-- recurse to self for next digit -->
86 <xsl:call-template name="Hex2Dec">
87 <xsl:with-param name="value" select="$remainder"/>
88 <xsl:with-param name="hex-power" select="$hex-power * 16"/>
89 <xsl:with-param name="accum" select="$accum + $this-digit-value"/>
90 </xsl:call-template>
91 </xsl:otherwise>
92 </xsl:choose>
93 </xsl:otherwise>
94 </xsl:choose>
95 </xsl:template>
96
97 <!-- ===================================================== -->
98 <!-- Function: Dec2Hex(<value>[,<digits>]) => Hex string -->
99 <!-- Parameters:- -->
100 <!-- <value> - the decimal value to be converted to hex -->
101 <!-- (must be a positive) -->
102 <!-- <digits> - the number of hex digits required -->
103 <!-- If this parameter is omitted then the -->
104 <!-- hex string returned is as long as reqd. -->
105 <!-- If the number of digits required exceeds -->
106 <!-- the value specified by this parameter -->
107 <!-- then the hex string is as long as reqd. -->
108 <xsl:template name="Dec2Hex">
109 <xsl:param name="value" select="number(0)"/>
110 <xsl:param name="digits" select="number(-1)"/>
111 <!-- the following paremeters are used only during recursion -->
112 <xsl:param name="hex" select="number(268435456)"/>
113 <xsl:param name="hex-power" select="number(28)"/>
114 <xsl:param name="nonzero-encounters" select="false()"/>
115 <!-- calculate the left over value to be passed to next recursion -->
116 <xsl:variable name="remainder" select="floor($value) mod $hex"/>
117 <!-- calculate the value of this nybble (this hex digit) -->
118 <xsl:variable name="this-nybble" select="(floor($value) - $remainder) div ($hex)"/>
119 <!-- determine whether a non-zero digit has been encountered yet -->
120 <xsl:variable name="nonzero-encountered" select="boolean($nonzero-encounters or ($this-nybble &gt; 0))"/>
121 <!-- only output hex digit if:- -->
122 <!-- non-zero has already been encountered OR -->
123 <!-- on the last digit OR -->
124 <!-- the number of required digits says so -->
125 <xsl:if test="$nonzero-encountered or ($hex-power = 0) or ((($hex-power div 4) + 1) &lt;= $digits)">
126 <xsl:value-of select="substring('0123456789ABCDEF',($this-nybble)+1,1)"/>
127 </xsl:if>
128 <!-- recursive call until all digits have been dealt with -->
129 <xsl:if test="$hex-power &gt; 0">
130 <xsl:call-template name="Dec2Hex">
131 <xsl:with-param name="value" select="$remainder"/>
132 <xsl:with-param name="hex" select="$hex div 16"/>
133 <xsl:with-param name="hex-power" select="$hex-power - 4"/>
134 <xsl:with-param name="digits" select="$digits"/>
135 <xsl:with-param name="nonzero-encounters" select="$nonzero-encountered"/>
136 </xsl:call-template>
137 </xsl:if>
138 </xsl:template>
139
140 <!-- ======================================================== -->
141 <!-- Function: Dec2Bin(<value>) => Binary string -->
142 <!-- Parameters:- -->
143 <!-- <value> - the decimal value to be converted to binary -->
144 <!-- (must be a positive) -->
145 <xsl:template name="Dec2Bin">
146 <xsl:param name="value" select="number(0)"/>
147 <!-- the following paremeters are used only during recursion -->
148 <xsl:param name="bin" select="number(2147483648)"/>
149 <xsl:param name="bin-power" select="number(31)"/>
150 <xsl:param name="one-encounters" select="false()"/>
151 <!-- calculate the left over value to be passed to next recursion -->
152 <xsl:variable name="remainder" select="$value mod $bin"/>
153 <!-- calculate the value of this bit (this binary digit) -->
154 <xsl:variable name="this-bit" select="$value - $remainder"/>
155 <!-- determine whether a non-zero digit has been encountered yet -->
156 <xsl:variable name="one-encountered" select="boolean($one-encounters or ($this-bit &gt; 0))"/>
157 <!-- only output digit if: -->
158 <!-- non-zero has already been encountered OR -->
159 <!-- on the last digit -->
160 <xsl:if test="$one-encountered or ($bin-power = 0)">
161 <xsl:value-of select="substring('01',($this-bit div $bin)+1,1)"/>
162 </xsl:if>
163 <!-- recursive call until all digits have been dealt with -->
164 <xsl:if test="$bin-power &gt; 0">
165 <xsl:call-template name="Dec2Bin">
166 <xsl:with-param name="value" select="$remainder"/>
167 <xsl:with-param name="bin" select="$bin div 2"/>
168 <xsl:with-param name="bin-power" select="$bin-power - 1"/>
169 <xsl:with-param name="one-encounters" select="$one-encountered"/>
170 </xsl:call-template>
171 </xsl:if>
172 </xsl:template>
173
174 <!-- ======================================================== -->
175 <!-- Function: BinAND(<bin1>,<bin2>) => Binary string -->
176 <!-- Parameters:- -->
177 <!-- <bin1> - the first binary string number to be ANDed -->
178 <!-- <bin2> - the second binary string number to be ANDed -->
179 <xsl:template name="BinAND">
180 <xsl:param name="bin1" select="'0'"/>
181 <xsl:param name="bin2" select="'0'"/>
182 <!-- param used for recursion iteration -->
183 <xsl:param name="i" select="number(1)"/>
184 <xsl:variable name="max-len">
185 <xsl:choose>
186 <xsl:when test="string-length($bin1) &gt; string-length($bin2)"><xsl:value-of select="string-length($bin1)"/></xsl:when>
187 <xsl:otherwise><xsl:value-of select="string-length($bin2)"/></xsl:otherwise>
188 </xsl:choose>
189 </xsl:variable>
190 <xsl:variable name="sbin1" select="substring(concat(substring('00000000000000000000000000000000',1,$max-len - string-length($bin1)),$bin1),$i,1)"/>
191 <xsl:variable name="sbin2" select="substring(concat(substring('00000000000000000000000000000000',1,$max-len - string-length($bin2)),$bin2),$i,1)"/>
192 <xsl:choose>
193 <xsl:when test="$sbin1 = '1' and $sbin2 = '1'"><xsl:text>1</xsl:text></xsl:when>
194 <xsl:otherwise><xsl:text>0</xsl:text></xsl:otherwise>
195 </xsl:choose>
196 <xsl:if test="$i &lt; $max-len">
197 <xsl:call-template name="BinAND">
198 <xsl:with-param name="bin1" select="$bin1"/>
199 <xsl:with-param name="bin2" select="$bin2"/>
200 <xsl:with-param name="i" select="$i + 1"/>
201 </xsl:call-template>
202 </xsl:if>
203 </xsl:template>
204
205 <!-- ======================================================== -->
206 <!-- Function: BinOR(<bin1>,<bin2>) => Binary string -->
207 <!-- Parameters:- -->
208 <!-- <bin1> - the first binary string number to be ORed -->
209 <!-- <bin2> - the second binary string number to be ORed -->
210 <xsl:template name="BinOR">
211 <xsl:param name="bin1" select="'0'"/>
212 <xsl:param name="bin2" select="'0'"/>
213 <!-- param used for recursion iteration -->
214 <xsl:param name="i" select="number(1)"/>
215 <xsl:variable name="max-len">
216 <xsl:choose>
217 <xsl:when test="string-length($bin1) &gt; string-length($bin2)"><xsl:value-of select="string-length($bin1)"/></xsl:when>
218 <xsl:otherwise><xsl:value-of select="string-length($bin2)"/></xsl:otherwise>
219 </xsl:choose>
220 </xsl:variable>
221 <xsl:variable name="sbin1" select="substring(concat(substring('00000000000000000000000000000000',1,$max-len - string-length($bin1)),$bin1),$i,1)"/>
222 <xsl:variable name="sbin2" select="substring(concat(substring('00000000000000000000000000000000',1,$max-len - string-length($bin2)),$bin2),$i,1)"/>
223 <xsl:choose>
224 <xsl:when test="$sbin1 = '1' or $sbin2 = '1'"><xsl:text>1</xsl:text></xsl:when>
225 <xsl:otherwise><xsl:text>0</xsl:text></xsl:otherwise>
226 </xsl:choose>
227 <xsl:if test="$i &lt; $max-len">
228 <xsl:call-template name="BinOR">
229 <xsl:with-param name="bin1" select="$bin1"/>
230 <xsl:with-param name="bin2" select="$bin2"/>
231 <xsl:with-param name="i" select="$i + 1"/>
232 </xsl:call-template>
233 </xsl:if>
234 </xsl:template>
235
236 <!-- ======================================================== -->
237 <!-- Function: BinXOR(<bin1>,<bin2>) => Binary string -->
238 <!-- Parameters:- -->
239 <!-- <bin1> - the first binary string number to be XORed -->
240 <!-- <bin2> - the second binary string number to be XORed -->
241 <xsl:template name="BinXOR">
242 <xsl:param name="bin1" select="'0'"/>
243 <xsl:param name="bin2" select="'0'"/>
244 <!-- param used for recursion iteration -->
245 <xsl:param name="i" select="number(1)"/>
246 <xsl:variable name="max-len">
247 <xsl:choose>
248 <xsl:when test="string-length($bin1) &gt; string-length($bin2)"><xsl:value-of select="string-length($bin1)"/></xsl:when>
249 <xsl:otherwise><xsl:value-of select="string-length($bin2)"/></xsl:otherwise>
250 </xsl:choose>
251 </xsl:variable>
252 <xsl:variable name="sbin1" select="substring(concat(substring('00000000000000000000000000000000',1,$max-len - string-length($bin1)),$bin1),$i,1)"/>
253 <xsl:variable name="sbin2" select="substring(concat(substring('00000000000000000000000000000000',1,$max-len - string-length($bin2)),$bin2),$i,1)"/>
254 <xsl:choose>
255 <xsl:when test="$sbin1 = '1' and $sbin2 = '1'"><xsl:text>0</xsl:text></xsl:when>
256 <xsl:when test="$sbin1 = '1' or $sbin2 = '1'"><xsl:text>1</xsl:text></xsl:when>
257 <xsl:otherwise><xsl:text>0</xsl:text></xsl:otherwise>
258 </xsl:choose>
259 <xsl:if test="$i &lt; $max-len">
260 <xsl:call-template name="BinXOR">
261 <xsl:with-param name="bin1" select="$bin1"/>
262 <xsl:with-param name="bin2" select="$bin2"/>
263 <xsl:with-param name="i" select="$i + 1"/>
264 </xsl:call-template>
265 </xsl:if>
266 </xsl:template>
267
268 <!-- ======================================================== -->
269 <!-- Function: BinNOT(<bin>) => Binary string -->
270 <!-- Parameters:- -->
271 <!-- <bin> - the binary string number to be NOTed -->
272 <xsl:template name="BinNOT">
273 <xsl:param name="bin" select="'0'"/>
274 <xsl:param name="max-bits" select="number(32)"/>
275 <xsl:variable name="not1" select="translate($bin,'01','10')"/>
276 <xsl:value-of select="concat(substring('11111111111111111111111111111111',1,$max-bits - string-length($not1)),$not1)"/>
277 </xsl:template>
278
279 <!-- ======================================================== -->
280 <!-- Function: BooleanOR(<value1>,<value2>) => number -->
281 <!-- Parameters:- -->
282 <!-- <value1> - the first number to be ORed -->
283 <!-- <value2> - the second number to be ORed -->
284 <!-- NB. Only works with positive numbers! -->
285 <xsl:template name="BooleanOR">
286 <xsl:param name="value1" select="number(0)"/>
287 <xsl:param name="value2" select="number(0)"/>
288 <!-- recurse parameters -->
289 <xsl:param name="bitval" select="number(2147483648)"/>
290 <xsl:param name="accum" select="number(0)"/>
291 <!-- calc bits present on values -->
292 <xsl:variable name="bit1" select="floor($value1 div $bitval)"/>
293 <xsl:variable name="bit2" select="floor($value2 div $bitval)"/>
294 <!-- do the OR on the bits -->
295 <xsl:variable name="thisbit">
296 <xsl:choose>
297 <xsl:when test="($bit1 != 0) or ($bit2 != 0)"><xsl:value-of select="$bitval"/></xsl:when>
298 <xsl:otherwise>0</xsl:otherwise>
299 </xsl:choose>
300 </xsl:variable>
301 <!-- if last recurse then output the value -->
302 <xsl:choose>
303 <xsl:when test="$bitval = 1"><xsl:value-of select="$accum + $thisbit"/></xsl:when>
304 <xsl:otherwise>
305 <!-- recurse required -->
306 <xsl:call-template name="BooleanOR">
307 <xsl:with-param name="value1" select="$value1 mod $bitval"/>
308 <xsl:with-param name="value2" select="$value2 mod $bitval"/>
309 <xsl:with-param name="bitval" select="$bitval div 2"/>
310 <xsl:with-param name="accum" select="$accum + $thisbit"/>
311 </xsl:call-template>
312 </xsl:otherwise>
313 </xsl:choose>
314 </xsl:template>
315
316 <!-- ======================================================== -->
317 <!-- Function: BooleanAND(<value1>,<value2>) => number -->
318 <!-- Parameters:- -->
319 <!-- <value1> - the first number to be ANDed -->
320 <!-- <value2> - the second number to be ANDed -->
321 <!-- NB. Only works with positive numbers! -->
322 <xsl:template name="BooleanAND">
323 <xsl:param name="value1" select="number(0)"/>
324 <xsl:param name="value2" select="number(0)"/>
325 <!-- recurse parameters -->
326 <xsl:param name="bitval" select="number(2147483648)"/>
327 <xsl:param name="accum" select="number(0)"/>
328 <!-- calc bits present on values -->
329 <xsl:variable name="bit1" select="floor($value1 div $bitval)"/>
330 <xsl:variable name="bit2" select="floor($value2 div $bitval)"/>
331 <!-- do the OR on the bits -->
332 <xsl:variable name="thisbit">
333 <xsl:choose>
334 <xsl:when test="($bit1 != 0) and ($bit2 != 0)"><xsl:value-of select="$bitval"/></xsl:when>
335 <xsl:otherwise>0</xsl:otherwise>
336 </xsl:choose>
337 </xsl:variable>
338 <!-- if last recurse then output the value -->
339 <xsl:choose>
340 <xsl:when test="$bitval = 1"><xsl:value-of select="$accum + $thisbit"/></xsl:when>
341 <xsl:otherwise>
342 <!-- recurse required -->
343 <xsl:call-template name="BooleanAND">
344 <xsl:with-param name="value1" select="$value1 mod $bitval"/>
345 <xsl:with-param name="value2" select="$value2 mod $bitval"/>
346 <xsl:with-param name="bitval" select="$bitval div 2"/>
347 <xsl:with-param name="accum" select="$accum + $thisbit"/>
348 </xsl:call-template>
349 </xsl:otherwise>
350 </xsl:choose>
351 </xsl:template>
352
353 <!-- ======================================================== -->
354 <!-- Function: BooleanXOR(<value1>,<value2>) => number -->
355 <!-- Parameters:- -->
356 <!-- <value1> - the first number to be XORed -->
357 <!-- <value2> - the second number to be XORed -->
358 <!-- NB. Only works with positive numbers! -->
359 <xsl:template name="BooleanXOR">
360 <xsl:param name="value1" select="number(0)"/>
361 <xsl:param name="value2" select="number(0)"/>
362 <!-- recurse parameters -->
363 <xsl:param name="bitval" select="number(2147483648)"/>
364 <xsl:param name="accum" select="number(0)"/>
365 <!-- calc bits present on values -->
366 <xsl:variable name="bit1" select="floor($value1 div $bitval)"/>
367 <xsl:variable name="bit2" select="floor($value2 div $bitval)"/>
368 <!-- do the XOR on the bits -->
369 <xsl:variable name="thisbit">
370 <xsl:choose>
371 <xsl:when test="(($bit1 != 0) and ($bit2 = 0)) or (($bit1 = 0) and ($bit2 != 0))"><xsl:value-of select="$bitval"/></xsl:when>
372 <xsl:otherwise>0</xsl:otherwise>
373 </xsl:choose>
374 </xsl:variable>
375 <!-- if last recurse then output the value -->
376 <xsl:choose>
377 <xsl:when test="$bitval = 1"><xsl:value-of select="$accum + $thisbit"/></xsl:when>
378 <xsl:otherwise>
379 <!-- recurse required -->
380 <xsl:call-template name="BooleanXOR">
381 <xsl:with-param name="value1" select="$value1 mod $bitval"/>
382 <xsl:with-param name="value2" select="$value2 mod $bitval"/>
383 <xsl:with-param name="bitval" select="$bitval div 2"/>
384 <xsl:with-param name="accum" select="$accum + $thisbit"/>
385 </xsl:call-template>
386 </xsl:otherwise>
387 </xsl:choose>
388 </xsl:template>
389
390 </xsl:stylesheet>

  ViewVC Help
Powered by ViewVC 1.1.23