yml2/standardlib.ysl2
changeset 52 b4a9a3122abb
equal deleted inserted replaced
22:3a2bd70c01df 52:b4a9a3122abb
       
     1 // YML2 standardlib version 2.5.8
       
     2 
       
     3 function "yml:hex2dec" {
       
     4     param "hex";
       
     5     param "_result", 0;
       
     6     
       
     7     const "hd", "substring($hex, 1, 1)";
       
     8     const "a", """translate($hd, 'ABCDEFabcdef123456789',
       
     9         '123456123456000000000')""";
       
    10 
       
    11     const "d" choose {
       
    12         when "$a>0" value "$a + 9";
       
    13         otherwise value "$hd";
       
    14     }
       
    15 
       
    16     choose {
       
    17         when "string-length($hex) = 1"
       
    18             value "$_result * 16 + $d";
       
    19         otherwise call "yml:hex2dec"
       
    20             with "hex", "substring($hex, 2, 8)", 
       
    21             with "_result", "$_result * 16 + $d";
       
    22     }
       
    23 }
       
    24 
       
    25 function "yml:dec2hex" {
       
    26     param "dec";
       
    27     param "bits", !16**7!;
       
    28 
       
    29     const "v", "floor($dec div $bits)";
       
    30     value "substring('0123456789abcdef', $v + 1, 1)";
       
    31 
       
    32     if "$bits > 1" call "yml:dec2hex"
       
    33         with "dec", "$dec - $bits * $v",
       
    34         with "bits", "$bits div 16";
       
    35 }
       
    36 
       
    37 def "yml:dec2hex" {
       
    38     param "dec";
       
    39     param "digits", 8;
       
    40         
       
    41     result call "yml:dec2hex" with "dec", "$dec", with "bits", "math:power(16, $digits - 1)";
       
    42 }
       
    43 
       
    44 def "yml:hex2dec" {
       
    45     param "hex";
       
    46     result call "yml:hex2dec" with "hex", "$hex";
       
    47 }
       
    48 
       
    49 def "yml:lcase" {
       
    50     param "text", "''";
       
    51     result "translate($text, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz')";
       
    52 }
       
    53 
       
    54 def "yml:ucase" {
       
    55     param "text", "''";
       
    56     result "translate($text, 'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')";
       
    57 }
       
    58 
       
    59 def "yml:mixedCase" {
       
    60     param "text", "''";
       
    61     result "concat(yml:lcase(substring($text,1,1)),substring($text,2))";
       
    62 }
       
    63 
       
    64 def "yml:capit" {
       
    65     param "text", "''";
       
    66     result "concat(yml:ucase(substring($text,1,1)),substring($text,2))";
       
    67 }
       
    68