Lichen

Change of templates/native/unicode.c

608:1053537493ff
templates/native/unicode.c method-wrapper-for-context
     1.1 --- a/templates/native/unicode.c	Mon Feb 20 12:59:34 2017 +0100
     1.2 +++ b/templates/native/unicode.c	Mon Feb 20 18:50:16 2017 +0100
     1.3 @@ -1,6 +1,6 @@
     1.4  /* Native functions for Unicode operations.
     1.5  
     1.6 -Copyright (C) 2016 Paul Boddie <paul@boddie.org.uk>
     1.7 +Copyright (C) 2016, 2017 Paul Boddie <paul@boddie.org.uk>
     1.8  
     1.9  This program is free software; you can redistribute it and/or modify it under
    1.10  the terms of the GNU General Public License as published by the Free Software
    1.11 @@ -202,6 +202,48 @@
    1.12      return __new_str(sub, resultsize);
    1.13  }
    1.14  
    1.15 +__attr __fn_native_unicode_unicode_unichr(__attr __args[])
    1.16 +{
    1.17 +    __attr * const value = &__args[1];
    1.18 +    /* value interpreted as int */
    1.19 +    int i = value->intvalue;
    1.20 +    unsigned int resultsize;
    1.21 +    char *s;
    1.22 +
    1.23 +    if (i < 128) resultsize = 1;
    1.24 +    else if (i < 2048) resultsize = 2;
    1.25 +    else if (i < 65536) resultsize = 3;
    1.26 +    else resultsize = 4;
    1.27 +
    1.28 +    /* Reserve space for a new string. */
    1.29 +
    1.30 +    s = (char *) __ALLOCATE(resultsize + 1, sizeof(char));
    1.31 +
    1.32 +    /* Populate the string. */
    1.33 +
    1.34 +    if (i < 128) s[0] = (char) i;
    1.35 +    else if (i < 2048)
    1.36 +    {
    1.37 +        s[0] = 0b11000000 | (i >> 6);
    1.38 +        s[1] = 0b10000000 | (i & 0b00111111);
    1.39 +    }
    1.40 +    else if (i < 65536)
    1.41 +    {
    1.42 +        s[0] = 0b11100000 | (i >> 12);
    1.43 +        s[1] = 0b10000000 | ((i >> 6) & 0b00111111);
    1.44 +        s[2] = 0b10000000 | (i & 0b00111111);
    1.45 +    }
    1.46 +    else
    1.47 +    {
    1.48 +        s[0] = 0b11110000 | (i >> 18);
    1.49 +        s[1] = 0b10000000 | ((i >> 12) & 0b00111111);
    1.50 +        s[2] = 0b10000000 | ((i >> 6) & 0b00111111);
    1.51 +        s[3] = 0b10000000 | (i & 0b00111111);
    1.52 +    }
    1.53 +
    1.54 +    return __new_str(s, resultsize);
    1.55 +}
    1.56 +
    1.57  /* Module initialisation. */
    1.58  
    1.59  void __main_native_unicode()