standardlib.ysl2
author Volker Birk <vb@pep.foundation>
Thu, 26 Apr 2018 19:26:19 +0200
changeset 19 8903f9e692b9
parent 17 3bc118c7d324
child 20 c936066cff62
permissions -rw-r--r--
Added tag 2.5.7 for changeset b4343a05f5d5
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
}
10
519fb60037ce adding mixedCase()
Volker Birk <vb@pep.foundation>
parents: 0
diff changeset
    56
519fb60037ce adding mixedCase()
Volker Birk <vb@pep.foundation>
parents: 0
diff changeset
    57
def "yml:mixedCase" {
519fb60037ce adding mixedCase()
Volker Birk <vb@pep.foundation>
parents: 0
diff changeset
    58
    param "text", "''";
519fb60037ce adding mixedCase()
Volker Birk <vb@pep.foundation>
parents: 0
diff changeset
    59
    result "concat(yml:lcase(substring($text,1,1)),substring($text,2))";
519fb60037ce adding mixedCase()
Volker Birk <vb@pep.foundation>
parents: 0
diff changeset
    60
}
519fb60037ce adding mixedCase()
Volker Birk <vb@pep.foundation>
parents: 0
diff changeset
    61
17
3bc118c7d324 on special demand
Volker Birk <vb@pep.foundation>
parents: 10
diff changeset
    62
def "yml:capit" {
3bc118c7d324 on special demand
Volker Birk <vb@pep.foundation>
parents: 10
diff changeset
    63
    param "text", "''";
3bc118c7d324 on special demand
Volker Birk <vb@pep.foundation>
parents: 10
diff changeset
    64
    result "concat(yml:ucase(substring($text,1,1)),substring($text,2))";
3bc118c7d324 on special demand
Volker Birk <vb@pep.foundation>
parents: 10
diff changeset
    65
}
3bc118c7d324 on special demand
Volker Birk <vb@pep.foundation>
parents: 10
diff changeset
    66