standardlib.ysl2
author Volker Birk <vb@pep-project.org>
Fri, 15 Feb 2019 10:45:50 +0100
changeset 29 6a8a7951d8e6
parent 20 c936066cff62
permissions -rw-r--r--
feature: default function is used as a template
// YML2 standardlib version 2.5.8

function "yml:hex2dec" {
    param "hex";
    param "_result", 0;
    
    const "hd", "substring($hex, 1, 1)";
    const "a", """translate($hd, 'ABCDEFabcdef123456789',
        '123456123456000000000')""";

    const "d" choose {
        when "$a>0" value "$a + 9";
        otherwise value "$hd";
    }

    choose {
        when "string-length($hex) = 1"
            value "$_result * 16 + $d";
        otherwise call "yml:hex2dec"
            with "hex", "substring($hex, 2, 8)", 
            with "_result", "$_result * 16 + $d";
    }
}

function "yml:dec2hex" {
    param "dec";
    param "bits", !16**7!;

    const "v", "floor($dec div $bits)";
    value "substring('0123456789abcdef', $v + 1, 1)";

    if "$bits > 1" call "yml:dec2hex"
        with "dec", "$dec - $bits * $v",
        with "bits", "$bits div 16";
}

def "yml:dec2hex" {
    param "dec";
    param "digits", 8;
        
    result call "yml:dec2hex" with "dec", "$dec", with "bits", "math:power(16, $digits - 1)";
}

def "yml:hex2dec" {
    param "hex";
    result call "yml:hex2dec" with "hex", "$hex";
}

def "yml:lcase" {
    param "text", "''";
    result "translate($text, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz')";
}

def "yml:ucase" {
    param "text", "''";
    result "translate($text, 'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')";
}

def "yml:mixedCase" {
    param "text", "''";
    result "concat(yml:lcase(substring($text,1,1)),substring($text,2))";
}

def "yml:capit" {
    param "text", "''";
    result "concat(yml:ucase(substring($text,1,1)),substring($text,2))";
}