standardlib.ysl2
author Volker Birk <vb@pep-project.org>
Mon, 11 Jul 2016 23:15:28 +0200
changeset 0 76005e62091d
child 10 519fb60037ce
permissions -rw-r--r--
initial commit
0
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
     1
function "yml:hex2dec" {
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
     2
    param "hex";
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
     3
    param "_result", 0;
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
     4
    
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
     5
    const "hd", "substring($hex, 1, 1)";
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
     6
    const "a", """translate($hd, 'ABCDEFabcdef123456789',
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
     7
        '123456123456000000000')""";
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
     8
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
     9
    const "d" choose {
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    10
        when "$a>0" value "$a + 9";
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    11
        otherwise value "$hd";
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    12
    }
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    13
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    14
    choose {
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    15
        when "string-length($hex) = 1"
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    16
            value "$_result * 16 + $d";
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    17
        otherwise call "yml:hex2dec"
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    18
            with "hex", "substring($hex, 2, 8)", 
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    19
            with "_result", "$_result * 16 + $d";
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    20
    }
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    21
}
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    22
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    23
function "yml:dec2hex" {
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    24
    param "dec";
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    25
    param "bits", !16**7!;
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    26
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    27
    const "v", "floor($dec div $bits)";
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    28
    value "substring('0123456789abcdef', $v + 1, 1)";
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    29
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    30
    if "$bits > 1" call "yml:dec2hex"
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    31
        with "dec", "$dec - $bits * $v",
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    32
        with "bits", "$bits div 16";
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    33
}
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    34
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    35
def "yml:dec2hex" {
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    36
    param "dec";
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    37
    param "digits", 8;
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    38
        
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    39
    result call "yml:dec2hex" with "dec", "$dec", with "bits", "math:power(16, $digits - 1)";
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    40
}
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    41
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    42
def "yml:hex2dec" {
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    43
    param "hex";
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    44
    result call "yml:hex2dec" with "hex", "$hex";
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    45
}
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    46
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    47
def "yml:lcase" {
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    48
    param "text", "''";
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    49
    result "translate($text, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz')";
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    50
}
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    51
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    52
def "yml:ucase" {
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    53
    param "text", "''";
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    54
    result "translate($text, 'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')";
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    55
}