Lichen

Annotated templates/native/str.c

610:2c1ae8f292a9
2017-02-22 Paul Boddie Fixed the testing of attribute usage. method-wrapper-for-context
paul@354 1
/* Native functions for string operations.
paul@354 2
paul@531 3
Copyright (C) 2016, 2017 Paul Boddie <paul@boddie.org.uk>
paul@354 4
paul@354 5
This program is free software; you can redistribute it and/or modify it under
paul@354 6
the terms of the GNU General Public License as published by the Free Software
paul@354 7
Foundation; either version 3 of the License, or (at your option) any later
paul@354 8
version.
paul@354 9
paul@354 10
This program is distributed in the hope that it will be useful, but WITHOUT
paul@354 11
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
paul@354 12
FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
paul@354 13
details.
paul@354 14
paul@354 15
You should have received a copy of the GNU General Public License along with
paul@354 16
this program.  If not, see <http://www.gnu.org/licenses/>.
paul@354 17
*/
paul@354 18
paul@378 19
#include <string.h> /* strcmp, memcpy */
paul@354 20
#include "native/common.h"
paul@354 21
#include "types.h"
paul@354 22
#include "exceptions.h"
paul@354 23
#include "ops.h"
paul@354 24
#include "progconsts.h"
paul@354 25
#include "progops.h"
paul@354 26
#include "progtypes.h"
paul@354 27
#include "main.h"
paul@354 28
paul@354 29
/* String operations. */
paul@354 30
paul@354 31
__attr __fn_native_str_str_add(__attr __args[])
paul@354 32
{
paul@354 33
    __attr * const _data = &__args[1];
paul@354 34
    __attr * const other = &__args[2];
paul@583 35
    __attr * const _size = &__args[3];
paul@583 36
    __attr * const othersize = &__args[4];
paul@354 37
    /* _data, other interpreted as string */
paul@354 38
    char *s = _data->strvalue;
paul@354 39
    char *o = other->strvalue;
paul@583 40
    int ss = _size->intvalue, os = othersize->intvalue;
paul@569 41
    int n = ss + os;
paul@378 42
    char *r = (char *) __ALLOCATE(n + 1, sizeof(char));
paul@354 43
paul@569 44
    memcpy(r, s, ss);
paul@569 45
    memcpy(r + ss, o, os);
paul@354 46
paul@354 47
    /* Return a new string. */
paul@583 48
    return __new_str(r, n);
paul@354 49
}
paul@354 50
paul@531 51
__attr __fn_native_str_str_chr(__attr __args[])
paul@531 52
{
paul@531 53
    __attr * const _data = &__args[1];
paul@531 54
    /* _data interpreted as int */
paul@531 55
    int n = _data->intvalue;
paul@531 56
    char *s = (char *) __ALLOCATE(2, sizeof(char));
paul@531 57
paul@531 58
    s[0] = (char) n;
paul@583 59
    return __new_str(s, 1);
paul@531 60
}
paul@531 61
paul@354 62
__attr __fn_native_str_str_lt(__attr __args[])
paul@354 63
{
paul@354 64
    __attr * const _data = &__args[1];
paul@354 65
    __attr * const other = &__args[2];
paul@354 66
    /* _data, other interpreted as string */
paul@354 67
    char *s = _data->strvalue;
paul@354 68
    char *o = other->strvalue;
paul@354 69
paul@354 70
    /* NOTE: Using simple byte-level string operations. */
paul@354 71
    return strcmp(s, o) < 0 ? __builtins___boolean_True : __builtins___boolean_False;
paul@354 72
}
paul@354 73
paul@354 74
__attr __fn_native_str_str_gt(__attr __args[])
paul@354 75
{
paul@354 76
    __attr * const _data = &__args[1];
paul@354 77
    __attr * const other = &__args[2];
paul@354 78
    /* _data, other interpreted as string */
paul@354 79
    char *s = _data->strvalue;
paul@354 80
    char *o = other->strvalue;
paul@354 81
paul@354 82
    /* NOTE: Using simple byte-level string operations. */
paul@354 83
    return strcmp(s, o) > 0 ? __builtins___boolean_True : __builtins___boolean_False;
paul@354 84
}
paul@354 85
paul@354 86
__attr __fn_native_str_str_eq(__attr __args[])
paul@354 87
{
paul@354 88
    __attr * const _data = &__args[1];
paul@354 89
    __attr * const other = &__args[2];
paul@354 90
    /* _data, other interpreted as string */
paul@354 91
    char *s = _data->strvalue;
paul@354 92
    char *o = other->strvalue;
paul@354 93
paul@354 94
    /* NOTE: Using simple byte-level string operations. */
paul@354 95
    return strcmp(s, o) == 0 ? __builtins___boolean_True : __builtins___boolean_False;
paul@354 96
}
paul@354 97
paul@354 98
__attr __fn_native_str_str_ord(__attr __args[])
paul@354 99
{
paul@354 100
    __attr * const _data = &__args[1];
paul@354 101
    /* _data interpreted as string */
paul@354 102
    char *s = _data->strvalue;
paul@354 103
paul@354 104
    return __new_int((unsigned int) s[0]);
paul@354 105
}
paul@354 106
paul@354 107
__attr __fn_native_str_str_substr(__attr __args[])
paul@354 108
{
paul@354 109
    __attr * const _data = &__args[1];
paul@354 110
    __attr * const start = &__args[2];
paul@384 111
    __attr * const end = &__args[3];
paul@384 112
    __attr * const step = &__args[4];
paul@354 113
    /* _data interpreted as string */
paul@354 114
    char *s = _data->strvalue, *sub;
paul@354 115
    /* start.__data__ interpreted as int */
paul@384 116
    int istart = __load_via_object(start->value, __pos___data__).intvalue;
paul@384 117
    /* end.__data__ interpreted as int */
paul@384 118
    int iend = __load_via_object(end->value, __pos___data__).intvalue;
paul@384 119
    /* step.__data__ interpreted as int */
paul@384 120
    int istep = __load_via_object(step->value, __pos___data__).intvalue;
paul@384 121
paul@384 122
    /* Calculate the size of the substring. */
paul@388 123
    size_t resultsize = ((iend - istart - (istep > 0 ? 1 : -1)) / istep) + 1;
paul@384 124
    int to, from;
paul@354 125
paul@354 126
    /* Reserve space for a new string. */
paul@384 127
    sub = (char *) __ALLOCATE(resultsize + 1, sizeof(char));
paul@384 128
paul@384 129
    /* Does not null terminate but final byte should be zero. */
paul@384 130
    if (istep > 0)
paul@384 131
        for (from = istart, to = 0; from < iend; from += istep, to++)
paul@384 132
            sub[to] = s[from];
paul@384 133
    else if (istep < 0)
paul@384 134
        for (from = istart, to = 0; from > iend; from += istep, to++)
paul@384 135
            sub[to] = s[from];
paul@384 136
paul@583 137
    return __new_str(sub, resultsize);
paul@354 138
}
paul@354 139
paul@354 140
/* Module initialisation. */
paul@354 141
paul@354 142
void __main_native_str()
paul@354 143
{
paul@354 144
}