示例#1
0
文件: os.c 项目: amjith/chimp
static ChimpRef *
_chimp_os_dirname (ChimpRef *self, ChimpRef *args)
{
    ChimpRef *path = CHIMP_ARRAY_ITEM(args, 0);
    size_t i;
    const char *begin = CHIMP_STR_DATA(path);
    const char *end = begin + CHIMP_STR_SIZE(path);

    for (i = 0; i < CHIMP_STR_SIZE(path); i++) {
        end--;
        if (*end == '/') {
            return chimp_str_new (begin, (size_t) (end - begin));
        }
    }
    return CHIMP_STR_NEW ("");
}
示例#2
0
文件: os.c 项目: amjith/chimp
static ChimpRef *
_chimp_os_basename (ChimpRef *self, ChimpRef *args)
{
    ChimpRef *path = CHIMP_ARRAY_ITEM(args, 0);
    size_t len = CHIMP_STR_SIZE(path);
    const char *s = CHIMP_STR_DATA(path) + len;

    if (len > 0) len--;
    while (len > 0) {
        if (*s == '/') {
            s++;
            break;
        }
        s--;
        len--;
    }

    return chimp_str_new (s, CHIMP_STR_SIZE(path) - len);
}
示例#3
0
文件: hash.c 项目: eax/chimp
static ChimpRef *
chimp_hash_str (ChimpRef *self)
{
    size_t size = CHIMP_HASH_SIZE(self);
    /* '{' + '}' + (': ' x size) + (', ' x (size-1)) + '\0' */
    size_t total_len = 2 + (size * 2) + (size > 0 ? ((size-1) * 2) : 0) + 1;
    ChimpRef *ref;
    ChimpRef *strs;
    char *data;
    size_t i, k;


    strs = chimp_array_new_with_capacity (size);
    if (strs == NULL) {
        return NULL;
    }

    for (i = 0; i < size; i++) {
        size_t j;
        ChimpRef *item[2];
        item[0] = CHIMP_HASH(self)->keys[i];
        item[1] = CHIMP_HASH(self)->values[i];
        for (j = 0; j < 2; j++) {
            ref = item[j];
            /* XXX what we really want is something like Python's repr() */
            if (CHIMP_ANY_CLASS(ref) == chimp_str_class) {
                /* for surrounding quotes */
                total_len += 2;
            }
            ref = chimp_object_str (ref);
            if (ref == NULL) {
                return NULL;
            }
            chimp_array_push (strs, ref);
            total_len += CHIMP_STR_SIZE(ref);
        }
    }

    data = CHIMP_MALLOC(char, total_len);
    if (data == NULL) {
        return NULL;
    }
    k = 0;
    data[k++] = '{';

    for (i = 0; i < size; i++) {
        size_t j;
        ChimpRef *item[2];
        item[0] = CHIMP_HASH(self)->keys[i];
        item[1] = CHIMP_HASH(self)->values[i];
        for (j = 0; j < 2; j++) {
            ref = CHIMP_ARRAY_ITEM(strs, (i * 2) + j);
            /* XXX what we really want is something like Python's repr() */
            /* TODO instanceof */
            if (CHIMP_ANY_CLASS(item[j]) == chimp_str_class) {
                data[k++] = '"';
            }
            memcpy (data + k, CHIMP_STR_DATA(ref), CHIMP_STR_SIZE(ref));
            k += CHIMP_STR_SIZE(ref);
            if (CHIMP_ANY_CLASS(item[j]) == chimp_str_class) {
                data[k++] = '"';
            }
            if (j == 0) {
                data[k++] = ':';
                data[k++] = ' ';
            }
        }
        if (i < (size-1)) {
            data[k++] = ',';
            data[k++] = ' ';
        }
    }

    data[k++] = '}';
    data[k] = '\0';

    return chimp_str_new_take (data, total_len-1);
}